MediaWiki master
deleteOrphanedRevisions.php
Go to the documentation of this file.
1<?php
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
32
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription(
42 'Maintenance script to delete revisions which refer to a nonexisting page' );
43 $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
44 }
45
46 public function execute() {
47 $this->output( "Delete Orphaned Revisions\n" );
48
49 $report = $this->hasOption( 'report' );
50
51 $dbw = $this->getPrimaryDB();
52 $this->beginTransaction( $dbw, __METHOD__ );
53
54 # Find all the orphaned revisions
55 $this->output( "Checking for orphaned revisions..." );
56 $res = $dbw->newSelectQueryBuilder()
57 ->select( 'rev_id' )
58 ->from( 'revision' )
59 ->leftJoin( 'page', null, 'rev_page = page_id' )
60 ->where( [ 'page_namespace' => null ] )
61 ->caller( 'deleteOrphanedRevisions' )
62 ->fetchResultSet();
63
64 # Stash 'em all up for deletion (if needed)
65 $revisions = [];
66 foreach ( $res as $row ) {
67 $revisions[] = $row->rev_id;
68 }
69 $count = count( $revisions );
70 $this->output( "found {$count}.\n" );
71
72 # Nothing to do?
73 if ( $report || $count == 0 ) {
74 $this->commitTransaction( $dbw, __METHOD__ );
75 return;
76 }
77
78 # Delete each revision
79 $this->output( "Deleting..." );
80 $this->deleteRevs( $revisions, $dbw );
81 $this->output( "done.\n" );
82
83 # Close the transaction and call the script to purge unused text records
84 $this->commitTransaction( $dbw, __METHOD__ );
85 $this->purgeRedundantText( true );
86 }
87
95 private function deleteRevs( array $id, $dbw ) {
96 $dbw->newDeleteQueryBuilder()
97 ->deleteFrom( 'revision' )
98 ->where( [ 'rev_id' => $id ] )
99 ->caller( __METHOD__ )->execute();
100
101 // Delete from ip_changes should a record exist.
102 $dbw->newDeleteQueryBuilder()
103 ->deleteFrom( 'ip_changes' )
104 ->where( [ 'ipc_rev_id' => $id ] )
105 ->caller( __METHOD__ )->execute();
106 }
107}
108
109// @codeCoverageIgnoreStart
110$maintClass = DeleteOrphanedRevisions::class;
111require_once RUN_MAINTENANCE_IF_MAIN;
112// @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...
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.
Interface to a relational database.
Definition IDatabase.php:48