| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Config\ServiceOptions; |
| 22 |
use MediaWiki\Content\Content; |
| 23 |
use MediaWiki\Context\RequestContext; |
| 24 |
use MediaWiki\Deferred\DeferredUpdates; |
| 25 |
use MediaWiki\Deferred\MessageCacheUpdate; |
| 26 |
use MediaWiki\HookContainer\HookContainer; |
| 27 |
use MediaWiki\HookContainer\HookRunner; |
| 28 |
use MediaWiki\Language\ILanguageConverter; |
| 29 |
use MediaWiki\Language\Language; |
| 30 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 31 |
use MediaWiki\Languages\LanguageFactory; |
| 32 |
use MediaWiki\Languages\LanguageFallback; |
| 33 |
use MediaWiki\Languages\LanguageNameUtils; |
| 34 |
use MediaWiki\Linker\LinkTarget; |
| 35 |
use MediaWiki\Logger\LoggerFactory; |
| 36 |
use MediaWiki\MainConfigNames; |
| 37 |
use MediaWiki\MediaWikiServices; |
| 38 |
use MediaWiki\Page\PageReference; |
| 39 |
use MediaWiki\Page\PageReferenceValue; |
| 40 |
use MediaWiki\Parser\Parser; |
| 41 |
use MediaWiki\Parser\ParserFactory; |
| 42 |
use MediaWiki\Parser\ParserOptions; |
| 43 |
use MediaWiki\Parser\ParserOutput; |
| 44 |
use MediaWiki\Revision\SlotRecord; |
| 45 |
use MediaWiki\StubObject\StubObject; |
| 46 |
use MediaWiki\StubObject\StubUserLang; |
| 47 |
use MediaWiki\Title\Title; |
| 48 |
use Psr\Log\LoggerAwareInterface; |
| 49 |
use Psr\Log\LoggerInterface; |
| 50 |
use Wikimedia\LightweightObjectStore\ExpirationAwareness; |
| 51 |
use Wikimedia\ObjectCache\BagOStuff; |
| 52 |
use Wikimedia\ObjectCache\EmptyBagOStuff; |
| 53 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 54 |
use Wikimedia\Rdbms\Database; |
| 55 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 56 |
use Wikimedia\Rdbms\IExpression; |
| 57 |
use Wikimedia\Rdbms\IResultWrapper; |
| 58 |
use Wikimedia\Rdbms\LikeValue; |
| 59 |
use Wikimedia\RequestTimeout\TimeoutException; |
| 60 |
use Wikimedia\ScopedCallback; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
define( 'MSG_CACHE_VERSION', 2 ); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
class MessageCache implements LoggerAwareInterface { |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public const CONSTRUCTOR_OPTIONS = [ |
| 78 |
MainConfigNames::UseDatabaseMessages, |
| 79 |
MainConfigNames::MaxMsgCacheEntrySize, |
| 80 |
MainConfigNames::AdaptiveMessageCache, |
| 81 |
MainConfigNames::UseXssLanguage, |
| 82 |
MainConfigNames::RawHtmlMessages, |
| 83 |
]; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public const MAX_REQUEST_LANGUAGES = 10; |
| 90 |
|
| 91 |
private const FOR_UPDATE = 1; |
| 92 |
|
| 93 |
|
| 94 |
private const WAIT_SEC = 15; |
| 95 |
|
| 96 |
private const LOCK_TTL = 30; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
private const WAN_TTL = ExpirationAwareness::TTL_DAY; |
| 102 |
|
| 103 |
|
| 104 |
private $logger; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private $cache; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private $systemMessageNames; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private $cacheVolatile = []; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private $disable; |
| 130 |
|
| 131 |
|
| 132 |
private $maxEntrySize; |
| 133 |
|
| 134 |
|
| 135 |
private $adaptive; |
| 136 |
|
| 137 |
|
| 138 |
private $useXssLanguage; |
| 139 |
|
| 140 |
|
| 141 |
private $rawHtmlMessages; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
private $parserOptions; |
| 148 |
|
| 149 |
|
| 150 |
private $parser = null; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
private $inParser = false; |
| 156 |
|
| 157 |
|
| 158 |
private $wanCache; |
| 159 |
|
| 160 |
private $clusterCache; |
| 161 |
|
| 162 |
private $srvCache; |
| 163 |
|
| 164 |
private $contLang; |
| 165 |
|
| 166 |
private $contLangCode; |
| 167 |
|
| 168 |
private $contLangConverter; |
| 169 |
|
| 170 |
private $langFactory; |
| 171 |
|
| 172 |
private $localisationCache; |
| 173 |
|
| 174 |
private $languageNameUtils; |
| 175 |
|
| 176 |
private $languageFallback; |
| 177 |
|
| 178 |
private $hookRunner; |
| 179 |
|
| 180 |
private $parserFactory; |
| 181 |
|
| 182 |
|
| 183 |
private $messageKeyOverrides; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public static function normalizeKey( $key ) { |
| 192 |
$lckey = strtr( $key, ' ', '_' ); |
| 193 |
if ( $lckey === '' ) { |
| 194 |
|
| 195 |
return $lckey; |
| 196 |
} |
| 197 |
|
| 198 |
if ( ord( $lckey[0] ) < 128 ) { |
| 199 |
$lckey[0] = strtolower( $lckey[0] ); |
| 200 |
} else { |
| 201 |
$lckey = MediaWikiServices::getInstance()->getContentLanguage()->lcfirst( $lckey ); |
| 202 |
} |
| 203 |
|
| 204 |
return $lckey; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public function __construct( |
| 224 |
WANObjectCache $wanCache, |
| 225 |
BagOStuff $clusterCache, |
| 226 |
BagOStuff $serverCache, |
| 227 |
Language $contLang, |
| 228 |
LanguageConverterFactory $langConverterFactory, |
| 229 |
LoggerInterface $logger, |
| 230 |
ServiceOptions $options, |
| 231 |
LanguageFactory $langFactory, |
| 232 |
LocalisationCache $localisationCache, |
| 233 |
LanguageNameUtils $languageNameUtils, |
| 234 |
LanguageFallback $languageFallback, |
| 235 |
HookContainer $hookContainer, |
| 236 |
ParserFactory $parserFactory |
| 237 |
) { |
| 238 |
$this->wanCache = $wanCache; |
| 239 |
$this->clusterCache = $clusterCache; |
| 240 |
$this->srvCache = $serverCache; |
| 241 |
$this->contLang = $contLang; |
| 242 |
$this->contLangConverter = $langConverterFactory->getLanguageConverter( $contLang ); |
| 243 |
$this->contLangCode = $contLang->getCode(); |
| 244 |
$this->logger = $logger; |
| 245 |
$this->langFactory = $langFactory; |
| 246 |
$this->localisationCache = $localisationCache; |
| 247 |
$this->languageNameUtils = $languageNameUtils; |
| 248 |
$this->languageFallback = $languageFallback; |
| 249 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 250 |
$this->parserFactory = $parserFactory; |
| 251 |
|
| 252 |
|
| 253 |
$this->cache = new MapCacheLRU( self::MAX_REQUEST_LANGUAGES ); |
| 254 |
|
| 255 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 256 |
$this->disable = !$options->get( MainConfigNames::UseDatabaseMessages ); |
| 257 |
$this->maxEntrySize = $options->get( MainConfigNames::MaxMsgCacheEntrySize ); |
| 258 |
$this->adaptive = $options->get( MainConfigNames::AdaptiveMessageCache ); |
| 259 |
$this->useXssLanguage = $options->get( MainConfigNames::UseXssLanguage ); |
| 260 |
$this->rawHtmlMessages = $options->get( MainConfigNames::RawHtmlMessages ); |
| 261 |
} |
| 262 |
|
| 263 |
public function setLogger( LoggerInterface $logger ) { |
| 264 |
$this->logger = $logger; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
private function getParserOptions() { |
| 273 |
if ( !$this->parserOptions ) { |
| 274 |
$context = RequestContext::getMain(); |
| 275 |
$user = $context->getUser(); |
| 276 |
if ( !$user->isSafeToLoad() ) { |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
$po = ParserOptions::newFromAnon(); |
| 281 |
$po->setAllowUnsafeRawHtml( false ); |
| 282 |
return $po; |
| 283 |
} |
| 284 |
|
| 285 |
$this->parserOptions = ParserOptions::newFromContext( $context ); |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
$this->parserOptions->setAllowUnsafeRawHtml( false ); |
| 290 |
} |
| 291 |
|
| 292 |
return $this->parserOptions; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
private function getLocalCache( $code ) { |
| 302 |
$cacheKey = $this->srvCache->makeKey( __CLASS__, $code ); |
| 303 |
|
| 304 |
return $this->srvCache->get( $cacheKey ); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
private function saveToLocalCache( $code, $cache ) { |
| 314 |
$cacheKey = $this->srvCache->makeKey( __CLASS__, $code ); |
| 315 |
$this->srvCache->set( $cacheKey, $cache ); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
private function load( string $code, $mode = null ) { |
| 339 |
|
| 340 |
if ( $this->isLanguageLoaded( $code ) && $mode !== self::FOR_UPDATE ) { |
| 341 |
return true; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
if ( $this->disable ) { |
| 346 |
static $shownDisabled = false; |
| 347 |
if ( !$shownDisabled ) { |
| 348 |
$this->logger->debug( __METHOD__ . ': disabled' ); |
| 349 |
$shownDisabled = true; |
| 350 |
} |
| 351 |
|
| 352 |
return true; |
| 353 |
} |
| 354 |
|
| 355 |
try { |
| 356 |
return $this->loadUnguarded( $code, $mode ); |
| 357 |
} catch ( Throwable $e ) { |
| 358 |
|
| 359 |
$this->disable = true; |
| 360 |
throw $e; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
private function loadUnguarded( $code, $mode ) { |
| 372 |
$success = false; |
| 373 |
$staleCache = false; |
| 374 |
$where = []; |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
[ $hash, $hashVolatile ] = $this->getValidationHash( $code ); |
| 379 |
$this->cacheVolatile[$code] = $hashVolatile; |
| 380 |
$volatilityOnlyStaleness = false; |
| 381 |
|
| 382 |
|
| 383 |
$cache = $this->getLocalCache( $code ); |
| 384 |
if ( !$cache ) { |
| 385 |
$where[] = 'local cache is empty'; |
| 386 |
} elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) { |
| 387 |
$where[] = 'local cache has the wrong hash'; |
| 388 |
$staleCache = $cache; |
| 389 |
} elseif ( $this->isCacheExpired( $cache ) ) { |
| 390 |
$where[] = 'local cache is expired'; |
| 391 |
$staleCache = $cache; |
| 392 |
} elseif ( $hashVolatile ) { |
| 393 |
|
| 394 |
$where[] = 'local cache validation key is expired/volatile'; |
| 395 |
$staleCache = $cache; |
| 396 |
$volatilityOnlyStaleness = true; |
| 397 |
} else { |
| 398 |
$where[] = 'got from local cache'; |
| 399 |
$this->cache->set( $code, $cache ); |
| 400 |
$success = true; |
| 401 |
} |
| 402 |
|
| 403 |
if ( !$success ) { |
| 404 |
|
| 405 |
$cacheKey = $this->clusterCache->makeKey( 'messages', $code ); |
| 406 |
for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) { |
| 407 |
if ( $volatilityOnlyStaleness && $staleCache ) { |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
$where[] = 'global cache is presumed expired'; |
| 413 |
} else { |
| 414 |
$cache = $this->clusterCache->get( $cacheKey ); |
| 415 |
if ( !$cache ) { |
| 416 |
$where[] = 'global cache is empty'; |
| 417 |
} elseif ( $this->isCacheExpired( $cache ) ) { |
| 418 |
$where[] = 'global cache is expired'; |
| 419 |
$staleCache = $cache; |
| 420 |
} elseif ( $hashVolatile ) { |
| 421 |
|
| 422 |
$where[] = 'global cache is expired/volatile'; |
| 423 |
$staleCache = $cache; |
| 424 |
} else { |
| 425 |
$where[] = 'got from global cache'; |
| 426 |
$this->cache->set( $code, $cache ); |
| 427 |
$this->saveToCaches( $cache, 'local-only', $code ); |
| 428 |
$success = true; |
| 429 |
break; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
$loadStatus = $this->loadFromDBWithMainLock( $code, $where, $mode ); |
| 437 |
if ( $loadStatus === true ) { |
| 438 |
$success = true; |
| 439 |
break; |
| 440 |
} elseif ( $staleCache ) { |
| 441 |
|
| 442 |
$where[] = 'using stale cache'; |
| 443 |
$this->cache->set( $code, $staleCache ); |
| 444 |
$success = true; |
| 445 |
break; |
| 446 |
} elseif ( $failedAttempts > 0 ) { |
| 447 |
$where[] = 'failed to find cache after waiting'; |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
break; |
| 452 |
} elseif ( $loadStatus === 'cantacquire' ) { |
| 453 |
|
| 454 |
|
| 455 |
$where[] = 'waiting for other thread to complete'; |
| 456 |
[ , $ioError ] = $this->getReentrantScopedLock( $code ); |
| 457 |
if ( $ioError ) { |
| 458 |
$where[] = 'failed waiting'; |
| 459 |
|
| 460 |
|
| 461 |
$success = $this->loadFromDBWithLocalLock( $code, $where, $mode ); |
| 462 |
break; |
| 463 |
} |
| 464 |
} else { |
| 465 |
|
| 466 |
break; |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
if ( !$success ) { |
| 472 |
$where[] = 'loading FAILED - cache is disabled'; |
| 473 |
$this->disable = true; |
| 474 |
$this->cache->set( $code, [] ); |
| 475 |
$this->logger->error( __METHOD__ . ": Failed to load $code" ); |
| 476 |
|
| 477 |
|
| 478 |
} |
| 479 |
|
| 480 |
if ( !$this->isLanguageLoaded( $code ) ) { |
| 481 |
throw new LogicException( "Process cache for '$code' should be set by now." ); |
| 482 |
} |
| 483 |
|
| 484 |
$info = implode( ', ', $where ); |
| 485 |
$this->logger->debug( __METHOD__ . ": Loading $code... $info" ); |
| 486 |
|
| 487 |
return $success; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
private function loadFromDBWithMainLock( $code, array &$where, $mode = null ) { |
| 497 |
|
| 498 |
|
| 499 |
$statusKey = $this->clusterCache->makeKey( 'messages', $code, 'status' ); |
| 500 |
$status = $this->clusterCache->get( $statusKey ); |
| 501 |
if ( $status === 'error' ) { |
| 502 |
$where[] = "could not load; method is still globally disabled"; |
| 503 |
return 'disabled'; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
$where[] = 'loading from DB'; |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
[ $scopedLock ] = $this->getReentrantScopedLock( $code, 0 ); |
| 514 |
if ( !$scopedLock ) { |
| 515 |
$where[] = 'could not acquire main lock'; |
| 516 |
return 'cantacquire'; |
| 517 |
} |
| 518 |
|
| 519 |
$cache = $this->loadFromDB( $code, $mode ); |
| 520 |
$this->cache->set( $code, $cache ); |
| 521 |
$saveSuccess = $this->saveToCaches( $cache, 'all', $code ); |
| 522 |
|
| 523 |
if ( !$saveSuccess ) { |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
if ( $this->srvCache instanceof EmptyBagOStuff ) { |
| 538 |
$this->clusterCache->set( $statusKey, 'error', 60 * 5 ); |
| 539 |
$where[] = 'could not save cache, disabled globally for 5 minutes'; |
| 540 |
} else { |
| 541 |
$where[] = "could not save global cache"; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
return true; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
private function loadFromDBWithLocalLock( $code, array &$where, $mode = null ) { |
| 555 |
$success = false; |
| 556 |
$where[] = 'loading from DB using local lock'; |
| 557 |
|
| 558 |
$scopedLock = $this->srvCache->getScopedLock( |
| 559 |
$this->srvCache->makeKey( 'messages', $code ), |
| 560 |
self::WAIT_SEC, |
| 561 |
self::LOCK_TTL, |
| 562 |
__METHOD__ |
| 563 |
); |
| 564 |
if ( $scopedLock ) { |
| 565 |
$cache = $this->loadFromDB( $code, $mode ); |
| 566 |
$this->cache->set( $code, $cache ); |
| 567 |
$this->saveToCaches( $cache, 'local-only', $code ); |
| 568 |
$success = true; |
| 569 |
} |
| 570 |
|
| 571 |
return $success; |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
private function loadFromDB( $code, $mode = null ) { |
| 584 |
$icp = MediaWikiServices::getInstance()->getConnectionProvider(); |
| 585 |
|
| 586 |
$dbr = ( $mode === self::FOR_UPDATE ) ? $icp->getPrimaryDatabase() : $icp->getReplicaDatabase(); |
| 587 |
|
| 588 |
$cache = []; |
| 589 |
|
| 590 |
$mostused = []; |
| 591 |
if ( $this->adaptive && $code !== $this->contLangCode ) { |
| 592 |
if ( !$this->cache->has( $this->contLangCode ) ) { |
| 593 |
$this->load( $this->contLangCode ); |
| 594 |
} |
| 595 |
$mostused = array_keys( $this->cache->get( $this->contLangCode ) ); |
| 596 |
foreach ( $mostused as $key => $value ) { |
| 597 |
$mostused[$key] = "$value/$code"; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
$conds = [ |
| 603 |
|
| 604 |
'page_is_redirect' => 0, |
| 605 |
'page_namespace' => NS_MEDIAWIKI, |
| 606 |
]; |
| 607 |
if ( count( $mostused ) ) { |
| 608 |
$conds['page_title'] = $mostused; |
| 609 |
} elseif ( $code !== $this->contLangCode ) { |
| 610 |
$conds[] = $dbr->expr( |
| 611 |
'page_title', |
| 612 |
IExpression::LIKE, |
| 613 |
new LikeValue( $dbr->anyString(), '/', $code ) |
| 614 |
); |
| 615 |
} else { |
| 616 |
|
| 617 |
|
| 618 |
$conds[] = $dbr->expr( |
| 619 |
'page_title', |
| 620 |
IExpression::NOT_LIKE, |
| 621 |
new LikeValue( $dbr->anyString(), '/', $dbr->anyString() ) |
| 622 |
); |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
$res = $dbr->newSelectQueryBuilder() |
| 627 |
->select( [ 'page_title', 'page_latest' ] ) |
| 628 |
->from( 'page' ) |
| 629 |
->where( $conds ) |
| 630 |
->andWhere( $dbr->expr( 'page_len', '>', intval( $this->maxEntrySize ) ) ) |
| 631 |
->caller( __METHOD__ . "($code)-big" )->fetchResultSet(); |
| 632 |
foreach ( $res as $row ) { |
| 633 |
|
| 634 |
if ( $this->adaptive || $this->isMainCacheable( $row->page_title ) ) { |
| 635 |
$cache[$row->page_title] = '!TOO BIG'; |
| 636 |
} |
| 637 |
|
| 638 |
$cache['EXCESSIVE'][$row->page_title] = $row->page_latest; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
$revisionStore = MediaWikiServices::getInstance()->getRevisionStore(); |
| 644 |
|
| 645 |
$revQuery = $revisionStore->getQueryInfo( [ 'page' ] ); |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
$revQuery['joins']['revision'] = $revQuery['joins']['page']; |
| 652 |
unset( $revQuery['joins']['page'] ); |
| 653 |
|
| 654 |
l |
| 655 |
|
| 656 |
$revQuery['tables'] = array_merge( |
| 657 |
[ 'page' ], |
| 658 |
array_diff( $revQuery['tables'], [ 'page' ] ) |
| 659 |
); |
| 660 |
|
| 661 |
$res = $dbr->newSelectQueryBuilder() |
| 662 |
->queryInfo( $revQuery ) |
| 663 |
->where( $conds ) |
| 664 |
->andWhere( [ |
| 665 |
$dbr->expr( 'page_len', '<=', intval( $this->maxEntrySize ) ), |
| 666 |
'page_latest = rev_id' |
| 667 |
] ) |
| 668 |
->caller( __METHOD__ . "($code)-small" ) |
| 669 |
->straightJoinOption() |
| 670 |
->fetchResultSet(); |
| 671 |
|
| 672 |
|
| 673 |
[ $cacheableRows, $uncacheableRows ] = $this->separateCacheableRows( $res ); |
| 674 |
$result = $revisionStore->newRevisionsFromBatch( $cacheableRows, [ |
| 675 |
'slots' => [ SlotRecord::MAIN ], |
| 676 |
'content' => true |
| 677 |
] ); |
| 678 |
$revisions = $result->isOK() ? $result->getValue() : []; |
| 679 |
|
| 680 |
foreach ( $cacheableRows as $row ) { |
| 681 |
try { |
| 682 |
$rev = $revisions[$row->rev_id] ?? null; |
| 683 |
$content = $rev ? $rev->getContent( SlotRecord::MAIN ) : null; |
| 684 |
$text = $this->getMessageTextFromContent( $content ); |
| 685 |
} catch ( TimeoutException $e ) { |
| 686 |
throw $e; |
| 687 |
} catch ( Exception $ex ) { |
| 688 |
$text = false; |
| 689 |
} |
| 690 |
|
| 691 |
if ( !is_string( $text ) ) { |
| 692 |
$entry = '!ERROR'; |
| 693 |
$this->logger->error( |
| 694 |
__METHOD__ |
| 695 |
. ": failed to load message page text for {$row->page_title} ($code)" |
| 696 |
); |
| 697 |
} else { |
| 698 |
$entry = ' ' . $text; |
| 699 |
} |
| 700 |
$cache[$row->page_title] = $entry; |
| 701 |
} |
| 702 |
|
| 703 |
foreach ( $uncacheableRows as $row ) { |
| 704 |
|
| 705 |
|
| 706 |
$cache['EXCESSIVE'][$row->page_title] = $row->page_latest; |
| 707 |
} |
| 708 |
|
| 709 |
$cache['VERSION'] = MSG_CACHE_VERSION; |
| 710 |
ksort( $cache ); |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
$cache['HASH'] = md5( serialize( $cache ) ); |
| 716 |
$cache['EXPIRY'] = wfTimestamp( TS_MW, time() + self::WAN_TTL ); |
| 717 |
unset( $cache['EXCESSIVE'] ); |
| 718 |
|
| 719 |
return $cache; |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
private function isLanguageLoaded( $lang ) { |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
return $this->cache->hasField( $lang, 'VERSION' ); |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
private function isMainCacheable( $name, $code = null ) { |
| 750 |
|
| 751 |
$name = $this->contLang->lcfirst( $name ); |
| 752 |
|
| 753 |
|
| 754 |
if ( strpos( $name, 'conversiontable/' ) === 0 ) { |
| 755 |
return true; |
| 756 |
} |
| 757 |
$msg = preg_replace( '/\/[a-z0-9-]{2,}$/', '', $name ); |
| 758 |
|
| 759 |
if ( $code === null ) { |
| 760 |
|
| 761 |
if ( $this->systemMessageNames === null ) { |
| 762 |
$this->systemMessageNames = array_fill_keys( |
| 763 |
$this->localisationCache->getSubitemList( $this->contLangCode, 'messages' ), |
| 764 |
true ); |
| 765 |
} |
| 766 |
return isset( $this->systemMessageNames[$msg] ); |
| 767 |
} else { |
| 768 |
|
| 769 |
return $this->localisationCache->getSubitem( $code, 'messages', $msg ) !== null; |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
private function separateCacheableRows( $res ) { |
| 781 |
if ( $this->adaptive ) { |
| 782 |
|
| 783 |
return [ $res, [] ]; |
| 784 |
} |
| 785 |
$cacheableRows = []; |
| 786 |
$uncacheableRows = []; |
| 787 |
foreach ( $res as $row ) { |
| 788 |
if ( $this->isMainCacheable( $row->page_title ) ) { |
| 789 |
$cacheableRows[] = $row; |
| 790 |
} else { |
| 791 |
$uncacheableRows[] = $row; |
| 792 |
} |
| 793 |
} |
| 794 |
return [ $cacheableRows, $uncacheableRows ]; |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
public function replace( $title, $text ) { |
| 804 |
if ( $this->disable ) { |
| 805 |
return; |
| 806 |
} |
| 807 |
|
| 808 |
[ $msg, $code ] = $this->figureMessage( $title ); |
| 809 |
if ( strpos( $title, '/' ) !== false && $code === $this->contLangCode ) { |
| 810 |
|
| 811 |
return; |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
if ( $text === false ) { |
| 816 |
|
| 817 |
$this->cache->setField( $code, $title, '!NONEXISTENT' ); |
| 818 |
} else { |
| 819 |
|
| 820 |
$this->cache->setField( $code, $title, ' ' . $text ); |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
DeferredUpdates::addUpdate( |
| 825 |
new MessageCacheUpdate( $code, $title, $msg ), |
| 826 |
DeferredUpdates::PRESEND |
| 827 |
); |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
public function refreshAndReplaceInternal( string $code, array $replacements ) { |
| 835 |
|
| 836 |
[ $scopedLock ] = $this->getReentrantScopedLock( $code ); |
| 837 |
if ( !$scopedLock ) { |
| 838 |
foreach ( $replacements as [ $title ] ) { |
| 839 |
$this->logger->error( |
| 840 |
__METHOD__ . ': could not acquire lock to update {title} ({code})', |
| 841 |
[ 'title' => $title, 'code' => $code ] ); |
| 842 |
} |
| 843 |
|
| 844 |
return; |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
if ( $this->load( $code, self::FOR_UPDATE ) ) { |
| 850 |
$cache = $this->cache->get( $code ); |
| 851 |
} else { |
| 852 |
|
| 853 |
$cache = $this->loadFromDB( $code, self::FOR_UPDATE ); |
| 854 |
} |
| 855 |
|
| 856 |
$newTextByTitle = []; |
| 857 |
$newBigTitles = []; |
| 858 |
|
| 859 |
|
| 860 |
$wikiPageFactory = MediaWikiServices::getInstance()->getWikiPageFactory(); |
| 861 |
foreach ( $replacements as [ $title ] ) { |
| 862 |
$page = $wikiPageFactory->newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) ); |
| 863 |
$page->loadPageData( IDBAccessObject::READ_LATEST ); |
| 864 |
$text = $this->getMessageTextFromContent( $page->getContent() ); |
| 865 |
|
| 866 |
$newTextByTitle[$title] = $text ?? ''; |
| 867 |
|
| 868 |
if ( !is_string( $text ) ) { |
| 869 |
$cache[$title] = '!NONEXISTENT'; |
| 870 |
} elseif ( strlen( $text ) > $this->maxEntrySize ) { |
| 871 |
$cache[$title] = '!TOO BIG'; |
| 872 |
$newBigTitles[$title] = $page->getLatest(); |
| 873 |
} else { |
| 874 |
$cache[$title] = ' ' . $text; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
$cache['HASH'] = md5( serialize( $cache + [ 'EXCESSIVE' => $newBigTitles ] ) ); |
| 882 |
|
| 883 |
foreach ( $newBigTitles as $title => $id ) { |
| 884 |
|
| 885 |
$this->wanCache->set( |
| 886 |
$this->bigMessageCacheKey( $cache['HASH'], $title ), |
| 887 |
' ' . $newTextByTitle[$title], |
| 888 |
self::WAN_TTL |
| 889 |
); |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
$cache['LATEST'] = time(); |
| 894 |
|
| 895 |
$this->cache->set( $code, $cache ); |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
$this->saveToCaches( $cache, 'all', $code ); |
| 901 |
|
| 902 |
ScopedCallback::consume( $scopedLock ); |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
$this->wanCache->touchCheckKey( $this->getCheckKey( $code ) ); |
| 907 |
|
| 908 |
|
| 909 |
$blobStore = MediaWikiServices::getInstance()->getResourceLoader()->getMessageBlobStore(); |
| 910 |
foreach ( $replacements as [ $title, $msg ] ) { |
| 911 |
$blobStore->updateMessage( $this->contLang->lcfirst( $msg ) ); |
| 912 |
$this->hookRunner->onMessageCacheReplace( $title, $newTextByTitle[$title] ); |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
private function isCacheExpired( $cache ) { |
| 923 |
return !isset( $cache['VERSION'] ) || |
| 924 |
!isset( $cache['EXPIRY'] ) || |
| 925 |
$cache['VERSION'] !== MSG_CACHE_VERSION || |
| 926 |
$cache['EXPIRY'] <= wfTimestampNow(); |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
private function saveToCaches( array $cache, $dest, $code = false ) { |
| 939 |
if ( $dest === 'all' ) { |
| 940 |
$cacheKey = $this->clusterCache->makeKey( 'messages', $code ); |
| 941 |
$success = $this->clusterCache->set( $cacheKey, $cache ); |
| 942 |
$this->setValidationHash( $code, $cache ); |
| 943 |
} else { |
| 944 |
$success = true; |
| 945 |
} |
| 946 |
|
| 947 |
$this->saveToLocalCache( $code, $cache ); |
| 948 |
|
| 949 |
return $success; |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
private function getValidationHash( $code ) { |
| 959 |
$curTTL = null; |
| 960 |
$value = $this->wanCache->get( |
| 961 |
$this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ), |
| 962 |
$curTTL, |
| 963 |
[ $this->getCheckKey( $code ) ] |
| 964 |
); |
| 965 |
|
| 966 |
if ( $value ) { |
| 967 |
$hash = $value['hash']; |
| 968 |
if ( ( time() - $value['latest'] ) < WANObjectCache::TTL_MINUTE ) { |
| 969 |
|
| 970 |
|
| 971 |
$expired = false; |
| 972 |
} else { |
| 973 |
|
| 974 |
$expired = ( $curTTL < 0 ); |
| 975 |
} |
| 976 |
} else { |
| 977 |
|
| 978 |
$hash = false; |
| 979 |
$expired = true; |
| 980 |
} |
| 981 |
|
| 982 |
return [ $hash, $expired ]; |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
private function setValidationHash( $code, array $cache ) { |
| 996 |
$this->wanCache->set( |
| 997 |
$this->wanCache->makeKey( 'messages', $code, 'hash', 'v1' ), |
| 998 |
[ |
| 999 |
'hash' => $cache['HASH'], |
| 1000 |
'latest' => $cache['LATEST'] ?? 0 |
| 1001 |
], |
| 1002 |
WANObjectCache::TTL_INDEFINITE |
| 1003 |
); |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
private function getReentrantScopedLock( $code, $timeout = self::WAIT_SEC ) { |
| 1013 |
$key = $this->clusterCache->makeKey( 'messages', $code ); |
| 1014 |
|
| 1015 |
$watchPoint = $this->clusterCache->watchErrors(); |
| 1016 |
$scopedLock = $this->clusterCache->getScopedLock( |
| 1017 |
$key, |
| 1018 |
$timeout, |
| 1019 |
self::LOCK_TTL, |
| 1020 |
__METHOD__ |
| 1021 |
); |
| 1022 |
$error = ( !$scopedLock && $this->clusterCache->getLastError( $watchPoint ) ); |
| 1023 |
|
| 1024 |
return [ $scopedLock, $error ]; |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
public function get( $key, $useDB = true, $language = null, &$usedKey = '' ) { |
| 1064 |
if ( is_int( $key ) ) { |
| 1065 |
|
| 1066 |
$key = (string)$key; |
| 1067 |
} elseif ( !is_string( $key ) ) { |
| 1068 |
throw new TypeError( 'Message key must be a string' ); |
| 1069 |
} elseif ( $key === '' ) { |
| 1070 |
|
| 1071 |
return false; |
| 1072 |
} |
| 1073 |
|
| 1074 |
$language ??= $this->contLang; |
| 1075 |
$language = $this->getLanguageObject( $language ); |
| 1076 |
|
| 1077 |
|
| 1078 |
$lckey = self::normalizeKey( $key ); |
| 1079 |
|
| 1080 |
|
| 1081 |
if ( $this->messageKeyOverrides === null ) { |
| 1082 |
$this->messageKeyOverrides = []; |
| 1083 |
$this->hookRunner->onMessageCacheFetchOverrides( $this->messageKeyOverrides ); |
| 1084 |
} |
| 1085 |
|
| 1086 |
if ( isset( $this->messageKeyOverrides[$lckey] ) ) { |
| 1087 |
$override = $this->messageKeyOverrides[$lckey]; |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
if ( is_string( $override ) ) { |
| 1092 |
$lckey = $override; |
| 1093 |
} else { |
| 1094 |
$lckey = $override( $lckey, $this, $language, $useDB ); |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
$this->hookRunner->onMessageCache__get( $lckey ); |
| 1099 |
|
| 1100 |
$usedKey = $lckey; |
| 1101 |
|
| 1102 |
|
| 1103 |
$message = $this->getMessageFromFallbackChain( |
| 1104 |
$language, |
| 1105 |
$lckey, |
| 1106 |
!$this->disable && $useDB |
| 1107 |
); |
| 1108 |
|
| 1109 |
|
| 1110 |
if ( $message === false ) { |
| 1111 |
$parts = explode( '/', $lckey ); |
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
if ( count( $parts ) === 2 && $parts[1] !== '' ) { |
| 1116 |
$message = $this->localisationCache->getSubitem( $parts[1], 'messages', $parts[0] ) ?? false; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
if ( $message !== false ) { |
| 1122 |
|
| 1123 |
$message = str_replace( |
| 1124 |
[ |
| 1125 |
|
| 1126 |
' ', |
| 1127 |
|
| 1128 |
' ', |
| 1129 |
' ', |
| 1130 |
'­' |
| 1131 |
], |
| 1132 |
[ |
| 1133 |
' ', |
| 1134 |
"\u{00A0}", |
| 1135 |
"\u{00A0}", |
| 1136 |
"\u{00AD}" |
| 1137 |
], |
| 1138 |
$message |
| 1139 |
); |
| 1140 |
} |
| 1141 |
|
| 1142 |
return $message; |
| 1143 |
} |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
private function getLanguageObject( $langcode ) { |
| 1161 |
|
| 1162 |
|
| 1163 |
if ( is_object( $langcode ) ) { |
| 1164 |
|
| 1165 |
return $langcode; |
| 1166 |
} |
| 1167 |
|
| 1168 |
wfDeprecated( __METHOD__ . ' with not a Language object in $langcode', '1.43' ); |
| 1169 |
if ( $langcode === true || $langcode === $this->contLangCode ) { |
| 1170 |
|
| 1171 |
|
| 1172 |
return $this->contLang; |
| 1173 |
} |
| 1174 |
|
| 1175 |
global $wgLang; |
| 1176 |
if ( $langcode === false || $langcode === $wgLang->getCode() ) { |
| 1177 |
|
| 1178 |
|
| 1179 |
return $wgLang; |
| 1180 |
} |
| 1181 |
|
| 1182 |
$validCodes = array_keys( $this->languageNameUtils->getLanguageNames() ); |
| 1183 |
if ( in_array( $langcode, $validCodes ) ) { |
| 1184 |
|
| 1185 |
return $this->langFactory->getLanguage( $langcode ); |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
$this->logger->debug( 'Invalid language code passed to' . __METHOD__ . ', falling back to content language.' ); |
| 1190 |
return $this->contLang; |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
private function getMessageFromFallbackChain( $lang, $lckey, $useDB ) { |
| 1206 |
$alreadyTried = []; |
| 1207 |
|
| 1208 |
|
| 1209 |
$message = $this->getMessageForLang( $lang, $lckey, $useDB, $alreadyTried ); |
| 1210 |
if ( $message !== false ) { |
| 1211 |
return $message; |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
$message = $this->getMessageForLang( $this->contLang, $lckey, $useDB, $alreadyTried ); |
| 1216 |
return $message; |
| 1217 |
} |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
private function getMessageForLang( $lang, $lckey, $useDB, &$alreadyTried ) { |
| 1230 |
$langcode = $lang->getCode(); |
| 1231 |
|
| 1232 |
|
| 1233 |
if ( $useDB ) { |
| 1234 |
$uckey = $this->contLang->ucfirst( $lckey ); |
| 1235 |
|
| 1236 |
if ( !isset( $alreadyTried[$langcode] ) ) { |
| 1237 |
$message = $this->getMsgFromNamespace( |
| 1238 |
$this->getMessagePageName( $langcode, $uckey ), |
| 1239 |
$langcode |
| 1240 |
); |
| 1241 |
if ( $message !== false ) { |
| 1242 |
return $message; |
| 1243 |
} |
| 1244 |
$alreadyTried[$langcode] = true; |
| 1245 |
} |
| 1246 |
} else { |
| 1247 |
$uckey = null; |
| 1248 |
} |
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
if ( $langcode === 'qqx' ) { |
| 1254 |
return '($*)'; |
| 1255 |
} elseif ( |
| 1256 |
$langcode === 'x-xss' && |
| 1257 |
$this->useXssLanguage && |
| 1258 |
!in_array( $lckey, $this->rawHtmlMessages, true ) |
| 1259 |
) { |
| 1260 |
$xssViaInnerHtml = "<script>alert('$lckey')</script>"; |
| 1261 |
$xssViaAttribute = '">' . $xssViaInnerHtml . '<x y="'; |
| 1262 |
return $xssViaInnerHtml . $xssViaAttribute . '($*)'; |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
[ $defaultMessage, $messageSource ] = |
| 1267 |
$this->localisationCache->getSubitemWithSource( $langcode, 'messages', $lckey ); |
| 1268 |
if ( $messageSource === $langcode ) { |
| 1269 |
return $defaultMessage; |
| 1270 |
} |
| 1271 |
|
| 1272 |
|
| 1273 |
if ( $useDB ) { |
| 1274 |
$fallbackChain = $this->languageFallback->getAll( $langcode ); |
| 1275 |
|
| 1276 |
foreach ( $fallbackChain as $code ) { |
| 1277 |
if ( isset( $alreadyTried[$code] ) ) { |
| 1278 |
continue; |
| 1279 |
} |
| 1280 |
|
| 1281 |
$message = $this->getMsgFromNamespace( |
| 1282 |
|
| 1283 |
$this->getMessagePageName( $code, $uckey ), $code ); |
| 1284 |
|
| 1285 |
if ( $message !== false ) { |
| 1286 |
return $message; |
| 1287 |
} |
| 1288 |
$alreadyTried[$code] = true; |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
if ( $code === $messageSource ) { |
| 1293 |
return $defaultMessage; |
| 1294 |
} |
| 1295 |
} |
| 1296 |
} |
| 1297 |
|
| 1298 |
return $defaultMessage ?? false; |
| 1299 |
} |
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
private function getMessagePageName( $langcode, $uckey ) { |
| 1309 |
if ( $langcode === $this->contLangCode ) { |
| 1310 |
|
| 1311 |
return $uckey; |
| 1312 |
} else { |
| 1313 |
return "$uckey/$langcode"; |
| 1314 |
} |
| 1315 |
} |
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
public function getMsgFromNamespace( $title, $code ) { |
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
$this->load( $code ); |
| 1334 |
|
| 1335 |
$entry = $this->cache->getField( $code, $title ); |
| 1336 |
|
| 1337 |
if ( $entry !== null ) { |
| 1338 |
|
| 1339 |
if ( substr( $entry, 0, 1 ) === ' ' ) { |
| 1340 |
|
| 1341 |
return substr( $entry, 1 ); |
| 1342 |
} elseif ( $entry === '!NONEXISTENT' ) { |
| 1343 |
|
| 1344 |
return false; |
| 1345 |
} |
| 1346 |
|
| 1347 |
|
| 1348 |
$entry = $this->loadCachedMessagePageEntry( |
| 1349 |
$title, |
| 1350 |
$code, |
| 1351 |
$this->cache->getField( $code, 'HASH' ) |
| 1352 |
); |
| 1353 |
} else { |
| 1354 |
|
| 1355 |
if ( !$this->isMainCacheable( $title, $code ) ) { |
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
$entry = $this->loadCachedMessagePageEntry( |
| 1360 |
$title, |
| 1361 |
$code, |
| 1362 |
$this->cache->getField( $code, 'HASH' ) |
| 1363 |
); |
| 1364 |
} |
| 1365 |
if ( $entry === null || substr( $entry, 0, 1 ) !== ' ' ) { |
| 1366 |
|
| 1367 |
$message = false; |
| 1368 |
|
| 1369 |
$this->hookRunner->onMessagesPreLoad( $title, $message, $code ); |
| 1370 |
if ( $message !== false ) { |
| 1371 |
$this->cache->setField( $code, $title, ' ' . $message ); |
| 1372 |
} else { |
| 1373 |
$this->cache->setField( $code, $title, '!NONEXISTENT' ); |
| 1374 |
} |
| 1375 |
|
| 1376 |
return $message; |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
if ( $entry !== false && substr( $entry, 0, 1 ) === ' ' ) { |
| 1381 |
if ( $this->cacheVolatile[$code] ) { |
| 1382 |
|
| 1383 |
$this->logger->debug( |
| 1384 |
__METHOD__ . ': loading volatile key \'{titleKey}\'', |
| 1385 |
[ 'titleKey' => $title, 'code' => $code ] ); |
| 1386 |
} else { |
| 1387 |
$this->cache->setField( $code, $title, $entry ); |
| 1388 |
} |
| 1389 |
|
| 1390 |
return substr( $entry, 1 ); |
| 1391 |
} |
| 1392 |
|
| 1393 |
$this->cache->setField( $code, $title, '!NONEXISTENT' ); |
| 1394 |
|
| 1395 |
return false; |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
private function loadCachedMessagePageEntry( $dbKey, $code, $hash ) { |
| 1405 |
$fname = __METHOD__; |
| 1406 |
return $this->srvCache->getWithSetCallback( |
| 1407 |
$this->srvCache->makeKey( 'messages-big', $hash, $dbKey ), |
| 1408 |
BagOStuff::TTL_HOUR, |
| 1409 |
function () use ( $code, $dbKey, $hash, $fname ) { |
| 1410 |
return $this->wanCache->getWithSetCallback( |
| 1411 |
$this->bigMessageCacheKey( $hash, $dbKey ), |
| 1412 |
self::WAN_TTL, |
| 1413 |
function ( $oldValue, &$ttl, &$setOpts ) use ( $dbKey, $code, $fname ) { |
| 1414 |
|
| 1415 |
$setOpts += Database::getCacheSetOptions( |
| 1416 |
MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase() |
| 1417 |
); |
| 1418 |
|
| 1419 |
$title = Title::makeTitle( NS_MEDIAWIKI, $dbKey ); |
| 1420 |
|
| 1421 |
|
| 1422 |
$revision = MediaWikiServices::getInstance() |
| 1423 |
->getRevisionLookup() |
| 1424 |
->getKnownCurrentRevision( $title ); |
| 1425 |
if ( !$revision ) { |
| 1426 |
|
| 1427 |
|
| 1428 |
return '!NONEXISTENT'; |
| 1429 |
} |
| 1430 |
$content = $revision->getContent( SlotRecord::MAIN ); |
| 1431 |
if ( $content ) { |
| 1432 |
$message = $this->getMessageTextFromContent( $content ); |
| 1433 |
} else { |
| 1434 |
$this->logger->warning( |
| 1435 |
$fname . ': failed to load page text for \'{titleKey}\'', |
| 1436 |
[ 'titleKey' => $dbKey, 'code' => $code ] |
| 1437 |
); |
| 1438 |
$message = null; |
| 1439 |
} |
| 1440 |
|
| 1441 |
if ( !is_string( $message ) ) { |
| 1442 |
|
| 1443 |
|
| 1444 |
$ttl = 5; |
| 1445 |
|
| 1446 |
return '!NONEXISTENT'; |
| 1447 |
} |
| 1448 |
|
| 1449 |
return ' ' . $message; |
| 1450 |
} |
| 1451 |
); |
| 1452 |
} |
| 1453 |
); |
| 1454 |
} |
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
public function transform( $message, $interface = false, $language = null, ?PageReference $page = null ) { |
| 1464 |
|
| 1465 |
if ( $this->inParser || !str_contains( $message, '{{' ) ) { |
| 1466 |
return $message; |
| 1467 |
} |
| 1468 |
|
| 1469 |
$parser = $this->getParser(); |
| 1470 |
$popts = $this->getParserOptions(); |
| 1471 |
$popts->setInterfaceMessage( $interface ); |
| 1472 |
$popts->setTargetLanguage( $language ); |
| 1473 |
|
| 1474 |
$userlang = $popts->setUserLang( $language ); |
| 1475 |
$this->inParser = true; |
| 1476 |
$message = $parser->transformMsg( $message, $popts, $page ); |
| 1477 |
$this->inParser = false; |
| 1478 |
$popts->setUserLang( $userlang ); |
| 1479 |
|
| 1480 |
return $message; |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
public function getParser() { |
| 1487 |
if ( !$this->parser ) { |
| 1488 |
$this->parser = $this->parserFactory->create(); |
| 1489 |
} |
| 1490 |
|
| 1491 |
return $this->parser; |
| 1492 |
} |
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
public function parse( $text, ?PageReference $page = null, $linestart = true, |
| 1503 |
$interface = false, $language = null |
| 1504 |
) { |
| 1505 |
|
| 1506 |
global $wgTitle; |
| 1507 |
|
| 1508 |
if ( $this->inParser ) { |
| 1509 |
return htmlspecialchars( $text ); |
| 1510 |
} |
| 1511 |
|
| 1512 |
$parser = $this->getParser(); |
| 1513 |
$popts = $this->getParserOptions(); |
| 1514 |
$popts->setInterfaceMessage( $interface ); |
| 1515 |
|
| 1516 |
if ( is_string( $language ) ) { |
| 1517 |
$language = $this->langFactory->getLanguage( $language ); |
| 1518 |
} |
| 1519 |
$popts->setTargetLanguage( $language ); |
| 1520 |
|
| 1521 |
if ( !$page ) { |
| 1522 |
$logger = LoggerFactory::getInstance( 'GlobalTitleFail' ); |
| 1523 |
$logger->info( |
| 1524 |
__METHOD__ . ' called with no title set.', |
| 1525 |
[ 'exception' => new RuntimeException ] |
| 1526 |
); |
| 1527 |
$page = $wgTitle; |
| 1528 |
} |
| 1529 |
|
| 1530 |
if ( !$page ) { |
| 1531 |
|
| 1532 |
|
| 1533 |
$page = PageReferenceValue::localReference( |
| 1534 |
NS_SPECIAL, |
| 1535 |
'Badtitle/title not set in ' . __METHOD__ |
| 1536 |
); |
| 1537 |
} |
| 1538 |
|
| 1539 |
$this->inParser = true; |
| 1540 |
$res = $parser->parse( $text, $page, $popts, $linestart ); |
| 1541 |
$this->inParser = false; |
| 1542 |
|
| 1543 |
return $res; |
| 1544 |
} |
| 1545 |
|
| 1546 |
public function disable() { |
| 1547 |
$this->disable = true; |
| 1548 |
} |
| 1549 |
|
| 1550 |
public function enable() { |
| 1551 |
$this->disable = false; |
| 1552 |
} |
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
public function isDisabled() { |
| 1567 |
return $this->disable; |
| 1568 |
} |
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
public function clear() { |
| 1576 |
$langs = $this->languageNameUtils->getLanguageNames(); |
| 1577 |
foreach ( $langs as $code => $_ ) { |
| 1578 |
$this->wanCache->touchCheckKey( $this->getCheckKey( $code ) ); |
| 1579 |
} |
| 1580 |
$this->cache->clear(); |
| 1581 |
} |
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
public function figureMessage( $key ) { |
| 1588 |
$pieces = explode( '/', $key ); |
| 1589 |
if ( count( $pieces ) < 2 ) { |
| 1590 |
return [ $key, $this->contLangCode ]; |
| 1591 |
} |
| 1592 |
|
| 1593 |
$lang = array_pop( $pieces ); |
| 1594 |
if ( !$this->languageNameUtils->getLanguageName( |
| 1595 |
$lang, |
| 1596 |
LanguageNameUtils::AUTONYMS, |
| 1597 |
LanguageNameUtils::DEFINED |
| 1598 |
) ) { |
| 1599 |
return [ $key, $this->contLangCode ]; |
| 1600 |
} |
| 1601 |
|
| 1602 |
$message = implode( '/', $pieces ); |
| 1603 |
|
| 1604 |
return [ $message, $lang ]; |
| 1605 |
} |
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
public function getAllMessageKeys( $code ) { |
| 1617 |
$this->load( $code ); |
| 1618 |
if ( !$this->cache->has( $code ) ) { |
| 1619 |
|
| 1620 |
return null; |
| 1621 |
} |
| 1622 |
|
| 1623 |
$cache = $this->cache->get( $code ); |
| 1624 |
unset( $cache['VERSION'] ); |
| 1625 |
unset( $cache['EXPIRY'] ); |
| 1626 |
unset( $cache['EXCESSIVE'] ); |
| 1627 |
|
| 1628 |
$cache = array_diff( $cache, [ '!NONEXISTENT' ] ); |
| 1629 |
|
| 1630 |
|
| 1631 |
return array_map( [ $this->contLang, 'lcfirst' ], array_keys( $cache ) ); |
| 1632 |
} |
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
public function updateMessageOverride( LinkTarget $linkTarget, ?Content $content = null ) { |
| 1642 |
|
| 1643 |
$msgText = $this->getMessageTextFromContent( $content ) ?? false; |
| 1644 |
|
| 1645 |
$this->replace( $linkTarget->getDBkey(), $msgText ); |
| 1646 |
|
| 1647 |
if ( $this->contLangConverter->hasVariants() ) { |
| 1648 |
$this->contLangConverter->updateConversionTable( $linkTarget ); |
| 1649 |
} |
| 1650 |
} |
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
public function getCheckKey( $code ) { |
| 1657 |
return $this->wanCache->makeKey( 'messages', $code ); |
| 1658 |
} |
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
private function getMessageTextFromContent( ?Content $content = null ) { |
| 1665 |
|
| 1666 |
if ( $content && $content->isRedirect() ) { |
| 1667 |
|
| 1668 |
$msgText = false; |
| 1669 |
} elseif ( $content ) { |
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
$msgText = $content->getWikitextForTransclusion(); |
| 1675 |
if ( $msgText === false || $msgText === null ) { |
| 1676 |
|
| 1677 |
$msgText = null; |
| 1678 |
$this->logger->warning( |
| 1679 |
__METHOD__ . ": message content doesn't provide wikitext " |
| 1680 |
. "(content model: " . $content->getModel() . ")" ); |
| 1681 |
} |
| 1682 |
} else { |
| 1683 |
|
| 1684 |
$msgText = false; |
| 1685 |
} |
| 1686 |
|
| 1687 |
return $msgText; |
| 1688 |
} |
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
private function bigMessageCacheKey( $hash, $title ) { |
| 1696 |
return $this->wanCache->makeKey( 'messages-big', $hash, $title ); |
| 1697 |
} |
| 1698 |
} |