00001 <?php 00002 00025 require_once( dirname(__FILE__) . '/Maintenance.php' ); 00026 00027 class DeleteOldRevisions extends Maintenance { 00028 public function __construct() { 00029 parent::__construct(); 00030 $this->mDescription = "Delete old (non-current) revisions from the database"; 00031 $this->addOption( 'delete', 'Actually perform the deletion' ); 00032 $this->addOption( 'page_id', 'List of page ids to work on', false ); 00033 } 00034 00035 public function execute() { 00036 $this->output( "Delete old revisions\n\n" ); 00037 $this->doDelete( $this->hasOption( 'delete' ), $this->mArgs ); 00038 } 00039 00040 function doDelete( $delete = false, $args = array() ) { 00041 00042 # Data should come off the master, wrapped in a transaction 00043 $dbw = wfGetDB( DB_MASTER ); 00044 $dbw->begin(); 00045 00046 $tbl_pag = $dbw->tableName( 'page' ); 00047 $tbl_rev = $dbw->tableName( 'revision' ); 00048 00049 $pageIdClause = ''; 00050 $revPageClause = ''; 00051 00052 # If a list of page_ids was provided, limit results to that set of page_ids 00053 if ( sizeof( $args ) > 0 ) { 00054 $pageIdList = implode( ',', $args ); 00055 $pageIdClause = " WHERE page_id IN ({$pageIdList})"; 00056 $revPageClause = " AND rev_page IN ({$pageIdList})"; 00057 $this->output( "Limiting to {$tbl_pag}.page_id IN ({$pageIdList})\n" ); 00058 } 00059 00060 # Get "active" revisions from the page table 00061 $this->output( "Searching for active revisions..." ); 00062 $res = $dbw->query( "SELECT page_latest FROM $tbl_pag{$pageIdClause}" ); 00063 foreach( $res as $row ) { 00064 $cur[] = $row->page_latest; 00065 } 00066 $this->output( "done.\n" ); 00067 00068 # Get all revisions that aren't in this set 00069 $old = array(); 00070 $this->output( "Searching for inactive revisions..." ); 00071 $set = implode( ', ', $cur ); 00072 $res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_id NOT IN ( $set ){$revPageClause}" ); 00073 foreach( $res as $row ) { 00074 $old[] = $row->rev_id; 00075 } 00076 $this->output( "done.\n" ); 00077 00078 # Inform the user of what we're going to do 00079 $count = count( $old ); 00080 $this->output( "$count old revisions found.\n" ); 00081 00082 # Delete as appropriate 00083 if( $delete && $count ) { 00084 $this->output( "Deleting..." ); 00085 $set = implode( ', ', $old ); 00086 $dbw->query( "DELETE FROM $tbl_rev WHERE rev_id IN ( $set )" ); 00087 $this->output( "done.\n" ); 00088 } 00089 00090 # This bit's done 00091 # Purge redundant text records 00092 $dbw->commit(); 00093 if( $delete ) { 00094 $this->purgeRedundantText( true ); 00095 } 00096 } 00097 } 00098 00099 $maintClass = "DeleteOldRevisions"; 00100 require_once( DO_MAINTENANCE ); 00101