Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / maintenance / deleteOrphanedRevisions.php
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 <robchur@gmail.com>
24 * @todo More efficient cleanup of text records
25 */
26
27 require_once __DIR__ . '/Maintenance.php';
28
29 use Wikimedia\Rdbms\IDatabase;
30
31 /**
32 * Maintenance script that deletes revisions which refer to a nonexisting page.
33 *
34 * @ingroup Maintenance
35 */
36 class DeleteOrphanedRevisions extends Maintenance {
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription(
40 'Maintenance script to delete revisions which refer to a nonexisting page' );
41 $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
42 }
43
44 public function execute() {
45 $this->output( "Delete Orphaned Revisions\n" );
46
47 $report = $this->hasOption( 'report' );
48
49 $dbw = $this->getDB( DB_MASTER );
50 $this->beginTransaction( $dbw, __METHOD__ );
51 list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' );
52
53 # Find all the orphaned revisions
54 $this->output( "Checking for orphaned revisions..." );
55 $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id "
56 . "WHERE page_namespace IS NULL";
57 $res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
58
59 # Stash 'em all up for deletion (if needed)
60 $revisions = [];
61 foreach ( $res as $row ) {
62 $revisions[] = $row->rev_id;
63 }
64 $count = count( $revisions );
65 $this->output( "found {$count}.\n" );
66
67 # Nothing to do?
68 if ( $report || $count == 0 ) {
69 $this->commitTransaction( $dbw, __METHOD__ );
70 exit( 0 );
71 }
72
73 # Delete each revision
74 $this->output( "Deleting..." );
75 $this->deleteRevs( $revisions, $dbw );
76 $this->output( "done.\n" );
77
78 # Close the transaction and call the script to purge unused text records
79 $this->commitTransaction( $dbw, __METHOD__ );
80 $this->purgeRedundantText( true );
81 }
82
83 /**
84 * Delete one or more revisions from the database
85 * Do this inside a transaction
86 *
87 * @param array $id Array of revision id values
88 * @param IDatabase $dbw Master DB handle
89 */
90 private function deleteRevs( $id, &$dbw ) {
91 if ( !is_array( $id ) ) {
92 $id = [ $id ];
93 }
94 $dbw->delete( 'revision', [ 'rev_id' => $id ], __METHOD__ );
95
96 // Delete from ip_changes should a record exist.
97 $dbw->delete( 'ip_changes', [ 'ipc_rev_id' => $id ], __METHOD__ );
98 }
99 }
100
101 $maintClass = DeleteOrphanedRevisions::class;
102 require_once RUN_MAINTENANCE_IF_MAIN;