Merge "Add more tests for paragraphs and headings with extra spacing"
[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 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script to populate the img_sha1 field.
28 *
29 * @ingroup Maintenance
30 */
31 class PopulateImageSha1 extends LoggedUpdateMaintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Populate the img_sha1 field";
35 $this->addOption( 'force', "Recalculate sha1 for rows that already have a value" );
36 $this->addOption( 'method', "Use 'pipe' to pipe to mysql command line,\n" .
37 "\t\tdefault uses Database class", false, true );
38 $this->addOption( 'file', 'Fix for a specific file, without File: namespace prefixed', false, true );
39 }
40
41 protected function getUpdateKey() {
42 return 'populate img_sha1';
43 }
44
45 protected function updateSkippedMessage() {
46 return 'img_sha1 column of image table already populated.';
47 }
48
49 public function execute() {
50 if ( $this->getOption( 'file' ) ) {
51 $this->doDBUpdates(); // skip update log checks/saves
52 } else {
53 parent::execute();
54 }
55 }
56
57 public function doDBUpdates() {
58 $method = $this->getOption( 'method', 'normal' );
59 $file = $this->getOption( 'file', '' );
60 $force = $this->getOption( 'force' );
61 $isRegen = ( $force || $file != '' ); // forced recalculation?
62
63 $t = -microtime( true );
64 $dbw = wfGetDB( DB_MASTER );
65 if ( $file != '' ) {
66 $res = $dbw->select(
67 'image',
68 array( 'img_name' ),
69 array( 'img_name' => $file ),
70 __METHOD__
71 );
72 if ( !$res ) {
73 $this->error( "No such file: $file", true );
74 return false;
75 }
76 $this->output( "Populating img_sha1 field for specified files\n" );
77 } else {
78 if ( $force ) {
79 $conds = array();
80 $this->output( "Populating and recalculating img_sha1 field\n" );
81 } else {
82 $conds = array( 'img_sha1' => '' );
83 $this->output( "Populating img_sha1 field\n" );
84 }
85 $res = $dbw->select( 'image', array( 'img_name' ), $conds, __METHOD__ );
86 }
87
88 $imageTable = $dbw->tableName( 'image' );
89 $oldImageTable = $dbw->tableName( 'oldimage' );
90
91 if ( $method == 'pipe' ) {
92 // Opening a pipe allows the SHA-1 operation to be done in parallel
93 // with the database write operation, because the writes are queued
94 // in the pipe buffer. This can improve performance by up to a
95 // factor of 2.
96 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
97 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
98 ' -h' . wfEscapeShellArg( $wgDBserver ) .
99 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
100 $this->output( "Using pipe method\n" );
101 $pipe = popen( $cmd, 'w' );
102 }
103
104 $numRows = $res->numRows();
105 $i = 0;
106 foreach ( $res as $row ) {
107 if ( $i % $this->mBatchSize == 0 ) {
108 $this->output( sprintf(
109 "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
110 wfWaitForSlaves();
111 }
112 $file = wfLocalFile( $row->img_name );
113 if ( !$file ) {
114 continue;
115 }
116 // Upgrade the current file version...
117 $sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
118 if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
119 if ( $isRegen && $file->getSha1() !== $sha1 ) {
120 // The population was probably done already. If the old SHA1
121 // does not match, then both fix the SHA1 and the metadata.
122 $file->upgradeRow();
123 } else {
124 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
125 " WHERE img_name=" . $dbw->addQuotes( $file->getName() );
126 if ( $method == 'pipe' ) {
127 fwrite( $pipe, "$sql;\n" );
128 } else {
129 $dbw->query( $sql, __METHOD__ );
130 }
131 }
132 }
133 // Upgrade the old file versions...
134 foreach ( $file->getHistory() as $oldFile ) {
135 $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
136 if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
137 if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
138 // The population was probably done already. If the old SHA1
139 // does not match, then both fix the SHA1 and the metadata.
140 $oldFile->upgradeRow();
141 } else {
142 $sql = "UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
143 " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) . " AND" .
144 " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) . ")";
145 if ( $method == 'pipe' ) {
146 fwrite( $pipe, "$sql;\n" );
147 } else {
148 $dbw->query( $sql, __METHOD__ );
149 }
150 }
151 }
152 }
153 $i++;
154 }
155 if ( $method == 'pipe' ) {
156 fflush( $pipe );
157 pclose( $pipe );
158 }
159 $t += microtime( true );
160 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
161
162 return !$file; // we only updated *some* files, don't log
163 }
164 }
165
166 $maintClass = "PopulateImageSha1";
167 require_once RUN_MAINTENANCE_IF_MAIN;