| 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 |
require_once __DIR__ . '/Maintenance.php'; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class DeleteOldRevisions extends Maintenance { |
| 35 |
public function __construct() { |
| 36 |
parent::__construct(); |
| 37 |
$this->addDescription( 'Delete old (non-current) revisions from the database' ); |
| 38 |
$this->addOption( 'delete', 'Actually perform the deletion' ); |
| 39 |
$this->addArg( 'page_id', 'List of page ids to work on', false, true ); |
| 40 |
} |
| 41 |
|
| 42 |
public function execute() { |
| 43 |
$this->output( "Delete old revisions\n\n" ); |
| 44 |
$this->doDelete( $this->hasOption( 'delete' ), $this->getArgs( 'page_id' ) ); |
| 45 |
} |
| 46 |
|
| 47 |
private function doDelete( $delete = false, $pageIds = [] ) { |
| 48 |
|
| 49 |
$dbw = $this->getPrimaryDB(); |
| 50 |
$this->beginTransaction( $dbw, __METHOD__ ); |
| 51 |
|
| 52 |
$pageConds = []; |
| 53 |
$revConds = []; |
| 54 |
|
| 55 |
|
| 56 |
if ( count( $pageIds ) > 0 ) { |
| 57 |
$pageConds['page_id'] = $pageIds; |
| 58 |
$revConds['rev_page'] = $pageIds; |
| 59 |
$this->output( "Limiting to page IDs " . implode( ',', $pageIds ) . "\n" ); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
$this->output( "Searching for active revisions..." ); |
| 64 |
$res = $dbw->newSelectQueryBuilder() |
| 65 |
->select( 'page_latest' ) |
| 66 |
->from( 'page' ) |
| 67 |
->where( $pageConds ) |
| 68 |
->caller( __METHOD__ ) |
| 69 |
->fetchResultSet(); |
| 70 |
$latestRevs = []; |
| 71 |
foreach ( $res as $row ) { |
| 72 |
$latestRevs[] = $row->page_latest; |
| 73 |
} |
| 74 |
$this->output( "done.\n" ); |
| 75 |
|
| 76 |
|
| 77 |
$this->output( "Searching for inactive revisions..." ); |
| 78 |
if ( count( $latestRevs ) > 0 ) { |
| 79 |
$revConds[] = $dbw->expr( 'rev_id', '!=', $latestRevs ); |
| 80 |
} |
| 81 |
$res = $dbw->newSelectQueryBuilder() |
| 82 |
->select( 'rev_id' ) |
| 83 |
->from( 'revision' ) |
| 84 |
->where( $revConds ) |
| 85 |
->caller( __METHOD__ ) |
| 86 |
->fetchResultSet(); |
| 87 |
$oldRevs = []; |
| 88 |
foreach ( $res as $row ) { |
| 89 |
$oldRevs[] = $row->rev_id; |
| 90 |
} |
| 91 |
$this->output( "done.\n" ); |
| 92 |
|
| 93 |
|
| 94 |
$count = count( $oldRevs ); |
| 95 |
$this->output( "$count old revisions found.\n" ); |
| 96 |
|
| 97 |
|
| 98 |
if ( $delete && $count ) { |
| 99 |
$this->output( "Deleting..." ); |
| 100 |
$dbw->newDeleteQueryBuilder() |
| 101 |
->deleteFrom( 'revision' ) |
| 102 |
->where( [ 'rev_id' => $oldRevs ] ) |
| 103 |
->caller( __METHOD__ )->execute(); |
| 104 |
$dbw->newDeleteQueryBuilder() |
| 105 |
->deleteFrom( 'ip_changes' ) |
| 106 |
->where( [ 'ipc_rev_id' => $oldRevs ] ) |
| 107 |
->caller( __METHOD__ )->execute(); |
| 108 |
$this->output( "done.\n" ); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
$this->commitTransaction( $dbw, __METHOD__ ); |
| 113 |
if ( $delete ) { |
| 114 |
$this->purgeRedundantText( true ); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
$maintClass = DeleteOldRevisions::class; |
| 121 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 122 |
|