MediaWiki  1.23.1
fixDoubleRedirects.php
Go to the documentation of this file.
1 <?php
28 require_once __DIR__ . '/Maintenance.php';
29 
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  $title = $this->getOption( 'title' );
48 
49  if ( isset( $title ) ) {
51  if ( !$title || !$title->isRedirect() ) {
52  $this->error( $title->getPrefixedText() . " is not a redirect!\n", true );
53  }
54  }
55 
56  $dbr = wfGetDB( DB_SLAVE );
57 
58  // See also SpecialDoubleRedirects
59  $tables = array(
60  'redirect',
61  'pa' => 'page',
62  'pb' => 'page',
63  );
64  $fields = array(
65  'pa.page_namespace AS pa_namespace',
66  'pa.page_title AS pa_title',
67  'pb.page_namespace AS pb_namespace',
68  'pb.page_title AS pb_title',
69  );
70  $conds = array(
71  'rd_from = pa.page_id',
72  'rd_namespace = pb.page_namespace',
73  'rd_title = pb.page_title',
74  'rd_interwiki IS NULL OR rd_interwiki = ' . $dbr->addQuotes( '' ), // bug 40352
75  'pb.page_is_redirect' => 1,
76  );
77 
78  if ( isset( $title ) ) {
79  $conds['pb.page_namespace'] = $title->getNamespace();
80  $conds['pb.page_title'] = $title->getDBkey();
81  }
82  // TODO: support batch querying
83 
84  $res = $dbr->select( $tables, $fields, $conds, __METHOD__ );
85 
86  if ( !$res->numRows() ) {
87  $this->output( "No double redirects found.\n" );
88  return;
89  }
90 
91  $jobs = array();
92  $processedTitles = "\n";
93  $n = 0;
94  foreach ( $res as $row ) {
95  $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
96  $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
97  RequestContext::getMain()->setTitle( $titleA );
98 
99  $processedTitles .= "* [[$titleA]]\n";
100 
101  $job = new DoubleRedirectJob( $titleA, array(
102  'reason' => 'maintenance',
103  'redirTitle' => $titleB->getPrefixedDBkey()
104  ) );
105 
106  if ( !$async ) {
107  $success = ( $dryrun ? true : $job->run() );
108  if ( !$success ) {
109  $this->error( "Error fixing " . $titleA->getPrefixedText() . ": " . $job->getLastError() . "\n" );
110  }
111  } else {
112  $jobs[] = $job;
113  // @todo FIXME: Hardcoded constant 10000 copied from DoubleRedirectJob class
114  if ( count( $jobs ) > 10000 ) {
115  $this->queueJobs( $jobs, $dryrun );
116  $jobs = array();
117  }
118  }
119 
120  if ( ++$n % 100 == 0 ) {
121  $this->output( "$n...\n" );
122  }
123  }
124 
125  if ( count( $jobs ) ) {
126  $this->queueJobs( $jobs, $dryrun );
127  }
128  $this->output( "$n double redirects processed" . $processedTitles . "\n" );
129  }
130 
131  protected function queueJobs( $jobs, $dryrun = false ) {
132  $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
133  JobQueueGroup::singleton()->push( $dryrun ? array() : $jobs );
134  }
135 }
136 
137 $maintClass = "FixDoubleRedirects";
138 require_once RUN_MAINTENANCE_IF_MAIN;
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$tables
namespace and then decline to actually register it RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist & $tables
Definition: hooks.txt:815
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
$n
$n
Definition: RandomTest.php:76
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1530
DoubleRedirectJob
Job to fix double redirects after moving a page.
Definition: DoubleRedirectJob.php:29
$dbr
$dbr
Definition: testCompression.php:48
FixDoubleRedirects\execute
execute()
Do the actual work.
Definition: fixDoubleRedirects.php:44
$success
$success
Definition: Utf8Test.php:91
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$maintClass
$maintClass
Definition: fixDoubleRedirects.php:137
FixDoubleRedirects\__construct
__construct()
Default constructor.
Definition: fixDoubleRedirects.php:36
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:420
FixDoubleRedirects
Maintenance script that fixes double redirects.
Definition: fixDoubleRedirects.php:35
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:42
JobQueueGroup\singleton
static singleton( $wiki=false)
Definition: JobQueueGroup.php:61
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
$res
$res
Definition: database.txt:21
FixDoubleRedirects\queueJobs
queueJobs( $jobs, $dryrun=false)
Definition: fixDoubleRedirects.php:131