Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.54% |
62 / 67 |
|
91.67% |
11 / 12 |
CRAP | |
0.00% |
0 / 1 |
| MessageBlobStore | |
92.54% |
62 / 67 |
|
91.67% |
11 / 12 |
22.20 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| setLogger | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getBlob | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getBlobs | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
6 | |||
| makeGlobalPurgeKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| makeModulePurgeKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| makeBlobCacheKey | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| recacheMessageBlob | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| updateMessage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| clearGlobalCacheEntry | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fetchMessage | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| generateMessageBlob | |
61.54% |
8 / 13 |
|
0.00% |
0 / 1 |
4.91 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | * @author Roan Kattouw |
| 6 | * @author Trevor Parscal |
| 7 | */ |
| 8 | |
| 9 | namespace MediaWiki\ResourceLoader; |
| 10 | |
| 11 | use MediaWiki\Json\FormatJson; |
| 12 | use MediaWiki\MediaWikiServices; |
| 13 | use Psr\Log\LoggerAwareInterface; |
| 14 | use Psr\Log\LoggerInterface; |
| 15 | use Psr\Log\NullLogger; |
| 16 | use Wikimedia\ObjectCache\WANObjectCache; |
| 17 | use Wikimedia\Rdbms\Database; |
| 18 | |
| 19 | /** |
| 20 | * This class generates message blobs for use by ResourceLoader. |
| 21 | * |
| 22 | * A message blob is a JSON object containing the interface messages for a |
| 23 | * certain module in a certain language. |
| 24 | * |
| 25 | * @ingroup ResourceLoader |
| 26 | * @since 1.17 |
| 27 | */ |
| 28 | class MessageBlobStore implements LoggerAwareInterface { |
| 29 | /** @var ResourceLoader */ |
| 30 | private $resourceloader; |
| 31 | |
| 32 | /** @var LoggerInterface */ |
| 33 | protected $logger; |
| 34 | |
| 35 | /** @var WANObjectCache */ |
| 36 | protected $wanCache; |
| 37 | |
| 38 | /** |
| 39 | * @param ResourceLoader $rl |
| 40 | * @param LoggerInterface|null $logger |
| 41 | * @param WANObjectCache|null $wanObjectCache |
| 42 | */ |
| 43 | public function __construct( |
| 44 | ResourceLoader $rl, |
| 45 | ?LoggerInterface $logger, |
| 46 | ?WANObjectCache $wanObjectCache |
| 47 | ) { |
| 48 | $this->resourceloader = $rl; |
| 49 | $this->logger = $logger ?? new NullLogger(); |
| 50 | |
| 51 | // NOTE: when changing this assignment, make sure the code in the instantiator for |
| 52 | // LocalisationCache which calls MessageBlobStore::clearGlobalCacheEntry() uses the |
| 53 | // same cache object. |
| 54 | $this->wanCache = $wanObjectCache ?? MediaWikiServices::getInstance() |
| 55 | ->getMainWANObjectCache(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @since 1.27 |
| 60 | * @param LoggerInterface $logger |
| 61 | */ |
| 62 | public function setLogger( LoggerInterface $logger ): void { |
| 63 | $this->logger = $logger; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get the message blob for a module |
| 68 | * |
| 69 | * @since 1.27 |
| 70 | * @param Module $module |
| 71 | * @param string $lang Language code |
| 72 | * @return string JSON |
| 73 | */ |
| 74 | public function getBlob( Module $module, $lang ) { |
| 75 | $blobs = $this->getBlobs( [ $module->getName() => $module ], $lang ); |
| 76 | return $blobs[$module->getName()]; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get the message blobs for a set of modules |
| 81 | * |
| 82 | * @since 1.27 |
| 83 | * @param Module[] $modules Array of module objects keyed by name |
| 84 | * @param string $lang Language code |
| 85 | * @return string[] An array mapping module names to message blobs |
| 86 | */ |
| 87 | public function getBlobs( array $modules, $lang ) { |
| 88 | // Each cache key for a message blob by module name and language code also has a generic |
| 89 | // check key without language code. This is used to invalidate any and all language subkeys |
| 90 | // that exist for a module from the updateMessage() method. |
| 91 | $checkKeys = [ |
| 92 | self::makeGlobalPurgeKey( $this->wanCache ) |
| 93 | ]; |
| 94 | $cacheKeys = []; |
| 95 | foreach ( $modules as $name => $module ) { |
| 96 | $cacheKey = $this->makeBlobCacheKey( $name, $lang, $module ); |
| 97 | $cacheKeys[$name] = $cacheKey; |
| 98 | $checkKeys[$cacheKey][] = $this->makeModulePurgeKey( $name ); |
| 99 | } |
| 100 | $curTTLs = []; |
| 101 | $result = $this->wanCache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys ); |
| 102 | |
| 103 | $blobs = []; |
| 104 | foreach ( $modules as $name => $module ) { |
| 105 | $key = $cacheKeys[$name]; |
| 106 | if ( !isset( $result[$key] ) || $curTTLs[$key] === null || $curTTLs[$key] < 0 ) { |
| 107 | $blobs[$name] = $this->recacheMessageBlob( $key, $module, $lang ); |
| 108 | } else { |
| 109 | // Use unexpired cache |
| 110 | $blobs[$name] = $result[$key]; |
| 111 | } |
| 112 | } |
| 113 | return $blobs; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @param WANObjectCache $cache |
| 118 | * @return string Cache key |
| 119 | */ |
| 120 | private static function makeGlobalPurgeKey( WANObjectCache $cache ) { |
| 121 | return $cache->makeGlobalKey( 'resourceloader-messageblob' ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Per-module check key, for ::updateMessage() |
| 126 | * |
| 127 | * @param string $name |
| 128 | * @return string Cache key |
| 129 | */ |
| 130 | private function makeModulePurgeKey( $name ) { |
| 131 | return $this->wanCache->makeKey( 'resourceloader-messageblob', $name ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param string $name |
| 136 | * @param string $lang |
| 137 | * @param Module $module |
| 138 | * @return string Cache key |
| 139 | */ |
| 140 | private function makeBlobCacheKey( $name, $lang, Module $module ) { |
| 141 | $messages = array_values( array_unique( $module->getMessages() ) ); |
| 142 | sort( $messages ); |
| 143 | return $this->wanCache->makeKey( 'resourceloader-messageblob', |
| 144 | $name, |
| 145 | $lang, |
| 146 | md5( json_encode( $messages ) ) |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @since 1.27 |
| 152 | * @param string $cacheKey |
| 153 | * @param Module $module |
| 154 | * @param string $lang |
| 155 | * @return string JSON blob |
| 156 | */ |
| 157 | protected function recacheMessageBlob( $cacheKey, Module $module, $lang ) { |
| 158 | $blob = $this->generateMessageBlob( $module, $lang ); |
| 159 | $cache = $this->wanCache; |
| 160 | $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 161 | $cache->set( $cacheKey, $blob, |
| 162 | // Add part of a day to TTL to avoid all modules expiring at once |
| 163 | $cache::TTL_WEEK + mt_rand( 0, $cache::TTL_DAY ), |
| 164 | Database::getCacheSetOptions( $dbr ) |
| 165 | ); |
| 166 | return $blob; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Invalidate cache keys for modules using this message key. |
| 171 | * Called by MessageCache when a message has changed. |
| 172 | * |
| 173 | * @param string $key Message key |
| 174 | */ |
| 175 | public function updateMessage( $key ): void { |
| 176 | $moduleNames = $this->resourceloader->getModulesByMessage( $key ); |
| 177 | foreach ( $moduleNames as $moduleName ) { |
| 178 | // Use the default holdoff TTL to account for database replica DB lag |
| 179 | // which can affect MessageCache. |
| 180 | $this->wanCache->touchCheckKey( $this->makeModulePurgeKey( $moduleName ) ); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Invalidate cache keys for all known modules. |
| 186 | * |
| 187 | * Used by LocalisationCache, DatabaseUpdater and purgeMessageBlobStore.php script |
| 188 | * after regenerating l10n cache. |
| 189 | */ |
| 190 | public static function clearGlobalCacheEntry( WANObjectCache $cache ) { |
| 191 | // Disable holdoff TTL because: |
| 192 | // - LocalisationCache is populated by messages on-disk and don't have DB lag, |
| 193 | // thus there is no need for hold off. We only clear it after new localisation |
| 194 | // updates are known to be deployed to all servers. |
| 195 | // - This global check key invalidates message blobs for all modules for all wikis |
| 196 | // in cache contexts (e.g. languages, skins). Setting a hold-off on this key could |
| 197 | // cause a cache stampede since no values would be stored for several seconds. |
| 198 | $cache->touchCheckKey( self::makeGlobalPurgeKey( $cache ), $cache::HOLDOFF_TTL_NONE ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @since 1.27 |
| 203 | * @param string $key Message key |
| 204 | * @param string $lang Language code |
| 205 | * @return string|null |
| 206 | */ |
| 207 | protected function fetchMessage( $key, $lang ) { |
| 208 | $message = wfMessage( $key )->inLanguage( $lang ); |
| 209 | if ( !$message->exists() ) { |
| 210 | $this->logger->warning( 'Failed to find {messageKey} ({lang})', [ |
| 211 | 'messageKey' => $key, |
| 212 | 'lang' => $lang, |
| 213 | ] ); |
| 214 | $value = null; |
| 215 | } else { |
| 216 | $value = $message->plain(); |
| 217 | } |
| 218 | return $value; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Generate the message blob for a given module in a given language. |
| 223 | * |
| 224 | * @param Module $module |
| 225 | * @param string $lang Language code |
| 226 | * @return string JSON blob |
| 227 | */ |
| 228 | private function generateMessageBlob( Module $module, $lang ) { |
| 229 | $messages = []; |
| 230 | foreach ( $module->getMessages() as $key ) { |
| 231 | $value = $this->fetchMessage( $key, $lang ); |
| 232 | // If the message does not exist, omit it from the blob so that |
| 233 | // client-side mw.message may do its own existence handling. |
| 234 | if ( $value !== null ) { |
| 235 | $messages[$key] = $value; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | $json = FormatJson::encode( (object)$messages, false, FormatJson::UTF8_OK ); |
| 240 | // @codeCoverageIgnoreStart |
| 241 | if ( $json === false ) { |
| 242 | $this->logger->warning( 'Failed to encode message blob for {module} ({lang})', [ |
| 243 | 'module' => $module->getName(), |
| 244 | 'lang' => $lang, |
| 245 | ] ); |
| 246 | $json = '{}'; |
| 247 | } |
| 248 | // codeCoverageIgnoreEnd |
| 249 | return $json; |
| 250 | } |
| 251 | } |