CapsCleanup::movePage()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
c 0
b 0
f 0
nc 3
nop 4
dl 0
loc 16
rs 9.4285
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 40 and the first side effect is on line 32.

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
 * Clean up broken page links when somebody turns on $wgCapitalLinks.
4
 *
5
 * Usage: php cleanupCaps.php [--dry-run]
6
 * Options:
7
 *   --dry-run  don't actually try moving them
8
 *
9
 * Copyright © 2005 Brion Vibber <[email protected]>
10
 * https://www.mediawiki.org/
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License along
23
 * with this program; if not, write to the Free Software Foundation, Inc.,
24
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25
 * http://www.gnu.org/copyleft/gpl.html
26
 *
27
 * @file
28
 * @author Brion Vibber <brion at pobox.com>
29
 * @ingroup Maintenance
30
 */
31
32
require_once __DIR__ . '/cleanupTable.inc';
33
34
/**
35
 * Maintenance script to clean up broken page links when somebody turns
36
 * on or off $wgCapitalLinks.
37
 *
38
 * @ingroup Maintenance
39
 */
40
class CapsCleanup extends TableCleanup {
41
42
	private $user;
43
	private $namespace;
44
45
	public function __construct() {
46
		parent::__construct();
47
		$this->addDescription( 'Script to cleanup capitalization' );
48
		$this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
49
	}
50
51
	public function execute() {
52
		$this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
53
54
		$this->namespace = intval( $this->getOption( 'namespace', 0 ) );
55
56
		if ( MWNamespace::isCapitalized( $this->namespace ) ) {
57
			$this->output( "Will be moving pages to first letter capitalized titles" );
58
			$callback = 'processRowToUppercase';
59
		} else {
60
			$this->output( "Will be moving pages to first letter lowercase titles" );
61
			$callback = 'processRowToLowercase';
62
		}
63
64
		$this->dryrun = $this->hasOption( 'dry-run' );
65
66
		$this->runTable( [
67
			'table' => 'page',
68
			'conds' => [ 'page_namespace' => $this->namespace ],
69
			'index' => 'page_id',
70
			'callback' => $callback ] );
71
	}
72
73
	protected function processRowToUppercase( $row ) {
74
		global $wgContLang;
75
76
		$current = Title::makeTitle( $row->page_namespace, $row->page_title );
77
		$display = $current->getPrefixedText();
78
		$lower = $row->page_title;
79
		$upper = $wgContLang->ucfirst( $row->page_title );
80
		if ( $upper == $lower ) {
81
			$this->output( "\"$display\" already uppercase.\n" );
82
83
			return $this->progress( 0 );
84
		}
85
86
		$target = Title::makeTitle( $row->page_namespace, $upper );
87
		if ( $target->exists() ) {
88
			// Prefix "CapsCleanup" to bypass the conflict
89
			$target = Title::newFromText( __CLASS__ . '/' . $display );
90
		}
91
		$ok = $this->movePage(
92
			$current,
93
			$target,
0 ignored issues
show
Bug introduced by
It seems like $target defined by \Title::newFromText(__CLASS__ . '/' . $display) on line 89 can be null; however, CapsCleanup::movePage() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
94
			'Converting page title to first-letter uppercase',
95
			false
96
		);
97 View Code Duplication
		if ( $ok ) {
98
			$this->progress( 1 );
99
			if ( $row->page_namespace == $this->namespace ) {
100
				$talk = $target->getTalkPage();
101
				$row->page_namespace = $talk->getNamespace();
102
				if ( $talk->exists() ) {
103
					return $this->processRowToUppercase( $row );
104
				}
105
			}
106
		}
107
108
		return $this->progress( 0 );
109
	}
110
111
	protected function processRowToLowercase( $row ) {
112
		global $wgContLang;
113
114
		$current = Title::makeTitle( $row->page_namespace, $row->page_title );
115
		$display = $current->getPrefixedText();
116
		$upper = $row->page_title;
117
		$lower = $wgContLang->lcfirst( $row->page_title );
118
		if ( $upper == $lower ) {
119
			$this->output( "\"$display\" already lowercase.\n" );
120
121
			return $this->progress( 0 );
122
		}
123
124
		$target = Title::makeTitle( $row->page_namespace, $lower );
125
		if ( $target->exists() ) {
126
			$targetDisplay = $target->getPrefixedText();
127
			$this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
128
129
			return $this->progress( 0 );
130
		}
131
132
		$ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
133 View Code Duplication
		if ( $ok === true ) {
134
			$this->progress( 1 );
135
			if ( $row->page_namespace == $this->namespace ) {
136
				$talk = $target->getTalkPage();
137
				$row->page_namespace = $talk->getNamespace();
138
				if ( $talk->exists() ) {
139
					return $this->processRowToLowercase( $row );
140
				}
141
			}
142
		}
143
144
		return $this->progress( 0 );
145
	}
146
147
	/**
148
	 * @param Title $current
149
	 * @param Title $target
150
	 * @param string $reason
151
	 * @param bool $createRedirect
152
	 * @return bool Success
153
	 */
154
	private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
155
		$display = $current->getPrefixedText();
156
		$targetDisplay = $target->getPrefixedText();
157
158
		if ( $this->dryrun ) {
159
			$this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
160
			$ok = 'OK';
161
		} else {
162
			$mp = new MovePage( $current, $target );
163
			$status = $mp->move( $this->user, $reason, $createRedirect );
0 ignored issues
show
Bug introduced by
It seems like $this->user can be null; however, move() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
164
			$ok = $status->isOK() ? 'OK' : $status->getWikiText( false, false, 'en' );
165
			$this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
166
		}
167
168
		return $ok === 'OK';
169
	}
170
}
171
172
$maintClass = "CapsCleanup";
173
require_once RUN_MAINTENANCE_IF_MAIN;
174