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