35use InvalidArgumentException;
39use Wikimedia\Assert\Assert;
40use Wikimedia\AtEase\AtEase;
206 return $lb->getConnectionRef( $index, [], $this->dbDomain );
223 # Write to external storage if required
224 if ( $this->useExternalStore ) {
226 $data = $this->extStoreAccess->insert( $data, [
'domain' => $this->dbDomain ] );
233 $flags .=
'external';
242 $old_id = $dbw->nextSequenceValue(
'text_old_id_seq' );
248 'old_flags' => $flags,
253 $textId = $dbw->insertId();
273 public function getBlob( $blobAddress, $queryFlags = 0 ) {
274 Assert::parameterType(
'string', $blobAddress,
'$blobAddress' );
277 $blob = $this->cache->getWithSetCallback(
280 function ( $unused, &$ttl, &$setOpts ) use ( $blobAddress, $queryFlags, &$error ) {
282 list( $result, $errors ) = $this->
fetchBlobs( [ $blobAddress ], $queryFlags );
284 $error = $errors[$blobAddress] ??
null;
285 return $result[$blobAddress];
294 Assert::postcondition( is_string(
$blob ),
'Blob must not be null' );
314 list( $blobsByAddress, $errors ) = $this->
fetchBlobs( $blobAddresses, $queryFlags );
316 $blobsByAddress = array_map(
static function (
$blob ) {
318 }, $blobsByAddress );
320 $result = StatusValue::newGood( $blobsByAddress );
322 foreach ( $errors as $error ) {
323 $result->warning(
'internalerror', $error );
340 $textIdToBlobAddress = [];
343 foreach ( $blobAddresses as $blobAddress ) {
346 }
catch ( InvalidArgumentException $ex ) {
348 $ex->getMessage() .
'. Use findBadBlobs.php to remedy.',
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 .
' Use findBadBlobs.php to remedy.';
369 $result[$blobAddress] =
false;
372 $textIdToBlobAddress[$textId] = $blobAddress;
374 $errors[$blobAddress] =
"Unknown blob address schema: $schema."
375 .
' Use findBadBlobs.php to remedy.';
376 $result[$blobAddress] =
false;
380 $textIds = array_keys( $textIdToBlobAddress );
382 return [ $result, $errors ];
386 $queryFlags |= DBAccessObjectUtils::hasFlags( $queryFlags, self::READ_LATEST )
387 ? self::READ_LATEST_IMMUTABLE
389 list( $index, $options, $fallbackIndex, $fallbackOptions ) =
390 DBAccessObjectUtils::getDBOptions( $queryFlags );
393 $rows = $dbConnection->select(
395 [
'old_id',
'old_text',
'old_flags' ],
396 [
'old_id' => $textIds ],
402 $numRows = $rows->numRows();
407 if ( $numRows !== count( $textIds ) && $fallbackIndex !==
null ) {
408 $fetchedTextIds = [];
409 foreach ( $rows as $row ) {
410 $fetchedTextIds[] = $row->old_id;
412 $missingTextIds = array_diff( $textIds, $fetchedTextIds );
414 $rowsFromFallback = $dbConnection->select(
416 [
'old_id',
'old_text',
'old_flags' ],
417 [
'old_id' => $missingTextIds ],
421 $appendIterator =
new AppendIterator();
422 $appendIterator->append( $rows );
423 $appendIterator->append( $rowsFromFallback );
424 $rows = $appendIterator;
427 foreach ( $rows as $row ) {
428 $blobAddress = $textIdToBlobAddress[$row->old_id];
430 if ( $row->old_text !==
null ) {
431 $blob = $this->
expandBlob( $row->old_text, $row->old_flags, $blobAddress );
433 if (
$blob ===
false ) {
434 $errors[$blobAddress] =
"Bad data in text row {$row->old_id}."
435 .
' Use findBadBlobs.php to remedy.';
437 $result[$blobAddress] =
$blob;
441 if ( count( $result ) !== count( $blobAddresses ) ) {
442 foreach ( $blobAddresses as $blobAddress ) {
443 if ( !isset( $result[$blobAddress ] ) ) {
444 $errors[$blobAddress] =
"Unable to fetch blob at $blobAddress."
445 .
' Use findBadBlobs.php to remedy.';
446 $result[$blobAddress] =
false;
450 return [ $result, $errors ];
464 return $this->cache->makeGlobalKey(
466 $this->dbLoadBalancer->resolveDomainID( $this->dbDomain ),
490 public function expandBlob( $raw, $flags, $cacheKey =
null ) {
491 if ( is_string( $flags ) ) {
492 $flags = explode(
',', $flags );
496 if ( in_array(
'external', $flags ) ) {
498 $parts = explode(
'://', $url, 2 );
499 if ( count( $parts ) == 1 || $parts[1] ==
'' ) {
505 return $this->cache->getWithSetCallback(
508 function () use ( $url, $flags ) {
510 $blob = $this->extStoreAccess
511 ->fetchFromURL( $url, [
'domain' => $this->dbDomain ] );
518 $blob = $this->extStoreAccess->fetchFromURL( $url, [
'domain' => $this->dbDomain ] );
548 $blobFlags[] =
'utf-8';
550 if ( $this->compressBlobs ) {
551 if ( function_exists(
'gzdeflate' ) ) {
552 $deflated = gzdeflate(
$blob );
554 if ( $deflated ===
false ) {
558 $blobFlags[] =
'gzip';
561 wfDebug( __METHOD__ .
" -- no zlib support, not compressing" );
564 return implode(
',', $blobFlags );
583 if ( in_array(
'error', $blobFlags ) ) {
588 if ( in_array(
'gzip', $blobFlags ) ) {
589 # Deal with optional compression of archived pages.
590 # This can be done periodically via maintenance/compressOld.php, and
591 # as pages are saved if $wgCompressRevisions is set.
594 if (
$blob ===
false ) {
595 wfWarn( __METHOD__ .
': gzinflate() failed' );
600 if ( in_array(
'object', $blobFlags ) ) {
601 # Generic compressed storage
603 if ( !is_object( $obj ) ) {
607 $blob = $obj->getText();
611 if (
$blob !==
false && $this->legacyEncoding
612 && !in_array(
'utf-8', $blobFlags ) && !in_array(
'utf8', $blobFlags )
614 # Old revisions kept around in a legacy encoding?
615 # Upconvert on demand.
616 # ("utf8" checked for compatibility with some broken
617 # conversion scripts 2008-12-30)
619 # *input* string. We just ignore those too.
622 AtEase::suppressWarnings();
623 $blob = iconv( $this->legacyEncoding,
'UTF-8//IGNORE',
$blob );
624 AtEase::restoreWarnings();
640 if (
$cache->
getQoS( $cache::ATTR_DURABILITY ) >= $cache::QOS_DURABILITY_RDBMS ) {
642 $ttl = $cache::TTL_UNCACHEABLE;
644 $ttl = $this->cacheExpiry ?: $cache::TTL_UNCACHEABLE;
673 if ( $schema !==
'tt' ) {
677 $textId = intval( $id );
679 if ( !$textId || $id !== (
string)$textId ) {
680 throw new InvalidArgumentException(
"Malformed text_id: $id" );
714 if ( !preg_match(
'/^([-+.\w]+):([^\s?]+)(\?([^\s]*))?$/', $address, $m ) ) {
715 throw new InvalidArgumentException(
"Bad blob address: $address" );
718 $schema = strtolower( $m[1] );
722 return [ $schema, $id, $parameters ];
726 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 a deprecated feature was used.
Helper class for DAO classes.
This is the main interface for fetching or inserting objects with ExternalStore.
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.