r81408 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81407‎ | r81408 | r81409 >
Date:19:25, 2 February 2011
Author:vyznev
Status:ok (Comments)
Tags:
Comment:
Add a maintenance script to fix double redirects (and corresponding edit summary messages). Still lacks some features like batch querying, but basically it works.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/languages/messages/MessagesEn.php (modified) (history)
  • /trunk/phase3/languages/messages/MessagesFi.php (modified) (history)
  • /trunk/phase3/languages/messages/MessagesQqq.php (modified) (history)
  • /trunk/phase3/maintenance/fixDoubleRedirects.php (added) (history)

Diff [purge]

Index: trunk/phase3/maintenance/fixDoubleRedirects.php
@@ -0,0 +1,120 @@
 2+<?php
 3+/**
 4+ * Script to fix double redirects.
 5+ *
 6+ * Copyright (C) 2010 Ilmari Karonen <nospam@vyznev.net>
 7+ * http://www.mediawiki.org/
 8+ *
 9+ * This program is free software; you can redistribute it and/or modify
 10+ * it under the terms of the GNU General Public License as published by
 11+ * the Free Software Foundation; either version 2 of the License, or
 12+ * (at your option) any later version.
 13+ *
 14+ * This program is distributed in the hope that it will be useful,
 15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 17+ * GNU General Public License for more details.
 18+ *
 19+ * You should have received a copy of the GNU General Public License along
 20+ * with this program; if not, write to the Free Software Foundation, Inc.,
 21+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 22+ * http://www.gnu.org/copyleft/gpl.html
 23+ *
 24+ * @file
 25+ * @author Ilmari Karonen <nospam@vyznev.net>
 26+ * @ingroup Maintenance
 27+ */
 28+
 29+require_once( dirname( __FILE__ ) . '/Maintenance.php' );
 30+
 31+class FixDoubleRedirects extends Maintenance {
 32+ public function __construct() {
 33+ parent::__construct();
 34+ $this->mDescription = "Script to fix double redirects";
 35+ $this->addOption( 'async', 'Don\'t fix anything directly, just add queue the jobs' );
 36+ $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
 37+ $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
 38+ }
 39+
 40+ public function execute() {
 41+ $async = $this->getOption( 'async', false );
 42+ $dryrun = $this->getOption( 'dry-run', false );
 43+ $title = $this->getOption( 'title' );
 44+
 45+ if ( isset( $title ) ) {
 46+ $title = Title::newFromText( $title );
 47+ if ( !$title || !$title->isRedirect() ) {
 48+ $this->error( $title->getPrefixedText() . " is not a redirect!\n", true );
 49+ }
 50+ }
 51+
 52+ $dbr = wfGetDB( DB_SLAVE );
 53+
 54+ $tables = array( 'redirect', 'pa' => 'page', 'pb' => 'page' );
 55+ $fields = array(
 56+ 'pa.page_namespace AS pa_namespace',
 57+ 'pa.page_title AS pa_title',
 58+ 'pb.page_namespace AS pb_namespace',
 59+ 'pb.page_title AS pb_title',
 60+ );
 61+ $conds = array(
 62+ 'rd_from = pa.page_id',
 63+ 'rd_namespace = pb.page_namespace',
 64+ 'rd_title = pb.page_title',
 65+ 'pb.page_is_redirect' => 1,
 66+ );
 67+
 68+ if ( isset( $title ) ) {
 69+ $conds['pb.page_namespace'] = $title->getNamespace();
 70+ $conds['pb.page_title'] = $title->getDBkey();
 71+ }
 72+ // TODO: support batch querying
 73+
 74+ $res = $dbr->select( $tables, $fields, $conds, __METHOD__ );
 75+
 76+ if ( !$res->numRows() ) {
 77+ $this->output( "No double redirects found.\n" );
 78+ return;
 79+ }
 80+
 81+ $jobs = array();
 82+ $n = 0;
 83+ foreach ( $res as $row ) {
 84+ $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
 85+ $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
 86+
 87+ $job = new DoubleRedirectJob( $titleA, array( 'reason' => 'maintenance', 'redirTitle' => $titleB->getPrefixedDBkey() ) );
 88+
 89+ if ( !$async ) {
 90+ $success = ( $dryrun ? true : $job->run() );
 91+ if ( !$success ) {
 92+ $this->error( "Error fixing " . $titleA->getPrefixedText() . ": " . $job->getLastError() . "\n" );
 93+ }
 94+ } else {
 95+ $jobs[] = $job;
 96+ // FIXME: hardcoded constant 10000 copied from DoubleRedirectFixer class
 97+ if ( count( $jobs ) > 10000 ) {
 98+ $this->queueJobs( $jobs, $dryrun );
 99+ $jobs = array();
 100+ }
 101+ }
 102+
 103+ if ( ++$n % 100 == 0 ) {
 104+ $this->output( "$n...\n" );
 105+ }
 106+ }
 107+
 108+ if ( count( $jobs ) ) {
 109+ $this->queueJobs( $jobs, $dryrun );
 110+ }
 111+ $this->output( "$n double redirects processed.\n" );
 112+ }
 113+
 114+ protected function queueJobs( $jobs, $dryrun = false ) {
 115+ $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
 116+ Job::batchInsert( $dryrun ? array() : $jobs );
 117+ }
 118+}
 119+
 120+$maintClass = "FixDoubleRedirects";
 121+require_once( RUN_MAINTENANCE_IF_MAIN );
