| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
use MediaWiki\Title\Title; |
| 29 |
|
| 30 |
|
| 31 |
require_once __DIR__ . '/Maintenance.php'; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class FixDoubleRedirects extends Maintenance { |
| 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 |
|
| 64 |
|
| 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 |
|
| 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 |
|
| 111 |
$job = new DoubleRedirectJob( |
| 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 |
|
| 154 |
$maintClass = FixDoubleRedirects::class; |
| 155 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 156 |
|