MediaWiki  1.32.5
populateImageSha1.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Populate the img_sha1 field' );
35  $this->addOption( 'force', "Recalculate sha1 for rows that already have a value" );
36  $this->addOption( 'multiversiononly', "Calculate only for files with several versions" );
37  $this->addOption( 'method', "Use 'pipe' to pipe to mysql command line,\n" .
38  "\t\tdefault uses Database class", false, true );
39  $this->addOption(
40  'file',
41  'Fix for a specific file, without File: namespace prefixed',
42  false,
43  true
44  );
45  }
46 
47  protected function getUpdateKey() {
48  return 'populate img_sha1';
49  }
50 
51  protected function updateSkippedMessage() {
52  return 'img_sha1 column of image table already populated.';
53  }
54 
55  public function execute() {
56  if ( $this->getOption( 'file' ) || $this->hasOption( 'multiversiononly' ) ) {
57  $this->doDBUpdates(); // skip update log checks/saves
58  } else {
60  }
61  }
62 
63  public function doDBUpdates() {
64  $method = $this->getOption( 'method', 'normal' );
65  $file = $this->getOption( 'file', '' );
66  $force = $this->getOption( 'force' );
67  $isRegen = ( $force || $file != '' ); // forced recalculation?
68 
69  $t = -microtime( true );
70  $dbw = $this->getDB( DB_MASTER );
71  if ( $file != '' ) {
72  $res = $dbw->select(
73  'image',
74  [ 'img_name' ],
75  [ 'img_name' => $file ],
76  __METHOD__
77  );
78  if ( !$res ) {
79  $this->fatalError( "No such file: $file" );
80  }
81  $this->output( "Populating img_sha1 field for specified files\n" );
82  } else {
83  if ( $this->hasOption( 'multiversiononly' ) ) {
84  $conds = [];
85  $this->output( "Populating and recalculating img_sha1 field for versioned files\n" );
86  } elseif ( $force ) {
87  $conds = [];
88  $this->output( "Populating and recalculating img_sha1 field\n" );
89  } else {
90  $conds = [ 'img_sha1' => '' ];
91  $this->output( "Populating img_sha1 field\n" );
92  }
93  if ( $this->hasOption( 'multiversiononly' ) ) {
94  $res = $dbw->select( 'oldimage',
95  [ 'img_name' => 'DISTINCT(oi_name)' ], $conds, __METHOD__ );
96  } else {
97  $res = $dbw->select( 'image', [ 'img_name' ], $conds, __METHOD__ );
98  }
99  }
100 
101  $imageTable = $dbw->tableName( 'image' );
102  $oldImageTable = $dbw->tableName( 'oldimage' );
103 
104  if ( $method == 'pipe' ) {
105  // Opening a pipe allows the SHA-1 operation to be done in parallel
106  // with the database write operation, because the writes are queued
107  // in the pipe buffer. This can improve performance by up to a
108  // factor of 2.
110  $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
111  ' -h' . wfEscapeShellArg( $wgDBserver ) .
113  $this->output( "Using pipe method\n" );
114  $pipe = popen( $cmd, 'w' );
115  }
116 
117  $numRows = $res->numRows();
118  $i = 0;
119  foreach ( $res as $row ) {
120  if ( $i % $this->getBatchSize() == 0 ) {
121  $this->output( sprintf(
122  "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
123  wfWaitForSlaves();
124  }
125 
126  $file = wfLocalFile( $row->img_name );
127  if ( !$file ) {
128  continue;
129  }
130 
131  // Upgrade the current file version...
132  $sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
133  if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
134  if ( $isRegen && $file->getSha1() !== $sha1 ) {
135  // The population was probably done already. If the old SHA1
136  // does not match, then both fix the SHA1 and the metadata.
137  $file->upgradeRow();
138  } else {
139  $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
140  " WHERE img_name=" . $dbw->addQuotes( $file->getName() );
141  if ( $method == 'pipe' ) {
142  fwrite( $pipe, "$sql;\n" );
143  } else {
144  $dbw->query( $sql, __METHOD__ );
145  }
146  }
147  }
148  // Upgrade the old file versions...
149  foreach ( $file->getHistory() as $oldFile ) {
150  $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
151  if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
152  if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
153  // The population was probably done already. If the old SHA1
154  // does not match, then both fix the SHA1 and the metadata.
155  $oldFile->upgradeRow();
156  } else {
157  $sql = "UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
158  " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) . " AND" .
159  " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) . ")";
160  if ( $method == 'pipe' ) {
161  fwrite( $pipe, "$sql;\n" );
162  } else {
163  $dbw->query( $sql, __METHOD__ );
164  }
165  }
166  }
167  }
168  $i++;
169  }
170  if ( $method == 'pipe' ) {
171  fflush( $pipe );
172  pclose( $pipe );
173  }
174  $t += microtime( true );
175  $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
176 
177  return !$file; // we only updated *some* files, don't log
178  }
179 }
180 
182 require_once RUN_MAINTENANCE_IF_MAIN;
$wgDBserver
$wgDBserver
Database host name or IP address.
Definition: DefaultSettings.php:1852
$wgDBname
controlled by the following MediaWiki still creates a BagOStuff but calls it to it are no ops If the cache daemon can t be it should also disable itself fairly $wgDBname
Definition: memcached.txt:93
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:465
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:317
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
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:2847
$wgDBpassword
$wgDBpassword
Database user's password.
Definition: DefaultSettings.php:1872
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
PopulateImageSha1\updateSkippedMessage
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
Definition: populateImageSha1.php:51
PopulateImageSha1\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateImageSha1.php:47
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1679
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:236
DB_MASTER
const DB_MASTER
Definition: defines.php:26
PopulateImageSha1\__construct
__construct()
Default constructor.
Definition: populateImageSha1.php:32
execute
$batch execute()
PopulateImageSha1
Maintenance script to populate the img_sha1 field.
Definition: populateImageSha1.php:31
PopulateImageSha1\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateImageSha1.php:63
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:271
PopulateImageSha1\execute
execute()
Do the actual work.
Definition: populateImageSha1.php:55
$maintClass
$maintClass
Definition: populateImageSha1.php:181
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:347
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1351
wfEscapeShellArg
wfEscapeShellArg(... $args)
Version of escapeshellarg() that works better on Windows.
Definition: GlobalFunctions.php:2183
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:414
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$wgDBuser
$wgDBuser
Database username.
Definition: DefaultSettings.php:1867
$t
$t
Definition: testCompression.php:69
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:257
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:2745