cleanupCaps.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Clean up broken page links when somebody turns on $wgCapitalLinks.
  4. *
  5. * Usage: php cleanupCaps.php [--dry-run]
  6. * Options:
  7. * --dry-run don't actually try moving them
  8. *
  9. * Copyright © 2005 Brion Vibber <brion@pobox.com>
  10. * https://www.mediawiki.org/
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. * http://www.gnu.org/copyleft/gpl.html
  26. *
  27. * @file
  28. * @author Brion Vibber <brion at pobox.com>
  29. * @ingroup Maintenance
  30. */
  31. require_once __DIR__ . '/cleanupTable.inc';
  32. /**
  33. * Maintenance script to clean up broken page links when somebody turns
  34. * on or off $wgCapitalLinks.
  35. *
  36. * @ingroup Maintenance
  37. */
  38. class CapsCleanup extends TableCleanup {
  39. private $user;
  40. private $namespace;
  41. public function __construct() {
  42. parent::__construct();
  43. $this->addDescription( 'Script to cleanup capitalization' );
  44. $this->addOption( 'namespace', 'Namespace number to run caps cleanup on', false, true );
  45. }
  46. public function execute() {
  47. $this->user = User::newSystemUser( 'Conversion script', [ 'steal' => true ] );
  48. $this->namespace = intval( $this->getOption( 'namespace', 0 ) );
  49. if ( MWNamespace::isCapitalized( $this->namespace ) ) {
  50. $this->output( "Will be moving pages to first letter capitalized titles" );
  51. $callback = 'processRowToUppercase';
  52. } else {
  53. $this->output( "Will be moving pages to first letter lowercase titles" );
  54. $callback = 'processRowToLowercase';
  55. }
  56. $this->dryrun = $this->hasOption( 'dry-run' );
  57. $this->runTable( [
  58. 'table' => 'page',
  59. 'conds' => [ 'page_namespace' => $this->namespace ],
  60. 'index' => 'page_id',
  61. 'callback' => $callback ] );
  62. }
  63. protected function processRowToUppercase( $row ) {
  64. global $wgContLang;
  65. $current = Title::makeTitle( $row->page_namespace, $row->page_title );
  66. $display = $current->getPrefixedText();
  67. $lower = $row->page_title;
  68. $upper = $wgContLang->ucfirst( $row->page_title );
  69. if ( $upper == $lower ) {
  70. $this->output( "\"$display\" already uppercase.\n" );
  71. return $this->progress( 0 );
  72. }
  73. $target = Title::makeTitle( $row->page_namespace, $upper );
  74. if ( $target->exists() ) {
  75. // Prefix "CapsCleanup" to bypass the conflict
  76. $target = Title::newFromText( __CLASS__ . '/' . $display );
  77. }
  78. $ok = $this->movePage(
  79. $current,
  80. $target,
  81. 'Converting page title to first-letter uppercase',
  82. false
  83. );
  84. if ( $ok ) {
  85. $this->progress( 1 );
  86. if ( $row->page_namespace == $this->namespace ) {
  87. $talk = $target->getTalkPage();
  88. $row->page_namespace = $talk->getNamespace();
  89. if ( $talk->exists() ) {
  90. return $this->processRowToUppercase( $row );
  91. }
  92. }
  93. }
  94. return $this->progress( 0 );
  95. }
  96. protected function processRowToLowercase( $row ) {
  97. global $wgContLang;
  98. $current = Title::makeTitle( $row->page_namespace, $row->page_title );
  99. $display = $current->getPrefixedText();
  100. $upper = $row->page_title;
  101. $lower = $wgContLang->lcfirst( $row->page_title );
  102. if ( $upper == $lower ) {
  103. $this->output( "\"$display\" already lowercase.\n" );
  104. return $this->progress( 0 );
  105. }
  106. $target = Title::makeTitle( $row->page_namespace, $lower );
  107. if ( $target->exists() ) {
  108. $targetDisplay = $target->getPrefixedText();
  109. $this->output( "\"$display\" skipped; \"$targetDisplay\" already exists\n" );
  110. return $this->progress( 0 );
  111. }
  112. $ok = $this->movePage( $current, $target, 'Converting page titles to lowercase', true );
  113. if ( $ok === true ) {
  114. $this->progress( 1 );
  115. if ( $row->page_namespace == $this->namespace ) {
  116. $talk = $target->getTalkPage();
  117. $row->page_namespace = $talk->getNamespace();
  118. if ( $talk->exists() ) {
  119. return $this->processRowToLowercase( $row );
  120. }
  121. }
  122. }
  123. return $this->progress( 0 );
  124. }
  125. /**
  126. * @param Title $current
  127. * @param Title $target
  128. * @param string $reason
  129. * @param bool $createRedirect
  130. * @return bool Success
  131. */
  132. private function movePage( Title $current, Title $target, $reason, $createRedirect ) {
  133. $display = $current->getPrefixedText();
  134. $targetDisplay = $target->getPrefixedText();
  135. if ( $this->dryrun ) {
  136. $this->output( "\"$display\" -> \"$targetDisplay\": DRY RUN, NOT MOVED\n" );
  137. $ok = 'OK';
  138. } else {
  139. $mp = new MovePage( $current, $target );
  140. $status = $mp->move( $this->user, $reason, $createRedirect );
  141. $ok = $status->isOK() ? 'OK' : $status->getWikiText( false, false, 'en' );
  142. $this->output( "\"$display\" -> \"$targetDisplay\": $ok\n" );
  143. }
  144. return $ok === 'OK';
  145. }
  146. }
  147. $maintClass = "CapsCleanup";
  148. require_once RUN_MAINTENANCE_IF_MAIN;