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