Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 81 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
CleanupCaps | |
0.00% |
0 / 78 |
|
0.00% |
0 / 5 |
342 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
processRowToUppercase | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
42 | |||
processRowToLowercase | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
42 | |||
movePage | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
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 Brooke Vibber <bvibber@wikimedia.org> |
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 Brooke Vibber <bvibber@wikimedia.org> |
29 | * @ingroup Maintenance |
30 | */ |
31 | |
32 | use MediaWiki\Title\Title; |
33 | use MediaWiki\User\User; |
34 | |
35 | require_once __DIR__ . '/TableCleanup.php'; |
36 | |
37 | /** |
38 | * Maintenance script to clean up broken page links when somebody turns |
39 | * on or off $wgCapitalLinks. |
40 | * |
41 | * @ingroup Maintenance |
42 | */ |
43 | class CleanupCaps extends TableCleanup { |
44 | |
45 | private $user; |
46 | private $namespace; |
47 | |
48 | public function __construct() { |
49 | parent::__construct(); |
50 | $this->addDescription( 'Script to cleanup capitalization' ); |
51 | $this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true ); |
52 | } |
53 | |
54 | public function execute() { |
55 | $this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] ); |
56 | |
57 | $this->namespace = intval( $this->getOption( 'namespace', 0 ) ); |
58 | |
59 | if ( |
60 | $this->getServiceContainer()->getNamespaceInfo()-> |
61 | isCapitalized( $this->namespace ) |
62 | ) { |
63 | $this->output( "Will be moving pages to first letter capitalized titles" ); |
64 | $callback = 'processRowToUppercase'; |
65 | } else { |
66 | $this->output( "Will be moving pages to first letter lowercase titles" ); |
67 | $callback = 'processRowToLowercase'; |
68 | } |
69 | |
70 | $this->dryrun = $this->hasOption( 'dry-run' ); |
71 | |
72 | $this->runTable( [ |
73 | 'table' => 'page', |
74 | 'conds' => [ 'page_namespace' => $this->namespace ], |
75 | 'index' => 'page_id', |
76 | 'callback' => $callback ] ); |
77 | } |
78 | |
79 | protected function processRowToUppercase( $row ) { |
80 | $current = Title::makeTitle( $row->page_namespace, $row->page_title ); |
81 | $display = $current->getPrefixedText(); |
82 | $lower = $row->page_title; |
83 | $upper = $this->getServiceContainer()->getContentLanguage()->ucfirst( $row->page_title ); |
84 | if ( $upper == $lower ) { |
85 | $this->output( "\"$display\" already uppercase.\n" ); |
86 | |
87 | $this->progress( 0 ); |
88 | return; |
89 | } |
90 | |
91 | $target = Title::makeTitle( $row->page_namespace, $upper ); |
92 | if ( $target->exists() ) { |
93 | // Prefix "CapsCleanup" to bypass the conflict |
94 | $target = Title::newFromText( 'CapsCleanup/' . $display ); |
95 | } |
96 | $ok = $this->movePage( |
97 | $current, |
98 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable target is always valid |
99 | $target, |
100 | 'Converting page title to first-letter uppercase', |
101 | false |
102 | ); |
103 | if ( $ok ) { |
104 | $this->progress( 1 ); |
105 | if ( $row->page_namespace == $this->namespace ) { |
106 | $talk = $target->getTalkPage(); |
107 | $row->page_namespace = $talk->getNamespace(); |
108 | if ( $talk->exists() ) { |
109 | $this->processRowToUppercase( $row ); |
110 | return; |
111 | } |
112 | } |
113 | } |
114 | |
115 | $this->progress( 0 ); |
116 | } |
117 | |
118 | protected function processRowToLowercase( $row ) { |
119 | $current = Title::makeTitle( $row->page_namespace, $row->page_title ); |
120 | $display = $current->getPrefixedText(); |
121 | $upper = $row->page_title; |
122 | $lower = $this->getServiceContainer()->getContentLanguage()->lcfirst( $row->page_title ); |
123 | if ( $upper == $lower ) { |
124 | $this->output( "\"$display\" already lowercase.\n" ); |
125 | |
126 | $this->progress( 0 ); |
127 | return; |
128 | } |
129 | |
130 | $target = Title::makeTitle( $row->page_namespace, $lower ); |
131 | if ( $target->exists() ) { |
132 | $targetDisplay = $target->getPrefixedText(); |
133 | $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" ); |
134 | |
135 | $this->progress( 0 ); |
136 | return; |
137 | } |
138 | |
139 | $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true ); |
140 | if ( $ok === true ) { |
141 | $this->progress( 1 ); |
142 | if ( $row->page_namespace == $this->namespace ) { |
143 | $talk = $target->getTalkPage(); |
144 | $row->page_namespace = $talk->getNamespace(); |
145 | if ( $talk->exists() ) { |
146 | $this->processRowToLowercase( $row ); |
147 | return; |
148 | } |
149 | } |
150 | } |
151 | |
152 | $this->progress( 0 ); |
153 | } |
154 | |
155 | /** |
156 | * @param Title $current |
157 | * @param Title $target |
158 | * @param string $reason |
159 | * @param bool $createRedirect |
160 | * @return bool Success |
161 | */ |
162 | private function movePage( Title $current, Title $target, $reason, $createRedirect ) { |
163 | $display = $current->getPrefixedText(); |
164 | $targetDisplay = $target->getPrefixedText(); |
165 | |
166 | if ( $this->dryrun ) { |
167 | $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" ); |
168 | $ok = 'OK'; |
169 | } else { |
170 | $mp = $this->getServiceContainer()->getMovePageFactory() |
171 | ->newMovePage( $current, $target ); |
172 | $status = $mp->move( $this->user, $reason, $createRedirect ); |
173 | $ok = $status->isOK() ? 'OK' : $status->getMessage( false, false, 'en' )->text(); |
174 | $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" ); |
175 | } |
176 | |
177 | return $ok === 'OK'; |
178 | } |
179 | } |
180 | |
181 | $maintClass = CleanupCaps::class; |
182 | require_once RUN_MAINTENANCE_IF_MAIN; |