MediaWiki
REL1_37
ResourceLoaderLessVarFileModule.php
Go to the documentation of this file.
1
<?php
20
use Wikimedia\Minify\CSSMin;
21
22
// Per https://phabricator.wikimedia.org/T241091
23
// phpcs:disable MediaWiki.Commenting.FunctionAnnotations.UnrecognizedAnnotation
24
31
class
ResourceLoaderLessVarFileModule
extends
ResourceLoaderFileModule
{
32
protected
$lessVariables
= [];
33
37
public
function
__construct
(
38
array $options = [],
39
$localBasePath
=
null
,
40
$remoteBasePath
=
null
41
) {
42
if
( isset( $options[
'lessMessages'
] ) ) {
43
$this->lessVariables = $options[
'lessMessages'
];
44
}
45
parent::__construct( $options,
$localBasePath
,
$remoteBasePath
);
46
}
47
51
public
function
getMessages
() {
52
// Overload so MessageBlobStore can detect updates to messages and purge as needed.
53
return
array_merge( $this->messages, $this->lessVariables );
54
}
55
63
private
function
pluckFromMessageBlob
(
$blob
, array $allowed ): array {
64
$data =
$blob
? json_decode(
$blob
,
true
) : [];
65
// Keep only the messages intended for LESS export
66
// (opposite of getMesssages essentially).
67
return
array_intersect_key( $data, array_fill_keys( $allowed,
true
) );
68
}
69
73
protected
function
getMessageBlob
(
ResourceLoaderContext
$context ) {
74
$blob
= parent::getMessageBlob( $context );
75
if
( !
$blob
) {
76
// If module has no blob, preserve null to avoid needless WAN cache allocation
77
// client output for modules without messages.
78
return
$blob
;
79
}
80
return
json_encode( (
object
)$this->pluckFromMessageBlob(
$blob
, $this->messages ) );
81
}
82
83
// phpcs:disable MediaWiki.Commenting.DocComment.SpacingDocTag, Squiz.WhiteSpace.FunctionSpacing.Before
104
private
static
function
wrapAndEscapeMessage
( $msg ) {
105
return
str_replace(
"'"
,
"\'"
, CSSMin::serializeStringValue( $msg ) );
106
}
107
108
// phpcs:enable MediaWiki.Commenting.DocComment.SpacingDocTag, Squiz.WhiteSpace.FunctionSpacing.Before
109
116
protected
function
getLessVars
(
ResourceLoaderContext
$context ) {
117
$vars = parent::getLessVars( $context );
118
119
$blob
= parent::getMessageBlob( $context );
120
$messages = $this->pluckFromMessageBlob(
$blob
, $this->lessVariables );
121
122
// It is important that we iterate the declared list from $this->lessVariables,
123
// and not $messages since in the case of undefined messages, the key is
124
// omitted entirely from the blob. This emits a log warning for developers,
125
// but we must still carry on and produce a valid LESS variable declaration,
126
// to avoid a LESS syntax error (T267785).
127
foreach
( $this->lessVariables as $msgKey ) {
128
$vars[
'msg-'
. $msgKey] = self::wrapAndEscapeMessage( $messages[$msgKey] ??
"⧼${msgKey}⧽"
);
129
}
130
131
return
$vars;
132
}
133
}
ResourceLoaderContext
Context object that contains information about the state of a specific ResourceLoader web request.
Definition
ResourceLoaderContext.php:34
ResourceLoaderFileModule
Module based on local JavaScript/CSS files.
Definition
ResourceLoaderFileModule.php:40
ResourceLoaderFileModule\$localBasePath
string $localBasePath
Local base path, see __construct()
Definition
ResourceLoaderFileModule.php:42
ResourceLoaderFileModule\$remoteBasePath
string $remoteBasePath
Remote base path, see __construct()
Definition
ResourceLoaderFileModule.php:45
ResourceLoaderLessVarFileModule
Module augmented with context-specific LESS variables.
Definition
ResourceLoaderLessVarFileModule.php:31
ResourceLoaderLessVarFileModule\getMessageBlob
getMessageBlob(ResourceLoaderContext $context)
Get the hash of the message blob.to override 1.27 string|null JSON blob or null if module has no mess...
Definition
ResourceLoaderLessVarFileModule.php:73
ResourceLoaderLessVarFileModule\__construct
__construct(array $options=[], $localBasePath=null, $remoteBasePath=null)
Constructs a new module from an options array.$wgResourceModules
Definition
ResourceLoaderLessVarFileModule.php:37
ResourceLoaderLessVarFileModule\getMessages
getMessages()
Gets list of message keys used by this module.string[] List of message keys
Definition
ResourceLoaderLessVarFileModule.php:51
ResourceLoaderLessVarFileModule\wrapAndEscapeMessage
static wrapAndEscapeMessage( $msg)
Escape and wrap a message value as literal string for LESS.
Definition
ResourceLoaderLessVarFileModule.php:104
ResourceLoaderLessVarFileModule\pluckFromMessageBlob
pluckFromMessageBlob( $blob, array $allowed)
Return a subset of messages from a JSON string representation.
Definition
ResourceLoaderLessVarFileModule.php:63
ResourceLoaderLessVarFileModule\$lessVariables
$lessVariables
Definition
ResourceLoaderLessVarFileModule.php:32
ResourceLoaderLessVarFileModule\getLessVars
getLessVars(ResourceLoaderContext $context)
Get language-specific LESS variables for this module.
Definition
ResourceLoaderLessVarFileModule.php:116
true
return true
Definition
router.php:92
$blob
$blob
Definition
testCompression.php:70
includes
resourceloader
ResourceLoaderLessVarFileModule.php
Generated on Fri Apr 5 2024 23:40:45 for MediaWiki by
1.9.8