Add more test cases for bug 289
[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 require_once( "commandLine.inc" );
29 require_once( "rebuildtextindex.inc" );
30 $wgTitle = Title::newFromText( "Rebuild text index script" );
31
32 checkDupes();
33
34 function fixDupes() {
35 $dbw =& wfGetDB( DB_MASTER );
36 $cur = $dbw->tableName( 'cur' );
37 $dbw->query( "LOCK TABLES $cur WRITE" );
38 echo "Checking for duplicate cur table entries... (this may take a while on a large wiki)\n";
39 $res = $dbw->query( <<<END
40 SELECT cur_namespace,cur_title,count(*) as c,max(cur_id) as id
41 FROM $cur
42 GROUP BY cur_namespace,cur_title
43 HAVING c > 1
44 END
45 );
46 $n = $dbw->numRows( $res );
47 echo "Found $n titles with duplicate entries.\n";
48 if( $n > 0 ) {
49 echo "Correcting...\n";
50 while( $row = $dbw->fetchObject( $res ) ) {
51 $ns = IntVal( $row->cur_namespace );
52 $title = $dbw->addQuotes( $row->cur_title );
53 $id = IntVal( $row->id );
54 $dbw->query( <<<END
55 DELETE
56 FROM $cur
57 WHERE cur_namespace=$ns
58 AND cur_title=$title
59 AND cur_id<$id
60 END
61 );
62 }
63 }
64 $dbw->query( 'UNLOCK TABLES' );
65 echo "Done.\n";
66 }
67
68 function checkDupes() {
69 $dbw =& wfGetDB( DB_MASTER );
70 if( $dbw->indexExists( 'cur', 'name_title' ) &&
71 $dbw->indexUnique( 'cur', 'name_title' ) ) {
72 echo "Your cur table has the current unique index; no duplicate entries.\n";
73 } else {
74 echo "Your cur table has the old non-unique index and may have duplicate entries.\n";
75 fixDupes();
76 }
77 }
78
79 ?>