PopulateCategory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 15.83 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 19
loc 120
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 34 1
C execute() 19 80 9

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 25.

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
 * Populate the category table.
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
 * @author Simetrical
23
 */
24
25
require_once __DIR__ . '/Maintenance.php';
26
27
/**
28
 * Maintenance script to populate the category table.
29
 *
30
 * @ingroup Maintenance
31
 */
32
class PopulateCategory extends Maintenance {
33
34
	const REPORTING_INTERVAL = 1000;
35
36
	public function __construct() {
37
		parent::__construct();
38
		$this->addDescription(
39
			<<<TEXT
40
This script will populate the category table, added in MediaWiki 1.13.  It will
41
print out progress indicators every 1000 categories it adds to the table.  The
42
script is perfectly safe to run on large, live wikis, and running it multiple
43
times is harmless.  You may want to use the throttling options if it's causing
44
too much load; they will not affect correctness.
45
46
If the script is stopped and later resumed, you can use the --begin option with
47
the last printed progress indicator to pick up where you left off.  This is
48
safe, because any newly-added categories before this cutoff will have been
49
added after the software update and so will be populated anyway.
50
51
When the script has finished, it will make a note of this in the database, and
52
will not run again without the --force option.
53
TEXT
54
		);
55
56
		$this->addOption(
57
			'begin',
58
			'Only do categories whose names are alphabetically after the provided name',
59
			false,
60
			true
61
		);
62
		$this->addOption(
63
			'throttle',
64
			'Wait this many milliseconds after each category. Default: 0',
65
			false,
66
			true
67
		);
68
		$this->addOption( 'force', 'Run regardless of whether the database says it\'s been run already' );
69
	}
70
71
	public function execute() {
72
		$begin = $this->getOption( 'begin', '' );
73
		$throttle = $this->getOption( 'throttle', 0 );
74
		$force = $this->getOption( 'force', false );
75
76
		$dbw = $this->getDB( DB_MASTER );
77
78
		if ( !$force ) {
79
			$row = $dbw->selectRow(
80
				'updatelog',
81
				'1',
82
				[ 'ul_key' => 'populate category' ],
83
				__METHOD__
84
			);
85
			if ( $row ) {
86
				$this->output( "Category table already populated.  Use php " .
87
					"maintenance/populateCategory.php\n--force from the command line " .
88
					"to override.\n" );
89
90
				return true;
91
			}
92
		}
93
94
		$throttle = intval( $throttle );
95 View Code Duplication
		if ( $begin !== '' ) {
96
			$where = 'cl_to > ' . $dbw->addQuotes( $begin );
97
		} else {
98
			$where = null;
99
		}
100
		$i = 0;
101
102
		while ( true ) {
103
			# Find which category to update
104
			$row = $dbw->selectRow(
105
				'categorylinks',
106
				'cl_to',
107
				$where,
108
				__METHOD__,
109
				[
110
					'ORDER BY' => 'cl_to'
111
				]
112
			);
113
			if ( !$row ) {
114
				# Done, hopefully.
115
				break;
116
			}
117
			$name = $row->cl_to;
118
			$where = 'cl_to > ' . $dbw->addQuotes( $name );
119
120
			# Use the row to update the category count
121
			$cat = Category::newFromName( $name );
122
			if ( !is_object( $cat ) ) {
123
				$this->output( "The category named $name is not valid?!\n" );
124
			} else {
125
				$cat->refreshCounts();
126
			}
127
128
			++$i;
129
			if ( !( $i % self::REPORTING_INTERVAL ) ) {
130
				$this->output( "$name\n" );
131
				wfWaitForSlaves();
0 ignored issues
show
Deprecated Code introduced by
The function wfWaitForSlaves() has been deprecated with message: since 1.27 Use LBFactory::waitForReplication

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
132
			}
133
			usleep( $throttle * 1000 );
134
		}
135
136 View Code Duplication
		if ( $dbw->insert(
137
			'updatelog',
138
			[ 'ul_key' => 'populate category' ],
139
			__METHOD__,
140
			'IGNORE'
141
		) ) {
142
			$this->output( "Category population complete.\n" );
143
144
			return true;
145
		} else {
146
			$this->output( "Could not insert category population row.\n" );
147
148
			return false;
149
		}
150
	}
151
}
152
153
$maintClass = "PopulateCategory";
154
require_once RUN_MAINTENANCE_IF_MAIN;
155