Fix copy-paste mistake in r94289
[lhc/web/wiklou.git] / maintenance / populateRevisionSha1.php
1 <?php
2 /**
3 * Fills the rev_sha1 and ar_sha1 columns of revision & archive tables.
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 PopulateRevisionSha1 extends Maintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Populates the rev_sha1 and ar_sha1 fields";
29 $this->setBatchSize( 150 );
30 }
31
32 public function execute() {
33 $db = wfGetDB( DB_MASTER );
34
35 $this->output( "Populating rev_sha1 column\n" );
36 $this->doSha1Updates( $db, 'revision', 'rev_id', 'rev' );
37
38 $this->output( "Populating ar_sha1 column\n" );
39 $this->doSha1Updates( $db, 'archive', 'ar_rev_id', 'ar' );
40
41 if ( $db->insert(
42 'updatelog',
43 array( 'ul_key' => 'populate rev_sha1' ),
44 __METHOD__,
45 'IGNORE'
46 )
47 ) {
48 $this->output( "rev_sha1 and ar_sha1 population complete.\n" );
49 return true;
50 } else {
51 $this->output( "Could not insert rev_sha1 population row.\n" );
52 return false;
53 }
54 }
55
56 protected function doSha1Updates( $db, $table, $idCol, $prefix ) {
57 $start = $db->selectField( $table, "MIN($idCol)", "$idCol IS NOT NULL", __METHOD__ );
58 if ( !$start ) {
59 $this->output( "Nothing to do.\n" );
60 return true;
61 }
62 $end = $db->selectField( $table, "MAX($idCol)", "$idCol IS NOT NULL", __METHOD__ );
63
64 # Do remaining chunk
65 $end += $this->mBatchSize - 1;
66 $blockStart = $start;
67 $blockEnd = $start + $this->mBatchSize - 1;
68 while ( $blockEnd <= $end ) {
69 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
70 $cond = "$idCol BETWEEN $blockStart AND $blockEnd
71 AND $idCol IS NOT NULL AND {$prefix}_sha1 IS NOT NULL";
72 $res = $db->select( $table, '*', $cond, __METHOD__ );
73
74 $db->begin();
75 foreach ( $res as $row ) {
76 if ( $table === 'archive' ) {
77 $rev = Revision::newFromArchiveRow( $row );
78 } else {
79 $rev = new Revision( $row );
80 }
81 $db->update( $table,
82 array( "{$prefix}_sha1" => Revision::base36Sha1( $rev->getRawText() ) ),
83 array( $idCol => $row->$idCol ),
84 __METHOD__ );
85 }
86 $db->commit();
87
88 $blockStart += $this->mBatchSize;
89 $blockEnd += $this->mBatchSize;
90 wfWaitForSlaves();
91 }
92 }
93 }
94
95 $maintClass = "PopulateRevisionSha1";
96 require_once( RUN_MAINTENANCE_IF_MAIN );