* fixed table duplication for unit tests
[lhc/web/wiklou.git] / maintenance / namespaceDupes.php
1 <?php
2 /**
3 * Check for articles to fix after adding/deleting namespaces
4 *
5 * Copyright (C) 2005-2007 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @ingroup Maintenance
24 */
25
26 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
27
28 class NamespaceConflictChecker extends Maintenance {
29 public function __construct() {
30 parent::__construct();
31 $this->mDescription = "";
32 $this->addOption( 'fix', 'Attempt to automatically fix errors' );
33 $this->addOption( 'suffix', "Dupes will be renamed with correct namespace with " .
34 "<text> appended after the article name", false, true );
35 $this->addOption( 'prefix', "Do an explicit check for the given title prefix " .
36 "appended after the article name", false, true );
37 }
38
39 public function execute() {
40 global $wgTitle;
41
42 $this->db = wfGetDB( DB_MASTER );
43 $wgTitle = Title::newFromText( 'Namespace title conflict cleanup script' );
44
45 $fix = $this->hasOption( 'fix' );
46 $suffix = $this->getOption( 'suffix', '' );
47 $prefix = $this->getOption( 'prefix', '' );
48 $key = intval( $this->getOption( 'key', 0 ) );
49
50 if ( $prefix ) {
51 $retval = $this->checkPrefix( $key, $prefix, $fix, $suffix );
52 } else {
53 $retval = $this->checkAll( $fix, $suffix );
54 }
55
56 if ( $retval ) {
57 $this->output( "\nLooks good!\n" );
58 } else {
59 $this->output( "\nOh noeees\n" );
60 }
61 }
62
63 /**
64 * @todo Document
65 * @param $fix Boolean: whether or not to fix broken entries
66 * @param $suffix String: suffix to append to renamed articles
67 */
68 private function checkAll( $fix, $suffix = '' ) {
69 global $wgContLang, $wgNamespaceAliases, $wgCapitalLinks;
70
71 $spaces = array();
72
73 // List interwikis first, so they'll be overridden
74 // by any conflicting local namespaces.
75 foreach ( $this->getInterwikiList() as $prefix ) {
76 $name = $wgContLang->ucfirst( $prefix );
77 $spaces[$name] = 0;
78 }
79
80 // Now pull in all canonical and alias namespaces...
81 foreach ( MWNamespace::getCanonicalNamespaces() as $ns => $name ) {
82 // This includes $wgExtraNamespaces
83 if ( $name !== '' ) {
84 $spaces[$name] = $ns;
85 }
86 }
87 foreach ( $wgContLang->getNamespaces() as $ns => $name ) {
88 if ( $name !== '' ) {
89 $spaces[$name] = $ns;
90 }
91 }
92 foreach ( $wgNamespaceAliases as $name => $ns ) {
93 $spaces[$name] = $ns;
94 }
95 foreach ( $wgContLang->getNamespaceAliases() as $name => $ns ) {
96 $spaces[$name] = $ns;
97 }
98
99 // We'll need to check for lowercase keys as well,
100 // since we're doing case-sensitive searches in the db.
101 foreach ( $spaces as $name => $ns ) {
102 $moreNames = array();
103 $moreNames[] = $wgContLang->uc( $name );
104 $moreNames[] = $wgContLang->ucfirst( $wgContLang->lc( $name ) );
105 $moreNames[] = $wgContLang->ucwords( $name );
106 $moreNames[] = $wgContLang->ucwords( $wgContLang->lc( $name ) );
107 $moreNames[] = $wgContLang->ucwordbreaks( $name );
108 $moreNames[] = $wgContLang->ucwordbreaks( $wgContLang->lc( $name ) );
109 if ( !$wgCapitalLinks ) {
110 foreach ( $moreNames as $altName ) {
111 $moreNames[] = $wgContLang->lcfirst( $altName );
112 }
113 $moreNames[] = $wgContLang->lcfirst( $name );
114 }
115 foreach ( array_unique( $moreNames ) as $altName ) {
116 if ( $altName !== $name ) {
117 $spaces[$altName] = $ns;
118 }
119 }
120 }
121
122 ksort( $spaces );
123 asort( $spaces );
124
125 $ok = true;
126 foreach ( $spaces as $name => $ns ) {
127 $ok = $this->checkNamespace( $ns, $name, $fix, $suffix ) && $ok;
128 }
129 return $ok;
130 }
131
132 /**
133 * Get the interwiki list
134 *
135 * @todo Needs to respect interwiki cache!
136 * @return Array
137 */
138 private function getInterwikiList() {
139 $result = $this->db->select( 'interwiki', array( 'iw_prefix' ) );
140 $prefixes = array();
141 foreach ( $result as $row ) {
142 $prefixes[] = $row->iw_prefix;
143 }
144 return $prefixes;
145 }
146
147 /**
148 * @todo Document
149 * @param $ns Integer: a namespace id
150 * @param $name String
151 * @param $fix Boolean: whether to fix broken entries
152 * @param $suffix String: suffix to append to renamed articles
153 */
154 private function checkNamespace( $ns, $name, $fix, $suffix = '' ) {
155 $conflicts = $this->getConflicts( $ns, $name );
156 $count = count( $conflicts );
157 if ( $count == 0 ) {
158 return true;
159 }
160
161 $ok = true;
162 foreach ( $conflicts as $row ) {
163 $resolvable = $this->reportConflict( $row, $suffix );
164 $ok = $ok && $resolvable;
165 if ( $fix && ( $resolvable || $suffix != '' ) ) {
166 $ok = $this->resolveConflict( $row, $resolvable, $suffix ) && $ok;
167 }
168 }
169 return $ok;
170 }
171
172 /**
173 * @todo: do this for reals
174 */
175 private function checkPrefix( $key, $prefix, $fix, $suffix = '' ) {
176 $this->output( "Checking prefix \"$prefix\" vs namespace $key\n" );
177 return $this->checkNamespace( $key, $prefix, $fix, $suffix );
178 }
179
180 /**
181 * Find pages in mainspace that have a prefix of the new namespace
182 * so we know titles that will need migrating
183 *
184 * @param $ns Integer: namespace id (id for new namespace?)
185 * @param $name String: prefix that is being made a namespace
186 */
187 private function getConflicts( $ns, $name ) {
188 $page = 'page';
189 $table = $this->db->tableName( $page );
190
191 $prefix = $this->db->strencode( $name );
192 $encNamespace = $this->db->addQuotes( $ns );
193
194 $titleSql = "TRIM(LEADING '$prefix:' FROM {$page}_title)";
195 if ( $ns == 0 ) {
196 // An interwiki; try an alternate encoding with '-' for ':'
197 $titleSql = $this->db->buildConcat( array( "'$prefix-'", $titleSql ) );
198 }
199
200 $sql = "SELECT {$page}_id AS id,
201 {$page}_title AS oldtitle,
202 $encNamespace + {$page}_namespace AS namespace,
203 $titleSql AS title,
204 {$page}_namespace AS oldnamespace
205 FROM {$table}
206 WHERE ( {$page}_namespace=0 OR {$page}_namespace=1 )
207 AND {$page}_title " . $this->db->buildLike( $name . ':', $this->db->anyString() );
208
209 $result = $this->db->query( $sql, __METHOD__ );
210
211 $set = array();
212 foreach ( $result as $row ) {
213 $set[] = $row;
214 }
215 return $set;
216 }
217
218 /**
219 * Report any conflicts we find
220 */
221 private function reportConflict( $row, $suffix ) {
222 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
223 if ( is_null( $newTitle ) || !$newTitle->canExist() ) {
224 // Title is also an illegal title...
225 // For the moment we'll let these slide to cleanupTitles or whoever.
226 $this->output( sprintf( "... %d (%d,\"%s\")\n",
227 $row->id,
228 $row->oldnamespace,
229 $row->oldtitle ) );
230 $this->output( "... *** cannot resolve automatically; illegal title ***\n" );
231 return false;
232 }
233
234 $this->output( sprintf( "... %d (%d,\"%s\") -> (%d,\"%s\") [[%s]]\n",
235 $row->id,
236 $row->oldnamespace,
237 $row->oldtitle,
238 $newTitle->getNamespace(),
239 $newTitle->getDBkey(),
240 $newTitle->getPrefixedText() ) );
241
242 $id = $newTitle->getArticleId();
243 if ( $id ) {
244 $this->output( "... *** cannot resolve automatically; page exists with ID $id ***\n" );
245 return false;
246 } else {
247 return true;
248 }
249 }
250
251 /**
252 * Resolve any conflicts
253 *
254 * @param $row Object: row from the page table to fix
255 * @param $resolvable Boolean
256 * @param $suffix String: suffix to append to the fixed page
257 */
258 private function resolveConflict( $row, $resolvable, $suffix ) {
259 if ( !$resolvable ) {
260 $this->output( "... *** old title {$row->title}\n" );
261 while ( true ) {
262 $row->title .= $suffix;
263 $this->output( "... *** new title {$row->title}\n" );
264 $title = Title::makeTitleSafe( $row->namespace, $row->title );
265 if ( !$title ) {
266 $this->output( "... !!! invalid title\n" );
267 return false;
268 }
269 $id = $title->getArticleId();
270 if ( $id ) {
271 $this->output( "... *** page exists with ID $id ***\n" );
272 } else {
273 break;
274 }
275 }
276 $this->output( "... *** using suffixed form [[" . $title->getPrefixedText() . "]] ***\n" );
277 }
278 $this->resolveConflictOn( $row, 'page', 'page' );
279 return true;
280 }
281
282 /**
283 * Resolve a given conflict
284 *
285 * @param $row Object: row from the old broken entry
286 * @param $table String: table to update
287 * @param $prefix String: prefix for column name, like page or ar
288 */
289 private function resolveConflictOn( $row, $table, $prefix ) {
290 $this->output( "... resolving on $table... " );
291 $newTitle = Title::makeTitleSafe( $row->namespace, $row->title );
292 $this->db->update( $table,
293 array(
294 "{$prefix}_namespace" => $newTitle->getNamespace(),
295 "{$prefix}_title" => $newTitle->getDBkey(),
296 ),
297 array(
298 // "{$prefix}_namespace" => 0,
299 // "{$prefix}_title" => $row->oldtitle,
300 "{$prefix}_id" => $row->id,
301 ),
302 __METHOD__ );
303 $this->output( "ok.\n" );
304 return true;
305 }
306 }
307
308 $maintClass = "NamespaceConflictChecker";
309 require_once( RUN_MAINTENANCE_IF_MAIN );