3e4bac87ecb01fff4f9a214a5cbcd3b9a690a976
[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 $request;
34 protected $modules;
35 protected $language;
36 protected $direction;
37 protected $skin;
38 protected $user;
39 protected $debug;
40 protected $only;
41 protected $version;
42 protected $hash;
43
44 /* Methods */
45
46 public function __construct( WebRequest $request ) {
47 global $wgLang, $wgDefaultSkin, $wgResourceLoaderDebug;
48
49 $this->request = $request;
50 // Interperet request
51 $this->modules = explode( '|', $request->getVal( 'modules' ) );
52 $this->language = $request->getVal( 'lang' );
53 $this->direction = $request->getVal( 'dir' );
54 $this->skin = $request->getVal( 'skin' );
55 $this->user = $request->getVal( 'user' );
56 $this->debug = $request->getFuzzyBool( 'debug', $wgResourceLoaderDebug );
57 $this->only = $request->getVal( 'only' );
58 $this->version = $request->getVal( 'version' );
59
60 // Fallback on system defaults
61 if ( !$this->language ) {
62 $this->language = $wgLang->getCode();
63 }
64
65 if ( !$this->direction ) {
66 $this->direction = Language::factory( $this->language )->getDir();
67 }
68
69 if ( !$this->skin ) {
70 $this->skin = $wgDefaultSkin;
71 }
72 }
73
74 public function getRequest() {
75 return $this->request;
76 }
77
78 public function getModules() {
79 return $this->modules;
80 }
81
82 public function getLanguage() {
83 return $this->language;
84 }
85
86 public function getDirection() {
87 return $this->direction;
88 }
89
90 public function getSkin() {
91 return $this->skin;
92 }
93
94 public function getUser() {
95 return $this->user;
96 }
97
98 public function getDebug() {
99 return $this->debug;
100 }
101
102 public function getOnly() {
103 return $this->only;
104 }
105
106 public function getVersion() {
107 return $this->version;
108 }
109
110 public function shouldIncludeScripts() {
111 return is_null( $this->only ) || $this->only === 'scripts';
112 }
113
114 public function shouldIncludeStyles() {
115 return is_null( $this->only ) || $this->only === 'styles';
116 }
117
118 public function shouldIncludeMessages() {
119 return is_null( $this->only ) || $this->only === 'messages';
120 }
121
122 public function getHash() {
123 if ( isset( $this->hash ) ) {
124 $this->hash = implode( '|', array(
125 $this->language, $this->direction, $this->skin, $this->user,
126 $this->debug, $this->only, $this->version
127 ) );
128 }
129 return $this->hash;
130 }
131 }