[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/maintenance/ -> fixDoubleRedirects.php (source)

   1  <?php
   2  /**
   3   * Fix double redirects.
   4   *
   5   * Copyright © 2011 Ilmari Karonen <[email protected]>
   6   * https://www.mediawiki.org/
   7   *
   8   * This program is free software; you can redistribute it and/or modify
   9   * it under the terms of the GNU General Public License as published by
  10   * the Free Software Foundation; either version 2 of the License, or
  11   * (at your option) any later version.
  12   *
  13   * This program is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16   * GNU General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU General Public License along
  19   * with this program; if not, write to the Free Software Foundation, Inc.,
  20   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21   * http://www.gnu.org/copyleft/gpl.html
  22   *
  23   * @file
  24   * @author Ilmari Karonen <[email protected]>
  25   * @ingroup Maintenance
  26   */
  27  
  28  require_once  __DIR__ . '/Maintenance.php';
  29  
  30  /**
  31   * Maintenance script that fixes double redirects.
  32   *
  33   * @ingroup Maintenance
  34   */
  35  class FixDoubleRedirects extends Maintenance {
  36  	public function __construct() {
  37          parent::__construct();
  38          $this->mDescription = "Script to fix double redirects";
  39          $this->addOption( 'async', 'Don\'t fix anything directly, just queue the jobs' );
  40          $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
  41          $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
  42      }
  43  
  44  	public function execute() {
  45          $async = $this->getOption( 'async', false );
  46          $dryrun = $this->getOption( 'dry-run', false );
  47  
  48          if ( $this->hasOption( 'title' ) ) {
  49              $title = Title::newFromText( $this->getOption( 'title' ) );
  50              if ( !$title || !$title->isRedirect() ) {
  51                  $this->error( $title->getPrefixedText() . " is not a redirect!\n", true );
  52              }
  53          } else {
  54              $title = null;
  55          }
  56  
  57          $dbr = wfGetDB( DB_SLAVE );
  58  
  59          // See also SpecialDoubleRedirects
  60          $tables = array(
  61              'redirect',
  62              'pa' => 'page',
  63              'pb' => 'page',
  64          );
  65          $fields = array(
  66              'pa.page_namespace AS pa_namespace',
  67              'pa.page_title AS pa_title',
  68              'pb.page_namespace AS pb_namespace',
  69              'pb.page_title AS pb_title',
  70          );
  71          $conds = array(
  72              'rd_from = pa.page_id',
  73              'rd_namespace = pb.page_namespace',
  74              'rd_title = pb.page_title',
  75              'rd_interwiki IS NULL OR rd_interwiki = ' . $dbr->addQuotes( '' ), // bug 40352
  76              'pb.page_is_redirect' => 1,
  77          );
  78  
  79          if ( $title != null ) {
  80              $conds['pb.page_namespace'] = $title->getNamespace();
  81              $conds['pb.page_title'] = $title->getDBkey();
  82          }
  83          // TODO: support batch querying
  84  
  85          $res = $dbr->select( $tables, $fields, $conds, __METHOD__ );
  86  
  87          if ( !$res->numRows() ) {
  88              $this->output( "No double redirects found.\n" );
  89  
  90              return;
  91          }
  92  
  93          $jobs = array();
  94          $processedTitles = "\n";
  95          $n = 0;
  96          foreach ( $res as $row ) {
  97              $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
  98              $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
  99  
 100              $processedTitles .= "* [[$titleA]]\n";
 101  
 102              $job = new DoubleRedirectJob( $titleA, array(
 103                  'reason' => 'maintenance',
 104                  'redirTitle' => $titleB->getPrefixedDBkey()
 105              ) );
 106  
 107              if ( !$async ) {
 108                  $success = ( $dryrun ? true : $job->run() );
 109                  if ( !$success ) {
 110                      $this->error( "Error fixing " . $titleA->getPrefixedText()
 111                          . ": " . $job->getLastError() . "\n" );
 112                  }
 113              } else {
 114                  $jobs[] = $job;
 115                  // @todo FIXME: Hardcoded constant 10000 copied from DoubleRedirectJob class
 116                  if ( count( $jobs ) > 10000 ) {
 117                      $this->queueJobs( $jobs, $dryrun );
 118                      $jobs = array();
 119                  }
 120              }
 121  
 122              if ( ++$n % 100 == 0 ) {
 123                  $this->output( "$n...\n" );
 124              }
 125          }
 126  
 127          if ( count( $jobs ) ) {
 128              $this->queueJobs( $jobs, $dryrun );
 129          }
 130          $this->output( "$n double redirects processed" . $processedTitles . "\n" );
 131      }
 132  
 133  	protected function queueJobs( $jobs, $dryrun = false ) {
 134          $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
 135          JobQueueGroup::singleton()->push( $dryrun ? array() : $jobs );
 136      }
 137  }
 138  
 139  $maintClass = "FixDoubleRedirects";
 140  require_once RUN_MAINTENANCE_IF_MAIN;


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