Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteOldRevisions
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 3
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 doDelete
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 1
72
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
25require_once __DIR__ . '/Maintenance.php';
26
27/**
28 * Maintenance script that deletes old (non-current) revisions from the database.
29 *
30 * @ingroup Maintenance
31 */
32class 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->getArgs() );
43    }
44
45    private function doDelete( $delete = false, $pageIds = [] ) {
46        # Data should come off the master, wrapped in a transaction
47        $dbw = $this->getPrimaryDB();
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( $pageIds ) > 0 ) {
55            $pageConds['page_id'] = $pageIds;
56            $revConds['rev_page'] = $pageIds;
57            $this->output( "Limiting to page IDs " . implode( ',', $pageIds ) . "\n" );
58        }
59
60        # Get "active" revisions from the page table
61        $this->output( "Searching for active revisions..." );
62        $res = $dbw->newSelectQueryBuilder()
63            ->select( 'page_latest' )
64            ->from( 'page' )
65            ->where( $pageConds )
66            ->caller( __METHOD__ )
67            ->fetchResultSet();
68        $latestRevs = [];
69        foreach ( $res as $row ) {
70            $latestRevs[] = $row->page_latest;
71        }
72        $this->output( "done.\n" );
73
74        # Get all revisions that aren't in this set
75        $this->output( "Searching for inactive revisions..." );
76        if ( count( $latestRevs ) > 0 ) {
77            $revConds[] = $dbw->expr( 'rev_id', '!=', $latestRevs );
78        }
79        $res = $dbw->newSelectQueryBuilder()
80            ->select( 'rev_id' )
81            ->from( 'revision' )
82            ->where( $revConds )
83            ->caller( __METHOD__ )
84            ->fetchResultSet();
85        $oldRevs = [];
86        foreach ( $res as $row ) {
87            $oldRevs[] = $row->rev_id;
88        }
89        $this->output( "done.\n" );
90
91        # Inform the user of what we're going to do
92        $count = count( $oldRevs );
93        $this->output( "$count old revisions found.\n" );
94
95        # Delete as appropriate
96        if ( $delete && $count ) {
97            $this->output( "Deleting..." );
98            $dbw->newDeleteQueryBuilder()
99                ->deleteFrom( 'revision' )
100                ->where( [ 'rev_id' => $oldRevs ] )
101                ->caller( __METHOD__ )->execute();
102            $dbw->newDeleteQueryBuilder()
103                ->deleteFrom( 'ip_changes' )
104                ->where( [ 'ipc_rev_id' => $oldRevs ] )
105                ->caller( __METHOD__ )->execute();
106            $this->output( "done.\n" );
107        }
108
109        # Purge redundant text records
110        $this->commitTransaction( $dbw, __METHOD__ );
111        if ( $delete ) {
112            $this->purgeRedundantText( true );
113        }
114    }
115}
116
117$maintClass = DeleteOldRevisions::class;
118require_once RUN_MAINTENANCE_IF_MAIN;