3 * Check for articles to fix after adding/deleting namespaces
5 * Copyright © 2005-2007 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
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.
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.
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
24 * @ingroup Maintenance
27 require_once __DIR__
. '/Maintenance.php';
30 * Maintenance script that checks for articles to fix after
31 * adding/deleting namespaces.
33 * @ingroup Maintenance
35 class NamespaceConflictChecker
extends Maintenance
{
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 );
52 public function execute() {
55 $this->db
= wfGetDB( DB_MASTER
);
56 $wgTitle = Title
::newFromText( 'Namespace title conflict cleanup script' );
58 $fix = $this->hasOption( 'fix' );
59 $suffix = $this->getOption( 'suffix', '' );
60 $prefix = $this->getOption( 'prefix', '' );
61 $key = intval( $this->getOption( 'key', 0 ) );
64 $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix );
66 $retval = $this->checkAll( $fix, $suffix );
70 $this->output( "\nLooks good!\n" );
72 $this->output( "\nOh noeees\n" );
78 * @param $fix Boolean: whether or not to fix broken entries
79 * @param $suffix String: suffix to append to renamed articles
83 private function checkAll( $fix, $suffix = '' ) {
84 global $wgContLang, $wgNamespaceAliases, $wgCapitalLinks;
88 // List interwikis first, so they'll be overridden
89 // by any conflicting local namespaces.
90 foreach ( $this->getInterwikiList() as $prefix ) {
91 $name = $wgContLang->ucfirst( $prefix );
95 // Now pull in all canonical and alias namespaces...
96 foreach ( MWNamespace
::getCanonicalNamespaces() as $ns => $name ) {
97 // This includes $wgExtraNamespaces
102 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
103 if ( $name !== '' ) {
104 $spaces[$name] = $ns;
107 foreach ( $wgNamespaceAliases as $name => $ns ) {
108 $spaces[$name] = $ns;
110 foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) {
111 $spaces[$name] = $ns;
114 // We'll need to check for lowercase keys as well,
115 // since we're doing case-sensitive searches in the db.
116 foreach ( $spaces as $name => $ns ) {
117 $moreNames = array();
118 $moreNames[] = $wgContLang->uc( $name );
119 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
120 $moreNames[] = $wgContLang->ucwords( $name );
121 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
122 $moreNames[] = $wgContLang->ucwordbreaks( $name );
123 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
124 if ( !$wgCapitalLinks ) {
125 foreach ( $moreNames as $altName ) {
126 $moreNames[] = $wgContLang->lcfirst( $altName );
128 $moreNames[] = $wgContLang->lcfirst( $name );
130 foreach ( array_unique( $moreNames ) as $altName ) {
131 if ( $altName !== $name ) {
132 $spaces[$altName] = $ns;
141 foreach ( $spaces as $name => $ns ) {
142 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
148 * Get the interwiki list
152 private function getInterwikiList() {
153 $result = Interwiki
::getAllPrefixes();
155 foreach ( $result as $row ) {
156 $prefixes[] = $row['iw_prefix'];
163 * @param $ns Integer: a namespace id
164 * @param $name String
165 * @param $fix Boolean: whether to fix broken entries
166 * @param $suffix String: suffix to append to renamed articles
169 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
170 $conflicts = $this->getConflicts( $ns, $name );
171 $count = count( $conflicts );
177 foreach ( $conflicts as $row ) {
178 $resolvable = $this->reportConflict( $row, $suffix );
179 $ok = $ok && $resolvable;
180 if ( $fix && ( $resolvable ||
$suffix != '' ) ) {
181 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
188 * @todo Do this for real
192 * @param $suffix string
195 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
196 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
197 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
201 * Find pages in mainspace that have a prefix of the new namespace
202 * so we know titles that will need migrating
204 * @param $ns Integer: namespace id (id for new namespace?)
205 * @param $name String: prefix that is being made a namespace
209 private function getConflicts( $ns, $name ) {
211 $table = $this->db
->tableName( $page );
213 $prefix = $this->db
->strencode( $name );
214 $encNamespace = $this->db
->addQuotes( $ns );
216 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
218 // An interwiki; try an alternate encoding with '-' for ':'
219 $titleSql = $this->db
->buildConcat( array( "'$prefix-'", $titleSql ) );
222 $sql = "SELECT {$page}_id AS id,
223 {$page}_title AS oldtitle,
224 $encNamespace + {$page}_namespace AS namespace,
226 {$page}_namespace AS oldnamespace
228 WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 )
229 AND {$page}_title " . $this->db
->buildLike( $name . ':', $this->db
->anyString() );
231 $result = $this->db
->query( $sql, __METHOD__
);
234 foreach ( $result as $row ) {
241 * Report any conflicts we find
245 private function reportConflict( $row, $suffix ) {
246 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
247 if ( is_null( $newTitle ) ||
!$newTitle->canExist() ) {
248 // Title is also an illegal title...
249 // For the moment we'll let these slide to cleanupTitles or whoever.
250 $this->output( sprintf( "... %d (%d,\"%s\")\n",
254 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
258 $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
262 $newTitle->getNamespace(),
263 $newTitle->getDBkey(),
264 $newTitle->getPrefixedText() ) );
266 $id = $newTitle->getArticleID();
268 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
276 * Resolve any conflicts
278 * @param $row Object: row from the page table to fix
279 * @param $resolvable Boolean
280 * @param $suffix String: suffix to append to the fixed page
283 private function resolveConflict( $row, $resolvable, $suffix ) {
284 if ( !$resolvable ) {
285 $this->output( "... *** old title {$row->title}\n" );
287 $row->title
.= $suffix;
288 $this->output( "... *** new title {$row->title}\n" );
289 $title = Title
::makeTitleSafe( $row->namespace, $row->title
);
291 $this->output( "... !!! invalid title\n" );
294 $id = $title->getArticleID();
296 $this->output( "... *** page exists with ID $id ***\n" );
301 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
303 $this->resolveConflictOn( $row, 'page', 'page' );
308 * Resolve a given conflict
310 * @param $row Object: row from the old broken entry
311 * @param $table String: table to update
312 * @param $prefix String: prefix for column name, like page or ar
315 private function resolveConflictOn( $row, $table, $prefix ) {
316 $this->output( "... resolving on $table... " );
317 $newTitle = Title
::makeTitleSafe( $row->namespace, $row->title
);
318 $this->db
->update( $table,
320 "{$prefix}_namespace" => $newTitle->getNamespace(),
321 "{$prefix}_title" => $newTitle->getDBkey(),
324 // "{$prefix}_namespace" => 0,
325 // "{$prefix}_title" => $row->oldtitle,
326 "{$prefix}_id" => $row->id
,
329 $this->output( "ok.\n" );
334 $maintClass = "NamespaceConflictChecker";
335 require_once RUN_MAINTENANCE_IF_MAIN
;