90fe82cfb5339c8a57ac6481a828299ee243e40c
[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( 'method', "Use 'pipe' to pipe to mysql command line,\n" .
30 "\t\tdefault uses Database class", false, true );
31 $this->addOption( 'file', 'Fix for a specific file, without File: namespace prefixed', false, true );
32 }
33
34 protected function getUpdateKey() {
35 return 'populate img_sha1';
36 }
37
38 protected function updateSkippedMessage() {
39 return 'img_sha1 column of image table already populated.';
40 }
41
42 public function doDBUpdates() {
43 $method = $this->getOption( 'method', 'normal' );
44 $file = $this->getOption( 'file' );
45
46 $t = -microtime( true );
47 $dbw = wfGetDB( DB_MASTER );
48 if ( $file ) {
49 $res = $dbw->selectRow(
50 'image',
51 array( 'img_name' ),
52 array( 'img_name' => $dbw->addQuotes( $file ) ),
53 __METHOD__
54 );
55 if ( !$res ) {
56 $this->error( "No such file: $file", true );
57 return false;
58 }
59 $this->output( "Populating img_sha1 field for specified files\n" );
60 } else {
61 $res = $dbw->select( 'image',
62 array( 'img_name' ), array( 'img_sha1' => '' ), __METHOD__ );
63 $this->output( "Populating img_sha1 field\n" );
64 }
65
66 $imageTable = $dbw->tableName( 'image' );
67
68 if ( $method == 'pipe' ) {
69 // @todo FIXME: Kill this and replace with a second unbuffered DB connection.
70 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
71 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
72 ' -h' . wfEscapeShellArg( $wgDBserver ) .
73 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
74 $this->output( "Using pipe method\n" );
75 $pipe = popen( $cmd, 'w' );
76 }
77
78 $numRows = $res->numRows();
79 $i = 0;
80 foreach ( $res as $row ) {
81 if ( $i % $this->mBatchSize == 0 ) {
82 $this->output( sprintf( "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
83 wfWaitForSlaves();
84 }
85 $file = wfLocalFile( $row->img_name );
86 if ( !$file ) {
87 continue;
88 }
89 $sha1 = File::sha1Base36( $file->getPath() );
90 if ( strval( $sha1 ) !== '' ) {
91 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
92 " WHERE img_name=" . $dbw->addQuotes( $row->img_name );
93 if ( $method == 'pipe' ) {
94 fwrite( $pipe, "$sql;\n" );
95 } else {
96 $dbw->query( $sql, __METHOD__ );
97 }
98 }
99 $i++;
100 }
101 if ( $method == 'pipe' ) {
102 fflush( $pipe );
103 pclose( $pipe );
104 }
105 $t += microtime( true );
106 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
107
108 if ( $file ) {
109 return false; // we only updated *some* files, don't log
110 } else {
111 return true;
112 }
113 }
114 }
115
116 $maintClass = "PopulateImageSha1";
117 require_once( RUN_MAINTENANCE_IF_MAIN );