2005-06-08 13:08:34 +00:00
|
|
|
<?php
|
|
|
|
|
# Copyright (C) 2005 Brion Vibber <brion@pobox.com>
|
|
|
|
|
# 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.,
|
|
|
|
|
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
# 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' );
|
|
|
|
|
#require_once( 'maintenance/userDupes.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
|
|
|
|
|
--fix : attempt to automaticly fix errors
|
|
|
|
|
--suffix=<text> : dupes will be renamed with correct namespace with <text>
|
|
|
|
|
appended after the article name.
|
|
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
die;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-08 13:08:34 +00:00
|
|
|
class NamespaceConflictChecker {
|
|
|
|
|
function NamespaceConflictChecker( &$db ) {
|
|
|
|
|
$this->db =& $db;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-08 16:36:08 +00:00
|
|
|
function checkAll( $fix, $suffix = '' ) {
|
2005-06-08 13:08:34 +00:00
|
|
|
global $wgContLang;
|
|
|
|
|
$spaces = $wgContLang->getNamespaces();
|
|
|
|
|
$ok = true;
|
|
|
|
|
foreach( $spaces as $ns => $name ) {
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-08 16:36:08 +00:00
|
|
|
function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
|
2005-06-08 13:08:34 +00:00
|
|
|
echo "Checking namespace $ns: \"$name\"\n";
|
|
|
|
|
if( $name == '' ) {
|
|
|
|
|
echo "... skipping article namespace\n";
|
2005-06-08 16:36:08 +00:00
|
|
|
return true;
|
2005-06-08 13:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$conflicts = $this->getConflicts( $ns, $name );
|
|
|
|
|
$count = count( $conflicts );
|
|
|
|
|
if( $count == 0 ) {
|
|
|
|
|
echo "... no conflicts detected!\n";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getConflicts( $ns, $name ) {
|
|
|
|
|
$page = $this->newSchema() ? 'page' : 'cur';
|
|
|
|
|
$table = $this->db->tableName( $page );
|
|
|
|
|
|
|
|
|
|
$prefix = $this->db->strencode( $name );
|
|
|
|
|
$likeprefix = str_replace( '_', '\\_', $prefix);
|
|
|
|
|
|
|
|
|
|
$sql = "SELECT {$page}_id AS id,
|
|
|
|
|
{$page}_title AS oldtitle,
|
|
|
|
|
$ns AS namespace,
|
|
|
|
|
TRIM(LEADING '$prefix:' FROM {$page}_title) AS title
|
|
|
|
|
FROM {$table}
|
|
|
|
|
WHERE {$page}_namespace=0
|
|
|
|
|
AND {$page}_title LIKE '$likeprefix:%'";
|
|
|
|
|
|
|
|
|
|
$result = $this->db->query( $sql, 'NamespaceConflictChecker::getConflicts' );
|
|
|
|
|
|
|
|
|
|
$set = array();
|
|
|
|
|
while( $row = $this->db->fetchObject( $result ) ) {
|
|
|
|
|
$set[] = $row;
|
|
|
|
|
}
|
|
|
|
|
$this->db->freeResult( $result );
|
|
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-08 16:36:08 +00:00
|
|
|
function reportConflict( $row, $suffix ) {
|
2005-06-08 13:08:34 +00:00
|
|
|
$newTitle = Title::makeTitle( $row->namespace, $row->title );
|
|
|
|
|
printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
|
|
|
|
|
$row->id,
|
|
|
|
|
$row->oldtitle,
|
|
|
|
|
$row->namespace,
|
|
|
|
|
$row->title,
|
|
|
|
|
$newTitle->getPrefixedText() );
|
|
|
|
|
|
|
|
|
|
$id = $newTitle->getArticleId();
|
|
|
|
|
if( $id ) {
|
|
|
|
|
echo "... *** cannot resolve automatically; page exists with ID $id ***\n";
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-08 16:36:08 +00:00
|
|
|
function resolveConflict( $row, $resolvable, $suffix ) {
|
|
|
|
|
if( !$resolvable ) {
|
|
|
|
|
$row->title .= $suffix;
|
|
|
|
|
$title = Title::makeTitle( $row->namespace, $row->title );
|
|
|
|
|
echo "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
|
|
|
|
|
}
|
2006-01-07 13:09:30 +00:00
|
|
|
$tables = $this->newSchema()
|
2005-06-08 13:08:34 +00:00
|
|
|
? array( 'page' )
|
|
|
|
|
: array( 'cur', 'old' );
|
|
|
|
|
foreach( $tables as $table ) {
|
|
|
|
|
$this->resolveConflictOn( $row, $table );
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resolveConflictOn( $row, $table ) {
|
|
|
|
|
$fname = 'NamespaceConflictChecker::resolveConflictOn';
|
|
|
|
|
echo "... resolving on $table... ";
|
|
|
|
|
$this->db->update( $table,
|
|
|
|
|
array(
|
|
|
|
|
"{$table}_namespace" => $row->namespace,
|
|
|
|
|
"{$table}_title" => $row->title,
|
|
|
|
|
),
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function newSchema() {
|
2005-08-24 07:25:16 +00:00
|
|
|
return class_exists( 'Revision' );
|
2005-06-08 13:08:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
|
|
|
|
|
|
|
|
|
|
$fix = isset( $options['fix'] );
|
2005-06-08 16:36:08 +00:00
|
|
|
$suffix = isset( $options['suffix'] ) ? $options['suffix'] : '';
|
2005-06-08 13:08:34 +00:00
|
|
|
$dbw =& wfGetDB( DB_MASTER );
|
|
|
|
|
$duper = new NamespaceConflictChecker( $dbw );
|
2005-06-08 16:36:08 +00:00
|
|
|
$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 );
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-29 19:12:15 +00:00
|
|
|
?>
|