Localisation updates from http://translatewiki.net.
[mediawiki.git] / maintenance / populateImageSha1.php
blob126d22d9864ec739b9b86312dc9e8807d4903d1f
1 <?php
2 /**
3 * Optional upgrade script to populate the img_sha1 field
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script to populate the img_sha1 field.
29 * @ingroup Maintenance
31 class PopulateImageSha1 extends LoggedUpdateMaintenance {
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 );
41 protected function getUpdateKey() {
42 return 'populate img_sha1';
45 protected function updateSkippedMessage() {
46 return 'img_sha1 column of image table already populated.';
49 public function execute() {
50 if ( $this->getOption( 'file' ) ) {
51 $this->doDBUpdates(); // skip update log checks/saves
52 } else {
53 parent::execute();
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?
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__
72 if ( !$res ) {
73 $this->error( "No such file: $file", true );
74 return false;
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" );
85 $res = $dbw->select( 'image', array( 'img_name' ), $conds, __METHOD__ );
88 $imageTable = $dbw->tableName( 'image' );
89 $oldImageTable = $dbw->tableName( 'oldimage' );
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' );
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();
112 $file = wfLocalFile( $row->img_name );
113 if ( !$file ) {
114 continue;
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__ );
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__ );
153 $i++;
155 if ( $method == 'pipe' ) {
156 fflush( $pipe );
157 pclose( $pipe );
159 $t += microtime( true );
160 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
162 return !$file; // we only updated *some* files, don't log
166 $maintClass = "PopulateImageSha1";
167 require_once RUN_MAINTENANCE_IF_MAIN;