summaryrefslogtreecommitdiff
path: root/maintenance/populateFilearchiveSha1.php
blob: c579d4fcd949cbc3d9bd8ede8d027fc5f8fa3d1a (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
<?php
/**
 * Optional upgrade script to populate the fa_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 dirname( __FILE__ ) . '/Maintenance.php';

/**
 * Maintenance script to populate the fa_sha1 field.
 *
 * @ingroup Maintenance
 * @since 1.21
 */
class PopulateFilearchiveSha1 extends LoggedUpdateMaintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription = "Populate the fa_sha1 field from fa_storage_key";
	}

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

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

	public function doDBUpdates() {
		$startTime = microtime( true );
		$dbw = wfGetDB( DB_MASTER );
		$table = 'filearchive';
		$conds = array( 'fa_sha1' => '', 'fa_storage_key IS NOT NULL' );

		if ( !$dbw->fieldExists( $table, 'fa_sha1', __METHOD__ ) ) {
			$this->output( "fa_sha1 column does not exist\n\n", true );
			return false;
		}

		$this->output( "Populating fa_sha1 field from fa_storage_key\n" );
		$endId = $dbw->selectField( $table, 'MAX(fa_id)', false, __METHOD__ );

		$batchSize = $this->mBatchSize;
		$done = 0;

		do {
			$res = $dbw->select(
				$table,
				array( 'fa_id', 'fa_storage_key' ),
				$conds,
				__METHOD__,
				array( 'LIMIT' => $batchSize )
			);

			$i = 0;
			foreach ( $res as $row ) {
				if ( $row->fa_storage_key == '' ) {
					// Revision was missing pre-deletion
					continue;
				}
				$sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
				$dbw->update( $table,
					array( 'fa_sha1' => $sha1 ),
					array( 'fa_id' => $row->fa_id ),
					__METHOD__
				);
				$lastId = $row->fa_id;
				$i++;
			}

			$done += $i;
			if ( $i !== $batchSize ) {
				break;
			}

			// print status and let slaves catch up
			$this->output( sprintf(
				"id %d done (up to %d), %5.3f%%  \r", $lastId, $endId, $lastId / $endId * 100 ) );
			wfWaitForSlaves();
		} while ( true );

		$processingTime = microtime( true ) - $startTime;
		$this->output( sprintf( "\nDone %d files in %.1f seconds\n", $done, $processingTime ) );

		return true; // we only updated *some* files, don't log
	}
}

$maintClass = "PopulateFilearchiveSha1";
require_once RUN_MAINTENANCE_IF_MAIN;