MediaWiki  master
fixDoubleRedirects.php
Go to the documentation of this file.
1 <?php
29 
30 require_once __DIR__ . '/Maintenance.php';
31 
38  public function __construct() {
39  parent::__construct();
40  $this->addDescription( 'Script to fix double redirects' );
41  $this->addOption( 'async', 'Don\'t fix anything directly, just queue the jobs' );
42  $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
43  $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
44  }
45 
46  public function execute() {
47  $async = $this->hasOption( 'async' );
48  $dryrun = $this->hasOption( 'dry-run' );
49 
50  if ( $this->hasOption( 'title' ) ) {
51  $title = Title::newFromText( $this->getOption( 'title' ) );
52  if ( !$title || !$title->isRedirect() ) {
53  $this->fatalError( $title->getPrefixedText() . " is not a redirect!\n" );
54  }
55  } else {
56  $title = null;
57  }
58 
59  $dbr = $this->getDB( DB_REPLICA );
60 
61  // See also SpecialDoubleRedirects
62  // TODO: support batch querying
63  $queryBuilder = $dbr->newSelectQueryBuilder()
64  ->select( [
65  'pa.page_namespace AS pa_namespace',
66  'pa.page_title AS pa_title',
67  'pb.page_namespace AS pb_namespace',
68  'pb.page_title AS pb_title',
69  ] )
70  ->from( 'redirect' )
71  ->join( 'page', 'pa', 'rd_from = pa.page_id' )
72  ->join( 'page', 'pb', [ 'rd_namespace = pb.page_namespace', 'rd_title = pb.page_title' ] )
73  // T42352
74  ->where( [ 'rd_interwiki' => [ null, '' ], 'pb.page_is_redirect' => 1 ] );
75 
76  if ( $title != null ) {
77  $queryBuilder->andWhere( [
78  'pb.page_namespace' => $title->getNamespace(),
79  'pb.page_title' => $title->getDBkey()
80  ] );
81  }
82  $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
83 
84  if ( !$res->numRows() ) {
85  $this->output( "No double redirects found.\n" );
86 
87  return;
88  }
89 
90  $jobs = [];
91  $processedTitles = "\n";
92  $n = 0;
93  $services = $this->getServiceContainer();
94  foreach ( $res as $row ) {
95  $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
96  $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
97  if ( !$titleA->canExist() || !$titleB->canExist() ) {
98  $this->error( "Cannot fix redirect from" .
99  ( $titleA->canExist() ? "" : " invalid" ) . " title " . $titleA->getPrefixedText()
100  . " to new" .
101  ( $titleB->canExist() ? "" : " invalid" ) . " target " . $titleB->getPrefixedText()
102  . "\n"
103  );
104  continue;
105  }
106 
107  $processedTitles .= "* [[$titleA]]\n";
108 
109  $job = new DoubleRedirectJob(
110  $titleA,
111  [
112  'reason' => 'maintenance',
113  'redirTitle' => $titleB->getPrefixedDBkey()
114  ],
115  $services->getRevisionLookup(),
116  $services->getMagicWordFactory(),
117  $services->getWikiPageFactory()
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  $this->getServiceContainer()->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.
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:76
const DB_REPLICA
Definition: defines.php:26
if(count( $args)< 1) $job
return true
Definition: router.php:90