Merge "Pass phpcs-strict on maintenance/benchmarks/"
[lhc/web/wiklou.git] / maintenance / populateImageSha1.php
1 <?php
2 /**
3 * Optional upgrade script to populate the img_sha1 field
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script to populate the img_sha1 field.
28 *
29 * @ingroup Maintenance
30 */
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( '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( 'file', 'Fix for a specific file, without File: namespace prefixed', false, true );
40 }
41
42 protected function getUpdateKey() {
43 return 'populate img_sha1';
44 }
45
46 protected function updateSkippedMessage() {
47 return 'img_sha1 column of image table already populated.';
48 }
49
50 public function execute() {
51 if ( $this->getOption( 'file' ) || $this->hasOption( 'multiversiononly' ) ) {
52 $this->doDBUpdates(); // skip update log checks/saves
53 } else {
54 parent::execute();
55 }
56 }
57
58 public function doDBUpdates() {
59 $method = $this->getOption( 'method', 'normal' );
60 $file = $this->getOption( 'file', '' );
61 $force = $this->getOption( 'force' );
62 $isRegen = ( $force || $file != '' ); // forced recalculation?
63
64 $t = -microtime( true );
65 $dbw = wfGetDB( DB_MASTER );
66 if ( $file != '' ) {
67 $res = $dbw->select(
68 'image',
69 array( 'img_name' ),
70 array( 'img_name' => $file ),
71 __METHOD__
72 );
73 if ( !$res ) {
74 $this->error( "No such file: $file", true );
75 return false;
76 }
77 $this->output( "Populating img_sha1 field for specified files\n" );
78 } else {
79 if ( $force ) {
80 $conds = array();
81 $this->output( "Populating and recalculating img_sha1 field\n" );
82 } else {
83 $conds = array( 'img_sha1' => '' );
84 $this->output( "Populating img_sha1 field\n" );
85 }
86 if ( $this->hasOption( 'multiversiononly' ) ) {
87 $res = $dbw->select( 'oldimage',
88 array( 'img_name' => 'DISTINCT(oi_name)' ), $conds, __METHOD__ );
89 } else {
90 $res = $dbw->select( 'image', array( 'img_name' ), $conds, __METHOD__ );
91 }
92 }
93
94 $imageTable = $dbw->tableName( 'image' );
95 $oldImageTable = $dbw->tableName( 'oldimage' );
96
97 if ( $method == 'pipe' ) {
98 // Opening a pipe allows the SHA-1 operation to be done in parallel
99 // with the database write operation, because the writes are queued
100 // in the pipe buffer. This can improve performance by up to a
101 // factor of 2.
102 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
103 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
104 ' -h' . wfEscapeShellArg( $wgDBserver ) .
105 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
106 $this->output( "Using pipe method\n" );
107 $pipe = popen( $cmd, 'w' );
108 }
109
110 $numRows = $res->numRows();
111 $i = 0;
112 foreach ( $res as $row ) {
113 if ( $i % $this->mBatchSize == 0 ) {
114 $this->output( sprintf(
115 "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
116 wfWaitForSlaves();
117 }
118
119 $file = wfLocalFile( $row->img_name );
120 if ( !$file ) {
121 continue;
122 }
123
124 // Upgrade the current file version...
125 $sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
126 if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
127 if ( $isRegen && $file->getSha1() !== $sha1 ) {
128 // The population was probably done already. If the old SHA1
129 // does not match, then both fix the SHA1 and the metadata.
130 $file->upgradeRow();
131 } else {
132 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
133 " WHERE img_name=" . $dbw->addQuotes( $file->getName() );
134 if ( $method == 'pipe' ) {
135 fwrite( $pipe, "$sql;\n" );
136 } else {
137 $dbw->query( $sql, __METHOD__ );
138 }
139 }
140 }
141 // Upgrade the old file versions...
142 foreach ( $file->getHistory() as $oldFile ) {
143 $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
144 if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
145 if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
146 // The population was probably done already. If the old SHA1
147 // does not match, then both fix the SHA1 and the metadata.
148 $oldFile->upgradeRow();
149 } else {
150 $sql = "UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
151 " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) . " AND" .
152 " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) . ")";
153 if ( $method == 'pipe' ) {
154 fwrite( $pipe, "$sql;\n" );
155 } else {
156 $dbw->query( $sql, __METHOD__ );
157 }
158 }
159 }
160 }
161 $i++;
162 }
163 if ( $method == 'pipe' ) {
164 fflush( $pipe );
165 pclose( $pipe );
166 }
167 $t += microtime( true );
168 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
169
170 return !$file; // we only updated *some* files, don't log
171 }
172 }
173
174 $maintClass = "PopulateImageSha1";
175 require_once RUN_MAINTENANCE_IF_MAIN;