MediaWiki REL1_35
deleteOrphanedRevisions.php
Go to the documentation of this file.
1<?php
27require_once __DIR__ . '/Maintenance.php';
28
30
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription(
40 'Maintenance script to delete revisions which refer to a nonexisting page' );
41 $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
42 }
43
44 public function execute() {
45 $this->output( "Delete Orphaned Revisions\n" );
46
47 $report = $this->hasOption( 'report' );
48
49 $dbw = $this->getDB( DB_MASTER );
50 $this->beginTransaction( $dbw, __METHOD__ );
51 list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' );
52
53 # Find all the orphaned revisions
54 $this->output( "Checking for orphaned revisions..." );
55 $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id "
56 . "WHERE page_namespace IS NULL";
57 $res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
58
59 # Stash 'em all up for deletion (if needed)
60 $revisions = [];
61 foreach ( $res as $row ) {
62 $revisions[] = $row->rev_id;
63 }
64 $count = count( $revisions );
65 $this->output( "found {$count}.\n" );
66
67 # Nothing to do?
68 if ( $report || $count == 0 ) {
69 $this->commitTransaction( $dbw, __METHOD__ );
70 exit( 0 );
71 }
72
73 # Delete each revision
74 $this->output( "Deleting..." );
75 $this->deleteRevs( $revisions, $dbw );
76 $this->output( "done.\n" );
77
78 # Close the transaction and call the script to purge unused text records
79 $this->commitTransaction( $dbw, __METHOD__ );
80 $this->purgeRedundantText( true );
81 }
82
90 private function deleteRevs( array $id, &$dbw ) {
91 $dbw->delete( 'revision', [ 'rev_id' => $id ], __METHOD__ );
92
93 // Delete from ip_changes should a record exist.
94 $dbw->delete( 'ip_changes', [ 'ipc_rev_id' => $id ], __METHOD__ );
95 }
96}
97
98$maintClass = DeleteOrphanedRevisions::class;
99require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const RUN_MAINTENANCE_IF_MAIN
Maintenance script that deletes revisions which refer to a nonexisting page.
deleteRevs(array $id, &$dbw)
Delete one or more revisions from the database Do this inside a transaction.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
purgeRedundantText( $delete=true)
Support function for cleaning up redundant text records.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
const DB_MASTER
Definition defines.php:29