| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use CLDRPluralRuleParser\Error as CLDRPluralRuleError; |
| 22 |
use CLDRPluralRuleParser\Evaluator; |
| 23 |
use MediaWiki\Config\ConfigException; |
| 24 |
use MediaWiki\Config\ServiceOptions; |
| 25 |
use MediaWiki\HookContainer\HookContainer; |
| 26 |
use MediaWiki\HookContainer\HookRunner; |
| 27 |
use MediaWiki\Json\FormatJson; |
| 28 |
use MediaWiki\Languages\LanguageNameUtils; |
| 29 |
use MediaWiki\MainConfigNames; |
| 30 |
use Psr\Log\LoggerInterface; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class LocalisationCache { |
| 49 |
public const VERSION = 5; |
| 50 |
|
| 51 |
|
| 52 |
private $options; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private $manualRecache; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
protected $data = []; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
protected $sourceLanguage = []; |
| 79 |
|
| 80 |
|
| 81 |
private $store; |
| 82 |
|
| 83 |
private $logger; |
| 84 |
|
| 85 |
private $hookRunner; |
| 86 |
|
| 87 |
private $clearStoreCallbacks; |
| 88 |
|
| 89 |
private $langNameUtils; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private $loadedItems = []; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private $loadedSubitems = []; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
private $initialisedLangs = []; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
private $shallowFallbacks = []; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
private $recachedLangs = []; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private $coreDataLoaded = []; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public const ALL_KEYS = [ |
| 151 |
'fallback', 'namespaceNames', 'bookstoreList', |
| 152 |
'magicWords', 'messages', 'rtl', |
| 153 |
'digitTransformTable', 'separatorTransformTable', |
| 154 |
'minimumGroupingDigits', 'numberingSystem', 'fallback8bitEncoding', |
| 155 |
'linkPrefixExtension', 'linkTrail', 'linkPrefixCharset', |
| 156 |
'namespaceAliases', 'dateFormats', 'jsDateFormats', 'datePreferences', |
| 157 |
'datePreferenceMigrationMap', 'defaultDateFormat', |
| 158 |
'specialPageAliases', 'imageFiles', 'preloadedMessages', |
| 159 |
'namespaceGenderAliases', 'digitGroupingPattern', 'pluralRules', |
| 160 |
'pluralRuleTypes', 'compiledPluralRules', 'formalityIndex' |
| 161 |
]; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
private const CORE_ONLY_KEYS = [ |
| 171 |
'fallback', 'rtl', 'digitTransformTable', 'separatorTransformTable', |
| 172 |
'minimumGroupingDigits', 'numberingSystem', |
| 173 |
'fallback8bitEncoding', 'linkPrefixExtension', |
| 174 |
'linkTrail', 'linkPrefixCharset', 'datePreferences', |
| 175 |
'datePreferenceMigrationMap', 'defaultDateFormat', 'digitGroupingPattern', |
| 176 |
'formalityIndex', |
| 177 |
]; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
private const ALL_EXCEPT_CORE_ONLY_KEYS = [ |
| 188 |
'namespaceNames', 'bookstoreList', 'magicWords', 'messages', |
| 189 |
'namespaceAliases', 'dateFormats', 'jsDateFormats', 'specialPageAliases', |
| 190 |
'imageFiles', 'preloadedMessages', 'namespaceGenderAliases', |
| 191 |
'pluralRules', 'pluralRuleTypes', 'compiledPluralRules', |
| 192 |
]; |
| 193 |
|
| 194 |
|
| 195 |
public const ALL_ALIAS_KEYS = [ 'specialPageAliases' ]; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
private const MERGEABLE_MAP_KEYS = [ 'messages', 'namespaceNames', |
| 202 |
'namespaceAliases', 'dateFormats', 'jsDateFormats', 'imageFiles', 'preloadedMessages' |
| 203 |
]; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
private const MERGEABLE_ALIAS_LIST_KEYS = [ 'specialPageAliases' ]; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
private const OPTIONAL_MERGE_KEYS = [ 'bookstoreList' ]; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
private const MAGIC_WORD_KEYS = [ 'magicWords' ]; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
private const SPLIT_KEYS = [ 'messages' ]; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private const SOURCE_PREFIX_KEYS = [ 'messages' ]; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
private const SOURCEPREFIX_SEPARATOR = ':'; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
private const PRELOADED_KEYS = [ 'dateFormats', 'namespaceNames' ]; |
| 243 |
|
| 244 |
private const PLURAL_FILES = [ |
| 245 |
|
| 246 |
MW_INSTALL_PATH . '/languages/data/plurals.xml', |
| 247 |
|
| 248 |
MW_INSTALL_PATH . '/languages/data/plurals-mediawiki.xml', |
| 249 |
]; |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private static $pluralRules = null; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
private static $pluralRuleTypes = null; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public static function getStoreFromConf( array $conf, $fallbackCacheDir ): LCStore { |
| 284 |
$storeArg = []; |
| 285 |
$storeArg['directory'] = |
| 286 |
$conf['storeDirectory'] ?: $fallbackCacheDir; |
| 287 |
|
| 288 |
if ( !empty( $conf['storeClass'] ) ) { |
| 289 |
$storeClass = $conf['storeClass']; |
| 290 |
} elseif ( $conf['store'] === 'files' || $conf['store'] === 'file' || |
| 291 |
( $conf['store'] === 'detect' && $storeArg['directory'] ) |
| 292 |
) { |
| 293 |
$storeClass = LCStoreCDB::class; |
| 294 |
} elseif ( $conf['store'] === 'db' || $conf['store'] === 'detect' ) { |
| 295 |
$storeClass = LCStoreDB::class; |
| 296 |
$storeArg['server'] = $conf['storeServer'] ?? []; |
| 297 |
} elseif ( $conf['store'] === 'array' ) { |
| 298 |
$storeClass = LCStoreStaticArray::class; |
| 299 |
} else { |
| 300 |
throw new ConfigException( |
| 301 |
'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.' |
| 302 |
); |
| 303 |
} |
| 304 |
|
| 305 |
return new $storeClass( $storeArg ); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public const CONSTRUCTOR_OPTIONS = [ |
| 312 |
|
| 313 |
'forceRecache', |
| 314 |
'manualRecache', |
| 315 |
MainConfigNames::ExtensionMessagesFiles, |
| 316 |
MainConfigNames::MessagesDirs, |
| 317 |
MainConfigNames::TranslationAliasesDirs, |
| 318 |
]; |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public function __construct( |
| 334 |
ServiceOptions $options, |
| 335 |
LCStore $store, |
| 336 |
LoggerInterface $logger, |
| 337 |
array $clearStoreCallbacks, |
| 338 |
LanguageNameUtils $langNameUtils, |
| 339 |
HookContainer $hookContainer |
| 340 |
) { |
| 341 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 342 |
|
| 343 |
$this->options = $options; |
| 344 |
$this->store = $store; |
| 345 |
$this->logger = $logger; |
| 346 |
$this->clearStoreCallbacks = $clearStoreCallbacks; |
| 347 |
$this->langNameUtils = $langNameUtils; |
| 348 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 349 |
|
| 350 |
|
| 351 |
$this->manualRecache = $options->get( 'manualRecache' ); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
private static function isMergeableKey( string $key ): bool { |
| 361 |
static $mergeableKeys; |
| 362 |
$mergeableKeys ??= array_fill_keys( [ |
| 363 |
...self::MERGEABLE_MAP_KEYS, |
| 364 |
...self::MERGEABLE_ALIAS_LIST_KEYS, |
| 365 |
...self::OPTIONAL_MERGE_KEYS, |
| 366 |
...self::MAGIC_WORD_KEYS, |
| 367 |
], true ); |
| 368 |
return isset( $mergeableKeys[$key] ); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
public function getItem( $code, $key ) { |
| 381 |
if ( !isset( $this->loadedItems[$code][$key] ) ) { |
| 382 |
$this->loadItem( $code, $key ); |
| 383 |
} |
| 384 |
|
| 385 |
if ( $key === 'fallback' && isset( $this->shallowFallbacks[$code] ) ) { |
| 386 |
return $this->shallowFallbacks[$code]; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
return $this->data[$code][$key]; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public function getSubitem( $code, $key, $subkey ) { |
| 401 |
if ( !isset( $this->loadedSubitems[$code][$key][$subkey] ) && |
| 402 |
!isset( $this->loadedItems[$code][$key] ) |
| 403 |
) { |
| 404 |
$this->loadSubitem( $code, $key, $subkey ); |
| 405 |
} |
| 406 |
|
| 407 |
return $this->data[$code][$key][$subkey] ?? null; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
public function getSubitemWithSource( $code, $key, $subkey ) { |
| 420 |
$subitem = $this->getSubitem( $code, $key, $subkey ); |
| 421 |
|
| 422 |
if ( $subitem === null ) { |
| 423 |
return null; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
return [ $subitem, $this->sourceLanguage[$code][$key][$subkey] ?? $code ]; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public function getSubitemList( $code, $key ) { |
| 444 |
if ( in_array( $key, self::SPLIT_KEYS ) ) { |
| 445 |
return $this->getSubitem( $code, 'list', $key ); |
| 446 |
} else { |
| 447 |
$item = $this->getItem( $code, $key ); |
| 448 |
if ( is_array( $item ) ) { |
| 449 |
return array_keys( $item ); |
| 450 |
} else { |
| 451 |
return false; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
private function loadItem( $code, $key ) { |
| 463 |
if ( isset( $this->loadedItems[$code][$key] ) ) { |
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
if ( |
| 468 |
in_array( $key, self::CORE_ONLY_KEYS, true ) || |
| 469 |
|
| 470 |
$key === 'fallbackSequence' || |
| 471 |
$key === 'originalFallbackSequence' |
| 472 |
) { |
| 473 |
if ( $this->langNameUtils->isValidBuiltInCode( $code ) ) { |
| 474 |
$this->loadCoreData( $code ); |
| 475 |
return; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
if ( !isset( $this->initialisedLangs[$code] ) ) { |
| 480 |
$this->initLanguage( $code ); |
| 481 |
|
| 482 |
|
| 483 |
if ( isset( $this->loadedItems[$code][$key] ) ) { |
| 484 |
return; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
if ( isset( $this->shallowFallbacks[$code] ) ) { |
| 489 |
$this->loadItem( $this->shallowFallbacks[$code], $key ); |
| 490 |
|
| 491 |
return; |
| 492 |
} |
| 493 |
|
| 494 |
if ( in_array( $key, self::SPLIT_KEYS ) ) { |
| 495 |
$subkeyList = $this->getSubitem( $code, 'list', $key ); |
| 496 |
foreach ( $subkeyList as $subkey ) { |
| 497 |
if ( isset( $this->data[$code][$key][$subkey] ) ) { |
| 498 |
continue; |
| 499 |
} |
| 500 |
$this->loadSubitem( $code, $key, $subkey ); |
| 501 |
} |
| 502 |
} else { |
| 503 |
$this->data[$code][$key] = $this->store->get( $code, $key ); |
| 504 |
} |
| 505 |
|
| 506 |
$this->loadedItems[$code][$key] = true; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
private function loadSubitem( $code, $key, $subkey ) { |
| 517 |
if ( !in_array( $key, self::SPLIT_KEYS ) ) { |
| 518 |
$this->loadItem( $code, $key ); |
| 519 |
|
| 520 |
return; |
| 521 |
} |
| 522 |
|
| 523 |
if ( !isset( $this->initialisedLangs[$code] ) ) { |
| 524 |
$this->initLanguage( $code ); |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
if ( isset( $this->loadedItems[$code][$key] ) || |
| 529 |
isset( $this->loadedSubitems[$code][$key][$subkey] ) |
| 530 |
) { |
| 531 |
return; |
| 532 |
} |
| 533 |
|
| 534 |
if ( isset( $this->shallowFallbacks[$code] ) ) { |
| 535 |
$this->loadSubitem( $this->shallowFallbacks[$code], $key, $subkey ); |
| 536 |
|
| 537 |
return; |
| 538 |
} |
| 539 |
|
| 540 |
$value = $this->store->get( $code, "$key:$subkey" ); |
| 541 |
if ( $value !== null && in_array( $key, self::SOURCE_PREFIX_KEYS ) ) { |
| 542 |
[ |
| 543 |
$this->sourceLanguage[$code][$key][$subkey], |
| 544 |
$this->data[$code][$key][$subkey] |
| 545 |
] = explode( self::SOURCEPREFIX_SEPARATOR, $value, 2 ); |
| 546 |
} else { |
| 547 |
$this->data[$code][$key][$subkey] = $value; |
| 548 |
} |
| 549 |
|
| 550 |
$this->loadedSubitems[$code][$key][$subkey] = true; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
public function isExpired( $code ) { |
| 561 |
if ( $this->options->get( 'forceRecache' ) && !isset( $this->recachedLangs[$code] ) ) { |
| 562 |
$this->logger->debug( __METHOD__ . "($code): forced reload" ); |
| 563 |
|
| 564 |
return true; |
| 565 |
} |
| 566 |
|
| 567 |
$deps = $this->store->get( $code, 'deps' ); |
| 568 |
$keys = $this->store->get( $code, 'list' ); |
| 569 |
$preload = $this->store->get( $code, 'preload' ); |
| 570 |
|
| 571 |
if ( $deps === null || $keys === null || $preload === null ) { |
| 572 |
$this->logger->debug( __METHOD__ . "($code): cache missing, need to make one" ); |
| 573 |
|
| 574 |
return true; |
| 575 |
} |
| 576 |
|
| 577 |
foreach ( $deps as $dep ) { |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
if ( !$dep instanceof CacheDependency || $dep->isExpired() ) { |
| 583 |
$this->logger->debug( __METHOD__ . "($code): cache for $code expired due to " . |
| 584 |
get_class( $dep ) ); |
| 585 |
|
| 586 |
return true; |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
return false; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
private function initLanguage( $code ) { |
| 599 |
if ( isset( $this->initialisedLangs[$code] ) ) { |
| 600 |
return; |
| 601 |
} |
| 602 |
|
| 603 |
$this->initialisedLangs[$code] = true; |
| 604 |
|
| 605 |
|
| 606 |
if ( !$this->langNameUtils->isValidBuiltInCode( $code ) ) { |
| 607 |
$this->initShallowFallback( $code, 'en' ); |
| 608 |
|
| 609 |
return; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
if ( !$this->manualRecache && $this->isExpired( $code ) ) { |
| 614 |
if ( $this->langNameUtils->isSupportedLanguage( $code ) ) { |
| 615 |
$this->recache( $code ); |
| 616 |
} elseif ( $code === 'en' ) { |
| 617 |
throw new RuntimeException( 'MessagesEn.php is missing.' ); |
| 618 |
} else { |
| 619 |
$this->initShallowFallback( $code, 'en' ); |
| 620 |
} |
| 621 |
|
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
$preload = $this->getItem( $code, 'preload' ); |
| 627 |
if ( $preload === null ) { |
| 628 |
if ( $this->manualRecache ) { |
| 629 |
|
| 630 |
if ( $code === 'en' ) { |
| 631 |
throw new RuntimeException( 'No localisation cache found for English. ' . |
| 632 |
'Please run maintenance/rebuildLocalisationCache.php.' ); |
| 633 |
} |
| 634 |
$this->initShallowFallback( $code, 'en' ); |
| 635 |
|
| 636 |
return; |
| 637 |
} else { |
| 638 |
throw new RuntimeException( 'Invalid or missing localisation cache.' ); |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
foreach ( self::SOURCE_PREFIX_KEYS as $key ) { |
| 643 |
if ( !isset( $preload[$key] ) ) { |
| 644 |
continue; |
| 645 |
} |
| 646 |
foreach ( $preload[$key] as $subkey => $value ) { |
| 647 |
if ( $value !== null ) { |
| 648 |
[ |
| 649 |
$this->sourceLanguage[$code][$key][$subkey], |
| 650 |
$preload[$key][$subkey] |
| 651 |
] = explode( self::SOURCEPREFIX_SEPARATOR, $value, 2 ); |
| 652 |
} else { |
| 653 |
$preload[$key][$subkey] = null; |
| 654 |
} |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
if ( isset( $this->data[$code] ) ) { |
| 659 |
foreach ( $preload as $key => $value ) { |
| 660 |
|
| 661 |
$this->mergeItem( $key, $this->data[$code][$key], $value ); |
| 662 |
} |
| 663 |
} else { |
| 664 |
$this->data[$code] = $preload; |
| 665 |
} |
| 666 |
foreach ( $preload as $key => $item ) { |
| 667 |
if ( in_array( $key, self::SPLIT_KEYS ) ) { |
| 668 |
foreach ( $item as $subkey => $subitem ) { |
| 669 |
$this->loadedSubitems[$code][$key][$subkey] = true; |
| 670 |
} |
| 671 |
} else { |
| 672 |
$this->loadedItems[$code][$key] = true; |
| 673 |
} |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
private function initShallowFallback( $primaryCode, $fallbackCode ) { |
| 685 |
$this->data[$primaryCode] =& $this->data[$fallbackCode]; |
| 686 |
$this->loadedItems[$primaryCode] =& $this->loadedItems[$fallbackCode]; |
| 687 |
$this->loadedSubitems[$primaryCode] =& $this->loadedSubitems[$fallbackCode]; |
| 688 |
$this->shallowFallbacks[$primaryCode] = $fallbackCode; |
| 689 |
$this->coreDataLoaded[$primaryCode] =& $this->coreDataLoaded[$fallbackCode]; |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
protected function readPHPFile( $_fileName, $_fileType ) { |
| 700 |
include $_fileName; |
| 701 |
|
| 702 |
$data = []; |
| 703 |
if ( $_fileType == 'core' ) { |
| 704 |
foreach ( self::ALL_KEYS as $key ) { |
| 705 |
|
| 706 |
|
| 707 |
if ( isset( $$key ) ) { |
| 708 |
$data[$key] = $$key; |
| 709 |
} |
| 710 |
} |
| 711 |
} elseif ( $_fileType == 'extension' ) { |
| 712 |
foreach ( self::ALL_EXCEPT_CORE_ONLY_KEYS as $key ) { |
| 713 |
if ( isset( $$key ) ) { |
| 714 |
$data[$key] = $$key; |
| 715 |
} |
| 716 |
} |
| 717 |
} elseif ( $_fileType == 'aliases' ) { |
| 718 |
|
| 719 |
if ( isset( $aliases ) ) { |
| 720 |
$data['aliases'] = $aliases; |
| 721 |
} |
| 722 |
} else { |
| 723 |
throw new InvalidArgumentException( __METHOD__ . ": Invalid file type: $_fileType" ); |
| 724 |
} |
| 725 |
|
| 726 |
return $data; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
private function readJSONFile( $fileName ) { |
| 736 |
if ( !is_readable( $fileName ) ) { |
| 737 |
return []; |
| 738 |
} |
| 739 |
|
| 740 |
$json = file_get_contents( $fileName ); |
| 741 |
if ( $json === false ) { |
| 742 |
return []; |
| 743 |
} |
| 744 |
|
| 745 |
$data = FormatJson::decode( $json, true ); |
| 746 |
if ( $data === null ) { |
| 747 |
throw new RuntimeException( __METHOD__ . ": Invalid JSON file: $fileName" ); |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
foreach ( $data as $key => $unused ) { |
| 752 |
if ( $key === '' || $key[0] === '@' ) { |
| 753 |
unset( $data[$key] ); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
return $data; |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
private function getCompiledPluralRules( $code ) { |
| 768 |
$rules = $this->getPluralRules( $code ); |
| 769 |
if ( $rules === null ) { |
| 770 |
return null; |
| 771 |
} |
| 772 |
try { |
| 773 |
$compiledRules = Evaluator::compile( $rules ); |
| 774 |
} catch ( CLDRPluralRuleError $e ) { |
| 775 |
$this->logger->debug( $e->getMessage() ); |
| 776 |
|
| 777 |
return []; |
| 778 |
} |
| 779 |
|
| 780 |
return $compiledRules; |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
private function getPluralRules( $code ) { |
| 793 |
if ( self::$pluralRules === null ) { |
| 794 |
self::loadPluralFiles(); |
| 795 |
} |
| 796 |
return self::$pluralRules[$code] ?? null; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
private function getPluralRuleTypes( $code ) { |
| 809 |
if ( self::$pluralRuleTypes === null ) { |
| 810 |
self::loadPluralFiles(); |
| 811 |
} |
| 812 |
return self::$pluralRuleTypes[$code] ?? null; |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
private static function loadPluralFiles() { |
| 819 |
foreach ( self::PLURAL_FILES as $fileName ) { |
| 820 |
self::loadPluralFile( $fileName ); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
private static function loadPluralFile( $fileName ) { |
| 831 |
|
| 832 |
$xml = file_get_contents( $fileName ); |
| 833 |
if ( !$xml ) { |
| 834 |
throw new RuntimeException( "Unable to read plurals file $fileName" ); |
| 835 |
} |
| 836 |
$doc = new DOMDocument; |
| 837 |
$doc->loadXML( $xml ); |
| 838 |
$rulesets = $doc->getElementsByTagName( "pluralRules" ); |
| 839 |
foreach ( $rulesets as $ruleset ) { |
| 840 |
$codes = $ruleset->getAttribute( 'locales' ); |
| 841 |
$rules = []; |
| 842 |
$ruleTypes = []; |
| 843 |
$ruleElements = $ruleset->getElementsByTagName( "pluralRule" ); |
| 844 |
foreach ( $ruleElements as $elt ) { |
| 845 |
$ruleType = $elt->getAttribute( 'count' ); |
| 846 |
if ( $ruleType === 'other' ) { |
| 847 |
|
| 848 |
continue; |
| 849 |
} |
| 850 |
$rules[] = $elt->nodeValue; |
| 851 |
$ruleTypes[] = $ruleType; |
| 852 |
} |
| 853 |
foreach ( explode( ' ', $codes ) as $code ) { |
| 854 |
self::$pluralRules[$code] = $rules; |
| 855 |
self::$pluralRuleTypes[$code] = $ruleTypes; |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
private function readSourceFilesAndRegisterDeps( $code, &$deps ) { |
| 869 |
|
| 870 |
$fileName = $this->langNameUtils->getMessagesFileName( $code ); |
| 871 |
if ( !is_file( $fileName ) ) { |
| 872 |
$data = []; |
| 873 |
} else { |
| 874 |
$deps[] = new FileDependency( $fileName ); |
| 875 |
$data = $this->readPHPFile( $fileName, 'core' ); |
| 876 |
} |
| 877 |
|
| 878 |
return $data; |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
private function readPluralFilesAndRegisterDeps( $code, &$deps ) { |
| 890 |
$data = [ |
| 891 |
|
| 892 |
'pluralRules' => $this->getPluralRules( $code ), |
| 893 |
|
| 894 |
'compiledPluralRules' => $this->getCompiledPluralRules( $code ), |
| 895 |
|
| 896 |
'pluralRuleTypes' => $this->getPluralRuleTypes( $code ), |
| 897 |
]; |
| 898 |
|
| 899 |
foreach ( self::PLURAL_FILES as $fileName ) { |
| 900 |
$deps[] = new FileDependency( $fileName ); |
| 901 |
} |
| 902 |
|
| 903 |
return $data; |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
private function mergeItem( $key, &$value, $fallbackValue ) { |
| 915 |
if ( $value !== null ) { |
| 916 |
if ( $fallbackValue !== null ) { |
| 917 |
if ( in_array( $key, self::MERGEABLE_MAP_KEYS ) ) { |
| 918 |
$value += $fallbackValue; |
| 919 |
} elseif ( in_array( $key, self::MERGEABLE_ALIAS_LIST_KEYS ) ) { |
| 920 |
$value = array_merge_recursive( $value, $fallbackValue ); |
| 921 |
} elseif ( in_array( $key, self::OPTIONAL_MERGE_KEYS ) ) { |
| 922 |
if ( !empty( $value['inherit'] ) ) { |
| 923 |
$value = array_merge( $fallbackValue, $value ); |
| 924 |
} |
| 925 |
|
| 926 |
unset( $value['inherit'] ); |
| 927 |
} elseif ( in_array( $key, self::MAGIC_WORD_KEYS ) ) { |
| 928 |
$this->mergeMagicWords( $value, $fallbackValue ); |
| 929 |
} |
| 930 |
} |
| 931 |
} else { |
| 932 |
$value = $fallbackValue; |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
private function mergeMagicWords( array &$value, array $fallbackValue ): void { |
| 941 |
foreach ( $fallbackValue as $magicName => $fallbackInfo ) { |
| 942 |
if ( !isset( $value[$magicName] ) ) { |
| 943 |
$value[$magicName] = $fallbackInfo; |
| 944 |
} else { |
| 945 |
$value[$magicName] = [ |
| 946 |
$fallbackInfo[0], |
| 947 |
...array_unique( [ |
| 948 |
|
| 949 |
...array_slice( $value[$magicName], 1 ), |
| 950 |
...array_slice( $fallbackInfo, 1 ), |
| 951 |
] ) |
| 952 |
]; |
| 953 |
} |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
public function getMessagesDirs() { |
| 965 |
global $IP; |
| 966 |
|
| 967 |
return [ |
| 968 |
'core' => "$IP/languages/i18n", |
| 969 |
'codex' => "$IP/languages/i18n/codex", |
| 970 |
'exif' => "$IP/languages/i18n/exif", |
| 971 |
'preferences' => "$IP/languages/i18n/preferences", |
| 972 |
'api' => "$IP/includes/api/i18n", |
| 973 |
'rest' => "$IP/includes/Rest/i18n", |
| 974 |
'oojs-ui' => "$IP/resources/lib/ooui/i18n", |
| 975 |
'paramvalidator' => "$IP/includes/libs/ParamValidator/i18n", |
| 976 |
] + $this->options->get( MainConfigNames::MessagesDirs ); |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
private function loadCoreData( string $code ) { |
| 990 |
if ( !$code ) { |
| 991 |
throw new InvalidArgumentException( "Invalid language code requested" ); |
| 992 |
} |
| 993 |
if ( $this->coreDataLoaded[$code] ?? false ) { |
| 994 |
return; |
| 995 |
} |
| 996 |
|
| 997 |
$coreData = array_fill_keys( self::CORE_ONLY_KEYS, null ); |
| 998 |
$deps = []; |
| 999 |
|
| 1000 |
|
| 1001 |
$data = $this->readSourceFilesAndRegisterDeps( $code, $deps ); |
| 1002 |
$this->logger->debug( __METHOD__ . ": got localisation for $code from source" ); |
| 1003 |
|
| 1004 |
|
| 1005 |
foreach ( $data as $key => $value ) { |
| 1006 |
$this->mergeItem( $key, $coreData[ $key ], $value ); |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
if ( ( $coreData['fallback'] === null || $coreData['fallback'] === false ) && $code === 'en' ) { |
| 1012 |
$coreData['fallback'] = false; |
| 1013 |
$coreData['originalFallbackSequence'] = $coreData['fallbackSequence'] = []; |
| 1014 |
} else { |
| 1015 |
if ( $coreData['fallback'] !== null ) { |
| 1016 |
$coreData['fallbackSequence'] = array_map( 'trim', explode( ',', $coreData['fallback'] ) ); |
| 1017 |
} else { |
| 1018 |
$coreData['fallbackSequence'] = []; |
| 1019 |
} |
| 1020 |
$len = count( $coreData['fallbackSequence'] ); |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
$coreData['originalFallbackSequence'] = $coreData['fallbackSequence']; |
| 1025 |
|
| 1026 |
|
| 1027 |
if ( !$len || $coreData['fallbackSequence'][$len - 1] !== 'en' ) { |
| 1028 |
$coreData['fallbackSequence'][] = 'en'; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
foreach ( $coreData['fallbackSequence'] as $fbCode ) { |
| 1033 |
|
| 1034 |
$fbData = $this->readSourceFilesAndRegisterDeps( $fbCode, $deps ); |
| 1035 |
foreach ( self::CORE_ONLY_KEYS as $key ) { |
| 1036 |
|
| 1037 |
if ( isset( $fbData[$key] ) && !isset( $coreData[$key] ) ) { |
| 1038 |
$coreData[$key] = $fbData[$key]; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
$coreData['deps'] = $deps; |
| 1044 |
foreach ( $coreData as $key => $item ) { |
| 1045 |
$this->data[$code][$key] ??= null; |
| 1046 |
|
| 1047 |
$this->mergeItem( $key, $this->data[$code][$key], $item ); |
| 1048 |
if ( |
| 1049 |
in_array( $key, self::CORE_ONLY_KEYS, true ) || |
| 1050 |
|
| 1051 |
$key === 'fallbackSequence' || |
| 1052 |
$key === 'originalFallbackSequence' |
| 1053 |
) { |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
$this->loadedItems[$code][$key] = true; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
$this->coreDataLoaded[$code] = true; |
| 1063 |
} |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
public function recache( $code ) { |
| 1072 |
if ( !$code ) { |
| 1073 |
throw new InvalidArgumentException( "Invalid language code requested" ); |
| 1074 |
} |
| 1075 |
$this->recachedLangs[ $code ] = true; |
| 1076 |
|
| 1077 |
|
| 1078 |
$initialData = array_fill_keys( self::ALL_KEYS, null ); |
| 1079 |
$this->data[$code] = []; |
| 1080 |
$this->loadedItems[$code] = []; |
| 1081 |
$this->loadedSubitems[$code] = []; |
| 1082 |
$this->coreDataLoaded[$code] = false; |
| 1083 |
$this->loadCoreData( $code ); |
| 1084 |
$coreData = $this->data[$code]; |
| 1085 |
|
| 1086 |
$deps = $coreData['deps']; |
| 1087 |
$coreData += $this->readPluralFilesAndRegisterDeps( $code, $deps ); |
| 1088 |
|
| 1089 |
$codeSequence = array_merge( [ $code ], $coreData['fallbackSequence'] ); |
| 1090 |
$messageDirs = $this->getMessagesDirs(); |
| 1091 |
$translationAliasesDirs = $this->options->get( MainConfigNames::TranslationAliasesDirs ); |
| 1092 |
|
| 1093 |
|
| 1094 |
$extensionData = array_fill_keys( $codeSequence, $initialData ); |
| 1095 |
foreach ( $this->options->get( MainConfigNames::ExtensionMessagesFiles ) as $extension => $fileName ) { |
| 1096 |
if ( isset( $messageDirs[$extension] ) || isset( $translationAliasesDirs[$extension] ) ) { |
| 1097 |
|
| 1098 |
continue; |
| 1099 |
} |
| 1100 |
|
| 1101 |
$data = $this->readPHPFile( $fileName, 'extension' ); |
| 1102 |
$used = false; |
| 1103 |
|
| 1104 |
foreach ( $data as $key => $item ) { |
| 1105 |
foreach ( $codeSequence as $csCode ) { |
| 1106 |
if ( isset( $item[$csCode] ) ) { |
| 1107 |
|
| 1108 |
|
| 1109 |
if ( in_array( $key, self::SOURCE_PREFIX_KEYS ) ) { |
| 1110 |
foreach ( $item[$csCode] as $subkey => $_ ) { |
| 1111 |
$this->sourceLanguage[$code][$key][$subkey] ??= $csCode; |
| 1112 |
} |
| 1113 |
} |
| 1114 |
$this->mergeItem( $key, $extensionData[$csCode][$key], $item[$csCode] ); |
| 1115 |
$used = true; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
if ( $used ) { |
| 1121 |
$deps[] = new FileDependency( $fileName ); |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
$allData = $initialData; |
| 1127 |
foreach ( $codeSequence as $csCode ) { |
| 1128 |
$csData = $initialData; |
| 1129 |
|
| 1130 |
|
| 1131 |
foreach ( $messageDirs as $dirs ) { |
| 1132 |
foreach ( (array)$dirs as $dir ) { |
| 1133 |
$fileName = "$dir/$csCode.json"; |
| 1134 |
$messages = $this->readJSONFile( $fileName ); |
| 1135 |
|
| 1136 |
foreach ( $messages as $subkey => $_ ) { |
| 1137 |
$this->sourceLanguage[$code]['messages'][$subkey] ??= $csCode; |
| 1138 |
} |
| 1139 |
$this->mergeItem( 'messages', $csData['messages'], $messages ); |
| 1140 |
|
| 1141 |
$deps[] = new FileDependency( $fileName ); |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
foreach ( $translationAliasesDirs as $dirs ) { |
| 1146 |
foreach ( (array)$dirs as $dir ) { |
| 1147 |
$fileName = "$dir/$csCode.json"; |
| 1148 |
$data = $this->readJSONFile( $fileName ); |
| 1149 |
|
| 1150 |
foreach ( $data as $key => $item ) { |
| 1151 |
|
| 1152 |
|
| 1153 |
$normalizedKey = lcfirst( $key ); |
| 1154 |
|
| 1155 |
if ( $normalizedKey === '@metadata' ) { |
| 1156 |
|
| 1157 |
continue; |
| 1158 |
} |
| 1159 |
|
| 1160 |
if ( !in_array( $normalizedKey, self::ALL_ALIAS_KEYS ) ) { |
| 1161 |
throw new UnexpectedValueException( |
| 1162 |
"Invalid key: \"$key\" for " . MainConfigNames::TranslationAliasesDirs . ". " . |
| 1163 |
'Valid keys: ' . implode( ', ', self::ALL_ALIAS_KEYS ) |
| 1164 |
); |
| 1165 |
} |
| 1166 |
|
| 1167 |
$this->mergeItem( $normalizedKey, $extensionData[$csCode][$normalizedKey], $item ); |
| 1168 |
} |
| 1169 |
|
| 1170 |
$deps[] = new FileDependency( $fileName ); |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
if ( isset( $extensionData[$csCode] ) ) { |
| 1176 |
foreach ( $extensionData[$csCode] as $key => $item ) { |
| 1177 |
$this->mergeItem( $key, $csData[$key], $item ); |
| 1178 |
} |
| 1179 |
} |
| 1180 |
|
| 1181 |
if ( $csCode === $code ) { |
| 1182 |
|
| 1183 |
foreach ( $coreData as $key => $item ) { |
| 1184 |
$this->mergeItem( $key, $csData[$key], $item ); |
| 1185 |
} |
| 1186 |
} else { |
| 1187 |
|
| 1188 |
|
| 1189 |
$fbData = $this->readSourceFilesAndRegisterDeps( $csCode, $deps ); |
| 1190 |
$fbData += $this->readPluralFilesAndRegisterDeps( $csCode, $deps ); |
| 1191 |
|
| 1192 |
foreach ( self::ALL_KEYS as $key ) { |
| 1193 |
if ( !isset( $fbData[ $key ] ) ) { |
| 1194 |
continue; |
| 1195 |
} |
| 1196 |
|
| 1197 |
if ( !isset( $coreData[ $key ] ) || self::isMergeableKey( $key ) ) { |
| 1198 |
$this->mergeItem( $key, $csData[ $key ], $fbData[ $key ] ); |
| 1199 |
} |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
|
| 1204 |
$this->hookRunner->onLocalisationCacheRecacheFallback( $this, $csCode, $csData ); |
| 1205 |
|
| 1206 |
|
| 1207 |
if ( $csCode === $code ) { |
| 1208 |
$allData = $csData; |
| 1209 |
} else { |
| 1210 |
foreach ( self::ALL_KEYS as $key ) { |
| 1211 |
if ( !isset( $csData[$key] ) ) { |
| 1212 |
continue; |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
if ( $allData[$key] === null || self::isMergeableKey( $key ) ) { |
| 1217 |
$this->mergeItem( $key, $allData[$key], $csData[$key] ); |
| 1218 |
} |
| 1219 |
} |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
if ( !isset( $allData['rtl'] ) ) { |
| 1224 |
throw new RuntimeException( __METHOD__ . ': Localisation data failed validation check! ' . |
| 1225 |
'Check that your languages/messages/MessagesEn.php file is intact.' ); |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
$deps['wgExtensionMessagesFiles'] = |
| 1231 |
new MainConfigDependency( MainConfigNames::ExtensionMessagesFiles ); |
| 1232 |
$deps['wgMessagesDirs'] = |
| 1233 |
new MainConfigDependency( MainConfigNames::MessagesDirs ); |
| 1234 |
$deps['version'] = new ConstantDependency( self::class . '::VERSION' ); |
| 1235 |
|
| 1236 |
|
| 1237 |
$allData['deps'] = $deps; |
| 1238 |
|
| 1239 |
|
| 1240 |
$allData['namespaceNames'] = str_replace( ' ', '_', $allData['namespaceNames'] ); |
| 1241 |
|
| 1242 |
|
| 1243 |
foreach ( $allData['specialPageAliases'] as &$page ) { |
| 1244 |
$page = str_replace( ' ', '_', $page ); |
| 1245 |
} |
| 1246 |
|
| 1247 |
unset( $page ); |
| 1248 |
|
| 1249 |
|
| 1250 |
$allData['pluralRules'] ??= []; |
| 1251 |
$allData['compiledPluralRules'] ??= []; |
| 1252 |
|
| 1253 |
$allData['pluralRuleTypes'] ??= []; |
| 1254 |
|
| 1255 |
|
| 1256 |
$allData['list'] = []; |
| 1257 |
foreach ( self::SPLIT_KEYS as $key ) { |
| 1258 |
$allData['list'][$key] = array_keys( $allData[$key] ); |
| 1259 |
} |
| 1260 |
|
| 1261 |
$unused = true; |
| 1262 |
$this->hookRunner->onLocalisationCacheRecache( $this, $code, $allData, $unused ); |
| 1263 |
|
| 1264 |
|
| 1265 |
$this->data[$code] = $allData; |
| 1266 |
$this->loadedItems[$code] = []; |
| 1267 |
$this->loadedSubitems[$code] = []; |
| 1268 |
foreach ( $allData as $key => $item ) { |
| 1269 |
$this->loadedItems[$code][$key] = true; |
| 1270 |
} |
| 1271 |
|
| 1272 |
|
| 1273 |
foreach ( self::SOURCE_PREFIX_KEYS as $key ) { |
| 1274 |
|
| 1275 |
foreach ( $allData[$key] as $subKey => $value ) { |
| 1276 |
|
| 1277 |
$allData[$key][$subKey] = ( $this->sourceLanguage[$code][$key][$subKey] ?? $code ) . |
| 1278 |
self::SOURCEPREFIX_SEPARATOR . $value; |
| 1279 |
} |
| 1280 |
} |
| 1281 |
|
| 1282 |
|
| 1283 |
$allData['preload'] = $this->buildPreload( $allData ); |
| 1284 |
|
| 1285 |
|
| 1286 |
$this->store->startWrite( $code ); |
| 1287 |
foreach ( $allData as $key => $value ) { |
| 1288 |
if ( in_array( $key, self::SPLIT_KEYS ) ) { |
| 1289 |
foreach ( $value as $subkey => $subvalue ) { |
| 1290 |
$this->store->set( "$key:$subkey", $subvalue ); |
| 1291 |
} |
| 1292 |
} else { |
| 1293 |
$this->store->set( $key, $value ); |
| 1294 |
} |
| 1295 |
} |
| 1296 |
$this->store->finishWrite(); |
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
if ( !$this->store instanceof LCStoreNull ) { |
| 1302 |
foreach ( $this->clearStoreCallbacks as $callback ) { |
| 1303 |
$callback(); |
| 1304 |
} |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
private function buildPreload( $data ) { |
| 1318 |
$preload = [ 'messages' => [] ]; |
| 1319 |
foreach ( self::PRELOADED_KEYS as $key ) { |
| 1320 |
$preload[$key] = $data[$key]; |
| 1321 |
} |
| 1322 |
|
| 1323 |
foreach ( $data['preloadedMessages'] as $subkey ) { |
| 1324 |
$subitem = $data['messages'][$subkey] ?? null; |
| 1325 |
$preload['messages'][$subkey] = $subitem; |
| 1326 |
} |
| 1327 |
|
| 1328 |
return $preload; |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
public function unload( $code ) { |
| 1339 |
unset( $this->data[$code] ); |
| 1340 |
unset( $this->loadedItems[$code] ); |
| 1341 |
unset( $this->loadedSubitems[$code] ); |
| 1342 |
unset( $this->initialisedLangs[$code] ); |
| 1343 |
unset( $this->shallowFallbacks[$code] ); |
| 1344 |
unset( $this->sourceLanguage[$code] ); |
| 1345 |
unset( $this->coreDataLoaded[$code] ); |
| 1346 |
|
| 1347 |
foreach ( $this->shallowFallbacks as $shallowCode => $fbCode ) { |
| 1348 |
if ( $fbCode === $code ) { |
| 1349 |
$this->unload( $shallowCode ); |
| 1350 |
} |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
public function unloadAll() { |
| 1358 |
foreach ( $this->initialisedLangs as $lang => $unused ) { |
| 1359 |
$this->unload( $lang ); |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
public function disableBackend() { |
| 1367 |
$this->store = new LCStoreNull; |
| 1368 |
$this->manualRecache = false; |
| 1369 |
} |
| 1370 |
} |