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