rebuildImages.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Update image metadata records.
  4. *
  5. * Usage: php rebuildImages.php [--missing] [--dry-run]
  6. * Options:
  7. * --missing Crawl the uploads dir for images without records, and
  8. * add them only.
  9. *
  10. * Copyright © 2005 Brion Vibber <brion@pobox.com>
  11. * https://www.mediawiki.org/
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  26. * http://www.gnu.org/copyleft/gpl.html
  27. *
  28. * @file
  29. * @author Brion Vibber <brion at pobox.com>
  30. * @ingroup Maintenance
  31. */
  32. require_once __DIR__ . '/Maintenance.php';
  33. /**
  34. * Maintenance script to update image metadata records.
  35. *
  36. * @ingroup Maintenance
  37. */
  38. class ImageBuilder extends Maintenance {
  39. /**
  40. * @var Database
  41. */
  42. protected $dbw;
  43. function __construct() {
  44. parent::__construct();
  45. global $wgUpdateCompatibleMetadata;
  46. // make sure to update old, but compatible img_metadata fields.
  47. $wgUpdateCompatibleMetadata = true;
  48. $this->addDescription( 'Script to update image metadata records' );
  49. $this->addOption( 'missing', 'Check for files without associated database record' );
  50. $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
  51. }
  52. public function execute() {
  53. $this->dbw = $this->getDB( DB_MASTER );
  54. $this->dryrun = $this->hasOption( 'dry-run' );
  55. if ( $this->dryrun ) {
  56. $GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
  57. }
  58. if ( $this->hasOption( 'missing' ) ) {
  59. $this->crawlMissing();
  60. } else {
  61. $this->build();
  62. }
  63. }
  64. /**
  65. * @return FileRepo
  66. */
  67. function getRepo() {
  68. if ( !isset( $this->repo ) ) {
  69. $this->repo = RepoGroup::singleton()->getLocalRepo();
  70. }
  71. return $this->repo;
  72. }
  73. function build() {
  74. $this->buildImage();
  75. $this->buildOldImage();
  76. }
  77. function init( $count, $table ) {
  78. $this->processed = 0;
  79. $this->updated = 0;
  80. $this->count = $count;
  81. $this->startTime = microtime( true );
  82. $this->table = $table;
  83. }
  84. function progress( $updated ) {
  85. $this->updated += $updated;
  86. $this->processed++;
  87. if ( $this->processed % 100 != 0 ) {
  88. return;
  89. }
  90. $portion = $this->processed / $this->count;
  91. $updateRate = $this->updated / $this->processed;
  92. $now = microtime( true );
  93. $delta = $now - $this->startTime;
  94. $estimatedTotalTime = $delta / $portion;
  95. $eta = $this->startTime + $estimatedTotalTime;
  96. $rate = $this->processed / $delta;
  97. $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
  98. wfTimestamp( TS_DB, intval( $now ) ),
  99. $portion * 100.0,
  100. $this->table,
  101. wfTimestamp( TS_DB, intval( $eta ) ),
  102. $this->processed,
  103. $this->count,
  104. $rate,
  105. $updateRate * 100.0 ) );
  106. flush();
  107. }
  108. function buildTable( $table, $key, $callback ) {
  109. $count = $this->dbw->selectField( $table, 'count(*)', '', __METHOD__ );
  110. $this->init( $count, $table );
  111. $this->output( "Processing $table...\n" );
  112. $result = $this->getDB( DB_REPLICA )->select( $table, '*', [], __METHOD__ );
  113. foreach ( $result as $row ) {
  114. $update = call_user_func( $callback, $row, null );
  115. if ( $update ) {
  116. $this->progress( 1 );
  117. } else {
  118. $this->progress( 0 );
  119. }
  120. }
  121. $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
  122. }
  123. function buildImage() {
  124. $callback = [ $this, 'imageCallback' ];
  125. $this->buildTable( 'image', 'img_name', $callback );
  126. }
  127. function imageCallback( $row, $copy ) {
  128. // Create a File object from the row
  129. // This will also upgrade it
  130. $file = $this->getRepo()->newFileFromRow( $row );
  131. return $file->getUpgraded();
  132. }
  133. function buildOldImage() {
  134. $this->buildTable( 'oldimage', 'oi_archive_name', [ $this, 'oldimageCallback' ] );
  135. }
  136. function oldimageCallback( $row, $copy ) {
  137. // Create a File object from the row
  138. // This will also upgrade it
  139. if ( $row->oi_archive_name == '' ) {
  140. $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
  141. return false;
  142. }
  143. $file = $this->getRepo()->newFileFromRow( $row );
  144. return $file->getUpgraded();
  145. }
  146. function crawlMissing() {
  147. $this->getRepo()->enumFiles( [ $this, 'checkMissingImage' ] );
  148. }
  149. function checkMissingImage( $fullpath ) {
  150. $filename = wfBaseName( $fullpath );
  151. $row = $this->dbw->selectRow( 'image',
  152. [ 'img_name' ],
  153. [ 'img_name' => $filename ],
  154. __METHOD__ );
  155. if ( !$row ) { // file not registered
  156. $this->addMissingImage( $filename, $fullpath );
  157. }
  158. }
  159. function addMissingImage( $filename, $fullpath ) {
  160. global $wgContLang;
  161. $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
  162. $altname = $wgContLang->checkTitleEncoding( $filename );
  163. if ( $altname != $filename ) {
  164. if ( $this->dryrun ) {
  165. $filename = $altname;
  166. $this->output( "Estimating transcoding... $altname\n" );
  167. } else {
  168. # @todo FIXME: create renameFile()
  169. $filename = $this->renameFile( $filename );
  170. }
  171. }
  172. if ( $filename == '' ) {
  173. $this->output( "Empty filename for $fullpath\n" );
  174. return;
  175. }
  176. if ( !$this->dryrun ) {
  177. $file = wfLocalFile( $filename );
  178. if ( !$file->recordUpload(
  179. '',
  180. '(recovered file, missing upload log entry)',
  181. '',
  182. '',
  183. '',
  184. false,
  185. $timestamp
  186. ) ) {
  187. $this->output( "Error uploading file $fullpath\n" );
  188. return;
  189. }
  190. }
  191. $this->output( $fullpath . "\n" );
  192. }
  193. }
  194. $maintClass = 'ImageBuilder';
  195. require_once RUN_MAINTENANCE_IF_MAIN;