4 * Delete old (non-current) revisions from the database
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.
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.
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
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
25 require_once( dirname( __FILE__
) . '/Maintenance.php' );
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 );
35 public function execute() {
36 $this->output( "Delete old revisions\n\n" );
37 $this->doDelete( $this->hasOption( 'delete' ), $this->mArgs
);
40 function doDelete( $delete = false, $args = array() ) {
42 # Data should come off the master, wrapped in a transaction
43 $dbw = wfGetDB( DB_MASTER
);
44 $dbw->begin( __METHOD__
);
46 $tbl_pag = $dbw->tableName( 'page' );
47 $tbl_rev = $dbw->tableName( 'revision' );
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" );
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}" );
64 foreach ( $res as $row ) {
65 $cur[] = $row->page_latest
;
67 $this->output( "done.\n" );
69 # Get all revisions that aren't in this set
71 $this->output( "Searching for inactive revisions..." );
72 $set = implode( ', ', $cur );
73 $res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_id NOT IN ( $set ){$revPageClause}" );
74 foreach ( $res as $row ) {
75 $old[] = $row->rev_id
;
77 $this->output( "done.\n" );
79 # Inform the user of what we're going to do
80 $count = count( $old );
81 $this->output( "$count old revisions found.\n" );
83 # Delete as appropriate
84 if ( $delete && $count ) {
85 $this->output( "Deleting..." );
86 $set = implode( ', ', $old );
87 $dbw->query( "DELETE FROM $tbl_rev WHERE rev_id IN ( $set )" );
88 $this->output( "done.\n" );
92 # Purge redundant text records
93 $dbw->commit( __METHOD__
);
95 $this->purgeRedundantText( true );
100 $maintClass = "DeleteOldRevisions";
101 require_once( RUN_MAINTENANCE_IF_MAIN
);