d758a8581b5d12df9881370f421f612daccda2cd
[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 $options = array( 'fix', 'suffix', 'help' );
21
22 /** */
23 require_once( 'commandLine.inc' );
24
25 if(isset( $options['help'] ) ) {
26 print <<<END
27 usage: namespaceDupes.php [--fix] [--suffix=<text>] [--help]
28 --help : this help message
29 --fix : attempt to automatically fix errors
30 --suffix=<text> : dupes will be renamed with correct namespace with <text>
31 appended after the article name.
32 --prefix=<text> : Do an explicit check for the given title prefix
33 in place of the standard namespace list.
34 --verbose : Display output for checked namespaces without conflicts
35
36 END;
37 die;
38 }
39
40 class NamespaceConflictChecker {
41 function NamespaceConflictChecker( $db, $verbose=false ) {
42 $this->db = $db;
43 $this->verbose = $verbose;
44 }
45
46 function checkAll( $fix, $suffix = '' ) {
47 global $wgContLang, $wgNamespaceAliases, $wgCanonicalNamespaceNames;
48 global $wgCapitalLinks;
49
50 $spaces = array();
51
52 // List interwikis first, so they'll be overridden
53 // by any conflicting local namespaces.
54 foreach( $this->getInterwikiList() as $prefix ) {
55 $name = $wgContLang->ucfirst( $prefix );
56 $spaces[$name] = 0;
57 }
58
59 // Now pull in all canonical and alias namespaces...
60 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
61 // This includes $wgExtraNamespaces
62 if( $name !== '' ) {
63 $spaces[$name] = $ns;
64 }
65 }
66 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
67 if( $name !== '' ) {
68 $spaces[$name] = $ns;
69 }
70 }
71 foreach( $wgNamespaceAliases as $name => $ns ) {
72 $spaces[$name] = $ns;
73 }
74 foreach( $wgContLang->namespaceAliases as $name => $ns ) {
75 $spaces[$name] = $ns;
76 }
77
78 // We'll need to check for lowercase keys as well,
79 // since we're doing case-sensitive searches in the db.
80 foreach( $spaces as $name => $ns ) {
81 $moreNames = array();
82 $moreNames[] = $wgContLang->uc( $name );
83 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
84 $moreNames[] = $wgContLang->ucwords( $name );
85 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
86 $moreNames[] = $wgContLang->ucwordbreaks( $name );
87 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
88 if( !$wgCapitalLinks ) {
89 foreach( $moreNames as $altName ) {
90 $moreNames[] = $wgContLang->lcfirst( $altName );
91 }
92 $moreNames[] = $wgContLang->lcfirst( $name );
93 }
94 foreach( array_unique( $moreNames ) as $altName ) {
95 if( $altName !== $name ) {
96 $spaces[$altName] = $ns;
97 }
98 }
99 }
100
101 ksort( $spaces );
102 asort( $spaces );
103
104 $ok = true;
105 foreach( $spaces as $name => $ns ) {
106 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
107 }
108 return $ok;
109 }
110
111 private function getInterwikiList() {
112 $result = $this->db->select( 'interwiki', array( 'iw_prefix' ) );
113 while( $row = $this->db->fetchObject( $result ) ) {
114 $prefixes[] = $row->iw_prefix;
115 }
116 $this->db->freeResult( $result );
117 return $prefixes;
118 }
119
120 function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
121 if( $ns == 0 ) {
122 $header = "Checking interwiki prefix: \"$name\"\n";
123 } else {
124 $header = "Checking namespace $ns: \"$name\"\n";
125 }
126
127 $conflicts = $this->getConflicts( $ns, $name );
128 $count = count( $conflicts );
129 if( $count == 0 ) {
130 if( $this->verbose ) {
131 echo $header;
132 echo "... no conflicts detected!\n";
133 }
134 return true;
135 }
136
137 echo $header;
138 echo "... $count conflicts detected:\n";
139 $ok = true;
140 foreach( $conflicts as $row ) {
141 $resolvable = $this->reportConflict( $row, $suffix );
142 $ok = $ok && $resolvable;
143 if( $fix && ( $resolvable || $suffix != '' ) ) {
144 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
145 }
146 }
147 return $ok;
148 }
149
150 /**
151 * @todo: do this for reals
152 */
153 function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
154 echo "Checking prefix \"$prefix\" vs namespace $key\n";
155 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
156 }
157
158 function getConflicts( $ns, $name ) {
159 $page = 'page';
160 $table = $this->db->tableName( $page );
161
162 $prefix = $this->db->strencode( $name );
163 $likeprefix = str_replace( '_', '\\_', $prefix);
164 $encNamespace = $this->db->addQuotes( $ns );
165
166 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
167 if( $ns == 0 ) {
168 // An interwiki; try an alternate encoding with '-' for ':'
169 $titleSql = "CONCAT('$prefix-',$titleSql)";
170 }
171
172 $sql = "SELECT {$page}_id AS id,
173 {$page}_title AS oldtitle,
174 $encNamespace AS namespace,
175 $titleSql AS title
176 FROM {$table}
177 WHERE {$page}_namespace=0
178 AND {$page}_title LIKE '$likeprefix:%'";
179
180 $result = $this->db->query( $sql, 'NamespaceConflictChecker::getConflicts' );
181
182 $set = array();
183 while( $row = $this->db->fetchObject( $result ) ) {
184 $set[] = $row;
185 }
186 $this->db->freeResult( $result );
187
188 return $set;
189 }
190
191 function reportConflict( $row, $suffix ) {
192 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
193 if( !$newTitle ) {
194 // Title is also an illegal title...
195 // For the moment we'll let these slide to cleanupTitles or whoever.
196 printf( "... %d (0,\"%s\")\n",
197 $row->id,
198 $row->oldtitle );
199 echo "... *** cannot resolve automatically; illegal title ***\n";
200 return false;
201 }
202
203 printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
204 $row->id,
205 $row->oldtitle,
206 $newTitle->getNamespace(),
207 $newTitle->getDbKey(),
208 $newTitle->getPrefixedText() );
209
210 $id = $newTitle->getArticleId();
211 if( $id ) {
212 echo "... *** cannot resolve automatically; page exists with ID $id ***\n";
213 return false;
214 } else {
215 return true;
216 }
217 }
218
219 function resolveConflict( $row, $resolvable, $suffix ) {
220 if( !$resolvable ) {
221 $row->title .= $suffix;
222 $title = Title::makeTitleSafe( $row->namespace, $row->title );
223 echo "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
224 }
225 $tables = array( 'page' );
226 foreach( $tables as $table ) {
227 $this->resolveConflictOn( $row, $table );
228 }
229 return true;
230 }
231
232 function resolveConflictOn( $row, $table ) {
233 echo "... resolving on $table... ";
234 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
235 $this->db->update( $table,
236 array(
237 "{$table}_namespace" => $newTitle->getNamespace(),
238 "{$table}_title" => $newTitle->getDbKey(),
239 ),
240 array(
241 "{$table}_namespace" => 0,
242 "{$table}_title" => $row->oldtitle,
243 ),
244 __METHOD__ );
245 echo "ok.\n";
246 return true;
247 }
248 }
249
250
251
252
253 $wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
254
255 $verbose = isset( $options['verbose'] );
256 $fix = isset( $options['fix'] );
257 $suffix = isset( $options['suffix'] ) ? $options['suffix'] : '';
258 $prefix = isset( $options['prefix'] ) ? $options['prefix'] : '';
259 $key = isset( $options['key'] ) ? intval( $options['key'] ) : 0;
260
261 $dbw = wfGetDB( DB_MASTER );
262 $duper = new NamespaceConflictChecker( $dbw, $verbose );
263
264 if( $prefix ) {
265 $retval = $duper->checkPrefix( $key, $prefix, $fix, $suffix );
266 } else {
267 $retval = $duper->checkAll( $fix, $suffix );
268 }
269
270 if( $retval ) {
271 echo "\nLooks good!\n";
272 exit( 0 );
273 } else {
274 echo "\nOh noeees\n";
275 exit( -1 );
276 }
277
278