Use TOCData methods to process new headings
[mediawiki.git] / maintenance / populateRevisionSha1.php
blob9286a11e84852e18a3b46db0dcfbf182e41d8284
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 use MediaWiki\MediaWikiServices;
29 /**
30 * Maintenance script that fills the rev_sha1 and ar_sha1 columns of revision
31 * and archive tables for revisions created before MW 1.19.
33 * @ingroup Maintenance
35 class PopulateRevisionSha1 extends LoggedUpdateMaintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Populates the rev_sha1 and ar_sha1 fields' );
39 $this->setBatchSize( 200 );
42 protected function getUpdateKey() {
43 return 'populate rev_sha1';
46 protected function doDBUpdates() {
47 $db = $this->getDB( DB_PRIMARY );
49 if ( !$db->tableExists( 'revision', __METHOD__ ) ) {
50 $this->fatalError( "revision table does not exist" );
51 } elseif ( !$db->tableExists( 'archive', __METHOD__ ) ) {
52 $this->fatalError( "archive table does not exist" );
53 } elseif ( !$db->fieldExists( 'revision', 'rev_sha1', __METHOD__ ) ) {
54 $this->output( "rev_sha1 column does not exist\n\n", true );
55 return false;
58 $revStore = MediaWikiServices::getInstance()->getRevisionStore();
60 $this->output( "Populating rev_sha1 column\n" );
61 $rc = $this->doSha1Updates( $revStore, 'revision', 'rev_id',
62 $revStore->getQueryInfo(), 'rev'
65 $this->output( "Populating ar_sha1 column\n" );
66 $ac = $this->doSha1Updates( $revStore, 'archive', 'ar_rev_id',
67 $revStore->getArchiveQueryInfo(), 'ar'
70 $this->output( "rev_sha1 and ar_sha1 population complete "
71 . "[$rc revision rows, $ac archive rows].\n" );
73 return true;
76 /**
77 * @param MediaWiki\Revision\RevisionStore $revStore
78 * @param string $table
79 * @param string $idCol
80 * @param array $queryInfo
81 * @param string $prefix
82 * @return int Rows changed
84 protected function doSha1Updates( $revStore, $table, $idCol, $queryInfo, $prefix ) {
85 $db = $this->getDB( DB_PRIMARY );
86 $batchSize = $this->getBatchSize();
87 $start = $db->selectField( $table, "MIN($idCol)", '', __METHOD__ );
88 $end = $db->selectField( $table, "MAX($idCol)", '', __METHOD__ );
89 if ( !$start || !$end ) {
90 $this->output( "...$table table seems to be empty.\n" );
92 return 0;
95 $count = 0;
96 # Do remaining chunk
97 $end += $batchSize - 1;
98 $blockStart = $start;
99 $blockEnd = $start + $batchSize - 1;
100 while ( $blockEnd <= $end ) {
101 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
103 $cond = "$idCol BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd .
104 " AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
105 $res = $db->select(
106 $queryInfo['tables'], $queryInfo['fields'], $cond, __METHOD__, [], $queryInfo['joins']
109 $this->beginTransaction( $db, __METHOD__ );
110 foreach ( $res as $row ) {
111 if ( $this->upgradeRow( $revStore, $row, $table, $idCol, $prefix ) ) {
112 $count++;
115 $this->commitTransaction( $db, __METHOD__ );
117 $blockStart += $batchSize;
118 $blockEnd += $batchSize;
121 return $count;
125 * @param MediaWiki\Revision\RevisionStore $revStore
126 * @param stdClass $row
127 * @param string $table
128 * @param string $idCol
129 * @param string $prefix
130 * @return bool
132 protected function upgradeRow( $revStore, $row, $table, $idCol, $prefix ) {
133 $db = $this->getDB( DB_PRIMARY );
135 // Create a revision and use it to get the sha1 from the content table, if possible.
136 try {
137 $rev = ( $table === 'archive' )
138 ? $revStore->newRevisionFromArchiveRow( $row )
139 : $revStore->newRevisionFromRow( $row );
140 $sha1 = $rev->getSha1();
141 } catch ( Exception $e ) {
142 $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
143 return false; // T24624? T22757?
146 $db->update( $table,
147 [ "{$prefix}_sha1" => $sha1 ],
148 [ $idCol => $row->$idCol ],
149 __METHOD__
152 return true;
156 $maintClass = PopulateRevisionSha1::class;
157 require_once RUN_MAINTENANCE_IF_MAIN;