MediaWiki REL1_35
ResourceLoaderLessVarFileModule.php
Go to the documentation of this file.
1<?php
21// Per https://phabricator.wikimedia.org/T241091
22// phpcs:disable MediaWiki.Commenting.FunctionAnnotations.UnrecognizedAnnotation
23
31 protected $lessVariables = [];
32
36 public function __construct(
37 array $options = [],
38 $localBasePath = null,
39 $remoteBasePath = null
40 ) {
41 if ( isset( $options['lessMessages'] ) ) {
42 $this->lessVariables = $options['lessMessages'];
43 }
44 parent::__construct( $options, $localBasePath, $remoteBasePath );
45 }
46
50 public function getMessages() {
51 // Overload so MessageBlobStore can detect updates to messages and purge as needed.
52 return array_merge( $this->messages, $this->lessVariables );
53 }
54
62 private function pluckFromMessageBlob( $blob, array $whitelist ) : array {
63 $data = json_decode( $blob, true );
64 // Keep only the messages intended for LESS export
65 // (opposite of getMesssages essentially).
66 return array_intersect_key( $data, array_flip( $whitelist ) );
67 }
68
72 protected function getMessageBlob( ResourceLoaderContext $context ) {
73 $blob = parent::getMessageBlob( $context );
74 return json_encode( (object)$this->pluckFromMessageBlob( $blob, $this->messages ) );
75 }
76
77 // phpcs:disable MediaWiki.Commenting.DocComment.SpacingDocTag, Squiz.WhiteSpace.FunctionSpacing.Before
98 private static function wrapAndEscapeMessage( $msg ) {
99 return str_replace( "'", "\'", CSSMin::serializeStringValue( $msg ) );
100 }
101
102 // phpcs:enable MediaWiki.Commenting.DocComment.SpacingDocTag, Squiz.WhiteSpace.FunctionSpacing.Before
103
110 protected function getLessVars( ResourceLoaderContext $context ) {
111 $blob = parent::getMessageBlob( $context );
112 $messages = $this->pluckFromMessageBlob( $blob, $this->lessVariables );
113
114 $vars = parent::getLessVars( $context );
115 foreach ( $messages as $msgKey => $value ) {
116 $vars['msg-' . $msgKey] = self::wrapAndEscapeMessage( $value );
117 }
118 return $vars;
119 }
120}
static serializeStringValue( $value)
Serialize a string (escape and quote) for use as a CSS string value.
Definition CSSMin.php:189
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.Stable to override 1.27 string|null JSON blob or null if module has ...
__construct(array $options=[], $localBasePath=null, $remoteBasePath=null)
Constructs a new module from an options array.$wgResourceModules
pluckFromMessageBlob( $blob, array $whitelist)
Return a subset of messages from a JSON string representation.
getMessages()
Gets list of message keys used by this module.array List of message keys
static wrapAndEscapeMessage( $msg)
Escape and wrap a message value as literal string for LESS.
getLessVars(ResourceLoaderContext $context)
Get language-specific LESS variables for this module.
return true
Definition router.php:92