| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
namespace MediaWiki\Storage; |
| 27 |
|
| 28 |
use AppendIterator; |
| 29 |
use ExternalStoreAccess; |
| 30 |
use ExternalStoreException; |
| 31 |
use HistoryBlobUtils; |
| 32 |
use InvalidArgumentException; |
| 33 |
use StatusValue; |
| 34 |
use Wikimedia\Assert\Assert; |
| 35 |
use Wikimedia\AtEase\AtEase; |
| 36 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 37 |
use Wikimedia\Rdbms\DBAccessObjectUtils; |
| 38 |
use Wikimedia\Rdbms\IDatabase; |
| 39 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 40 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
class SqlBlobStore implements BlobStore { |
| 51 |
|
| 52 |
|
| 53 |
public const TEXT_CACHE_GROUP = 'revisiontext:10'; |
| 54 |
|
| 55 |
|
| 56 |
public const DEFAULT_TTL = 7 * 24 * 3600; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private $dbLoadBalancer; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private $extStoreAccess; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private $cache; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private $dbDomain; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
private $cacheExpiry = self::DEFAULT_TTL; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private $compressBlobs = false; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private $legacyEncoding = false; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private $useExternalStore = false; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function __construct( |
| 110 |
ILoadBalancer $dbLoadBalancer, |
| 111 |
ExternalStoreAccess $extStoreAccess, |
| 112 |
WANObjectCache $cache, |
| 113 |
$dbDomain = false |
| 114 |
) { |
| 115 |
$this->dbLoadBalancer = $dbLoadBalancer; |
| 116 |
$this->extStoreAccess = $extStoreAccess; |
| 117 |
$this->cache = $cache; |
| 118 |
$this->dbDomain = $dbDomain; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function getCacheExpiry() { |
| 125 |
return $this->cacheExpiry; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function setCacheExpiry( int $cacheExpiry ) { |
| 132 |
$this->cacheExpiry = $cacheExpiry; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function getCompressBlobs() { |
| 139 |
return $this->compressBlobs; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function setCompressBlobs( $compressBlobs ) { |
| 146 |
$this->compressBlobs = $compressBlobs; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function getLegacyEncoding() { |
| 154 |
return $this->legacyEncoding; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function setLegacyEncoding( string $legacyEncoding ) { |
| 166 |
$this->legacyEncoding = $legacyEncoding; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public function getUseExternalStore() { |
| 173 |
return $this->useExternalStore; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function setUseExternalStore( bool $useExternalStore ) { |
| 180 |
$this->useExternalStore = $useExternalStore; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
private function getDBLoadBalancer() { |
| 187 |
return $this->dbLoadBalancer; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private function getDBConnection( $index ) { |
| 196 |
$lb = $this->getDBLoadBalancer(); |
| 197 |
return $lb->getConnection( $index, [], $this->dbDomain ); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function storeBlob( $data, $hints = [] ) { |
| 211 |
$flags = $this->compressData( $data ); |
| 212 |
|
| 213 |
|
| 214 |
if ( $this->useExternalStore ) { |
| 215 |
|
| 216 |
try { |
| 217 |
$data = $this->extStoreAccess->insert( $data, [ 'domain' => $this->dbDomain ] ); |
| 218 |
} catch ( ExternalStoreException $e ) { |
| 219 |
throw new BlobAccessException( $e->getMessage(), 0, $e ); |
| 220 |
} |
| 221 |
if ( !$data ) { |
| 222 |
throw new BlobAccessException( "Failed to store text to external storage" ); |
| 223 |
} |
| 224 |
if ( $flags ) { |
| 225 |
return 'es:' . $data . '?flags=' . $flags; |
| 226 |
} else { |
| 227 |
return 'es:' . $data; |
| 228 |
} |
| 229 |
} else { |
| 230 |
$dbw = $this->getDBConnection( DB_PRIMARY ); |
| 231 |
|
| 232 |
$dbw->newInsertQueryBuilder() |
| 233 |
->insertInto( 'text' ) |
| 234 |
->row( [ 'old_text' => $data, 'old_flags' => $flags ] ) |
| 235 |
->caller( __METHOD__ )->execute(); |
| 236 |
|
| 237 |
$textId = $dbw->insertId(); |
| 238 |
|
| 239 |
return self::makeAddressFromTextId( $textId ); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public function getBlob( $blobAddress, $queryFlags = 0 ) { |
| 256 |
Assert::parameterType( 'string', $blobAddress, '$blobAddress' ); |
| 257 |
|
| 258 |
$error = null; |
| 259 |
$blob = $this->cache->getWithSetCallback( |
| 260 |
$this->getCacheKey( $blobAddress ), |
| 261 |
$this->getCacheTTL(), |
| 262 |
function ( $unused, &$ttl, &$setOpts ) use ( $blobAddress, $queryFlags, &$error ) { |
| 263 |
|
| 264 |
[ $result, $errors ] = $this->fetchBlobs( [ $blobAddress ], $queryFlags ); |
| 265 |
|
| 266 |
$error = $errors[$blobAddress] ?? null; |
| 267 |
if ( $error ) { |
| 268 |
$ttl = WANObjectCache::TTL_UNCACHEABLE; |
| 269 |
} |
| 270 |
return $result[$blobAddress]; |
| 271 |
}, |
| 272 |
$this->getCacheOptions() |
| 273 |
); |
| 274 |
|
| 275 |
if ( $error ) { |
| 276 |
if ( $error[0] === 'badrevision' ) { |
| 277 |
throw new BadBlobException( $error[1] ); |
| 278 |
} else { |
| 279 |
throw new BlobAccessException( $error[1] ); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
Assert::postcondition( is_string( $blob ), 'Blob must not be null' ); |
| 284 |
return $blob; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public function getBlobBatch( $blobAddresses, $queryFlags = 0 ) { |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
[ $blobsByAddress, $errors ] = $this->fetchBlobs( $blobAddresses, $queryFlags ); |
| 304 |
|
| 305 |
$blobsByAddress = array_map( static function ( $blob ) { |
| 306 |
return $blob === false ? null : $blob; |
| 307 |
}, $blobsByAddress ); |
| 308 |
|
| 309 |
$result = StatusValue::newGood( $blobsByAddress ); |
| 310 |
foreach ( $errors as $error ) { |
| 311 |
|
| 312 |
$result->warning( ...$error ); |
| 313 |
} |
| 314 |
return $result; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
private function fetchBlobs( $blobAddresses, $queryFlags ) { |
| 332 |
$textIdToBlobAddress = []; |
| 333 |
$result = []; |
| 334 |
$errors = []; |
| 335 |
foreach ( $blobAddresses as $blobAddress ) { |
| 336 |
try { |
| 337 |
[ $schema, $id, $params ] = self::splitBlobAddress( $blobAddress ); |
| 338 |
} catch ( InvalidArgumentException $ex ) { |
| 339 |
throw new BlobAccessException( |
| 340 |
$ex->getMessage() . '. Use findBadBlobs.php to remedy.', |
| 341 |
0, |
| 342 |
$ex |
| 343 |
); |
| 344 |
} |
| 345 |
|
| 346 |
if ( $schema === 'es' ) { |
| 347 |
if ( $params && isset( $params['flags'] ) ) { |
| 348 |
$blob = $this->expandBlob( $id, $params['flags'] . ',external', $blobAddress ); |
| 349 |
} else { |
| 350 |
$blob = $this->expandBlob( $id, 'external', $blobAddress ); |
| 351 |
} |
| 352 |
|
| 353 |
if ( $blob === false ) { |
| 354 |
$errors[$blobAddress] = [ |
| 355 |
'internalerror', |
| 356 |
"Bad data in external store address $id. Use findBadBlobs.php to remedy." |
| 357 |
]; |
| 358 |
} |
| 359 |
$result[$blobAddress] = $blob; |
| 360 |
} elseif ( $schema === 'bad' ) { |
| 361 |
|
| 362 |
wfDebug( |
| 363 |
__METHOD__ |
| 364 |
. ": loading known-bad content ($blobAddress), returning empty string" |
| 365 |
); |
| 366 |
$result[$blobAddress] = ''; |
| 367 |
$errors[$blobAddress] = [ |
| 368 |
'badrevision', |
| 369 |
'The content of this revision is missing or corrupted (bad schema)' |
| 370 |
]; |
| 371 |
} elseif ( $schema === 'tt' ) { |
| 372 |
$textId = intval( $id ); |
| 373 |
|
| 374 |
if ( $textId < 1 || $id !== (string)$textId ) { |
| 375 |
$errors[$blobAddress] = [ |
| 376 |
'internalerror', |
| 377 |
"Bad blob address: $blobAddress. Use findBadBlobs.php to remedy." |
| 378 |
]; |
| 379 |
$result[$blobAddress] = false; |
| 380 |
} |
| 381 |
|
| 382 |
$textIdToBlobAddress[$textId] = $blobAddress; |
| 383 |
} else { |
| 384 |
$errors[$blobAddress] = [ |
| 385 |
'internalerror', |
| 386 |
"Unknown blob address schema: $schema. Use findBadBlobs.php to remedy." |
| 387 |
]; |
| 388 |
$result[$blobAddress] = false; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
$textIds = array_keys( $textIdToBlobAddress ); |
| 393 |
if ( !$textIds ) { |
| 394 |
return [ $result, $errors ]; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
$queryFlags |= DBAccessObjectUtils::hasFlags( $queryFlags, IDBAccessObject::READ_LATEST ) |
| 399 |
? IDBAccessObject::READ_LATEST_IMMUTABLE |
| 400 |
: 0; |
| 401 |
[ $index, $options, $fallbackIndex, $fallbackOptions ] = |
| 402 |
self::getDBOptions( $queryFlags ); |
| 403 |
|
| 404 |
$dbConnection = $this->getDBConnection( $index ); |
| 405 |
$rows = $dbConnection->newSelectQueryBuilder() |
| 406 |
->select( [ 'old_id', 'old_text', 'old_flags' ] ) |
| 407 |
->from( 'text' ) |
| 408 |
->where( [ 'old_id' => $textIds ] ) |
| 409 |
->options( $options ) |
| 410 |
->caller( __METHOD__ )->fetchResultSet(); |
| 411 |
$numRows = $rows->numRows(); |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
if ( $numRows !== count( $textIds ) && $fallbackIndex !== null ) { |
| 416 |
$fetchedTextIds = []; |
| 417 |
foreach ( $rows as $row ) { |
| 418 |
$fetchedTextIds[] = $row->old_id; |
| 419 |
} |
| 420 |
$missingTextIds = array_diff( $textIds, $fetchedTextIds ); |
| 421 |
$dbConnection = $this->getDBConnection( $fallbackIndex ); |
| 422 |
$rowsFromFallback = $dbConnection->newSelectQueryBuilder() |
| 423 |
->select( [ 'old_id', 'old_text', 'old_flags' ] ) |
| 424 |
->from( 'text' ) |
| 425 |
->where( [ 'old_id' => $missingTextIds ] ) |
| 426 |
->options( $fallbackOptions ) |
| 427 |
->caller( __METHOD__ )->fetchResultSet(); |
| 428 |
$appendIterator = new AppendIterator(); |
| 429 |
$appendIterator->append( $rows ); |
| 430 |
$appendIterator->append( $rowsFromFallback ); |
| 431 |
$rows = $appendIterator; |
| 432 |
} |
| 433 |
|
| 434 |
foreach ( $rows as $row ) { |
| 435 |
$blobAddress = $textIdToBlobAddress[$row->old_id]; |
| 436 |
$blob = false; |
| 437 |
if ( $row->old_text !== null ) { |
| 438 |
$blob = $this->expandBlob( $row->old_text, $row->old_flags, $blobAddress ); |
| 439 |
} |
| 440 |
if ( $blob === false ) { |
| 441 |
$errors[$blobAddress] = [ |
| 442 |
'internalerror', |
| 443 |
"Bad data in text row {$row->old_id}. Use findBadBlobs.php to remedy." |
| 444 |
]; |
| 445 |
} |
| 446 |
$result[$blobAddress] = $blob; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
if ( count( $result ) !== count( $blobAddresses ) ) { |
| 451 |
foreach ( $blobAddresses as $blobAddress ) { |
| 452 |
if ( !isset( $result[$blobAddress ] ) ) { |
| 453 |
$errors[$blobAddress] = [ |
| 454 |
'internalerror', |
| 455 |
"Unable to fetch blob at $blobAddress. Use findBadBlobs.php to remedy." |
| 456 |
]; |
| 457 |
$result[$blobAddress] = false; |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
return [ $result, $errors ]; |
| 462 |
} |
| 463 |
|
| 464 |
private static function getDBOptions( $bitfield ) { |
| 465 |
if ( DBAccessObjectUtils::hasFlags( $bitfield, IDBAccessObject::READ_LATEST_IMMUTABLE ) ) { |
| 466 |
$index = DB_REPLICA; |
| 467 |
$fallbackIndex = DB_PRIMARY; |
| 468 |
} elseif ( DBAccessObjectUtils::hasFlags( $bitfield, IDBAccessObject::READ_LATEST ) ) { |
| 469 |
$index = DB_PRIMARY; |
| 470 |
$fallbackIndex = null; |
| 471 |
} else { |
| 472 |
$index = DB_REPLICA; |
| 473 |
$fallbackIndex = null; |
| 474 |
} |
| 475 |
|
| 476 |
$lockingOptions = []; |
| 477 |
if ( DBAccessObjectUtils::hasFlags( $bitfield, IDBAccessObject::READ_EXCLUSIVE ) ) { |
| 478 |
$lockingOptions[] = 'FOR UPDATE'; |
| 479 |
} elseif ( DBAccessObjectUtils::hasFlags( $bitfield, IDBAccessObject::READ_LOCKING ) ) { |
| 480 |
$lockingOptions[] = 'LOCK IN SHARE MODE'; |
| 481 |
} |
| 482 |
|
| 483 |
if ( $fallbackIndex !== null ) { |
| 484 |
$options = []; |
| 485 |
$fallbackOptions = $lockingOptions; |
| 486 |
} else { |
| 487 |
$options = $lockingOptions; |
| 488 |
$fallbackOptions = []; |
| 489 |
} |
| 490 |
|
| 491 |
return [ $index, $options, $fallbackIndex, $fallbackOptions ]; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
private function getCacheKey( $blobAddress ) { |
| 505 |
return $this->cache->makeGlobalKey( |
| 506 |
'SqlBlobStore-blob', |
| 507 |
$this->dbLoadBalancer->resolveDomainID( $this->dbDomain ), |
| 508 |
$blobAddress |
| 509 |
); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
private function getCacheOptions() { |
| 518 |
return [ |
| 519 |
'pcGroup' => self::TEXT_CACHE_GROUP, |
| 520 |
'pcTTL' => WANObjectCache::TTL_PROC_LONG, |
| 521 |
'segmentable' => true |
| 522 |
]; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
public function expandBlob( $raw, $flags, $blobAddress = null ) { |
| 546 |
if ( is_string( $flags ) ) { |
| 547 |
$flags = self::explodeFlags( $flags ); |
| 548 |
} |
| 549 |
if ( in_array( 'error', $flags ) ) { |
| 550 |
throw new BadBlobException( |
| 551 |
"The content of this revision is missing or corrupted (error flag)" |
| 552 |
); |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
if ( in_array( 'external', $flags ) ) { |
| 557 |
$url = $raw; |
| 558 |
$parts = explode( '://', $url, 2 ); |
| 559 |
if ( count( $parts ) == 1 || $parts[1] == '' ) { |
| 560 |
return false; |
| 561 |
} |
| 562 |
|
| 563 |
if ( $blobAddress ) { |
| 564 |
|
| 565 |
return $this->cache->getWithSetCallback( |
| 566 |
$this->getCacheKey( $blobAddress ), |
| 567 |
$this->getCacheTTL(), |
| 568 |
function () use ( $url, $flags ) { |
| 569 |
|
| 570 |
$blob = $this->extStoreAccess |
| 571 |
->fetchFromURL( $url, [ 'domain' => $this->dbDomain ] ); |
| 572 |
|
| 573 |
return $blob === false ? false : $this->decompressData( $blob, $flags ); |
| 574 |
}, |
| 575 |
$this->getCacheOptions() |
| 576 |
); |
| 577 |
} else { |
| 578 |
$blob = $this->extStoreAccess->fetchFromURL( $url, [ 'domain' => $this->dbDomain ] ); |
| 579 |
return $blob === false ? false : $this->decompressData( $blob, $flags ); |
| 580 |
} |
| 581 |
} else { |
| 582 |
return $this->decompressData( $raw, $flags ); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
public function compressData( &$blob ) { |
| 603 |
$blobFlags = []; |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
$blobFlags[] = 'utf-8'; |
| 609 |
|
| 610 |
if ( $this->compressBlobs ) { |
| 611 |
if ( function_exists( 'gzdeflate' ) ) { |
| 612 |
$deflated = gzdeflate( $blob ); |
| 613 |
|
| 614 |
if ( $deflated === false ) { |
| 615 |
wfLogWarning( __METHOD__ . ': gzdeflate() failed' ); |
| 616 |
} else { |
| 617 |
$blob = $deflated; |
| 618 |
$blobFlags[] = 'gzip'; |
| 619 |
} |
| 620 |
} else { |
| 621 |
wfDebug( __METHOD__ . " -- no zlib support, not compressing" ); |
| 622 |
} |
| 623 |
} |
| 624 |
return implode( ',', $blobFlags ); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
public function decompressData( string $blob, array $blobFlags ) { |
| 643 |
if ( in_array( 'error', $blobFlags ) ) { |
| 644 |
|
| 645 |
return false; |
| 646 |
} |
| 647 |
|
| 648 |
if ( in_array( 'gzip', $blobFlags ) ) { |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
$blob = gzinflate( $blob ); |
| 653 |
|
| 654 |
if ( $blob === false ) { |
| 655 |
wfWarn( __METHOD__ . ': gzinflate() failed' ); |
| 656 |
return false; |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
if ( in_array( 'object', $blobFlags ) ) { |
| 661 |
|
| 662 |
$obj = HistoryBlobUtils::unserialize( $blob ); |
| 663 |
if ( !$obj ) { |
| 664 |
|
| 665 |
return false; |
| 666 |
} |
| 667 |
$blob = $obj->getText(); |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
if ( $blob !== false && $this->legacyEncoding |
| 672 |
&& !in_array( 'utf-8', $blobFlags ) && !in_array( 'utf8', $blobFlags ) |
| 673 |
) { |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
AtEase::suppressWarnings(); |
| 683 |
$blob = iconv( $this->legacyEncoding, 'UTF-8//IGNORE', $blob ); |
| 684 |
AtEase::restoreWarnings(); |
| 685 |
} |
| 686 |
|
| 687 |
return $blob; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
private function getCacheTTL() { |
| 698 |
$cache = $this->cache; |
| 699 |
|
| 700 |
if ( $cache->getQoS( $cache::ATTR_DURABILITY ) >= $cache::QOS_DURABILITY_RDBMS ) { |
| 701 |
|
| 702 |
$ttl = $cache::TTL_UNCACHEABLE; |
| 703 |
} else { |
| 704 |
$ttl = $this->cacheExpiry ?: $cache::TTL_UNCACHEABLE; |
| 705 |
} |
| 706 |
|
| 707 |
return $ttl; |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
public function getTextIdFromAddress( $address ) { |
| 731 |
[ $schema, $id, ] = self::splitBlobAddress( $address ); |
| 732 |
|
| 733 |
if ( $schema !== 'tt' ) { |
| 734 |
return null; |
| 735 |
} |
| 736 |
|
| 737 |
$textId = intval( $id ); |
| 738 |
|
| 739 |
if ( !$textId || $id !== (string)$textId ) { |
| 740 |
throw new InvalidArgumentException( "Malformed text_id: $id" ); |
| 741 |
} |
| 742 |
|
| 743 |
return $textId; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
public static function makeAddressFromTextId( $id ) { |
| 760 |
return 'tt:' . $id; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
public static function explodeFlags( string $flagsString ) { |
| 770 |
return $flagsString === '' ? [] : explode( ',', $flagsString ); |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
public static function splitBlobAddress( $address ) { |
| 783 |
if ( !preg_match( '/^([-+.\w]+):([^\s?]+)(\?([^\s]*))?$/', $address, $m ) ) { |
| 784 |
throw new InvalidArgumentException( "Bad blob address: $address" ); |
| 785 |
} |
| 786 |
|
| 787 |
$schema = strtolower( $m[1] ); |
| 788 |
$id = $m[2]; |
| 789 |
$parameters = wfCgiToArray( $m[4] ?? '' ); |
| 790 |
|
| 791 |
return [ $schema, $id, $parameters ]; |
| 792 |
} |
| 793 |
|
| 794 |
public function isReadOnly() { |
| 795 |
if ( $this->useExternalStore && $this->extStoreAccess->isReadOnly() ) { |
| 796 |
return true; |
| 797 |
} |
| 798 |
|
| 799 |
return ( $this->getDBLoadBalancer()->getReadOnlyReason() !== false ); |
| 800 |
} |
| 801 |
} |