00001 <?php 00002 # Copyright (C) 2004 Brion Vibber <brion@pobox.com> 00003 # http://www.mediawiki.org/ 00004 # 00005 # This program is free software; you can redistribute it and/or modify 00006 # it under the terms of the GNU General Public License as published by 00007 # the Free Software Foundation; either version 2 of the License, or 00008 # (at your option) any later version. 00009 # 00010 # This program is distributed in the hope that it will be useful, 00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 # GNU General Public License for more details. 00014 # 00015 # You should have received a copy of the GNU General Public License along 00016 # with this program; if not, write to the Free Software Foundation, Inc., 00017 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 # http://www.gnu.org/copyleft/gpl.html 00019 00028 function fixDupes( $fixthem = false) { 00029 $dbw = wfGetDB( DB_MASTER ); 00030 $cur = $dbw->tableName( 'cur' ); 00031 $old = $dbw->tableName( 'old' ); 00032 $dbw->query( "LOCK TABLES $cur WRITE, $old WRITE" ); 00033 echo "Checking for duplicate cur table entries... (this may take a while on a large wiki)\n"; 00034 $res = $dbw->query( <<<END 00035 SELECT cur_namespace,cur_title,count(*) as c,min(cur_id) as id 00036 FROM $cur 00037 GROUP BY cur_namespace,cur_title 00038 HAVING c > 1 00039 END 00040 ); 00041 $n = $dbw->numRows( $res ); 00042 echo "Found $n titles with duplicate entries.\n"; 00043 if( $n > 0 ) { 00044 if( $fixthem ) { 00045 echo "Correcting...\n"; 00046 } else { 00047 echo "Just a demo...\n"; 00048 } 00049 while( $row = $dbw->fetchObject( $res ) ) { 00050 $ns = intval( $row->cur_namespace ); 00051 $title = $dbw->addQuotes( $row->cur_title ); 00052 00053 # Get the first responding ID; that'll be the one we keep. 00054 $id = $dbw->selectField( 'cur', 'cur_id', array( 00055 'cur_namespace' => $row->cur_namespace, 00056 'cur_title' => $row->cur_title ) ); 00057 00058 echo "$ns:$row->cur_title (canonical ID $id)\n"; 00059 if( $id != $row->id ) { 00060 echo " ** minimum ID $row->id; "; 00061 $timeMin = $dbw->selectField( 'cur', 'cur_timestamp', array( 00062 'cur_id' => $row->id ) ); 00063 $timeFirst = $dbw->selectField( 'cur', 'cur_timestamp', array( 00064 'cur_id' => $id ) ); 00065 if( $timeMin == $timeFirst ) { 00066 echo "timestamps match at $timeFirst; ok\n"; 00067 } else { 00068 echo "timestamps don't match! min: $timeMin, first: $timeFirst; "; 00069 if( $timeMin > $timeFirst ) { 00070 $id = $row->id; 00071 echo "keeping minimum: $id\n"; 00072 } else { 00073 echo "keeping first: $id\n"; 00074 } 00075 } 00076 } 00077 00078 if( $fixthem ) { 00079 $dbw->query( <<<END 00080 INSERT 00081 INTO $old 00082 (old_namespace, old_title, old_text, 00083 old_comment, old_user, old_user_text, 00084 old_timestamp, old_minor_edit, old_flags, 00085 inverse_timestamp) 00086 SELECT cur_namespace, cur_title, cur_text, 00087 cur_comment, cur_user, cur_user_text, 00088 cur_timestamp, cur_minor_edit, '', 00089 inverse_timestamp 00090 FROM $cur 00091 WHERE cur_namespace=$ns 00092 AND cur_title=$title 00093 AND cur_id != $id 00094 END 00095 ); 00096 $dbw->query( <<<END 00097 DELETE 00098 FROM $cur 00099 WHERE cur_namespace=$ns 00100 AND cur_title=$title 00101 AND cur_id != $id 00102 END 00103 ); 00104 } 00105 } 00106 } 00107 $dbw->query( 'UNLOCK TABLES' ); 00108 if( $fixthem ) { 00109 echo "Done.\n"; 00110 } else { 00111 echo "Run again with --fix option to delete the duplicates.\n"; 00112 } 00113 } 00114 00115 function checkDupes( $fixthem = false, $indexonly = false ) { 00116 $dbw = wfGetDB( DB_MASTER ); 00117 if( $dbw->indexExists( 'cur', 'name_title' ) && 00118 $dbw->indexUnique( 'cur', 'name_title' ) ) { 00119 echo wfWikiID().": cur table has the current unique index; no duplicate entries.\n"; 00120 } elseif( $dbw->indexExists( 'cur', 'name_title_dup_prevention' ) ) { 00121 echo wfWikiID().": cur table has a temporary name_title_dup_prevention unique index; no duplicate entries.\n"; 00122 } else { 00123 echo wfWikiID().": cur table has the old non-unique index and may have duplicate entries.\n"; 00124 if( !$indexonly ) { 00125 fixDupes( $fixthem ); 00126 } 00127 } 00128 }