Whitespace changes
[mediawiki.git] / maintenance / namespaceDupes.php
blobe70dea4e032a5d413e94a72c415ce9f0c8465a53
1 <?php
2 /**
3 * Check for articles to fix after adding/deleting namespaces
5 * Copyright (C) 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 * @ingroup Maintenance
26 require_once( dirname(__FILE__) . '/Maintenance.php' );
28 class NamespaceConflictChecker extends Maintenance {
29 public function __construct() {
30 parent::__construct();
31 $this->mDescription = "";
32 $this->addOption( 'fix', 'Attempt to automatically fix errors' );
33 $this->addOption( 'suffix', "Dupes will be renamed with correct namespace with\n" .
34 "\t\t<text> Appended after the article name", false, true );
35 $this->addOption( 'prefix', "Do an explicit check for the given title prefix\n" .
36 "\t\tappended after the article name", false, true );
39 public function execute() {
40 global $wgTitle;
42 $this->db = wfGetDB( DB_MASTER );
43 $wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
45 $fix = $this->hasOption( 'fix' );
46 $suffix = $this->getOption( 'suffix', '' );
47 $prefix = $this->getOption( 'prefix', '' );
48 $key = intval( $this->getOption( 'key', 0 ) );
50 if( $prefix ) {
51 $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix );
52 } else {
53 $retval = $this->checkAll( $fix, $suffix );
56 if( $retval ) {
57 $this->output( "\nLooks good!\n" );
58 } else {
59 $this->output( "\nOh noeees\n" );
63 /**
64 * @todo Document
65 * @param $fix bool Whether or not to fix broken entries
66 * @param $suffix String Suffix to append to renamed articles
68 private function checkAll( $fix, $suffix = '' ) {
69 global $wgContLang, $wgNamespaceAliases, $wgCanonicalNamespaceNames;
70 global $wgCapitalLinks;
72 $spaces = array();
74 // List interwikis first, so they'll be overridden
75 // by any conflicting local namespaces.
76 foreach( $this->getInterwikiList() as $prefix ) {
77 $name = $wgContLang->ucfirst( $prefix );
78 $spaces[$name] = 0;
81 // Now pull in all canonical and alias namespaces...
82 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
83 // This includes $wgExtraNamespaces
84 if( $name !== '' ) {
85 $spaces[$name] = $ns;
88 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
89 if( $name !== '' ) {
90 $spaces[$name] = $ns;
93 foreach( $wgNamespaceAliases as $name => $ns ) {
94 $spaces[$name] = $ns;
96 foreach( $wgContLang->namespaceAliases as $name => $ns ) {
97 $spaces[$name] = $ns;
100 // We'll need to check for lowercase keys as well,
101 // since we're doing case-sensitive searches in the db.
102 foreach( $spaces as $name => $ns ) {
103 $moreNames = array();
104 $moreNames[] = $wgContLang->uc( $name );
105 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
106 $moreNames[] = $wgContLang->ucwords( $name );
107 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
108 $moreNames[] = $wgContLang->ucwordbreaks( $name );
109 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
110 if( !$wgCapitalLinks ) {
111 foreach( $moreNames as $altName ) {
112 $moreNames[] = $wgContLang->lcfirst( $altName );
114 $moreNames[] = $wgContLang->lcfirst( $name );
116 foreach( array_unique( $moreNames ) as $altName ) {
117 if( $altName !== $name ) {
118 $spaces[$altName] = $ns;
123 ksort( $spaces );
124 asort( $spaces );
126 $ok = true;
127 foreach( $spaces as $name => $ns ) {
128 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
130 return $ok;
134 * Get the interwiki list
135 * @return array
137 private function getInterwikiList() {
138 $result = $this->db->select( 'interwiki', array( 'iw_prefix' ) );
139 $prefixes = array();
140 while( $row = $this->db->fetchObject( $result ) ) {
141 $prefixes[] = $row->iw_prefix;
143 $this->db->freeResult( $result );
144 return $prefixes;
148 * @todo Document
149 * @param $ns int A namespace id
150 * @param $name String
151 * @param $fix bool Whether to fix broken entries
152 * @param $suffix String Suffix to append to renamed articles
154 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
155 if( $ns == 0 ) {
156 $header = "Checking interwiki prefix: \"$name\"\n";
157 } else {
158 $header = "Checking namespace $ns: \"$name\"\n";
161 $conflicts = $this->getConflicts( $ns, $name );
162 $count = count( $conflicts );
163 if( $count == 0 ) {
164 $this->output( $header . "... no conflict detected!\n" );
165 return true;
168 $this->output( $header . "... $count conflicts detected:\n" );
169 $ok = true;
170 foreach( $conflicts as $row ) {
171 $resolvable = $this->reportConflict( $row, $suffix );
172 $ok = $ok && $resolvable;
173 if( $fix && ( $resolvable || $suffix != '' ) ) {
174 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
177 return $ok;
181 * @todo: do this for reals
183 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
184 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
185 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
189 * Find pages in mainspace that have a prefix of the new namespace
190 * so we know titles that will need migrating
191 * @param $ns int Namespace id (id for new namespace?)
192 * @param $name String Prefix that is being made a namespace
194 private function getConflicts( $ns, $name ) {
195 $page = 'page';
196 $table = $this->db->tableName( $page );
198 $prefix = $this->db->strencode( $name );
199 $likeprefix = str_replace( '_', '\\_', $prefix);
200 $encNamespace = $this->db->addQuotes( $ns );
202 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
203 if( $ns == 0 ) {
204 // An interwiki; try an alternate encoding with '-' for ':'
205 $titleSql = $this->db->buildConcat( array( "'$prefix-'", $titleSql ) );
208 $sql = "SELECT {$page}_id AS id,
209 {$page}_title AS oldtitle,
210 $encNamespace AS namespace,
211 $titleSql AS title
212 FROM {$table}
213 WHERE {$page}_namespace=0
214 AND {$page}_title LIKE '$likeprefix:%'";
216 $result = $this->db->query( $sql, __METHOD__ );
218 $set = array();
219 while( $row = $this->db->fetchObject( $result ) ) {
220 $set[] = $row;
222 $this->db->freeResult( $result );
224 return $set;
228 * Report any conflicts we find
230 private function reportConflict( $row, $suffix ) {
231 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
232 if( is_null($newTitle) || !$newTitle->canExist() ) {
233 // Title is also an illegal title...
234 // For the moment we'll let these slide to cleanupTitles or whoever.
235 $this->output( sprintf( "... %d (0,\"%s\")\n",
236 $row->id,
237 $row->oldtitle ) );
238 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
239 return false;
242 $this->output( sprintf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
243 $row->id,
244 $row->oldtitle,
245 $newTitle->getNamespace(),
246 $newTitle->getDBkey(),
247 $newTitle->getPrefixedText() ) );
249 $id = $newTitle->getArticleId();
250 if( $id ) {
251 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
252 return false;
253 } else {
254 return true;
259 * Resolve any conflicts
260 * @param $row Row from the page table to fix
261 * @param $resolveable bool
262 * @param $suffix String Suffix to append to the fixed page
264 private function resolveConflict( $row, $resolvable, $suffix ) {
265 if( !$resolvable ) {
266 $this->output( "... *** old title {$row->title}\n" );
267 while( true ) {
268 $row->title .= $suffix;
269 $this->output( "... *** new title {$row->title}\n" );
270 $title = Title::makeTitleSafe( $row->namespace, $row->title );
271 if ( ! $title ) {
272 $this->output( "... !!! invalid title\n" );
273 return false;
275 if ( $id = $title->getArticleId() ) {
276 $this->output( "... *** page exists with ID $id ***\n" );
277 } else {
278 break;
281 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
283 $tables = array( 'page' );
284 foreach( $tables as $table ) {
285 $this->resolveConflictOn( $row, $table );
287 return true;
291 * Resolve a given conflict
292 * @param $row Row from the old broken entry
293 * @param $table String Table to update
295 private function resolveConflictOn( $row, $table ) {
296 $this->output( "... resolving on $table... " );
297 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
298 $this->db->update( $table,
299 array(
300 "{$table}_namespace" => $newTitle->getNamespace(),
301 "{$table}_title" => $newTitle->getDBkey(),
303 array(
304 "{$table}_namespace" => 0,
305 "{$table}_title" => $row->oldtitle,
307 __METHOD__ );
308 $this->output( "ok.\n" );
309 return true;
313 $maintClass = "NamespaceConflictChecker";
314 require_once( DO_MAINTENANCE );