Followup r92081: SQL fix for cleanupUploadStash.php on PostgreSQL (bug 32822)
[lhc/web/wiklou.git] / maintenance / populateRevisionSha1.php
1 <?php
2 /**
3 * Fills the rev_sha1 and ar_sha1 columns of revision
4 * and archive tables for revisions created before MW 1.19.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @ingroup Maintenance
22 */
23
24 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
25
26 class PopulateRevisionSha1 extends LoggedUpdateMaintenance {
27 public function __construct() {
28 parent::__construct();
29 $this->mDescription = "Populates the rev_sha1 and ar_sha1 fields";
30 $this->setBatchSize( 200 );
31 }
32
33 protected function getUpdateKey() {
34 return 'populate rev_sha1';
35 }
36
37 protected function doDBUpdates() {
38 $db = $this->getDB( DB_MASTER );
39 if ( !$db->tableExists( 'revision' ) ) {
40 $this->error( "revision table does not exist", true );
41 }
42 if ( !$db->tableExists( 'archive' ) ) {
43 $this->error( "archive table does not exist", true );
44 }
45
46 $this->output( "Populating rev_sha1 column\n" );
47 $rc = $this->doSha1Updates( $db, 'revision', 'rev_id', 'rev' );
48
49 $this->output( "Populating ar_sha1 column\n" );
50 $ac = $this->doSha1Updates( $db, 'archive', 'ar_rev_id', 'ar' );
51
52 $this->output( "rev_sha1 and ar_sha1 population complete [$rc revision rows, $ac archive rows].\n" );
53 return true;
54 }
55
56 /**
57 * @return Integer Rows changed
58 */
59 protected function doSha1Updates( $db, $table, $idCol, $prefix ) {
60 $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
61 $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
62 if ( !$start || !$end ) {
63 $this->output( "...$table table seems to be empty.\n" );
64 return true;
65 }
66
67 $count = 0;
68 # Do remaining chunk
69 $end += $this->mBatchSize - 1;
70 $blockStart = $start;
71 $blockEnd = $start + $this->mBatchSize - 1;
72 while ( $blockEnd <= $end ) {
73 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
74 $cond = "$idCol BETWEEN $blockStart AND $blockEnd
75 AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
76 $res = $db->select( $table, '*', $cond, __METHOD__ );
77
78 $db->begin();
79 foreach ( $res as $row ) {
80 if ( $table === 'archive' ) {
81 $rev = Revision::newFromArchiveRow( $row );
82 } else {
83 $rev = new Revision( $row );
84 }
85 $text = $rev->getRawText();
86 if ( !is_string( $text ) ) {
87 # This should not happen, but sometimes does (bug 20757)
88 $this->output( "Text of revision {$row->$idCol} unavailable!\n" );
89 } else {
90 $db->update( $table,
91 array( "{$prefix}_sha1" => Revision::base36Sha1( $text ) ),
92 array( $idCol => $row->$idCol ),
93 __METHOD__ );
94 $count++;
95 }
96 }
97 $db->commit();
98
99 $blockStart += $this->mBatchSize;
100 $blockEnd += $this->mBatchSize;
101 wfWaitForSlaves();
102 }
103 return $count;
104 }
105 }
106
107 $maintClass = "PopulateRevisionSha1";
108 require_once( RUN_MAINTENANCE_IF_MAIN );