summaryrefslogtreecommitdiff
path: root/maintenance/populateImageSha1.php
blob: e9123aa34410efd8e8746cdc21c10ff52fae79b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
 * Optional upgrade script to populate the img_sha1 field
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

require_once __DIR__ . '/Maintenance.php';

/**
 * Maintenance script to populate the img_sha1 field.
 *
 * @ingroup Maintenance
 */
class PopulateImageSha1 extends LoggedUpdateMaintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription = "Populate the img_sha1 field";
		$this->addOption( 'force', "Recalculate sha1 for rows that already have a value" );
		$this->addOption( 'multiversiononly', "Calculate only for files with several versions" );
		$this->addOption( 'method', "Use 'pipe' to pipe to mysql command line,\n" .
			"\t\tdefault uses Database class", false, true );
		$this->addOption(
			'file',
			'Fix for a specific file, without File: namespace prefixed',
			false,
			true
		);
	}

	protected function getUpdateKey() {
		return 'populate img_sha1';
	}

	protected function updateSkippedMessage() {
		return 'img_sha1 column of image table already populated.';
	}

	public function execute() {
		if ( $this->getOption( 'file' ) || $this->hasOption( 'multiversiononly' ) ) {
			$this->doDBUpdates(); // skip update log checks/saves
		} else {
			parent::execute();
		}
	}

	public function doDBUpdates() {
		$method = $this->getOption( 'method', 'normal' );
		$file = $this->getOption( 'file', '' );
		$force = $this->getOption( 'force' );
		$isRegen = ( $force || $file != '' ); // forced recalculation?

		$t = -microtime( true );
		$dbw = wfGetDB( DB_MASTER );
		if ( $file != '' ) {
			$res = $dbw->select(
				'image',
				array( 'img_name' ),
				array( 'img_name' => $file ),
				__METHOD__
			);
			if ( !$res ) {
				$this->error( "No such file: $file", true );

				return false;
			}
			$this->output( "Populating img_sha1 field for specified files\n" );
		} else {
			if ( $this->hasOption( 'multiversiononly' ) ) {
				$conds = array();
				$this->output( "Populating and recalculating img_sha1 field for versioned files\n" );
			} elseif ( $force ) {
				$conds = array();
				$this->output( "Populating and recalculating img_sha1 field\n" );
			} else {
				$conds = array( 'img_sha1' => '' );
				$this->output( "Populating img_sha1 field\n" );
			}
			if ( $this->hasOption( 'multiversiononly' ) ) {
				$res = $dbw->select( 'oldimage',
					array( 'img_name' => 'DISTINCT(oi_name)' ), $conds, __METHOD__ );
			} else {
				$res = $dbw->select( 'image', array( 'img_name' ), $conds, __METHOD__ );
			}
		}

		$imageTable = $dbw->tableName( 'image' );
		$oldImageTable = $dbw->tableName( 'oldimage' );

		if ( $method == 'pipe' ) {
			// Opening a pipe allows the SHA-1 operation to be done in parallel
			// with the database write operation, because the writes are queued
			// in the pipe buffer. This can improve performance by up to a
			// factor of 2.
			global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
			$cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
				' -h' . wfEscapeShellArg( $wgDBserver ) .
				' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
			$this->output( "Using pipe method\n" );
			$pipe = popen( $cmd, 'w' );
		}

		$numRows = $res->numRows();
		$i = 0;
		foreach ( $res as $row ) {
			if ( $i % $this->mBatchSize == 0 ) {
				$this->output( sprintf(
					"Done %d of %d, %5.3f%%  \r", $i, $numRows, $i / $numRows * 100 ) );
				wfWaitForSlaves();
			}

			$file = wfLocalFile( $row->img_name );
			if ( !$file ) {
				continue;
			}

			// Upgrade the current file version...
			$sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
			if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
				if ( $isRegen && $file->getSha1() !== $sha1 ) {
					// The population was probably done already. If the old SHA1
					// does not match, then both fix the SHA1 and the metadata.
					$file->upgradeRow();
				} else {
					$sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
						" WHERE img_name=" . $dbw->addQuotes( $file->getName() );
					if ( $method == 'pipe' ) {
						fwrite( $pipe, "$sql;\n" );
					} else {
						$dbw->query( $sql, __METHOD__ );
					}
				}
			}
			// Upgrade the old file versions...
			foreach ( $file->getHistory() as $oldFile ) {
				$sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
				if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
					if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
						// The population was probably done already. If the old SHA1
						// does not match, then both fix the SHA1 and the metadata.
						$oldFile->upgradeRow();
					} else {
						$sql = "UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
							" WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) . " AND" .
							" oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) . ")";
						if ( $method == 'pipe' ) {
							fwrite( $pipe, "$sql;\n" );
						} else {
							$dbw->query( $sql, __METHOD__ );
						}
					}
				}
			}
			$i++;
		}
		if ( $method == 'pipe' ) {
			fflush( $pipe );
			pclose( $pipe );
		}
		$t += microtime( true );
		$this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );

		return !$file; // we only updated *some* files, don't log
	}
}

$maintClass = "PopulateImageSha1";
require_once RUN_MAINTENANCE_IF_MAIN;