MediaWiki master
populateFilearchiveSha1.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
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;
getDB()
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
Maintenance script to populate the fa_sha1 field.
__construct()
Default constructor.
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
getUpdateKey()
Get the update key name to go in the update log table.
const DB_PRIMARY
Definition defines.php:28