Merge "Pass phpcs-strict on maintenance/ (3/8)"
[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(
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 {
59 parent::execute();
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 = wfGetDB( DB_MASTER );
71 if ( $file != '' ) {
72 $res = $dbw->select(
73 'image',
74 array( 'img_name' ),
75 array( 'img_name' => $file ),
76 __METHOD__
77 );
78 if ( !$res ) {
79 $this->error( "No such file: $file", true );
80 return false;
81 }
82 $this->output( "Populating img_sha1 field for specified files\n" );
83 } else {
84 if ( $force ) {
85 $conds = array();
86 $this->output( "Populating and recalculating img_sha1 field\n" );
87 } else {
88 $conds = array( 'img_sha1' => '' );
89 $this->output( "Populating img_sha1 field\n" );
90 }
91 if ( $this->hasOption( 'multiversiononly' ) ) {
92 $res = $dbw->select( 'oldimage',
93 array( 'img_name' => 'DISTINCT(oi_name)' ), $conds, __METHOD__ );
94 } else {
95 $res = $dbw->select( 'image', array( 'img_name' ), $conds, __METHOD__ );
96 }
97 }
98
99 $imageTable = $dbw->tableName( 'image' );
100 $oldImageTable = $dbw->tableName( 'oldimage' );
101
102 if ( $method == 'pipe' ) {
103 // Opening a pipe allows the SHA-1 operation to be done in parallel
104 // with the database write operation, because the writes are queued
105 // in the pipe buffer. This can improve performance by up to a
106 // factor of 2.
107 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
108 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
109 ' -h' . wfEscapeShellArg( $wgDBserver ) .
110 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
111 $this->output( "Using pipe method\n" );
112 $pipe = popen( $cmd, 'w' );
113 }
114
115 $numRows = $res->numRows();
116 $i = 0;
117 foreach ( $res as $row ) {
118 if ( $i % $this->mBatchSize == 0 ) {
119 $this->output( sprintf(
120 "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
121 wfWaitForSlaves();
122 }
123
124 $file = wfLocalFile( $row->img_name );
125 if ( !$file ) {
126 continue;
127 }
128
129 // Upgrade the current file version...
130 $sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
131 if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
132 if ( $isRegen && $file->getSha1() !== $sha1 ) {
133 // The population was probably done already. If the old SHA1
134 // does not match, then both fix the SHA1 and the metadata.
135 $file->upgradeRow();
136 } else {
137 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
138 " WHERE img_name=" . $dbw->addQuotes( $file->getName() );
139 if ( $method == 'pipe' ) {
140 fwrite( $pipe, "$sql;\n" );
141 } else {
142 $dbw->query( $sql, __METHOD__ );
143 }
144 }
145 }
146 // Upgrade the old file versions...
147 foreach ( $file->getHistory() as $oldFile ) {
148 $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
149 if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
150 if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
151 // The population was probably done already. If the old SHA1
152 // does not match, then both fix the SHA1 and the metadata.
153 $oldFile->upgradeRow();
154 } else {
155 $sql = "UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
156 " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) . " AND" .
157 " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) . ")";
158 if ( $method == 'pipe' ) {
159 fwrite( $pipe, "$sql;\n" );
160 } else {
161 $dbw->query( $sql, __METHOD__ );
162 }
163 }
164 }
165 }
166 $i++;
167 }
168 if ( $method == 'pipe' ) {
169 fflush( $pipe );
170 pclose( $pipe );
171 }
172 $t += microtime( true );
173 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
174
175 return !$file; // we only updated *some* files, don't log
176 }
177 }
178
179 $maintClass = "PopulateImageSha1";
180 require_once RUN_MAINTENANCE_IF_MAIN;