Apply jdanni's patch from from 2+ years ago for Bug 15218 - LinkSearch results should...
[lhc/web/wiklou.git] / maintenance / deleteOrphanedRevisions.php
1 <?php
2
3 /**
4 * Maintenance script to delete revisions which refer to a nonexisting page
5 * Sometimes manual deletion done in a rush leaves crap in the database
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @ingroup Maintenance
23 * @author Rob Church <robchur@gmail.com>
24 * @todo More efficient cleanup of text records
25 */
26
27 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
28
29 class DeleteOrphanedRevisions extends Maintenance {
30 public function __construct() {
31 parent::__construct();
32 $this->mDescription = "Maintenance script to delete revisions which refer to a nonexisting page";
33 $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
34 }
35
36 public function execute() {
37 $this->output( "Delete Orphaned Revisions\n" );
38
39 $report = $this->hasOption( 'report' );
40
41 $dbw = wfGetDB( DB_MASTER );
42 $dbw->begin();
43 list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' );
44
45 # Find all the orphaned revisions
46 $this->output( "Checking for orphaned revisions..." );
47 $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id WHERE page_namespace IS NULL";
48 $res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
49
50 # Stash 'em all up for deletion (if needed)
51 $revisions = array();
52 foreach ( $res as $row )
53 $revisions[] = $row->rev_id;
54 $count = count( $revisions );
55 $this->output( "found {$count}.\n" );
56
57 # Nothing to do?
58 if ( $report || $count == 0 ) {
59 $dbw->commit();
60 exit( 0 );
61 }
62
63 # Delete each revision
64 $this->output( "Deleting..." );
65 $this->deleteRevs( $revisions, $dbw );
66 $this->output( "done.\n" );
67
68 # Close the transaction and call the script to purge unused text records
69 $dbw->commit();
70 $this->purgeRedundantText( true );
71 }
72
73 /**
74 * Delete one or more revisions from the database
75 * Do this inside a transaction
76 *
77 * @param $id Array of revision id values
78 * @param $dbw DatabaseBase class (needs to be a master)
79 */
80 private function deleteRevs( $id, &$dbw ) {
81 if ( !is_array( $id ) )
82 $id = array( $id );
83 $dbw->delete( 'revision', array( 'rev_id' => $id ), __METHOD__ );
84 }
85 }
86
87 $maintClass = "DeleteOrphanedRevisions";
88 require_once( RUN_MAINTENANCE_IF_MAIN );
89