MediaWiki  1.23.1
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->mDescription = "Populate the img_sha1 field";
35  $this->addOption( 'force', "Recalculate sha1 for rows that already have a value" );
36  $this->addOption( 'method', "Use 'pipe' to pipe to mysql command line,\n" .
37  "\t\tdefault uses Database class", false, true );
38  $this->addOption( 'file', 'Fix for a specific file, without File: namespace prefixed', false, true );
39  }
40 
41  protected function getUpdateKey() {
42  return 'populate img_sha1';
43  }
44 
45  protected function updateSkippedMessage() {
46  return 'img_sha1 column of image table already populated.';
47  }
48 
49  public function execute() {
50  if ( $this->getOption( 'file' ) ) {
51  $this->doDBUpdates(); // skip update log checks/saves
52  } else {
54  }
55  }
56 
57  public function doDBUpdates() {
58  $method = $this->getOption( 'method', 'normal' );
59  $file = $this->getOption( 'file', '' );
60  $force = $this->getOption( 'force' );
61  $isRegen = ( $force || $file != '' ); // forced recalculation?
62 
63  $t = -microtime( true );
64  $dbw = wfGetDB( DB_MASTER );
65  if ( $file != '' ) {
66  $res = $dbw->select(
67  'image',
68  array( 'img_name' ),
69  array( 'img_name' => $file ),
70  __METHOD__
71  );
72  if ( !$res ) {
73  $this->error( "No such file: $file", true );
74  return false;
75  }
76  $this->output( "Populating img_sha1 field for specified files\n" );
77  } else {
78  if ( $force ) {
79  $conds = array();
80  $this->output( "Populating and recalculating img_sha1 field\n" );
81  } else {
82  $conds = array( 'img_sha1' => '' );
83  $this->output( "Populating img_sha1 field\n" );
84  }
85  $res = $dbw->select( 'image', array( 'img_name' ), $conds, __METHOD__ );
86  }
87 
88  $imageTable = $dbw->tableName( 'image' );
89  $oldImageTable = $dbw->tableName( 'oldimage' );
90 
91  if ( $method == 'pipe' ) {
92  // Opening a pipe allows the SHA-1 operation to be done in parallel
93  // with the database write operation, because the writes are queued
94  // in the pipe buffer. This can improve performance by up to a
95  // factor of 2.
96  global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
97  $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
98  ' -h' . wfEscapeShellArg( $wgDBserver ) .
99  ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
100  $this->output( "Using pipe method\n" );
101  $pipe = popen( $cmd, 'w' );
102  }
103 
104  $numRows = $res->numRows();
105  $i = 0;
106  foreach ( $res as $row ) {
107  if ( $i % $this->mBatchSize == 0 ) {
108  $this->output( sprintf(
109  "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
110  wfWaitForSlaves();
111  }
112  $file = wfLocalFile( $row->img_name );
113  if ( !$file ) {
114  continue;
115  }
116  // Upgrade the current file version...
117  $sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
118  if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
119  if ( $isRegen && $file->getSha1() !== $sha1 ) {
120  // The population was probably done already. If the old SHA1
121  // does not match, then both fix the SHA1 and the metadata.
122  $file->upgradeRow();
123  } else {
124  $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
125  " WHERE img_name=" . $dbw->addQuotes( $file->getName() );
126  if ( $method == 'pipe' ) {
127  fwrite( $pipe, "$sql;\n" );
128  } else {
129  $dbw->query( $sql, __METHOD__ );
130  }
131  }
132  }
133  // Upgrade the old file versions...
134  foreach ( $file->getHistory() as $oldFile ) {
135  $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
136  if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
137  if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
138  // The population was probably done already. If the old SHA1
139  // does not match, then both fix the SHA1 and the metadata.
140  $oldFile->upgradeRow();
141  } else {
142  $sql = "UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
143  " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) . " AND" .
144  " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) . ")";
145  if ( $method == 'pipe' ) {
146  fwrite( $pipe, "$sql;\n" );
147  } else {
148  $dbw->query( $sql, __METHOD__ );
149  }
150  }
151  }
152  }
153  $i++;
154  }
155  if ( $method == 'pipe' ) {
156  fflush( $pipe );
157  pclose( $pipe );
158  }
159  $t += microtime( true );
160  $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
161 
162  return !$file; // we only updated *some* files, don't log
163  }
164 }
165 
166 $maintClass = "PopulateImageSha1";
167 require_once RUN_MAINTENANCE_IF_MAIN;
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
PopulateImageSha1\updateSkippedMessage
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
Definition: populateImageSha1.php:45
$wgDBname
controlled by $wgMainCacheType controlled by $wgParserCacheType controlled by $wgMessageCacheType If you set CACHE_NONE to one of the three control default value for MediaWiki still create a but requests to it are no ops and we always fall through to the database If the cache daemon can t be it should also disable itself fairly smoothly By $wgMemc is used but when it is $parserMemc or $messageMemc this is mentioned $wgDBname
Definition: memcached.txt:96
PopulateImageSha1\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateImageSha1.php:41
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1209
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfWaitForSlaves
wfWaitForSlaves( $maxLag=false, $wiki=false, $cluster=false)
Modern version of wfWaitForSlaves().
Definition: GlobalFunctions.php:3795
PopulateImageSha1\__construct
__construct()
Default constructor.
Definition: populateImageSha1.php:32
execute
$batch execute()
wfEscapeShellArg
wfEscapeShellArg()
Windows-compatible version of escapeshellarg() Windows doesn't recognise single-quotes in the shell,...
Definition: GlobalFunctions.php:2705
PopulateImageSha1
Maintenance script to populate the img_sha1 field.
Definition: populateImageSha1.php:31
PopulateImageSha1\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateImageSha1.php:57
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
PopulateImageSha1\execute
execute()
Do the actual work.
Definition: populateImageSha1.php:49
$maintClass
$maintClass
Definition: populateImageSha1.php:166
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\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
$t
$t
Definition: testCompression.php:65
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:3704
$res
$res
Definition: database.txt:21