Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderLessVarFileModule.php
1 <?php
2
3 /**
4 * Subclass with context specific LESS variables
5 */
6 class ResourceLoaderLessVarFileModule extends ResourceLoaderFileModule {
7 protected $lessVariables = [
8 'collapsible-collapse',
9 'collapsible-expand',
10 ];
11
12 /**
13 * @inheritDoc
14 */
15 public function getMessages() {
16 // Overload so MessageBlobStore can detect updates to messages and purge as needed.
17 return array_merge( $this->messages, $this->lessVariables );
18 }
19
20 /**
21 * Exclude a set of messages from a JSON string representation
22 * @param string $blob
23 * @param array $exclusions
24 * @return array $blob
25 */
26 protected function excludeMessagesFromBlob( $blob, $exclusions ) {
27 $data = json_decode( $blob, true );
28 // unset the LESS variables so that they are not forwarded to JavaScript
29 foreach ( $exclusions as $key ) {
30 unset( $data[$key] );
31 }
32 return $data;
33 }
34
35 /**
36 * @inheritDoc
37 */
38 protected function getMessageBlob( ResourceLoaderContext $context ) {
39 $blob = parent::getMessageBlob( $context );
40 return json_encode( $this->excludeMessagesFromBlob( $blob, $this->lessVariables ) );
41 }
42
43 /**
44 * Takes a message and wraps it in quotes for compatibility with LESS parser
45 * (ModifyVars) method so that the variable can be loaded and made available to stylesheets.
46 * Note this does not take care of CSS escaping. That will be taken care of as part
47 * of CSS Janus.
48 * @param string $msg
49 * @return string wrapped LESS variable definition
50 */
51 private static function wrapAndEscapeMessage( $msg ) {
52 return str_replace( "'", "\'", CSSMin::serializeStringValue( $msg ) );
53 }
54
55 /**
56 * @param \ResourceLoaderContext $context
57 * @return array LESS variables
58 */
59 protected function getLessVars( \ResourceLoaderContext $context ) {
60 $blob = parent::getMessageBlob( $context );
61 $lessMessages = $this->excludeMessagesFromBlob( $blob, $this->messages );
62
63 $vars = [];
64 foreach ( $lessMessages as $msgKey => $value ) {
65 $vars['msg-' . $msgKey] = self::wrapAndEscapeMessage( $value );
66 }
67 return $vars;
68 }
69 }