3 * Look for 'orphan' revisions hooked to pages which don't exist and
4 * 'childless' pages with no revisions.
5 * Then, kill the poor widows and orphans.
6 * Man this is depressing.
8 * Copyright © 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
27 * @author <brion@pobox.com>
28 * @ingroup Maintenance
31 require_once( __DIR__
. '/Maintenance.php' );
34 * Maintenance script that looks for 'orphan' revisions hooked to pages which
35 * don't exist and 'childless' pages with no revisions.
37 * @ingroup Maintenance
39 class Orphans
extends Maintenance
{
40 public function __construct() {
41 parent
::__construct();
42 $this->mDescription
= "Look for 'orphan' revisions hooked to pages which don't exist\n" .
43 "and 'childless' pages with no revisions\n" .
44 "Then, kill the poor widows and orphans\n" .
45 "Man this is depressing";
46 $this->addOption( 'fix', 'Actually fix broken entries' );
49 public function execute() {
51 $wgTitle = Title
::newFromText( 'Orphan revision cleanup script' );
52 $this->checkOrphans( $this->hasOption( 'fix' ) );
53 $this->checkSeparation( $this->hasOption( 'fix' ) );
54 # Does not work yet, do not use
55 # $this->checkWidows( $this->hasOption( 'fix' ) );
59 * Lock the appropriate tables for the script
60 * @param $db DatabaseBase object
61 * @param $extraTable String The name of any extra tables to lock (eg: text)
63 private function lockTables( $db, $extraTable = array() ) {
64 $tbls = array( 'page', 'revision', 'redirect' );
66 $tbls = array_merge( $tbls, $extraTable );
68 $db->lockTables( array(), $tbls, __METHOD__
, false );
72 * Check for orphan revisions
73 * @param $fix bool Whether to fix broken revisions when found
75 private function checkOrphans( $fix ) {
76 $dbw = wfGetDB( DB_MASTER
);
77 $page = $dbw->tableName( 'page' );
78 $revision = $dbw->tableName( 'revision' );
81 $this->lockTables( $dbw );
84 $this->output( "Checking for orphan revision table entries... (this may take a while on a large wiki)\n" );
85 $result = $dbw->query( "
87 FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
90 $orphans = $result->numRows();
93 $this->output( "$orphans orphan revisions...\n" );
94 $this->output( sprintf( "%10s %10s %14s %20s %s\n", 'rev_id', 'rev_page', 'rev_timestamp', 'rev_user_text', 'rev_comment' ) );
95 foreach ( $result as $row ) {
96 $comment = ( $row->rev_comment
== '' )
98 : '(' . $wgContLang->truncate( $row->rev_comment
, 40 ) . ')';
99 $this->output( sprintf( "%10d %10d %14s %20s %s\n",
103 $wgContLang->truncate( $row->rev_user_text
, 17 ),
106 $dbw->delete( 'revision', array( 'rev_id' => $row->rev_id
) );
110 $this->output( "Run again with --fix to remove these entries automatically.\n" );
113 $this->output( "No orphans! Yay!\n" );
117 $dbw->unlockTables( __METHOD__
);
123 * @todo DON'T USE THIS YET! It will remove entries which have children,
124 * but which aren't properly attached (eg if page_latest is bogus
125 * but valid revisions do exist)
127 private function checkWidows( $fix ) {
128 $dbw = wfGetDB( DB_MASTER
);
129 $page = $dbw->tableName( 'page' );
130 $revision = $dbw->tableName( 'revision' );
133 $this->lockTables( $dbw );
136 $this->output( "\nChecking for childless page table entries... (this may take a while on a large wiki)\n" );
137 $result = $dbw->query( "
139 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
142 $widows = $result->numRows();
144 $this->output( "$widows childless pages...\n" );
145 $this->output( sprintf( "%10s %11s %2s %s\n", 'page_id', 'page_latest', 'ns', 'page_title' ) );
146 foreach ( $result as $row ) {
147 printf( "%10d %11d %2d %s\n",
150 $row->page_namespace
,
153 $dbw->delete( 'page', array( 'page_id' => $row->page_id
) );
157 $this->output( "Run again with --fix to remove these entries automatically.\n" );
160 $this->output( "No childless pages! Yay!\n" );
164 $dbw->unlockTables( __METHOD__
);
169 * Check for pages where page_latest is wrong
170 * @param $fix bool Whether to fix broken entries
172 private function checkSeparation( $fix ) {
173 $dbw = wfGetDB( DB_MASTER
);
174 $page = $dbw->tableName( 'page' );
175 $revision = $dbw->tableName( 'revision' );
178 $this->lockTables( $dbw, array( 'user', 'text' ) );
181 $this->output( "\nChecking for pages whose page_latest links are incorrect... (this may take a while on a large wiki)\n" );
182 $result = $dbw->query( "
184 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
187 foreach ( $result as $row ) {
188 $result2 = $dbw->query( "
189 SELECT MAX(rev_timestamp) as max_timestamp
191 WHERE rev_page=$row->page_id
193 $row2 = $dbw->fetchObject( $result2 );
195 if ( $row->rev_timestamp
!= $row2->max_timestamp
) {
197 $this->output( sprintf( "%10s %10s %14s %14s\n",
198 'page_id', 'rev_id', 'timestamp', 'max timestamp' ) );
201 $this->output( sprintf( "%10d %10d %14s %14s\n",
205 $row2->max_timestamp
) );
208 $maxId = $dbw->selectField(
212 'rev_page' => $row->page_id
,
213 'rev_timestamp' => $row2->max_timestamp
) );
214 $this->output( "... updating to revision $maxId\n" );
215 $maxRev = Revision
::newFromId( $maxId );
216 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
217 $article = WikiPage
::factory( $title );
218 $article->updateRevisionOn( $dbw, $maxRev );
222 $this->output( "wtf\n" );
227 $this->output( "Found $found pages with incorrect latest revision.\n" );
229 $this->output( "No pages with incorrect latest revision. Yay!\n" );
231 if ( !$fix && $found > 0 ) {
232 $this->output( "Run again with --fix to remove these entries automatically.\n" );
236 $dbw->unlockTables( __METHOD__
);
241 $maintClass = "Orphans";
242 require_once( RUN_MAINTENANCE_IF_MAIN
);