MediaWiki  REL1_20
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                 $tbl_pag = $dbw->tableName( 'page' );
00052                 $tbl_rev = $dbw->tableName( 'revision' );
00053 
00054                 $pageIdClause = '';
00055                 $revPageClause = '';
00056 
00057                 # If a list of page_ids was provided, limit results to that set of page_ids
00058                 if ( sizeof( $args ) > 0 ) {
00059                         $pageIdList = implode( ',', $args );
00060                         $pageIdClause = " WHERE page_id IN ({$pageIdList})";
00061                         $revPageClause = " AND rev_page IN ({$pageIdList})";
00062                         $this->output( "Limiting to {$tbl_pag}.page_id IN ({$pageIdList})\n" );
00063                 }
00064 
00065                 # Get "active" revisions from the page table
00066                 $this->output( "Searching for active revisions..." );
00067                 $res = $dbw->query( "SELECT page_latest FROM $tbl_pag{$pageIdClause}" );
00068                 $cur = array();
00069                 foreach ( $res as $row ) {
00070                         $cur[] = $row->page_latest;
00071                 }
00072                 $this->output( "done.\n" );
00073 
00074                 # Get all revisions that aren't in this set
00075                 $old = array();
00076                 $this->output( "Searching for inactive revisions..." );
00077                 $set = implode( ', ', $cur );
00078                 $res = $dbw->query( "SELECT rev_id FROM $tbl_rev WHERE rev_id NOT IN ( $set ){$revPageClause}" );
00079                 foreach ( $res as $row ) {
00080                         $old[] = $row->rev_id;
00081                 }
00082                 $this->output( "done.\n" );
00083 
00084                 # Inform the user of what we're going to do
00085                 $count = count( $old );
00086                 $this->output( "$count old revisions found.\n" );
00087 
00088                 # Delete as appropriate
00089                 if ( $delete && $count ) {
00090                         $this->output( "Deleting..." );
00091                         $set = implode( ', ', $old );
00092                         $dbw->query( "DELETE FROM $tbl_rev WHERE rev_id IN ( $set )" );
00093                         $this->output( "done.\n" );
00094                 }
00095 
00096                 # This bit's done
00097                 # Purge redundant text records
00098                 $dbw->commit( __METHOD__ );
00099                 if ( $delete ) {
00100                         $this->purgeRedundantText( true );
00101                 }
00102         }
00103 }
00104 
00105 $maintClass = "DeleteOldRevisions";
00106 require_once( RUN_MAINTENANCE_IF_MAIN );
00107