* (bug 2173) Fatal error when removing an article with an empty title from
[lhc/web/wiklou.git] / maintenance / cleanupDupes.php
1 <?php
2 # Copyright (C) 2004 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 /**
21 * If on the old non-unique indexes, check the cur table for duplicate
22 * entries and remove them...
23 *
24 * @author <brion@pobox.com>
25 * @package MediaWiki
26 * @subpackage Maintenance
27 */
28
29 $options = array( 'fix' );
30
31 /** */
32 require_once( 'commandLine.inc' );
33 $wgTitle = Title::newFromText( 'Dupe cur entry cleanup script' );
34
35 checkDupes( isset( $options['fix'] ) );
36
37 function fixDupes( $fixthem = false) {
38 $dbw =& wfGetDB( DB_MASTER );
39 $cur = $dbw->tableName( 'cur' );
40 $dbw->query( "LOCK TABLES $cur WRITE" );
41 echo "Checking for duplicate cur table entries... (this may take a while on a large wiki)\n";
42 $res = $dbw->query( <<<END
43 SELECT cur_namespace,cur_title,count(*) as c,min(cur_id) as id
44 FROM $cur
45 GROUP BY cur_namespace,cur_title
46 HAVING c > 1
47 END
48 );
49 $n = $dbw->numRows( $res );
50 echo "Found $n titles with duplicate entries.\n";
51 if( $n > 0 ) {
52 if( $fixthem ) {
53 echo "Correcting...\n";
54 } else {
55 echo "Just a demo...\n";
56 }
57 while( $row = $dbw->fetchObject( $res ) ) {
58 $ns = IntVal( $row->cur_namespace );
59 $title = $dbw->addQuotes( $row->cur_title );
60 $id = IntVal( $row->id );
61 echo "$ns:$row->cur_title (canonical ID $id)\n";
62 if( $fixthem ) {
63 $dbw->query( <<<END
64 DELETE
65 FROM $cur
66 WHERE cur_namespace=$ns
67 AND cur_title=$title
68 AND cur_id>$id
69 END
70 );
71 }
72 }
73 }
74 $dbw->query( 'UNLOCK TABLES' );
75 if( $fixthem ) {
76 echo "Done.\n";
77 } else {
78 echo "Run again with --fix option to delete the duplicates.\n";
79 }
80 }
81
82 function checkDupes( $fixthem = false ) {
83 $dbw =& wfGetDB( DB_MASTER );
84 if( $dbw->indexExists( 'cur', 'name_title' ) &&
85 $dbw->indexUnique( 'cur', 'name_title' ) ) {
86 echo "Your cur table has the current unique index; no duplicate entries.\n";
87 } else {
88 echo "Your cur table has the old non-unique index and may have duplicate entries.\n";
89 fixDupes( $fixthem );
90 }
91 }
92 ?>