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