CleanupAncientTables   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 16
loc 80
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C execute() 16 71 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Cleans up old database tables, dropping old indexes and fields.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 * http://www.gnu.org/copyleft/gpl.html
19
 *
20
 * @file
21
 * @ingroup Maintenance
22
 */
23
24
require_once __DIR__ . '/Maintenance.php';
25
26
/**
27
 * Maintenance script to cleans up old database tables, dropping old indexes
28
 * and fields.
29
 *
30
 * @ingroup Maintenance
31
 */
32
class CleanupAncientTables extends Maintenance {
33
34
	public function __construct() {
35
		parent::__construct();
36
		$this->addDescription( 'Cleanup ancient tables and indexes' );
37
		$this->addOption( 'force', 'Actually run this script' );
38
	}
39
40
	public function execute() {
41
		if ( !$this->hasOption( 'force' ) ) {
42
			$this->error( "This maintenance script will remove old columns and indexes.\n"
43
				. "It is recommended to backup your database first, and ensure all your data has\n"
44
				. "been migrated to newer tables. If you want to continue, run this script again\n"
45
				. "with --force.\n"
46
			);
47
		}
48
49
		$db = $this->getDB( DB_MASTER );
50
		$ancientTables = [
51
			'blobs', // 1.4
52
			'brokenlinks', // 1.4
53
			'cur', // 1.4
54
			'ip_blocks_old', // Temporary in 1.6
55
			'links', // 1.4
56
			'linkscc', // 1.4
57
			// 'math', // 1.18, but don't want to drop if math extension is enabled...
58
			'old', // 1.4
59
			'oldwatchlist', // pre 1.1?
60
			'trackback', // 1.19
61
			'user_rights', // 1.5
62
			'validate', // 1.6
63
		];
64
65
		foreach ( $ancientTables as $table ) {
66
			if ( $db->tableExists( $table, __METHOD__ ) ) {
67
				$this->output( "Dropping table $table..." );
68
				$db->dropTable( $table, __METHOD__ );
69
				$this->output( "done.\n" );
70
			}
71
		}
72
73
		$this->output( "Cleaning up text table\n" );
74
75
		$oldIndexes = [
76
			'old_namespace',
77
			'old_timestamp',
78
			'name_title_timestamp',
79
			'user_timestamp',
80
			'usertext_timestamp',
81
		];
82 View Code Duplication
		foreach ( $oldIndexes as $index ) {
83
			if ( $db->indexExists( 'text', $index, __METHOD__ ) ) {
84
				$this->output( "Dropping index $index from the text table..." );
85
				$db->query( "DROP INDEX " . $db->addIdentifierQuotes( $index )
86
					. " ON " . $db->tableName( 'text' ) );
87
				$this->output( "done.\n" );
88
			}
89
		}
90
91
		$oldFields = [
92
			'old_namespace',
93
			'old_title',
94
			'old_comment',
95
			'old_user',
96
			'old_user_text',
97
			'old_timestamp',
98
			'old_minor_edit',
99
			'inverse_timestamp',
100
		];
101 View Code Duplication
		foreach ( $oldFields as $field ) {
102
			if ( $db->fieldExists( 'text', $field, __METHOD__ ) ) {
103
				$this->output( "Dropping the $field field from the text table..." );
104
				$db->query( "ALTER TABLE  " . $db->tableName( 'text' )
105
					. " DROP COLUMN " . $db->addIdentifierQuotes( $field ) );
106
				$this->output( "done.\n" );
107
			}
108
		}
109
		$this->output( "Done!\n" );
110
	}
111
}
112
113
$maintClass = "CleanupAncientTables";
114
require_once RUN_MAINTENANCE_IF_MAIN;
115