Move up to date the parser test expectation.
[lhc/web/wiklou.git] / maintenance / orphans.php
1 <?php
2 /**
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.
7 *
8 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
9 * http://www.mediawiki.org/
10 *
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.
15 *
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.
20 *
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
25 *
26 * @author <brion@pobox.com>
27 * @ingroup Maintenance
28 */
29
30 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
31
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' );
40 }
41
42 public function execute() {
43 global $wgTitle;
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' ) );
49 }
50
51 /**
52 * Lock the appropriate tables for the script
53 * @param $db Database object
54 * @param $extraTable String The name of any extra tables to lock (eg: text)
55 */
56 private function lockTables( &$db, $extraTable = null ) {
57 $tbls = array( 'page', 'revision', 'redirect' );
58 if ( $extraTable )
59 $tbls[] = $extraTable;
60 $db->lockTables( array(), $tbls, __METHOD__, false );
61 }
62
63 /**
64 * Check for orphan revisions
65 * @param $fix bool Whether to fix broken revisions when found
66 */
67 private function checkOrphans( $fix ) {
68 $dbw = wfGetDB( DB_MASTER );
69 $page = $dbw->tableName( 'page' );
70 $revision = $dbw->tableName( 'revision' );
71
72 if ( $fix ) {
73 $this->lockTables( $dbw );
74 }
75
76 $this->output( "Checking for orphan revision table entries... (this may take a while on a large wiki)\n" );
77 $result = $dbw->query( "
78 SELECT *
79 FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
80 WHERE page_id IS NULL
81 " );
82 $orphans = $dbw->numRows( $result );
83 if ( $orphans > 0 ) {
84 global $wgContLang;
85 $this->output( "$orphans orphan revisions...\n" );
86 $this->output( sprintf( "%10s %10s %14s %20s %s\n", 'rev_id', 'rev_page', 'rev_timestamp', 'rev_user_text', 'rev_comment' ) );
87 foreach ( $result as $row ) {
88 $comment = ( $row->rev_comment == '' )
89 ? ''
90 : '(' . $wgContLang->truncate( $row->rev_comment, 40 ) . ')';
91 $this->output( sprintf( "%10d %10d %14s %20s %s\n",
92 $row->rev_id,
93 $row->rev_page,
94 $row->rev_timestamp,
95 $wgContLang->truncate( $row->rev_user_text, 17 ),
96 $comment ) );
97 if ( $fix ) {
98 $dbw->delete( 'revision', array( 'rev_id' => $row->rev_id ) );
99 }
100 }
101 if ( !$fix ) {
102 $this->output( "Run again with --fix to remove these entries automatically.\n" );
103 }
104 } else {
105 $this->output( "No orphans! Yay!\n" );
106 }
107
108 if ( $fix ) {
109 $dbw->unlockTables();
110 }
111 }
112
113 /**
114 * @param $fix bool
115 * @todo DON'T USE THIS YET! It will remove entries which have children,
116 * but which aren't properly attached (eg if page_latest is bogus
117 * but valid revisions do exist)
118 */
119 private function checkWidows( $fix ) {
120 $dbw = wfGetDB( DB_MASTER );
121 $page = $dbw->tableName( 'page' );
122 $revision = $dbw->tableName( 'revision' );
123
124 if ( $fix ) {
125 $this->lockTables( $dbw );
126 }
127
128 $this->output( "\nChecking for childless page table entries... (this may take a while on a large wiki)\n" );
129 $result = $dbw->query( "
130 SELECT *
131 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
132 WHERE rev_id IS NULL
133 " );
134 $widows = $dbw->numRows( $result );
135 if ( $widows > 0 ) {
136 $this->output( "$widows childless pages...\n" );
137 $this->output( sprintf( "%10s %11s %2s %s\n", 'page_id', 'page_latest', 'ns', 'page_title' ) );
138 foreach ( $result as $row ) {
139 printf( "%10d %11d %2d %s\n",
140 $row->page_id,
141 $row->page_latest,
142 $row->page_namespace,
143 $row->page_title );
144 if ( $fix ) {
145 $dbw->delete( 'page', array( 'page_id' => $row->page_id ) );
146 }
147 }
148 if ( !$fix ) {
149 $this->output( "Run again with --fix to remove these entries automatically.\n" );
150 }
151 } else {
152 $this->output( "No childless pages! Yay!\n" );
153 }
154
155 if ( $fix ) {
156 $dbw->unlockTables();
157 }
158 }
159
160 /**
161 * Check for pages where page_latest is wrong
162 * @param $fix bool Whether to fix broken entries
163 */
164 private function checkSeparation( $fix ) {
165 $dbw = wfGetDB( DB_MASTER );
166 $page = $dbw->tableName( 'page' );
167 $revision = $dbw->tableName( 'revision' );
168 $text = $dbw->tableName( 'text' );
169
170 if ( $fix ) {
171 $dbw->lockTables( $dbw, 'text' );
172 }
173
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( "
176 SELECT *
177 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
178 " );
179 $found = 0;
180 foreach ( $result as $row ) {
181 $result2 = $dbw->query( "
182 SELECT MAX(rev_timestamp) as max_timestamp
183 FROM $revision
184 WHERE rev_page=$row->page_id
185 " );
186 $row2 = $dbw->fetchObject( $result2 );
187 $dbw->freeResult( $result2 );
188 if ( $row2 ) {
189 if ( $row->rev_timestamp != $row2->max_timestamp ) {
190 if ( $found == 0 ) {
191 $this->output( sprintf( "%10s %10s %14s %14s\n",
192 'page_id', 'rev_id', 'timestamp', 'max timestamp' ) );
193 }
194 ++$found;
195 $this->output( sprintf( "%10d %10d %14s %14s\n",
196 $row->page_id,
197 $row->page_latest,
198 $row->rev_timestamp,
199 $row2->max_timestamp ) );
200 if ( $fix ) {
201 # ...
202 $maxId = $dbw->selectField(
203 'revision',
204 'rev_id',
205 array(
206 'rev_page' => $row->page_id,
207 'rev_timestamp' => $row2->max_timestamp ) );
208 $this->output( "... updating to revision $maxId\n" );
209 $maxRev = Revision::newFromId( $maxId );
210 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
211 $article = new Article( $title );
212 $article->updateRevisionOn( $dbw, $maxRev );
213 }
214 }
215 } else {
216 $this->output( "wtf\n" );
217 }
218 }
219
220 if ( $found ) {
221 $this->output( "Found $found pages with incorrect latest revision.\n" );
222 } else {
223 $this->output( "No pages with incorrect latest revision. Yay!\n" );
224 }
225 if ( !$fix && $found > 0 ) {
226 $this->output( "Run again with --fix to remove these entries automatically.\n" );
227 }
228
229 if ( $fix ) {
230 $dbw->unlockTables();
231 }
232 }
233 }
234
235 $maintClass = "Orphans";
236 require_once( DO_MAINTENANCE );