Property changes on: trunk/phase3/maintenance/fixDoubleRedirects.php
___________________________________________________________________
Added: svn:eol-style
1122 + native
Index: trunk/phase3/languages/messages/MessagesQqq.php
@@ -2050,6 +2050,7 @@
20512051 'doubleredirects' => 'Name of [[Special:DoubleRedirects]] displayed in [[Special:SpecialPages]]',
20522052 'doubleredirectstext' => 'Shown on top of [[Special:Doubleredirects]]',
20532053 '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}}.',
 2054+'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}}.',
20542055 '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}}.",
20552056
20562057 'brokenredirects' => 'Name of [[Special:BrokenRedirects]] displayed in [[Special:SpecialPages]]',
Index: trunk/phase3/languages/messages/MessagesEn.php
@@ -2435,6 +2435,7 @@
24362436 <del>Crossed out</del> entries have been solved.',
24372437 'double-redirect-fixed-move' => '[[$1]] has been moved.
24382438 It now redirects to [[$2]].',
 2439+'double-redirect-fixed-maintenance' => 'Fixing double redirect from [[$1]] to [[$2]].',
24392440 'double-redirect-fixer' => 'Redirect fixer',
24402441
24412442 'brokenredirects' => 'Broken redirects',
Index: trunk/phase3/languages/messages/MessagesFi.php
@@ -1871,6 +1871,7 @@
18721872 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.
18731873 <del>Yliviivatut</del> kohteet on korjattu.',
18741874 'double-redirect-fixed-move' => '[[$1]] on siirretty, ja se ohjaa nyt sivulle [[$2]]',
 1875+'double-redirect-fixed-maintenance' => 'Korjataan kaksinkertainen ohjaus sivulta [[$1]] sivulle [[$2]]',
18751876 'double-redirect-fixer' => 'Ohjausten korjaaja',
18761877
18771878 'brokenredirects' => 'Virheelliset ohjaukset',
Index: trunk/phase3/RELEASE-NOTES
@@ -58,6 +58,7 @@
5959 * (bug 26285) Extensions will be automatically generated on upload if the user
6060 specified a filename without extension.
6161 * (bug 26851) Special:UserRights now allows to prefill the reason field
 62+* New maintenance script to fix double redirects (maintenance/fixDoubleRedirects.php)
6263
6364 === Bug fixes in 1.18 ===
6465 * (bug 23119) WikiError class and subclasses are now marked as deprecated

Follow-up revisions

RevisionCommit summaryAuthorDate
r81409followup r81408: fix class name in commentvyznev19:33, 2 February 2011
r81416followup r81408: fix option desc and year in copyright statementvyznev21:30, 2 February 2011
r81477Follow-up r81408: Add new message key to maintenance fileraymond19:22, 3 February 2011

Comments

#Comment by Nikerabbit (talk | contribs)   21:09, 2 February 2011

The log message wording doesn't follow general pattern. Should perhaps check that title[AB] are not null.

#Comment by Ilmari Karonen (talk | contribs)   21:37, 2 February 2011

There's a pattern? If either of the title objects are null, we have bigger problems than double redirects (like page table entries with invalid titles). But I might consider adding a check anyway if/when I commit another revision of this script.

#Comment by Siebrand (talk | contribs)   23:10, 2 February 2011

Please update maintenance/languages/messages.inc when adding core messages.

#Comment by Raymond (talk | contribs)   19:23, 3 February 2011

Done with r81477

Status & tagging log