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