Merge "fix sidebar HTML escaping in CologneBlue (again)"
[lhc/web/wiklou.git] / maintenance / populateFilearchiveSha1.php
1 <?php
2 /**
3 * Optional upgrade script to populate the fa_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( dirname( __FILE__ ) . '/Maintenance.php' );
25
26 /**
27 * Maintenance script to populate the fa_sha1 field.
28 *
29 * @ingroup Maintenance
30 * @since 1.21
31 */
32 class PopulateFilearchiveSha1 extends LoggedUpdateMaintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Populate the fa_sha1 field from fa_storage_key";
36 }
37
38 protected function getUpdateKey() {
39 return 'populate fa_sha1';
40 }
41
42 protected function updateSkippedMessage() {
43 return 'fa_sha1 column of filearchive table already populated.';
44 }
45
46 public function doDBUpdates() {
47 $startTime = microtime( true );
48 $dbw = wfGetDB( DB_MASTER );
49 $table = 'filearchive';
50 $conds = array( 'fa_sha1' => '', 'fa_storage_key IS NOT NULL' );
51 $this->output( "Populating fa_sha1 field from fa_storage_key\n" );
52 $endId = $dbw->selectField( $table, 'MAX(fa_id)', false, __METHOD__ );
53
54 $batchSize = $this->mBatchSize;
55 $done = 0;
56
57 do {
58 $res = $dbw->select(
59 $table,
60 array( 'fa_id', 'fa_storage_key' ),
61 $conds,
62 __METHOD__,
63 array( 'LIMIT' => $batchSize )
64 );
65
66 $i = 0;
67 foreach ( $res as $row ) {
68 if ( $row->fa_storage_key == '' ) {
69 // Revision was missing pre-deletion
70 continue;
71 }
72 $sha1 = LocalRepo::getHashFromKey( $row->fa_storage_key );
73 $dbw->update( $table,
74 array( 'fa_sha1' => $sha1 ),
75 array( 'fa_id' => $row->fa_id ),
76 __METHOD__
77 );
78 $lastId = $row->fa_id;
79 $i++;
80 }
81
82 $done += $i;
83 if( $i !== $batchSize ) {
84 break;
85 }
86
87 // print status and let slaves catch up
88 $this->output( sprintf(
89 "id %d done (up to %d), %5.3f%% \r", $lastId, $endId, $lastId / $endId * 100 ) );
90 wfWaitForSlaves();
91 } while( true );
92
93 $processingTime = microtime( true ) - $startTime;
94 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $done, $processingTime ) );
95
96 return true; // we only updated *some* files, don't log
97 }
98 }
99
100 $maintClass = "PopulateFilearchiveSha1";
101 require_once( RUN_MAINTENANCE_IF_MAIN );