3 * Context for resource loader modules.
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.
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.
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
21 * @author Trevor Parscal
22 * @author Roan Kattouw
26 * Object passed around to modules which contains information about the state
27 * of a specific loader request
29 class ResourceLoaderContext
{
30 /* Protected Members */
32 protected $resourceLoader;
48 * @param ResourceLoader $resourceLoader
49 * @param WebRequest $request
51 public function __construct( $resourceLoader, WebRequest
$request ) {
52 global $wgDefaultSkin, $wgResourceLoaderDebug;
54 $this->resourceLoader
= $resourceLoader;
55 $this->request
= $request;
59 $modules = $request->getVal( 'modules' );
60 $this->modules
= $modules ? self
::expandModuleNames( $modules ) : array();
62 $this->skin
= $request->getVal( 'skin' );
63 $this->user
= $request->getVal( 'user' );
64 $this->debug
= $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
65 $this->only
= $request->getVal( 'only' );
66 $this->version
= $request->getVal( 'version' );
67 $this->raw
= $request->getFuzzyBool( 'raw' );
69 $skinnames = Skin
::getSkinNames();
70 // If no skin is specified, or we don't recognize the skin, use the default skin
71 if ( !$this->skin ||
!isset( $skinnames[$this->skin
] ) ) {
72 $this->skin
= $wgDefaultSkin;
77 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
78 * an array of module names like array( 'jquery.foo', 'jquery.bar',
79 * 'jquery.ui.baz', 'jquery.ui.quux' )
80 * @param string $modules Packed module name list
81 * @return array Array of module names
83 public static function expandModuleNames( $modules ) {
85 $exploded = explode( '|', $modules );
86 foreach ( $exploded as $group ) {
87 if ( strpos( $group, ',' ) === false ) {
88 // This is not a set of modules in foo.bar,baz notation
89 // but a single module
92 // This is a set of modules in foo.bar,baz notation
93 $pos = strrpos( $group, '.' );
94 if ( $pos === false ) {
95 // Prefixless modules, i.e. without dots
96 $retval = array_merge( $retval, explode( ',', $group ) );
98 // We have a prefix and a bunch of suffixes
99 $prefix = substr( $group, 0, $pos ); // 'foo'
100 $suffixes = explode( ',', substr( $group, $pos +
1 ) ); // array( 'bar', 'baz' )
101 foreach ( $suffixes as $suffix ) {
102 $retval[] = "$prefix.$suffix";
111 * Return a dummy ResourceLoaderContext object suitable for passing into
112 * things that don't "really" need a context.
113 * @return ResourceLoaderContext
115 public static function newDummyContext() {
116 return new self( null, new FauxRequest( array() ) );
120 * @return ResourceLoader
122 public function getResourceLoader() {
123 return $this->resourceLoader
;
129 public function getRequest() {
130 return $this->request
;
136 public function getModules() {
137 return $this->modules
;
143 public function getLanguage() {
144 if ( $this->language
=== null ) {
145 // Must be a valid language code after this point (bug 62849)
146 $this->language
= RequestContext
::sanitizeLangCode( $this->request
->getVal( 'lang' ) );
148 return $this->language
;
154 public function getDirection() {
155 if ( $this->direction
=== null ) {
156 $this->direction
= $this->request
->getVal( 'dir' );
157 if ( !$this->direction
) {
158 // Determine directionality based on user language (bug 6100)
159 $this->direction
= Language
::factory( $this->getLanguage() )->getDir();
162 return $this->direction
;
166 * @return string|null
168 public function getSkin() {
173 * @return string|null
175 public function getUser() {
182 public function getDebug() {
187 * @return string|null
189 public function getOnly() {
194 * @return string|null
196 public function getVersion() {
197 return $this->version
;
203 public function getRaw() {
210 public function shouldIncludeScripts() {
211 return is_null( $this->only
) ||
$this->only
=== 'scripts';
217 public function shouldIncludeStyles() {
218 return is_null( $this->only
) ||
$this->only
=== 'styles';
224 public function shouldIncludeMessages() {
225 return is_null( $this->only
) ||
$this->only
=== 'messages';
231 public function getHash() {
232 if ( !isset( $this->hash
) ) {
233 $this->hash
= implode( '|', array(
234 $this->getLanguage(), $this->getDirection(), $this->skin
, $this->user
,
235 $this->debug
, $this->only
, $this->version