Merge "HtmlFormatter::filterContent() should always return an array"
[lhc/web/wiklou.git] / maintenance / namespaceDupes.php
1 <?php
2 /**
3 * Check for articles to fix after adding/deleting namespaces
4 *
5 * Copyright © 2005-2007 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
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 checks for articles to fix after
31 * adding/deleting namespaces.
32 *
33 * @ingroup Maintenance
34 */
35 class NamespaceConflictChecker extends Maintenance {
36
37 /**
38 * @var DatabaseBase
39 */
40 protected $db;
41
42 public function __construct() {
43 parent::__construct();
44 $this->mDescription = "";
45 $this->addOption( 'fix', 'Attempt to automatically fix errors' );
46 $this->addOption( 'suffix', "Dupes will be renamed with correct namespace with " .
47 "<text> appended after the article name", false, true );
48 $this->addOption( 'prefix', "Do an explicit check for the given title prefix " .
49 "appended after the article name", false, true );
50 }
51
52 public function execute() {
53 $this->db = wfGetDB( DB_MASTER );
54
55 $fix = $this->hasOption( 'fix' );
56 $suffix = $this->getOption( 'suffix', '' );
57 $prefix = $this->getOption( 'prefix', '' );
58 $key = intval( $this->getOption( 'key', 0 ) );
59
60 if ( $prefix ) {
61 $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix );
62 } else {
63 $retval = $this->checkAll( $fix, $suffix );
64 }
65
66 if ( $retval ) {
67 $this->output( "\nLooks good!\n" );
68 } else {
69 $this->output( "\nOh noeees\n" );
70 }
71 }
72
73 /**
74 * @todo Document
75 * @param bool $fix Whether or not to fix broken entries
76 * @param string $suffix Suffix to append to renamed articles
77 *
78 * @return bool
79 */
80 private function checkAll( $fix, $suffix = '' ) {
81 global $wgContLang, $wgNamespaceAliases, $wgCapitalLinks;
82
83 $spaces = array();
84
85 // List interwikis first, so they'll be overridden
86 // by any conflicting local namespaces.
87 foreach ( $this->getInterwikiList() as $prefix ) {
88 $name = $wgContLang->ucfirst( $prefix );
89 $spaces[$name] = 0;
90 }
91
92 // Now pull in all canonical and alias namespaces...
93 foreach ( MWNamespace::getCanonicalNamespaces() as $ns => $name ) {
94 // This includes $wgExtraNamespaces
95 if ( $name !== '' ) {
96 $spaces[$name] = $ns;
97 }
98 }
99 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
100 if ( $name !== '' ) {
101 $spaces[$name] = $ns;
102 }
103 }
104 foreach ( $wgNamespaceAliases as $name => $ns ) {
105 $spaces[$name] = $ns;
106 }
107 foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) {
108 $spaces[$name] = $ns;
109 }
110
111 // We'll need to check for lowercase keys as well,
112 // since we're doing case-sensitive searches in the db.
113 foreach ( $spaces as $name => $ns ) {
114 $moreNames = array();
115 $moreNames[] = $wgContLang->uc( $name );
116 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
117 $moreNames[] = $wgContLang->ucwords( $name );
118 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
119 $moreNames[] = $wgContLang->ucwordbreaks( $name );
120 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
121 if ( !$wgCapitalLinks ) {
122 foreach ( $moreNames as $altName ) {
123 $moreNames[] = $wgContLang->lcfirst( $altName );
124 }
125 $moreNames[] = $wgContLang->lcfirst( $name );
126 }
127 foreach ( array_unique( $moreNames ) as $altName ) {
128 if ( $altName !== $name ) {
129 $spaces[$altName] = $ns;
130 }
131 }
132 }
133
134 ksort( $spaces );
135 asort( $spaces );
136
137 $ok = true;
138 foreach ( $spaces as $name => $ns ) {
139 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
140 }
141
142 return $ok;
143 }
144
145 /**
146 * Get the interwiki list
147 *
148 * @return array
149 */
150 private function getInterwikiList() {
151 $result = Interwiki::getAllPrefixes();
152 $prefixes = array();
153 foreach ( $result as $row ) {
154 $prefixes[] = $row['iw_prefix'];
155 }
156
157 return $prefixes;
158 }
159
160 /**
161 * @todo Document
162 * @param int $ns A namespace id
163 * @param string $name
164 * @param bool $fix Whether to fix broken entries
165 * @param string $suffix Suffix to append to renamed articles
166 * @return bool
167 */
168 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
169 $conflicts = $this->getConflicts( $ns, $name );
170 $count = count( $conflicts );
171 if ( $count == 0 ) {
172 return true;
173 }
174
175 $ok = true;
176 foreach ( $conflicts as $row ) {
177 $resolvable = $this->reportConflict( $row, $suffix );
178 $ok = $ok && $resolvable;
179 if ( $fix && ( $resolvable || $suffix != '' ) ) {
180 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
181 }
182 }
183
184 return $ok;
185 }
186
187 /**
188 * @todo Do this for real
189 * @param int $ns
190 * @param string $name
191 * @param bool $fix
192 * @param string $suffix
193 * @return bool
194 */
195 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
196 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
197
198 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
199 }
200
201 /**
202 * Find pages in mainspace that have a prefix of the new namespace
203 * so we know titles that will need migrating
204 *
205 * @param int $ns Namespace id (id for new namespace?)
206 * @param string $name Prefix that is being made a namespace
207 *
208 * @return array
209 */
210 private function getConflicts( $ns, $name ) {
211 $page = 'page';
212 $table = $this->db->tableName( $page );
213
214 $prefix = $this->db->strencode( $name );
215 $encNamespace = $this->db->addQuotes( $ns );
216
217 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
218 if ( $ns == 0 ) {
219 // An interwiki; try an alternate encoding with '-' for ':'
220 $titleSql = $this->db->buildConcat( array( "'$prefix-'", $titleSql ) );
221 }
222
223 $sql = "SELECT {$page}_id AS id,
224 {$page}_title AS oldtitle,
225 $encNamespace + {$page}_namespace AS namespace,
226 $titleSql AS title,
227 {$page}_namespace AS oldnamespace
228 FROM {$table}
229 WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 )
230 AND {$page}_title " . $this->db->buildLike( $name . ':', $this->db->anyString() );
231
232 $result = $this->db->query( $sql, __METHOD__ );
233
234 $set = array();
235 foreach ( $result as $row ) {
236 $set[] = $row;
237 }
238
239 return $set;
240 }
241
242 /**
243 * Report any conflicts we find
244 *
245 * @param stdClass $row
246 * @param string $suffix
247 * @return bool
248 */
249 private function reportConflict( $row, $suffix ) {
250 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
251 if ( is_null( $newTitle ) || !$newTitle->canExist() ) {
252 // Title is also an illegal title...
253 // For the moment we'll let these slide to cleanupTitles or whoever.
254 $this->output( sprintf( "... %d (%d,\"%s\")\n",
255 $row->id,
256 $row->oldnamespace,
257 $row->oldtitle ) );
258 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
259
260 return false;
261 }
262
263 $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
264 $row->id,
265 $row->oldnamespace,
266 $row->oldtitle,
267 $newTitle->getNamespace(),
268 $newTitle->getDBkey(),
269 $newTitle->getPrefixedText() ) );
270
271 $id = $newTitle->getArticleID();
272 if ( $id ) {
273 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
274
275 return false;
276 } else {
277 return true;
278 }
279 }
280
281 /**
282 * Resolve any conflicts
283 *
284 * @param stClass $row Row from the page table to fix
285 * @param bool $resolvable
286 * @param string $suffix Suffix to append to the fixed page
287 * @return bool
288 */
289 private function resolveConflict( $row, $resolvable, $suffix ) {
290 if ( !$resolvable ) {
291 $this->output( "... *** old title {$row->title}\n" );
292 while ( true ) {
293 $row->title .= $suffix;
294 $this->output( "... *** new title {$row->title}\n" );
295 $title = Title::makeTitleSafe( $row->namespace, $row->title );
296 if ( !$title ) {
297 $this->output( "... !!! invalid title\n" );
298
299 return false;
300 }
301 $id = $title->getArticleID();
302 if ( $id ) {
303 $this->output( "... *** page exists with ID $id ***\n" );
304 } else {
305 break;
306 }
307 }
308 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
309 }
310 $this->resolveConflictOn( $row, 'page', 'page' );
311
312 return true;
313 }
314
315 /**
316 * Resolve a given conflict
317 *
318 * @param stdClass $row Row from the old broken entry
319 * @param string $table Table to update
320 * @param string $prefix Prefix for column name, like page or ar
321 * @return bool
322 */
323 private function resolveConflictOn( $row, $table, $prefix ) {
324 $this->output( "... resolving on $table... " );
325 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
326 $this->db->update( $table,
327 array(
328 "{$prefix}_namespace" => $newTitle->getNamespace(),
329 "{$prefix}_title" => $newTitle->getDBkey(),
330 ),
331 array(
332 // "{$prefix}_namespace" => 0,
333 // "{$prefix}_title" => $row->oldtitle,
334 "{$prefix}_id" => $row->id,
335 ),
336 __METHOD__ );
337 $this->output( "ok.\n" );
338
339 return true;
340 }
341 }
342
343 $maintClass = "NamespaceConflictChecker";
344 require_once RUN_MAINTENANCE_IF_MAIN;