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