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