51 throw new InvalidArgumentException(
"LBFactory required in 'lbFactory' field." );
53 $this->lbFactory =
$params[
'lbFactory'];
67 [ $cluster, $id, $itemID ] = $this->
parseURL( $url );
68 $ret = $this->fetchBlob( $cluster, $id, $itemID );
70 if ( $itemID !==
false && $ret !==
false ) {
71 return $ret->getItem( $itemID );
88 $batched = $inverseUrlMap = [];
89 foreach ( $urls as $url ) {
90 [ $cluster, $id, $itemID ] = $this->
parseURL( $url );
91 $batched[$cluster][$id][] = $itemID;
94 $inverseUrlMap[$cluster][$id][$itemID] = $url;
97 foreach ( $batched as $cluster => $batchByCluster ) {
98 $res = $this->batchFetchBlobs( $cluster, $batchByCluster );
100 foreach ( $res as $id => $blob ) {
101 foreach ( $batchByCluster[$id] as $itemID ) {
102 $url = $inverseUrlMap[$cluster][$id][$itemID];
103 if ( $itemID ===
false ) {
106 $ret[$url] = $blob->getItem( $itemID );
118 public function store( $location, $data ) {
120 $dbw->newInsertQueryBuilder()
121 ->insertInto( $this->
getTable( $dbw, $location ) )
122 ->row( [
'blob_text' => $data ] )
123 ->caller( __METHOD__ )->execute();
124 $id = $dbw->insertId();
129 return "DB://$location/$id";
136 if ( parent::isReadOnly( $location ) ) {
140 return ( $this->getLoadBalancer( $location )->getReadOnlyReason() !==
false );
149 private function getLoadBalancer( $cluster ) {
150 return $this->lbFactory->getExternalLB( $cluster );
161 $lb = $this->getLoadBalancer( $cluster );
163 return $lb->getConnectionRef(
166 $this->getDomainId( $lb->getServerInfo( $lb->getWriterIndex() ) ),
167 $lb::CONN_TRX_AUTOCOMMIT
179 $lb = $this->getLoadBalancer( $cluster );
181 return $lb->getMaintenanceConnectionRef(
184 $this->getDomainId( $lb->getServerInfo( $lb->getWriterIndex() ) ),
185 $lb::CONN_TRX_AUTOCOMMIT
193 private function getDomainId( array $server ) {
194 if ( $this->isDbDomainExplicit ) {
198 if ( isset( $server[
'dbname'] ) ) {
206 $server[
'schema'] ??
null,
207 $server[
'tablePrefix'] ??
''
210 return $domain->getId();
224 if ( $cluster !==
null ) {
225 $lb = $this->getLoadBalancer( $cluster );
226 $info = $lb->getServerInfo( $lb->getWriterIndex() );
227 if ( isset( $info[
'blobs table'] ) ) {
228 return $info[
'blobs table'];
232 return $db->getLBInfo(
'blobs table' ) ??
'blobs';
245 static $supportedTypes = [
'mysql',
'sqlite' ];
248 if ( !in_array( $dbw->getType(), $supportedTypes,
true ) ) {
249 throw new DBUnexpectedError( $dbw,
"RDBMS type '{$dbw->getType()}' not supported." );
252 $sqlFilePath =
"$IP/maintenance/storage/blobs.sql";
253 $sql = file_get_contents( $sqlFilePath );
254 if ( $sql ===
false ) {
255 throw new RuntimeException(
"Failed to read '$sqlFilePath'." );
258 $rawTable = $this->
getTable( $dbw, $cluster );
259 $encTable = $dbw->tableName( $rawTable );
261 $sqlWithReplacedVars = str_replace(
262 [
'/*$wgDBprefix*/blobs',
'/*_*/blobs' ],
263 [ $encTable, $encTable ],
269 $sqlWithReplacedVars,
270 $dbw::QUERY_CHANGE_SCHEMA,
288 private function fetchBlob( $cluster, $id, $itemID ) {
295 static $externalBlobCache = [];
297 $cacheID = ( $itemID === false ) ?
"$cluster/$id" :
"$cluster/$id/";
298 $cacheID =
"$cacheID@{$this->dbDomain}";
300 if ( isset( $externalBlobCache[$cacheID] ) ) {
301 $this->logger->debug( __METHOD__ .
": cache hit on $cacheID" );
303 return $externalBlobCache[$cacheID];
306 $this->logger->debug( __METHOD__ .
": cache miss on $cacheID" );
309 $ret = $dbr->newSelectQueryBuilder()
310 ->select(
'blob_text' )
311 ->from( $this->
getTable( $dbr, $cluster ) )
312 ->where( [
'blob_id' => $id ] )
313 ->caller( __METHOD__ )->fetchField();
314 if ( $ret ===
false ) {
316 $this->logger->warning( __METHOD__ .
": primary DB fallback on $cacheID" );
317 $trxProfiler = $this->lbFactory->getTransactionProfiler();
318 $scope = $trxProfiler->silenceForScope( $trxProfiler::EXPECTATION_REPLICAS_ONLY );
320 $ret = $dbw->newSelectQueryBuilder()
321 ->select(
'blob_text' )
322 ->from( $this->
getTable( $dbw, $cluster ) )
323 ->where( [
'blob_id' => $id ] )
324 ->caller( __METHOD__ )->fetchField();
325 ScopedCallback::consume( $scope );
326 if ( $ret ===
false ) {
327 $this->logger->warning( __METHOD__ .
": primary DB failed to find $cacheID" );
330 if ( $itemID !==
false && $ret !==
false ) {
335 $externalBlobCache = [ $cacheID => $ret ];
348 private function batchFetchBlobs( $cluster, array $ids ) {
350 $res = $dbr->newSelectQueryBuilder()
351 ->select( [
'blob_id',
'blob_text' ] )
352 ->from( $this->
getTable( $dbr, $cluster ) )
353 ->where( [
'blob_id' => array_keys( $ids ) ] )
354 ->caller( __METHOD__ )
358 if ( $res !==
false ) {
359 $this->mergeBatchResult( $ret, $ids, $res );
364 __METHOD__ .
": primary fallback on '$cluster' for: " .
365 implode(
',', array_keys( $ids ) )
367 $trxProfiler = $this->lbFactory->getTransactionProfiler();
368 $scope = $trxProfiler->silenceForScope( $trxProfiler::EXPECTATION_REPLICAS_ONLY );
370 $res = $dbw->newSelectQueryBuilder()
371 ->select( [
'blob_id',
'blob_text' ] )
372 ->from( $this->
getTable( $dbr, $cluster ) )
373 ->where( [
'blob_id' => array_keys( $ids ) ] )
374 ->caller( __METHOD__ )
376 ScopedCallback::consume( $scope );
377 if ( $res ===
false ) {
378 $this->logger->error( __METHOD__ .
": primary failed on '$cluster'" );
380 $this->mergeBatchResult( $ret, $ids, $res );
384 $this->logger->error(
385 __METHOD__ .
": primary on '$cluster' failed locating items: " .
386 implode(
',', array_keys( $ids ) )
399 private function mergeBatchResult( array &$ret, array &$ids, $res ) {
400 foreach ( $res as $row ) {
402 $itemIDs = $ids[$id];
404 if ( count( $itemIDs ) === 1 && reset( $itemIDs ) ===
false ) {
406 $ret[$id] = $row->blob_text;
419 $path = explode(
'/', $url );
static unserialize(string $str, bool $allowDouble=false)
Unserialize a HistoryBlob.
Base class for general text storage via the "object" flag in old_flags, or two-part external storage ...