MediaWiki master
resolveStubs.php
Go to the documentation of this file.
1<?php
14
15// @codeCoverageIgnoreStart
16require_once __DIR__ . '/../Maintenance.php';
17// @codeCoverageIgnoreEnd
18
21 private $undoLog;
22
23 public function __construct() {
24 parent::__construct();
25 $this->setBatchSize( 1000 );
26 $this->addOption( 'dry-run', 'Don\'t update any rows' );
27 $this->addOption( 'undo', 'Undo log location', false, true );
28 }
29
34 public function execute() {
35 $dbw = $this->getPrimaryDB();
36 $dbr = $this->getReplicaDB();
37 $maxID = $dbr->newSelectQueryBuilder()
38 ->select( 'MAX(old_id)' )
39 ->from( 'text' )
40 ->caller( __METHOD__ )->fetchField();
41 $blockSize = $this->getBatchSize();
42 $dryRun = $this->getOption( 'dry-run' );
43 $this->setUndoLog( new UndoLog( $this->getOption( 'undo' ), $dbw ) );
44
45 $numBlocks = intval( $maxID / $blockSize ) + 1;
46 $numResolved = 0;
47 $numTotal = 0;
48
49 for ( $b = 0; $b < $numBlocks; $b++ ) {
50 $this->waitForReplication();
51
52 $this->output( sprintf( "%5.2f%%\n", $b / $numBlocks * 100 ) );
53 $start = $blockSize * $b + 1;
54 $end = $blockSize * ( $b + 1 );
55
56 $res = $dbr->newSelectQueryBuilder()
57 ->select( [ 'old_id', 'old_text', 'old_flags' ] )
58 ->from( 'text' )
59 ->where(
60 "old_id>=$start AND old_id<=$end " .
61 "AND old_flags LIKE '%object%' AND old_flags NOT LIKE '%external%' " .
62 // LOWER() doesn't work on binary text, need to convert
63 'AND LOWER(CONVERT(LEFT(old_text,22) USING latin1)) = \'o:15:"historyblobstub"\''
64 )
65 ->caller( __METHOD__ )->fetchResultSet();
66 foreach ( $res as $row ) {
67 $numResolved += $this->resolveStub( $row, $dryRun ) ? 1 : 0;
68 $numTotal++;
69 }
70 }
71 $this->output( "100%\n" );
72 $this->output( "$numResolved of $numTotal stubs resolved\n" );
73 }
74
75 public function setUndoLog( UndoLog $undoLog ) {
76 $this->undoLog = $undoLog;
77 }
78
88 public function resolveStub( $row, $dryRun ) {
89 $id = $row->old_id;
90 $stub = unserialize( $row->old_text );
91 $flags = SqlBlobStore::explodeFlags( $row->old_flags );
92
93 $dbr = $this->getReplicaDB();
94
95 if ( !( $stub instanceof HistoryBlobStub ) ) {
96 print "Error at old_id $id: found object of class " . get_class( $stub ) .
97 ", expecting HistoryBlobStub\n";
98 return false;
99 }
100
101 $mainId = $stub->getLocation();
102 if ( !$mainId ) {
103 print "Error at old_id $id: falsey location\n";
104 return false;
105 }
106
107 # Get the main text row
108 $mainTextRow = $dbr->newSelectQueryBuilder()
109 ->select( [ 'old_text', 'old_flags' ] )
110 ->from( 'text' )
111 ->where( [ 'old_id' => $mainId ] )
112 ->caller( __METHOD__ )->fetchRow();
113
114 if ( !$mainTextRow ) {
115 print "Error at old_id $id: can't find main text row old_id $mainId\n";
116 return false;
117 }
118
119 $mainFlags = SqlBlobStore::explodeFlags( $mainTextRow->old_flags );
120 $mainText = $mainTextRow->old_text;
121
122 if ( !in_array( 'external', $mainFlags ) ) {
123 print "Error at old_id $id: target $mainId is not external\n";
124 return false;
125 }
126 if ( preg_match( '!^DB://([^/]*)/([^/]*)/[0-9a-f]{32}$!', $mainText ) ) {
127 print "Error at old_id $id: target $mainId is a CGZ pointer\n";
128 return false;
129 }
130 if ( preg_match( '!^DB://([^/]*)/([^/]*)/[0-9]{1,6}$!', $mainText ) ) {
131 print "Error at old_id $id: target $mainId is a DHB pointer\n";
132 return false;
133 }
134 if ( !preg_match( '!^DB://([^/]*)/([^/]*)$!', $mainText ) ) {
135 print "Error at old_id $id: target $mainId has unrecognised text\n";
136 return false;
137 }
138
139 # Preserve the legacy encoding flag, but switch from object to external
140 if ( in_array( 'utf-8', $flags ) ) {
141 $newFlags = 'utf-8,external';
142 } else {
143 $newFlags = 'external';
144 }
145 $newText = $mainText . '/' . $stub->getHash();
146
147 # Update the row
148 if ( $dryRun ) {
149 $this->output( "Resolve $id => $newFlags $newText\n" );
150 } else {
151 $updated = $this->undoLog->update(
152 'text',
153 [
154 'old_flags' => $newFlags,
155 'old_text' => $newText
156 ],
157 (array)$row,
158 __METHOD__
159 );
160 if ( !$updated ) {
161 $this->output( "Updated of old_id $id failed to match\n" );
162 return false;
163 }
164 }
165 return true;
166 }
167}
168
169// @codeCoverageIgnoreStart
170$maintClass = ResolveStubs::class;
171require_once RUN_MAINTENANCE_IF_MAIN;
172// @codeCoverageIgnoreEnd
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...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
getOption( $name, $default=null)
Get an option, or return the default.
getReplicaDB(string|false $virtualDomain=false)
getPrimaryDB(string|false $virtualDomain=false)
Update a database while optionally writing SQL that reverses the update to a file.
Definition UndoLog.php:11
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.
$maintClass