Merge "Minor CSS cleanup for Vector and Monobook skins"
[mediawiki.git] / maintenance / orphans.php
blobbae0b88b87fd27df6a9ebcfcf22405782d6a0afa
1 <?php
2 /**
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
26 * @file
27 * @author <brion@pobox.com>
28 * @ingroup Maintenance
31 require_once( __DIR__ . '/Maintenance.php' );
33 /**
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() {
50 global $wgTitle;
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' ) );
58 /**
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' );
65 if ( $extraTable ) {
66 $tbls = array_merge( $tbls, $extraTable );
68 $db->lockTables( array(), $tbls, __METHOD__, false );
71 /**
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' );
80 if ( $fix ) {
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( "
86 SELECT *
87 FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
88 WHERE page_id IS NULL
89 " );
90 $orphans = $result->numRows();
91 if ( $orphans > 0 ) {
92 global $wgContLang;
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 == '' )
97 ? ''
98 : '(' . $wgContLang->truncate( $row->rev_comment, 40 ) . ')';
99 $this->output( sprintf( "%10d %10d %14s %20s %s\n",
100 $row->rev_id,
101 $row->rev_page,
102 $row->rev_timestamp,
103 $wgContLang->truncate( $row->rev_user_text, 17 ),
104 $comment ) );
105 if ( $fix ) {
106 $dbw->delete( 'revision', array( 'rev_id' => $row->rev_id ) );
109 if ( !$fix ) {
110 $this->output( "Run again with --fix to remove these entries automatically.\n" );
112 } else {
113 $this->output( "No orphans! Yay!\n" );
116 if ( $fix ) {
117 $dbw->unlockTables( __METHOD__ );
122 * @param $fix bool
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' );
132 if ( $fix ) {
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( "
138 SELECT *
139 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
140 WHERE rev_id IS NULL
141 " );
142 $widows = $result->numRows();
143 if ( $widows > 0 ) {
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",
148 $row->page_id,
149 $row->page_latest,
150 $row->page_namespace,
151 $row->page_title );
152 if ( $fix ) {
153 $dbw->delete( 'page', array( 'page_id' => $row->page_id ) );
156 if ( !$fix ) {
157 $this->output( "Run again with --fix to remove these entries automatically.\n" );
159 } else {
160 $this->output( "No childless pages! Yay!\n" );
163 if ( $fix ) {
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' );
177 if ( $fix ) {
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( "
183 SELECT *
184 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
185 " );
186 $found = 0;
187 foreach ( $result as $row ) {
188 $result2 = $dbw->query( "
189 SELECT MAX(rev_timestamp) as max_timestamp
190 FROM $revision
191 WHERE rev_page=$row->page_id
192 " );
193 $row2 = $dbw->fetchObject( $result2 );
194 if ( $row2 ) {
195 if ( $row->rev_timestamp != $row2->max_timestamp ) {
196 if ( $found == 0 ) {
197 $this->output( sprintf( "%10s %10s %14s %14s\n",
198 'page_id', 'rev_id', 'timestamp', 'max timestamp' ) );
200 ++$found;
201 $this->output( sprintf( "%10d %10d %14s %14s\n",
202 $row->page_id,
203 $row->page_latest,
204 $row->rev_timestamp,
205 $row2->max_timestamp ) );
206 if ( $fix ) {
207 # ...
208 $maxId = $dbw->selectField(
209 'revision',
210 'rev_id',
211 array(
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 );
221 } else {
222 $this->output( "wtf\n" );
226 if ( $found ) {
227 $this->output( "Found $found pages with incorrect latest revision.\n" );
228 } else {
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" );
235 if ( $fix ) {
236 $dbw->unlockTables( __METHOD__ );
241 $maintClass = "Orphans";
242 require_once( RUN_MAINTENANCE_IF_MAIN );