MediaWiki  master
fixDoubleRedirects.php
Go to the documentation of this file.
1 <?php
29 
30 require_once __DIR__ . '/Maintenance.php';
31 
38  public function __construct() {
39  parent::__construct();
40  $this->addDescription( 'Script to fix double redirects' );
41  $this->addOption( 'async', 'Don\'t fix anything directly, just queue the jobs' );
42  $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
43  $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
44  }
45 
46  public function execute() {
47  $async = $this->hasOption( 'async' );
48  $dryrun = $this->hasOption( 'dry-run' );
49 
50  if ( $this->hasOption( 'title' ) ) {
51  $title = Title::newFromText( $this->getOption( 'title' ) );
52  if ( !$title || !$title->isRedirect() ) {
53  $this->fatalError( $title->getPrefixedText() . " is not a redirect!\n" );
54  }
55  } else {
56  $title = null;
57  }
58 
59  $dbr = $this->getDB( DB_REPLICA );
60 
61  // See also SpecialDoubleRedirects
62  $tables = [
63  'redirect',
64  'pa' => 'page',
65  'pb' => 'page',
66  ];
67  $fields = [
68  'pa.page_namespace AS pa_namespace',
69  'pa.page_title AS pa_title',
70  'pb.page_namespace AS pb_namespace',
71  'pb.page_title AS pb_title',
72  ];
73  $conds = [
74  'rd_from = pa.page_id',
75  'rd_namespace = pb.page_namespace',
76  'rd_title = pb.page_title',
77  // T42352
78  'rd_interwiki IS NULL OR rd_interwiki = ' . $dbr->addQuotes( '' ),
79  'pb.page_is_redirect' => 1,
80  ];
81 
82  if ( $title != null ) {
83  $conds['pb.page_namespace'] = $title->getNamespace();
84  $conds['pb.page_title'] = $title->getDBkey();
85  }
86  // TODO: support batch querying
87 
88  $res = $dbr->select( $tables, $fields, $conds, __METHOD__ );
89 
90  if ( !$res->numRows() ) {
91  $this->output( "No double redirects found.\n" );
92 
93  return;
94  }
95 
96  $jobs = [];
97  $processedTitles = "\n";
98  $n = 0;
99  foreach ( $res as $row ) {
100  $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
101  $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
102 
103  $processedTitles .= "* [[$titleA]]\n";
104 
105  $job = new DoubleRedirectJob( $titleA, [
106  'reason' => 'maintenance',
107  'redirTitle' => $titleB->getPrefixedDBkey()
108  ] );
109 
110  if ( !$async ) {
111  $success = ( $dryrun ? true : $job->run() );
112  if ( !$success ) {
113  $this->error( "Error fixing " . $titleA->getPrefixedText()
114  . ": " . $job->getLastError() . "\n" );
115  }
116  } else {
117  $jobs[] = $job;
118  if ( count( $jobs ) > DoubleRedirectJob::MAX_DR_JOBS_COUNTER ) {
119  $this->queueJobs( $jobs, $dryrun );
120  $jobs = [];
121  }
122  }
123 
124  if ( ++$n % 100 == 0 ) {
125  $this->output( "$n...\n" );
126  }
127  }
128 
129  if ( count( $jobs ) ) {
130  $this->queueJobs( $jobs, $dryrun );
131  }
132  $this->output( "$n double redirects processed" . $processedTitles . "\n" );
133  }
134 
135  protected function queueJobs( $jobs, $dryrun = false ) {
136  $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
137  MediaWikiServices::getInstance()->getJobQueueGroup()->push( $dryrun ? [] : $jobs );
138  }
139 }
140 
141 $maintClass = FixDoubleRedirects::class;
142 require_once RUN_MAINTENANCE_IF_MAIN;
$success
Job to fix double redirects after moving a page.
Maintenance script that fixes double redirects.
queueJobs( $jobs, $dryrun=false)
__construct()
Default constructor.
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:370
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:638
const DB_REPLICA
Definition: defines.php:26
if(count( $args)< 1) $job
return true
Definition: router.php:90