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