Debian Codegraph @

maintenance/deleteOrphanedRevisions.php

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