Start on a script to find and clean up orphan revisions
[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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 * @package MediaWiki
28 * @subpackage 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 #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 ?>