summaryrefslogtreecommitdiff
path: root/maintenance/namespaceDupes.php
diff options
context:
space:
mode:
Diffstat (limited to 'maintenance/namespaceDupes.php')
-rw-r--r--maintenance/namespaceDupes.php113
1 files changed, 86 insertions, 27 deletions
diff --git a/maintenance/namespaceDupes.php b/maintenance/namespaceDupes.php
index c5c1ec58..16796ba3 100644
--- a/maintenance/namespaceDupes.php
+++ b/maintenance/namespaceDupes.php
@@ -1,5 +1,5 @@
<?php
-# Copyright (C) 2005 Brion Vibber <brion@pobox.com>
+# Copyright (C) 2005-2007 Brion Vibber <brion@pobox.com>
# http://www.mediawiki.org/
#
# This program is free software; you can redistribute it and/or modify
@@ -21,7 +21,6 @@ $options = array( 'fix', 'suffix', 'help' );
/** */
require_once( 'commandLine.inc' );
-#require_once( 'maintenance/userDupes.inc' );
if(isset( $options['help'] ) ) {
print <<<END
@@ -30,40 +29,97 @@ usage: namespaceDupes.php [--fix] [--suffix=<text>] [--help]
--fix : attempt to automatically fix errors
--suffix=<text> : dupes will be renamed with correct namespace with <text>
appended after the article name.
+ --prefix=<text> : Do an explicit check for the given title prefix
+ in place of the standard namespace list.
+ --verbose : Display output for checked namespaces without conflicts
END;
die;
}
class NamespaceConflictChecker {
- function NamespaceConflictChecker( &$db ) {
- $this->db =& $db;
+ function NamespaceConflictChecker( $db, $verbose=false ) {
+ $this->db = $db;
+ $this->verbose = $verbose;
}
function checkAll( $fix, $suffix = '' ) {
- global $wgContLang;
- $spaces = $wgContLang->getNamespaces();
+ global $wgContLang, $wgNamespaceAliases, $wgCanonicalNamespaceNames;
+ global $wgCapitalLinks;
+
+ $spaces = array();
+
+ // 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;
+ }
+
+ // Now pull in all canonical and alias namespaces...
+ foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
+ // This includes $wgExtraNamespaces
+ if( $name !== '' ) {
+ $spaces[$name] = $ns;
+ }
+ }
+ foreach( $wgContLang->getNamespaces() as $ns => $name ) {
+ if( $name !== '' ) {
+ $spaces[$name] = $ns;
+ }
+ }
+ foreach( $wgNamespaceAliases as $name => $ns ) {
+ $spaces[$name] = $ns;
+ }
+ foreach( $wgContLang->namespaceAliases as $name => $ns ) {
+ $spaces[$name] = $ns;
+ }
+
+ if( !$wgCapitalLinks ) {
+ // We'll need to check for lowercase keys as well,
+ // since we're doing case-sensitive searches in the db.
+ foreach( array_values( $spaces ) as $name => $ns ) {
+ $lcname = $wgContLang->lcfirst( $name );
+ $spaces[$lcname] = $ns;
+ }
+ }
+ ksort( $spaces );
+ asort( $spaces );
+
$ok = true;
- foreach( $spaces as $ns => $name ) {
+ foreach( $spaces as $name => $ns ) {
$ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
}
return $ok;
}
+
+ 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;
+ }
function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
- echo "Checking namespace $ns: \"$name\"\n";
- if( $name == '' ) {
- echo "... skipping article namespace\n";
- return true;
+ if( $ns == 0 ) {
+ $header = "Checking interwiki prefix: \"$name\"\n";
+ } else {
+ $header = "Checking namespace $ns: \"$name\"\n";
}
$conflicts = $this->getConflicts( $ns, $name );
$count = count( $conflicts );
if( $count == 0 ) {
- echo "... no conflicts detected!\n";
+ if( $this->verbose ) {
+ echo $header;
+ echo "... no conflicts detected!\n";
+ }
return true;
}
+ echo $header;
echo "... $count conflicts detected:\n";
$ok = true;
foreach( $conflicts as $row ) {
@@ -77,7 +133,7 @@ class NamespaceConflictChecker {
}
/**
- * @fixme: do this for reals
+ * @todo: do this for reals
*/
function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
echo "Checking prefix \"$prefix\" vs namespace $key\n";
@@ -85,16 +141,23 @@ class NamespaceConflictChecker {
}
function getConflicts( $ns, $name ) {
- $page = $this->newSchema() ? 'page' : 'cur';
+ $page = 'page';
$table = $this->db->tableName( $page );
$prefix = $this->db->strencode( $name );
$likeprefix = str_replace( '_', '\\_', $prefix);
+ $encNamespace = $this->db->addQuotes( $ns );
- $sql = "SELECT {$page}_id AS id,
- {$page}_title AS oldtitle,
- $ns AS namespace,
- TRIM(LEADING '$prefix:' FROM {$page}_title) AS title
+ $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
FROM {$table}
WHERE {$page}_namespace=0
AND {$page}_title LIKE '$likeprefix:%'";
@@ -134,9 +197,7 @@ class NamespaceConflictChecker {
$title = Title::makeTitleSafe( $row->namespace, $row->title );
echo "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
}
- $tables = $this->newSchema()
- ? array( 'page' )
- : array( 'cur', 'old' );
+ $tables = array( 'page' );
foreach( $tables as $table ) {
$this->resolveConflictOn( $row, $table );
}
@@ -160,10 +221,6 @@ class NamespaceConflictChecker {
echo "ok.\n";
return true;
}
-
- function newSchema() {
- return class_exists( 'Revision' );
- }
}
@@ -171,12 +228,14 @@ class NamespaceConflictChecker {
$wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
+$verbose = isset( $options['verbose'] );
$fix = isset( $options['fix'] );
$suffix = isset( $options['suffix'] ) ? $options['suffix'] : '';
$prefix = isset( $options['prefix'] ) ? $options['prefix'] : '';
$key = isset( $options['key'] ) ? intval( $options['key'] ) : 0;
+
$dbw = wfGetDB( DB_MASTER );
-$duper = new NamespaceConflictChecker( $dbw );
+$duper = new NamespaceConflictChecker( $dbw, $verbose );
if( $prefix ) {
$retval = $duper->checkPrefix( $key, $prefix, $fix, $suffix );
@@ -192,4 +251,4 @@ if( $retval ) {
exit( -1 );
}
-?>
+