Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulateFilearchiveSha1
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 updateSkippedMessage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
30
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
24require_once __DIR__ . '/Maintenance.php';
25
26/**
27 * Maintenance script to populate the fa_sha1 field.
28 *
29 * @ingroup Maintenance
30 * @since 1.21
31 */
32class PopulateFilearchiveSha1 extends LoggedUpdateMaintenance {
33    public function __construct() {
34        parent::__construct();
35        $this->addDescription( 'Populate the fa_sha1 field from fa_storage_key' );
36    }
37
38    protected function getUpdateKey() {
39        return 'populate fa_sha1';
40    }
41
42    protected function updateSkippedMessage() {
43        return 'fa_sha1 column of filearchive table already populated.';
44    }
45
46    public function doDBUpdates() {
47        $startTime = microtime( true );
48        $dbw = $this->getPrimaryDB();
49        $table = 'filearchive';
50
51        if ( !$this->getDB( DB_PRIMARY )->fieldExists( $table, 'fa_sha1', __METHOD__ ) ) {
52            $this->output( "fa_sha1 column does not exist\n\n", true );
53
54            return false;
55        }
56
57        $this->output( "Populating fa_sha1 field from fa_storage_key\n" );
58        $endId = $dbw->newSelectQueryBuilder()
59            ->select( 'MAX(fa_id)' )
60            ->from( $table )
61            ->caller( __METHOD__ )->fetchField();
62
63        $batchSize = $this->getBatchSize();
64        $done = 0;
65
66        do {
67            $res = $dbw->newSelectQueryBuilder()
68                ->select( [ 'fa_id', 'fa_storage_key' ] )
69                ->from( $table )
70                ->where( [ 'fa_sha1' => '', 'fa_storage_key IS NOT NULL' ] )
71                ->limit( $batchSize )
72                ->caller( __METHOD__ )->fetchResultSet();
73
74            $i = 0;
75            foreach ( $res as $row ) {
76                if ( $row->fa_storage_key == '' ) {
77                    // Revision was missing pre-deletion
78                    continue;
79                }
80                $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
81                $dbw->newUpdateQueryBuilder()
82                    ->update( $table )
83                    ->set( [ 'fa_sha1' => $sha1 ] )
84                    ->where( [ 'fa_id' => $row->fa_id ] )
85                    ->caller( __METHOD__ )
86                    ->execute();
87                $lastId = $row->fa_id;
88                $i++;
89            }
90
91            $done += $i;
92            if ( $i !== $batchSize ) {
93                break;
94            }
95
96            // print status and let replica DBs catch up
97            $this->output( sprintf(
98                // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $lastId is set for non-empty $res
99                "id %d done (up to %d), %5.3f%%  \r", $lastId, $endId, $lastId / $endId * 100 ) );
100            $this->waitForReplication();
101        } while ( true );
102
103        $processingTime = microtime( true ) - $startTime;
104        $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $done, $processingTime ) );
105
106        // we only updated *some* files, don't log
107        return true;
108    }
109}
110
111$maintClass = PopulateFilearchiveSha1::class;
112require_once RUN_MAINTENANCE_IF_MAIN;