b40c3a9e76c46d2110e6553373f7a9a1bf66dd67
[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 $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->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' );
61
62 if ( !$this->skin ) {
63 $this->skin = $wgDefaultSkin;
64 }
65 }
66
67 public function getResourceLoader() {
68 return $this->resourceLoader;
69 }
70
71 public function getRequest() {
72 return $this->request;
73 }
74
75 public function getModules() {
76 return $this->modules;
77 }
78
79 public function getLanguage() {
80 if ( $this->language === null ) {
81 global $wgLang;
82 $this->language = $this->request->getVal( 'lang' );
83 if ( !$this->language ) {
84 $this->language = $wgLang->getCode();
85 }
86 }
87 return $this->language;
88 }
89
90 public function getDirection() {
91 if ( $this->direction === null ) {
92 $this->direction = $this->request->getVal( 'dir' );
93 if ( !$this->direction ) {
94 global $wgContLang;
95 $this->direction = $wgContLang->getDir();
96 }
97 }
98 return $this->direction;
99 }
100
101 public function getSkin() {
102 return $this->skin;
103 }
104
105 public function getUser() {
106 return $this->user;
107 }
108
109 public function getDebug() {
110 return $this->debug;
111 }
112
113 public function getOnly() {
114 return $this->only;
115 }
116
117 public function getVersion() {
118 return $this->version;
119 }
120
121 public function shouldIncludeScripts() {
122 return is_null( $this->only ) || $this->only === 'scripts';
123 }
124
125 public function shouldIncludeStyles() {
126 return is_null( $this->only ) || $this->only === 'styles';
127 }
128
129 public function shouldIncludeMessages() {
130 return is_null( $this->only ) || $this->only === 'messages';
131 }
132
133 public function getHash() {
134 if ( !isset( $this->hash ) ) {
135 $this->hash = implode( '|', array(
136 $this->getLanguage(), $this->getDirection(), $this->skin, $this->user,
137 $this->debug, $this->only, $this->version
138 ) );
139 }
140 return $this->hash;
141 }
142 }