* Added LoggedUpdateMaintenance subclass
[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 updateSkippedMessage() {
38 return 'rev_sha1 column of revision table already populated.';
39 }
40
41 protected function updatelogFailedMessage() {
42 return 'Could not insert rev_sha1 population row.';
43 }
44
45 protected function doDBUpdates() {
46 $db = $this->getDB( DB_MASTER );
47 if ( !$db->tableExists( 'revision' ) ) {
48 $this->error( "revision table does not exist", true );
49 }
50 if ( !$db->tableExists( 'archive' ) ) {
51 $this->error( "archive table does not exist", true );
52 }
53
54 $this->output( "Populating rev_sha1 column\n" );
55 $rc = $this->doSha1Updates( $db, 'revision', 'rev_id', 'rev' );
56
57 $this->output( "Populating ar_sha1 column\n" );
58 $ac = $this->doSha1Updates( $db, 'archive', 'ar_rev_id', 'ar' );
59
60 $this->output( "rev_sha1 and ar_sha1 population complete [$rc revision rows, $ac archive rows].\n" );
61 return true;
62 }
63
64 /**
65 * @return Integer Rows changed
66 */
67 protected function doSha1Updates( $db, $table, $idCol, $prefix ) {
68 $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
69 $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
70 if ( !$start || !$end ) {
71 $this->output( "...revision table seems to be empty.\n" );
72 return true;
73 }
74
75 $count = 0;
76 # Do remaining chunk
77 $end += $this->mBatchSize - 1;
78 $blockStart = $start;
79 $blockEnd = $start + $this->mBatchSize - 1;
80 while ( $blockEnd <= $end ) {
81 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
82 $cond = "$idCol BETWEEN $blockStart AND $blockEnd
83 AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
84 $res = $db->select( $table, '*', $cond, __METHOD__ );
85
86 $db->begin();
87 foreach ( $res as $row ) {
88 if ( $table === 'archive' ) {
89 $rev = Revision::newFromArchiveRow( $row );
90 } else {
91 $rev = new Revision( $row );
92 }
93 $text = $rev->getRawText();
94 if ( !is_string( $text ) ) {
95 # This should not happen, but sometimes does (bug 20757)
96 $this->output( "Text of revision {$row->$idCol} unavailable!\n" );
97 } else {
98 $db->update( $table,
99 array( "{$prefix}_sha1" => Revision::base36Sha1( $text ) ),
100 array( $idCol => $row->$idCol ),
101 __METHOD__ );
102 $count++;
103 }
104 }
105 $db->commit();
106
107 $blockStart += $this->mBatchSize;
108 $blockEnd += $this->mBatchSize;
109 wfWaitForSlaves();
110 }
111 return $count;
112 }
113 }
114
115 $maintClass = "PopulateRevisionSha1";
116 require_once( RUN_MAINTENANCE_IF_MAIN );