* @author Kunal Mehta */ require_once __DIR__ . '/Maintenance.php'; use Wikimedia\Rdbms\IDatabase; /** * Maintenance script that deletes revisions which refer to a nonexisting page. * * @ingroup Maintenance */ class PurgeOrphanedLinks extends Maintenance { public function __construct() { parent::__construct(); $this->addDescription( 'Maintenance script to delete links entries which refer to a nonexisting page' ); $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' ); $this->setBatchSize( 500 ); } public function execute() { $this->output( "Delete Orphaned Revisions\n" ); $report = $this->hasOption( 'report' ); $dbw = $this->getDB( DB_PRIMARY ); $this->beginTransaction( $dbw, __METHOD__ ); do { list( $page, $links ) = $dbw->tableNamesN( 'page', 'categorylinks' ); $sql = "SELECT cl_from FROM {$links} LEFT JOIN {$page} ON cl_from = page_id " . "WHERE page_id IS NULL LIMIT {$this->mBatchSize}"; # Find all the orphaned links $res = $dbw->query( $sql, 'purgeOrphanedLinks' ); # Stash 'em all up for deletion (if needed) $toDelete = []; foreach ( $res as $row ) { $toDelete[] = $row->cl_from; } if ( !$toDelete ) { break; } $count = count( $toDelete ); $this->output( "deleting {$count}..." ); $dbw->delete( 'categorylinks', [ 'cl_from' => $id ], __METHOD__ ); $this->output( "done. Sleeping\n" ); } while ( $dbw->affectedRows() ); $this->output("Done done!\n" ); } } $maintClass = PurgeOrphanedLinks::class; require_once RUN_MAINTENANCE_IF_MAIN;