ajaxwatch.js: coding style tweaks
[lhc/web/wiklou.git] / maintenance / cleanupDupes.inc
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 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 * @file
25 * @ingroup Maintenance
26 */
27
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
36 FROM $cur
37 GROUP BY cur_namespace,cur_title
38 HAVING c > 1
39 END
40 );
41 $n = $dbw->numRows( $res );
42 echo "Found $n titles with duplicate entries.\n";
43 if ( $n > 0 ) {
44 if ( $fixthem ) {
45 echo "Correcting...\n";
46 } else {
47 echo "Just a demo...\n";
48 }
49 while ( $row = $dbw->fetchObject( $res ) ) {
50 $ns = intval( $row->cur_namespace );
51 $title = $dbw->addQuotes( $row->cur_title );
52
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 ) );
57
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(
64 'cur_id' => $id ) );
65 if ( $timeMin == $timeFirst ) {
66 echo "timestamps match at $timeFirst; ok\n";
67 } else {
68 echo "timestamps don't match! min: $timeMin, first: $timeFirst; ";
69 if ( $timeMin > $timeFirst ) {
70 $id = $row->id;
71 echo "keeping minimum: $id\n";
72 } else {
73 echo "keeping first: $id\n";
74 }
75 }
76 }
77
78 if ( $fixthem ) {
79 $dbw->query( <<<END
80 INSERT
81 INTO $old
82 (old_namespace, old_title, old_text,
83 old_comment, old_user, old_user_text,
84 old_timestamp, old_minor_edit, old_flags,
85 inverse_timestamp)
86 SELECT cur_namespace, cur_title, cur_text,
87 cur_comment, cur_user, cur_user_text,
88 cur_timestamp, cur_minor_edit, '',
89 inverse_timestamp
90 FROM $cur
91 WHERE cur_namespace=$ns
92 AND cur_title=$title
93 AND cur_id != $id
94 END
95 );
96 $dbw->query( <<<END
97 DELETE
98 FROM $cur
99 WHERE cur_namespace=$ns
100 AND cur_title=$title
101 AND cur_id != $id
102 END
103 );
104 }
105 }
106 }
107 $dbw->query( 'UNLOCK TABLES' );
108 if ( $fixthem ) {
109 echo "Done.\n";
110 } else {
111 echo "Run again with --fix option to delete the duplicates.\n";
112 }
113 }
114
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";
122 } else {
123 echo wfWikiID() . ": cur table has the old non-unique index and may have duplicate entries.\n";
124 if ( !$indexonly ) {
125 fixDupes( $fixthem );
126 }
127 }
128 }