UpdateRestrictions::execute()   D
last analyzed

Complexity

Conditions 13
Paths 208

Size

Total Lines 83
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 58
nc 208
nop 0
dl 0
loc 83
rs 4.5186
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 35 and the first side effect is on line 27.

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
 * Makes the required database updates for Special:ProtectedPages
4
 * to show all protected pages, even ones before the page restrictions
5
 * schema change. All remaining page_restriction column values are moved
6
 * to the new table.
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License along
19
 * with this program; if not, write to the Free Software Foundation, Inc.,
20
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
 * http://www.gnu.org/copyleft/gpl.html
22
 *
23
 * @file
24
 * @ingroup Maintenance
25
 */
26
27
require_once __DIR__ . '/Maintenance.php';
28
29
/**
30
 * Maintenance script that updates page_restrictions table from
31
 * old page_restriction column.
32
 *
33
 * @ingroup Maintenance
34
 */
35
class UpdateRestrictions extends Maintenance {
36
	public function __construct() {
37
		parent::__construct();
38
		$this->addDescription( 'Updates page_restrictions table from old page_restriction column' );
39
		$this->setBatchSize( 100 );
40
	}
41
42
	public function execute() {
43
		$db = $this->getDB( DB_MASTER );
44
		if ( !$db->tableExists( 'page_restrictions' ) ) {
45
			$this->error( "page_restrictions table does not exist", true );
46
		}
47
48
		$start = $db->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
49
		if ( !$start ) {
50
			$this->error( "Nothing to do.", true );
51
		}
52
		$end = $db->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
53
54
		# Do remaining chunk
55
		$end += $this->mBatchSize - 1;
56
		$blockStart = $start;
57
		$blockEnd = $start + $this->mBatchSize - 1;
58
		$encodedExpiry = 'infinity';
59
		while ( $blockEnd <= $end ) {
60
			$this->output( "...doing page_id from $blockStart to $blockEnd\n" );
61
			$cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_restrictions !=''";
62
			$res = $db->select(
63
				'page',
64
				[ 'page_id', 'page_namespace', 'page_restrictions' ],
65
				$cond,
66
				__METHOD__
67
			);
68
			$batch = [];
69
			foreach ( $res as $row ) {
70
				$oldRestrictions = [];
71
				foreach ( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
72
					$temp = explode( '=', trim( $restrict ) );
73
					// Make sure we are not settings restrictions to ""
74
					if ( count( $temp ) == 1 && $temp[0] ) {
75
						// old old format should be treated as edit/move restriction
76
						$oldRestrictions["edit"] = trim( $temp[0] );
77
						$oldRestrictions["move"] = trim( $temp[0] );
78
					} elseif ( $temp[1] ) {
79
						$oldRestrictions[$temp[0]] = trim( $temp[1] );
80
					}
81
				}
82
				# Clear invalid columns
83
				if ( $row->page_namespace == NS_MEDIAWIKI ) {
84
					$db->update( 'page', [ 'page_restrictions' => '' ],
85
						[ 'page_id' => $row->page_id ], __FUNCTION__ );
86
					$this->output( "...removed dead page_restrictions column for page {$row->page_id}\n" );
87
				}
88
				# Update restrictions table
89
				foreach ( $oldRestrictions as $action => $restrictions ) {
90
					$batch[] = [
91
						'pr_page' => $row->page_id,
92
						'pr_type' => $action,
93
						'pr_level' => $restrictions,
94
						'pr_cascade' => 0,
95
						'pr_expiry' => $encodedExpiry
96
					];
97
				}
98
			}
99
			# We use insert() and not replace() as Article.php replaces
100
			# page_restrictions with '' when protected in the restrictions table
101
			if ( count( $batch ) ) {
102
				$ok = $db->deadlockLoop( [ $db, 'insert' ], 'page_restrictions',
103
					$batch, __FUNCTION__, [ 'IGNORE' ] );
104
				if ( !$ok ) {
105
					throw new MWException( "Deadlock loop failed wtf :(" );
106
				}
107
			}
108
			$blockStart += $this->mBatchSize - 1;
109
			$blockEnd += $this->mBatchSize - 1;
110
			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...
111
		}
112
		$this->output( "...removing dead rows from page_restrictions\n" );
113
		// Kill any broken rows from previous imports
114
		$db->delete( 'page_restrictions', [ 'pr_level' => '' ] );
115
		// Kill other invalid rows
116
		$db->deleteJoin(
117
			'page_restrictions',
118
			'page',
119
			'pr_page',
120
			'page_id',
121
			[ 'page_namespace' => NS_MEDIAWIKI ]
122
		);
123
		$this->output( "...Done!\n" );
124
	}
125
}
126
127
$maintClass = "UpdateRestrictions";
128
require_once RUN_MAINTENANCE_IF_MAIN;
129