Two new parser tests related to bug 6200
[lhc/web/wiklou.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 $oldimageTable = $dbw->tableName( 'oldimage' );
56
57 if ( $method == 'pipe' ) {
58 // @fixme kill this and replace with a second unbuffered DB connection.
59 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
60 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
61 ' -h' . wfEscapeShellArg( $wgDBserver ) .
62 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
63 $this->output( "Using pipe method\n" );
64 $pipe = popen( $cmd, 'w' );
65 }
66
67 $numRows = $res->numRows();
68 $i = 0;
69 foreach ( $res as $row ) {
70 if ( $i % 100 == 0 ) {
71 $this->output( sprintf( "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
72 wfWaitForSlaves( 5 );
73 }
74 $file = wfLocalFile( $row->img_name );
75 if ( !$file ) {
76 continue;
77 }
78 $sha1 = File::sha1Base36( $file->getPath() );
79 if ( strval( $sha1 ) !== '' ) {
80 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
81 " WHERE img_name=" . $dbw->addQuotes( $row->img_name );
82 if ( $method == 'pipe' ) {
83 fwrite( $pipe, "$sql;\n" );
84 } else {
85 $dbw->query( $sql, __METHOD__ );
86 }
87 }
88 $i++;
89 }
90 if ( $method == 'pipe' ) {
91 fflush( $pipe );
92 pclose( $pipe );
93 }
94 $t += microtime( true );
95 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
96 }
97 }
98
99 $maintClass = "PopulateSha1";
100 require_once( DO_MAINTENANCE );