summaryrefslogtreecommitdiff
path: root/maintenance/deleteOrphanedRevisions.php
blob: f0a969280543a52b04c8a92dfe6fb61e6ab722fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
 * Delete revisions which refer to a nonexisting page.
 * Sometimes manual deletion done in a rush leaves crap in the database.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 * @author Rob Church <robchur@gmail.com>
 * @todo More efficient cleanup of text records
 */

require_once __DIR__ . '/Maintenance.php';

/**
 * Maintenance script that deletes revisions which refer to a nonexisting page.
 *
 * @ingroup Maintenance
 */
class DeleteOrphanedRevisions extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription = "Maintenance script to delete revisions which refer to a nonexisting page";
		$this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
	}

	public function execute() {
		$this->output( "Delete Orphaned Revisions\n" );

		$report = $this->hasOption( 'report' );

		$dbw = wfGetDB( DB_MASTER );
		$dbw->begin( __METHOD__ );
		list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' );

		# Find all the orphaned revisions
		$this->output( "Checking for orphaned revisions..." );
		$sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id WHERE page_namespace IS NULL";
		$res = $dbw->query( $sql, 'deleteOrphanedRevisions' );

		# Stash 'em all up for deletion (if needed)
		$revisions = array();
		foreach ( $res as $row ) {
			$revisions[] = $row->rev_id;
		}
		$count = count( $revisions );
		$this->output( "found {$count}.\n" );

		# Nothing to do?
		if ( $report || $count == 0 ) {
			$dbw->commit( __METHOD__ );
			exit( 0 );
		}

		# Delete each revision
		$this->output( "Deleting..." );
		$this->deleteRevs( $revisions, $dbw );
		$this->output( "done.\n" );

		# Close the transaction and call the script to purge unused text records
		$dbw->commit( __METHOD__ );
		$this->purgeRedundantText( true );
	}

	/**
	 * Delete one or more revisions from the database
	 * Do this inside a transaction
	 *
	 * @param $id Array of revision id values
	 * @param $dbw DatabaseBase class (needs to be a master)
	 */
	private function deleteRevs( $id, &$dbw ) {
		if ( !is_array( $id ) ) {
			$id = array( $id );
		}
		$dbw->delete( 'revision', array( 'rev_id' => $id ), __METHOD__ );
	}
}

$maintClass = "DeleteOrphanedRevisions";
require_once RUN_MAINTENANCE_IF_MAIN;