Merge "(bug 32297) Use symbolic names, not offsets for a default timezone."
[lhc/web/wiklou.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( __METHOD__ );
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 $cur = array();
64 foreach ( $res as $row ) {
65 $cur[] = $row->page_latest;
66 }
67 $this->output( "done.\n" );
68
69 # Get all revisions that aren't in this set
70 $old = array();
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;
76 }
77 $this->output( "done.\n" );
78
79 # Inform the user of what we're going to do
80 $count = count( $old );
81 $this->output( "$count old revisions found.\n" );
82
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" );
89 }
90
91 # This bit's done
92 # Purge redundant text records
93 $dbw->commit( __METHOD__ );
94 if ( $delete ) {
95 $this->purgeRedundantText( true );
96 }
97 }
98 }
99
100 $maintClass = "DeleteOldRevisions";
101 require_once( RUN_MAINTENANCE_IF_MAIN );
102