Use __DIR__ instead of dirname( __FILE__ )
[lhc/web/wiklou.git] / maintenance / deleteOldRevisions.php
1 <?php
2 /**
3 * Delete old (non-current) revisions from the database
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 */
24
25 require_once( __DIR__ . '/Maintenance.php' );
26
27 /**
28 * Maintenance script that deletes old (non-current) revisions from the database.
29 *
30 * @ingroup Maintenance
31 */
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 );
38 }
39
40 public function execute() {
41 $this->output( "Delete old revisions\n\n" );
42 $this->doDelete( $this->hasOption( 'delete' ), $this->mArgs );
43 }
44
45 function doDelete( $delete = false, $args = array() ) {
46
47 # Data should come off the master, wrapped in a transaction
48 $dbw = wfGetDB( DB_MASTER );
49 $dbw->begin( __METHOD__ );
50
51 $tbl_pag = $dbw->tableName( 'page' );
52 $tbl_rev = $dbw->tableName( 'revision' );
53
54 $pageIdClause = '';
55 $revPageClause = '';
56
57 # If a list of page_ids was provided, limit results to that set of page_ids
58 if ( sizeof( $args ) > 0 ) {
59 $pageIdList = implode( ',', $args );
60 $pageIdClause = " WHERE page_id IN ({$pageIdList})";
61 $revPageClause = " AND rev_page IN ({$pageIdList})";
62 $this->output( "Limiting to {$tbl_pag}.page_id IN ({$pageIdList})\n" );
63 }
64
65 # Get "active" revisions from the page table
66 $this->output( "Searching for active revisions..." );
67 $res = $dbw->query( "SELECT page_latest FROM $tbl_pag{$pageIdClause}" );
68 $cur = array();
69 foreach ( $res as $row ) {
70 $cur[] = $row->page_latest;
71 }
72 $this->output( "done.\n" );
73
74 # Get all revisions that aren't in this set
75 $old = array();
76 $this->output( "Searching for inactive revisions..." );
77 $set = implode( ', ', $cur );
78 $res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_id NOT IN ( $set ){$revPageClause}" );
79 foreach ( $res as $row ) {
80 $old[] = $row->rev_id;
81 }
82 $this->output( "done.\n" );
83
84 # Inform the user of what we're going to do
85 $count = count( $old );
86 $this->output( "$count old revisions found.\n" );
87
88 # Delete as appropriate
89 if ( $delete && $count ) {
90 $this->output( "Deleting..." );
91 $set = implode( ', ', $old );
92 $dbw->query( "DELETE FROM $tbl_rev WHERE rev_id IN ( $set )" );
93 $this->output( "done.\n" );
94 }
95
96 # This bit's done
97 # Purge redundant text records
98 $dbw->commit( __METHOD__ );
99 if ( $delete ) {
100 $this->purgeRedundantText( true );
101 }
102 }
103 }
104
105 $maintClass = "DeleteOldRevisions";
106 require_once( RUN_MAINTENANCE_IF_MAIN );
107