MediaWiki 1.40.4
resolveStubs.php
Go to the documentation of this file.
1<?php
28
29require_once __DIR__ . '/../Maintenance.php';
30
33 private $undoLog;
34
35 public function __construct() {
36 parent::__construct();
37 $this->setBatchSize( 1000 );
38 $this->addOption( 'dry-run', 'Don\'t update any rows' );
39 $this->addOption( 'undo', 'Undo log location', false, true );
40 }
41
46 public function execute() {
47 $dbw = $this->getDB( DB_PRIMARY );
48 $dbr = $this->getDB( DB_REPLICA );
49 $maxID = $dbr->selectField( 'text', 'MAX(old_id)', '', __METHOD__ );
50 $blockSize = $this->getBatchSize();
51 $dryRun = $this->getOption( 'dry-run' );
52 $this->setUndoLog( new UndoLog( $this->getOption( 'undo' ), $dbw ) );
53
54 $numBlocks = intval( $maxID / $blockSize ) + 1;
55 $numResolved = 0;
56 $numTotal = 0;
57 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
58
59 for ( $b = 0; $b < $numBlocks; $b++ ) {
60 $lbFactory->waitForReplication();
61
62 $this->output( sprintf( "%5.2f%%\n", $b / $numBlocks * 100 ) );
63 $start = $blockSize * $b + 1;
64 $end = $blockSize * ( $b + 1 );
65
66 $res = $dbr->select( 'text', [ 'old_id', 'old_text', 'old_flags' ],
67 "old_id>=$start AND old_id<=$end " .
68 "AND old_flags LIKE '%object%' AND old_flags NOT LIKE '%external%' " .
69 // LOWER() doesn't work on binary text, need to convert
70 'AND LOWER(CONVERT(LEFT(old_text,22) USING latin1)) = \'o:15:"historyblobstub"\'',
71 __METHOD__ );
72 foreach ( $res as $row ) {
73 $numResolved += $this->resolveStub( $row, $dryRun ) ? 1 : 0;
74 $numTotal++;
75 }
76 }
77 $this->output( "100%\n" );
78 $this->output( "$numResolved of $numTotal stubs resolved\n" );
79 }
80
84 public function setUndoLog( UndoLog $undoLog ) {
85 $this->undoLog = $undoLog;
86 }
87
97 public function resolveStub( $row, $dryRun ) {
98 $id = $row->old_id;
99 $stub = unserialize( $row->old_text );
100 $flags = SqlBlobStore::explodeFlags( $row->old_flags );
101
102 $dbr = $this->getDB( DB_REPLICA );
103
104 if ( !( $stub instanceof HistoryBlobStub ) ) {
105 print "Error at old_id $id: found object of class " . get_class( $stub ) .
106 ", expecting HistoryBlobStub\n";
107 return false;
108 }
109
110 $mainId = $stub->getLocation();
111 if ( !$mainId ) {
112 print "Error at old_id $id: falsey location\n";
113 return false;
114 }
115
116 # Get the main text row
117 $mainTextRow = $dbr->selectRow(
118 'text',
119 [ 'old_text', 'old_flags' ],
120 [ 'old_id' => $mainId ],
121 __METHOD__
122 );
123
124 if ( !$mainTextRow ) {
125 print "Error at old_id $id: can't find main text row old_id $mainId\n";
126 return false;
127 }
128
129 $mainFlags = SqlBlobStore::explodeFlags( $mainTextRow->old_flags );
130 $mainText = $mainTextRow->old_text;
131
132 if ( !in_array( 'external', $mainFlags ) ) {
133 print "Error at old_id $id: target $mainId is not external\n";
134 return false;
135 }
136 if ( preg_match( '!^DB://([^/]*)/([^/]*)/[0-9a-f]{32}$!', $mainText ) ) {
137 print "Error at old_id $id: target $mainId is a CGZ pointer\n";
138 return false;
139 }
140 if ( preg_match( '!^DB://([^/]*)/([^/]*)/[0-9]{1,6}$!', $mainText ) ) {
141 print "Error at old_id $id: target $mainId is a DHB pointer\n";
142 return false;
143 }
144 if ( !preg_match( '!^DB://([^/]*)/([^/]*)$!', $mainText ) ) {
145 print "Error at old_id $id: target $mainId has unrecognised text\n";
146 return false;
147 }
148
149 # Preserve the legacy encoding flag, but switch from object to external
150 if ( in_array( 'utf-8', $flags ) ) {
151 $newFlags = 'utf-8,external';
152 } else {
153 $newFlags = 'external';
154 }
155 $newText = $mainText . '/' . $stub->getHash();
156
157 # Update the row
158 if ( $dryRun ) {
159 $this->output( "Resolve $id => $newFlags $newText\n" );
160 } else {
161 $updated = $this->undoLog->update(
162 'text',
163 [
164 'old_flags' => $newFlags,
165 'old_text' => $newText
166 ],
167 (array)$row,
168 __METHOD__
169 );
170 if ( !$updated ) {
171 $this->output( "Updated of old_id $id failed to match\n" );
172 return false;
173 }
174 }
175 return true;
176 }
177}
178
179$maintClass = ResolveStubs::class;
180require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Pointer object for an item within a CGZ blob stored in the text table.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
getBatchSize()
Returns batch size.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
Update a database while optionally writing SQL that reverses the update to a file.
Definition UndoLog.php:11
Service locator for MediaWiki core services.
Service for storing and loading Content objects representing revision data blobs.
setUndoLog(UndoLog $undoLog)
resolveStub( $row, $dryRun)
Resolve a history stub.
__construct()
Default constructor.
execute()
Convert history stubs that point to an external row to direct external pointers.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28
$maintClass