MediaWiki 1.40.4
populateRevisionSha1.php
Go to the documentation of this file.
1<?php
25require_once __DIR__ . '/Maintenance.php';
26
28
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Populates the rev_sha1 and ar_sha1 fields' );
39 $this->setBatchSize( 200 );
40 }
41
42 protected function getUpdateKey() {
43 return 'populate rev_sha1';
44 }
45
46 protected function doDBUpdates() {
47 $db = $this->getDB( DB_PRIMARY );
48
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;
56 }
57
58 $revStore = MediaWikiServices::getInstance()->getRevisionStore();
59
60 $this->output( "Populating rev_sha1 column\n" );
61 $rc = $this->doSha1Updates( $revStore, 'revision', 'rev_id',
62 $revStore->getQueryInfo(), 'rev'
63 );
64
65 $this->output( "Populating ar_sha1 column\n" );
66 $ac = $this->doSha1Updates( $revStore, 'archive', 'ar_rev_id',
67 $revStore->getArchiveQueryInfo(), 'ar'
68 );
69
70 $this->output( "rev_sha1 and ar_sha1 population complete "
71 . "[$rc revision rows, $ac archive rows].\n" );
72
73 return true;
74 }
75
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" );
91
92 return 0;
93 }
94
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" );
102
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']
107 );
108
109 $this->beginTransaction( $db, __METHOD__ );
110 foreach ( $res as $row ) {
111 if ( $this->upgradeRow( $revStore, $row, $table, $idCol, $prefix ) ) {
112 $count++;
113 }
114 }
115 $this->commitTransaction( $db, __METHOD__ );
116
117 $blockStart += $batchSize;
118 $blockEnd += $batchSize;
119 }
120
121 return $count;
122 }
123
132 protected function upgradeRow( $revStore, $row, $table, $idCol, $prefix ) {
133 $db = $this->getDB( DB_PRIMARY );
134
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?
144 }
145
146 $db->update( $table,
147 [ "{$prefix}_sha1" => $sha1 ],
148 [ $idCol => $row->$idCol ],
149 __METHOD__
150 );
151
152 return true;
153 }
154}
155
156$maintClass = PopulateRevisionSha1::class;
157require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
Maintenance script that fills the rev_sha1 and ar_sha1 columns of revision and archive tables for rev...
getUpdateKey()
Get the update key name to go in the update log table.
__construct()
Default constructor.
upgradeRow( $revStore, $row, $table, $idCol, $prefix)
doSha1Updates( $revStore, $table, $idCol, $queryInfo, $prefix)
doDBUpdates()
Do the actual work.
const DB_PRIMARY
Definition defines.php:28