MediaWiki REL1_41
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->getDB( DB_PRIMARY );
49 $table = 'filearchive';
50
51 if ( !$dbw->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->update( $table,
82 [ 'fa_sha1' => $sha1 ],
83 [ 'fa_id' => $row->fa_id ],
84 __METHOD__
85 );
86 $lastId = $row->fa_id;
87 $i++;
88 }
89
90 $done += $i;
91 if ( $i !== $batchSize ) {
92 break;
93 }
94
95 // print status and let replica DBs catch up
96 $this->output( sprintf(
97 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable $lastId is set for non-empty $res
98 "id %d done (up to %d), %5.3f%% \r", $lastId, $endId, $lastId / $endId * 100 ) );
99 $this->waitForReplication();
100 } while ( true );
101
102 $processingTime = microtime( true ) - $startTime;
103 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $done, $processingTime ) );
104
105 // we only updated *some* files, don't log
106 return true;
107 }
108}
109
110$maintClass = PopulateFilearchiveSha1::class;
111require_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