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