Merge "Whitelist the <wbr> element."
[mediawiki.git] / maintenance / deleteOldRevisions.php
blob847d863412662f4996edb5b076fed3d1087ce84b
1 <?php
2 /**
3 * Delete old (non-current) revisions from the database
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.
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.
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
20 * @file
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
25 require_once __DIR__ . '/Maintenance.php';
27 /**
28 * Maintenance script that deletes old (non-current) revisions from the database.
30 * @ingroup Maintenance
32 class DeleteOldRevisions extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Delete old (non-current) revisions from the database";
36 $this->addOption( 'delete', 'Actually perform the deletion' );
37 $this->addOption( 'page_id', 'List of page ids to work on', false );
40 public function execute() {
41 $this->output( "Delete old revisions\n\n" );
42 $this->doDelete( $this->hasOption( 'delete' ), $this->mArgs );
45 function doDelete( $delete = false, $args = array() ) {
47 # Data should come off the master, wrapped in a transaction
48 $dbw = wfGetDB( DB_MASTER );
49 $dbw->begin( __METHOD__ );
51 $pageConds = array();
52 $revConds = array();
54 # If a list of page_ids was provided, limit results to that set of page_ids
55 if ( count( $args ) > 0 ) {
56 $pageConds['page_id'] = $args;
57 $revConds['rev_page'] = $args;
58 $this->output( "Limiting to page IDs " . implode( ',', $args ) . "\n" );
61 # Get "active" revisions from the page table
62 $this->output( "Searching for active revisions..." );
63 $res = $dbw->select( 'page', 'page_latest', $pageConds, __METHOD__ );
64 $latestRevs = array();
65 foreach ( $res as $row ) {
66 $latestRevs[] = $row->page_latest;
68 $this->output( "done.\n" );
70 # Get all revisions that aren't in this set
71 $this->output( "Searching for inactive revisions..." );
72 if ( count( $latestRevs ) > 0 ) {
73 $revConds[] = 'rev_id NOT IN (' . $dbw->makeList( $latestRevs ) . ')';
75 $res = $dbw->select( 'revision', 'rev_id', $revConds, __METHOD__ );
76 $oldRevs = array();
77 foreach ( $res as $row ) {
78 $oldRevs[] = $row->rev_id;
80 $this->output( "done.\n" );
82 # Inform the user of what we're going to do
83 $count = count( $oldRevs );
84 $this->output( "$count old revisions found.\n" );
86 # Delete as appropriate
87 if ( $delete && $count ) {
88 $this->output( "Deleting..." );
89 $dbw->delete( 'revision', array( 'rev_id' => $oldRevs ), __METHOD__ );
90 $this->output( "done.\n" );
93 # This bit's done
94 # Purge redundant text records
95 $dbw->commit( __METHOD__ );
96 if ( $delete ) {
97 $this->purgeRedundantText( true );
102 $maintClass = "DeleteOldRevisions";
103 require_once RUN_MAINTENANCE_IF_MAIN;