MediaWiki 1.41.2
populateRevisionSha1.php
Go to the documentation of this file.
1<?php
25require_once __DIR__ . '/Maintenance.php';
26
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Populates the rev_sha1 and ar_sha1 fields' );
37 $this->setBatchSize( 200 );
38 }
39
40 protected function getUpdateKey() {
41 return 'populate rev_sha1';
42 }
43
44 protected function doDBUpdates() {
45 $db = $this->getDB( DB_PRIMARY );
46
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;
54 }
55
56 $revStore = $this->getServiceContainer()->getRevisionStore();
57
58 $this->output( "Populating rev_sha1 column\n" );
59 $rc = $this->doSha1Updates( $revStore, 'revision', 'rev_id',
60 $revStore->getQueryInfo(), 'rev'
61 );
62
63 $this->output( "Populating ar_sha1 column\n" );
64 $ac = $this->doSha1Updates( $revStore, 'archive', 'ar_rev_id',
65 $revStore->getArchiveQueryInfo(), 'ar'
66 );
67
68 $this->output( "rev_sha1 and ar_sha1 population complete "
69 . "[$rc revision rows, $ac archive rows].\n" );
70
71 return true;
72 }
73
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" );
95
96 return 0;
97 }
98
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" );
106
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']
111 );
112
113 $this->beginTransaction( $db, __METHOD__ );
114 foreach ( $res as $row ) {
115 if ( $this->upgradeRow( $revStore, $row, $table, $idCol, $prefix ) ) {
116 $count++;
117 }
118 }
119 $this->commitTransaction( $db, __METHOD__ );
120
121 $blockStart += $batchSize;
122 $blockEnd += $batchSize;
123 }
124
125 return $count;
126 }
127
136 protected function upgradeRow( $revStore, $row, $table, $idCol, $prefix ) {
137 $db = $this->getDB( DB_PRIMARY );
138
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?
148 }
149
150 $db->update( $table,
151 [ "{$prefix}_sha1" => $sha1 ],
152 [ $idCol => $row->$idCol ],
153 __METHOD__
154 );
155
156 return true;
157 }
158}
159
160$maintClass = PopulateRevisionSha1::class;
161require_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.
getServiceContainer()
Returns the main service container.
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.
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