Whitelist the <wbr> element.
[mediawiki.git] / maintenance / namespaceDupes.php
blobff024682c6a5563501800e6d2bb61742eba33a64
1 <?php
2 /**
3 * Check for articles to fix after adding/deleting namespaces
5 * Copyright © 2005-2007 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
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.
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.
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
23 * @file
24 * @ingroup Maintenance
27 require_once __DIR__ . '/Maintenance.php';
29 /**
30 * Maintenance script that checks for articles to fix after
31 * adding/deleting namespaces.
33 * @ingroup Maintenance
35 class NamespaceConflictChecker extends Maintenance {
37 /**
38 * @var DatabaseBase
40 protected $db;
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 );
52 public function execute() {
53 global $wgTitle;
55 $this->db = wfGetDB( DB_MASTER );
56 $wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
58 $fix = $this->hasOption( 'fix' );
59 $suffix = $this->getOption( 'suffix', '' );
60 $prefix = $this->getOption( 'prefix', '' );
61 $key = intval( $this->getOption( 'key', 0 ) );
63 if ( $prefix ) {
64 $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix );
65 } else {
66 $retval = $this->checkAll( $fix, $suffix );
69 if ( $retval ) {
70 $this->output( "\nLooks good!\n" );
71 } else {
72 $this->output( "\nOh noeees\n" );
76 /**
77 * @todo Document
78 * @param $fix Boolean: whether or not to fix broken entries
79 * @param $suffix String: suffix to append to renamed articles
81 * @return bool
83 private function checkAll( $fix, $suffix = '' ) {
84 global $wgContLang, $wgNamespaceAliases, $wgCapitalLinks;
86 $spaces = array();
88 // List interwikis first, so they'll be overridden
89 // by any conflicting local namespaces.
90 foreach ( $this->getInterwikiList() as $prefix ) {
91 $name = $wgContLang->ucfirst( $prefix );
92 $spaces[$name] = 0;
95 // Now pull in all canonical and alias namespaces...
96 foreach ( MWNamespace::getCanonicalNamespaces() as $ns => $name ) {
97 // This includes $wgExtraNamespaces
98 if ( $name !== '' ) {
99 $spaces[$name] = $ns;
102 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
103 if ( $name !== '' ) {
104 $spaces[$name] = $ns;
107 foreach ( $wgNamespaceAliases as $name => $ns ) {
108 $spaces[$name] = $ns;
110 foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) {
111 $spaces[$name] = $ns;
114 // We'll need to check for lowercase keys as well,
115 // since we're doing case-sensitive searches in the db.
116 foreach ( $spaces as $name => $ns ) {
117 $moreNames = array();
118 $moreNames[] = $wgContLang->uc( $name );
119 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
120 $moreNames[] = $wgContLang->ucwords( $name );
121 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
122 $moreNames[] = $wgContLang->ucwordbreaks( $name );
123 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
124 if ( !$wgCapitalLinks ) {
125 foreach ( $moreNames as $altName ) {
126 $moreNames[] = $wgContLang->lcfirst( $altName );
128 $moreNames[] = $wgContLang->lcfirst( $name );
130 foreach ( array_unique( $moreNames ) as $altName ) {
131 if ( $altName !== $name ) {
132 $spaces[$altName] = $ns;
137 ksort( $spaces );
138 asort( $spaces );
140 $ok = true;
141 foreach ( $spaces as $name => $ns ) {
142 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
144 return $ok;
148 * Get the interwiki list
150 * @return Array
152 private function getInterwikiList() {
153 $result = Interwiki::getAllPrefixes();
154 $prefixes = array();
155 foreach ( $result as $row ) {
156 $prefixes[] = $row['iw_prefix'];
158 return $prefixes;
162 * @todo Document
163 * @param $ns Integer: a namespace id
164 * @param $name String
165 * @param $fix Boolean: whether to fix broken entries
166 * @param $suffix String: suffix to append to renamed articles
167 * @return bool
169 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
170 $conflicts = $this->getConflicts( $ns, $name );
171 $count = count( $conflicts );
172 if ( $count == 0 ) {
173 return true;
176 $ok = true;
177 foreach ( $conflicts as $row ) {
178 $resolvable = $this->reportConflict( $row, $suffix );
179 $ok = $ok && $resolvable;
180 if ( $fix && ( $resolvable || $suffix != '' ) ) {
181 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
184 return $ok;
188 * @todo Do this for real
189 * @param $key
190 * @param $prefix
191 * @param $fix
192 * @param $suffix string
193 * @return bool
195 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
196 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
197 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
201 * Find pages in mainspace that have a prefix of the new namespace
202 * so we know titles that will need migrating
204 * @param $ns Integer: namespace id (id for new namespace?)
205 * @param $name String: prefix that is being made a namespace
207 * @return array
209 private function getConflicts( $ns, $name ) {
210 $page = 'page';
211 $table = $this->db->tableName( $page );
213 $prefix = $this->db->strencode( $name );
214 $encNamespace = $this->db->addQuotes( $ns );
216 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
217 if ( $ns == 0 ) {
218 // An interwiki; try an alternate encoding with '-' for ':'
219 $titleSql = $this->db->buildConcat( array( "'$prefix-'", $titleSql ) );
222 $sql = "SELECT {$page}_id AS id,
223 {$page}_title AS oldtitle,
224 $encNamespace + {$page}_namespace AS namespace,
225 $titleSql AS title,
226 {$page}_namespace AS oldnamespace
227 FROM {$table}
228 WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 )
229 AND {$page}_title " . $this->db->buildLike( $name . ':', $this->db->anyString() );
231 $result = $this->db->query( $sql, __METHOD__ );
233 $set = array();
234 foreach ( $result as $row ) {
235 $set[] = $row;
237 return $set;
241 * Report any conflicts we find
243 * @return bool
245 private function reportConflict( $row, $suffix ) {
246 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
247 if ( is_null( $newTitle ) || !$newTitle->canExist() ) {
248 // Title is also an illegal title...
249 // For the moment we'll let these slide to cleanupTitles or whoever.
250 $this->output( sprintf( "... %d (%d,\"%s\")\n",
251 $row->id,
252 $row->oldnamespace,
253 $row->oldtitle ) );
254 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
255 return false;
258 $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
259 $row->id,
260 $row->oldnamespace,
261 $row->oldtitle,
262 $newTitle->getNamespace(),
263 $newTitle->getDBkey(),
264 $newTitle->getPrefixedText() ) );
266 $id = $newTitle->getArticleID();
267 if ( $id ) {
268 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
269 return false;
270 } else {
271 return true;
276 * Resolve any conflicts
278 * @param $row Object: row from the page table to fix
279 * @param $resolvable Boolean
280 * @param $suffix String: suffix to append to the fixed page
281 * @return bool
283 private function resolveConflict( $row, $resolvable, $suffix ) {
284 if ( !$resolvable ) {
285 $this->output( "... *** old title {$row->title}\n" );
286 while ( true ) {
287 $row->title .= $suffix;
288 $this->output( "... *** new title {$row->title}\n" );
289 $title = Title::makeTitleSafe( $row->namespace, $row->title );
290 if ( !$title ) {
291 $this->output( "... !!! invalid title\n" );
292 return false;
294 $id = $title->getArticleID();
295 if ( $id ) {
296 $this->output( "... *** page exists with ID $id ***\n" );
297 } else {
298 break;
301 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
303 $this->resolveConflictOn( $row, 'page', 'page' );
304 return true;
308 * Resolve a given conflict
310 * @param $row Object: row from the old broken entry
311 * @param $table String: table to update
312 * @param $prefix String: prefix for column name, like page or ar
313 * @return bool
315 private function resolveConflictOn( $row, $table, $prefix ) {
316 $this->output( "... resolving on $table... " );
317 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
318 $this->db->update( $table,
319 array(
320 "{$prefix}_namespace" => $newTitle->getNamespace(),
321 "{$prefix}_title" => $newTitle->getDBkey(),
323 array(
324 // "{$prefix}_namespace" => 0,
325 // "{$prefix}_title" => $row->oldtitle,
326 "{$prefix}_id" => $row->id,
328 __METHOD__ );
329 $this->output( "ok.\n" );
330 return true;
334 $maintClass = "NamespaceConflictChecker";
335 require_once RUN_MAINTENANCE_IF_MAIN;