Bug 25338: fixed exception in Resource Loader
[lhc/web/wiklou.git] / includes / ResourceLoaderContext.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @author Trevor Parscal
20 * @author Roan Kattouw
21 */
22
23 defined( 'MEDIAWIKI' ) || die( 1 );
24
25 /**
26 * Object passed around to modules which contains information about the state
27 * of a specific loader request
28 */
29 class ResourceLoaderContext {
30
31 /* Protected Members */
32
33 protected $resourceLoader;
34 protected $request;
35 protected $modules;
36 protected $language;
37 protected $direction;
38 protected $skin;
39 protected $user;
40 protected $debug;
41 protected $only;
42 protected $version;
43 protected $hash;
44
45 /* Methods */
46
47 public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
48 global $wgLang, $wgDefaultSkin, $wgResourceLoaderDebug;
49
50 $this->resourceLoader = $resourceLoader;
51 $this->request = $request;
52 // Interperet request
53 $modules = $request->getVal( 'modules' );
54 $this->modules = $modules ? explode( '|', $modules ) : array();
55 $this->language = $request->getVal( 'lang' );
56 $this->direction = $request->getVal( 'dir' );
57 $this->skin = $request->getVal( 'skin' );
58 $this->user = $request->getVal( 'user' );
59 $this->debug = $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
60 $this->only = $request->getVal( 'only' );
61 $this->version = $request->getVal( 'version' );
62
63 // Fallback on system defaults
64 if ( !$this->language ) {
65 $this->language = $wgLang->getCode();
66 }
67
68 if ( !$this->direction ) {
69 $this->direction = Language::factory( $this->language )->getDir();
70 }
71
72 if ( !$this->skin ) {
73 $this->skin = $wgDefaultSkin;
74 }
75 }
76
77 public function getResourceLoader() {
78 return $this->resourceLoader;
79 }
80
81 public function getRequest() {
82 return $this->request;
83 }
84
85 public function getModules() {
86 return $this->modules;
87 }
88
89 public function getLanguage() {
90 return $this->language;
91 }
92
93 public function getDirection() {
94 return $this->direction;
95 }
96
97 public function getSkin() {
98 return $this->skin;
99 }
100
101 public function getUser() {
102 return $this->user;
103 }
104
105 public function getDebug() {
106 return $this->debug;
107 }
108
109 public function getOnly() {
110 return $this->only;
111 }
112
113 public function getVersion() {
114 return $this->version;
115 }
116
117 public function shouldIncludeScripts() {
118 return is_null( $this->only ) || $this->only === 'scripts';
119 }
120
121 public function shouldIncludeStyles() {
122 return is_null( $this->only ) || $this->only === 'styles';
123 }
124
125 public function shouldIncludeMessages() {
126 return is_null( $this->only ) || $this->only === 'messages';
127 }
128
129 public function getHash() {
130 if ( isset( $this->hash ) ) {
131 $this->hash = implode( '|', array(
132 $this->language, $this->direction, $this->skin, $this->user,
133 $this->debug, $this->only, $this->version
134 ) );
135 }
136 return $this->hash;
137 }
138 }