Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / maintenance / deleteOrphanedRevisions.php
blobdf496d435b27f85f73f322666e5aa14c5c86b6cb
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 require_once __DIR__ . '/Maintenance.php';
29 /**
30 * Maintenance script that deletes revisions which refer to a nonexisting page.
32 * @ingroup Maintenance
34 class DeleteOrphanedRevisions extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription(
38 'Maintenance script to delete revisions which refer to a nonexisting page' );
39 $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
42 public function execute() {
43 $this->output( "Delete Orphaned Revisions\n" );
45 $report = $this->hasOption( 'report' );
47 $dbw = $this->getDB( DB_MASTER );
48 $this->beginTransaction( $dbw, __METHOD__ );
49 list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' );
51 # Find all the orphaned revisions
52 $this->output( "Checking for orphaned revisions..." );
53 $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id "
54 . "WHERE page_namespace IS NULL";
55 $res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
57 # Stash 'em all up for deletion (if needed)
58 $revisions = [];
59 foreach ( $res as $row ) {
60 $revisions[] = $row->rev_id;
62 $count = count( $revisions );
63 $this->output( "found {$count}.\n" );
65 # Nothing to do?
66 if ( $report || $count == 0 ) {
67 $this->commitTransaction( $dbw, __METHOD__ );
68 exit( 0 );
71 # Delete each revision
72 $this->output( "Deleting..." );
73 $this->deleteRevs( $revisions, $dbw );
74 $this->output( "done.\n" );
76 # Close the transaction and call the script to purge unused text records
77 $this->commitTransaction( $dbw, __METHOD__ );
78 $this->purgeRedundantText( true );
81 /**
82 * Delete one or more revisions from the database
83 * Do this inside a transaction
85 * @param array $id Array of revision id values
86 * @param Database $dbw Database class (needs to be a master)
88 private function deleteRevs( $id, &$dbw ) {
89 if ( !is_array( $id ) ) {
90 $id = [ $id ];
92 $dbw->delete( 'revision', [ 'rev_id' => $id ], __METHOD__ );
96 $maintClass = "DeleteOrphanedRevisions";
97 require_once RUN_MAINTENANCE_IF_MAIN;