Merge "Follow-up I774a89d6 (2fabea7): use $this->msg() in HistoryAction"
[mediawiki.git] / maintenance / namespaceDupes.php
blobe2de686cab2fc03612e4f8ab395961d6530c9459
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( dirname( __FILE__ ) . '/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 * @todo Needs to respect interwiki cache!
151 * @return Array
153 private function getInterwikiList() {
154 $result = $this->db->select( 'interwiki', array( 'iw_prefix' ) );
155 $prefixes = array();
156 foreach ( $result as $row ) {
157 $prefixes[] = $row->iw_prefix;
159 return $prefixes;
163 * @todo Document
164 * @param $ns Integer: a namespace id
165 * @param $name String
166 * @param $fix Boolean: whether to fix broken entries
167 * @param $suffix String: suffix to append to renamed articles
168 * @return bool
170 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
171 $conflicts = $this->getConflicts( $ns, $name );
172 $count = count( $conflicts );
173 if ( $count == 0 ) {
174 return true;
177 $ok = true;
178 foreach ( $conflicts as $row ) {
179 $resolvable = $this->reportConflict( $row, $suffix );
180 $ok = $ok && $resolvable;
181 if ( $fix && ( $resolvable || $suffix != '' ) ) {
182 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
185 return $ok;
189 * @todo: do this for reals
190 * @param $key
191 * @param $prefix
192 * @param $fix
193 * @param $suffix string
194 * @return bool
196 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
197 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
198 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
202 * Find pages in mainspace that have a prefix of the new namespace
203 * so we know titles that will need migrating
205 * @param $ns Integer: namespace id (id for new namespace?)
206 * @param $name String: prefix that is being made a namespace
208 * @return array
210 private function getConflicts( $ns, $name ) {
211 $page = 'page';
212 $table = $this->db->tableName( $page );
214 $prefix = $this->db->strencode( $name );
215 $encNamespace = $this->db->addQuotes( $ns );
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 ) );
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() );
232 $result = $this->db->query( $sql, __METHOD__ );
234 $set = array();
235 foreach ( $result as $row ) {
236 $set[] = $row;
238 return $set;
242 * Report any conflicts we find
244 * @return bool
246 private function reportConflict( $row, $suffix ) {
247 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
248 if ( is_null( $newTitle ) || !$newTitle->canExist() ) {
249 // Title is also an illegal title...
250 // For the moment we'll let these slide to cleanupTitles or whoever.
251 $this->output( sprintf( "... %d (%d,\"%s\")\n",
252 $row->id,
253 $row->oldnamespace,
254 $row->oldtitle ) );
255 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
256 return false;
259 $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
260 $row->id,
261 $row->oldnamespace,
262 $row->oldtitle,
263 $newTitle->getNamespace(),
264 $newTitle->getDBkey(),
265 $newTitle->getPrefixedText() ) );
267 $id = $newTitle->getArticleID();
268 if ( $id ) {
269 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
270 return false;
271 } else {
272 return true;
277 * Resolve any conflicts
279 * @param $row Object: row from the page table to fix
280 * @param $resolvable Boolean
281 * @param $suffix String: suffix to append to the fixed page
282 * @return bool
284 private function resolveConflict( $row, $resolvable, $suffix ) {
285 if ( !$resolvable ) {
286 $this->output( "... *** old title {$row->title}\n" );
287 while ( true ) {
288 $row->title .= $suffix;
289 $this->output( "... *** new title {$row->title}\n" );
290 $title = Title::makeTitleSafe( $row->namespace, $row->title );
291 if ( !$title ) {
292 $this->output( "... !!! invalid title\n" );
293 return false;
295 $id = $title->getArticleID();
296 if ( $id ) {
297 $this->output( "... *** page exists with ID $id ***\n" );
298 } else {
299 break;
302 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
304 $this->resolveConflictOn( $row, 'page', 'page' );
305 return true;
309 * Resolve a given conflict
311 * @param $row Object: row from the old broken entry
312 * @param $table String: table to update
313 * @param $prefix String: prefix for column name, like page or ar
314 * @return bool
316 private function resolveConflictOn( $row, $table, $prefix ) {
317 $this->output( "... resolving on $table... " );
318 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
319 $this->db->update( $table,
320 array(
321 "{$prefix}_namespace" => $newTitle->getNamespace(),
322 "{$prefix}_title" => $newTitle->getDBkey(),
324 array(
325 // "{$prefix}_namespace" => 0,
326 // "{$prefix}_title" => $row->oldtitle,
327 "{$prefix}_id" => $row->id,
329 __METHOD__ );
330 $this->output( "ok.\n" );
331 return true;
335 $maintClass = "NamespaceConflictChecker";
336 require_once( RUN_MAINTENANCE_IF_MAIN );