MediaWiki master
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->getPrimaryDB();
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->newDeleteQueryBuilder()
95 ->deleteFrom( 'revision' )
96 ->where( [ 'rev_id' => $id ] )
97 ->caller( __METHOD__ )->execute();
98
99 // Delete from ip_changes should a record exist.
100 $dbw->newDeleteQueryBuilder()
101 ->deleteFrom( 'ip_changes' )
102 ->where( [ 'ipc_rev_id' => $id ] )
103 ->caller( __METHOD__ )->execute();
104 }
105}
106
107$maintClass = DeleteOrphanedRevisions::class;
108require_once RUN_MAINTENANCE_IF_MAIN;
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:36