From: Brion Vibber Date: Wed, 8 Jun 2005 13:08:34 +0000 (+0000) Subject: Quickie namespace title conflict checker/resolver script. X-Git-Tag: 1.5.0beta1~209 X-Git-Url: http://git.cyclocoop.org/fichier?a=commitdiff_plain;h=4b326c88058d87d65f762c85c3740933078bbd32;p=lhc%2Fweb%2Fwiklou.git Quickie namespace title conflict checker/resolver script. Should work on 1.4 as well as 1.5. --- diff --git a/maintenance/namespaceDupes.php b/maintenance/namespaceDupes.php new file mode 100644 index 0000000000..94e43bcef4 --- /dev/null +++ b/maintenance/namespaceDupes.php @@ -0,0 +1,169 @@ + +# http://www.mediawiki.org/ +# +# 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 +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# 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. +# +# 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 + +$options = array( 'fix' ); + +/** */ +require_once( 'commandLine.inc' ); +#require_once( 'maintenance/userDupes.inc' ); + +class NamespaceConflictChecker { + function NamespaceConflictChecker( &$db ) { + $this->db =& $db; + } + + function checkAll( $fix ) { + global $wgContLang; + $spaces = $wgContLang->getNamespaces(); + $ok = true; + foreach( $spaces as $ns => $name ) { + $ok = $this->checkNamespace( $ns, $name, $fix ) && $ok; + } + return $ok; + } + + function checkNamespace( $ns, $name, $fix ) { + echo "Checking namespace $ns: \"$name\"\n"; + if( $name == '' ) { + echo "... skipping article namespace\n"; + return; + } + + $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 ) { + $resolvable = $this->reportConflict( $row ); + $ok = $ok && $resolvable; + if( $fix && $resolvable ) { + $ok = $this->resolveConflict( $row ) && $ok; + } + } + 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; + } + + function reportConflict( $row ) { + $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; + } + } + + function resolveConflict( $row ) { + $tables = $this->newSchema() + ? 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 ); + return true; + } + + function newSchema() { + global $wgVersion; + return version_compare( $wgVersion, '1.5alpha', 'ge' ); + } + + function pageTable() { + if( $this->newSchema() ) { + return 'page'; + } else { + return 'cur'; + } + } +} + + + + +$wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' ); + +$fix = isset( $options['fix'] ); +$dbw =& wfGetDB( DB_MASTER ); +$duper = new NamespaceConflictChecker( $dbw ); +$retval = $duper->checkAll( $fix ); + +if( $retval ) { + echo "\nLooks good!\n"; + exit( 0 ); +} else { + echo "\nOh noeees\n"; + exit( -1 ); +} + +?> \ No newline at end of file