2011-02-02 19:25:53 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-07-10 16:50:19 +00:00
|
|
|
* Fix double redirects.
|
2011-02-02 19:25:53 +00:00
|
|
|
*
|
2012-07-10 16:50:19 +00:00
|
|
|
* Copyright © 2011 Ilmari Karonen <nospam@vyznev.net>
|
2014-03-20 15:45:01 +00:00
|
|
|
* https://www.mediawiki.org/
|
2011-02-02 19:25:53 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @author Ilmari Karonen <nospam@vyznev.net>
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-01-27 20:19:18 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2011-02-02 19:25:53 +00:00
|
|
|
|
2012-07-10 16:50:19 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script that fixes double redirects.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2011-02-02 19:25:53 +00:00
|
|
|
class FixDoubleRedirects extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Script to fix double redirects' );
|
2011-02-02 21:30:02 +00:00
|
|
|
$this->addOption( 'async', 'Don\'t fix anything directly, just queue the jobs' );
|
2011-02-02 19:25:53 +00:00
|
|
|
$this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
|
2012-07-10 16:50:19 +00:00
|
|
|
$this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
|
2011-02-02 19:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2017-07-19 19:51:30 +00:00
|
|
|
$async = $this->hasOption( 'async' );
|
|
|
|
|
$dryrun = $this->hasOption( 'dry-run' );
|
2011-02-02 19:25:53 +00:00
|
|
|
|
2014-05-10 12:23:50 +00:00
|
|
|
if ( $this->hasOption( 'title' ) ) {
|
|
|
|
|
$title = Title::newFromText( $this->getOption( 'title' ) );
|
2011-02-02 19:25:53 +00:00
|
|
|
if ( !$title || !$title->isRedirect() ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( $title->getPrefixedText() . " is not a redirect!\n" );
|
2011-02-02 19:25:53 +00:00
|
|
|
}
|
2014-05-10 12:23:50 +00:00
|
|
|
} else {
|
|
|
|
|
$title = null;
|
2011-02-02 19:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbr = $this->getReplicaDB();
|
2011-02-02 19:25:53 +00:00
|
|
|
|
2012-09-19 13:32:30 +00:00
|
|
|
// See also SpecialDoubleRedirects
|
2023-09-21 11:54:38 +00:00
|
|
|
// TODO: support batch querying
|
|
|
|
|
$queryBuilder = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( [
|
2024-01-08 20:47:45 +00:00
|
|
|
'pa_namespace' => 'pa.page_namespace',
|
|
|
|
|
'pa_title' => 'pa.page_title',
|
|
|
|
|
'pb_namespace' => 'pb.page_namespace',
|
|
|
|
|
'pb_title' => 'pb.page_title',
|
2023-09-21 11:54:38 +00:00
|
|
|
] )
|
|
|
|
|
->from( 'redirect' )
|
|
|
|
|
->join( 'page', 'pa', 'rd_from = pa.page_id' )
|
|
|
|
|
->join( 'page', 'pb', [ 'rd_namespace = pb.page_namespace', 'rd_title = pb.page_title' ] )
|
2020-05-19 22:10:27 +00:00
|
|
|
// T42352
|
2023-10-03 17:29:38 +00:00
|
|
|
->where( [ 'rd_interwiki' => '', 'pb.page_is_redirect' => 1 ] );
|
2011-02-02 19:25:53 +00:00
|
|
|
|
2014-05-10 12:23:50 +00:00
|
|
|
if ( $title != null ) {
|
2023-09-21 11:54:38 +00:00
|
|
|
$queryBuilder->andWhere( [
|
|
|
|
|
'pb.page_namespace' => $title->getNamespace(),
|
|
|
|
|
'pb.page_title' => $title->getDBkey()
|
|
|
|
|
] );
|
2011-02-02 19:25:53 +00:00
|
|
|
}
|
2023-09-21 11:54:38 +00:00
|
|
|
$res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
|
2011-02-02 19:25:53 +00:00
|
|
|
|
|
|
|
|
if ( !$res->numRows() ) {
|
|
|
|
|
$this->output( "No double redirects found.\n" );
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2011-02-02 19:25:53 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$jobs = [];
|
2012-09-19 13:32:30 +00:00
|
|
|
$processedTitles = "\n";
|
2011-02-02 19:25:53 +00:00
|
|
|
$n = 0;
|
2023-08-31 09:21:12 +00:00
|
|
|
$services = $this->getServiceContainer();
|
2011-02-02 19:25:53 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
$titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
|
|
|
|
|
$titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
|
2022-10-04 18:44:50 +00:00
|
|
|
if ( !$titleA->canExist() || !$titleB->canExist() ) {
|
|
|
|
|
$this->error( "Cannot fix redirect from" .
|
|
|
|
|
( $titleA->canExist() ? "" : " invalid" ) . " title " . $titleA->getPrefixedText()
|
|
|
|
|
. " to new" .
|
|
|
|
|
( $titleB->canExist() ? "" : " invalid" ) . " target " . $titleB->getPrefixedText()
|
|
|
|
|
. "\n"
|
|
|
|
|
);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2011-02-02 19:25:53 +00:00
|
|
|
|
2012-09-19 13:32:30 +00:00
|
|
|
$processedTitles .= "* [[$titleA]]\n";
|
|
|
|
|
|
2023-06-28 12:33:38 +00:00
|
|
|
$job = new DoubleRedirectJob(
|
|
|
|
|
$titleA,
|
|
|
|
|
[
|
|
|
|
|
'reason' => 'maintenance',
|
|
|
|
|
'redirTitle' => $titleB->getPrefixedDBkey()
|
|
|
|
|
],
|
|
|
|
|
$services->getRevisionLookup(),
|
|
|
|
|
$services->getMagicWordFactory(),
|
|
|
|
|
$services->getWikiPageFactory()
|
|
|
|
|
);
|
2011-02-02 19:25:53 +00:00
|
|
|
|
|
|
|
|
if ( !$async ) {
|
|
|
|
|
$success = ( $dryrun ? true : $job->run() );
|
|
|
|
|
if ( !$success ) {
|
2014-04-23 08:53:03 +00:00
|
|
|
$this->error( "Error fixing " . $titleA->getPrefixedText()
|
|
|
|
|
. ": " . $job->getLastError() . "\n" );
|
2011-02-02 19:25:53 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$jobs[] = $job;
|
2021-11-26 18:59:41 +00:00
|
|
|
if ( count( $jobs ) > DoubleRedirectJob::MAX_DR_JOBS_COUNTER ) {
|
2011-02-02 19:25:53 +00:00
|
|
|
$this->queueJobs( $jobs, $dryrun );
|
2016-02-17 09:09:32 +00:00
|
|
|
$jobs = [];
|
2011-02-02 19:25:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ++$n % 100 == 0 ) {
|
|
|
|
|
$this->output( "$n...\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( count( $jobs ) ) {
|
|
|
|
|
$this->queueJobs( $jobs, $dryrun );
|
|
|
|
|
}
|
2012-09-19 13:32:30 +00:00
|
|
|
$this->output( "$n double redirects processed" . $processedTitles . "\n" );
|
2011-02-02 19:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function queueJobs( $jobs, $dryrun = false ) {
|
|
|
|
|
$this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
|
2023-08-31 09:21:12 +00:00
|
|
|
$this->getServiceContainer()->getJobQueueGroup()->push( $dryrun ? [] : $jobs );
|
2011-02-02 19:25:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = FixDoubleRedirects::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|