|
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 <[email protected]> |
|
24
|
|
|
* @todo More efficient cleanup of text records |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
require_once __DIR__ . '/Maintenance.php'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Maintenance script that deletes revisions which refer to a nonexisting page. |
|
31
|
|
|
* |
|
32
|
|
|
* @ingroup Maintenance |
|
33
|
|
|
*/ |
|
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' ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function execute() { |
|
43
|
|
|
$this->output( "Delete Orphaned Revisions\n" ); |
|
44
|
|
|
|
|
45
|
|
|
$report = $this->hasOption( 'report' ); |
|
46
|
|
|
|
|
47
|
|
|
$dbw = $this->getDB( DB_MASTER ); |
|
48
|
|
|
$this->beginTransaction( $dbw, __METHOD__ ); |
|
49
|
|
|
list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' ); |
|
50
|
|
|
|
|
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' ); |
|
56
|
|
|
|
|
57
|
|
|
# Stash 'em all up for deletion (if needed) |
|
58
|
|
|
$revisions = []; |
|
59
|
|
|
foreach ( $res as $row ) { |
|
60
|
|
|
$revisions[] = $row->rev_id; |
|
61
|
|
|
} |
|
62
|
|
|
$count = count( $revisions ); |
|
63
|
|
|
$this->output( "found {$count}.\n" ); |
|
64
|
|
|
|
|
65
|
|
|
# Nothing to do? |
|
66
|
|
|
if ( $report || $count == 0 ) { |
|
67
|
|
|
$this->commitTransaction( $dbw, __METHOD__ ); |
|
68
|
|
|
exit( 0 ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
# Delete each revision |
|
72
|
|
|
$this->output( "Deleting..." ); |
|
73
|
|
|
$this->deleteRevs( $revisions, $dbw ); |
|
|
|
|
|
|
74
|
|
|
$this->output( "done.\n" ); |
|
75
|
|
|
|
|
76
|
|
|
# Close the transaction and call the script to purge unused text records |
|
77
|
|
|
$this->commitTransaction( $dbw, __METHOD__ ); |
|
78
|
|
|
$this->purgeRedundantText( true ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Delete one or more revisions from the database |
|
83
|
|
|
* Do this inside a transaction |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $id Array of revision id values |
|
86
|
|
|
* @param Database $dbw Database class (needs to be a master) |
|
87
|
|
|
*/ |
|
88
|
|
|
private function deleteRevs( $id, &$dbw ) { |
|
89
|
|
|
if ( !is_array( $id ) ) { |
|
90
|
|
|
$id = [ $id ]; |
|
91
|
|
|
} |
|
92
|
|
|
$dbw->delete( 'revision', [ 'rev_id' => $id ], __METHOD__ ); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$maintClass = "DeleteOrphanedRevisions"; |
|
97
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN; |
|
98
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: