MediaWiki  1.23.8
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->mDescription = "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  return false;
54  }
55 
56  $this->output( "Populating rev_sha1 column\n" );
57  $rc = $this->doSha1Updates( 'revision', 'rev_id', 'rev' );
58 
59  $this->output( "Populating ar_sha1 column\n" );
60  $ac = $this->doSha1Updates( 'archive', 'ar_rev_id', 'ar' );
61  $this->output( "Populating ar_sha1 column legacy rows\n" );
62  $ac += $this->doSha1LegacyUpdates();
63 
64  $this->output( "rev_sha1 and ar_sha1 population complete [$rc revision rows, $ac archive rows].\n" );
65  return true;
66  }
67 
74  protected function doSha1Updates( $table, $idCol, $prefix ) {
75  $db = $this->getDB( DB_MASTER );
76  $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
77  $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
78  if ( !$start || !$end ) {
79  $this->output( "...$table table seems to be empty.\n" );
80  return 0;
81  }
82 
83  $count = 0;
84  # Do remaining chunk
85  $end += $this->mBatchSize - 1;
86  $blockStart = $start;
87  $blockEnd = $start + $this->mBatchSize - 1;
88  while ( $blockEnd <= $end ) {
89  $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
90  $cond = "$idCol BETWEEN $blockStart AND $blockEnd
91  AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
92  $res = $db->select( $table, '*', $cond, __METHOD__ );
93 
94  $db->begin( __METHOD__ );
95  foreach ( $res as $row ) {
96  if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
97  $count++;
98  }
99  }
100  $db->commit( __METHOD__ );
101 
102  $blockStart += $this->mBatchSize;
103  $blockEnd += $this->mBatchSize;
104  wfWaitForSlaves();
105  }
106  return $count;
107  }
108 
112  protected function doSha1LegacyUpdates() {
113  $count = 0;
114  $db = $this->getDB( DB_MASTER );
115  $res = $db->select( 'archive', '*',
116  array( 'ar_rev_id IS NULL', 'ar_sha1' => '' ), __METHOD__ );
117 
118  $updateSize = 0;
119  $db->begin( __METHOD__ );
120  foreach ( $res as $row ) {
121  if ( $this->upgradeLegacyArchiveRow( $row ) ) {
122  ++$count;
123  }
124  if ( ++$updateSize >= 100 ) {
125  $updateSize = 0;
126  $db->commit( __METHOD__ );
127  $this->output( "Commited row with ar_timestamp={$row->ar_timestamp}\n" );
128  wfWaitForSlaves();
129  $db->begin( __METHOD__ );
130  }
131  }
132  $db->commit( __METHOD__ );
133  return $count;
134  }
135 
143  protected function upgradeRow( $row, $table, $idCol, $prefix ) {
144  $db = $this->getDB( DB_MASTER );
145  try {
146  $rev = ( $table === 'archive' )
148  : new Revision( $row );
149  $text = $rev->getSerializedData();
150  } catch ( MWException $e ) {
151  $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
152  return false; // bug 22624?
153  }
154  if ( !is_string( $text ) ) {
155  # This should not happen, but sometimes does (bug 20757)
156  $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
157  return false;
158  } else {
159  $db->update( $table,
160  array( "{$prefix}_sha1" => Revision::base36Sha1( $text ) ),
161  array( $idCol => $row->$idCol ),
162  __METHOD__
163  );
164  return true;
165  }
166  }
167 
172  protected function upgradeLegacyArchiveRow( $row ) {
173  $db = $this->getDB( DB_MASTER );
174  try {
176  } catch ( MWException $e ) {
177  $this->output( "Text of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
178  return false; // bug 22624?
179  }
180  $text = $rev->getSerializedData();
181  if ( !is_string( $text ) ) {
182  # This should not happen, but sometimes does (bug 20757)
183  $this->output( "Data of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
184  return false;
185  } else {
186  # Archive table as no PK, but (NS,title,time) should be near unique.
187  # Any duplicates on those should also have duplicated text anyway.
188  $db->update( 'archive',
189  array( 'ar_sha1' => Revision::base36Sha1( $text ) ),
190  array(
191  'ar_namespace' => $row->ar_namespace,
192  'ar_title' => $row->ar_title,
193  'ar_timestamp' => $row->ar_timestamp,
194  'ar_len' => $row->ar_len // extra sanity
195  ),
196  __METHOD__
197  );
198  return true;
199  }
200  }
201 }
202 
203 $maintClass = "PopulateRevisionSha1";
204 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:97
PopulateRevisionSha1\upgradeLegacyArchiveRow
upgradeLegacyArchiveRow( $row)
Definition: populateRevisionSha1.php:172
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$maintClass
$maintClass
Definition: populateRevisionSha1.php:203
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
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:1479
Revision
Definition: Revision.php:26
PopulateRevisionSha1
Maintenance script that fills the rev_sha1 and ar_sha1 columns of revision and archive tables for rev...
Definition: populateRevisionSha1.php:33
Maintenance\getDB
& getDB( $db, $groups=array(), $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1007
MWException
MediaWiki exception.
Definition: MWException.php:26
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1209
PopulateRevisionSha1\upgradeRow
upgradeRow( $row, $table, $idCol, $prefix)
Definition: populateRevisionSha1.php:143
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
wfWaitForSlaves
wfWaitForSlaves( $maxLag=false, $wiki=false, $cluster=false)
Modern version of wfWaitForSlaves().
Definition: GlobalFunctions.php:3804
PopulateRevisionSha1\doSha1Updates
doSha1Updates( $table, $idCol, $prefix)
Definition: populateRevisionSha1.php:74
Revision\newFromArchiveRow
static newFromArchiveRow( $row, $overrides=array())
Make a fake revision object from an archive table row.
Definition: Revision.php:159
$count
$count
Definition: UtfNormalTest2.php:96
$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:1337
PopulateRevisionSha1\__construct
__construct()
Default constructor.
Definition: populateRevisionSha1.php:34
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
PopulateRevisionSha1\doSha1LegacyUpdates
doSha1LegacyUpdates()
Definition: populateRevisionSha1.php:112
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
$e
if( $useReadline) $e
Definition: eval.php:66
$res
$res
Definition: database.txt:21
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:254