cleanupImages.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Clean up broken, unparseable upload filenames.
  4. *
  5. * Copyright © 2005-2006 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 upload filenames.
  30. *
  31. * @ingroup Maintenance
  32. */
  33. class ImageCleanup extends TableCleanup {
  34. protected $defaultParams = [
  35. 'table' => 'image',
  36. 'conds' => [],
  37. 'index' => 'img_name',
  38. 'callback' => 'processRow',
  39. ];
  40. public function __construct() {
  41. parent::__construct();
  42. $this->addDescription( 'Script to clean up broken, unparseable upload filenames' );
  43. }
  44. protected function processRow( $row ) {
  45. global $wgContLang;
  46. $source = $row->img_name;
  47. if ( $source == '' ) {
  48. // Ye olde empty rows. Just kill them.
  49. $this->killRow( $source );
  50. return $this->progress( 1 );
  51. }
  52. $cleaned = $source;
  53. // About half of old bad image names have percent-codes
  54. $cleaned = rawurldecode( $cleaned );
  55. // We also have some HTML entities there
  56. $cleaned = Sanitizer::decodeCharReferences( $cleaned );
  57. // Some are old latin-1
  58. $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
  59. // Many of remainder look like non-normalized unicode
  60. $cleaned = $wgContLang->normalize( $cleaned );
  61. $title = Title::makeTitleSafe( NS_FILE, $cleaned );
  62. if ( is_null( $title ) ) {
  63. $this->output( "page $source ($cleaned) is illegal.\n" );
  64. $safe = $this->buildSafeTitle( $cleaned );
  65. if ( $safe === false ) {
  66. return $this->progress( 0 );
  67. }
  68. $this->pokeFile( $source, $safe );
  69. return $this->progress( 1 );
  70. }
  71. if ( $title->getDBkey() !== $source ) {
  72. $munged = $title->getDBkey();
  73. $this->output( "page $source ($munged) doesn't match self.\n" );
  74. $this->pokeFile( $source, $munged );
  75. return $this->progress( 1 );
  76. }
  77. return $this->progress( 0 );
  78. }
  79. /**
  80. * @param string $name
  81. */
  82. private function killRow( $name ) {
  83. if ( $this->dryrun ) {
  84. $this->output( "DRY RUN: would delete bogus row '$name'\n" );
  85. } else {
  86. $this->output( "deleting bogus row '$name'\n" );
  87. $db = $this->getDB( DB_MASTER );
  88. $db->delete( 'image',
  89. [ 'img_name' => $name ],
  90. __METHOD__ );
  91. }
  92. }
  93. private function filePath( $name ) {
  94. if ( !isset( $this->repo ) ) {
  95. $this->repo = RepoGroup::singleton()->getLocalRepo();
  96. }
  97. return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
  98. }
  99. private function imageExists( $name, $db ) {
  100. return $db->selectField( 'image', '1', [ 'img_name' => $name ], __METHOD__ );
  101. }
  102. private function pageExists( $name, $db ) {
  103. return $db->selectField(
  104. 'page',
  105. '1',
  106. [ 'page_namespace' => NS_FILE, 'page_title' => $name ],
  107. __METHOD__
  108. );
  109. }
  110. private function pokeFile( $orig, $new ) {
  111. $path = $this->filePath( $orig );
  112. if ( !file_exists( $path ) ) {
  113. $this->output( "missing file: $path\n" );
  114. $this->killRow( $orig );
  115. return;
  116. }
  117. $db = $this->getDB( DB_MASTER );
  118. /*
  119. * To prevent key collisions in the update() statements below,
  120. * if the target title exists in the image table, or if both the
  121. * original and target titles exist in the page table, append
  122. * increasing version numbers until the target title exists in
  123. * neither. (See also bug 16916.)
  124. */
  125. $version = 0;
  126. $final = $new;
  127. $conflict = ( $this->imageExists( $final, $db ) ||
  128. ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
  129. while ( $conflict ) {
  130. $this->output( "Rename conflicts with '$final'...\n" );
  131. $version++;
  132. $final = $this->appendTitle( $new, "_$version" );
  133. $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
  134. }
  135. $finalPath = $this->filePath( $final );
  136. if ( $this->dryrun ) {
  137. $this->output( "DRY RUN: would rename $path to $finalPath\n" );
  138. } else {
  139. $this->output( "renaming $path to $finalPath\n" );
  140. // @todo FIXME: Should this use File::move()?
  141. $this->beginTransaction( $db, __METHOD__ );
  142. $db->update( 'image',
  143. [ 'img_name' => $final ],
  144. [ 'img_name' => $orig ],
  145. __METHOD__ );
  146. $db->update( 'oldimage',
  147. [ 'oi_name' => $final ],
  148. [ 'oi_name' => $orig ],
  149. __METHOD__ );
  150. $db->update( 'page',
  151. [ 'page_title' => $final ],
  152. [ 'page_title' => $orig, 'page_namespace' => NS_FILE ],
  153. __METHOD__ );
  154. $dir = dirname( $finalPath );
  155. if ( !file_exists( $dir ) ) {
  156. if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
  157. $this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
  158. $this->rollbackTransaction( $db, __METHOD__ );
  159. return;
  160. }
  161. }
  162. if ( rename( $path, $finalPath ) ) {
  163. $this->commitTransaction( $db, __METHOD__ );
  164. } else {
  165. $this->error( "RENAME FAILED" );
  166. $this->rollbackTransaction( $db, __METHOD__ );
  167. }
  168. }
  169. }
  170. private function appendTitle( $name, $suffix ) {
  171. return preg_replace( '/^(.*)(\..*?)$/',
  172. "\\1$suffix\\2", $name );
  173. }
  174. private function buildSafeTitle( $name ) {
  175. $x = preg_replace_callback(
  176. '/([^' . Title::legalChars() . ']|~)/',
  177. [ $this, 'hexChar' ],
  178. $name );
  179. $test = Title::makeTitleSafe( NS_FILE, $x );
  180. if ( is_null( $test ) || $test->getDBkey() !== $x ) {
  181. $this->error( "Unable to generate safe title from '$name', got '$x'" );
  182. return false;
  183. }
  184. return $x;
  185. }
  186. }
  187. $maintClass = "ImageCleanup";
  188. require_once RUN_MAINTENANCE_IF_MAIN;