3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @author Trevor Parscal
20 * @author Roan Kattouw
24 * Object passed around to modules which contains information about the state
25 * of a specific loader request
27 class ResourceLoaderContext
{
29 /* Protected Members */
31 protected $resourceLoader;
45 public function __construct( ResourceLoader
$resourceLoader, WebRequest
$request ) {
46 global $wgDefaultSkin, $wgResourceLoaderDebug;
48 $this->resourceLoader
= $resourceLoader;
49 $this->request
= $request;
53 $modules = $request->getVal( 'modules' );
54 $this->modules
= $modules ? self
::expandModuleNames( $modules ) : array();
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' );
63 $this->skin
= $wgDefaultSkin;
68 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
69 * an array of module names like array( 'jquery.foo', 'jquery.bar',
70 * 'jquery.ui.baz', 'jquery.ui.quux' ) Also translating ! to .
71 * @param $modules String Packed module name list
72 * @return array of module names
74 public static function expandModuleNames( $modules ) {
76 $modules = str_replace( "!", ".", $modules ); # bug 28840 - IE is stupid.
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
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 = explode( ',', $group );
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";
102 public function getResourceLoader() {
103 return $this->resourceLoader
;
106 public function getRequest() {
107 return $this->request
;
110 public function getModules() {
111 return $this->modules
;
114 public function getLanguage() {
115 if ( $this->language
=== null ) {
117 $this->language
= $this->request
->getVal( 'lang' );
118 if ( !$this->language
) {
119 $this->language
= $wgLang->getCode();
122 return $this->language
;
125 public function getDirection() {
126 if ( $this->direction
=== null ) {
127 $this->direction
= $this->request
->getVal( 'dir' );
128 if ( !$this->direction
) {
130 $this->direction
= $wgContLang->getDir();
133 return $this->direction
;
136 public function getSkin() {
140 public function getUser() {
144 public function getDebug() {
148 public function getOnly() {
152 public function getVersion() {
153 return $this->version
;
156 public function shouldIncludeScripts() {
157 return is_null( $this->only
) ||
$this->only
=== 'scripts';
160 public function shouldIncludeStyles() {
161 return is_null( $this->only
) ||
$this->only
=== 'styles';
164 public function shouldIncludeMessages() {
165 return is_null( $this->only
) ||
$this->only
=== 'messages';
168 public function getHash() {
169 if ( !isset( $this->hash
) ) {
170 $this->hash
= implode( '|', array(
171 $this->getLanguage(), $this->getDirection(), $this->skin
, $this->user
,
172 $this->debug
, $this->only
, $this->version