Merge "Print chained exceptions when maintenance script fails."
[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->addDescription( '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 = [] ) {
46 # Data should come off the master, wrapped in a transaction
47 $dbw = $this->getDB( DB_MASTER );
48 $this->beginTransaction( $dbw, __METHOD__ );
49
50 $pageConds = [];
51 $revConds = [];
52
53 # If a list of page_ids was provided, limit results to that set of page_ids
54 if ( count( $args ) > 0 ) {
55 $pageConds['page_id'] = $args;
56 $revConds['rev_page'] = $args;
57 $this->output( "Limiting to page IDs " . implode( ',', $args ) . "\n" );
58 }
59
60 # Get "active" revisions from the page table
61 $this->output( "Searching for active revisions..." );
62 $res = $dbw->select( 'page', 'page_latest', $pageConds, __METHOD__ );
63 $latestRevs = [];
64 foreach ( $res as $row ) {
65 $latestRevs[] = $row->page_latest;
66 }
67 $this->output( "done.\n" );
68
69 # Get all revisions that aren't in this set
70 $this->output( "Searching for inactive revisions..." );
71 if ( count( $latestRevs ) > 0 ) {
72 $revConds[] = 'rev_id NOT IN (' . $dbw->makeList( $latestRevs ) . ')';
73 }
74 $res = $dbw->select( 'revision', 'rev_id', $revConds, __METHOD__ );
75 $oldRevs = [];
76 foreach ( $res as $row ) {
77 $oldRevs[] = $row->rev_id;
78 }
79 $this->output( "done.\n" );
80
81 # Inform the user of what we're going to do
82 $count = count( $oldRevs );
83 $this->output( "$count old revisions found.\n" );
84
85 # Delete as appropriate
86 if ( $delete && $count ) {
87 $this->output( "Deleting..." );
88 $dbw->delete( 'revision', [ 'rev_id' => $oldRevs ], __METHOD__ );
89 $dbw->delete( 'ip_changes', [ 'ipc_rev_id' => $oldRevs ], __METHOD__ );
90 $this->output( "done.\n" );
91 }
92
93 # Purge redundant text records
94 $this->commitTransaction( $dbw, __METHOD__ );
95 if ( $delete ) {
96 $this->purgeRedundantText( true );
97 }
98 }
99 }
100
101 $maintClass = DeleteOldRevisions::class;
102 require_once RUN_MAINTENANCE_IF_MAIN;