Bit more refactoring
[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 private function checkAll( $fix, $suffix = '' ) {
75 global $wgContLang, $wgNamespaceAliases, $wgCapitalLinks;
76
77 $spaces = array();
78
79 // List interwikis first, so they'll be overridden
80 // by any conflicting local namespaces.
81 foreach ( $this->getInterwikiList() as $prefix ) {
82 $name = $wgContLang->ucfirst( $prefix );
83 $spaces[$name] = 0;
84 }
85
86 // Now pull in all canonical and alias namespaces...
87 foreach ( MWNamespace::getCanonicalNamespaces() as $ns => $name ) {
88 // This includes $wgExtraNamespaces
89 if ( $name !== '' ) {
90 $spaces[$name] = $ns;
91 }
92 }
93 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
94 if ( $name !== '' ) {
95 $spaces[$name] = $ns;
96 }
97 }
98 foreach ( $wgNamespaceAliases as $name => $ns ) {
99 $spaces[$name] = $ns;
100 }
101 foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) {
102 $spaces[$name] = $ns;
103 }
104
105 // We'll need to check for lowercase keys as well,
106 // since we're doing case-sensitive searches in the db.
107 foreach ( $spaces as $name => $ns ) {
108 $moreNames = array();
109 $moreNames[] = $wgContLang->uc( $name );
110 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
111 $moreNames[] = $wgContLang->ucwords( $name );
112 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
113 $moreNames[] = $wgContLang->ucwordbreaks( $name );
114 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
115 if ( !$wgCapitalLinks ) {
116 foreach ( $moreNames as $altName ) {
117 $moreNames[] = $wgContLang->lcfirst( $altName );
118 }
119 $moreNames[] = $wgContLang->lcfirst( $name );
120 }
121 foreach ( array_unique( $moreNames ) as $altName ) {
122 if ( $altName !== $name ) {
123 $spaces[$altName] = $ns;
124 }
125 }
126 }
127
128 ksort( $spaces );
129 asort( $spaces );
130
131 $ok = true;
132 foreach ( $spaces as $name => $ns ) {
133 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
134 }
135 return $ok;
136 }
137
138 /**
139 * Get the interwiki list
140 *
141 * @todo Needs to respect interwiki cache!
142 * @return Array
143 */
144 private function getInterwikiList() {
145 $result = $this->db->select( 'interwiki', array( 'iw_prefix' ) );
146 $prefixes = array();
147 foreach ( $result as $row ) {
148 $prefixes[] = $row->iw_prefix;
149 }
150 return $prefixes;
151 }
152
153 /**
154 * @todo Document
155 * @param $ns Integer: a namespace id
156 * @param $name String
157 * @param $fix Boolean: whether to fix broken entries
158 * @param $suffix String: suffix to append to renamed articles
159 */
160 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
161 $conflicts = $this->getConflicts( $ns, $name );
162 $count = count( $conflicts );
163 if ( $count == 0 ) {
164 return true;
165 }
166
167 $ok = true;
168 foreach ( $conflicts as $row ) {
169 $resolvable = $this->reportConflict( $row, $suffix );
170 $ok = $ok && $resolvable;
171 if ( $fix && ( $resolvable || $suffix != '' ) ) {
172 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
173 }
174 }
175 return $ok;
176 }
177
178 /**
179 * @todo: do this for reals
180 */
181 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
182 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
183 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
184 }
185
186 /**
187 * Find pages in mainspace that have a prefix of the new namespace
188 * so we know titles that will need migrating
189 *
190 * @param $ns Integer: namespace id (id for new namespace?)
191 * @param $name String: prefix that is being made a namespace
192 *
193 * @return array
194 */
195 private function getConflicts( $ns, $name ) {
196 $page = 'page';
197 $table = $this->db->tableName( $page );
198
199 $prefix = $this->db->strencode( $name );
200 $encNamespace = $this->db->addQuotes( $ns );
201
202 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
203 if ( $ns == 0 ) {
204 // An interwiki; try an alternate encoding with '-' for ':'
205 $titleSql = $this->db->buildConcat( array( "'$prefix-'", $titleSql ) );
206 }
207
208 $sql = "SELECT {$page}_id AS id,
209 {$page}_title AS oldtitle,
210 $encNamespace + {$page}_namespace AS namespace,
211 $titleSql AS title,
212 {$page}_namespace AS oldnamespace
213 FROM {$table}
214 WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 )
215 AND {$page}_title " . $this->db->buildLike( $name . ':', $this->db->anyString() );
216
217 $result = $this->db->query( $sql, __METHOD__ );
218
219 $set = array();
220 foreach ( $result as $row ) {
221 $set[] = $row;
222 }
223 return $set;
224 }
225
226 /**
227 * Report any conflicts we find
228 *
229 * @return bool
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 (%d,\"%s\")\n",
237 $row->id,
238 $row->oldnamespace,
239 $row->oldtitle ) );
240 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
241 return false;
242 }
243
244 $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
245 $row->id,
246 $row->oldnamespace,
247 $row->oldtitle,
248 $newTitle->getNamespace(),
249 $newTitle->getDBkey(),
250 $newTitle->getPrefixedText() ) );
251
252 $id = $newTitle->getArticleId();
253 if ( $id ) {
254 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
255 return false;
256 } else {
257 return true;
258 }
259 }
260
261 /**
262 * Resolve any conflicts
263 *
264 * @param $row Object: row from the page table to fix
265 * @param $resolvable Boolean
266 * @param $suffix String: suffix to append to the fixed page
267 */
268 private function resolveConflict( $row, $resolvable, $suffix ) {
269 if ( !$resolvable ) {
270 $this->output( "... *** old title {$row->title}\n" );
271 while ( true ) {
272 $row->title .= $suffix;
273 $this->output( "... *** new title {$row->title}\n" );
274 $title = Title::makeTitleSafe( $row->namespace, $row->title );
275 if ( !$title ) {
276 $this->output( "... !!! invalid title\n" );
277 return false;
278 }
279 $id = $title->getArticleId();
280 if ( $id ) {
281 $this->output( "... *** page exists with ID $id ***\n" );
282 } else {
283 break;
284 }
285 }
286 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
287 }
288 $this->resolveConflictOn( $row, 'page', 'page' );
289 return true;
290 }
291
292 /**
293 * Resolve a given conflict
294 *
295 * @param $row Object: row from the old broken entry
296 * @param $table String: table to update
297 * @param $prefix String: prefix for column name, like page or ar
298 */
299 private function resolveConflictOn( $row, $table, $prefix ) {
300 $this->output( "... resolving on $table... " );
301 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
302 $this->db->update( $table,
303 array(
304 "{$prefix}_namespace" => $newTitle->getNamespace(),
305 "{$prefix}_title" => $newTitle->getDBkey(),
306 ),
307 array(
308 // "{$prefix}_namespace" => 0,
309 // "{$prefix}_title" => $row->oldtitle,
310 "{$prefix}_id" => $row->id,
311 ),
312 __METHOD__ );
313 $this->output( "ok.\n" );
314 return true;
315 }
316 }
317
318 $maintClass = "NamespaceConflictChecker";
319 require_once( RUN_MAINTENANCE_IF_MAIN );