]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/deleteOrphanedRevisions.php
MediaWiki 1.16.4-scripts
[autoinstalls/mediawiki.git] / maintenance / deleteOrphanedRevisions.php
1 <?php
2
3 /**
4  * Maintenance script to delete revisions which refer to a nonexisting page
5  * Sometimes manual deletion done in a rush leaves crap in the database
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  * http://www.gnu.org/copyleft/gpl.html
21  *
22  * @ingroup Maintenance
23  * @author Rob Church <robchur@gmail.com>
24  * @todo More efficient cleanup of text records
25  */
26
27 require_once( dirname(__FILE__) . '/Maintenance.php' );
28
29 class DeleteOrphanedRevisions extends Maintenance {
30         public function __construct() {
31                 parent::__construct();
32                 $this->mDescription = "Maintenance script to delete revisions which refer to a nonexisting page";
33                 $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
34         }
35
36         public function execute() {
37                 $this->output( "Delete Orphaned Revisions\n" );
38
39                 $report = $this->hasOption('report');
40
41                 $dbw = wfGetDB( DB_MASTER );
42                 $dbw->begin();
43                 list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' );
44
45                 # Find all the orphaned revisions
46                 $this->output( "Checking for orphaned revisions..." );
47                 $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id WHERE page_namespace IS NULL";
48                 $res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
49         
50                 # Stash 'em all up for deletion (if needed)
51                 $revisions = array();
52                 foreach( $res as $row )
53                         $revisions[] = $row->rev_id;
54                 $dbw->freeResult( $res );
55                 $count = count( $revisions );
56                 $this->output( "found {$count}.\n" );
57         
58                 # Nothing to do?
59                 if( $report || $count == 0 ) {
60                         $dbw->commit();
61                         exit(0);
62                 }
63         
64                 # Delete each revision
65                 $this->output( "Deleting..." );
66                 $this->deleteRevs( $revisions, $dbw );
67                 $this->output( "done.\n" );
68         
69                 # Close the transaction and call the script to purge unused text records
70                 $dbw->commit();
71                 $this->purgeRedundantText( true );
72         }
73         
74         /**
75          * Delete one or more revisions from the database
76          * Do this inside a transaction
77          *
78          * @param $id Array of revision id values
79          * @param $db Database class (needs to be a master)
80          */
81         private function deleteRevs( $id, &$dbw ) {
82                 if( !is_array( $id ) )
83                         $id = array( $id );
84                 $dbw->delete( 'revision', array( 'rev_id' => $id ), __METHOD__ );
85         }
86 }
87
88 $maintClass = "DeleteOrphanedRevisions";
89 require_once( DO_MAINTENANCE );
90