Fixed case where tryForKey can sometimes not return an array
[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 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
53 // Interpret request
54 # list of modules
55 $modules = $request->getVal( 'modules' );
56 $this->modules = $modules ? explode( '|', $modules ) : array();
57 # various parameters
58 $this->language = $request->getVal( 'lang' );
59 $this->direction = $request->getVal( 'dir' );
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' );
65
66 // Fallback on system defaults
67 if ( !$this->language ) {
68 $this->language = $wgLang->getCode();
69 }
70
71 if ( !$this->direction ) {
72 $this->direction = Language::factory( $this->language )->getDir();
73 }
74
75 if ( !$this->skin ) {
76 $this->skin = $wgDefaultSkin;
77 }
78 }
79
80 public function getResourceLoader() {
81 return $this->resourceLoader;
82 }
83
84 public function getRequest() {
85 return $this->request;
86 }
87
88 public function getModules() {
89 return $this->modules;
90 }
91
92 public function getLanguage() {
93 return $this->language;
94 }
95
96 public function getDirection() {
97 return $this->direction;
98 }
99
100 public function getSkin() {
101 return $this->skin;
102 }
103
104 public function getUser() {
105 return $this->user;
106 }
107
108 public function getDebug() {
109 return $this->debug;
110 }
111
112 public function getOnly() {
113 return $this->only;
114 }
115
116 public function getVersion() {
117 return $this->version;
118 }
119
120 public function shouldIncludeScripts() {
121 return is_null( $this->only ) || $this->only === 'scripts';
122 }
123
124 public function shouldIncludeStyles() {
125 return is_null( $this->only ) || $this->only === 'styles';
126 }
127
128 public function shouldIncludeMessages() {
129 return is_null( $this->only ) || $this->only === 'messages';
130 }
131
132 public function getHash() {
133 if ( isset( $this->hash ) ) {
134 $this->hash = implode( '|', array(
135 $this->language, $this->direction, $this->skin, $this->user,
136 $this->debug, $this->only, $this->version
137 ) );
138 }
139 return $this->hash;
140 }
141 }