2 # Copyright (C) 2005 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 * Look for 'orphan' revisions hooked to pages which don't exist
22 * And 'childless' pages with no revisions.
23 * Then, kill the poor widows and orphans.
24 * Man this is depressing.
27 * @author <brion@pobox.com>
28 * @ingroup Maintenance
31 $options = array( 'fix' );
34 require_once( 'commandLine.inc' );
35 $wgTitle = Title
::newFromText( 'Orphan revision cleanup script' );
37 checkOrphans( isset( $options['fix'] ) );
38 checkSeparation( isset( $options['fix'] ) );
39 #checkWidows( isset( $options['fix'] ) );
43 function checkOrphans( $fix ) {
44 $dbw = wfGetDB( DB_MASTER
);
45 $page = $dbw->tableName( 'page' );
46 $revision = $dbw->tableName( 'revision' );
49 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE" );
52 echo "Checking for orphan revision table entries... (this may take a while on a large wiki)\n";
53 $result = $dbw->query( "
55 FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
58 $orphans = $dbw->numRows( $result );
61 echo "$orphans orphan revisions...\n";
62 printf( "%10s %10s %14s %20s %s\n", 'rev_id', 'rev_page', 'rev_timestamp', 'rev_user_text', 'rev_comment' );
63 while( $row = $dbw->fetchObject( $result ) ) {
64 $comment = ( $row->rev_comment
== '' )
66 : '(' . $wgContLang->truncate( $row->rev_comment
, 40, '...' ) . ')';
67 printf( "%10d %10d %14s %20s %s\n",
71 $wgContLang->truncate( $row->rev_user_text
, 17, '...' ),
74 $dbw->delete( 'revision', array( 'rev_id' => $row->rev_id
) );
78 echo "Run again with --fix to remove these entries automatically.\n";
81 echo "No orphans! Yay!\n";
85 $dbw->query( "UNLOCK TABLES" );
90 * @todo DON'T USE THIS YET! It will remove entries which have children,
91 * but which aren't properly attached (eg if page_latest is bogus
92 * but valid revisions do exist)
94 function checkWidows( $fix ) {
95 $dbw = wfGetDB( DB_MASTER
);
96 $page = $dbw->tableName( 'page' );
97 $revision = $dbw->tableName( 'revision' );
100 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE" );
103 echo "\nChecking for childless page table entries... (this may take a while on a large wiki)\n";
104 $result = $dbw->query( "
106 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
109 $widows = $dbw->numRows( $result );
112 echo "$widows childless pages...\n";
113 printf( "%10s %11s %2s %s\n", 'page_id', 'page_latest', 'ns', 'page_title' );
114 while( $row = $dbw->fetchObject( $result ) ) {
115 printf( "%10d %11d %2d %s\n",
118 $row->page_namespace
,
121 $dbw->delete( 'page', array( 'page_id' => $row->page_id
) );
125 echo "Run again with --fix to remove these entries automatically.\n";
128 echo "No childless pages! Yay!\n";
132 $dbw->query( "UNLOCK TABLES" );
137 function checkSeparation( $fix ) {
138 $dbw = wfGetDB( DB_MASTER
);
139 $page = $dbw->tableName( 'page' );
140 $revision = $dbw->tableName( 'revision' );
141 $text = $dbw->tableName( 'text' );
144 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE, $text WRITE" );
147 echo "\nChecking for pages whose page_latest links are incorrect... (this may take a while on a large wiki)\n";
148 $result = $dbw->query( "
150 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
153 while( $row = $dbw->fetchObject( $result ) ) {
154 $result2 = $dbw->query( "
155 SELECT MAX(rev_timestamp) as max_timestamp
157 WHERE rev_page=$row->page_id
159 $row2 = $dbw->fetchObject( $result2 );
160 $dbw->freeResult( $result2 );
162 if( $row->rev_timestamp
!= $row2->max_timestamp
) {
164 printf( "%10s %10s %14s %14s\n",
165 'page_id', 'rev_id', 'timestamp', 'max timestamp' );
168 printf( "%10d %10d %14s %14s\n",
172 $row2->max_timestamp
);
175 $maxId = $dbw->selectField(
179 'rev_page' => $row->page_id
,
180 'rev_timestamp' => $row2->max_timestamp
) );
181 echo "... updating to revision $maxId\n";
182 $maxRev = Revision
::newFromId( $maxId );
183 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
184 $article = new Article( $title );
185 $article->updateRevisionOn( $dbw, $maxRev );
194 echo "Found $found pages with incorrect latest revision.\n";
196 echo "No pages with incorrect latest revision. Yay!\n";
198 if( !$fix && $found > 0 ) {
199 echo "Run again with --fix to remove these entries automatically.\n";
203 $dbw->query( "UNLOCK TABLES" );