MediaWiki  1.33.0
populateImageSha1.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
34  public function __construct() {
35  parent::__construct();
36  $this->addDescription( 'Populate the img_sha1 field' );
37  $this->addOption( 'force', "Recalculate sha1 for rows that already have a value" );
38  $this->addOption( 'multiversiononly', "Calculate only for files with several versions" );
39  $this->addOption( 'method', "Use 'pipe' to pipe to mysql command line,\n" .
40  "\t\tdefault uses Database class", false, true );
41  $this->addOption(
42  'file',
43  'Fix for a specific file, without File: namespace prefixed',
44  false,
45  true
46  );
47  }
48 
49  protected function getUpdateKey() {
50  return 'populate img_sha1';
51  }
52 
53  protected function updateSkippedMessage() {
54  return 'img_sha1 column of image table already populated.';
55  }
56 
57  public function execute() {
58  if ( $this->getOption( 'file' ) || $this->hasOption( 'multiversiononly' ) ) {
59  $this->doDBUpdates(); // skip update log checks/saves
60  } else {
62  }
63  }
64 
65  public function doDBUpdates() {
66  $method = $this->getOption( 'method', 'normal' );
67  $file = $this->getOption( 'file', '' );
68  $force = $this->getOption( 'force' );
69  $isRegen = ( $force || $file != '' ); // forced recalculation?
70 
71  $t = -microtime( true );
72  $dbw = $this->getDB( DB_MASTER );
73  if ( $file != '' ) {
74  $res = $dbw->select(
75  'image',
76  [ 'img_name' ],
77  [ 'img_name' => $file ],
78  __METHOD__
79  );
80  if ( !$res ) {
81  $this->fatalError( "No such file: $file" );
82  }
83  $this->output( "Populating img_sha1 field for specified files\n" );
84  } else {
85  if ( $this->hasOption( 'multiversiononly' ) ) {
86  $conds = [];
87  $this->output( "Populating and recalculating img_sha1 field for versioned files\n" );
88  } elseif ( $force ) {
89  $conds = [];
90  $this->output( "Populating and recalculating img_sha1 field\n" );
91  } else {
92  $conds = [ 'img_sha1' => '' ];
93  $this->output( "Populating img_sha1 field\n" );
94  }
95  if ( $this->hasOption( 'multiversiononly' ) ) {
96  $res = $dbw->select( 'oldimage',
97  [ 'img_name' => 'DISTINCT(oi_name)' ], $conds, __METHOD__ );
98  } else {
99  $res = $dbw->select( 'image', [ 'img_name' ], $conds, __METHOD__ );
100  }
101  }
102 
103  $imageTable = $dbw->tableName( 'image' );
104  $oldImageTable = $dbw->tableName( 'oldimage' );
105 
106  if ( $method == 'pipe' ) {
107  // Opening a pipe allows the SHA-1 operation to be done in parallel
108  // with the database write operation, because the writes are queued
109  // in the pipe buffer. This can improve performance by up to a
110  // factor of 2.
112  $cmd = 'mysql -u' . Shell::escape( $wgDBuser ) .
113  ' -h' . Shell::escape( $wgDBserver ) .
114  ' -p' . Shell::escape( $wgDBpassword, $wgDBname );
115  $this->output( "Using pipe method\n" );
116  $pipe = popen( $cmd, 'w' );
117  }
118 
119  $numRows = $res->numRows();
120  $i = 0;
121  foreach ( $res as $row ) {
122  if ( $i % $this->getBatchSize() == 0 ) {
123  $this->output( sprintf(
124  "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
125  wfWaitForSlaves();
126  }
127 
128  $file = wfLocalFile( $row->img_name );
129  if ( !$file ) {
130  continue;
131  }
132 
133  // Upgrade the current file version...
134  $sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
135  if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
136  if ( $isRegen && $file->getSha1() !== $sha1 ) {
137  // The population was probably done already. If the old SHA1
138  // does not match, then both fix the SHA1 and the metadata.
139  $file->upgradeRow();
140  } else {
141  $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
142  " WHERE img_name=" . $dbw->addQuotes( $file->getName() );
143  if ( $method == 'pipe' ) {
144  fwrite( $pipe, "$sql;\n" );
145  } else {
146  $dbw->query( $sql, __METHOD__ );
147  }
148  }
149  }
150  // Upgrade the old file versions...
151  foreach ( $file->getHistory() as $oldFile ) {
152  $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
153  if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
154  if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
155  // The population was probably done already. If the old SHA1
156  // does not match, then both fix the SHA1 and the metadata.
157  $oldFile->upgradeRow();
158  } else {
159  $sql = "UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
160  " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) . " AND" .
161  " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) . ")";
162  if ( $method == 'pipe' ) {
163  fwrite( $pipe, "$sql;\n" );
164  } else {
165  $dbw->query( $sql, __METHOD__ );
166  }
167  }
168  }
169  }
170  $i++;
171  }
172  if ( $method == 'pipe' ) {
173  fflush( $pipe );
174  pclose( $pipe );
175  }
176  $t += microtime( true );
177  $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
178 
179  return !$file; // we only updated *some* files, don't log
180  }
181 }
182 
184 require_once RUN_MAINTENANCE_IF_MAIN;
MediaWiki\Shell\Shell
Executes shell commands.
Definition: Shell.php:44
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
$wgDBserver
$wgDBserver
Database host name or IP address.
Definition: DefaultSettings.php:1863
$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:485
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
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:2790
$wgDBpassword
$wgDBpassword
Database user's password.
Definition: DefaultSettings.php:1883
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:53
PopulateImageSha1\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateImageSha1.php:49
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1700
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:248
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DB_MASTER
const DB_MASTER
Definition: defines.php:26
PopulateImageSha1\__construct
__construct()
Default constructor.
Definition: populateImageSha1.php:34
execute
$batch execute()
PopulateImageSha1
Maintenance script to populate the img_sha1 field.
Definition: populateImageSha1.php:33
PopulateImageSha1\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateImageSha1.php:65
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:283
PopulateImageSha1\execute
execute()
Do the actual work.
Definition: populateImageSha1.php:57
$maintClass
$maintClass
Definition: populateImageSha1.php:183
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:367
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:1373
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
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:1878
$t
$t
Definition: testCompression.php:69
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:269
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:2688