Assorted minor live patches to command-line scripts.
[lhc/web/wiklou.git] / maintenance / namespaceDupes.php
1 <?php
2 # Copyright (C) 2005-2007 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * @file
22 * @ingroup Maintenance
23 */
24
25 $options = array( 'fix', 'suffix', 'help' );
26
27 /** */
28 require_once( 'commandLine.inc' );
29
30 if(isset( $options['help'] ) ) {
31 print <<<ENDS
32 usage: namespaceDupes.php [--fix] [--suffix=<text>] [--help]
33 --help : this help message
34 --fix : attempt to automatically fix errors
35 --suffix=<text> : dupes will be renamed with correct namespace with <text>
36 appended after the article name.
37 --prefix=<text> : Do an explicit check for the given title prefix
38 in place of the standard namespace list.
39 --verbose : Display output for checked namespaces without conflicts
40 --wiki=<wiki> : enter the wiki database to edit
41 ENDS;
42 die;
43 }
44
45 class NamespaceConflictChecker {
46 function NamespaceConflictChecker( $db, $verbose=false ) {
47 $this->db = $db;
48 $this->verbose = $verbose;
49 }
50
51 function checkAll( $fix, $suffix = '' ) {
52 global $wgContLang, $wgNamespaceAliases, $wgCanonicalNamespaceNames;
53 global $wgCapitalLinks;
54
55 $spaces = array();
56
57 // List interwikis first, so they'll be overridden
58 // by any conflicting local namespaces.
59 foreach( $this->getInterwikiList() as $prefix ) {
60 $name = $wgContLang->ucfirst( $prefix );
61 $spaces[$name] = 0;
62 }
63
64 // Now pull in all canonical and alias namespaces...
65 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
66 // This includes $wgExtraNamespaces
67 if( $name !== '' ) {
68 $spaces[$name] = $ns;
69 }
70 }
71 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
72 if( $name !== '' ) {
73 $spaces[$name] = $ns;
74 }
75 }
76 foreach( $wgNamespaceAliases as $name => $ns ) {
77 $spaces[$name] = $ns;
78 }
79 foreach( $wgContLang->namespaceAliases as $name => $ns ) {
80 $spaces[$name] = $ns;
81 }
82
83 // We'll need to check for lowercase keys as well,
84 // since we're doing case-sensitive searches in the db.
85 foreach( $spaces as $name => $ns ) {
86 $moreNames = array();
87 $moreNames[] = $wgContLang->uc( $name );
88 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
89 $moreNames[] = $wgContLang->ucwords( $name );
90 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
91 $moreNames[] = $wgContLang->ucwordbreaks( $name );
92 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
93 if( !$wgCapitalLinks ) {
94 foreach( $moreNames as $altName ) {
95 $moreNames[] = $wgContLang->lcfirst( $altName );
96 }
97 $moreNames[] = $wgContLang->lcfirst( $name );
98 }
99 foreach( array_unique( $moreNames ) as $altName ) {
100 if( $altName !== $name ) {
101 $spaces[$altName] = $ns;
102 }
103 }
104 }
105
106 ksort( $spaces );
107 asort( $spaces );
108
109 $ok = true;
110 foreach( $spaces as $name => $ns ) {
111 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
112 }
113 return $ok;
114 }
115
116 private function getInterwikiList() {
117 $result = $this->db->select( 'interwiki', array( 'iw_prefix' ) );
118 $prefixes = array();
119 while( $row = $this->db->fetchObject( $result ) ) {
120 $prefixes[] = $row->iw_prefix;
121 }
122 $this->db->freeResult( $result );
123 return $prefixes;
124 }
125
126 function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
127 if( $ns == 0 ) {
128 $header = "Checking interwiki prefix: \"$name\"\n";
129 } else {
130 $header = "Checking namespace $ns: \"$name\"\n";
131 }
132
133 $conflicts = $this->getConflicts( $ns, $name );
134 $count = count( $conflicts );
135 if( $count == 0 ) {
136 if( $this->verbose ) {
137 echo $header;
138 echo "... no conflicts detected!\n";
139 }
140 return true;
141 }
142
143 echo $header;
144 echo "... $count conflicts detected:\n";
145 $ok = true;
146 foreach( $conflicts as $row ) {
147 $resolvable = $this->reportConflict( $row, $suffix );
148 $ok = $ok && $resolvable;
149 if( $fix && ( $resolvable || $suffix != '' ) ) {
150 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
151 }
152 }
153 return $ok;
154 }
155
156 /**
157 * @todo: do this for reals
158 */
159 function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
160 echo "Checking prefix \"$prefix\" vs namespace $key\n";
161 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
162 }
163
164 function getConflicts( $ns, $name ) {
165 $page = 'page';
166 $table = $this->db->tableName( $page );
167
168 $prefix = $this->db->strencode( $name );
169 $likeprefix = str_replace( '_', '\\_', $prefix);
170 $encNamespace = $this->db->addQuotes( $ns );
171
172 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
173 if( $ns == 0 ) {
174 // An interwiki; try an alternate encoding with '-' for ':'
175 $titleSql = "CONCAT('$prefix-',$titleSql)";
176 }
177
178 $sql = "SELECT {$page}_id AS id,
179 {$page}_title AS oldtitle,
180 $encNamespace AS namespace,
181 $titleSql AS title
182 FROM {$table}
183 WHERE {$page}_namespace=0
184 AND {$page}_title LIKE '$likeprefix:%'";
185
186 $result = $this->db->query( $sql, 'NamespaceConflictChecker::getConflicts' );
187
188 $set = array();
189 while( $row = $this->db->fetchObject( $result ) ) {
190 $set[] = $row;
191 }
192 $this->db->freeResult( $result );
193
194 return $set;
195 }
196
197 function reportConflict( $row, $suffix ) {
198 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
199 if( is_null($newTitle) || !$newTitle->canExist() ) {
200 // Title is also an illegal title...
201 // For the moment we'll let these slide to cleanupTitles or whoever.
202 printf( "... %d (0,\"%s\")\n",
203 $row->id,
204 $row->oldtitle );
205 echo "... *** cannot resolve automatically; illegal title ***\n";
206 return false;
207 }
208
209 printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
210 $row->id,
211 $row->oldtitle,
212 $newTitle->getNamespace(),
213 $newTitle->getDBkey(),
214 $newTitle->getPrefixedText() );
215
216 $id = $newTitle->getArticleId();
217 if( $id ) {
218 echo "... *** cannot resolve automatically; page exists with ID $id ***\n";
219 return false;
220 } else {
221 return true;
222 }
223 }
224
225 function resolveConflict( $row, $resolvable, $suffix ) {
226 if( !$resolvable ) {
227 echo "... *** old title {$row->title}\n";
228 while( true ) {
229 $row->title .= $suffix;
230 echo "... *** new title {$row->title}\n";
231 $title = Title::makeTitleSafe( $row->namespace, $row->title );
232 if ( ! $title ) {
233 echo "... !!! invalid title\n";
234 return false;
235 }
236 if ( $id = $title->getArticleId() ) {
237 echo "... *** page exists with ID $id ***\n";
238 } else {
239 break;
240 }
241 }
242 echo "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
243 }
244 $tables = array( 'page' );
245 foreach( $tables as $table ) {
246 $this->resolveConflictOn( $row, $table );
247 }
248 return true;
249 }
250
251 function resolveConflictOn( $row, $table ) {
252 echo "... resolving on $table... ";
253 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
254 $this->db->update( $table,
255 array(
256 "{$table}_namespace" => $newTitle->getNamespace(),
257 "{$table}_title" => $newTitle->getDBkey(),
258 ),
259 array(
260 "{$table}_namespace" => 0,
261 "{$table}_title" => $row->oldtitle,
262 ),
263 __METHOD__ );
264 echo "ok.\n";
265 return true;
266 }
267 }
268
269
270
271
272 $wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
273
274 $verbose = isset( $options['verbose'] );
275 $fix = isset( $options['fix'] );
276 $suffix = isset( $options['suffix'] ) ? $options['suffix'] : '';
277 $prefix = isset( $options['prefix'] ) ? $options['prefix'] : '';
278 $key = isset( $options['key'] ) ? intval( $options['key'] ) : 0;
279
280 $dbw = wfGetDB( DB_MASTER );
281 $duper = new NamespaceConflictChecker( $dbw, $verbose );
282
283 if( $prefix ) {
284 $retval = $duper->checkPrefix( $key, $prefix, $fix, $suffix );
285 } else {
286 $retval = $duper->checkAll( $fix, $suffix );
287 }
288
289 if( $retval ) {
290 echo "\nLooks good!\n";
291 exit( 0 );
292 } else {
293 echo "\nOh noeees\n";
294 exit( -1 );
295 }
296
297