]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/populateSha1.php
MediaWiki 1.17.0
[autoinstallsdev/mediawiki.git] / maintenance / populateSha1.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 PopulateSha1 extends Maintenance {
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         public function execute() {
35                 $method = $this->getOption( 'method', 'normal' );
36                 $file = $this->getOption( 'file' );
37
38                 $t = -microtime( true );
39                 $dbw = wfGetDB( DB_MASTER );
40                 if ( $file ) {
41                         $res = $dbw->selectRow(
42                                 'image',
43                                 array( 'img_name' ),
44                                 array( 'img_name' => $dbw->addQuotes( $file ) ),
45                                 __METHOD__
46                         );
47                         if ( !$res ) {
48                                 $this->error( "No such file: $file", true );
49                                 return;
50                         }
51                 } else {
52                         $res = $dbw->select( 'image', array( 'img_name' ), array( 'img_sha1' => '' ), __METHOD__ );
53                 }
54                 $imageTable = $dbw->tableName( 'image' );
55
56                 if ( $method == 'pipe' ) {
57                         // @fixme kill this and replace with a second unbuffered DB connection.
58                         global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
59                         $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
60                                 ' -h' . wfEscapeShellArg( $wgDBserver ) .
61                                 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
62                         $this->output( "Using pipe method\n" );
63                         $pipe = popen( $cmd, 'w' );
64                 }
65
66                 $numRows = $res->numRows();
67                 $i = 0;
68                 foreach ( $res as $row ) {
69                         if ( $i % 100 == 0 ) {
70                                 $this->output( sprintf( "Done %d of %d, %5.3f%%  \r", $i, $numRows, $i / $numRows * 100 ) );
71                                 wfWaitForSlaves( 5 );
72                         }
73                         $file = wfLocalFile( $row->img_name );
74                         if ( !$file ) {
75                                 continue;
76                         }
77                         $sha1 = File::sha1Base36( $file->getPath() );
78                         if ( strval( $sha1 ) !== '' ) {
79                                 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
80                                         " WHERE img_name=" . $dbw->addQuotes( $row->img_name );
81                                 if ( $method == 'pipe' ) {
82                                         fwrite( $pipe, "$sql;\n" );
83                                 } else {
84                                         $dbw->query( $sql, __METHOD__ );
85                                 }
86                         }
87                         $i++;
88                 }
89                 if ( $method == 'pipe' ) {
90                         fflush( $pipe );
91                         pclose( $pipe );
92                 }
93                 $t += microtime( true );
94                 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
95         }
96 }
97
98 $maintClass = "PopulateSha1";
99 require_once( RUN_MAINTENANCE_IF_MAIN );