From 846c60b9ae98dd9b36c2dd048acdab0e2b6f921f Mon Sep 17 00:00:00 2001 From: Ilmari Karonen Date: Wed, 2 Feb 2011 19:25:53 +0000 Subject: [PATCH] Add a maintenance script to fix double redirects (and corresponding edit summary messages). Still lacks some features like batch querying, but basically it works. --- RELEASE-NOTES | 1 + languages/messages/MessagesEn.php | 1 + languages/messages/MessagesFi.php | 1 + languages/messages/MessagesQqq.php | 1 + maintenance/fixDoubleRedirects.php | 120 +++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 maintenance/fixDoubleRedirects.php diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 89459c619b..aa236a1f8e 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -58,6 +58,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 26285) Extensions will be automatically generated on upload if the user specified a filename without extension. * (bug 26851) Special:UserRights now allows to prefill the reason field +* New maintenance script to fix double redirects (maintenance/fixDoubleRedirects.php) === Bug fixes in 1.18 === * (bug 23119) WikiError class and subclasses are now marked as deprecated diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 4aadb9d536..26dc3ca673 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -2435,6 +2435,7 @@ Each row contains links to the first and second redirect, as well as the target Crossed out entries have been solved.', 'double-redirect-fixed-move' => '[[$1]] has been moved. It now redirects to [[$2]].', +'double-redirect-fixed-maintenance' => 'Fixing double redirect from [[$1]] to [[$2]].', 'double-redirect-fixer' => 'Redirect fixer', 'brokenredirects' => 'Broken redirects', diff --git a/languages/messages/MessagesFi.php b/languages/messages/MessagesFi.php index 16f486102f..0bd7b899c0 100644 --- a/languages/messages/MessagesFi.php +++ b/languages/messages/MessagesFi.php @@ -1871,6 +1871,7 @@ Tiedot [$2 tiedoston kuvaussivulta] näkyvät alla.', Jokaisella rivillä on linkit ensimmäiseen ja toiseen ohjaukseen sekä toisen ohjauksen kohteen ensimmäiseen riviin, eli yleensä ”oikeaan” kohteeseen, johon ensimmäisen ohjauksen pitäisi osoittaa. Yliviivatut kohteet on korjattu.', 'double-redirect-fixed-move' => '[[$1]] on siirretty, ja se ohjaa nyt sivulle [[$2]]', +'double-redirect-fixed-maintenance' => 'Korjataan kaksinkertainen ohjaus sivulta [[$1]] sivulle [[$2]]', 'double-redirect-fixer' => 'Ohjausten korjaaja', 'brokenredirects' => 'Virheelliset ohjaukset', diff --git a/languages/messages/MessagesQqq.php b/languages/messages/MessagesQqq.php index 090ffbeb91..74665fffd0 100644 --- a/languages/messages/MessagesQqq.php +++ b/languages/messages/MessagesQqq.php @@ -2050,6 +2050,7 @@ Possible alternatives to the word 'content' are 'subject matter' or 'wiki subjec 'doubleredirects' => 'Name of [[Special:DoubleRedirects]] displayed in [[Special:SpecialPages]]', 'doubleredirectstext' => 'Shown on top of [[Special:Doubleredirects]]', 'double-redirect-fixed-move' => 'This is the message in the log when the software (under the username {{msg|double-redirect-fixer}}) updates the redirects after a page move. See also {{msg|fix-double-redirects}}.', +'double-redirect-fixed-maintenance' => 'This is the message in the log when the software (under the username {{msg|double-redirect-fixer}}) updates the redirects after running maintenance/fixDoubleRedirects.php. Compare with {{msg|double-redirect-fixed-move}}.', 'double-redirect-fixer' => "This is the '''username''' of the user who updates the double redirects after a page move. A user is created with this username, so it is perhaps better to not change this message too often. See also {{msg|double-redirect-fixed-move}} and {{msg|fix-double-redirects}}.", 'brokenredirects' => 'Name of [[Special:BrokenRedirects]] displayed in [[Special:SpecialPages]]', diff --git a/maintenance/fixDoubleRedirects.php b/maintenance/fixDoubleRedirects.php new file mode 100644 index 0000000000..a6eb667eb6 --- /dev/null +++ b/maintenance/fixDoubleRedirects.php @@ -0,0 +1,120 @@ + + * http://www.mediawiki.org/ + * + * 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 + * @ingroup Maintenance + */ + +require_once( dirname( __FILE__ ) . '/Maintenance.php' ); + +class FixDoubleRedirects extends Maintenance { + public function __construct() { + parent::__construct(); + $this->mDescription = "Script to fix double redirects"; + $this->addOption( 'async', 'Don\'t fix anything directly, just add queue the jobs' ); + $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true ); + $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' ); + } + + public function execute() { + $async = $this->getOption( 'async', false ); + $dryrun = $this->getOption( 'dry-run', false ); + $title = $this->getOption( 'title' ); + + if ( isset( $title ) ) { + $title = Title::newFromText( $title ); + if ( !$title || !$title->isRedirect() ) { + $this->error( $title->getPrefixedText() . " is not a redirect!\n", true ); + } + } + + $dbr = wfGetDB( DB_SLAVE ); + + $tables = array( 'redirect', 'pa' => 'page', 'pb' => 'page' ); + $fields = array( + 'pa.page_namespace AS pa_namespace', + 'pa.page_title AS pa_title', + 'pb.page_namespace AS pb_namespace', + 'pb.page_title AS pb_title', + ); + $conds = array( + 'rd_from = pa.page_id', + 'rd_namespace = pb.page_namespace', + 'rd_title = pb.page_title', + 'pb.page_is_redirect' => 1, + ); + + if ( isset( $title ) ) { + $conds['pb.page_namespace'] = $title->getNamespace(); + $conds['pb.page_title'] = $title->getDBkey(); + } + // TODO: support batch querying + + $res = $dbr->select( $tables, $fields, $conds, __METHOD__ ); + + if ( !$res->numRows() ) { + $this->output( "No double redirects found.\n" ); + return; + } + + $jobs = array(); + $n = 0; + foreach ( $res as $row ) { + $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title ); + $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title ); + + $job = new DoubleRedirectJob( $titleA, array( 'reason' => 'maintenance', 'redirTitle' => $titleB->getPrefixedDBkey() ) ); + + if ( !$async ) { + $success = ( $dryrun ? true : $job->run() ); + if ( !$success ) { + $this->error( "Error fixing " . $titleA->getPrefixedText() . ": " . $job->getLastError() . "\n" ); + } + } else { + $jobs[] = $job; + // FIXME: hardcoded constant 10000 copied from DoubleRedirectFixer class + if ( count( $jobs ) > 10000 ) { + $this->queueJobs( $jobs, $dryrun ); + $jobs = array(); + } + } + + if ( ++$n % 100 == 0 ) { + $this->output( "$n...\n" ); + } + } + + if ( count( $jobs ) ) { + $this->queueJobs( $jobs, $dryrun ); + } + $this->output( "$n double redirects processed.\n" ); + } + + protected function queueJobs( $jobs, $dryrun = false ) { + $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" ); + Job::batchInsert( $dryrun ? array() : $jobs ); + } +} + +$maintClass = "FixDoubleRedirects"; +require_once( RUN_MAINTENANCE_IF_MAIN ); -- 2.20.1