MediaWiki master
fixDoubleRedirects.php
Go to the documentation of this file.
1<?php
17
18// @codeCoverageIgnoreStart
19require_once __DIR__ . '/Maintenance.php';
20// @codeCoverageIgnoreEnd
21
28 public function __construct() {
29 parent::__construct();
30 $this->addDescription( 'Script to fix double redirects' );
31 $this->addOption( 'async', 'Don\'t fix anything directly, just queue the jobs' );
32 $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
33 $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
34 }
35
36 public function execute() {
37 $async = $this->hasOption( 'async' );
38 $dryrun = $this->hasOption( 'dry-run' );
39
40 if ( $this->hasOption( 'title' ) ) {
41 $title = Title::newFromText( $this->getOption( 'title' ) );
42 if ( !$title || !$title->isRedirect() ) {
43 $this->fatalError( $title->getPrefixedText() . " is not a redirect!\n" );
44 }
45 } else {
46 $title = null;
47 }
48
49 $dbr = $this->getReplicaDB();
50
51 // See also SpecialDoubleRedirects
52 // TODO: support batch querying
53 $queryBuilder = $dbr->newSelectQueryBuilder()
54 ->select( [
55 'pa_namespace' => 'pa.page_namespace',
56 'pa_title' => 'pa.page_title',
57 'pb_namespace' => 'pb.page_namespace',
58 'pb_title' => 'pb.page_title',
59 ] )
60 ->from( 'redirect' )
61 ->join( 'page', 'pa', 'rd_from = pa.page_id' )
62 ->join( 'page', 'pb', [ 'rd_namespace = pb.page_namespace', 'rd_title = pb.page_title' ] )
63 // T42352
64 ->where( [ 'rd_interwiki' => '', 'pb.page_is_redirect' => 1 ] );
65
66 if ( $title != null ) {
67 $queryBuilder->andWhere( [
68 'pb.page_namespace' => $title->getNamespace(),
69 'pb.page_title' => $title->getDBkey()
70 ] );
71 }
72 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
73
74 if ( !$res->numRows() ) {
75 $this->output( "No double redirects found.\n" );
76
77 return;
78 }
79
80 $jobs = [];
81 $processedTitles = "\n";
82 $n = 0;
83 $services = $this->getServiceContainer();
84 foreach ( $res as $row ) {
85 $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
86 $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
87 if ( !$titleA->canExist() || !$titleB->canExist() ) {
88 $this->error( "Cannot fix redirect from" .
89 ( $titleA->canExist() ? "" : " invalid" ) . " title " . $titleA->getPrefixedText()
90 . " to new" .
91 ( $titleB->canExist() ? "" : " invalid" ) . " target " . $titleB->getPrefixedText()
92 . "\n"
93 );
94 continue;
95 }
96
97 $processedTitles .= "* [[$titleA]]\n";
98
100 $titleA,
101 [
102 'reason' => 'maintenance',
103 'redirTitle' => $titleB->getPrefixedDBkey()
104 ],
105 $services->getRevisionLookup(),
106 $services->getMagicWordFactory(),
107 $services->getWikiPageFactory()
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( array $jobs, bool $dryrun = false ) {
136 $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
137 $this->getServiceContainer()->getJobQueueGroup()->push( $dryrun ? [] : $jobs );
138 }
139}
140
141// @codeCoverageIgnoreStart
142$maintClass = FixDoubleRedirects::class;
143require_once RUN_MAINTENANCE_IF_MAIN;
144// @codeCoverageIgnoreEnd
Maintenance script that fixes double redirects.
queueJobs(array $jobs, bool $dryrun=false)
__construct()
Default constructor.
execute()
Do the actual work.
Fix any double redirects after moving a page.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getReplicaDB(string|false $virtualDomain=false)
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:69
if(count( $args)< 1) $job