94e43bcef4f32c9f22f5c29873e678f954ebace5
[lhc/web/wiklou.git] / maintenance / namespaceDupes.php
1 <?php
2 # Copyright (C) 2005 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
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.
9 #
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.
14 #
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
19
20 $options = array( 'fix' );
21
22 /** */
23 require_once( 'commandLine.inc' );
24 #require_once( 'maintenance/userDupes.inc' );
25
26 class NamespaceConflictChecker {
27 function NamespaceConflictChecker( &$db ) {
28 $this->db =& $db;
29 }
30
31 function checkAll( $fix ) {
32 global $wgContLang;
33 $spaces = $wgContLang->getNamespaces();
34 $ok = true;
35 foreach( $spaces as $ns => $name ) {
36 $ok = $this->checkNamespace( $ns, $name, $fix ) && $ok;
37 }
38 return $ok;
39 }
40
41 function checkNamespace( $ns, $name, $fix ) {
42 echo "Checking namespace $ns: \"$name\"\n";
43 if( $name == '' ) {
44 echo "... skipping article namespace\n";
45 return;
46 }
47
48 $conflicts = $this->getConflicts( $ns, $name );
49 $count = count( $conflicts );
50 if( $count == 0 ) {
51 echo "... no conflicts detected!\n";
52 return true;
53 }
54
55 echo "... $count conflicts detected:\n";
56 $ok = true;
57 foreach( $conflicts as $row ) {
58 $resolvable = $this->reportConflict( $row );
59 $ok = $ok && $resolvable;
60 if( $fix && $resolvable ) {
61 $ok = $this->resolveConflict( $row ) && $ok;
62 }
63 }
64 return $ok;
65 }
66
67 function getConflicts( $ns, $name ) {
68 $page = $this->newSchema() ? 'page' : 'cur';
69 $table = $this->db->tableName( $page );
70
71 $prefix = $this->db->strencode( $name );
72 $likeprefix = str_replace( '_', '\\_', $prefix);
73
74 $sql = "SELECT {$page}_id AS id,
75 {$page}_title AS oldtitle,
76 $ns AS namespace,
77 TRIM(LEADING '$prefix:' FROM {$page}_title) AS title
78 FROM {$table}
79 WHERE {$page}_namespace=0
80 AND {$page}_title LIKE '$likeprefix:%'";
81
82 $result = $this->db->query( $sql, 'NamespaceConflictChecker::getConflicts' );
83
84 $set = array();
85 while( $row = $this->db->fetchObject( $result ) ) {
86 $set[] = $row;
87 }
88 $this->db->freeResult( $result );
89
90 return $set;
91 }
92
93 function reportConflict( $row ) {
94 $newTitle = Title::makeTitle( $row->namespace, $row->title );
95 printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
96 $row->id,
97 $row->oldtitle,
98 $row->namespace,
99 $row->title,
100 $newTitle->getPrefixedText() );
101
102 $id = $newTitle->getArticleId();
103 if( $id ) {
104 echo "... *** cannot resolve automatically; page exists with ID $id ***\n";
105 return false;
106 } else {
107 return true;
108 }
109 }
110
111 function resolveConflict( $row ) {
112 $tables = $this->newSchema()
113 ? array( 'page' )
114 : array( 'cur', 'old' );
115 foreach( $tables as $table ) {
116 $this->resolveConflictOn( $row, $table );
117 }
118 return true;
119 }
120
121 function resolveConflictOn( $row, $table ) {
122 $fname = 'NamespaceConflictChecker::resolveConflictOn';
123 echo "... resolving on $table... ";
124 $this->db->update( $table,
125 array(
126 "{$table}_namespace" => $row->namespace,
127 "{$table}_title" => $row->title,
128 ),
129 array(
130 "{$table}_namespace" => 0,
131 "{$table}_title" => $row->oldtitle,
132 ),
133 $fname );
134 return true;
135 }
136
137 function newSchema() {
138 global $wgVersion;
139 return version_compare( $wgVersion, '1.5alpha', 'ge' );
140 }
141
142 function pageTable() {
143 if( $this->newSchema() ) {
144 return 'page';
145 } else {
146 return 'cur';
147 }
148 }
149 }
150
151
152
153
154 $wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
155
156 $fix = isset( $options['fix'] );
157 $dbw =& wfGetDB( DB_MASTER );
158 $duper = new NamespaceConflictChecker( $dbw );
159 $retval = $duper->checkAll( $fix );
160
161 if( $retval ) {
162 echo "\nLooks good!\n";
163 exit( 0 );
164 } else {
165 echo "\nOh noeees\n";
166 exit( -1 );
167 }
168
169 ?>