MediaWiki master
deleteOrphanedRevisions.php
Go to the documentation of this file.
1<?php
13// @codeCoverageIgnoreStart
14require_once __DIR__ . '/Maintenance.php';
15// @codeCoverageIgnoreEnd
16
19
26 public function __construct() {
27 parent::__construct();
28 $this->addDescription(
29 'Maintenance script to delete revisions which refer to a nonexisting page' );
30 $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
31 }
32
33 public function execute() {
34 $this->output( "Delete Orphaned Revisions\n" );
35
36 $report = $this->hasOption( 'report' );
37
38 $dbw = $this->getPrimaryDB();
39 $this->beginTransactionRound( __METHOD__ );
40
41 # Find all the orphaned revisions
42 $this->output( "Checking for orphaned revisions..." );
43 $revisions = $dbw->newSelectQueryBuilder()
44 ->select( 'rev_id' )
45 ->from( 'revision' )
46 ->leftJoin( 'page', null, 'rev_page = page_id' )
47 ->where( [ 'page_namespace' => null ] )
48 ->caller( 'deleteOrphanedRevisions' )
49 ->fetchFieldValues();
50
51 # Stash 'em all up for deletion (if needed)
52 $count = count( $revisions );
53 $this->output( "found {$count}.\n" );
54
55 # Nothing to do?
56 if ( $report || $count === 0 ) {
57 $this->commitTransactionRound( __METHOD__ );
58 return;
59 }
60
61 # Delete each revision
62 $this->output( "Deleting..." );
63 $this->deleteRevs( $revisions, $dbw );
64 $this->output( "done.\n" );
65
66 # Close the transaction and call the script to purge unused text records
67 $this->commitTransactionRound( __METHOD__ );
68 $this->purgeRedundantText( true );
69 }
70
78 private function deleteRevs( array $id, IDatabase $dbw ) {
80 ->deleteFrom( 'revision' )
81 ->where( [ 'rev_id' => $id ] )
82 ->caller( __METHOD__ )->execute();
83
84 // Delete from ip_changes should a record exist.
86 ->deleteFrom( 'ip_changes' )
87 ->where( [ 'ipc_rev_id' => $id ] )
88 ->caller( __METHOD__ )->execute();
89 }
90}
91
92// @codeCoverageIgnoreStart
93$maintClass = DeleteOrphanedRevisions::class;
94require_once RUN_MAINTENANCE_IF_MAIN;
95// @codeCoverageIgnoreEnd
Maintenance script that deletes revisions which refer to a nonexisting page.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
Interface to a relational database.
Definition IDatabase.php:31
newDeleteQueryBuilder()
Get an DeleteQueryBuilder bound to this connection.
newSelectQueryBuilder()
Create an empty SelectQueryBuilder which can be used to run queries against this connection.