fixDoubleRedirects.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Fix double redirects.
  4. *
  5. * Copyright © 2011 Ilmari Karonen <nospam@vyznev.net>
  6. * https://www.mediawiki.org/
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. * @author Ilmari Karonen <nospam@vyznev.net>
  25. * @ingroup Maintenance
  26. */
  27. require_once __DIR__ . '/Maintenance.php';
  28. /**
  29. * Maintenance script that fixes double redirects.
  30. *
  31. * @ingroup Maintenance
  32. */
  33. class FixDoubleRedirects extends Maintenance {
  34. public function __construct() {
  35. parent::__construct();
  36. $this->addDescription( 'Script to fix double redirects' );
  37. $this->addOption( 'async', 'Don\'t fix anything directly, just queue the jobs' );
  38. $this->addOption( 'title', 'Fix only redirects pointing to this page', false, true );
  39. $this->addOption( 'dry-run', 'Perform a dry run, fix nothing' );
  40. }
  41. public function execute() {
  42. $async = $this->getOption( 'async', false );
  43. $dryrun = $this->getOption( 'dry-run', false );
  44. if ( $this->hasOption( 'title' ) ) {
  45. $title = Title::newFromText( $this->getOption( 'title' ) );
  46. if ( !$title || !$title->isRedirect() ) {
  47. $this->error( $title->getPrefixedText() . " is not a redirect!\n", true );
  48. }
  49. } else {
  50. $title = null;
  51. }
  52. $dbr = $this->getDB( DB_REPLICA );
  53. // See also SpecialDoubleRedirects
  54. $tables = [
  55. 'redirect',
  56. 'pa' => 'page',
  57. 'pb' => 'page',
  58. ];
  59. $fields = [
  60. 'pa.page_namespace AS pa_namespace',
  61. 'pa.page_title AS pa_title',
  62. 'pb.page_namespace AS pb_namespace',
  63. 'pb.page_title AS pb_title',
  64. ];
  65. $conds = [
  66. 'rd_from = pa.page_id',
  67. 'rd_namespace = pb.page_namespace',
  68. 'rd_title = pb.page_title',
  69. 'rd_interwiki IS NULL OR rd_interwiki = ' . $dbr->addQuotes( '' ), // bug 40352
  70. 'pb.page_is_redirect' => 1,
  71. ];
  72. if ( $title != null ) {
  73. $conds['pb.page_namespace'] = $title->getNamespace();
  74. $conds['pb.page_title'] = $title->getDBkey();
  75. }
  76. // TODO: support batch querying
  77. $res = $dbr->select( $tables, $fields, $conds, __METHOD__ );
  78. if ( !$res->numRows() ) {
  79. $this->output( "No double redirects found.\n" );
  80. return;
  81. }
  82. $jobs = [];
  83. $processedTitles = "\n";
  84. $n = 0;
  85. foreach ( $res as $row ) {
  86. $titleA = Title::makeTitle( $row->pa_namespace, $row->pa_title );
  87. $titleB = Title::makeTitle( $row->pb_namespace, $row->pb_title );
  88. $processedTitles .= "* [[$titleA]]\n";
  89. $job = new DoubleRedirectJob( $titleA, [
  90. 'reason' => 'maintenance',
  91. 'redirTitle' => $titleB->getPrefixedDBkey()
  92. ] );
  93. if ( !$async ) {
  94. $success = ( $dryrun ? true : $job->run() );
  95. if ( !$success ) {
  96. $this->error( "Error fixing " . $titleA->getPrefixedText()
  97. . ": " . $job->getLastError() . "\n" );
  98. }
  99. } else {
  100. $jobs[] = $job;
  101. // @todo FIXME: Hardcoded constant 10000 copied from DoubleRedirectJob class
  102. if ( count( $jobs ) > 10000 ) {
  103. $this->queueJobs( $jobs, $dryrun );
  104. $jobs = [];
  105. }
  106. }
  107. if ( ++$n % 100 == 0 ) {
  108. $this->output( "$n...\n" );
  109. }
  110. }
  111. if ( count( $jobs ) ) {
  112. $this->queueJobs( $jobs, $dryrun );
  113. }
  114. $this->output( "$n double redirects processed" . $processedTitles . "\n" );
  115. }
  116. protected function queueJobs( $jobs, $dryrun = false ) {
  117. $this->output( "Queuing batch of " . count( $jobs ) . " double redirects.\n" );
  118. JobQueueGroup::singleton()->push( $dryrun ? [] : $jobs );
  119. }
  120. }
  121. $maintClass = "FixDoubleRedirects";
  122. require_once RUN_MAINTENANCE_IF_MAIN;