MediaWiki  REL1_22
deleteOrphanedRevisions.php
Go to the documentation of this file.
00001 <?php
00027 require_once __DIR__ . '/Maintenance.php';
00028 
00034 class DeleteOrphanedRevisions extends Maintenance {
00035     public function __construct() {
00036         parent::__construct();
00037         $this->mDescription = "Maintenance script to delete revisions which refer to a nonexisting page";
00038         $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
00039     }
00040 
00041     public function execute() {
00042         $this->output( "Delete Orphaned Revisions\n" );
00043 
00044         $report = $this->hasOption( 'report' );
00045 
00046         $dbw = wfGetDB( DB_MASTER );
00047         $dbw->begin( __METHOD__ );
00048         list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' );
00049 
00050         # Find all the orphaned revisions
00051         $this->output( "Checking for orphaned revisions..." );
00052         $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id WHERE page_namespace IS NULL";
00053         $res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
00054 
00055         # Stash 'em all up for deletion (if needed)
00056         $revisions = array();
00057         foreach ( $res as $row ) {
00058             $revisions[] = $row->rev_id;
00059         }
00060         $count = count( $revisions );
00061         $this->output( "found {$count}.\n" );
00062 
00063         # Nothing to do?
00064         if ( $report || $count == 0 ) {
00065             $dbw->commit( __METHOD__ );
00066             exit( 0 );
00067         }
00068 
00069         # Delete each revision
00070         $this->output( "Deleting..." );
00071         $this->deleteRevs( $revisions, $dbw );
00072         $this->output( "done.\n" );
00073 
00074         # Close the transaction and call the script to purge unused text records
00075         $dbw->commit( __METHOD__ );
00076         $this->purgeRedundantText( true );
00077     }
00078 
00086     private function deleteRevs( $id, &$dbw ) {
00087         if ( !is_array( $id ) ) {
00088             $id = array( $id );
00089         }
00090         $dbw->delete( 'revision', array( 'rev_id' => $id ), __METHOD__ );
00091     }
00092 }
00093 
00094 $maintClass = "DeleteOrphanedRevisions";
00095 require_once RUN_MAINTENANCE_IF_MAIN;