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