MediaWiki  1.29.1
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->error( "revision table does not exist", true );
49  } elseif ( !$db->tableExists( 'archive' ) ) {
50  $this->error( "archive table does not exist", true );
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', 'rev' );
59 
60  $this->output( "Populating ar_sha1 column\n" );
61  $ac = $this->doSha1Updates( 'archive', 'ar_rev_id', '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 
77  protected function doSha1Updates( $table, $idCol, $prefix ) {
78  $db = $this->getDB( DB_MASTER );
79  $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
80  $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
81  if ( !$start || !$end ) {
82  $this->output( "...$table table seems to be empty.\n" );
83 
84  return 0;
85  }
86 
87  $count = 0;
88  # Do remaining chunk
89  $end += $this->mBatchSize - 1;
90  $blockStart = $start;
91  $blockEnd = $start + $this->mBatchSize - 1;
92  while ( $blockEnd <= $end ) {
93  $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
94  $cond = "$idCol BETWEEN $blockStart AND $blockEnd
95  AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
96  $res = $db->select( $table, '*', $cond, __METHOD__ );
97 
98  $this->beginTransaction( $db, __METHOD__ );
99  foreach ( $res as $row ) {
100  if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
101  $count++;
102  }
103  }
104  $this->commitTransaction( $db, __METHOD__ );
105 
106  $blockStart += $this->mBatchSize;
107  $blockEnd += $this->mBatchSize;
108  wfWaitForSlaves();
109  }
110 
111  return $count;
112  }
113 
117  protected function doSha1LegacyUpdates() {
118  $count = 0;
119  $db = $this->getDB( DB_MASTER );
120  $res = $db->select( 'archive', '*',
121  [ 'ar_rev_id IS NULL', 'ar_sha1' => '' ], __METHOD__ );
122 
123  $updateSize = 0;
124  $this->beginTransaction( $db, __METHOD__ );
125  foreach ( $res as $row ) {
126  if ( $this->upgradeLegacyArchiveRow( $row ) ) {
127  ++$count;
128  }
129  if ( ++$updateSize >= 100 ) {
130  $updateSize = 0;
131  $this->commitTransaction( $db, __METHOD__ );
132  $this->output( "Commited row with ar_timestamp={$row->ar_timestamp}\n" );
133  wfWaitForSlaves();
134  $this->beginTransaction( $db, __METHOD__ );
135  }
136  }
137  $this->commitTransaction( $db, __METHOD__ );
138 
139  return $count;
140  }
141 
149  protected function upgradeRow( $row, $table, $idCol, $prefix ) {
150  $db = $this->getDB( DB_MASTER );
151  try {
152  $rev = ( $table === 'archive' )
154  : new Revision( $row );
155  $text = $rev->getSerializedData();
156  } catch ( Exception $e ) {
157  $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
158 
159  return false; // T24624?
160  }
161  if ( !is_string( $text ) ) {
162  # This should not happen, but sometimes does (T22757)
163  $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
164 
165  return false;
166  } else {
167  $db->update( $table,
168  [ "{$prefix}_sha1" => Revision::base36Sha1( $text ) ],
169  [ $idCol => $row->$idCol ],
170  __METHOD__
171  );
172 
173  return true;
174  }
175  }
176 
181  protected function upgradeLegacyArchiveRow( $row ) {
182  $db = $this->getDB( DB_MASTER );
183  try {
185  } catch ( Exception $e ) {
186  $this->output( "Text of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
187 
188  return false; // T24624?
189  }
190  $text = $rev->getSerializedData();
191  if ( !is_string( $text ) ) {
192  # This should not happen, but sometimes does (T22757)
193  $this->output( "Data of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
194 
195  return false;
196  } else {
197  # Archive table as no PK, but (NS,title,time) should be near unique.
198  # Any duplicates on those should also have duplicated text anyway.
199  $db->update( 'archive',
200  [ 'ar_sha1' => Revision::base36Sha1( $text ) ],
201  [
202  'ar_namespace' => $row->ar_namespace,
203  'ar_title' => $row->ar_title,
204  'ar_timestamp' => $row->ar_timestamp,
205  'ar_len' => $row->ar_len // extra sanity
206  ],
207  __METHOD__
208  );
209 
210  return true;
211  }
212  }
213 }
214 
215 $maintClass = "PopulateRevisionSha1";
216 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:189
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:103
PopulateRevisionSha1\upgradeLegacyArchiveRow
upgradeLegacyArchiveRow( $row)
Definition: populateRevisionSha1.php:181
$maintClass
$maintClass
Definition: populateRevisionSha1.php:215
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
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
$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:1578
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:3214
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:33
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1278
PopulateRevisionSha1
Maintenance script that fills the rev_sha1 and ar_sha1 columns of revision and archive tables for rev...
Definition: populateRevisionSha1.php:33
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1562
PopulateRevisionSha1\upgradeRow
upgradeRow( $row, $table, $idCol, $prefix)
Definition: populateRevisionSha1.php:149
DB_MASTER
const DB_MASTER
Definition: defines.php:26
PopulateRevisionSha1\doSha1Updates
doSha1Updates( $table, $idCol, $prefix)
Definition: populateRevisionSha1.php:77
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1293
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:1741
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:1251
PopulateRevisionSha1\doSha1LegacyUpdates
doSha1LegacyUpdates()
Definition: populateRevisionSha1.php:117
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:314