MediaWiki  REL1_21
deleteOrphanedRevisions.php
Go to the documentation of this file.
00001 <?php
00027 require_once( __DIR__ . '/Maintenance.php' );
00028 
00034 class DeleteOrphanedRevisions extends Maintenance {
00035         public function __construct() {
00036                 parent::__construct();
00037                 $this->mDescription = "Maintenance script to delete revisions which refer to a nonexisting page";
00038                 $this->addOption( 'report', 'Prints out a count of affected revisions but doesn\'t delete them' );
00039         }
00040 
00041         public function execute() {
00042                 $this->output( "Delete Orphaned Revisions\n" );
00043 
00044                 $report = $this->hasOption( 'report' );
00045 
00046                 $dbw = wfGetDB( DB_MASTER );
00047                 $dbw->begin( __METHOD__ );
00048                 list( $page, $revision ) = $dbw->tableNamesN( 'page', 'revision' );
00049 
00050                 # Find all the orphaned revisions
00051                 $this->output( "Checking for orphaned revisions..." );
00052                 $sql = "SELECT rev_id FROM {$revision} LEFT JOIN {$page} ON rev_page = page_id WHERE page_namespace IS NULL";
00053                 $res = $dbw->query( $sql, 'deleteOrphanedRevisions' );
00054 
00055                 # Stash 'em all up for deletion (if needed)
00056                 $revisions = array();
00057                 foreach ( $res as $row )
00058                         $revisions[] = $row->rev_id;
00059                 $count = count( $revisions );
00060                 $this->output( "found {$count}.\n" );
00061 
00062                 # Nothing to do?
00063                 if ( $report || $count == 0 ) {
00064                         $dbw->commit( __METHOD__ );
00065                         exit( 0 );
00066                 }
00067 
00068                 # Delete each revision
00069                 $this->output( "Deleting..." );
00070                 $this->deleteRevs( $revisions, $dbw );
00071                 $this->output( "done.\n" );
00072 
00073                 # Close the transaction and call the script to purge unused text records
00074                 $dbw->commit( __METHOD__ );
00075                 $this->purgeRedundantText( true );
00076         }
00077 
00085         private function deleteRevs( $id, &$dbw ) {
00086                 if ( !is_array( $id ) )
00087                         $id = array( $id );
00088                 $dbw->delete( 'revision', array( 'rev_id' => $id ), __METHOD__ );
00089         }
00090 }
00091 
00092 $maintClass = "DeleteOrphanedRevisions";
00093 require_once( RUN_MAINTENANCE_IF_MAIN );