MediaWiki REL1_39
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_PRIMARY );
50 $this->beginTransaction( $dbw, __METHOD__ );
51
52 # Find all the orphaned revisions
53 $this->output( "Checking for orphaned revisions..." );
54 $res = $dbw->newSelectQueryBuilder()
55 ->select( 'rev_id' )
56 ->from( 'revision' )
57 ->leftJoin( 'page', null, 'rev_page = page_id' )
58 ->where( [ 'page_namespace' => null ] )
59 ->caller( 'deleteOrphanedRevisions' )
60 ->fetchResultSet();
61
62 # Stash 'em all up for deletion (if needed)
63 $revisions = [];
64 foreach ( $res as $row ) {
65 $revisions[] = $row->rev_id;
66 }
67 $count = count( $revisions );
68 $this->output( "found {$count}.\n" );
69
70 # Nothing to do?
71 if ( $report || $count == 0 ) {
72 $this->commitTransaction( $dbw, __METHOD__ );
73 return;
74 }
75
76 # Delete each revision
77 $this->output( "Deleting..." );
78 $this->deleteRevs( $revisions, $dbw );
79 $this->output( "done.\n" );
80
81 # Close the transaction and call the script to purge unused text records
82 $this->commitTransaction( $dbw, __METHOD__ );
83 $this->purgeRedundantText( true );
84 }
85
93 private function deleteRevs( array $id, $dbw ) {
94 $dbw->delete( 'revision', [ 'rev_id' => $id ], __METHOD__ );
95
96 // Delete from ip_changes should a record exist.
97 $dbw->delete( 'ip_changes', [ 'ipc_rev_id' => $id ], __METHOD__ );
98 }
99}
100
101$maintClass = DeleteOrphanedRevisions::class;
102require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
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...
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction 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:39
const DB_PRIMARY
Definition defines.php:28