Merge "resourceloader: Add $context to static functions in ResourceLoader"
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderContext.php
index c3948cb..1274052 100644 (file)
@@ -1,7 +1,5 @@
 <?php
 /**
- * Context for ResourceLoader modules.
- *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -26,8 +24,11 @@ use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\MediaWikiServices;
 
 /**
- * Object passed around to modules which contains information about the state
- * of a specific loader request.
+ * Context object that contains information about the state of a specific
+ * ResourceLoader web request. Passed around to ResourceLoaderModule methods.
+ *
+ * @ingroup ResourceLoader
+ * @since 1.17
  */
 class ResourceLoaderContext implements MessageLocalizer {
        const DEFAULT_LANG = 'qqx';
@@ -55,6 +56,7 @@ class ResourceLoaderContext implements MessageLocalizer {
        protected $direction;
        protected $hash;
        protected $userObj;
+       /** @var ResourceLoaderImage|false */
        protected $imageObj;
 
        /**
@@ -214,6 +216,7 @@ class ResourceLoaderContext implements MessageLocalizer {
         * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
         *   or a MessageSpecifier.
         * @param mixed $args,...
+        * @suppress PhanCommentParamWithoutRealParam HHVM bug T228695#5450847
         * @return Message
         */
        public function msg( $key ) {
@@ -403,7 +406,7 @@ class ResourceLoaderContext implements MessageLocalizer {
        /**
         * Get the request base parameters, omitting any defaults.
         *
-        * @internal For internal use by ResourceLoaderStartUpModule only
+        * @internal For use by ResourceLoaderStartUpModule only
         * @return array
         */
        public function getReqBase() {
@@ -419,4 +422,32 @@ class ResourceLoaderContext implements MessageLocalizer {
                }
                return $reqBase;
        }
+
+       /**
+        * Wrapper around json_encode that avoids needless escapes,
+        * and pretty-prints in debug mode.
+        *
+        * @internal
+        * @param mixed $data
+        * @return string|false JSON string, false on error
+        */
+       public function encodeJson( $data ) {
+               // Keep output as small as possible by disabling needless escape modes
+               // that PHP uses by default.
+               // However, while most module scripts are only served on HTTP responses
+               // for JavaScript, some modules can also be embedded in the HTML as inline
+               // scripts. This, and the fact that we sometimes need to export strings
+               // containing user-generated content and labels that may genuinely contain
+               // a sequences like "</script>", we need to encode either '/' or '<'.
+               // By default PHP escapes '/'. Let's escape '<' instead which is less common
+               // and allows URLs to mostly remain readable.
+               $jsonFlags = JSON_UNESCAPED_SLASHES |
+                       JSON_UNESCAPED_UNICODE |
+                       JSON_HEX_TAG |
+                       JSON_HEX_AMP;
+               if ( $this->getDebug() ) {
+                       $jsonFlags |= JSON_PRETTY_PRINT;
+               }
+               return json_encode( $data, $jsonFlags );
+       }
 }