MediaWiki  master
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->addDescription( '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->hasOption( 'async' );
46  $dryrun = $this->hasOption( 'dry-run' );
47 
48  if ( $this->hasOption( 'title' ) ) {
49  $title = Title::newFromText( $this->getOption( 'title' ) );
50  if ( !$title || !$title->isRedirect() ) {
51  $this->fatalError( $title->getPrefixedText() . " is not a redirect!\n" );
52  }
53  } else {
54  $title = null;
55  }
56 
57  $dbr = $this->getDB( DB_REPLICA );
58 
59  // See also SpecialDoubleRedirects
60  $tables = [
61  'redirect',
62  'pa' => 'page',
63  'pb' => 'page',
64  ];
65  $fields = [
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 = [
72  'rd_from = pa.page_id',
73  'rd_namespace = pb.page_namespace',
74  'rd_title = pb.page_title',
75  // T42352
76  'rd_interwiki IS NULL OR rd_interwiki = ' . $dbr->addQuotes( '' ),
77  'pb.page_is_redirect' => 1,
78  ];
79 
80  if ( $title != null ) {
81  $conds['pb.page_namespace'] = $title->getNamespace();
82  $conds['pb.page_title'] = $title->getDBkey();
83  }
84  // TODO: support batch querying
85 
86  $res = $dbr->select( $tables, $fields, $conds, __METHOD__ );
87 
88  if ( !$res->numRows() ) {
89  $this->output( "No double redirects found.\n" );
90 
91  return;
92  }
93 
94  $jobs = [];
95  $processedTitles = "\n";
96  $n = 0;
97  foreach ( $res as $row ) {
98  $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
99  $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
100 
101  $processedTitles .= "* [[$titleA]]\n";
102 
103  $job = new DoubleRedirectJob( $titleA, [
104  'reason' => 'maintenance',
105  'redirTitle' => $titleB->getPrefixedDBkey()
106  ] );
107 
108  if ( !$async ) {
109  $success = ( $dryrun ? true : $job->run() );
110  if ( !$success ) {
111  $this->error( "Error fixing " . $titleA->getPrefixedText()
112  . ": " . $job->getLastError() . "\n" );
113  }
114  } else {
115  $jobs[] = $job;
116  // @todo FIXME: Hardcoded constant 10000 copied from DoubleRedirectJob class
117  if ( count( $jobs ) > 10000 ) {
118  $this->queueJobs( $jobs, $dryrun );
119  $jobs = [];
120  }
121  }
122 
123  if ( ++$n % 100 == 0 ) {
124  $this->output( "$n...\n" );
125  }
126  }
127 
128  if ( count( $jobs ) ) {
129  $this->queueJobs( $jobs, $dryrun );
130  }
131  $this->output( "$n double redirects processed" . $processedTitles . "\n" );
132  }
133 
134  protected function queueJobs( $jobs, $dryrun = false ) {
135  $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
136  JobQueueGroup::singleton()->push( $dryrun ? [] : $jobs );
137  }
138 }
139 
140 $maintClass = FixDoubleRedirects::class;
141 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
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:328
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
true
return true
Definition: router.php:90
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
$success
$success
Definition: NoLocalSettings.php:42
$res
$res
Definition: testCompression.php:57
DoubleRedirectJob
Job to fix double redirects after moving a page.
Definition: DoubleRedirectJob.php:33
$dbr
$dbr
Definition: testCompression.php:54
FixDoubleRedirects\execute
execute()
Do the actual work.
Definition: fixDoubleRedirects.php:44
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:591
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
$maintClass
$maintClass
Definition: fixDoubleRedirects.php:140
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1366
FixDoubleRedirects\__construct
__construct()
Default constructor.
Definition: fixDoubleRedirects.php:36
FixDoubleRedirects
Maintenance script that fixes double redirects.
Definition: fixDoubleRedirects.php:35
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:70
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:50
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:463
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
FixDoubleRedirects\queueJobs
queueJobs( $jobs, $dryrun=false)
Definition: fixDoubleRedirects.php:134