MediaWiki master
LessVarFileModule.php
Go to the documentation of this file.
1<?php
21
22use Wikimedia\Minify\CSSMin;
23
24// Per https://phabricator.wikimedia.org/T241091
25// phpcs:disable MediaWiki.Commenting.FunctionAnnotations.UnrecognizedAnnotation
26
35 protected $lessVariables = [];
36
40 public function __construct(
41 array $options = [],
42 $localBasePath = null,
43 $remoteBasePath = null
44 ) {
45 if ( isset( $options['lessMessages'] ) ) {
46 $this->lessVariables = $options['lessMessages'];
47 }
48 parent::__construct( $options, $localBasePath, $remoteBasePath );
49 }
50
54 public function getMessages() {
55 // Overload so MessageBlobStore can detect updates to messages and purge as needed.
56 return array_merge( $this->messages, $this->lessVariables );
57 }
58
66 private function pluckFromMessageBlob( $blob, array $allowed ): array {
67 $data = $blob ? json_decode( $blob, true ) : [];
68 // Keep only the messages intended for LESS export
69 // (opposite of getMessages essentially).
70 return array_intersect_key( $data, array_fill_keys( $allowed, true ) );
71 }
72
76 protected function getMessageBlob( Context $context ) {
77 $blob = parent::getMessageBlob( $context );
78 if ( !$blob ) {
79 // If module has no blob, preserve null to avoid needless WAN cache allocation
80 // client output for modules without messages.
81 return $blob;
82 }
83 return json_encode( (object)$this->pluckFromMessageBlob( $blob, $this->messages ) );
84 }
85
86 // phpcs:disable MediaWiki.Commenting.DocComment.SpacingDocTag, Squiz.WhiteSpace.FunctionSpacing.Before
107 private static function wrapAndEscapeMessage( $msg ) {
108 return str_replace( "'", "\'", CSSMin::serializeStringValue( $msg ) );
109 }
110
111 // phpcs:enable MediaWiki.Commenting.DocComment.SpacingDocTag, Squiz.WhiteSpace.FunctionSpacing.Before
112
119 protected function getLessVars( Context $context ) {
120 $vars = parent::getLessVars( $context );
121
122 $blob = parent::getMessageBlob( $context );
123 $messages = $this->pluckFromMessageBlob( $blob, $this->lessVariables );
124
125 // It is important that we iterate the declared list from $this->lessVariables,
126 // and not $messages since in the case of undefined messages, the key is
127 // omitted entirely from the blob. This emits a log warning for developers,
128 // but we must still carry on and produce a valid LESS variable declaration,
129 // to avoid a LESS syntax error (T267785).
130 foreach ( $this->lessVariables as $msgKey ) {
131 $vars['msg-' . $msgKey] = self::wrapAndEscapeMessage( $messages[$msgKey] ?? "⧼{$msgKey}⧽" );
132 }
133
134 return $vars;
135 }
136}
Context object that contains information about the state of a specific ResourceLoader web request.
Definition Context.php:46
Module based on local JavaScript/CSS files.
string $remoteBasePath
Remote base path, see __construct()
string $localBasePath
Local base path, see __construct()
Module augmented with context-specific LESS variables.
getMessageBlob(Context $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)
getMessages()
Get message keys used by this module.string[] List of message keys
getLessVars(Context $context)
Get language-specific LESS variables for this module.