Followup r92081: SQL fix for cleanupUploadStash.php on PostgreSQL (bug 32822)
[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 execute() {
43 if ( $this->getOption( 'file' ) ) {
44 $this->doDBUpdates(); // skip update log checks/saves
45 } else {
46 parent::execute();
47 }
48 }
49
50 public function doDBUpdates() {
51 $method = $this->getOption( 'method', 'normal' );
52 $file = $this->getOption( 'file' );
53
54 $t = -microtime( true );
55 $dbw = wfGetDB( DB_MASTER );
56 if ( $file ) {
57 $res = $dbw->select(
58 'image',
59 array( 'img_name' ),
60 array( 'img_name' => $file ),
61 __METHOD__
62 );
63 if ( !$res ) {
64 $this->error( "No such file: $file", true );
65 return false;
66 }
67 $this->output( "Populating img_sha1 field for specified files\n" );
68 } else {
69 $res = $dbw->select( 'image',
70 array( 'img_name' ), array( 'img_sha1' => '' ), __METHOD__ );
71 $this->output( "Populating img_sha1 field\n" );
72 }
73
74 $imageTable = $dbw->tableName( 'image' );
75
76 if ( $method == 'pipe' ) {
77 // @todo FIXME: Kill this and replace with a second unbuffered DB connection.
78 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
79 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
80 ' -h' . wfEscapeShellArg( $wgDBserver ) .
81 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
82 $this->output( "Using pipe method\n" );
83 $pipe = popen( $cmd, 'w' );
84 }
85
86 $numRows = $res->numRows();
87 $i = 0;
88 foreach ( $res as $row ) {
89 if ( $i % $this->mBatchSize == 0 ) {
90 $this->output( sprintf( "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
91 wfWaitForSlaves();
92 }
93 $file = wfLocalFile( $row->img_name );
94 if ( !$file ) {
95 continue;
96 }
97 $sha1 = File::sha1Base36( $file->getPath() );
98 if ( strval( $sha1 ) !== '' ) {
99 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
100 " WHERE img_name=" . $dbw->addQuotes( $row->img_name );
101 if ( $method == 'pipe' ) {
102 fwrite( $pipe, "$sql;\n" );
103 } else {
104 $dbw->query( $sql, __METHOD__ );
105 }
106 }
107 $i++;
108 }
109 if ( $method == 'pipe' ) {
110 fflush( $pipe );
111 pclose( $pipe );
112 }
113 $t += microtime( true );
114 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
115
116 return !$file; // we only updated *some* files, don't log
117 }
118 }
119
120 $maintClass = "PopulateImageSha1";
121 require_once( RUN_MAINTENANCE_IF_MAIN );