]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/deleteOldRevisions.php
MediaWiki 1.16.4-scripts
[autoinstalls/mediawiki.git] / maintenance / deleteOldRevisions.php
1 <?php
2
3 /**
4  * Delete old (non-current) revisions from the database
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  * http://www.gnu.org/copyleft/gpl.html
20  *
21  * @ingroup Maintenance
22  * @author Rob Church <robchur@gmail.com>
23  */
24
25 require_once( dirname(__FILE__) . '/Maintenance.php' );
26
27 class DeleteOldRevisions extends Maintenance {
28         public function __construct() {
29                 parent::__construct();
30                 $this->mDescription = "Delete old (non-current) revisions from the database";
31                 $this->addOption( 'delete', 'Actually perform the deletion' );
32                 $this->addOption( 'page_id', 'List of page ids to work on', false );
33         }
34         
35         public function execute() {
36                 $this->output( "Delete old revisions\n\n" );
37                 $this->doDelete( $this->hasOption( 'delete' ), $this->mArgs );
38         }
39         
40         function doDelete( $delete = false, $args = array() ) {
41
42                 # Data should come off the master, wrapped in a transaction
43                 $dbw = wfGetDB( DB_MASTER );
44                 $dbw->begin();
45         
46                 $tbl_pag = $dbw->tableName( 'page' );
47                 $tbl_rev = $dbw->tableName( 'revision' );
48         
49                 $pageIdClause = '';
50                 $revPageClause = '';
51         
52                 # If a list of page_ids was provided, limit results to that set of page_ids
53                 if ( sizeof( $args ) > 0 ) {
54                         $pageIdList = implode( ',', $args );
55                         $pageIdClause = " WHERE page_id IN ({$pageIdList})";
56                         $revPageClause = " AND rev_page IN ({$pageIdList})";
57                         $this->output( "Limiting to {$tbl_pag}.page_id IN ({$pageIdList})\n" );
58                 }
59         
60                 # Get "active" revisions from the page table
61                 $this->output( "Searching for active revisions..." );
62                 $res = $dbw->query( "SELECT page_latest FROM $tbl_pag{$pageIdClause}" );
63                 foreach( $res as $row ) {
64                         $cur[] = $row->page_latest;
65                 }
66                 $this->output( "done.\n" );
67         
68                 # Get all revisions that aren't in this set
69                 $old = array();
70                 $this->output( "Searching for inactive revisions..." );
71                 $set = implode( ', ', $cur );
72                 $res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_id NOT IN ( $set ){$revPageClause}" );
73                 foreach( $res as $row ) {
74                         $old[] = $row->rev_id;
75                 }
76                 $this->output( "done.\n" );
77         
78                 # Inform the user of what we're going to do
79                 $count = count( $old );
80                 $this->output( "$count old revisions found.\n" );
81         
82                 # Delete as appropriate
83                 if( $delete && $count ) {
84                         $this->output( "Deleting..." );
85                         $set = implode( ', ', $old );
86                         $dbw->query( "DELETE FROM $tbl_rev WHERE rev_id IN ( $set )" );
87                         $this->output( "done.\n" );
88                 }
89         
90                 # This bit's done
91                 # Purge redundant text records
92                 $dbw->commit();
93                 if( $delete ) {
94                         $this->purgeRedundantText( true );
95                 }
96         }
97 }
98
99 $maintClass = "DeleteOldRevisions";
100 require_once( DO_MAINTENANCE );
101