MediaWiki  master
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 }
ResourceLoaderLessVarFileModule
Module augmented with context-specific LESS variables.
Definition: ResourceLoaderLessVarFileModule.php:30
ResourceLoaderContext
Context object that contains information about the state of a specific ResourceLoader web request.
Definition: ResourceLoaderContext.php:33
ResourceLoaderLessVarFileModule\__construct
__construct(array $options=[], $localBasePath=null, $remoteBasePath=null)
Constructs a new module from an options array.See $wgResourceModules for the available options....
Definition: ResourceLoaderLessVarFileModule.php:36
ResourceLoaderLessVarFileModule\pluckFromMessageBlob
pluckFromMessageBlob( $blob, array $whitelist)
Return a subset of messages from a JSON string representation.
Definition: ResourceLoaderLessVarFileModule.php:62
ResourceLoaderLessVarFileModule\$lessVariables
$lessVariables
Definition: ResourceLoaderLessVarFileModule.php:31
CSSMin\serializeStringValue
static serializeStringValue( $value)
Serialize a string (escape and quote) for use as a CSS string value.
Definition: CSSMin.php:189
ResourceLoaderLessVarFileModule\getLessVars
getLessVars(ResourceLoaderContext $context)
Get language-specific LESS variables for this module.
Definition: ResourceLoaderLessVarFileModule.php:110
ResourceLoaderFileModule
Module based on local JavaScript/CSS files.
Definition: ResourceLoaderFileModule.php:39
$blob
$blob
Definition: testCompression.php:70
ResourceLoaderFileModule\$messages
array $messages
List of message keys used by this module.
Definition: ResourceLoaderFileModule.php:145
ResourceLoaderFileModule\$localBasePath
string $localBasePath
Local base path, see __construct()
Definition: ResourceLoaderFileModule.php:41
ResourceLoaderLessVarFileModule\getMessageBlob
getMessageBlob(ResourceLoaderContext $context)
Get the hash of the message blob.Stable to override 1.27 string|null JSON blob or null if module has ...
Definition: ResourceLoaderLessVarFileModule.php:72
ResourceLoaderFileModule\$remoteBasePath
string $remoteBasePath
Remote base path, see __construct()
Definition: ResourceLoaderFileModule.php:44
ResourceLoaderLessVarFileModule\getMessages
getMessages()
Gets list of message keys used by this module.array List of message keys
Definition: ResourceLoaderLessVarFileModule.php:50
ResourceLoaderLessVarFileModule\wrapAndEscapeMessage
static wrapAndEscapeMessage( $msg)
Escape and wrap a message value as literal string for LESS.
Definition: ResourceLoaderLessVarFileModule.php:98