Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / maintenance / populateFilearchiveSha1.php
blobc579d4fcd949cbc3d9bd8ede8d027fc5f8fa3d1a
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 dirname( __FILE__ ) . '/Maintenance.php';
26 /**
27 * Maintenance script to populate the fa_sha1 field.
29 * @ingroup Maintenance
30 * @since 1.21
32 class PopulateFilearchiveSha1 extends LoggedUpdateMaintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Populate the fa_sha1 field from fa_storage_key";
38 protected function getUpdateKey() {
39 return 'populate fa_sha1';
42 protected function updateSkippedMessage() {
43 return 'fa_sha1 column of filearchive table already populated.';
46 public function doDBUpdates() {
47 $startTime = microtime( true );
48 $dbw = wfGetDB( DB_MASTER );
49 $table = 'filearchive';
50 $conds = array( 'fa_sha1' => '', 'fa_storage_key IS NOT NULL' );
52 if ( !$dbw->fieldExists( $table, 'fa_sha1', __METHOD__ ) ) {
53 $this->output( "fa_sha1 column does not exist\n\n", true );
54 return false;
57 $this->output( "Populating fa_sha1 field from fa_storage_key\n" );
58 $endId = $dbw->selectField( $table, 'MAX(fa_id)', false, __METHOD__ );
60 $batchSize = $this->mBatchSize;
61 $done = 0;
63 do {
64 $res = $dbw->select(
65 $table,
66 array( 'fa_id', 'fa_storage_key' ),
67 $conds,
68 __METHOD__,
69 array( 'LIMIT' => $batchSize )
72 $i = 0;
73 foreach ( $res as $row ) {
74 if ( $row->fa_storage_key == '' ) {
75 // Revision was missing pre-deletion
76 continue;
78 $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
79 $dbw->update( $table,
80 array( 'fa_sha1' => $sha1 ),
81 array( 'fa_id' => $row->fa_id ),
82 __METHOD__
84 $lastId = $row->fa_id;
85 $i++;
88 $done += $i;
89 if ( $i !== $batchSize ) {
90 break;
93 // print status and let slaves catch up
94 $this->output( sprintf(
95 "id %d done (up to %d), %5.3f%% \r", $lastId, $endId, $lastId / $endId * 100 ) );
96 wfWaitForSlaves();
97 } while ( true );
99 $processingTime = microtime( true ) - $startTime;
100 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $done, $processingTime ) );
102 return true; // we only updated *some* files, don't log
106 $maintClass = "PopulateFilearchiveSha1";
107 require_once RUN_MAINTENANCE_IF_MAIN;