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