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