Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteOrphanedRevisions
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
42
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 / 25
0.00% covered (danger)
0.00%
0 / 1
20
 deleteRevs
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Delete revisions which refer to a nonexisting page.
4 * Sometimes manual deletion done in a rush leaves crap in 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 * @file
22 * @ingroup Maintenance
23 * @author Rob Church <robchur@gmail.com>
24 * @todo More efficient cleanup of text records
25 */
26
27require_once __DIR__ . '/Maintenance.php';
28
29use Wikimedia\Rdbms\IDatabase;
30
31/**
32 * Maintenance script that deletes revisions which refer to a nonexisting page.
33 *
34 * @ingroup Maintenance
35 */
36class DeleteOrphanedRevisions extends Maintenance {
37    public function __construct() {
38        parent::__construct();
39        $this->addDescription(
40            'Maintenance script to delete revisions which refer to a nonexisting page' );
41        $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
42    }
43
44    public function execute() {
45        $this->output( "Delete Orphaned Revisions\n" );
46
47        $report = $this->hasOption( 'report' );
48
49        $dbw = $this->getPrimaryDB();
50        $this->beginTransaction( $dbw, __METHOD__ );
51
52        # Find all the orphaned revisions
53        $this->output( "Checking for orphaned revisions..." );
54        $res = $dbw->newSelectQueryBuilder()
55            ->select( 'rev_id' )
56            ->from( 'revision' )
57            ->leftJoin( 'page', null, 'rev_page = page_id' )
58            ->where( [ 'page_namespace' => null ] )
59            ->caller( 'deleteOrphanedRevisions' )
60            ->fetchResultSet();
61
62        # Stash 'em all up for deletion (if needed)
63        $revisions = [];
64        foreach ( $res as $row ) {
65            $revisions[] = $row->rev_id;
66        }
67        $count = count( $revisions );
68        $this->output( "found {$count}.\n" );
69
70        # Nothing to do?
71        if ( $report || $count == 0 ) {
72            $this->commitTransaction( $dbw, __METHOD__ );
73            return;
74        }
75
76        # Delete each revision
77        $this->output( "Deleting..." );
78        $this->deleteRevs( $revisions, $dbw );
79        $this->output( "done.\n" );
80
81        # Close the transaction and call the script to purge unused text records
82        $this->commitTransaction( $dbw, __METHOD__ );
83        $this->purgeRedundantText( true );
84    }
85
86    /**
87     * Delete one or more revisions from the database
88     * Do this inside a transaction
89     *
90     * @param int[] $id Array of revision id values
91     * @param IDatabase $dbw Primary DB handle
92     */
93    private function deleteRevs( array $id, $dbw ) {
94        $dbw->newDeleteQueryBuilder()
95            ->deleteFrom( 'revision' )
96            ->where( [ 'rev_id' => $id ] )
97            ->caller( __METHOD__ )->execute();
98
99        // Delete from ip_changes should a record exist.
100        $dbw->newDeleteQueryBuilder()
101            ->deleteFrom( 'ip_changes' )
102            ->where( [ 'ipc_rev_id' => $id ] )
103            ->caller( __METHOD__ )->execute();
104    }
105}
106
107$maintClass = DeleteOrphanedRevisions::class;
108require_once RUN_MAINTENANCE_IF_MAIN;