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