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