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