cleanupTitles.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Clean up broken, unparseable titles.
  4. *
  5. * Copyright © 2005 Brion Vibber <brion@pobox.com>
  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 Brion Vibber <brion at pobox.com>
  25. * @ingroup Maintenance
  26. */
  27. require_once __DIR__ . '/cleanupTable.inc';
  28. /**
  29. * Maintenance script to clean up broken, unparseable titles.
  30. *
  31. * @ingroup Maintenance
  32. */
  33. class TitleCleanup extends TableCleanup {
  34. public function __construct() {
  35. parent::__construct();
  36. $this->addDescription( 'Script to clean up broken, unparseable titles' );
  37. }
  38. /**
  39. * @param object $row
  40. */
  41. protected function processRow( $row ) {
  42. global $wgContLang;
  43. $display = Title::makeName( $row->page_namespace, $row->page_title );
  44. $verified = $wgContLang->normalize( $display );
  45. $title = Title::newFromText( $verified );
  46. if ( !is_null( $title )
  47. && $title->canExist()
  48. && $title->getNamespace() == $row->page_namespace
  49. && $title->getDBkey() === $row->page_title
  50. ) {
  51. $this->progress( 0 ); // all is fine
  52. return;
  53. }
  54. if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
  55. $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
  56. $this->progress( 0 );
  57. } elseif ( is_null( $title ) ) {
  58. $this->output( "page $row->page_id ($display) is illegal.\n" );
  59. $this->moveIllegalPage( $row );
  60. $this->progress( 1 );
  61. } else {
  62. $this->output( "page $row->page_id ($display) doesn't match self.\n" );
  63. $this->moveInconsistentPage( $row, $title );
  64. $this->progress( 1 );
  65. }
  66. }
  67. /**
  68. * @param string $name
  69. * @return bool
  70. */
  71. protected function fileExists( $name ) {
  72. // XXX: Doesn't actually check for file existence, just presence of image record.
  73. // This is reasonable, since cleanupImages.php only iterates over the image table.
  74. $dbr = $this->getDB( DB_REPLICA );
  75. $row = $dbr->selectRow( 'image', [ 'img_name' ], [ 'img_name' => $name ], __METHOD__ );
  76. return $row !== false;
  77. }
  78. /**
  79. * @param object $row
  80. */
  81. protected function moveIllegalPage( $row ) {
  82. $legal = 'A-Za-z0-9_/\\\\-';
  83. $legalized = preg_replace_callback( "!([^$legal])!",
  84. [ $this, 'hexChar' ],
  85. $row->page_title );
  86. if ( $legalized == '.' ) {
  87. $legalized = '(dot)';
  88. }
  89. if ( $legalized == '_' ) {
  90. $legalized = '(space)';
  91. }
  92. $legalized = 'Broken/' . $legalized;
  93. $title = Title::newFromText( $legalized );
  94. if ( is_null( $title ) ) {
  95. $clean = 'Broken/id:' . $row->page_id;
  96. $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
  97. $title = Title::newFromText( $clean );
  98. } elseif ( $title->exists() ) {
  99. $clean = 'Broken/id:' . $row->page_id;
  100. $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
  101. $title = Title::newFromText( $clean );
  102. }
  103. $dest = $title->getDBkey();
  104. if ( $this->dryrun ) {
  105. $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
  106. "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
  107. } else {
  108. $this->output( "renaming $row->page_id ($row->page_namespace," .
  109. "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
  110. $dbw = $this->getDB( DB_MASTER );
  111. $dbw->update( 'page',
  112. [ 'page_title' => $dest ],
  113. [ 'page_id' => $row->page_id ],
  114. __METHOD__ );
  115. }
  116. }
  117. /**
  118. * @param object $row
  119. * @param Title $title
  120. */
  121. protected function moveInconsistentPage( $row, $title ) {
  122. if ( $title->exists() || $title->getInterwiki() || !$title->canExist() ) {
  123. if ( $title->getInterwiki() || !$title->canExist() ) {
  124. $prior = $title->getPrefixedDBkey();
  125. } else {
  126. $prior = $title->getDBkey();
  127. }
  128. # Old cleanupTitles could move articles there. See bug 23147.
  129. $ns = $row->page_namespace;
  130. if ( $ns < 0 ) {
  131. $ns = 0;
  132. }
  133. # Namespace which no longer exists. Put the page in the main namespace
  134. # since we don't have any idea of the old namespace name. See bug 68501.
  135. if ( !MWNamespace::exists( $ns ) ) {
  136. $ns = 0;
  137. }
  138. $clean = 'Broken/' . $prior;
  139. $verified = Title::makeTitleSafe( $ns, $clean );
  140. if ( !$verified || $verified->exists() ) {
  141. $blah = "Broken/id:" . $row->page_id;
  142. $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
  143. $verified = Title::makeTitleSafe( $ns, $blah );
  144. }
  145. $title = $verified;
  146. }
  147. if ( is_null( $title ) ) {
  148. $this->error( "Something awry; empty title.", true );
  149. }
  150. $ns = $title->getNamespace();
  151. $dest = $title->getDBkey();
  152. if ( $this->dryrun ) {
  153. $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
  154. "'$row->page_title') to ($ns,'$dest')\n" );
  155. } else {
  156. $this->output( "renaming $row->page_id ($row->page_namespace," .
  157. "'$row->page_title') to ($ns,'$dest')\n" );
  158. $dbw = $this->getDB( DB_MASTER );
  159. $dbw->update( 'page',
  160. [
  161. 'page_namespace' => $ns,
  162. 'page_title' => $dest
  163. ],
  164. [ 'page_id' => $row->page_id ],
  165. __METHOD__ );
  166. LinkCache::singleton()->clear();
  167. }
  168. }
  169. }
  170. $maintClass = "TitleCleanup";
  171. require_once RUN_MAINTENANCE_IF_MAIN;