MediaWiki  REL1_19
deleteOldRevisions.php
Go to the documentation of this file.
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                 $cur = array();
00064                 foreach ( $res as $row ) {
00065                         $cur[] = $row->page_latest;
00066                 }
00067                 $this->output( "done.\n" );
00068 
00069                 # Get all revisions that aren't in this set
00070                 $old = array();
00071                 $this->output( "Searching for inactive revisions..." );
00072                 $set = implode( ', ', $cur );
00073                 $res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_id NOT IN ( $set ){$revPageClause}" );
00074                 foreach ( $res as $row ) {
00075                         $old[] = $row->rev_id;
00076                 }
00077                 $this->output( "done.\n" );
00078 
00079                 # Inform the user of what we're going to do
00080                 $count = count( $old );
00081                 $this->output( "$count old revisions found.\n" );
00082 
00083                 # Delete as appropriate
00084                 if ( $delete && $count ) {
00085                         $this->output( "Deleting..." );
00086                         $set = implode( ', ', $old );
00087                         $dbw->query( "DELETE FROM $tbl_rev WHERE rev_id IN ( $set )" );
00088                         $this->output( "done.\n" );
00089                 }
00090 
00091                 # This bit's done
00092                 # Purge redundant text records
00093                 $dbw->commit();
00094                 if ( $delete ) {
00095                         $this->purgeRedundantText( true );
00096                 }
00097         }
00098 }
00099 
00100 $maintClass = "DeleteOldRevisions";
00101 require_once( RUN_MAINTENANCE_IF_MAIN );
00102