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.
26 * @author <brion@pobox.com>
27 * @addtogroup Maintenance
30 $options = array( 'fix' );
33 require_once( 'commandLine.inc' );
34 $wgTitle = Title
::newFromText( 'Orphan revision cleanup script' );
36 checkOrphans( isset( $options['fix'] ) );
37 checkSeparation( isset( $options['fix'] ) );
38 #checkWidows( isset( $options['fix'] ) );
42 function checkOrphans( $fix ) {
43 $dbw = wfGetDB( DB_MASTER
);
44 $page = $dbw->tableName( 'page' );
45 $revision = $dbw->tableName( 'revision' );
48 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE" );
51 echo "Checking for orphan revision table entries... (this may take a while on a large wiki)\n";
52 $result = $dbw->query( "
54 FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
57 $orphans = $dbw->numRows( $result );
60 echo "$orphans orphan revisions...\n";
61 printf( "%10s %10s %14s %20s %s\n", 'rev_id', 'rev_page', 'rev_timestamp', 'rev_user_text', 'rev_comment' );
62 while( $row = $dbw->fetchObject( $result ) ) {
63 $comment = ( $row->rev_comment
== '' )
65 : '(' . $wgContLang->truncate( $row->rev_comment
, 40, '...' ) . ')';
66 printf( "%10d %10d %14s %20s %s\n",
70 $wgContLang->truncate( $row->rev_user_text
, 17, '...' ),
73 $dbw->delete( 'revision', array( 'rev_id' => $row->rev_id
) );
77 echo "Run again with --fix to remove these entries automatically.\n";
80 echo "No orphans! Yay!\n";
84 $dbw->query( "UNLOCK TABLES" );
89 * @todo DON'T USE THIS YET! It will remove entries which have children,
90 * but which aren't properly attached (eg if page_latest is bogus
91 * but valid revisions do exist)
93 function checkWidows( $fix ) {
94 $dbw = wfGetDB( DB_MASTER
);
95 $page = $dbw->tableName( 'page' );
96 $revision = $dbw->tableName( 'revision' );
99 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE" );
102 echo "\nChecking for childless page table entries... (this may take a while on a large wiki)\n";
103 $result = $dbw->query( "
105 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
108 $widows = $dbw->numRows( $result );
111 echo "$widows childless pages...\n";
112 printf( "%10s %11s %2s %s\n", 'page_id', 'page_latest', 'ns', 'page_title' );
113 while( $row = $dbw->fetchObject( $result ) ) {
114 printf( "%10d %11d %2d %s\n",
117 $row->page_namespace
,
120 $dbw->delete( 'page', array( 'page_id' => $row->page_id
) );
124 echo "Run again with --fix to remove these entries automatically.\n";
127 echo "No childless pages! Yay!\n";
131 $dbw->query( "UNLOCK TABLES" );
136 function checkSeparation( $fix ) {
137 $dbw = wfGetDB( DB_MASTER
);
138 $page = $dbw->tableName( 'page' );
139 $revision = $dbw->tableName( 'revision' );
140 $text = $dbw->tableName( 'text' );
143 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE, $text WRITE" );
146 echo "\nChecking for pages whose page_latest links are incorrect... (this may take a while on a large wiki)\n";
147 $result = $dbw->query( "
149 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
152 while( $row = $dbw->fetchObject( $result ) ) {
153 $result2 = $dbw->query( "
154 SELECT MAX(rev_timestamp) as max_timestamp
156 WHERE rev_page=$row->page_id
158 $row2 = $dbw->fetchObject( $result2 );
159 $dbw->freeResult( $result2 );
161 if( $row->rev_timestamp
!= $row2->max_timestamp
) {
163 printf( "%10s %10s %14s %14s\n",
164 'page_id', 'rev_id', 'timestamp', 'max timestamp' );
167 printf( "%10d %10d %14s %14s\n",
171 $row2->max_timestamp
);
174 $maxId = $dbw->selectField(
178 'rev_page' => $row->page_id
,
179 'rev_timestamp' => $row2->max_timestamp
) );
180 echo "... updating to revision $maxId\n";
181 $maxRev = Revision
::newFromId( $maxId );
182 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
183 $article = new Article( $title );
184 $article->updateRevisionOn( $dbw, $maxRev );
193 echo "Found $found pages with incorrect latest revision.\n";
195 echo "No pages with incorrect latest revision. Yay!\n";
197 if( !$fix && $found > 0 ) {
198 echo "Run again with --fix to remove these entries automatically.\n";
202 $dbw->query( "UNLOCK TABLES" );