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
21 * If on the old non-unique indexes, check the cur table for duplicate
22 * entries and remove them...
24 * @addtogroup Maintenance
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
36 GROUP BY cur_namespace,cur_title
40 $n = $dbw->numRows( $res );
41 echo "Found $n titles with duplicate entries.\n";
44 echo "Correcting...\n";
46 echo "Just a demo...\n";
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(
64 if( $timeMin == $timeFirst ) {
65 echo "timestamps match at $timeFirst; ok\n";
67 echo "timestamps don't match! min: $timeMin, first: $timeFirst; ";
68 if( $timeMin > $timeFirst ) {
70 echo "keeping minimum: $id\n";
72 echo "keeping first: $id\n";
81 (old_namespace, old_title, old_text,
82 old_comment, old_user, old_user_text,
83 old_timestamp, old_minor_edit, old_flags,
85 SELECT cur_namespace, cur_title, cur_text,
86 cur_comment, cur_user, cur_user_text,
87 cur_timestamp, cur_minor_edit, '',
90 WHERE cur_namespace=$ns
98 WHERE cur_namespace=$ns
106 $dbw->query( 'UNLOCK TABLES' );
110 echo "Run again with --fix option to delete the duplicates.\n";
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";
122 echo wfWikiID().": cur table has the old non-unique index and may have duplicate entries.\n";
124 fixDupes( $fixthem );