removed wikisql user
[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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 $options = array( 'fix', 'suffix', 'help' );
21
22 /** */
23 require_once( 'commandLine.inc' );
24 #require_once( 'maintenance/userDupes.inc' );
25
26 if(isset( $options['help'] ) ) {
27 print <<<END
28 usage: namespaceDupes.php [--fix] [--suffix=<text>] [--help]
29 --help : this help message
30 --fix : attempt to automatically fix errors
31 --suffix=<text> : dupes will be renamed with correct namespace with <text>
32 appended after the article name.
33
34 END;
35 die;
36 }
37
38 class NamespaceConflictChecker {
39 function NamespaceConflictChecker( &$db ) {
40 $this->db =& $db;
41 }
42
43 function checkAll( $fix, $suffix = '' ) {
44 global $wgContLang;
45 $spaces = $wgContLang->getNamespaces();
46 $ok = true;
47 foreach( $spaces as $ns => $name ) {
48 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
49 }
50 return $ok;
51 }
52
53 function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
54 echo "Checking namespace $ns: \"$name\"\n";
55 if( $name == '' ) {
56 echo "... skipping article namespace\n";
57 return true;
58 }
59
60 $conflicts = $this->getConflicts( $ns, $name );
61 $count = count( $conflicts );
62 if( $count == 0 ) {
63 echo "... no conflicts detected!\n";
64 return true;
65 }
66
67 echo "... $count conflicts detected:\n";
68 $ok = true;
69 foreach( $conflicts as $row ) {
70 $resolvable = $this->reportConflict( $row, $suffix );
71 $ok = $ok && $resolvable;
72 if( $fix && ( $resolvable || $suffix != '' ) ) {
73 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
74 }
75 }
76 return $ok;
77 }
78
79 /**
80 * @fixme: do this for reals
81 */
82 function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
83 echo "Checking prefix \"$prefix\" vs namespace $key\n";
84 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
85 }
86
87 function getConflicts( $ns, $name ) {
88 $page = $this->newSchema() ? 'page' : 'cur';
89 $table = $this->db->tableName( $page );
90
91 $prefix = $this->db->strencode( $name );
92 $likeprefix = str_replace( '_', '\\_', $prefix);
93
94 $sql = "SELECT {$page}_id AS id,
95 {$page}_title AS oldtitle,
96 $ns AS namespace,
97 TRIM(LEADING '$prefix:' FROM {$page}_title) AS title
98 FROM {$table}
99 WHERE {$page}_namespace=0
100 AND {$page}_title LIKE '$likeprefix:%'";
101
102 $result = $this->db->query( $sql, 'NamespaceConflictChecker::getConflicts' );
103
104 $set = array();
105 while( $row = $this->db->fetchObject( $result ) ) {
106 $set[] = $row;
107 }
108 $this->db->freeResult( $result );
109
110 return $set;
111 }
112
113 function reportConflict( $row, $suffix ) {
114 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
115 printf( "... %d (0,\"%s\") -> (%d,\"%s\") [[%s]]\n",
116 $row->id,
117 $row->oldtitle,
118 $newTitle->getNamespace(),
119 $newTitle->getDbKey(),
120 $newTitle->getPrefixedText() );
121
122 $id = $newTitle->getArticleId();
123 if( $id ) {
124 echo "... *** cannot resolve automatically; page exists with ID $id ***\n";
125 return false;
126 } else {
127 return true;
128 }
129 }
130
131 function resolveConflict( $row, $resolvable, $suffix ) {
132 if( !$resolvable ) {
133 $row->title .= $suffix;
134 $title = Title::makeTitleSafe( $row->namespace, $row->title );
135 echo "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n";
136 }
137 $tables = $this->newSchema()
138 ? array( 'page' )
139 : array( 'cur', 'old' );
140 foreach( $tables as $table ) {
141 $this->resolveConflictOn( $row, $table );
142 }
143 return true;
144 }
145
146 function resolveConflictOn( $row, $table ) {
147 $fname = 'NamespaceConflictChecker::resolveConflictOn';
148 echo "... resolving on $table... ";
149 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
150 $this->db->update( $table,
151 array(
152 "{$table}_namespace" => $newTitle->getNamespace(),
153 "{$table}_title" => $newTitle->getDbKey(),
154 ),
155 array(
156 "{$table}_namespace" => 0,
157 "{$table}_title" => $row->oldtitle,
158 ),
159 $fname );
160 echo "ok.\n";
161 return true;
162 }
163
164 function newSchema() {
165 return class_exists( 'Revision' );
166 }
167 }
168
169
170
171
172 $wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
173
174 $fix = isset( $options['fix'] );
175 $suffix = isset( $options['suffix'] ) ? $options['suffix'] : '';
176 $prefix = isset( $options['prefix'] ) ? $options['prefix'] : '';
177 $key = isset( $options['key'] ) ? intval( $options['key'] ) : 0;
178 $dbw =& wfGetDB( DB_MASTER );
179 $duper = new NamespaceConflictChecker( $dbw );
180
181 if( $prefix ) {
182 $retval = $duper->checkPrefix( $key, $prefix, $fix, $suffix );
183 } else {
184 $retval = $duper->checkAll( $fix, $suffix );
185 }
186
187 if( $retval ) {
188 echo "\nLooks good!\n";
189 exit( 0 );
190 } else {
191 echo "\nOh noeees\n";
192 exit( -1 );
193 }
194
195 ?>