| 1 |
<?php |
| 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 |
require_once __DIR__ . '/Maintenance.php'; |
| 29 |
|
| 30 |
|
| 31 |
use Wikimedia\Rdbms\IDatabase; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class DeleteOrphanedRevisions extends Maintenance { |
| 39 |
public function __construct() { |
| 40 |
parent::__construct(); |
| 41 |
$this->addDescription( |
| 42 |
'Maintenance script to delete revisions which refer to a nonexisting page' ); |
| 43 |
$this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' ); |
| 44 |
} |
| 45 |
|
| 46 |
public function execute() { |
| 47 |
$this->output( "Delete Orphaned Revisions\n" ); |
| 48 |
|
| 49 |
$report = $this->hasOption( 'report' ); |
| 50 |
|
| 51 |
$dbw = $this->getPrimaryDB(); |
| 52 |
$this->beginTransaction( $dbw, __METHOD__ ); |
| 53 |
|
| 54 |
|
| 55 |
$this->output( "Checking for orphaned revisions..." ); |
| 56 |
$res = $dbw->newSelectQueryBuilder() |
| 57 |
->select( 'rev_id' ) |
| 58 |
->from( 'revision' ) |
| 59 |
->leftJoin( 'page', null, 'rev_page = page_id' ) |
| 60 |
->where( [ 'page_namespace' => null ] ) |
| 61 |
->caller( 'deleteOrphanedRevisions' ) |
| 62 |
->fetchResultSet(); |
| 63 |
|
| 64 |
|
| 65 |
$revisions = []; |
| 66 |
foreach ( $res as $row ) { |
| 67 |
$revisions[] = $row->rev_id; |
| 68 |
} |
| 69 |
$count = count( $revisions ); |
| 70 |
$this->output( "found {$count}.\n" ); |
| 71 |
|
| 72 |
|
| 73 |
if ( $report || $count == 0 ) { |
| 74 |
$this->commitTransaction( $dbw, __METHOD__ ); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
$this->output( "Deleting..." ); |
| 80 |
$this->deleteRevs( $revisions, $dbw ); |
| 81 |
$this->output( "done.\n" ); |
| 82 |
|
| 83 |
|
| 84 |
$this->commitTransaction( $dbw, __METHOD__ ); |
| 85 |
$this->purgeRedundantText( true ); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private function deleteRevs( array $id, $dbw ) { |
| 96 |
$dbw->newDeleteQueryBuilder() |
| 97 |
->deleteFrom( 'revision' ) |
| 98 |
->where( [ 'rev_id' => $id ] ) |
| 99 |
->caller( __METHOD__ )->execute(); |
| 100 |
|
| 101 |
|
| 102 |
$dbw->newDeleteQueryBuilder() |
| 103 |
->deleteFrom( 'ip_changes' ) |
| 104 |
->where( [ 'ipc_rev_id' => $id ] ) |
| 105 |
->caller( __METHOD__ )->execute(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
$maintClass = DeleteOrphanedRevisions::class; |
| 111 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 112 |
|