52 private $compressBlobs =
false;
57 private $legacyEncoding =
false;
62 private $useExternalStore =
false;
79 private readonly
bool|
string $dbDomain =
false,
87 return $this->cacheExpiry;
94 $this->cacheExpiry = $cacheExpiry;
101 return $this->compressBlobs;
108 $this->compressBlobs = $compressBlobs;
116 return $this->legacyEncoding;
128 $this->legacyEncoding = $legacyEncoding;
135 return $this->useExternalStore;
142 $this->useExternalStore = $useExternalStore;
148 private function getDBLoadBalancer() {
149 return $this->dbLoadBalancer;
157 private function getDBConnection( $index ) {
158 $lb = $this->getDBLoadBalancer();
159 return $lb->getConnection( $index, [], $this->dbDomain );
175 # Write to external storage if required
176 if ( $this->useExternalStore ) {
179 $data = $this->extStoreAccess->insert( $data, [
'domain' => $this->dbDomain ] );
187 return 'es:' . $data .
'?flags=' . $flags;
189 return 'es:' . $data;
194 $dbw->newInsertQueryBuilder()
195 ->insertInto(
'text' )
196 ->row( [
'old_text' => $data,
'old_flags' => $flags ] )
197 ->caller( __METHOD__ )->execute();
199 $textId = $dbw->insertId();
217 public function getBlob( $blobAddress, $queryFlags = 0 ) {
218 Assert::parameterType(
'string', $blobAddress,
'$blobAddress' );
221 $blob = $this->cache->getWithSetCallback(
222 $this->getCacheKey( $blobAddress ),
223 $this->getCacheTTL(),
224 function ( $unused, &$ttl, &$setOpts ) use ( $blobAddress, $queryFlags, &$error ) {
226 [ $result, $errors ] = $this->fetchBlobs( [ $blobAddress ], $queryFlags );
228 $error = $errors[$blobAddress] ??
null;
230 $ttl = WANObjectCache::TTL_UNCACHEABLE;
232 return $result[$blobAddress];
234 $this->getCacheOptions()
238 if ( $error[0] ===
'badrevision' ) {
245 Assert::postcondition( is_string( $blob ),
'Blob must not be null' );
265 [ $blobsByAddress, $errors ] = $this->fetchBlobs( $blobAddresses, $queryFlags );
267 $blobsByAddress = array_map(
static function ( $blob ) {
268 return $blob ===
false ? null : $blob;
269 }, $blobsByAddress );
271 $result = StatusValue::newGood( $blobsByAddress );
272 foreach ( $errors as $error ) {
274 $result->warning( ...$error );
293 private function fetchBlobs( $blobAddresses, $queryFlags ) {
294 $textIdToBlobAddress = [];
297 foreach ( $blobAddresses as $blobAddress ) {
300 }
catch ( InvalidArgumentException $ex ) {
301 throw new BlobAccessException(
302 $ex->getMessage() .
'. Use findBadBlobs.php to remedy.',
308 if ( $schema ===
'es' ) {
309 if ( $params && isset( $params[
'flags'] ) ) {
310 $blob = $this->
expandBlob( $id, $params[
'flags'] .
',external', $blobAddress );
312 $blob = $this->
expandBlob( $id,
'external', $blobAddress );
315 if ( $blob ===
false ) {
316 $errors[$blobAddress] = [
318 "Bad data in external store address $id. Use findBadBlobs.php to remedy."
321 $result[$blobAddress] = $blob;
322 } elseif ( $schema ===
'bad' ) {
326 .
": loading known-bad content ($blobAddress), returning empty string"
328 $result[$blobAddress] =
'';
329 $errors[$blobAddress] = [
331 'The content of this revision is missing or corrupted (bad schema)'
333 } elseif ( $schema ===
'tt' ) {
334 $textId = intval( $id );
336 if ( $textId < 1 || $id !== (
string)$textId ) {
337 $errors[$blobAddress] = [
339 "Bad blob address: $blobAddress. Use findBadBlobs.php to remedy."
341 $result[$blobAddress] =
false;
344 $textIdToBlobAddress[$textId] = $blobAddress;
346 $errors[$blobAddress] = [
348 "Unknown blob address schema: $schema. Use findBadBlobs.php to remedy."
350 $result[$blobAddress] =
false;
354 $textIds = array_keys( $textIdToBlobAddress );
356 return [ $result, $errors ];
360 $queryFlags |= DBAccessObjectUtils::hasFlags( $queryFlags, IDBAccessObject::READ_LATEST )
361 ? IDBAccessObject::READ_LATEST_IMMUTABLE
363 [ $index, $options, $fallbackIndex, $fallbackOptions ] =
364 self::getDBOptions( $queryFlags );
366 $dbConnection = $this->getDBConnection( $index );
367 $rows = $dbConnection->newSelectQueryBuilder()
368 ->select( [
'old_id',
'old_text',
'old_flags' ] )
370 ->where( [
'old_id' => $textIds ] )
371 ->options( $options )
372 ->caller( __METHOD__ )->fetchResultSet();
373 $numRows = $rows->numRows();
377 if ( $numRows !== count( $textIds ) && $fallbackIndex !==
null ) {
378 $fetchedTextIds = [];
379 foreach ( $rows as $row ) {
380 $fetchedTextIds[] = $row->old_id;
382 $missingTextIds = array_diff( $textIds, $fetchedTextIds );
383 $dbConnection = $this->getDBConnection( $fallbackIndex );
384 $rowsFromFallback = $dbConnection->newSelectQueryBuilder()
385 ->select( [
'old_id',
'old_text',
'old_flags' ] )
387 ->where( [
'old_id' => $missingTextIds ] )
388 ->options( $fallbackOptions )
389 ->caller( __METHOD__ )->fetchResultSet();
390 $appendIterator =
new AppendIterator();
391 $appendIterator->append( $rows );
392 $appendIterator->append( $rowsFromFallback );
393 $rows = $appendIterator;
396 foreach ( $rows as $row ) {
397 $blobAddress = $textIdToBlobAddress[$row->old_id];
399 if ( $row->old_text !==
null ) {
400 $blob = $this->
expandBlob( $row->old_text, $row->old_flags, $blobAddress );
402 if ( $blob ===
false ) {
403 $errors[$blobAddress] = [
405 "Bad data in text row {$row->old_id}. Use findBadBlobs.php to remedy."
408 $result[$blobAddress] = $blob;
412 if ( count( $result ) !== count( $blobAddresses ) ) {
413 foreach ( $blobAddresses as $blobAddress ) {
414 if ( !isset( $result[$blobAddress ] ) ) {
415 $errors[$blobAddress] = [
417 "Unable to fetch blob at $blobAddress. Use findBadBlobs.php to remedy."
419 $result[$blobAddress] =
false;
423 return [ $result, $errors ];
426 private static function getDBOptions(
int $bitfield ): array {
427 if ( DBAccessObjectUtils::hasFlags( $bitfield, IDBAccessObject::READ_LATEST_IMMUTABLE ) ) {
430 } elseif ( DBAccessObjectUtils::hasFlags( $bitfield, IDBAccessObject::READ_LATEST ) ) {
432 $fallbackIndex =
null;
435 $fallbackIndex =
null;
438 $lockingOptions = [];
439 if ( DBAccessObjectUtils::hasFlags( $bitfield, IDBAccessObject::READ_EXCLUSIVE ) ) {
440 $lockingOptions[] =
'FOR UPDATE';
441 } elseif ( DBAccessObjectUtils::hasFlags( $bitfield, IDBAccessObject::READ_LOCKING ) ) {
442 $lockingOptions[] =
'LOCK IN SHARE MODE';
445 if ( $fallbackIndex !==
null ) {
447 $fallbackOptions = $lockingOptions;
449 $options = $lockingOptions;
450 $fallbackOptions = [];
453 return [ $index, $options, $fallbackIndex, $fallbackOptions ];
467 return $this->cache->makeGlobalKey(
469 $this->dbLoadBalancer->resolveDomainID( $this->dbDomain ),
479 private function getCacheOptions() {
481 'pcGroup' => self::TEXT_CACHE_GROUP,
482 'pcTTL' => WANObjectCache::TTL_PROC_LONG,
483 'segmentable' => true
507 public function expandBlob( $raw, $flags, $blobAddress =
null ) {
508 if ( is_string( $flags ) ) {
509 $flags = self::explodeFlags( $flags );
511 if ( in_array(
'error', $flags ) ) {
513 "The content of this revision is missing or corrupted (error flag)"
518 if ( in_array(
'external', $flags ) ) {
520 $parts = explode(
'://',
$url, 2 );
521 if ( count( $parts ) == 1 || $parts[1] ==
'' ) {
525 if ( $blobAddress ) {
527 return $this->cache->getWithSetCallback(
528 $this->getCacheKey( $blobAddress ),
529 $this->getCacheTTL(),
530 function () use (
$url, $flags, $blobAddress ) {
532 $blob = $this->extStoreAccess
533 ->fetchFromURL(
$url, [
'domain' => $this->dbDomain ] );
535 return $blob ===
false ? false : $this->decompressData( $blob, $flags, $blobAddress );
537 $this->getCacheOptions()
540 $blob = $this->extStoreAccess->fetchFromURL(
$url, [
'domain' => $this->dbDomain ] );
541 return $blob ===
false ? false : $this->decompressData( $blob, $flags, $blobAddress );
544 return $this->decompressData( $raw, $flags, $blobAddress );
570 $blobFlags[] =
'utf-8';
572 if ( $this->compressBlobs ) {
573 if ( function_exists(
'gzdeflate' ) ) {
574 $deflated = gzdeflate( $blob );
576 if ( $deflated ===
false ) {
580 $blobFlags[] =
'gzip';
583 wfDebug( __METHOD__ .
" -- no zlib support, not compressing" );
586 return implode(
',', $blobFlags );
605 public function decompressData(
string $blob, array $blobFlags, ?
string $blobAddress =
null ) {
606 if ( in_array(
'error', $blobFlags ) ) {
614 if ( in_array(
'gzip', $blobFlags ) ) {
617 $blob = @gzinflate( $blob );
618 if ( $blob ===
false ) {
619 wfWarn( __METHOD__ .
': gzinflate() failed' .
620 ( $blobAddress ?
' (at blob address ' . $blobAddress .
')' :
'' ) );
625 if ( in_array(
'object', $blobFlags ) ) {
626 # Generic compressed storage
627 $obj = HistoryBlobUtils::unserialize( $blob );
632 $blob = $obj->getText();
636 if ( $blob !==
false && $this->legacyEncoding
637 && !in_array(
'utf-8', $blobFlags ) && !in_array(
'utf8', $blobFlags )
649 $blob = @iconv( $this->legacyEncoding,
'UTF-8//IGNORE', $blob );
662 private function getCacheTTL() {
663 $cache = $this->cache;
665 if ( $cache->getQoS( BagOStuff::ATTR_DURABILITY ) >= BagOStuff::QOS_DURABILITY_RDBMS ) {
667 $ttl = $cache::TTL_UNCACHEABLE;
669 $ttl = $this->cacheExpiry ?: $cache::TTL_UNCACHEABLE;
696 [ $schema, $id, ] = self::splitBlobAddress( $address );
698 if ( $schema !==
'tt' ) {
702 $textId = intval( $id );
704 if ( !$textId || $id !== (
string)$textId ) {
705 throw new InvalidArgumentException(
"Malformed text_id: $id" );
735 return $flagsString ===
'' ? [] : explode(
',', $flagsString );
748 if ( !preg_match(
'/^([-+.\w]+):([^\s?]+)(\?([^\s]*))?$/', $address, $m ) ) {
749 throw new InvalidArgumentException(
"Bad blob address: $address" );
752 $schema = strtolower( $m[1] );
756 return [ $schema, $id, $parameters ];
761 if ( $this->useExternalStore && $this->extStoreAccess->isReadOnly() ) {
765 return ( $this->getDBLoadBalancer()->getReadOnlyReason() !==
false );