MediaWiki  REL1_19
deleteBatch.php
Go to the documentation of this file.
00001 <?php
00030 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
00031 
00032 class DeleteBatch extends Maintenance {
00033 
00034         public function __construct() {
00035                 parent::__construct();
00036                 $this->mDescription = "Deletes a batch of pages";
00037                 $this->addOption( 'u', "User to perform deletion", false, true );
00038                 $this->addOption( 'r', "Reason to delete page", false, true );
00039                 $this->addOption( 'i', "Interval to sleep between deletions" );
00040                 $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
00041                         'If not given, stdin will be used.', false );
00042         }
00043 
00044         public function execute() {
00045                 global $wgUser;
00046 
00047                 # Change to current working directory
00048                 $oldCwd = getcwd();
00049                 chdir( $oldCwd );
00050 
00051                 # Options processing
00052                 $username = $this->getOption( 'u', 'Delete page script' );
00053                 $reason = $this->getOption( 'r', '' );
00054                 $interval = $this->getOption( 'i', 0 );
00055 
00056                 $user = User::newFromName( $username );
00057                 if ( !$user ) {
00058                         $this->error( "Invalid username", true );
00059                 }
00060                 $wgUser = $user;
00061 
00062                 if ( $this->hasArg() ) {
00063                         $file = fopen( $this->getArg(), 'r' );
00064                 } else {
00065                         $file = $this->getStdin();
00066                 }
00067 
00068                 # Setup
00069                 if ( !$file ) {
00070                         $this->error( "Unable to read file, exiting", true );
00071                 }
00072 
00073                 $dbw = wfGetDB( DB_MASTER );
00074 
00075                 # Handle each entry
00076                 for ( $linenum = 1; !feof( $file ); $linenum++ ) {
00077                         $line = trim( fgets( $file ) );
00078                         if ( $line == '' ) {
00079                                 continue;
00080                         }
00081                         $title = Title::newFromText( $line );
00082                         if ( is_null( $title ) ) {
00083                                 $this->output( "Invalid title '$line' on line $linenum\n" );
00084                                 continue;
00085                         }
00086                         if ( !$title->exists() ) {
00087                                 $this->output( "Skipping nonexistent page '$line'\n" );
00088                                 continue;
00089                         }
00090 
00091                         $this->output( $title->getPrefixedText() );
00092                         $dbw->begin();
00093                         if ( $title->getNamespace() == NS_FILE ) {
00094                                 $img = wfFindFile( $title );
00095                                 if ( $img && $img->isLocal() && !$img->delete( $reason ) ) {
00096                                         $this->output( " FAILED to delete associated file... " );
00097                                 }
00098                         }
00099                         $page = WikiPage::factory( $title );
00100                         $error = '';
00101                         $success = $page->doDeleteArticle( $reason, false, 0, false, $error, $user );
00102                         $dbw->commit();
00103                         if ( $success ) {
00104                                 $this->output( " Deleted!\n" );
00105                         } else {
00106                                 $this->output( " FAILED to delete article\n" );
00107                         }
00108 
00109                         if ( $interval ) {
00110                                 sleep( $interval );
00111                         }
00112                         wfWaitForSlaves();
00113                 }
00114         }
00115 }
00116 
00117 $maintClass = "DeleteBatch";
00118 require_once( RUN_MAINTENANCE_IF_MAIN );