Use buildConcat()
[lhc/web/wiklou.git] / maintenance / namespaceDupes.php
1 <?php
2 /**
3 * Check for articles to fix after adding/deleting namespaces
4 *
5 * Copyright (C) 2005-2007 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @ingroup Maintenance
24 */
25
26 require_once( "Maintenance.php" );
27
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 );
37 $this->addOption( 'wiki', 'Enter the wiki database to edit', false, true );
38 }
39
40 public function execute() {
41 global $wgTitle;
42
43 $this->db = wfGetDB( DB_MASTER );
44 $wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
45
46 $fix = $this->hasOption( 'fix' );
47 $suffix = $this->getOption( 'suffix', '' );
48 $prefix = $this->getOption( 'prefix', '' );
49 $key = intval( $this->getOption( 'key', 0 ) );
50
51 if( $prefix ) {
52 $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix );
53 } else {
54 $retval = $this->checkAll( $fix, $suffix );
55 }
56
57 if( $retval ) {
58 $this->output( "\nLooks good!\n" );
59 } else {
60 $this->output( "\nOh noeees\n" );
61 }
62 }
63
64 /**
65 * @todo Document
66 * @param $fix bool Whether or not to fix broken entries
67 * @param $suffix String Suffix to append to renamed articles
68 */
69 private function checkAll( $fix, $suffix = '' ) {
70 global $wgContLang, $wgNamespaceAliases, $wgCanonicalNamespaceNames;
71 global $wgCapitalLinks;
72
73 $spaces = array();
74
75 // List interwikis first, so they'll be overridden
76 // by any conflicting local namespaces.
77 foreach( $this->getInterwikiList() as $prefix ) {
78 $name = $wgContLang->ucfirst( $prefix );
79 $spaces[$name] = 0;
80 }
81
82 // Now pull in all canonical and alias namespaces...
83 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
84 // This includes $wgExtraNamespaces
85 if( $name !== '' ) {
86 $spaces[$name] = $ns;
87 }
88 }
89 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
90 if( $name !== '' ) {
91 $spaces[$name] = $ns;
92 }
93 }
94 foreach( $wgNamespaceAliases as $name => $ns ) {
95 $spaces[$name] = $ns;
96 }
97 foreach( $wgContLang->namespaceAliases as $name => $ns ) {
98 $spaces[$name] = $ns;
99 }
100
101 // We'll need to check for lowercase keys as well,
102 // since we're doing case-sensitive searches in the db.
103 foreach( $spaces as $name => $ns ) {
104 $moreNames = array();
105 $moreNames[] = $wgContLang->uc( $name );
106 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
107 $moreNames[] = $wgContLang->ucwords( $name );
108 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
109 $moreNames[] = $wgContLang->ucwordbreaks( $name );
110 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
111 if( !$wgCapitalLinks ) {
112 foreach( $moreNames as $altName ) {
113 $moreNames[] = $wgContLang->lcfirst( $altName );
114 }
115 $moreNames[] = $wgContLang->lcfirst( $name );
116 }
117 foreach( array_unique( $moreNames ) as $altName ) {
118 if( $altName !== $name ) {
119 $spaces[$altName] = $ns;
120 }
121 }
122 }
123
124 ksort( $spaces );
125 asort( $spaces );
126
127 $ok = true;
128 foreach( $spaces as $name => $ns ) {
129 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
130 }
131 return $ok;
132 }
133
134 /**
135 * Get the interwiki list
136 * @return array
137 */
138 private function getInterwikiList() {
139 $result = $this->db->select( 'interwiki', array( 'iw_prefix' ) );
140 $prefixes = array();
141 while( $row = $this->db->fetchObject( $result ) ) {
142 $prefixes[] = $row->iw_prefix;
143 }
144 $this->db->freeResult( $result );
145 return $prefixes;
146 }
147
148 /**
149 * @todo Document
150 * @param $ns int A namespace id
151 * @param $name String
152 * @param $fix bool Whether to fix broken entries
153 * @param $suffix String Suffix to append to renamed articles
154 */
155 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
156 if( $ns == 0 ) {
157 $header = "Checking interwiki prefix: \"$name\"\n";
158 } else {
159 $header = "Checking namespace $ns: \"$name\"\n";
160 }
161
162 $conflicts = $this->getConflicts( $ns, $name );
163 $count = count( $conflicts );
164 if( $count == 0 ) {
165 $this->output( $header . "... no conflict detected!\n" );
166 return true;
167 }
168
169 $this->output( $header . "... $count conflicts detected:\n" );
170 $ok = true;
171 foreach( $conflicts as $row ) {
172 $resolvable = $this->reportConflict( $row, $suffix );
173 $ok = $ok && $resolvable;
174 if( $fix && ( $resolvable || $suffix != '' ) ) {
175 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
176 }
177 }
178 return $ok;
179 }
180
181 /**
182 * @todo: do this for reals
183 */
184 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
185 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
186 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
187 }
188
189 /**
190 * Find pages in mainspace that have a prefix of the new namespace
191 * so we know titles that will need migrating
192 * @param $ns int Namespace id (id for new namespace?)
193 * @param $name String Prefix that is being made a namespace
194 */
195 private function getConflicts( $ns, $name ) {
196 $page = 'page';
197 $table = $this->db->tableName( $page );
198
199 $prefix = $this->db->strencode( $name );
200 $likeprefix = str_replace( '_', '\\_', $prefix);
201 $encNamespace = $this->db->addQuotes( $ns );
202
203 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
204 if( $ns == 0 ) {
205 // An interwiki; try an alternate encoding with '-' for ':'
206 $titleSql = $this->db->buildConcat( array( "'$prefix-'", $titleSql ) );
207 }
208
209 $sql = "SELECT {$page}_id AS id,
210 {$page}_title AS oldtitle,
211 $encNamespace AS namespace,
212 $titleSql AS title
213 FROM {$table}
214 WHERE {$page}_namespace=0
215 AND {$page}_title LIKE '$likeprefix:%'";
216
217 $result = $this->db->query( $sql, __METHOD__ );
218
219 $set = array();
220 while( $row = $this->db->fetchObject( $result ) ) {
221 $set[] = $row;
222 }
223 $this->db->freeResult( $result );
224
225 return $set;
226 }
227
228 /**
229 * Report any conflicts we find
230 */
231 private function reportConflict( $row, $suffix ) {
232 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
233 if( is_null($newTitle) || !$newTitle->canExist() ) {
234 // Title is also an illegal title...
235 // For the moment we'll let these slide to cleanupTitles or whoever.
236 $this->output( sprintf( "... %d (0,\"%s\")\n",
237 $row->id,
238 $row->oldtitle ) );
239 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
240 return false;
241 }
242
243 $this->output( sprintf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
244 $row->id,
245 $row->oldtitle,
246 $newTitle->getNamespace(),
247 $newTitle->getDBkey(),
248 $newTitle->getPrefixedText() ) );
249
250 $id = $newTitle->getArticleId();
251 if( $id ) {
252 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
253 return false;
254 } else {
255 return true;
256 }
257 }
258
259 /**
260 * Resolve any conflicts
261 * @param $row Row from the page table to fix
262 * @param $resolveable bool
263 * @param $suffix String Suffix to append to the fixed page
264 */
265 private function resolveConflict( $row, $resolvable, $suffix ) {
266 if( !$resolvable ) {
267 $this->output( "... *** old title {$row->title}\n" );
268 while( true ) {
269 $row->title .= $suffix;
270 $this->output( "... *** new title {$row->title}\n" );
271 $title = Title::makeTitleSafe( $row->namespace, $row->title );
272 if ( ! $title ) {
273 $this->output( "... !!! invalid title\n" );
274 return false;
275 }
276 if ( $id = $title->getArticleId() ) {
277 $this->output( "... *** page exists with ID $id ***\n" );
278 } else {
279 break;
280 }
281 }
282 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
283 }
284 $tables = array( 'page' );
285 foreach( $tables as $table ) {
286 $this->resolveConflictOn( $row, $table );
287 }
288 return true;
289 }
290
291 /**
292 * Resolve a given conflict
293 * @param $row Row from the old broken entry
294 * @param $table String Table to update
295 */
296 private function resolveConflictOn( $row, $table ) {
297 $this->output( "... resolving on $table... " );
298 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
299 $this->db->update( $table,
300 array(
301 "{$table}_namespace" => $newTitle->getNamespace(),
302 "{$table}_title" => $newTitle->getDBkey(),
303 ),
304 array(
305 "{$table}_namespace" => 0,
306 "{$table}_title" => $row->oldtitle,
307 ),
308 __METHOD__ );
309 $this->output( "ok.\n" );
310 return true;
311 }
312 }
313
314 $maintClass = "NamespaceConflictChecker";
315 require_once( DO_MAINTENANCE );