MediaWiki REL1_37
ResourceLoaderLessVarFileModule.php
Go to the documentation of this file.
1<?php
20use Wikimedia\Minify\CSSMin;
21
22// Per https://phabricator.wikimedia.org/T241091
23// phpcs:disable MediaWiki.Commenting.FunctionAnnotations.UnrecognizedAnnotation
24
32 protected $lessVariables = [];
33
37 public function __construct(
38 array $options = [],
39 $localBasePath = null,
40 $remoteBasePath = null
41 ) {
42 if ( isset( $options['lessMessages'] ) ) {
43 $this->lessVariables = $options['lessMessages'];
44 }
45 parent::__construct( $options, $localBasePath, $remoteBasePath );
46 }
47
51 public function getMessages() {
52 // Overload so MessageBlobStore can detect updates to messages and purge as needed.
53 return array_merge( $this->messages, $this->lessVariables );
54 }
55
63 private function pluckFromMessageBlob( $blob, array $allowed ): array {
64 $data = $blob ? json_decode( $blob, true ) : [];
65 // Keep only the messages intended for LESS export
66 // (opposite of getMesssages essentially).
67 return array_intersect_key( $data, array_fill_keys( $allowed, true ) );
68 }
69
73 protected function getMessageBlob( ResourceLoaderContext $context ) {
74 $blob = parent::getMessageBlob( $context );
75 if ( !$blob ) {
76 // If module has no blob, preserve null to avoid needless WAN cache allocation
77 // client output for modules without messages.
78 return $blob;
79 }
80 return json_encode( (object)$this->pluckFromMessageBlob( $blob, $this->messages ) );
81 }
82
83 // phpcs:disable MediaWiki.Commenting.DocComment.SpacingDocTag, Squiz.WhiteSpace.FunctionSpacing.Before
104 private static function wrapAndEscapeMessage( $msg ) {
105 return str_replace( "'", "\'", CSSMin::serializeStringValue( $msg ) );
106 }
107
108 // phpcs:enable MediaWiki.Commenting.DocComment.SpacingDocTag, Squiz.WhiteSpace.FunctionSpacing.Before
109
116 protected function getLessVars( ResourceLoaderContext $context ) {
117 $vars = parent::getLessVars( $context );
118
119 $blob = parent::getMessageBlob( $context );
120 $messages = $this->pluckFromMessageBlob( $blob, $this->lessVariables );
121
122 // It is important that we iterate the declared list from $this->lessVariables,
123 // and not $messages since in the case of undefined messages, the key is
124 // omitted entirely from the blob. This emits a log warning for developers,
125 // but we must still carry on and produce a valid LESS variable declaration,
126 // to avoid a LESS syntax error (T267785).
127 foreach ( $this->lessVariables as $msgKey ) {
128 $vars['msg-' . $msgKey] = self::wrapAndEscapeMessage( $messages[$msgKey] ?? "⧼${msgKey}⧽" );
129 }
130
131 return $vars;
132 }
133}
Context object that contains information about the state of a specific ResourceLoader web request.
Module based on local JavaScript/CSS files.
string $localBasePath
Local base path, see __construct()
string $remoteBasePath
Remote base path, see __construct()
Module augmented with context-specific LESS variables.
getMessageBlob(ResourceLoaderContext $context)
Get the hash of the message blob.to override 1.27 string|null JSON blob or null if module has no mess...
__construct(array $options=[], $localBasePath=null, $remoteBasePath=null)
Constructs a new module from an options array.$wgResourceModules
getMessages()
Gets list of message keys used by this module.string[] List of message keys
static wrapAndEscapeMessage( $msg)
Escape and wrap a message value as literal string for LESS.
pluckFromMessageBlob( $blob, array $allowed)
Return a subset of messages from a JSON string representation.
getLessVars(ResourceLoaderContext $context)
Get language-specific LESS variables for this module.
return true
Definition router.php:92