Revision: Remove some unnecessary temporary variables for returns
[mediawiki.git] / maintenance / populateFilearchiveSha1.php
blobe125c0e2dcad8216efa471353e221978cda113c7
1 <?php
2 /**
3 * Optional upgrade script to populate the fa_sha1 field
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 use MediaWiki\MediaWikiServices;
28 /**
29 * Maintenance script to populate the fa_sha1 field.
31 * @ingroup Maintenance
32 * @since 1.21
34 class PopulateFilearchiveSha1 extends LoggedUpdateMaintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Populate the fa_sha1 field from fa_storage_key' );
40 protected function getUpdateKey() {
41 return 'populate fa_sha1';
44 protected function updateSkippedMessage() {
45 return 'fa_sha1 column of filearchive table already populated.';
48 public function doDBUpdates() {
49 $startTime = microtime( true );
50 $dbw = $this->getDB( DB_MASTER );
51 $table = 'filearchive';
52 $conds = [ 'fa_sha1' => '', 'fa_storage_key IS NOT NULL' ];
54 if ( !$dbw->fieldExists( $table, 'fa_sha1', __METHOD__ ) ) {
55 $this->output( "fa_sha1 column does not exist\n\n", true );
57 return false;
60 $this->output( "Populating fa_sha1 field from fa_storage_key\n" );
61 $endId = $dbw->selectField( $table, 'MAX(fa_id)', '', __METHOD__ );
63 $batchSize = $this->getBatchSize();
64 $done = 0;
65 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
67 do {
68 $res = $dbw->select(
69 $table,
70 [ 'fa_id', 'fa_storage_key' ],
71 $conds,
72 __METHOD__,
73 [ 'LIMIT' => $batchSize ]
76 $i = 0;
77 foreach ( $res as $row ) {
78 if ( $row->fa_storage_key == '' ) {
79 // Revision was missing pre-deletion
80 continue;
82 $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
83 $dbw->update( $table,
84 [ 'fa_sha1' => $sha1 ],
85 [ 'fa_id' => $row->fa_id ],
86 __METHOD__
88 $lastId = $row->fa_id;
89 $i++;
92 $done += $i;
93 if ( $i !== $batchSize ) {
94 break;
97 // print status and let replica DBs catch up
98 $this->output( sprintf(
99 "id %d done (up to %d), %5.3f%% \r", $lastId, $endId, $lastId / $endId * 100 ) );
100 $lbFactory->waitForReplication();
101 } while ( true );
103 $processingTime = microtime( true ) - $startTime;
104 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $done, $processingTime ) );
106 // we only updated *some* files, don't log
107 return true;
111 $maintClass = PopulateFilearchiveSha1::class;
112 require_once RUN_MAINTENANCE_IF_MAIN;