MediaWiki  master
resolveStubs.php
Go to the documentation of this file.
1 <?php
26 
27 if ( !defined( 'MEDIAWIKI' ) ) {
28  $optionsWithArgs = [ 'm' ];
29 
30  require_once __DIR__ . '/../commandLine.inc';
31 
32  resolveStubs();
33 }
34 
39 function resolveStubs() {
40  $fname = 'resolveStubs';
41 
42  $dbr = wfGetDB( DB_REPLICA );
43  $maxID = $dbr->selectField( 'text', 'MAX(old_id)', '', $fname );
44  $blockSize = 10000;
45  $numBlocks = intval( $maxID / $blockSize ) + 1;
46  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
47 
48  for ( $b = 0; $b < $numBlocks; $b++ ) {
49  $lbFactory->waitForReplication();
50 
51  printf( "%5.2f%%\n", $b / $numBlocks * 100 );
52  $start = intval( $maxID / $numBlocks ) * $b + 1;
53  $end = intval( $maxID / $numBlocks ) * ( $b + 1 );
54 
55  $res = $dbr->select( 'text', [ 'old_id', 'old_text', 'old_flags' ],
56  "old_id>=$start AND old_id<=$end " .
57  "AND old_flags LIKE '%object%' AND old_flags NOT LIKE '%external%' " .
58  'AND LOWER(CONVERT(LEFT(old_text,22) USING latin1)) = \'o:15:"historyblobstub"\'',
59  $fname );
60  foreach ( $res as $row ) {
61  resolveStub( $row->old_id, $row->old_text, $row->old_flags );
62  }
63  }
64  print "100%\n";
65 }
66 
73 function resolveStub( $id, $stubText, $flags ) {
74  $fname = 'resolveStub';
75 
76  $stub = unserialize( $stubText );
77  $flags = explode( ',', $flags );
78 
79  $dbr = wfGetDB( DB_REPLICA );
80  $dbw = wfGetDB( DB_MASTER );
81 
82  if ( strtolower( get_class( $stub ) ) !== 'historyblobstub' ) {
83  print "Error found object of class " . get_class( $stub ) . ", expecting historyblobstub\n";
84 
85  return;
86  }
87 
88  # Get the (maybe) external row
89  $externalRow = $dbr->selectRow(
90  'text',
91  [ 'old_text' ],
92  [
93  'old_id' => $stub->mOldId,
94  'old_flags' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() )
95  ],
96  $fname
97  );
98 
99  if ( !$externalRow ) {
100  # Object wasn't external
101  return;
102  }
103 
104  # Preserve the legacy encoding flag, but switch from object to external
105  if ( in_array( 'utf-8', $flags ) ) {
106  $newFlags = 'external,utf-8';
107  } else {
108  $newFlags = 'external';
109  }
110 
111  # Update the row
112  # print "oldid=$id\n";
113  $dbw->update( 'text',
114  [ /* SET */
115  'old_flags' => $newFlags,
116  'old_text' => $externalRow->old_text . '/' . $stub->mHash
117  ],
118  [ /* WHERE */
119  'old_id' => $id
120  ], $fname
121  );
122 }
$optionsWithArgs
global $optionsWithArgs
Definition: commandLine.inc:24
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:155
resolveStubs
if(!defined( 'MEDIAWIKI')) resolveStubs()
Convert history stubs that point to an external row to direct external pointers.
Definition: resolveStubs.php:39
$res
$res
Definition: testCompression.php:57
$dbr
$dbr
Definition: testCompression.php:54
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2467
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
resolveStub
resolveStub( $id, $stubText, $flags)
Resolve a history stub.
Definition: resolveStubs.php:73