populateFilearchiveSha1.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Optional upgrade script to populate the fa_sha1 field
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script to populate the fa_sha1 field.
  26. *
  27. * @ingroup Maintenance
  28. * @since 1.21
  29. */
  30. class PopulateFilearchiveSha1 extends LoggedUpdateMaintenance {
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription( 'Populate the fa_sha1 field from fa_storage_key' );
  34. }
  35. protected function getUpdateKey() {
  36. return 'populate fa_sha1';
  37. }
  38. protected function updateSkippedMessage() {
  39. return 'fa_sha1 column of filearchive table already populated.';
  40. }
  41. public function doDBUpdates() {
  42. $startTime = microtime( true );
  43. $dbw = $this->getDB( DB_MASTER );
  44. $table = 'filearchive';
  45. $conds = [ 'fa_sha1' => '', 'fa_storage_key IS NOT NULL' ];
  46. if ( !$dbw->fieldExists( $table, 'fa_sha1', __METHOD__ ) ) {
  47. $this->output( "fa_sha1 column does not exist\n\n", true );
  48. return false;
  49. }
  50. $this->output( "Populating fa_sha1 field from fa_storage_key\n" );
  51. $endId = $dbw->selectField( $table, 'MAX(fa_id)', false, __METHOD__ );
  52. $batchSize = $this->mBatchSize;
  53. $done = 0;
  54. do {
  55. $res = $dbw->select(
  56. $table,
  57. [ 'fa_id', 'fa_storage_key' ],
  58. $conds,
  59. __METHOD__,
  60. [ 'LIMIT' => $batchSize ]
  61. );
  62. $i = 0;
  63. foreach ( $res as $row ) {
  64. if ( $row->fa_storage_key == '' ) {
  65. // Revision was missing pre-deletion
  66. continue;
  67. }
  68. $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
  69. $dbw->update( $table,
  70. [ 'fa_sha1' => $sha1 ],
  71. [ 'fa_id' => $row->fa_id ],
  72. __METHOD__
  73. );
  74. $lastId = $row->fa_id;
  75. $i++;
  76. }
  77. $done += $i;
  78. if ( $i !== $batchSize ) {
  79. break;
  80. }
  81. // print status and let replica DBs catch up
  82. $this->output( sprintf(
  83. "id %d done (up to %d), %5.3f%% \r", $lastId, $endId, $lastId / $endId * 100 ) );
  84. wfWaitForSlaves();
  85. } while ( true );
  86. $processingTime = microtime( true ) - $startTime;
  87. $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $done, $processingTime ) );
  88. return true; // we only updated *some* files, don't log
  89. }
  90. }
  91. $maintClass = "PopulateFilearchiveSha1";
  92. require_once RUN_MAINTENANCE_IF_MAIN;