PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/phase3/maintenance/refreshImageMetadata.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 207 lines | 133 code | 28 blank | 46 comment | 24 complexity | f3eb645ef7019b9015014c954dd7f72a MD5 | raw file
  1. <?php
  2. /**
  3. * Script to refresh image metadata fields. See also rebuildImages.php
  4. *
  5. * Usage: php refreshImageMetadata.php
  6. *
  7. * Copyright © 2011 Brian Wolff
  8. * http://www.mediawiki.org/
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23. * http://www.gnu.org/copyleft/gpl.html
  24. *
  25. * @file
  26. * @author Brian Wolff
  27. * @ingroup maintenance
  28. */
  29. require_once( dirname( __FILE__ ) . '/Maintenance.php' );
  30. class RefreshImageMetadata extends Maintenance {
  31. /**
  32. * @var DatabaseBase
  33. */
  34. protected $dbw;
  35. function __construct() {
  36. parent::__construct();
  37. $this->mDescription = 'Script to update image metadata records';
  38. $this->setBatchSize( 200 );
  39. $this->addOption( 'force', 'Reload metadata from file even if the metadata looks ok', false, false, 'f' );
  40. $this->addOption( 'broken-only', 'Only fix really broken records, leave old but still compatible records alone.' );
  41. $this->addOption( 'verbose', 'Output extra information about each upgraded/non-upgraded file.', false, false, 'v' );
  42. $this->addOption( 'start', 'Name of file to start with', false, true );
  43. $this->addOption( 'end', 'Name of file to end with', false, true );
  44. $this->addOption( 'mime', '(Inefficient!) Only refresh files with this mime type. Can accept wild-card image/*' , false, true );
  45. $this->addOption( 'metadata-contains', '(Inefficient!) Only refresh files where the img_metadata field contains this string. Can be used if its known a specific property was being extracted incorrectly.', false, true );
  46. }
  47. public function execute() {
  48. $force = $this->hasOption( 'force' );
  49. $brokenOnly = $this->hasOption( 'broken-only' );
  50. $verbose = $this->hasOption( 'verbose' );
  51. $start = $this->getOption( 'start', false );
  52. $this->setupParameters( $force, $brokenOnly );
  53. $upgraded = 0;
  54. $leftAlone = 0;
  55. $error = 0;
  56. $dbw = wfGetDB( DB_MASTER );
  57. if ( $this->mBatchSize <= 0 ) {
  58. $this->error( "Batch size is too low...", 12 );
  59. }
  60. $repo = RepoGroup::singleton()->getLocalRepo();
  61. $conds = $this->getConditions( $dbw );
  62. // For the WHERE img_name > 'foo' condition that comes after doing a batch
  63. $conds2 = array();
  64. if ( $start !== false ) {
  65. $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
  66. }
  67. $options = array(
  68. 'LIMIT' => $this->mBatchSize,
  69. 'ORDER BY' => 'img_name ASC',
  70. );
  71. do {
  72. $res = $dbw->select(
  73. 'image',
  74. '*',
  75. array_merge( $conds, $conds2 ),
  76. __METHOD__,
  77. $options
  78. );
  79. if ( $res->numRows() > 0 ) {
  80. $row1 = $res->current();
  81. $this->output( "Processing next {$this->mBatchSize} rows starting with {$row1->img_name}.\n");
  82. $res->rewind();
  83. } else {
  84. $this->error( "No images to process.", 4 );
  85. }
  86. foreach ( $res as $row ) {
  87. $file = $repo->newFileFromRow( $row );
  88. if ( $file->getUpgraded() ) {
  89. // File was upgraded.
  90. $upgraded++;
  91. $newLength = strlen( $file->getMetadata() );
  92. $oldLength = strlen( $row->img_metadata );
  93. if ( $newLength < $oldLength - 5 ) {
  94. // If after updating, the metadata is smaller then
  95. // what it was before, that's probably not a good thing
  96. // because we extract more data with time, not less.
  97. // Thus this probably indicates an error of some sort,
  98. // or at the very least is suspicious. Have the - 5 just
  99. // to weed out any inconsequential changes.
  100. $error++;
  101. $this->output( "Warning: File:{$row->img_name} used to have " .
  102. "$oldLength bytes of metadata but now has $newLength bytes.\n" );
  103. } elseif ( $verbose ) {
  104. $this->output("Refreshed File:{$row->img_name}.\n" );
  105. }
  106. } else {
  107. $leftAlone++;
  108. if ( $force ) {
  109. $file->upgradeRow();
  110. $newLength = strlen( $file->getMetadata() );
  111. $oldLength = strlen( $row->img_metadata );
  112. if ( $newLength < $oldLength - 5 ) {
  113. $error++;
  114. $this->output( "Warning: File:{$row->img_name} used to have " .
  115. "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n" );
  116. }
  117. if ( $verbose ) {
  118. $this->output("Forcibly refreshed File:{$row->img_name}.\n" );
  119. }
  120. }
  121. else {
  122. if ( $verbose ) {
  123. $this->output( "Skipping File:{$row->img_name}.\n" );
  124. }
  125. }
  126. }
  127. }
  128. $conds2 = array( 'img_name > ' . $dbw->addQuotes( $row->img_name ) );
  129. wfWaitForSlaves();
  130. } while( $res->numRows() === $this->mBatchSize );
  131. $total = $upgraded + $leftAlone;
  132. if ( $force ) {
  133. $this->output( "\nFinished refreshing file metadata for $total files. $upgraded needed to be refreshed, $leftAlone did not need to be but were refreshed anyways, and $error refreshes were suspicious.\n" );
  134. } else {
  135. $this->output( "\nFinished refreshing file metadata for $total files. $upgraded were refreshed, $leftAlone were already up to date, and $error refreshes were suspicious.\n" );
  136. }
  137. }
  138. /**
  139. * @param $dbw DatabaseBase
  140. * @return array
  141. */
  142. function getConditions( $dbw ) {
  143. $conds = array();
  144. $end = $this->getOption( 'end', false );
  145. $mime = $this->getOption( 'mime', false );
  146. $like = $this->getOption( 'metadata-contains', false );
  147. if ( $end !== false ) {
  148. $conds[] = 'img_name <= ' . $dbw->addQuotes( $end ) ;
  149. }
  150. if ( $mime !== false ) {
  151. list( $major, $minor ) = File::splitMime( $mime );
  152. $conds['img_major_mime'] = $major;
  153. if ( $minor !== '*' ) {
  154. $conds['img_minor_mime'] = $minor;
  155. }
  156. }
  157. if ( $like ) {
  158. $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
  159. }
  160. return $conds;
  161. }
  162. /**
  163. * @param $force bool
  164. * @param $brokenOnly bool
  165. */
  166. function setupParameters( $force, $brokenOnly ) {
  167. global $wgUpdateCompatibleMetadata;
  168. if ( $brokenOnly ) {
  169. $wgUpdateCompatibleMetadata = false;
  170. } else {
  171. $wgUpdateCompatibleMetadata = true;
  172. }
  173. if ( $brokenOnly && $force ) {
  174. $this->error( 'Cannot use --broken-only and --force together. ', 2 );
  175. }
  176. }
  177. }
  178. $maintClass = 'RefreshImageMetadata';
  179. require_once( RUN_MAINTENANCE_IF_MAIN );