MediaWiki  REL1_24
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 "
00053             . "WHERE page_namespace IS NULL";
00054         $res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
00055 
00056         # Stash 'em all up for deletion (if needed)
00057         $revisions = array();
00058         foreach ( $res as $row ) {
00059             $revisions[] = $row->rev_id;
00060         }
00061         $count = count( $revisions );
00062         $this->output( "found {$count}.\n" );
00063 
00064         # Nothing to do?
00065         if ( $report || $count == 0 ) {
00066             $dbw->commit( __METHOD__ );
00067             exit( 0 );
00068         }
00069 
00070         # Delete each revision
00071         $this->output( "Deleting..." );
00072         $this->deleteRevs( $revisions, $dbw );
00073         $this->output( "done.\n" );
00074 
00075         # Close the transaction and call the script to purge unused text records
00076         $dbw->commit( __METHOD__ );
00077         $this->purgeRedundantText( true );
00078     }
00079 
00087     private function deleteRevs( $id, &$dbw ) {
00088         if ( !is_array( $id ) ) {
00089             $id = array( $id );
00090         }
00091         $dbw->delete( 'revision', array( 'rev_id' => $id ), __METHOD__ );
00092     }
00093 }
00094 
00095 $maintClass = "DeleteOrphanedRevisions";
00096 require_once RUN_MAINTENANCE_IF_MAIN;