PopulateBacklinkNamespace   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getUpdateKey() 0 3 1
A updateSkippedMessage() 0 3 1
B doDBUpdates() 0 48 5
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 31 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
 * Optional upgrade script to populate *_from_namespace 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 populate *_from_namespace fields
28
 *
29
 * @ingroup Maintenance
30
 */
31
class PopulateBacklinkNamespace extends LoggedUpdateMaintenance {
32
	public function __construct() {
33
		parent::__construct();
34
		$this->addDescription( 'Populate the *_from_namespace fields' );
35
		$this->addOption( 'lastUpdatedId', "Highest page_id with updated links", false, true );
36
	}
37
38
	protected function getUpdateKey() {
39
		return 'populate *_from_namespace';
40
	}
41
42
	protected function updateSkippedMessage() {
43
		return '*_from_namespace column of backlink tables already populated.';
44
	}
45
46
	public function doDBUpdates() {
47
		$force = $this->getOption( 'force' );
0 ignored issues
show
Unused Code introduced by
$force is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
49
		$db = $this->getDB( DB_MASTER );
50
51
		$this->output( "Updating *_from_namespace fields in links tables.\n" );
52
53
		$start = $this->getOption( 'lastUpdatedId' );
54
		if ( !$start ) {
55
			$start = $db->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
56
		}
57
		if ( !$start ) {
58
			$this->output( "Nothing to do." );
59
			return false;
60
		}
61
		$end = $db->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
62
63
		# Do remaining chunk
64
		$end += $this->mBatchSize - 1;
65
		$blockStart = $start;
66
		$blockEnd = $start + $this->mBatchSize - 1;
67
		while ( $blockEnd <= $end ) {
68
			$this->output( "...doing page_id from $blockStart to $blockEnd\n" );
69
			$cond = "page_id BETWEEN $blockStart AND $blockEnd";
70
			$res = $db->select( 'page', [ 'page_id', 'page_namespace' ], $cond, __METHOD__ );
71
			foreach ( $res as $row ) {
72
				$db->update( 'pagelinks',
73
					[ 'pl_from_namespace' => $row->page_namespace ],
74
					[ 'pl_from' => $row->page_id ],
75
					__METHOD__
76
				);
77
				$db->update( 'templatelinks',
78
					[ 'tl_from_namespace' => $row->page_namespace ],
79
					[ 'tl_from' => $row->page_id ],
80
					__METHOD__
81
				);
82
				$db->update( 'imagelinks',
83
					[ 'il_from_namespace' => $row->page_namespace ],
84
					[ 'il_from' => $row->page_id ],
85
					__METHOD__
86
				);
87
			}
88
			$blockStart += $this->mBatchSize - 1;
89
			$blockEnd += $this->mBatchSize - 1;
90
			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...
91
		}
92
		return true;
93
	}
94
}
95
96
$maintClass = "PopulateBacklinkNamespace";
97
require_once RUN_MAINTENANCE_IF_MAIN;
98