MediaWiki  REL1_19
deleteOrphanedRevisions.php
Go to the documentation of this file.
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                 $count = count( $revisions );
00055                 $this->output( "found {$count}.\n" );
00056 
00057                 # Nothing to do?
00058                 if ( $report || $count == 0 ) {
00059                         $dbw->commit();
00060                         exit( 0 );
00061                 }
00062 
00063                 # Delete each revision
00064                 $this->output( "Deleting..." );
00065                 $this->deleteRevs( $revisions, $dbw );
00066                 $this->output( "done.\n" );
00067 
00068                 # Close the transaction and call the script to purge unused text records
00069                 $dbw->commit();
00070                 $this->purgeRedundantText( true );
00071         }
00072 
00080         private function deleteRevs( $id, &$dbw ) {
00081                 if ( !is_array( $id ) )
00082                         $id = array( $id );
00083                 $dbw->delete( 'revision', array( 'rev_id' => $id ), __METHOD__ );
00084         }
00085 }
00086 
00087 $maintClass = "DeleteOrphanedRevisions";
00088 require_once( RUN_MAINTENANCE_IF_MAIN );
00089