MediaWiki  master
fixDoubleRedirects.php
Go to the documentation of this file.
1 <?php
30 
31 require_once __DIR__ . '/Maintenance.php';
32 
39  public function __construct() {
40  parent::__construct();
41  $this->addDescription( 'Script to fix double redirects' );
42  $this->addOption( 'async', 'Don\'t fix anything directly, just queue the jobs' );
43  $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
44  $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
45  }
46 
47  public function execute() {
48  $async = $this->hasOption( 'async' );
49  $dryrun = $this->hasOption( 'dry-run' );
50 
51  if ( $this->hasOption( 'title' ) ) {
52  $title = Title::newFromText( $this->getOption( 'title' ) );
53  if ( !$title || !$title->isRedirect() ) {
54  $this->fatalError( $title->getPrefixedText() . " is not a redirect!\n" );
55  }
56  } else {
57  $title = null;
58  }
59 
60  $dbr = $this->getDB( DB_REPLICA );
61 
62  // See also SpecialDoubleRedirects
63  $tables = [
64  'redirect',
65  'pa' => 'page',
66  'pb' => 'page',
67  ];
68  $fields = [
69  'pa.page_namespace AS pa_namespace',
70  'pa.page_title AS pa_title',
71  'pb.page_namespace AS pb_namespace',
72  'pb.page_title AS pb_title',
73  ];
74  $conds = [
75  'rd_from = pa.page_id',
76  'rd_namespace = pb.page_namespace',
77  'rd_title = pb.page_title',
78  // T42352
79  'rd_interwiki IS NULL OR rd_interwiki = ' . $dbr->addQuotes( '' ),
80  'pb.page_is_redirect' => 1,
81  ];
82 
83  if ( $title != null ) {
84  $conds['pb.page_namespace'] = $title->getNamespace();
85  $conds['pb.page_title'] = $title->getDBkey();
86  }
87  // TODO: support batch querying
88 
89  $res = $dbr->select( $tables, $fields, $conds, __METHOD__ );
90 
91  if ( !$res->numRows() ) {
92  $this->output( "No double redirects found.\n" );
93 
94  return;
95  }
96 
97  $jobs = [];
98  $processedTitles = "\n";
99  $n = 0;
100  foreach ( $res as $row ) {
101  $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
102  $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
103  if ( !$titleA->canExist() || !$titleB->canExist() ) {
104  $this->error( "Cannot fix redirect from" .
105  ( $titleA->canExist() ? "" : " invalid" ) . " title " . $titleA->getPrefixedText()
106  . " to new" .
107  ( $titleB->canExist() ? "" : " invalid" ) . " target " . $titleB->getPrefixedText()
108  . "\n"
109  );
110  continue;
111  }
112 
113  $processedTitles .= "* [[$titleA]]\n";
114 
115  $job = new DoubleRedirectJob( $titleA, [
116  'reason' => 'maintenance',
117  'redirTitle' => $titleB->getPrefixedDBkey()
118  ] );
119 
120  if ( !$async ) {
121  $success = ( $dryrun ? true : $job->run() );
122  if ( !$success ) {
123  $this->error( "Error fixing " . $titleA->getPrefixedText()
124  . ": " . $job->getLastError() . "\n" );
125  }
126  } else {
127  $jobs[] = $job;
128  if ( count( $jobs ) > DoubleRedirectJob::MAX_DR_JOBS_COUNTER ) {
129  $this->queueJobs( $jobs, $dryrun );
130  $jobs = [];
131  }
132  }
133 
134  if ( ++$n % 100 == 0 ) {
135  $this->output( "$n...\n" );
136  }
137  }
138 
139  if ( count( $jobs ) ) {
140  $this->queueJobs( $jobs, $dryrun );
141  }
142  $this->output( "$n double redirects processed" . $processedTitles . "\n" );
143  }
144 
145  protected function queueJobs( $jobs, $dryrun = false ) {
146  $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
147  MediaWikiServices::getInstance()->getJobQueueGroup()->push( $dryrun ? [] : $jobs );
148  }
149 }
150 
151 $maintClass = FixDoubleRedirects::class;
152 require_once RUN_MAINTENANCE_IF_MAIN;
$success
Fix any 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.
Represents a title within MediaWiki.
Definition: Title.php:82
const DB_REPLICA
Definition: defines.php:26
if(count( $args)< 1) $job
return true
Definition: router.php:90