X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/maintenance/deleteOrphanedRevisions.php diff --git a/maintenance/deleteOrphanedRevisions.php b/maintenance/deleteOrphanedRevisions.php index e972d1fa..4d600706 100644 --- a/maintenance/deleteOrphanedRevisions.php +++ b/maintenance/deleteOrphanedRevisions.php @@ -1,8 +1,7 @@ * @todo More efficient cleanup of text records */ -require_once( dirname( __FILE__ ) . '/Maintenance.php' ); +require_once __DIR__ . '/Maintenance.php'; + +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->mDescription = "Maintenance script to delete revisions which refer to a nonexisting page"; + $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' ); } @@ -38,25 +46,27 @@ class DeleteOrphanedRevisions extends Maintenance { $report = $this->hasOption( 'report' ); - $dbw = wfGetDB( DB_MASTER ); - $dbw->begin(); + $dbw = $this->getDB( DB_MASTER ); + $this->beginTransaction( $dbw, __METHOD__ ); list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' ); # Find all the orphaned revisions $this->output( "Checking for orphaned revisions..." ); - $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id WHERE page_namespace IS NULL"; + $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id " + . "WHERE page_namespace IS NULL"; $res = $dbw->query( $sql, 'deleteOrphanedRevisions' ); # Stash 'em all up for deletion (if needed) - $revisions = array(); - foreach ( $res as $row ) + $revisions = []; + foreach ( $res as $row ) { $revisions[] = $row->rev_id; + } $count = count( $revisions ); $this->output( "found {$count}.\n" ); # Nothing to do? if ( $report || $count == 0 ) { - $dbw->commit(); + $this->commitTransaction( $dbw, __METHOD__ ); exit( 0 ); } @@ -66,7 +76,7 @@ class DeleteOrphanedRevisions extends Maintenance { $this->output( "done.\n" ); # Close the transaction and call the script to purge unused text records - $dbw->commit(); + $this->commitTransaction( $dbw, __METHOD__ ); $this->purgeRedundantText( true ); } @@ -74,16 +84,19 @@ class DeleteOrphanedRevisions extends Maintenance { * Delete one or more revisions from the database * Do this inside a transaction * - * @param $id Array of revision id values - * @param $dbw Database class (needs to be a master) + * @param array $id Array of revision id values + * @param IDatabase $dbw Master DB handle */ private function deleteRevs( $id, &$dbw ) { - if ( !is_array( $id ) ) - $id = array( $id ); - $dbw->delete( 'revision', array( 'rev_id' => $id ), __METHOD__ ); + if ( !is_array( $id ) ) { + $id = [ $id ]; + } + $dbw->delete( 'revision', [ 'rev_id' => $id ], __METHOD__ ); + + // Delete from ip_changes should a record exist. + $dbw->delete( 'ip_changes', [ 'ipc_rev_id' => $id ], __METHOD__ ); } } $maintClass = "DeleteOrphanedRevisions"; -require_once( RUN_MAINTENANCE_IF_MAIN ); - +require_once RUN_MAINTENANCE_IF_MAIN;