PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/resourceloader/ResourceLoaderContext.php

https://gitlab.com/qiusct/mediawiki-i
PHP | 240 lines | 116 code | 24 blank | 100 comment | 13 complexity | 0f9cb2b5569f10c85ae883861a69b41a MD5 | raw file
Possible License(s): Apache-2.0, MIT, GPL-2.0
  1. <?php
  2. /**
  3. * Context for resource loader modules.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @author Trevor Parscal
  22. * @author Roan Kattouw
  23. */
  24. /**
  25. * Object passed around to modules which contains information about the state
  26. * of a specific loader request
  27. */
  28. class ResourceLoaderContext {
  29. /* Protected Members */
  30. protected $resourceLoader;
  31. protected $request;
  32. protected $modules;
  33. protected $language;
  34. protected $direction;
  35. protected $skin;
  36. protected $user;
  37. protected $debug;
  38. protected $only;
  39. protected $version;
  40. protected $hash;
  41. protected $raw;
  42. /* Methods */
  43. /**
  44. * @param ResourceLoader $resourceLoader
  45. * @param WebRequest $request
  46. */
  47. public function __construct( $resourceLoader, WebRequest $request ) {
  48. global $wgDefaultSkin, $wgResourceLoaderDebug;
  49. $this->resourceLoader = $resourceLoader;
  50. $this->request = $request;
  51. // Interpret request
  52. // List of modules
  53. $modules = $request->getVal( 'modules' );
  54. $this->modules = $modules ? self::expandModuleNames( $modules ) : array();
  55. // Various parameters
  56. $this->skin = $request->getVal( 'skin' );
  57. $this->user = $request->getVal( 'user' );
  58. $this->debug = $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
  59. $this->only = $request->getVal( 'only' );
  60. $this->version = $request->getVal( 'version' );
  61. $this->raw = $request->getFuzzyBool( 'raw' );
  62. $skinnames = Skin::getSkinNames();
  63. // If no skin is specified, or we don't recognize the skin, use the default skin
  64. if ( !$this->skin || !isset( $skinnames[$this->skin] ) ) {
  65. $this->skin = $wgDefaultSkin;
  66. }
  67. }
  68. /**
  69. * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
  70. * an array of module names like array( 'jquery.foo', 'jquery.bar',
  71. * 'jquery.ui.baz', 'jquery.ui.quux' )
  72. * @param string $modules Packed module name list
  73. * @return array Array of module names
  74. */
  75. public static function expandModuleNames( $modules ) {
  76. $retval = array();
  77. $exploded = explode( '|', $modules );
  78. foreach ( $exploded as $group ) {
  79. if ( strpos( $group, ',' ) === false ) {
  80. // This is not a set of modules in foo.bar,baz notation
  81. // but a single module
  82. $retval[] = $group;
  83. } else {
  84. // This is a set of modules in foo.bar,baz notation
  85. $pos = strrpos( $group, '.' );
  86. if ( $pos === false ) {
  87. // Prefixless modules, i.e. without dots
  88. $retval = array_merge( $retval, explode( ',', $group ) );
  89. } else {
  90. // We have a prefix and a bunch of suffixes
  91. $prefix = substr( $group, 0, $pos ); // 'foo'
  92. $suffixes = explode( ',', substr( $group, $pos + 1 ) ); // array( 'bar', 'baz' )
  93. foreach ( $suffixes as $suffix ) {
  94. $retval[] = "$prefix.$suffix";
  95. }
  96. }
  97. }
  98. }
  99. return $retval;
  100. }
  101. /**
  102. * Return a dummy ResourceLoaderContext object suitable for passing into
  103. * things that don't "really" need a context.
  104. * @return ResourceLoaderContext
  105. */
  106. public static function newDummyContext() {
  107. return new self( null, new FauxRequest( array() ) );
  108. }
  109. /**
  110. * @return ResourceLoader
  111. */
  112. public function getResourceLoader() {
  113. return $this->resourceLoader;
  114. }
  115. /**
  116. * @return WebRequest
  117. */
  118. public function getRequest() {
  119. return $this->request;
  120. }
  121. /**
  122. * @return array
  123. */
  124. public function getModules() {
  125. return $this->modules;
  126. }
  127. /**
  128. * @return string
  129. */
  130. public function getLanguage() {
  131. if ( $this->language === null ) {
  132. // Must be a valid language code after this point (bug 62849)
  133. $this->language = RequestContext::sanitizeLangCode( $this->request->getVal( 'lang' ) );
  134. }
  135. return $this->language;
  136. }
  137. /**
  138. * @return string
  139. */
  140. public function getDirection() {
  141. if ( $this->direction === null ) {
  142. $this->direction = $this->request->getVal( 'dir' );
  143. if ( !$this->direction ) {
  144. // Determine directionality based on user language (bug 6100)
  145. $this->direction = Language::factory( $this->getLanguage() )->getDir();
  146. }
  147. }
  148. return $this->direction;
  149. }
  150. /**
  151. * @return string|null
  152. */
  153. public function getSkin() {
  154. return $this->skin;
  155. }
  156. /**
  157. * @return string|null
  158. */
  159. public function getUser() {
  160. return $this->user;
  161. }
  162. /**
  163. * @return bool
  164. */
  165. public function getDebug() {
  166. return $this->debug;
  167. }
  168. /**
  169. * @return string|null
  170. */
  171. public function getOnly() {
  172. return $this->only;
  173. }
  174. /**
  175. * @return string|null
  176. */
  177. public function getVersion() {
  178. return $this->version;
  179. }
  180. /**
  181. * @return bool
  182. */
  183. public function getRaw() {
  184. return $this->raw;
  185. }
  186. /**
  187. * @return bool
  188. */
  189. public function shouldIncludeScripts() {
  190. return is_null( $this->only ) || $this->only === 'scripts';
  191. }
  192. /**
  193. * @return bool
  194. */
  195. public function shouldIncludeStyles() {
  196. return is_null( $this->only ) || $this->only === 'styles';
  197. }
  198. /**
  199. * @return bool
  200. */
  201. public function shouldIncludeMessages() {
  202. return is_null( $this->only ) || $this->only === 'messages';
  203. }
  204. /**
  205. * @return string
  206. */
  207. public function getHash() {
  208. if ( !isset( $this->hash ) ) {
  209. $this->hash = implode( '|', array(
  210. $this->getLanguage(), $this->getDirection(), $this->skin, $this->user,
  211. $this->debug, $this->only, $this->version
  212. ) );
  213. }
  214. return $this->hash;
  215. }
  216. }