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