]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/orphans.php
MediaWiki 1.17.1-scripts
[autoinstalls/mediawiki.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( __METHOD__ );
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( __METHOD__ );
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
169                 if ( $fix ) {
170                         $dbw->lockTables( $dbw, 'text', __METHOD__ );
171                 }
172
173                 $this->output( "\nChecking for pages whose page_latest links are incorrect... (this may take a while on a large wiki)\n" );
174                 $result = $dbw->query( "
175                         SELECT *
176                         FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
177                 " );
178                 $found = 0;
179                 foreach ( $result as $row ) {
180                         $result2 = $dbw->query( "
181                                 SELECT MAX(rev_timestamp) as max_timestamp
182                                 FROM $revision
183                                 WHERE rev_page=$row->page_id
184                         " );
185                         $row2 = $dbw->fetchObject( $result2 );
186                         if ( $row2 ) {
187                                 if ( $row->rev_timestamp != $row2->max_timestamp ) {
188                                         if ( $found == 0 ) {
189                                                 $this->output( sprintf( "%10s %10s %14s %14s\n",
190                                                         'page_id', 'rev_id', 'timestamp', 'max timestamp' ) );
191                                         }
192                                         ++$found;
193                                         $this->output( sprintf( "%10d %10d %14s %14s\n",
194                                                 $row->page_id,
195                                                 $row->page_latest,
196                                                 $row->rev_timestamp,
197                                                 $row2->max_timestamp ) );
198                                         if ( $fix ) {
199                                                 # ...
200                                                 $maxId = $dbw->selectField(
201                                                         'revision',
202                                                         'rev_id',
203                                                         array(
204                                                                 'rev_page'      => $row->page_id,
205                                                                 'rev_timestamp' => $row2->max_timestamp ) );
206                                                 $this->output( "... updating to revision $maxId\n" );
207                                                 $maxRev = Revision::newFromId( $maxId );
208                                                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
209                                                 $article = new Article( $title );
210                                                 $article->updateRevisionOn( $dbw, $maxRev );
211                                         }
212                                 }
213                         } else {
214                                 $this->output( "wtf\n" );
215                         }
216                 }
217
218                 if ( $found ) {
219                         $this->output( "Found $found pages with incorrect latest revision.\n" );
220                 } else {
221                         $this->output( "No pages with incorrect latest revision. Yay!\n" );
222                 }
223                 if ( !$fix && $found > 0 ) {
224                         $this->output( "Run again with --fix to remove these entries automatically.\n" );
225                 }
226
227                 if ( $fix ) {
228                         $dbw->unlockTables( __METHOD__ );
229                 }
230         }
231 }
232
233 $maintClass = "Orphans";
234 require_once( RUN_MAINTENANCE_IF_MAIN );