Merge branch 'Wikidata' of ssh://gerrit.wikimedia.org:29418/mediawiki/core into Wikidata
[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
56 $t = -microtime( true );
57 $dbw = wfGetDB( DB_MASTER );
58 if ( $file ) {
59 $res = $dbw->select(
60 'image',
61 array( 'img_name' ),
62 array( 'img_name' => $file ),
63 __METHOD__
64 );
65 if ( !$res ) {
66 $this->error( "No such file: $file", true );
67 return false;
68 }
69 $this->output( "Populating img_sha1 field for specified files\n" );
70 } else {
71 if ( $force ) {
72 $conds = array();
73 $this->output( "Populating and recalculating img_sha1 field\n" );
74 } else {
75 $conds = array( 'img_sha1' => '' );
76 $this->output( "Populating img_sha1 field\n" );
77 }
78 $res = $dbw->select( 'image', array( 'img_name' ), $conds, __METHOD__ );
79 }
80
81 $imageTable = $dbw->tableName( 'image' );
82 $oldImageTable = $dbw->tableName( 'oldimage' );
83
84 if ( $method == 'pipe' ) {
85 // Opening a pipe allows the SHA-1 operation to be done in parallel
86 // with the database write operation, because the writes are queued
87 // in the pipe buffer. This can improve performance by up to a
88 // factor of 2.
89 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
90 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
91 ' -h' . wfEscapeShellArg( $wgDBserver ) .
92 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
93 $this->output( "Using pipe method\n" );
94 $pipe = popen( $cmd, 'w' );
95 }
96
97 $numRows = $res->numRows();
98 $i = 0;
99 foreach ( $res as $row ) {
100 if ( $i % $this->mBatchSize == 0 ) {
101 $this->output( sprintf(
102 "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
103 wfWaitForSlaves();
104 }
105 $file = wfLocalFile( $row->img_name );
106 if ( !$file ) {
107 continue;
108 }
109 // Upgrade the current file version...
110 $sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
111 if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
112 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
113 " WHERE img_name=" . $dbw->addQuotes( $file->getName() );
114 if ( $method == 'pipe' ) {
115 fwrite( $pipe, "$sql;\n" );
116 } else {
117 $dbw->query( $sql, __METHOD__ );
118 }
119 }
120 // Upgrade the old file versions...
121 foreach ( $file->getHistory() as $oldFile ) {
122 $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
123 if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
124 $sql = "UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
125 " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) . " AND" .
126 " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) . ")";
127 if ( $method == 'pipe' ) {
128 fwrite( $pipe, "$sql;\n" );
129 } else {
130 $dbw->query( $sql, __METHOD__ );
131 }
132 }
133 }
134 $i++;
135 }
136 if ( $method == 'pipe' ) {
137 fflush( $pipe );
138 pclose( $pipe );
139 }
140 $t += microtime( true );
141 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
142
143 return !$file; // we only updated *some* files, don't log
144 }
145 }
146
147 $maintClass = "PopulateImageSha1";
148 require_once( RUN_MAINTENANCE_IF_MAIN );