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