3 * Fills the rev_sha1 and ar_sha1 columns of revision
4 * and archive tables for revisions created before MW 1.19.
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.
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.
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
22 * @ingroup Maintenance
25 require_once( __DIR__
. '/Maintenance.php' );
28 * Maintenance script that fills the rev_sha1 and ar_sha1 columns of revision
29 * and archive tables for revisions created before MW 1.19.
31 * @ingroup Maintenance
33 class PopulateRevisionSha1
extends LoggedUpdateMaintenance
{
34 public function __construct() {
35 parent
::__construct();
36 $this->mDescription
= "Populates the rev_sha1 and ar_sha1 fields";
37 $this->setBatchSize( 200 );
40 protected function getUpdateKey() {
41 return 'populate rev_sha1';
44 protected function doDBUpdates() {
45 $db = $this->getDB( DB_MASTER
);
47 if ( !$db->tableExists( 'revision' ) ) {
48 $this->error( "revision table does not exist", true );
49 } elseif ( !$db->tableExists( 'archive' ) ) {
50 $this->error( "archive table does not exist", true );
53 $this->output( "Populating rev_sha1 column\n" );
54 $rc = $this->doSha1Updates( 'revision', 'rev_id', 'rev' );
56 $this->output( "Populating ar_sha1 column\n" );
57 $ac = $this->doSha1Updates( 'archive', 'ar_rev_id', 'ar' );
58 $this->output( "Populating ar_sha1 column legacy rows\n" );
59 $ac +
= $this->doSha1LegacyUpdates();
61 $this->output( "rev_sha1 and ar_sha1 population complete [$rc revision rows, $ac archive rows].\n" );
66 * @param $table string
68 * @param $prefix string
69 * @return Integer Rows changed
71 protected function doSha1Updates( $table, $idCol, $prefix ) {
72 $db = $this->getDB( DB_MASTER
);
73 $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__
);
74 $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__
);
75 if ( !$start ||
!$end ) {
76 $this->output( "...$table table seems to be empty.\n" );
82 $end +
= $this->mBatchSize
- 1;
84 $blockEnd = $start +
$this->mBatchSize
- 1;
85 while ( $blockEnd <= $end ) {
86 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
87 $cond = "$idCol BETWEEN $blockStart AND $blockEnd
88 AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
89 $res = $db->select( $table, '*', $cond, __METHOD__
);
91 $db->begin( __METHOD__
);
92 foreach ( $res as $row ) {
93 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
97 $db->commit( __METHOD__
);
99 $blockStart +
= $this->mBatchSize
;
100 $blockEnd +
= $this->mBatchSize
;
109 protected function doSha1LegacyUpdates() {
111 $db = $this->getDB( DB_MASTER
);
112 $res = $db->select( 'archive', '*',
113 array( 'ar_rev_id IS NULL', 'ar_sha1' => '' ), __METHOD__
);
116 $db->begin( __METHOD__
);
117 foreach ( $res as $row ) {
118 if ( $this->upgradeLegacyArchiveRow( $row ) ) {
121 if ( ++
$updateSize >= 100 ) {
123 $db->commit( __METHOD__
);
124 $this->output( "Commited row with ar_timestamp={$row->ar_timestamp}\n" );
126 $db->begin( __METHOD__
);
129 $db->commit( __METHOD__
);
140 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
141 $db = $this->getDB( DB_MASTER
);
143 $rev = ( $table === 'archive' )
144 ? Revision
::newFromArchiveRow( $row )
145 : new Revision( $row );
146 $text = $rev->getRawText();
147 } catch ( MWException
$e ) {
148 $this->output( "Text of revision with {$idCol}={$row->$idCol} unavailable!\n" );
149 return false; // bug 22624?
151 if ( !is_string( $text ) ) {
152 # This should not happen, but sometimes does (bug 20757)
153 $this->output( "Text of revision with {$idCol}={$row->$idCol} unavailable!\n" );
157 array( "{$prefix}_sha1" => Revision
::base36Sha1( $text ) ),
158 array( $idCol => $row->$idCol ),
169 protected function upgradeLegacyArchiveRow( $row ) {
170 $db = $this->getDB( DB_MASTER
);
172 $rev = Revision
::newFromArchiveRow( $row );
173 } catch ( MWException
$e ) {
174 $this->output( "Text of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
175 return false; // bug 22624?
177 $text = $rev->getRawText();
178 if ( !is_string( $text ) ) {
179 # This should not happen, but sometimes does (bug 20757)
180 $this->output( "Text of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
183 # Archive table as no PK, but (NS,title,time) should be near unique.
184 # Any duplicates on those should also have duplicated text anyway.
185 $db->update( 'archive',
186 array( 'ar_sha1' => Revision
::base36Sha1( $text ) ),
188 'ar_namespace' => $row->ar_namespace
,
189 'ar_title' => $row->ar_title
,
190 'ar_timestamp' => $row->ar_timestamp
,
191 'ar_len' => $row->ar_len
// extra sanity
200 $maintClass = "PopulateRevisionSha1";
201 require_once( RUN_MAINTENANCE_IF_MAIN
);