Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
37 / 37 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
DeleteOrphanedRevisions | |
100.00% |
37 / 37 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
4 | |||
deleteRevs | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 |
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 | // @codeCoverageIgnoreStart |
28 | require_once __DIR__ . '/Maintenance.php'; |
29 | // @codeCoverageIgnoreEnd |
30 | |
31 | use MediaWiki\Maintenance\Maintenance; |
32 | use Wikimedia\Rdbms\IDatabase; |
33 | |
34 | /** |
35 | * Maintenance script that deletes revisions which refer to a nonexisting page. |
36 | * |
37 | * @ingroup Maintenance |
38 | */ |
39 | class DeleteOrphanedRevisions extends Maintenance { |
40 | public function __construct() { |
41 | parent::__construct(); |
42 | $this->addDescription( |
43 | 'Maintenance script to delete revisions which refer to a nonexisting page' ); |
44 | $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' ); |
45 | } |
46 | |
47 | public function execute() { |
48 | $this->output( "Delete Orphaned Revisions\n" ); |
49 | |
50 | $report = $this->hasOption( 'report' ); |
51 | |
52 | $dbw = $this->getPrimaryDB(); |
53 | $this->beginTransaction( $dbw, __METHOD__ ); |
54 | |
55 | # Find all the orphaned revisions |
56 | $this->output( "Checking for orphaned revisions..." ); |
57 | $res = $dbw->newSelectQueryBuilder() |
58 | ->select( 'rev_id' ) |
59 | ->from( 'revision' ) |
60 | ->leftJoin( 'page', null, 'rev_page = page_id' ) |
61 | ->where( [ 'page_namespace' => null ] ) |
62 | ->caller( 'deleteOrphanedRevisions' ) |
63 | ->fetchResultSet(); |
64 | |
65 | # Stash 'em all up for deletion (if needed) |
66 | $revisions = []; |
67 | foreach ( $res as $row ) { |
68 | $revisions[] = $row->rev_id; |
69 | } |
70 | $count = count( $revisions ); |
71 | $this->output( "found {$count}.\n" ); |
72 | |
73 | # Nothing to do? |
74 | if ( $report || $count == 0 ) { |
75 | $this->commitTransaction( $dbw, __METHOD__ ); |
76 | return; |
77 | } |
78 | |
79 | # Delete each revision |
80 | $this->output( "Deleting..." ); |
81 | $this->deleteRevs( $revisions, $dbw ); |
82 | $this->output( "done.\n" ); |
83 | |
84 | # Close the transaction and call the script to purge unused text records |
85 | $this->commitTransaction( $dbw, __METHOD__ ); |
86 | $this->purgeRedundantText( true ); |
87 | } |
88 | |
89 | /** |
90 | * Delete one or more revisions from the database |
91 | * Do this inside a transaction |
92 | * |
93 | * @param int[] $id Array of revision id values |
94 | * @param IDatabase $dbw Primary DB handle |
95 | */ |
96 | private function deleteRevs( array $id, $dbw ) { |
97 | $dbw->newDeleteQueryBuilder() |
98 | ->deleteFrom( 'revision' ) |
99 | ->where( [ 'rev_id' => $id ] ) |
100 | ->caller( __METHOD__ )->execute(); |
101 | |
102 | // Delete from ip_changes should a record exist. |
103 | $dbw->newDeleteQueryBuilder() |
104 | ->deleteFrom( 'ip_changes' ) |
105 | ->where( [ 'ipc_rev_id' => $id ] ) |
106 | ->caller( __METHOD__ )->execute(); |
107 | } |
108 | } |
109 | |
110 | // @codeCoverageIgnoreStart |
111 | $maintClass = DeleteOrphanedRevisions::class; |
112 | require_once RUN_MAINTENANCE_IF_MAIN; |
113 | // @codeCoverageIgnoreEnd |