Nobody wants to touch this....
[mediawiki.git] / maintenance / cleanupDupes.inc
blobce1ffc6ea0016ecbbcba10ab800f97a555e67d63
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
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.
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.
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
20 /**
21  * If on the old non-unique indexes, check the cur table for duplicate
22  * entries and remove them...
23  *
24  * @addtogroup Maintenance
25  */
27 function fixDupes( $fixthem = false) {
28         $dbw = wfGetDB( DB_MASTER );
29         $cur = $dbw->tableName( 'cur' );
30         $old = $dbw->tableName( 'old' );
31         $dbw->query( "LOCK TABLES $cur WRITE, $old WRITE" );
32         echo "Checking for duplicate cur table entries... (this may take a while on a large wiki)\n";
33         $res = $dbw->query( <<<END
34 SELECT cur_namespace,cur_title,count(*) as c,min(cur_id) as id
35   FROM $cur
36  GROUP BY cur_namespace,cur_title
37 HAVING c > 1
38 END
39         );
40         $n = $dbw->numRows( $res );
41         echo "Found $n titles with duplicate entries.\n";
42         if( $n > 0 ) {
43                 if( $fixthem ) {
44                         echo "Correcting...\n";
45                 } else {
46                         echo "Just a demo...\n";
47                 }
48                 while( $row = $dbw->fetchObject( $res ) ) {
49                         $ns = intval( $row->cur_namespace );
50                         $title = $dbw->addQuotes( $row->cur_title );
52                         # Get the first responding ID; that'll be the one we keep.
53                         $id = $dbw->selectField( 'cur', 'cur_id', array(
54                                 'cur_namespace' => $row->cur_namespace,
55                                 'cur_title'     => $row->cur_title ) );
57                         echo "$ns:$row->cur_title (canonical ID $id)\n";
58                         if( $id != $row->id ) {
59                                 echo "  ** minimum ID $row->id; ";
60                                 $timeMin = $dbw->selectField( 'cur', 'cur_timestamp', array(
61                                         'cur_id' => $row->id ) );
62                                 $timeFirst = $dbw->selectField( 'cur', 'cur_timestamp', array(
63                                         'cur_id' => $id ) );
64                                 if( $timeMin == $timeFirst ) {
65                                         echo "timestamps match at $timeFirst; ok\n";
66                                 } else {
67                                         echo "timestamps don't match! min: $timeMin, first: $timeFirst; ";
68                                         if( $timeMin > $timeFirst ) {
69                                                 $id = $row->id;
70                                                 echo "keeping minimum: $id\n";
71                                         } else {
72                                                 echo "keeping first: $id\n";
73                                         }
74                                 }
75                         }
77                         if( $fixthem ) {
78                                 $dbw->query( <<<END
79 INSERT
80   INTO $old
81       (old_namespace, old_title,      old_text,
82        old_comment,   old_user,       old_user_text,
83        old_timestamp, old_minor_edit, old_flags,
84        inverse_timestamp)
85 SELECT cur_namespace, cur_title,      cur_text,
86        cur_comment,   cur_user,       cur_user_text,
87        cur_timestamp, cur_minor_edit, '',
88        inverse_timestamp
89   FROM $cur
90  WHERE cur_namespace=$ns
91    AND cur_title=$title
92    AND cur_id != $id
93 END
94                                 );
95                                 $dbw->query( <<<END
96 DELETE
97   FROM $cur
98  WHERE cur_namespace=$ns
99    AND cur_title=$title
100    AND cur_id != $id
102                                 );
103                         }
104                 }
105         }
106         $dbw->query( 'UNLOCK TABLES' );
107         if( $fixthem ) {
108                 echo "Done.\n";
109         } else {
110                 echo "Run again with --fix option to delete the duplicates.\n";
111         }
114 function checkDupes( $fixthem = false, $indexonly = false ) {
115         $dbw = wfGetDB( DB_MASTER );
116         if( $dbw->indexExists( 'cur', 'name_title' ) &&
117             $dbw->indexUnique( 'cur', 'name_title' ) ) {
118                 echo wfWikiID().": cur table has the current unique index; no duplicate entries.\n";
119         } elseif( $dbw->indexExists( 'cur', 'name_title_dup_prevention' ) ) {
120                 echo wfWikiID().": cur table has a temporary name_title_dup_prevention unique index; no duplicate entries.\n";
121         } else {
122                 echo wfWikiID().": cur table has the old non-unique index and may have duplicate entries.\n";
123                 if( !$indexonly ) {
124                         fixDupes( $fixthem );
125                 }
126         }