[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/maintenance/ -> deleteBatch.php (source)

   1  <?php
   2  /**
   3   * Deletes a batch of pages.
   4   * Usage: php deleteBatch.php [-u <user>] [-r <reason>] [-i <interval>] [listfile]
   5   * where
   6   *   [listfile] is a file where each line contains the title of a page to be
   7   *     deleted, standard input is used if listfile is not given.
   8   *   <user> is the username
   9   *   <reason> is the delete reason
  10   *   <interval> is the number of seconds to sleep for after each delete
  11   *
  12   * This program is free software; you can redistribute it and/or modify
  13   * it under the terms of the GNU General Public License as published by
  14   * the Free Software Foundation; either version 2 of the License, or
  15   * (at your option) any later version.
  16   *
  17   * This program is distributed in the hope that it will be useful,
  18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20   * GNU General Public License for more details.
  21   *
  22   * You should have received a copy of the GNU General Public License along
  23   * with this program; if not, write to the Free Software Foundation, Inc.,
  24   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25   * http://www.gnu.org/copyleft/gpl.html
  26   *
  27   * @file
  28   * @ingroup Maintenance
  29   */
  30  
  31  require_once  __DIR__ . '/Maintenance.php';
  32  
  33  /**
  34   * Maintenance script to delete a batch of pages.
  35   *
  36   * @ingroup Maintenance
  37   */
  38  class DeleteBatch extends Maintenance {
  39  
  40  	public function __construct() {
  41          parent::__construct();
  42          $this->mDescription = "Deletes a batch of pages";
  43          $this->addOption( 'u', "User to perform deletion", false, true );
  44          $this->addOption( 'r', "Reason to delete page", false, true );
  45          $this->addOption( 'i', "Interval to sleep between deletions" );
  46          $this->addArg( 'listfile', 'File with titles to delete, separated by newlines. ' .
  47              'If not given, stdin will be used.', false );
  48      }
  49  
  50  	public function execute() {
  51          global $wgUser;
  52  
  53          # Change to current working directory
  54          $oldCwd = getcwd();
  55          chdir( $oldCwd );
  56  
  57          # Options processing
  58          $username = $this->getOption( 'u', 'Delete page script' );
  59          $reason = $this->getOption( 'r', '' );
  60          $interval = $this->getOption( 'i', 0 );
  61  
  62          $user = User::newFromName( $username );
  63          if ( !$user ) {
  64              $this->error( "Invalid username", true );
  65          }
  66          $wgUser = $user;
  67  
  68          if ( $this->hasArg() ) {
  69              $file = fopen( $this->getArg(), 'r' );
  70          } else {
  71              $file = $this->getStdin();
  72          }
  73  
  74          # Setup
  75          if ( !$file ) {
  76              $this->error( "Unable to read file, exiting", true );
  77          }
  78  
  79          $dbw = wfGetDB( DB_MASTER );
  80  
  81          # Handle each entry
  82          // @codingStandardsIgnoreStart Ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
  83          for ( $linenum = 1; !feof( $file ); $linenum++ ) {
  84              // @codingStandardsIgnoreEnd
  85              $line = trim( fgets( $file ) );
  86              if ( $line == '' ) {
  87                  continue;
  88              }
  89              $title = Title::newFromText( $line );
  90              if ( is_null( $title ) ) {
  91                  $this->output( "Invalid title '$line' on line $linenum\n" );
  92                  continue;
  93              }
  94              if ( !$title->exists() ) {
  95                  $this->output( "Skipping nonexistent page '$line'\n" );
  96                  continue;
  97              }
  98  
  99              $this->output( $title->getPrefixedText() );
 100              $dbw->begin( __METHOD__ );
 101              if ( $title->getNamespace() == NS_FILE ) {
 102                  $img = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
 103                  if ( $img && $img->isLocal() && !$img->delete( $reason ) ) {
 104                      $this->output( " FAILED to delete associated file... " );
 105                  }
 106              }
 107              $page = WikiPage::factory( $title );
 108              $error = '';
 109              $success = $page->doDeleteArticle( $reason, false, 0, false, $error, $user );
 110              $dbw->commit( __METHOD__ );
 111              if ( $success ) {
 112                  $this->output( " Deleted!\n" );
 113              } else {
 114                  $this->output( " FAILED to delete article\n" );
 115              }
 116  
 117              if ( $interval ) {
 118                  sleep( $interval );
 119              }
 120              wfWaitForSlaves();
 121          }
 122      }
 123  }
 124  
 125  $maintClass = "DeleteBatch";
 126  require_once RUN_MAINTENANCE_IF_MAIN;


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1