MediaWiki  master
ResourceLoaderLessVarFileModule.php
Go to the documentation of this file.
1 <?php
28  protected $lessVariables = [];
29 
33  public function __construct(
34  $options = [],
35  $localBasePath = null,
36  $remoteBasePath = null
37  ) {
38  if ( isset( $options['lessMessages'] ) ) {
39  $this->lessVariables = $options['lessMessages'];
40  }
41  parent::__construct( $options, $localBasePath, $remoteBasePath );
42  }
43 
47  public function getMessages() {
48  // Overload so MessageBlobStore can detect updates to messages and purge as needed.
49  return array_merge( $this->messages, $this->lessVariables );
50  }
51 
59  protected function excludeMessagesFromBlob( $blob, $exclusions ) {
60  $data = json_decode( $blob, true );
61  // unset the LESS variables so that they are not forwarded to JavaScript
62  foreach ( $exclusions as $key ) {
63  unset( $data[$key] );
64  }
65  return (object)$data;
66  }
67 
72  $blob = parent::getMessageBlob( $context );
73  return json_encode( $this->excludeMessagesFromBlob( $blob, $this->lessVariables ) );
74  }
75 
85  private static function wrapAndEscapeMessage( $msg ) {
86  return str_replace( "'", "\'", CSSMin::serializeStringValue( $msg ) );
87  }
88 
95  protected function getLessVars( ResourceLoaderContext $context ) {
96  $blob = parent::getMessageBlob( $context );
97  $lessMessages = $this->excludeMessagesFromBlob( $blob, $this->messages );
98 
99  $vars = parent::getLessVars( $context );
100  foreach ( $lessMessages as $msgKey => $value ) {
101  $vars['msg-' . $msgKey] = self::wrapAndEscapeMessage( $value );
102  }
103  return $vars;
104  }
105 }
$context
Definition: load.php:45
static serializeStringValue( $value)
Serialize a string (escape and quote) for use as a CSS string value.
Definition: CSSMin.php:190
excludeMessagesFromBlob( $blob, $exclusions)
Exclude a set of messages from a JSON string representation.
__construct( $options=[], $localBasePath=null, $remoteBasePath=null)
getLessVars(ResourceLoaderContext $context)
Get language-specific LESS variables for this module.
Module augmented with context-specific LESS variables.
static wrapAndEscapeMessage( $msg)
Takes a message and wraps it in quotes for compatibility with LESS parser (ModifyVars) method so that...
Module based on local JavaScript/CSS files.
string $localBasePath
Local base path, see __construct()
string $remoteBasePath
Remote base path, see __construct()
getMessageBlob(ResourceLoaderContext $context)
Context object that contains information about the state of a specific ResourceLoader web request...