34use InvalidArgumentException;
38use Wikimedia\Assert\Assert;
39use Wikimedia\AtEase\AtEase;
130 Assert::parameterType(
'integer',
$cacheExpiry,
'$cacheExpiry' );
210 return $lb->getConnectionRef( $index, [], $this->dbDomain );
227 # Write to external storage if required
228 if ( $this->useExternalStore ) {
230 $data = $this->extStoreAccess->insert( $data, [
'domain' => $this->dbDomain ] );
237 $flags .=
'external';
246 $old_id = $dbw->nextSequenceValue(
'text_old_id_seq' );
252 'old_flags' => $flags,
257 $textId = $dbw->insertId();
277 public function getBlob( $blobAddress, $queryFlags = 0 ) {
278 Assert::parameterType(
'string', $blobAddress,
'$blobAddress' );
281 $blob = $this->cache->getWithSetCallback(
284 function ( $unused, &$ttl, &$setOpts ) use ( $blobAddress, $queryFlags, &$error ) {
286 list( $result, $errors ) = $this->
fetchBlobs( [ $blobAddress ], $queryFlags );
288 $error = $errors[$blobAddress] ??
null;
289 return $result[$blobAddress];
298 Assert::postcondition( is_string(
$blob ),
'Blob must not be null' );
318 list( $blobsByAddress, $errors ) = $this->
fetchBlobs( $blobAddresses, $queryFlags );
320 $blobsByAddress = array_map(
function (
$blob ) {
322 }, $blobsByAddress );
324 $result = StatusValue::newGood( $blobsByAddress );
326 foreach ( $errors as $error ) {
327 $result->warning(
'internalerror', $error );
344 $textIdToBlobAddress = [];
347 foreach ( $blobAddresses as $blobAddress ) {
350 }
catch ( InvalidArgumentException $ex ) {
355 if ( $schema ===
'bad' ) {
359 .
": loading known-bad content ($blobAddress), returning empty string"
361 $result[$blobAddress] =
'';
363 } elseif ( $schema ===
'tt' ) {
364 $textId = intval( $id );
366 if ( $textId < 1 || $id !== (
string)$textId ) {
367 $errors[$blobAddress] =
"Bad blob address: $blobAddress";
368 $result[$blobAddress] =
false;
371 $textIdToBlobAddress[$textId] = $blobAddress;
373 $errors[$blobAddress] =
"Unknown blob address schema: $schema";
374 $result[$blobAddress] =
false;
379 $textIds = array_keys( $textIdToBlobAddress );
381 return [ $result, $errors ];
385 $queryFlags |= DBAccessObjectUtils::hasFlags( $queryFlags, self::READ_LATEST )
386 ? self::READ_LATEST_IMMUTABLE
388 list( $index, $options, $fallbackIndex, $fallbackOptions ) =
389 DBAccessObjectUtils::getDBOptions( $queryFlags );
392 $rows = $dbConnection->select(
394 [
'old_id',
'old_text',
'old_flags' ],
395 [
'old_id' => $textIds ],
402 if ( $dbConnection->numRows( $rows ) !== count( $textIds ) && $fallbackIndex !==
null ) {
403 $fetchedTextIds = [];
404 foreach ( $rows as $row ) {
405 $fetchedTextIds[] = $row->old_id;
407 $missingTextIds = array_diff( $textIds, $fetchedTextIds );
409 $rowsFromFallback = $dbConnection->select(
411 [
'old_id',
'old_text',
'old_flags' ],
412 [
'old_id' => $missingTextIds ],
416 $appendIterator =
new AppendIterator();
417 $appendIterator->append( $rows );
418 $appendIterator->append( $rowsFromFallback );
419 $rows = $appendIterator;
422 foreach ( $rows as $row ) {
423 $blobAddress = $textIdToBlobAddress[$row->old_id];
425 if ( $row->old_text !==
null ) {
426 $blob = $this->
expandBlob( $row->old_text, $row->old_flags, $blobAddress );
428 if (
$blob ===
false ) {
429 $errors[$blobAddress] =
"Bad data in text row {$row->old_id}.";
431 $result[$blobAddress] =
$blob;
435 if ( count( $result ) !== count( $blobAddresses ) ) {
436 foreach ( $blobAddresses as $blobAddress ) {
437 if ( !isset( $result[$blobAddress ] ) ) {
438 $errors[$blobAddress] =
"Unable to fetch blob at $blobAddress";
439 $result[$blobAddress] =
false;
443 return [ $result, $errors ];
457 return $this->cache->makeGlobalKey(
459 $this->dbLoadBalancer->resolveDomainID( $this->dbDomain ),
483 public function expandBlob( $raw, $flags, $cacheKey =
null ) {
484 if ( is_string( $flags ) ) {
485 $flags = explode(
',', $flags );
489 if ( in_array(
'external', $flags ) ) {
491 $parts = explode(
'://', $url, 2 );
492 if ( count( $parts ) == 1 || $parts[1] ==
'' ) {
498 return $this->cache->getWithSetCallback(
501 function () use ( $url, $flags ) {
503 $blob = $this->extStoreAccess
504 ->fetchFromURL( $url, [
'domain' => $this->dbDomain ] );
511 $blob = $this->extStoreAccess->fetchFromURL( $url, [
'domain' => $this->dbDomain ] );
541 $blobFlags[] =
'utf-8';
543 if ( $this->compressBlobs ) {
544 if ( function_exists(
'gzdeflate' ) ) {
545 $deflated = gzdeflate(
$blob );
547 if ( $deflated ===
false ) {
551 $blobFlags[] =
'gzip';
554 wfDebug( __METHOD__ .
" -- no zlib support, not compressing" );
557 return implode(
',', $blobFlags );
577 Assert::parameterType(
'string',
$blob,
'$blob' );
579 if ( in_array(
'error', $blobFlags ) ) {
584 if ( in_array(
'gzip', $blobFlags ) ) {
585 # Deal with optional compression of archived pages.
586 # This can be done periodically via maintenance/compressOld.php, and
587 # as pages are saved if $wgCompressRevisions is set.
590 if (
$blob ===
false ) {
591 wfWarn( __METHOD__ .
': gzinflate() failed' );
596 if ( in_array(
'object', $blobFlags ) ) {
597 # Generic compressed storage
599 if ( !is_object( $obj ) ) {
603 $blob = $obj->getText();
607 if (
$blob !==
false && $this->legacyEncoding
608 && !in_array(
'utf-8', $blobFlags ) && !in_array(
'utf8', $blobFlags )
610 # Old revisions kept around in a legacy encoding?
611 # Upconvert on demand.
612 # ("utf8" checked for compatibility with some broken
613 # conversion scripts 2008-12-30)
615 # *input* string. We just ignore those too.
618 AtEase::suppressWarnings();
619 $blob = iconv( $this->legacyEncoding,
'UTF-8//IGNORE',
$blob );
620 AtEase::restoreWarnings();
634 if ( $this->cache->getQoS( WANObjectCache::ATTR_EMULATION )
635 <= WANObjectCache::QOS_EMULATION_SQL
638 $ttl = WANObjectCache::TTL_UNCACHEABLE;
640 $ttl = $this->cacheExpiry ?: WANObjectCache::TTL_UNCACHEABLE;
669 if ( $schema !==
'tt' ) {
673 $textId = intval( $id );
675 if ( !$textId || $id !== (
string)$textId ) {
676 throw new InvalidArgumentException(
"Malformed text_id: $id" );
710 if ( !preg_match(
'/^([-+.\w]+):([^\s?]+)(\?([^\s]*))?$/', $address, $m ) ) {
711 throw new InvalidArgumentException(
"Bad blob address: $address" );
714 $schema = strtolower( $m[1] );
716 $parameters = isset( $m[4] ) ?
wfCgiToArray( $m[4] ) : [];
718 return [ $schema, $id, $parameters ];
722 if ( $this->useExternalStore && $this->extStoreAccess->isReadOnly() ) {
unserialize( $serialized)
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
wfLogWarning( $msg, $callerOffset=1, $level=E_USER_WARNING)
Send a warning as a PHP error and the debug log.
wfCgiToArray( $query)
This is the logical opposite of wfArrayToCgi(): it accepts a query string as its argument and returns...
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Helper class for DAO classes.
Key/value blob storage for a collection of storage medium types (e.g.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Multi-datacenter aware caching interface.
Interface for database access objects.
Generic interface providing TTL constants for lightweight expiring object stores.