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;
46 * @param $resourceLoader ResourceLoader
47 * @param $request WebRequest
49 public function __construct( $resourceLoader, WebRequest
$request ) {
50 global $wgDefaultSkin, $wgResourceLoaderDebug;
52 $this->resourceLoader
= $resourceLoader;
53 $this->request
= $request;
57 $modules = $request->getVal( 'modules' );
58 $this->modules
= $modules ? self
::expandModuleNames( $modules ) : array();
60 $this->skin
= $request->getVal( 'skin' );
61 $this->user
= $request->getVal( 'user' );
62 $this->debug
= $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
63 $this->only
= $request->getVal( 'only' );
64 $this->version
= $request->getVal( 'version' );
67 $this->skin
= $wgDefaultSkin;
72 * Expand a string of the form jquery.foo,bar|jquery.ui.baz,quux to
73 * an array of module names like array( 'jquery.foo', 'jquery.bar',
74 * 'jquery.ui.baz', 'jquery.ui.quux' )
75 * @param $modules String Packed module name list
76 * @return array of module names
78 public static function expandModuleNames( $modules ) {
80 // For backwards compatibility with an earlier hack, replace ! with .
81 $modules = str_replace( '!', '.', $modules );
82 $exploded = explode( '|', $modules );
83 foreach ( $exploded as $group ) {
84 if ( strpos( $group, ',' ) === false ) {
85 // This is not a set of modules in foo.bar,baz notation
86 // but a single module
89 // This is a set of modules in foo.bar,baz notation
90 $pos = strrpos( $group, '.' );
91 if ( $pos === false ) {
92 // Prefixless modules, i.e. without dots
93 $retval = explode( ',', $group );
95 // We have a prefix and a bunch of suffixes
96 $prefix = substr( $group, 0, $pos ); // 'foo'
97 $suffixes = explode( ',', substr( $group, $pos +
1 ) ); // array( 'bar', 'baz' )
98 foreach ( $suffixes as $suffix ) {
99 $retval[] = "$prefix.$suffix";
108 * Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need a context
109 * @return ResourceLoaderContext
111 public static function newDummyContext() {
112 return new self( null, new FauxRequest( array() ) );
116 * @return ResourceLoader
118 public function getResourceLoader() {
119 return $this->resourceLoader
;
125 public function getRequest() {
126 return $this->request
;
132 public function getModules() {
133 return $this->modules
;
139 public function getLanguage() {
140 if ( $this->language
=== null ) {
142 $this->language
= $this->request
->getVal( 'lang' );
143 if ( !$this->language
) {
144 $this->language
= $wgLang->getCode();
147 return $this->language
;
153 public function getDirection() {
154 if ( $this->direction
=== null ) {
155 $this->direction
= $this->request
->getVal( 'dir' );
156 if ( !$this->direction
) {
157 # directionality based on user language (see bug 6100)
158 $this->direction
= Language
::factory( $this->language
)->getDir();
161 return $this->direction
;
165 * @return string|null
167 public function getSkin() {
172 * @return string|null
174 public function getUser() {
181 public function getDebug() {
186 * @return String|null
188 public function getOnly() {
193 * @return String|null
195 public function getVersion() {
196 return $this->version
;
202 public function shouldIncludeScripts() {
203 return is_null( $this->only
) ||
$this->only
=== 'scripts';
209 public function shouldIncludeStyles() {
210 return is_null( $this->only
) ||
$this->only
=== 'styles';
216 public function shouldIncludeMessages() {
217 return is_null( $this->only
) ||
$this->only
=== 'messages';
223 public function getHash() {
224 if ( !isset( $this->hash
) ) {
225 $this->hash
= implode( '|', array(
226 $this->getLanguage(), $this->getDirection(), $this->skin
, $this->user
,
227 $this->debug
, $this->only
, $this->version