]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/orphans.php
MediaWiki 1.14.0
[autoinstalls/mediawiki.git] / maintenance / orphans.php
1 <?php
2 # Copyright (C) 2005 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21  * Look for 'orphan' revisions hooked to pages which don't exist
22  * And 'childless' pages with no revisions.
23  * Then, kill the poor widows and orphans.
24  * Man this is depressing.
25  *
26  * @file
27  * @author <brion@pobox.com>
28  * @ingroup Maintenance
29  */
30
31 $options = array( 'fix' );
32
33 /** */
34 require_once( 'commandLine.inc' );
35 $wgTitle = Title::newFromText( 'Orphan revision cleanup script' );
36
37 checkOrphans( isset( $options['fix'] ) );
38 checkSeparation( isset( $options['fix'] ) );
39 #checkWidows( isset( $options['fix'] ) );
40
41 # ------
42
43 function checkOrphans( $fix ) {
44         $dbw = wfGetDB( DB_MASTER );
45         $page = $dbw->tableName( 'page' );
46         $revision = $dbw->tableName( 'revision' );
47
48         if( $fix ) {
49                 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE" );
50         }
51
52         echo "Checking for orphan revision table entries... (this may take a while on a large wiki)\n";
53         $result = $dbw->query( "
54                 SELECT *
55                 FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
56                 WHERE page_id IS NULL
57         ");
58         $orphans = $dbw->numRows( $result );
59         if( $orphans > 0 ) {
60                 global $wgContLang;
61                 echo "$orphans orphan revisions...\n";
62                 printf( "%10s %10s %14s %20s %s\n", 'rev_id', 'rev_page', 'rev_timestamp', 'rev_user_text', 'rev_comment' );
63                 while( $row = $dbw->fetchObject( $result ) ) {
64                         $comment = ( $row->rev_comment == '' )
65                                 ? ''
66                                 : '(' . $wgContLang->truncate( $row->rev_comment, 40, '...' ) . ')';
67                         printf( "%10d %10d %14s %20s %s\n",
68                                 $row->rev_id,
69                                 $row->rev_page,
70                                 $row->rev_timestamp,
71                                 $wgContLang->truncate( $row->rev_user_text, 17, '...' ),
72                                 $comment );
73                         if( $fix ) {
74                                 $dbw->delete( 'revision', array( 'rev_id' => $row->rev_id ) );
75                         }
76                 }
77                 if( !$fix ) {
78                         echo "Run again with --fix to remove these entries automatically.\n";
79                 }
80         } else {
81                 echo "No orphans! Yay!\n";
82         }
83
84         if( $fix ) {
85                 $dbw->query( "UNLOCK TABLES" );
86         }
87 }
88
89 /**
90  * @todo DON'T USE THIS YET! It will remove entries which have children,
91  *       but which aren't properly attached (eg if page_latest is bogus
92  *       but valid revisions do exist)
93  */
94 function checkWidows( $fix ) {
95         $dbw = wfGetDB( DB_MASTER );
96         $page = $dbw->tableName( 'page' );
97         $revision = $dbw->tableName( 'revision' );
98
99         if( $fix ) {
100                 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE" );
101         }
102
103         echo "\nChecking for childless page table entries... (this may take a while on a large wiki)\n";
104         $result = $dbw->query( "
105                 SELECT *
106                 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
107                 WHERE rev_id IS NULL
108         ");
109         $widows = $dbw->numRows( $result );
110         if( $widows > 0 ) {
111                 global $wgContLang;
112                 echo "$widows childless pages...\n";
113                 printf( "%10s %11s %2s %s\n", 'page_id', 'page_latest', 'ns', 'page_title' );
114                 while( $row = $dbw->fetchObject( $result ) ) {
115                         printf( "%10d %11d %2d %s\n",
116                                 $row->page_id,
117                                 $row->page_latest,
118                                 $row->page_namespace,
119                                 $row->page_title );
120                         if( $fix ) {
121                                 $dbw->delete( 'page', array( 'page_id' => $row->page_id ) );
122                         }
123                 }
124                 if( !$fix ) {
125                         echo "Run again with --fix to remove these entries automatically.\n";
126                 }
127         } else {
128                 echo "No childless pages! Yay!\n";
129         }
130
131         if( $fix ) {
132                 $dbw->query( "UNLOCK TABLES" );
133         }
134 }
135
136
137 function checkSeparation( $fix ) {
138         $dbw = wfGetDB( DB_MASTER );
139         $page     = $dbw->tableName( 'page' );
140         $revision = $dbw->tableName( 'revision' );
141         $text     = $dbw->tableName( 'text' );
142
143         if( $fix ) {
144                 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE, $text WRITE" );
145         }
146
147         echo "\nChecking for pages whose page_latest links are incorrect... (this may take a while on a large wiki)\n";
148         $result = $dbw->query( "
149                 SELECT *
150                 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
151         ");
152         $found = 0;
153         while( $row = $dbw->fetchObject( $result ) ) {
154                 $result2 = $dbw->query( "
155                         SELECT MAX(rev_timestamp) as max_timestamp
156                         FROM $revision
157                         WHERE rev_page=$row->page_id
158                 " );
159                 $row2 = $dbw->fetchObject( $result2 );
160                 $dbw->freeResult( $result2 );
161                 if( $row2 ) {
162                         if( $row->rev_timestamp != $row2->max_timestamp ) {
163                                 if( $found == 0 ) {
164                                         printf( "%10s %10s %14s %14s\n",
165                                                 'page_id', 'rev_id', 'timestamp', 'max timestamp' );
166                                 }
167                                 ++$found;
168                                 printf( "%10d %10d %14s %14s\n",
169                                         $row->page_id,
170                                         $row->page_latest,
171                                         $row->rev_timestamp,
172                                         $row2->max_timestamp );
173                                 if( $fix ) {
174                                         # ...
175                                         $maxId = $dbw->selectField(
176                                                 'revision',
177                                                 'rev_id',
178                                                 array(
179                                                         'rev_page'      => $row->page_id,
180                                                         'rev_timestamp' => $row2->max_timestamp ) );
181                                         echo "... updating to revision $maxId\n";
182                                         $maxRev = Revision::newFromId( $maxId );
183                                         $title = Title::makeTitle( $row->page_namespace, $row->page_title );
184                                         $article = new Article( $title );
185                                         $article->updateRevisionOn( $dbw, $maxRev );
186                                 }
187                         }
188                 } else {
189                         echo "wtf\n";
190                 }
191         }
192
193         if( $found ) {
194                 echo "Found $found pages with incorrect latest revision.\n";
195         } else {
196                 echo "No pages with incorrect latest revision. Yay!\n";
197         }
198         if( !$fix && $found > 0 ) {
199                 echo "Run again with --fix to remove these entries automatically.\n";
200         }
201
202         if( $fix ) {
203                 $dbw->query( "UNLOCK TABLES" );
204         }
205 }
206