3 * Look for 'orphan' revisions hooked to pages which don't exist
4 * And 'childless' pages with no revisions.
5 * Then, kill the poor widows and orphans.
6 * Man this is depressing.
8 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
9 * http://www.mediawiki.org/
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
26 * @author <brion@pobox.com>
27 * @ingroup Maintenance
30 require_once( dirname( __FILE__
) . '/Maintenance.php' );
32 class Orphans
extends Maintenance
{
33 public function __construct() {
34 parent
::__construct();
35 $this->mDescription
= "Look for 'orphan' revisions hooked to pages which don't exist\n" .
36 "And 'childless' pages with no revisions\n" .
37 "Then, kill the poor widows and orphans\n" .
38 "Man this is depressing";
39 $this->addOption( 'fix', 'Actually fix broken entries' );
42 public function execute() {
44 $wgTitle = Title
::newFromText( 'Orphan revision cleanup script' );
45 $this->checkOrphans( $this->hasOption( 'fix' ) );
46 $this->checkSeparation( $this->hasOption( 'fix' ) );
47 # Does not work yet, do not use
48 # $this->checkWidows( $this->hasOption( 'fix' ) );
52 * Lock the appropriate tables for the script
53 * @param $db DatabaseBase object
54 * @param $extraTable String The name of any extra tables to lock (eg: text)
56 private function lockTables( $db, $extraTable = array() ) {
57 $tbls = array( 'page', 'revision', 'redirect' );
59 $tbls = array_merge( $tbls, $extraTable );
61 $db->lockTables( array(), $tbls, __METHOD__
, false );
65 * Check for orphan revisions
66 * @param $fix bool Whether to fix broken revisions when found
68 private function checkOrphans( $fix ) {
69 $dbw = wfGetDB( DB_MASTER
);
70 $page = $dbw->tableName( 'page' );
71 $revision = $dbw->tableName( 'revision' );
74 $this->lockTables( $dbw );
77 $this->output( "Checking for orphan revision table entries... (this may take a while on a large wiki)\n" );
78 $result = $dbw->query( "
80 FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
83 $orphans = $dbw->numRows( $result );
86 $this->output( "$orphans orphan revisions...\n" );
87 $this->output( sprintf( "%10s %10s %14s %20s %s\n", 'rev_id', 'rev_page', 'rev_timestamp', 'rev_user_text', 'rev_comment' ) );
88 foreach ( $result as $row ) {
89 $comment = ( $row->rev_comment
== '' )
91 : '(' . $wgContLang->truncate( $row->rev_comment
, 40 ) . ')';
92 $this->output( sprintf( "%10d %10d %14s %20s %s\n",
96 $wgContLang->truncate( $row->rev_user_text
, 17 ),
99 $dbw->delete( 'revision', array( 'rev_id' => $row->rev_id
) );
103 $this->output( "Run again with --fix to remove these entries automatically.\n" );
106 $this->output( "No orphans! Yay!\n" );
110 $dbw->unlockTables( __METHOD__
);
116 * @todo DON'T USE THIS YET! It will remove entries which have children,
117 * but which aren't properly attached (eg if page_latest is bogus
118 * but valid revisions do exist)
120 private function checkWidows( $fix ) {
121 $dbw = wfGetDB( DB_MASTER
);
122 $page = $dbw->tableName( 'page' );
123 $revision = $dbw->tableName( 'revision' );
126 $this->lockTables( $dbw );
129 $this->output( "\nChecking for childless page table entries... (this may take a while on a large wiki)\n" );
130 $result = $dbw->query( "
132 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
135 $widows = $dbw->numRows( $result );
137 $this->output( "$widows childless pages...\n" );
138 $this->output( sprintf( "%10s %11s %2s %s\n", 'page_id', 'page_latest', 'ns', 'page_title' ) );
139 foreach ( $result as $row ) {
140 printf( "%10d %11d %2d %s\n",
143 $row->page_namespace
,
146 $dbw->delete( 'page', array( 'page_id' => $row->page_id
) );
150 $this->output( "Run again with --fix to remove these entries automatically.\n" );
153 $this->output( "No childless pages! Yay!\n" );
157 $dbw->unlockTables( __METHOD__
);
162 * Check for pages where page_latest is wrong
163 * @param $fix bool Whether to fix broken entries
165 private function checkSeparation( $fix ) {
166 $dbw = wfGetDB( DB_MASTER
);
167 $page = $dbw->tableName( 'page' );
168 $revision = $dbw->tableName( 'revision' );
171 $this->lockTables( $dbw, array( 'user', 'text' ) );
174 $this->output( "\nChecking for pages whose page_latest links are incorrect... (this may take a while on a large wiki)\n" );
175 $result = $dbw->query( "
177 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
180 foreach ( $result as $row ) {
181 $result2 = $dbw->query( "
182 SELECT MAX(rev_timestamp) as max_timestamp
184 WHERE rev_page=$row->page_id
186 $row2 = $dbw->fetchObject( $result2 );
188 if ( $row->rev_timestamp
!= $row2->max_timestamp
) {
190 $this->output( sprintf( "%10s %10s %14s %14s\n",
191 'page_id', 'rev_id', 'timestamp', 'max timestamp' ) );
194 $this->output( sprintf( "%10d %10d %14s %14s\n",
198 $row2->max_timestamp
) );
201 $maxId = $dbw->selectField(
205 'rev_page' => $row->page_id
,
206 'rev_timestamp' => $row2->max_timestamp
) );
207 $this->output( "... updating to revision $maxId\n" );
208 $maxRev = Revision
::newFromId( $maxId );
209 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
210 $article = WikiPage
::factory( $title );
211 $article->updateRevisionOn( $dbw, $maxRev );
215 $this->output( "wtf\n" );
220 $this->output( "Found $found pages with incorrect latest revision.\n" );
222 $this->output( "No pages with incorrect latest revision. Yay!\n" );
224 if ( !$fix && $found > 0 ) {
225 $this->output( "Run again with --fix to remove these entries automatically.\n" );
229 $dbw->unlockTables( __METHOD__
);
234 $maintClass = "Orphans";
235 require_once( RUN_MAINTENANCE_IF_MAIN
);