MediaWiki  1.34.0
populateFilearchiveSha1.php
Go to the documentation of this file.
1 <?php
24 require_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_MASTER );
49  $table = 'filearchive';
50  $conds = [ 'fa_sha1' => '', 'fa_storage_key IS NOT NULL' ];
51 
52  if ( !$dbw->fieldExists( $table, 'fa_sha1', __METHOD__ ) ) {
53  $this->output( "fa_sha1 column does not exist\n\n", true );
54 
55  return false;
56  }
57 
58  $this->output( "Populating fa_sha1 field from fa_storage_key\n" );
59  $endId = $dbw->selectField( $table, 'MAX(fa_id)', '', __METHOD__ );
60 
61  $batchSize = $this->getBatchSize();
62  $done = 0;
63 
64  do {
65  $res = $dbw->select(
66  $table,
67  [ 'fa_id', 'fa_storage_key' ],
68  $conds,
69  __METHOD__,
70  [ 'LIMIT' => $batchSize ]
71  );
72 
73  $i = 0;
74  foreach ( $res as $row ) {
75  if ( $row->fa_storage_key == '' ) {
76  // Revision was missing pre-deletion
77  continue;
78  }
79  $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
80  $dbw->update( $table,
81  [ 'fa_sha1' => $sha1 ],
82  [ 'fa_id' => $row->fa_id ],
83  __METHOD__
84  );
85  $lastId = $row->fa_id;
86  $i++;
87  }
88 
89  $done += $i;
90  if ( $i !== $batchSize ) {
91  break;
92  }
93 
94  // print status and let replica DBs catch up
95  $this->output( sprintf(
96  "id %d done (up to %d), %5.3f%% \r", $lastId, $endId, $lastId / $endId * 100 ) );
98  } while ( true );
99 
100  $processingTime = microtime( true ) - $startTime;
101  $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $done, $processingTime ) );
102 
103  return true; // we only updated *some* files, don't log
104  }
105 }
106 
107 $maintClass = PopulateFilearchiveSha1::class;
108 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
PopulateFilearchiveSha1\updateSkippedMessage
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
Definition: populateFilearchiveSha1.php:42
PopulateFilearchiveSha1\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateFilearchiveSha1.php:38
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
$res
$res
Definition: testCompression.php:52
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:2718
LocalRepo\getHashFromKey
static getHashFromKey( $key)
Gets the SHA1 hash from a storage key.
Definition: LocalRepo.php:183
PopulateFilearchiveSha1\__construct
__construct()
Default constructor.
Definition: populateFilearchiveSha1.php:33
PopulateFilearchiveSha1\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateFilearchiveSha1.php:46
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1727
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$maintClass
$maintClass
Definition: populateFilearchiveSha1.php:107
PopulateFilearchiveSha1
Maintenance script to populate the fa_sha1 field.
Definition: populateFilearchiveSha1.php:32
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453