| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\ResourceLoader; |
| 24 |
|
| 25 |
use MediaWiki\Json\FormatJson; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use Psr\Log\LoggerAwareInterface; |
| 28 |
use Psr\Log\LoggerInterface; |
| 29 |
use Psr\Log\NullLogger; |
| 30 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 31 |
use Wikimedia\Rdbms\Database; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class MessageBlobStore implements LoggerAwareInterface { |
| 43 |
|
| 44 |
private $resourceloader; |
| 45 |
|
| 46 |
|
| 47 |
protected $logger; |
| 48 |
|
| 49 |
|
| 50 |
protected $wanCache; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( |
| 58 |
ResourceLoader $rl, |
| 59 |
?LoggerInterface $logger, |
| 60 |
?WANObjectCache $wanObjectCache |
| 61 |
) { |
| 62 |
$this->resourceloader = $rl; |
| 63 |
$this->logger = $logger ?: new NullLogger(); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
$this->wanCache = $wanObjectCache ?: MediaWikiServices::getInstance() |
| 69 |
->getMainWANObjectCache(); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function setLogger( LoggerInterface $logger ) { |
| 77 |
$this->logger = $logger; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function getBlob( Module $module, $lang ) { |
| 89 |
$blobs = $this->getBlobs( [ $module->getName() => $module ], $lang ); |
| 90 |
return $blobs[$module->getName()]; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function getBlobs( array $modules, $lang ) { |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
$checkKeys = [ |
| 106 |
self::makeGlobalPurgeKey( $this->wanCache ) |
| 107 |
]; |
| 108 |
$cacheKeys = []; |
| 109 |
foreach ( $modules as $name => $module ) { |
| 110 |
$cacheKey = $this->makeBlobCacheKey( $name, $lang, $module ); |
| 111 |
$cacheKeys[$name] = $cacheKey; |
| 112 |
$checkKeys[$cacheKey][] = $this->makeModulePurgeKey( $name ); |
| 113 |
} |
| 114 |
$curTTLs = []; |
| 115 |
$result = $this->wanCache->getMulti( array_values( $cacheKeys ), $curTTLs, $checkKeys ); |
| 116 |
|
| 117 |
$blobs = []; |
| 118 |
foreach ( $modules as $name => $module ) { |
| 119 |
$key = $cacheKeys[$name]; |
| 120 |
if ( !isset( $result[$key] ) || $curTTLs[$key] === null || $curTTLs[$key] < 0 ) { |
| 121 |
$blobs[$name] = $this->recacheMessageBlob( $key, $module, $lang ); |
| 122 |
} else { |
| 123 |
|
| 124 |
$blobs[$name] = $result[$key]; |
| 125 |
} |
| 126 |
} |
| 127 |
return $blobs; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private static function makeGlobalPurgeKey( WANObjectCache $cache ) { |
| 137 |
return $cache->makeGlobalKey( 'resourceloader-messageblob' ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
private function makeModulePurgeKey( $name ) { |
| 147 |
return $this->wanCache->makeKey( 'resourceloader-messageblob', $name ); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
private function makeBlobCacheKey( $name, $lang, Module $module ) { |
| 157 |
$messages = array_values( array_unique( $module->getMessages() ) ); |
| 158 |
sort( $messages ); |
| 159 |
return $this->wanCache->makeKey( 'resourceloader-messageblob', |
| 160 |
$name, |
| 161 |
$lang, |
| 162 |
md5( json_encode( $messages ) ) |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
protected function recacheMessageBlob( $cacheKey, Module $module, $lang ) { |
| 174 |
$blob = $this->generateMessageBlob( $module, $lang ); |
| 175 |
$cache = $this->wanCache; |
| 176 |
$dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 177 |
$cache->set( $cacheKey, $blob, |
| 178 |
|
| 179 |
$cache::TTL_WEEK + mt_rand( 0, $cache::TTL_DAY ), |
| 180 |
Database::getCacheSetOptions( $dbr ) |
| 181 |
); |
| 182 |
return $blob; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function updateMessage( $key ): void { |
| 192 |
$moduleNames = $this->resourceloader->getModulesByMessage( $key ); |
| 193 |
foreach ( $moduleNames as $moduleName ) { |
| 194 |
|
| 195 |
|
| 196 |
$this->wanCache->touchCheckKey( $this->makeModulePurgeKey( $moduleName ) ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function clear() { |
| 206 |
self::clearGlobalCacheEntry( $this->wanCache ); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public static function clearGlobalCacheEntry( WANObjectCache $cache ) { |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
$cache->touchCheckKey( self::makeGlobalPurgeKey( $cache ), $cache::HOLDOFF_TTL_NONE ); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
protected function fetchMessage( $key, $lang ) { |
| 234 |
$message = wfMessage( $key )->inLanguage( $lang ); |
| 235 |
if ( !$message->exists() ) { |
| 236 |
$this->logger->warning( 'Failed to find {messageKey} ({lang})', [ |
| 237 |
'messageKey' => $key, |
| 238 |
'lang' => $lang, |
| 239 |
] ); |
| 240 |
$value = null; |
| 241 |
} else { |
| 242 |
$value = $message->plain(); |
| 243 |
} |
| 244 |
return $value; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
private function generateMessageBlob( Module $module, $lang ) { |
| 255 |
$messages = []; |
| 256 |
foreach ( $module->getMessages() as $key ) { |
| 257 |
$value = $this->fetchMessage( $key, $lang ); |
| 258 |
|
| 259 |
|
| 260 |
if ( $value !== null ) { |
| 261 |
$messages[$key] = $value; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
$json = FormatJson::encode( (object)$messages, false, FormatJson::UTF8_OK ); |
| 266 |
|
| 267 |
if ( $json === false ) { |
| 268 |
$this->logger->warning( 'Failed to encode message blob for {module} ({lang})', [ |
| 269 |
'module' => $module->getName(), |
| 270 |
'lang' => $lang, |
| 271 |
] ); |
| 272 |
$json = '{}'; |
| 273 |
} |
| 274 |
|
| 275 |
return $json; |
| 276 |
} |
| 277 |
} |