Why: * Maintenance scripts in core have bolierplate code that is added before and after the class to allow directly running the maintenance script. * Running the maintenance script directly has been deprecated since 1.40, so this boilerplate code is only to support a now deprecated method of running maintenance scripts. * This code cannot also be marked as covered, due to PHPUnit not recognising code coverage for files. * Therefore, it is best to ignore this boilerplate code in code coverage reports as it cannot be marked as covered and also is for deprecated code. What: * Wrap the boilerplate code (requiring Maintenance.php and then later defining the maintenance script class and running if the maintenance script was called directly) with @codeCoverageIgnore comments. * Some files use a different boilerplate code, however, these should also be marked as ignored for coverage for the same reason that coverage is not properly reported for files. Bug: T371167 Change-Id: I32f5c6362dfb354149a48ce9c28da9a7fc494f7c
112 lines
3.4 KiB
PHP
112 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* Delete revisions which refer to a nonexisting page.
|
|
* Sometimes manual deletion done in a rush leaves crap in the database.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
*
|
|
* @file
|
|
* @ingroup Maintenance
|
|
* @author Rob Church <robchur@gmail.com>
|
|
* @todo More efficient cleanup of text records
|
|
*/
|
|
|
|
// @codeCoverageIgnoreStart
|
|
require_once __DIR__ . '/Maintenance.php';
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
/**
|
|
* Maintenance script that deletes revisions which refer to a nonexisting page.
|
|
*
|
|
* @ingroup Maintenance
|
|
*/
|
|
class DeleteOrphanedRevisions extends Maintenance {
|
|
public function __construct() {
|
|
parent::__construct();
|
|
$this->addDescription(
|
|
'Maintenance script to delete revisions which refer to a nonexisting page' );
|
|
$this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
|
|
}
|
|
|
|
public function execute() {
|
|
$this->output( "Delete Orphaned Revisions\n" );
|
|
|
|
$report = $this->hasOption( 'report' );
|
|
|
|
$dbw = $this->getPrimaryDB();
|
|
$this->beginTransaction( $dbw, __METHOD__ );
|
|
|
|
# Find all the orphaned revisions
|
|
$this->output( "Checking for orphaned revisions..." );
|
|
$res = $dbw->newSelectQueryBuilder()
|
|
->select( 'rev_id' )
|
|
->from( 'revision' )
|
|
->leftJoin( 'page', null, 'rev_page = page_id' )
|
|
->where( [ 'page_namespace' => null ] )
|
|
->caller( 'deleteOrphanedRevisions' )
|
|
->fetchResultSet();
|
|
|
|
# Stash 'em all up for deletion (if needed)
|
|
$revisions = [];
|
|
foreach ( $res as $row ) {
|
|
$revisions[] = $row->rev_id;
|
|
}
|
|
$count = count( $revisions );
|
|
$this->output( "found {$count}.\n" );
|
|
|
|
# Nothing to do?
|
|
if ( $report || $count == 0 ) {
|
|
$this->commitTransaction( $dbw, __METHOD__ );
|
|
return;
|
|
}
|
|
|
|
# Delete each revision
|
|
$this->output( "Deleting..." );
|
|
$this->deleteRevs( $revisions, $dbw );
|
|
$this->output( "done.\n" );
|
|
|
|
# Close the transaction and call the script to purge unused text records
|
|
$this->commitTransaction( $dbw, __METHOD__ );
|
|
$this->purgeRedundantText( true );
|
|
}
|
|
|
|
/**
|
|
* Delete one or more revisions from the database
|
|
* Do this inside a transaction
|
|
*
|
|
* @param int[] $id Array of revision id values
|
|
* @param IDatabase $dbw Primary DB handle
|
|
*/
|
|
private function deleteRevs( array $id, $dbw ) {
|
|
$dbw->newDeleteQueryBuilder()
|
|
->deleteFrom( 'revision' )
|
|
->where( [ 'rev_id' => $id ] )
|
|
->caller( __METHOD__ )->execute();
|
|
|
|
// Delete from ip_changes should a record exist.
|
|
$dbw->newDeleteQueryBuilder()
|
|
->deleteFrom( 'ip_changes' )
|
|
->where( [ 'ipc_rev_id' => $id ] )
|
|
->caller( __METHOD__ )->execute();
|
|
}
|
|
}
|
|
|
|
// @codeCoverageIgnoreStart
|
|
$maintClass = DeleteOrphanedRevisions::class;
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
|
// @codeCoverageIgnoreEnd
|