Update git submodules
[mediawiki.git] / maintenance / populateRevisionSha1.php
blobc3314850df32ba3726f0b9d9a8d1c8f5d45427ac
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.
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
21 * @file
22 * @ingroup Maintenance
25 require_once __DIR__ . '/Maintenance.php';
27 /**
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->addDescription( '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_PRIMARY );
47 if ( !$db->tableExists( 'revision', __METHOD__ ) ) {
48 $this->fatalError( "revision table does not exist" );
49 } elseif ( !$db->tableExists( 'archive', __METHOD__ ) ) {
50 $this->fatalError( "archive table does not exist" );
51 } elseif ( !$db->fieldExists( 'revision', 'rev_sha1', __METHOD__ ) ) {
52 $this->output( "rev_sha1 column does not exist\n\n", true );
53 return false;
56 $revStore = $this->getServiceContainer()->getRevisionStore();
58 $this->output( "Populating rev_sha1 column\n" );
59 $rc = $this->doSha1Updates( $revStore, 'revision', 'rev_id',
60 $revStore->getQueryInfo(), 'rev'
63 $this->output( "Populating ar_sha1 column\n" );
64 $ac = $this->doSha1Updates( $revStore, 'archive', 'ar_rev_id',
65 $revStore->getArchiveQueryInfo(), 'ar'
68 $this->output( "rev_sha1 and ar_sha1 population complete "
69 . "[$rc revision rows, $ac archive rows].\n" );
71 return true;
74 /**
75 * @param MediaWiki\Revision\RevisionStore $revStore
76 * @param string $table
77 * @param string $idCol
78 * @param array $queryInfo
79 * @param string $prefix
80 * @return int Rows changed
82 protected function doSha1Updates( $revStore, $table, $idCol, $queryInfo, $prefix ) {
83 $db = $this->getDB( DB_PRIMARY );
84 $batchSize = $this->getBatchSize();
85 $start = $db->newSelectQueryBuilder()
86 ->select( "MIN($idCol)" )
87 ->from( $table )
88 ->caller( __METHOD__ )->fetchField();
89 $end = $db->newSelectQueryBuilder()
90 ->select( "MAX($idCol)" )
91 ->from( $table )
92 ->caller( __METHOD__ )->fetchField();
93 if ( !$start || !$end ) {
94 $this->output( "...$table table seems to be empty.\n" );
96 return 0;
99 $count = 0;
100 # Do remaining chunk
101 $end += $batchSize - 1;
102 $blockStart = $start;
103 $blockEnd = $start + $batchSize - 1;
104 while ( $blockEnd <= $end ) {
105 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
107 $cond = "$idCol BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd .
108 " AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
109 $res = $db->select(
110 $queryInfo['tables'], $queryInfo['fields'], $cond, __METHOD__, [], $queryInfo['joins']
113 $this->beginTransaction( $db, __METHOD__ );
114 foreach ( $res as $row ) {
115 if ( $this->upgradeRow( $revStore, $row, $table, $idCol, $prefix ) ) {
116 $count++;
119 $this->commitTransaction( $db, __METHOD__ );
121 $blockStart += $batchSize;
122 $blockEnd += $batchSize;
125 return $count;
129 * @param MediaWiki\Revision\RevisionStore $revStore
130 * @param stdClass $row
131 * @param string $table
132 * @param string $idCol
133 * @param string $prefix
134 * @return bool
136 protected function upgradeRow( $revStore, $row, $table, $idCol, $prefix ) {
137 $db = $this->getDB( DB_PRIMARY );
139 // Create a revision and use it to get the sha1 from the content table, if possible.
140 try {
141 $rev = ( $table === 'archive' )
142 ? $revStore->newRevisionFromArchiveRow( $row )
143 : $revStore->newRevisionFromRow( $row );
144 $sha1 = $rev->getSha1();
145 } catch ( Exception $e ) {
146 $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
147 return false; // T24624? T22757?
150 $db->update( $table,
151 [ "{$prefix}_sha1" => $sha1 ],
152 [ $idCol => $row->$idCol ],
153 __METHOD__
156 return true;
160 $maintClass = PopulateRevisionSha1::class;
161 require_once RUN_MAINTENANCE_IF_MAIN;