Merge "Purge Squid variant pages based on page language (not $wgContLang)"
[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 DatabaseBase object
54 * @param $extraTable String The name of any extra tables to lock (eg: text)
55 */
56 private function lockTables( $db, $extraTable = array() ) {
57 $tbls = array( 'page', 'revision', 'redirect' );
58 if ( $extraTable ) {
59 $tbls = array_merge( $tbls, $extraTable );
60 }
61 $db->lockTables( array(), $tbls, __METHOD__, false );
62 }
63
64 /**
65 * Check for orphan revisions
66 * @param $fix bool Whether to fix broken revisions when found
67 */
68 private function checkOrphans( $fix ) {
69 $dbw = wfGetDB( DB_MASTER );
70 $page = $dbw->tableName( 'page' );
71 $revision = $dbw->tableName( 'revision' );
72
73 if ( $fix ) {
74 $this->lockTables( $dbw );
75 }
76
77 $this->output( "Checking for orphan revision table entries... (this may take a while on a large wiki)\n" );
78 $result = $dbw->query( "
79 SELECT *
80 FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
81 WHERE page_id IS NULL
82 " );
83 $orphans = $dbw->numRows( $result );
84 if ( $orphans > 0 ) {
85 global $wgContLang;
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 == '' )
90 ? ''
91 : '(' . $wgContLang->truncate( $row->rev_comment, 40 ) . ')';
92 $this->output( sprintf( "%10d %10d %14s %20s %s\n",
93 $row->rev_id,
94 $row->rev_page,
95 $row->rev_timestamp,
96 $wgContLang->truncate( $row->rev_user_text, 17 ),
97 $comment ) );
98 if ( $fix ) {
99 $dbw->delete( 'revision', array( 'rev_id' => $row->rev_id ) );
100 }
101 }
102 if ( !$fix ) {
103 $this->output( "Run again with --fix to remove these entries automatically.\n" );
104 }
105 } else {
106 $this->output( "No orphans! Yay!\n" );
107 }
108
109 if ( $fix ) {
110 $dbw->unlockTables( __METHOD__ );
111 }
112 }
113
114 /**
115 * @param $fix bool
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)
119 */
120 private function checkWidows( $fix ) {
121 $dbw = wfGetDB( DB_MASTER );
122 $page = $dbw->tableName( 'page' );
123 $revision = $dbw->tableName( 'revision' );
124
125 if ( $fix ) {
126 $this->lockTables( $dbw );
127 }
128
129 $this->output( "\nChecking for childless page table entries... (this may take a while on a large wiki)\n" );
130 $result = $dbw->query( "
131 SELECT *
132 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
133 WHERE rev_id IS NULL
134 " );
135 $widows = $dbw->numRows( $result );
136 if ( $widows > 0 ) {
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",
141 $row->page_id,
142 $row->page_latest,
143 $row->page_namespace,
144 $row->page_title );
145 if ( $fix ) {
146 $dbw->delete( 'page', array( 'page_id' => $row->page_id ) );
147 }
148 }
149 if ( !$fix ) {
150 $this->output( "Run again with --fix to remove these entries automatically.\n" );
151 }
152 } else {
153 $this->output( "No childless pages! Yay!\n" );
154 }
155
156 if ( $fix ) {
157 $dbw->unlockTables( __METHOD__ );
158 }
159 }
160
161 /**
162 * Check for pages where page_latest is wrong
163 * @param $fix bool Whether to fix broken entries
164 */
165 private function checkSeparation( $fix ) {
166 $dbw = wfGetDB( DB_MASTER );
167 $page = $dbw->tableName( 'page' );
168 $revision = $dbw->tableName( 'revision' );
169
170 if ( $fix ) {
171 $this->lockTables( $dbw, array( 'user', '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 if ( $row2 ) {
188 if ( $row->rev_timestamp != $row2->max_timestamp ) {
189 if ( $found == 0 ) {
190 $this->output( sprintf( "%10s %10s %14s %14s\n",
191 'page_id', 'rev_id', 'timestamp', 'max timestamp' ) );
192 }
193 ++$found;
194 $this->output( sprintf( "%10d %10d %14s %14s\n",
195 $row->page_id,
196 $row->page_latest,
197 $row->rev_timestamp,
198 $row2->max_timestamp ) );
199 if ( $fix ) {
200 # ...
201 $maxId = $dbw->selectField(
202 'revision',
203 'rev_id',
204 array(
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 );
212 }
213 }
214 } else {
215 $this->output( "wtf\n" );
216 }
217 }
218
219 if ( $found ) {
220 $this->output( "Found $found pages with incorrect latest revision.\n" );
221 } else {
222 $this->output( "No pages with incorrect latest revision. Yay!\n" );
223 }
224 if ( !$fix && $found > 0 ) {
225 $this->output( "Run again with --fix to remove these entries automatically.\n" );
226 }
227
228 if ( $fix ) {
229 $dbw->unlockTables( __METHOD__ );
230 }
231 }
232 }
233
234 $maintClass = "Orphans";
235 require_once( RUN_MAINTENANCE_IF_MAIN );