2005-06-08 13:08:34 +00:00
|
|
|
<?php
|
2007-06-26 20:18:23 +00:00
|
|
|
# Copyright (C) 2005-2007 Brion Vibber <brion@pobox.com>
|
2005-06-08 13:08:34 +00:00
|
|
|
# http://www.mediawiki.org/
|
2006-01-07 13:09:30 +00:00
|
|
|
#
|
2005-06-08 13:08:34 +00:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2006-01-07 13:09:30 +00:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
2005-06-08 13:08:34 +00:00
|
|
|
# (at your option) any later version.
|
2006-01-07 13:09:30 +00:00
|
|
|
#
|
2005-06-08 13:08:34 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
2006-01-07 13:09:30 +00:00
|
|
|
#
|
2005-06-08 13:08:34 +00:00
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
2006-04-05 07:43:17 +00:00
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-06-08 13:08:34 +00:00
|
|
|
# http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
|
2005-11-29 19:12:15 +00:00
|
|
|
$options = array( 'fix', 'suffix', 'help' );
|
2005-06-08 13:08:34 +00:00
|
|
|
|
|
|
|
|
/** */
|
|
|
|
|
require_once( 'commandLine.inc' );
|
|
|
|
|
|
2005-11-29 19:12:15 +00:00
|
|
|
if(isset( $options['help'] ) ) {
|
|
|
|
|
print <<<END
|
|
|
|
|
usage: namespaceDupes.php [--fix] [--suffix=<text>] [--help]
|
|
|
|
|
--help : this help message
|
2006-01-13 14:34:02 +00:00
|
|
|
--fix : attempt to automatically fix errors
|
2005-11-29 19:12:15 +00:00
|
|
|
--suffix=<text> : dupes will be renamed with correct namespace with <text>
|
|
|
|
|
appended after the article name.
|
2007-06-26 20:18:23 +00:00
|
|
|
--prefix=<text> : Do an explicit check for the given title prefix
|
|
|
|
|
in place of the standard namespace list.
|
2007-06-26 21:18:27 +00:00
|
|
|
--verbose : Display output for checked namespaces without conflicts
|
2005-11-29 19:12:15 +00:00
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
die;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-08 13:08:34 +00:00
|
|
|
class NamespaceConflictChecker {
|
2007-06-26 21:18:27 +00:00
|
|
|
function NamespaceConflictChecker( $db, $verbose=false ) {
|
2007-06-26 20:18:23 +00:00
|
|
|
$this->db = $db;
|
2007-06-26 21:18:27 +00:00
|
|
|
$this->verbose = $verbose;
|
2005-06-08 13:08:34 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 16:36:08 +00:00
|
|
|
function checkAll( $fix, $suffix = '' ) {
|
2007-06-26 20:18:23 +00:00
|
|
|
global $wgContLang, $wgNamespaceAliases, $wgCanonicalNamespaceNames;
|
2007-06-26 21:18:27 +00:00
|
|
|
global $wgCapitalLinks;
|
2007-06-26 20:18:23 +00:00
|
|
|
|
|
|
|
|
$spaces = array();
|
2007-06-26 21:18:27 +00:00
|
|
|
|
|
|
|
|
// List interwikis first, so they'll be overridden
|
|
|
|
|
// by any conflicting local namespaces.
|
|
|
|
|
foreach( $this->getInterwikiList() as $prefix ) {
|
|
|
|
|
$name = $wgContLang->ucfirst( $prefix );
|
|
|
|
|
$spaces[$name] = 0;
|
2007-06-26 20:18:23 +00:00
|
|
|
}
|
2007-06-26 21:18:27 +00:00
|
|
|
|
|
|
|
|
// Now pull in all canonical and alias namespaces...
|
2007-06-26 20:18:23 +00:00
|
|
|
foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
|
2007-06-26 21:18:27 +00:00
|
|
|
// This includes $wgExtraNamespaces
|
|
|
|
|
if( $name !== '' ) {
|
|
|
|
|
$spaces[$name] = $ns;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach( $wgContLang->getNamespaces() as $ns => $name ) {
|
|
|
|
|
if( $name !== '' ) {
|
|
|
|
|
$spaces[$name] = $ns;
|
|
|
|
|
}
|
2007-06-26 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
foreach( $wgNamespaceAliases as $name => $ns ) {
|
|
|
|
|
$spaces[$name] = $ns;
|
|
|
|
|
}
|
|
|
|
|
foreach( $wgContLang->namespaceAliases as $name => $ns ) {
|
|
|
|
|
$spaces[$name] = $ns;
|
|
|
|
|
}
|
2007-06-26 21:18:27 +00:00
|
|
|
|
|
|
|
|
if( !$wgCapitalLinks ) {
|
|
|
|
|
// We'll need to check for lowercase keys as well,
|
|
|
|
|
// since we're doing case-sensitive searches in the db.
|
2007-10-18 14:22:02 +00:00
|
|
|
foreach( $spaces as $name => $ns ) {
|
2007-06-26 21:18:27 +00:00
|
|
|
$lcname = $wgContLang->lcfirst( $name );
|
|
|
|
|
$spaces[$lcname] = $ns;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ksort( $spaces );
|
2007-06-26 20:18:23 +00:00
|
|
|
asort( $spaces );
|
|
|
|
|
|
2005-06-08 13:08:34 +00:00
|
|
|
$ok = true;
|
2007-06-26 20:18:23 +00:00
|
|
|
foreach( $spaces as $name => $ns ) {
|
2005-06-08 16:36:08 +00:00
|
|
|
$ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
|
2005-06-08 13:08:34 +00:00
|
|
|
}
|
|
|
|
|
return $ok;
|
|
|
|
|
}
|
2007-06-26 21:18:27 +00:00
|
|
|
|
|
|
|
|
private function getInterwikiList() {
|
|
|
|
|
$result = $this->db->select( 'interwiki', array( 'iw_prefix' ) );
|
|
|
|
|
while( $row = $this->db->fetchObject( $result ) ) {
|
|
|
|
|
$prefixes[] = $row->iw_prefix;
|
|
|
|
|
}
|
|
|
|
|
$this->db->freeResult( $result );
|
|
|
|
|
return $prefixes;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 16:36:08 +00:00
|
|
|
function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
|
2007-06-26 21:18:27 +00:00
|
|
|
if( $ns == 0 ) {
|
|
|
|
|
$header = "Checking interwiki prefix: \"$name\"\n";
|
|
|
|
|
} else {
|
|
|
|
|
$header = "Checking namespace $ns: \"$name\"\n";
|
2005-06-08 13:08:34 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 13:08:34 +00:00
|
|
|
$conflicts = $this->getConflicts( $ns, $name );
|
|
|
|
|
$count = count( $conflicts );
|
|
|
|
|
if( $count == 0 ) {
|
2007-06-26 21:18:27 +00:00
|
|
|
if( $this->verbose ) {
|
|
|
|
|
echo $header;
|
|
|
|
|
echo "... no conflicts detected!\n";
|
|
|
|
|
}
|
2005-06-08 13:08:34 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2007-06-26 21:18:27 +00:00
|
|
|
echo $header;
|
2005-06-08 13:08:34 +00:00
|
|
|
echo "... $count conflicts detected:\n";
|
|
|
|
|
$ok = true;
|
|
|
|
|
foreach( $conflicts as $row ) {
|
2005-06-08 16:36:08 +00:00
|
|
|
$resolvable = $this->reportConflict( $row, $suffix );
|
2005-06-08 13:08:34 +00:00
|
|
|
$ok = $ok && $resolvable;
|
2005-06-08 16:36:08 +00:00
|
|
|
if( $fix && ( $resolvable || $suffix != '' ) ) {
|
|
|
|
|
$ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
|
2005-06-08 13:08:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $ok;
|
|
|
|
|
}
|
2006-06-17 12:51:41 +00:00
|
|
|
|
|
|
|
|
/**
|
2007-07-11 08:09:21 +00:00
|
|
|
* @todo: do this for reals
|
2006-06-17 12:51:41 +00:00
|
|
|
*/
|
2006-06-28 19:15:32 +00:00
|
|
|
function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
|
|
|
|
|
echo "Checking prefix \"$prefix\" vs namespace $key\n";
|
|
|
|
|
return $this->checkNamespace( $key, $prefix, $fix, $suffix );
|
2006-06-17 12:51:41 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 13:08:34 +00:00
|
|
|
function getConflicts( $ns, $name ) {
|
2007-06-26 20:18:23 +00:00
|
|
|
$page = 'page';
|
2005-06-08 13:08:34 +00:00
|
|
|
$table = $this->db->tableName( $page );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 13:08:34 +00:00
|
|
|
$prefix = $this->db->strencode( $name );
|
|
|
|
|
$likeprefix = str_replace( '_', '\\_', $prefix);
|
2007-06-26 21:18:27 +00:00
|
|
|
$encNamespace = $this->db->addQuotes( $ns );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2007-06-26 21:18:27 +00:00
|
|
|
$titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
|
|
|
|
|
if( $ns == 0 ) {
|
|
|
|
|
// An interwiki; try an alternate encoding with '-' for ':'
|
|
|
|
|
$titleSql = "CONCAT('$prefix-',$titleSql)";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sql = "SELECT {$page}_id AS id,
|
|
|
|
|
{$page}_title AS oldtitle,
|
|
|
|
|
$encNamespace AS namespace,
|
|
|
|
|
$titleSql AS title
|
2005-06-08 13:08:34 +00:00
|
|
|
FROM {$table}
|
|
|
|
|
WHERE {$page}_namespace=0
|
|
|
|
|
AND {$page}_title LIKE '$likeprefix:%'";
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 13:08:34 +00:00
|
|
|
$result = $this->db->query( $sql, 'NamespaceConflictChecker::getConflicts' );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 13:08:34 +00:00
|
|
|
$set = array();
|
|
|
|
|
while( $row = $this->db->fetchObject( $result ) ) {
|
|
|
|
|
$set[] = $row;
|
|
|
|
|
}
|
|
|
|
|
$this->db->freeResult( $result );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 13:08:34 +00:00
|
|
|
return $set;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 16:36:08 +00:00
|
|
|
function reportConflict( $row, $suffix ) {
|
2006-08-16 20:28:59 +00:00
|
|
|
$newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
|
2005-06-08 13:08:34 +00:00
|
|
|
printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
|
|
|
|
|
$row->id,
|
|
|
|
|
$row->oldtitle,
|
2006-08-16 20:28:59 +00:00
|
|
|
$newTitle->getNamespace(),
|
|
|
|
|
$newTitle->getDbKey(),
|
2005-06-08 13:08:34 +00:00
|
|
|
$newTitle->getPrefixedText() );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 13:08:34 +00:00
|
|
|
$id = $newTitle->getArticleId();
|
|
|
|
|
if( $id ) {
|
|
|
|
|
echo "... *** cannot resolve automatically; page exists with ID $id ***\n";
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 16:36:08 +00:00
|
|
|
function resolveConflict( $row, $resolvable, $suffix ) {
|
|
|
|
|
if( !$resolvable ) {
|
|
|
|
|
$row->title .= $suffix;
|
2006-08-16 20:28:59 +00:00
|
|
|
$title = Title::makeTitleSafe( $row->namespace, $row->title );
|
2005-06-08 16:36:08 +00:00
|
|
|
echo "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
|
|
|
|
|
}
|
2007-06-26 20:18:23 +00:00
|
|
|
$tables = array( 'page' );
|
2005-06-08 13:08:34 +00:00
|
|
|
foreach( $tables as $table ) {
|
|
|
|
|
$this->resolveConflictOn( $row, $table );
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-08 13:08:34 +00:00
|
|
|
function resolveConflictOn( $row, $table ) {
|
|
|
|
|
$fname = 'NamespaceConflictChecker::resolveConflictOn';
|
|
|
|
|
echo "... resolving on $table... ";
|
2006-08-16 20:28:59 +00:00
|
|
|
$newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
|
2005-06-08 13:08:34 +00:00
|
|
|
$this->db->update( $table,
|
|
|
|
|
array(
|
2006-08-16 20:28:59 +00:00
|
|
|
"{$table}_namespace" => $newTitle->getNamespace(),
|
|
|
|
|
"{$table}_title" => $newTitle->getDbKey(),
|
2005-06-08 13:08:34 +00:00
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
"{$table}_namespace" => 0,
|
|
|
|
|
"{$table}_title" => $row->oldtitle,
|
|
|
|
|
),
|
|
|
|
|
$fname );
|
2005-06-08 16:36:08 +00:00
|
|
|
echo "ok.\n";
|
2005-06-08 13:08:34 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
|
|
|
|
|
|
2007-06-26 21:18:27 +00:00
|
|
|
$verbose = isset( $options['verbose'] );
|
2005-06-08 13:08:34 +00:00
|
|
|
$fix = isset( $options['fix'] );
|
2005-06-08 16:36:08 +00:00
|
|
|
$suffix = isset( $options['suffix'] ) ? $options['suffix'] : '';
|
2006-06-17 12:51:41 +00:00
|
|
|
$prefix = isset( $options['prefix'] ) ? $options['prefix'] : '';
|
2006-06-28 19:15:32 +00:00
|
|
|
$key = isset( $options['key'] ) ? intval( $options['key'] ) : 0;
|
2007-06-26 21:18:27 +00:00
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2007-06-26 21:18:27 +00:00
|
|
|
$duper = new NamespaceConflictChecker( $dbw, $verbose );
|
2006-06-17 12:51:41 +00:00
|
|
|
|
|
|
|
|
if( $prefix ) {
|
2006-06-28 19:15:32 +00:00
|
|
|
$retval = $duper->checkPrefix( $key, $prefix, $fix, $suffix );
|
2006-06-17 12:51:41 +00:00
|
|
|
} else {
|
|
|
|
|
$retval = $duper->checkAll( $fix, $suffix );
|
|
|
|
|
}
|
2005-06-08 13:08:34 +00:00
|
|
|
|
|
|
|
|
if( $retval ) {
|
|
|
|
|
echo "\nLooks good!\n";
|
|
|
|
|
exit( 0 );
|
|
|
|
|
} else {
|
|
|
|
|
echo "\nOh noeees\n";
|
|
|
|
|
exit( -1 );
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-29 01:19:14 +00:00
|
|
|
|