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...
25 * @ingroup Maintenance
28 function fixDupes( $fixthem = false) {
29 $dbw = wfGetDB( DB_MASTER );
30 $cur = $dbw->tableName( 'cur' );
31 $old = $dbw->tableName( 'old' );
32 $dbw->query( "LOCK TABLES $cur WRITE, $old WRITE" );
33 echo "Checking for duplicate cur table entries... (this may take a while on a large wiki)\n";
34 $res = $dbw->query( <<<END
35 SELECT cur_namespace,cur_title,count(*) as c,min(cur_id) as id
37 GROUP BY cur_namespace,cur_title
41 $n = $dbw->numRows( $res );
42 echo "Found $n titles with duplicate entries.\n";
45 echo "Correcting...\n";
47 echo "Just a demo...\n";
49 while( $row = $dbw->fetchObject( $res ) ) {
50 $ns = intval( $row->cur_namespace );
51 $title = $dbw->addQuotes( $row->cur_title );
53 # Get the first responding ID; that'll be the one we keep.
54 $id = $dbw->selectField( 'cur', 'cur_id', array(
55 'cur_namespace' => $row->cur_namespace,
56 'cur_title' => $row->cur_title ) );
58 echo "$ns:$row->cur_title (canonical ID $id)\n";
59 if( $id != $row->id ) {
60 echo " ** minimum ID $row->id; ";
61 $timeMin = $dbw->selectField( 'cur', 'cur_timestamp', array(
62 'cur_id' => $row->id ) );
63 $timeFirst = $dbw->selectField( 'cur', 'cur_timestamp', array(
65 if( $timeMin == $timeFirst ) {
66 echo "timestamps match at $timeFirst; ok\n";
68 echo "timestamps don't match! min: $timeMin, first: $timeFirst; ";
69 if( $timeMin > $timeFirst ) {
71 echo "keeping minimum: $id\n";
73 echo "keeping first: $id\n";
82 (old_namespace, old_title, old_text,
83 old_comment, old_user, old_user_text,
84 old_timestamp, old_minor_edit, old_flags,
86 SELECT cur_namespace, cur_title, cur_text,
87 cur_comment, cur_user, cur_user_text,
88 cur_timestamp, cur_minor_edit, '',
91 WHERE cur_namespace=$ns
99 WHERE cur_namespace=$ns
107 $dbw->query( 'UNLOCK TABLES' );
111 echo "Run again with --fix option to delete the duplicates.\n";
115 function checkDupes( $fixthem = false, $indexonly = false ) {
116 $dbw = wfGetDB( DB_MASTER );
117 if( $dbw->indexExists( 'cur', 'name_title' ) &&
118 $dbw->indexUnique( 'cur', 'name_title' ) ) {
119 echo wfWikiID().": cur table has the current unique index; no duplicate entries.\n";
120 } elseif( $dbw->indexExists( 'cur', 'name_title_dup_prevention' ) ) {
121 echo wfWikiID().": cur table has a temporary name_title_dup_prevention unique index; no duplicate entries.\n";
123 echo wfWikiID().": cur table has the old non-unique index and may have duplicate entries.\n";
125 fixDupes( $fixthem );