2 # Copyright (C) 2005 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
20 $options = array( 'fix', 'suffix' );
23 require_once( 'commandLine.inc' );
24 #require_once( 'maintenance/userDupes.inc' );
26 class NamespaceConflictChecker
{
27 function NamespaceConflictChecker( &$db ) {
31 function checkAll( $fix, $suffix = '' ) {
33 $spaces = $wgContLang->getNamespaces();
35 foreach( $spaces as $ns => $name ) {
36 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
41 function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
42 echo "Checking namespace $ns: \"$name\"\n";
44 echo "... skipping article namespace\n";
48 $conflicts = $this->getConflicts( $ns, $name );
49 $count = count( $conflicts );
51 echo "... no conflicts detected!\n";
55 echo "... $count conflicts detected:\n";
57 foreach( $conflicts as $row ) {
58 $resolvable = $this->reportConflict( $row, $suffix );
59 $ok = $ok && $resolvable;
60 if( $fix && ( $resolvable ||
$suffix != '' ) ) {
61 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
67 function getConflicts( $ns, $name ) {
68 $page = $this->newSchema() ?
'page' : 'cur';
69 $table = $this->db
->tableName( $page );
71 $prefix = $this->db
->strencode( $name );
72 $likeprefix = str_replace( '_', '\\_', $prefix);
74 $sql = "SELECT {$page}_id AS id,
75 {$page}_title AS oldtitle,
77 TRIM(LEADING '$prefix:' FROM {$page}_title) AS title
79 WHERE {$page}_namespace=0
80 AND {$page}_title LIKE '$likeprefix:%'";
82 $result = $this->db
->query( $sql, 'NamespaceConflictChecker::getConflicts' );
85 while( $row = $this->db
->fetchObject( $result ) ) {
88 $this->db
->freeResult( $result );
93 function reportConflict( $row, $suffix ) {
94 $newTitle = Title
::makeTitle( $row->namespace, $row->title
);
95 printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
100 $newTitle->getPrefixedText() );
102 $id = $newTitle->getArticleId();
104 echo "... *** cannot resolve automatically; page exists with ID $id ***\n";
111 function resolveConflict( $row, $resolvable, $suffix ) {
113 $row->title
.= $suffix;
114 $title = Title
::makeTitle( $row->namespace, $row->title
);
115 echo "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
117 $tables = $this->newSchema()
119 : array( 'cur', 'old' );
120 foreach( $tables as $table ) {
121 $this->resolveConflictOn( $row, $table );
126 function resolveConflictOn( $row, $table ) {
127 $fname = 'NamespaceConflictChecker::resolveConflictOn';
128 echo "... resolving on $table... ";
129 $this->db
->update( $table,
131 "{$table}_namespace" => $row->namespace,
132 "{$table}_title" => $row->title
,
135 "{$table}_namespace" => 0,
136 "{$table}_title" => $row->oldtitle
,
143 function newSchema() {
145 return version_compare( $wgVersion, '1.5alpha', 'ge' );
152 $wgTitle = Title
::newFromText( 'Namespace title conflict cleanup script' );
154 $fix = isset( $options['fix'] );
155 $suffix = isset( $options['suffix'] ) ?
$options['suffix'] : '';
156 $dbw =& wfGetDB( DB_MASTER
);
157 $duper = new NamespaceConflictChecker( $dbw );
158 $retval = $duper->checkAll( $fix, $suffix );
161 echo "\nLooks good!\n";
164 echo "\nOh noeees\n";