Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.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 * @author <brion@pobox.com>
27 * @addtogroup Maintenance
28 */
29
30 $options = array( 'fix' );
31
32 /** */
33 require_once( 'commandLine.inc' );
34 $wgTitle = Title::newFromText( 'Orphan revision cleanup script' );
35
36 checkOrphans( isset( $options['fix'] ) );
37 checkSeparation( isset( $options['fix'] ) );
38 #checkWidows( isset( $options['fix'] ) );
39
40 # ------
41
42 function checkOrphans( $fix ) {
43 $dbw =& wfGetDB( DB_MASTER );
44 $page = $dbw->tableName( 'page' );
45 $revision = $dbw->tableName( 'revision' );
46
47 if( $fix ) {
48 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE" );
49 }
50
51 echo "Checking for orphan revision table entries... (this may take a while on a large wiki)\n";
52 $result = $dbw->query( "
53 SELECT *
54 FROM $revision LEFT OUTER JOIN $page ON rev_page=page_id
55 WHERE page_id IS NULL
56 ");
57 $orphans = $dbw->numRows( $result );
58 if( $orphans > 0 ) {
59 global $wgContLang;
60 echo "$orphans orphan revisions...\n";
61 printf( "%10s %10s %14s %20s %s\n", 'rev_id', 'rev_page', 'rev_timestamp', 'rev_user_text', 'rev_comment' );
62 while( $row = $dbw->fetchObject( $result ) ) {
63 $comment = ( $row->rev_comment == '' )
64 ? ''
65 : '(' . $wgContLang->truncate( $row->rev_comment, 40, '...' ) . ')';
66 printf( "%10d %10d %14s %20s %s\n",
67 $row->rev_id,
68 $row->rev_page,
69 $row->rev_timestamp,
70 $wgContLang->truncate( $row->rev_user_text, 17, '...' ),
71 $comment );
72 if( $fix ) {
73 $dbw->delete( 'revision', array( 'rev_id' => $row->rev_id ) );
74 }
75 }
76 if( !$fix ) {
77 echo "Run again with --fix to remove these entries automatically.\n";
78 }
79 } else {
80 echo "No orphans! Yay!\n";
81 }
82
83 if( $fix ) {
84 $dbw->query( "UNLOCK TABLES" );
85 }
86 }
87
88 /**
89 * @todo DON'T USE THIS YET! It will remove entries which have children,
90 * but which aren't properly attached (eg if page_latest is bogus
91 * but valid revisions do exist)
92 */
93 function checkWidows( $fix ) {
94 $dbw =& wfGetDB( DB_MASTER );
95 $page = $dbw->tableName( 'page' );
96 $revision = $dbw->tableName( 'revision' );
97
98 if( $fix ) {
99 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE" );
100 }
101
102 echo "\nChecking for childless page table entries... (this may take a while on a large wiki)\n";
103 $result = $dbw->query( "
104 SELECT *
105 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
106 WHERE rev_id IS NULL
107 ");
108 $widows = $dbw->numRows( $result );
109 if( $widows > 0 ) {
110 global $wgContLang;
111 echo "$widows childless pages...\n";
112 printf( "%10s %11s %2s %s\n", 'page_id', 'page_latest', 'ns', 'page_title' );
113 while( $row = $dbw->fetchObject( $result ) ) {
114 printf( "%10d %11d %2d %s\n",
115 $row->page_id,
116 $row->page_latest,
117 $row->page_namespace,
118 $row->page_title );
119 if( $fix ) {
120 $dbw->delete( 'page', array( 'page_id' => $row->page_id ) );
121 }
122 }
123 if( !$fix ) {
124 echo "Run again with --fix to remove these entries automatically.\n";
125 }
126 } else {
127 echo "No childless pages! Yay!\n";
128 }
129
130 if( $fix ) {
131 $dbw->query( "UNLOCK TABLES" );
132 }
133 }
134
135
136 function checkSeparation( $fix ) {
137 $dbw =& wfGetDB( DB_MASTER );
138 $page = $dbw->tableName( 'page' );
139 $revision = $dbw->tableName( 'revision' );
140 $text = $dbw->tableName( 'text' );
141
142 if( $fix ) {
143 $dbw->query( "LOCK TABLES $page WRITE, $revision WRITE, $text WRITE" );
144 }
145
146 echo "\nChecking for pages whose page_latest links are incorrect... (this may take a while on a large wiki)\n";
147 $result = $dbw->query( "
148 SELECT *
149 FROM $page LEFT OUTER JOIN $revision ON page_latest=rev_id
150 ");
151 $found = 0;
152 while( $row = $dbw->fetchObject( $result ) ) {
153 $result2 = $dbw->query( "
154 SELECT MAX(rev_timestamp) as max_timestamp
155 FROM $revision
156 WHERE rev_page=$row->page_id
157 " );
158 $row2 = $dbw->fetchObject( $result2 );
159 $dbw->freeResult( $result2 );
160 if( $row2 ) {
161 if( $row->rev_timestamp != $row2->max_timestamp ) {
162 if( $found == 0 ) {
163 printf( "%10s %10s %14s %14s\n",
164 'page_id', 'rev_id', 'timestamp', 'max timestamp' );
165 }
166 ++$found;
167 printf( "%10d %10d %14s %14s\n",
168 $row->page_id,
169 $row->page_latest,
170 $row->rev_timestamp,
171 $row2->max_timestamp );
172 if( $fix ) {
173 # ...
174 $maxId = $dbw->selectField(
175 'revision',
176 'rev_id',
177 array(
178 'rev_page' => $row->page_id,
179 'rev_timestamp' => $row2->max_timestamp ) );
180 echo "... updating to revision $maxId\n";
181 $maxRev = Revision::newFromId( $maxId );
182 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
183 $article = new Article( $title );
184 $article->updateRevisionOn( $dbw, $maxRev );
185 }
186 }
187 } else {
188 echo "wtf\n";
189 }
190 }
191
192 if( $found ) {
193 echo "Found $found pages with incorrect latest revision.\n";
194 } else {
195 echo "No pages with incorrect latest revision. Yay!\n";
196 }
197 if( !$fix && $found > 0 ) {
198 echo "Run again with --fix to remove these entries automatically.\n";
199 }
200
201 if( $fix ) {
202 $dbw->query( "UNLOCK TABLES" );
203 }
204 }
205
206 ?>