MediaWiki  1.32.5
populateRevisionSha1.php
Go to the documentation of this file.
1 <?php
25 require_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_MASTER );
46 
47  if ( !$db->tableExists( 'revision' ) ) {
48  $this->fatalError( "revision table does not exist" );
49  } elseif ( !$db->tableExists( 'archive' ) ) {
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 
54  return false;
55  }
56 
57  $this->output( "Populating rev_sha1 column\n" );
58  $rc = $this->doSha1Updates( 'revision', 'rev_id', Revision::getQueryInfo(), 'rev' );
59 
60  $this->output( "Populating ar_sha1 column\n" );
61  $ac = $this->doSha1Updates( 'archive', 'ar_rev_id', Revision::getArchiveQueryInfo(), 'ar' );
62  $this->output( "Populating ar_sha1 column legacy rows\n" );
63  $ac += $this->doSha1LegacyUpdates();
64 
65  $this->output( "rev_sha1 and ar_sha1 population complete "
66  . "[$rc revision rows, $ac archive rows].\n" );
67 
68  return true;
69  }
70 
78  protected function doSha1Updates( $table, $idCol, $queryInfo, $prefix ) {
79  $db = $this->getDB( DB_MASTER );
80  $batchSize = $this->getBatchSize();
81  $start = $db->selectField( $table, "MIN($idCol)", '', __METHOD__ );
82  $end = $db->selectField( $table, "MAX($idCol)", '', __METHOD__ );
83  if ( !$start || !$end ) {
84  $this->output( "...$table table seems to be empty.\n" );
85 
86  return 0;
87  }
88 
89  $count = 0;
90  # Do remaining chunk
91  $end += $batchSize - 1;
92  $blockStart = $start;
93  $blockEnd = $start + $batchSize - 1;
94  while ( $blockEnd <= $end ) {
95  $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
96  $cond = "$idCol BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd .
97  " AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
98  $res = $db->select(
99  $queryInfo['tables'], $queryInfo['fields'], $cond, __METHOD__, [], $queryInfo['joins']
100  );
101 
102  $this->beginTransaction( $db, __METHOD__ );
103  foreach ( $res as $row ) {
104  if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
105  $count++;
106  }
107  }
108  $this->commitTransaction( $db, __METHOD__ );
109 
110  $blockStart += $batchSize;
111  $blockEnd += $batchSize;
112  }
113 
114  return $count;
115  }
116 
120  protected function doSha1LegacyUpdates() {
121  $count = 0;
122  $db = $this->getDB( DB_MASTER );
123  $arQuery = Revision::getArchiveQueryInfo();
124  $res = $db->select( $arQuery['tables'], $arQuery['fields'],
125  [ 'ar_rev_id IS NULL', 'ar_sha1' => '' ], __METHOD__, [], $arQuery['joins'] );
126 
127  $updateSize = 0;
128  $this->beginTransaction( $db, __METHOD__ );
129  foreach ( $res as $row ) {
130  if ( $this->upgradeLegacyArchiveRow( $row ) ) {
131  ++$count;
132  }
133  if ( ++$updateSize >= 100 ) {
134  $updateSize = 0;
135  $this->commitTransaction( $db, __METHOD__ );
136  $this->output( "Commited row with ar_timestamp={$row->ar_timestamp}\n" );
137  $this->beginTransaction( $db, __METHOD__ );
138  }
139  }
140  $this->commitTransaction( $db, __METHOD__ );
141 
142  return $count;
143  }
144 
152  protected function upgradeRow( $row, $table, $idCol, $prefix ) {
153  $db = $this->getDB( DB_MASTER );
154  try {
155  $rev = ( $table === 'archive' )
157  : new Revision( $row );
158  $text = $rev->getSerializedData();
159  } catch ( Exception $e ) {
160  $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
161 
162  return false; // T24624?
163  }
164  if ( !is_string( $text ) ) {
165  # This should not happen, but sometimes does (T22757)
166  $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
167 
168  return false;
169  } else {
170  $db->update( $table,
171  [ "{$prefix}_sha1" => Revision::base36Sha1( $text ) ],
172  [ $idCol => $row->$idCol ],
173  __METHOD__
174  );
175 
176  return true;
177  }
178  }
179 
184  protected function upgradeLegacyArchiveRow( $row ) {
185  $db = $this->getDB( DB_MASTER );
186  try {
188  } catch ( Exception $e ) {
189  $this->output( "Text of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
190 
191  return false; // T24624?
192  }
193  $text = $rev->getSerializedData();
194  if ( !is_string( $text ) ) {
195  # This should not happen, but sometimes does (T22757)
196  $this->output( "Data of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
197 
198  return false;
199  } else {
200  # Archive table as no PK, but (NS,title,time) should be near unique.
201  # Any duplicates on those should also have duplicated text anyway.
202  $db->update( 'archive',
203  [ 'ar_sha1' => Revision::base36Sha1( $text ) ],
204  [
205  'ar_namespace' => $row->ar_namespace,
206  'ar_title' => $row->ar_title,
207  'ar_timestamp' => $row->ar_timestamp,
208  'ar_len' => $row->ar_len // extra sanity
209  ],
210  __METHOD__
211  );
212 
213  return true;
214  }
215  }
216 }
217 
219 require_once RUN_MAINTENANCE_IF_MAIN;
Revision\newFromArchiveRow
static newFromArchiveRow( $row, $overrides=[])
Make a fake revision object from an archive table row.
Definition: Revision.php:167
PopulateRevisionSha1\upgradeLegacyArchiveRow
upgradeLegacyArchiveRow( $row)
Definition: populateRevisionSha1.php:184
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:465
$maintClass
$maintClass
Definition: populateRevisionSha1.php:218
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:317
PopulateRevisionSha1\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateRevisionSha1.php:44
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Revision\getArchiveQueryInfo
static getArchiveQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archived revision objec...
Definition: Revision.php:535
$res
$res
Definition: database.txt:21
PopulateRevisionSha1\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateRevisionSha1.php:40
Revision\base36Sha1
static base36Sha1( $text)
Get the base 36 SHA-1 value for a string of text.
Definition: Revision.php:1151
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Revision
Definition: Revision.php:41
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1378
PopulateRevisionSha1
Maintenance script that fills the rev_sha1 and ar_sha1 columns of revision and archive tables for rev...
Definition: populateRevisionSha1.php:33
Revision\getQueryInfo
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
Definition: Revision.php:521
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1679
PopulateRevisionSha1\upgradeRow
upgradeRow( $row, $table, $idCol, $prefix)
Definition: populateRevisionSha1.php:152
PopulateRevisionSha1\doSha1Updates
doSha1Updates( $table, $idCol, $queryInfo, $prefix)
Definition: populateRevisionSha1.php:78
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2221
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1393
PopulateRevisionSha1\__construct
__construct()
Default constructor.
Definition: populateRevisionSha1.php:34
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1808
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:347
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1351
PopulateRevisionSha1\doSha1LegacyUpdates
doSha1LegacyUpdates()
Definition: populateRevisionSha1.php:120
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:414
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:355