Follow-up r85278, r85240:
[lhc/web/wiklou.git] / includes / RequestContext.php
1 <?php
2 /**
3 * Group all the pieces relevant to the context of a request into one instance
4 *
5 * @author IAlex
6 * @author Daniel Friesen
7 * @file
8 */
9
10 class RequestContext {
11 private $request; /// WebRequest object
12 private $title; /// Title object
13 private $output; /// OutputPage object
14 private $user; /// User object
15 private $lang; /// Language object
16 private $skin; /// Skin object
17
18 /**
19 * Set the WebRequest object
20 *
21 * @param $r WebRequest object
22 */
23 public function setRequest( WebRequest $r ) {
24 $this->request = $r;
25 }
26
27 /**
28 * Get the WebRequest object
29 *
30 * @return WebRequest
31 */
32 public function getRequest() {
33 if ( !isset($this->request) ) {
34 global $wgRequest; # fallback to $wg till we can improve this
35 $this->request = $wgRequest;
36 }
37 return $this->request;
38 }
39
40 /**
41 * Set the Title object
42 *
43 * @param $t Title object
44 */
45 public function setTitle( Title $t ) {
46 $this->title = $t;
47 }
48
49 /**
50 * Get the Title object
51 *
52 * @return Title
53 */
54 public function getTitle() {
55 if ( !isset($this->title) ) {
56 global $wgTitle; # fallback to $wg till we can improve this
57 $this->title = $wgTitle;
58 }
59 return $this->title;
60 }
61
62 /**
63 * Set the OutputPage object
64 *
65 * @param $u OutputPage
66 */
67 public function setOutput( OutputPage $u ) {
68 $this->output = $u;
69 }
70
71 /**
72 * Get the OutputPage object
73 *
74 * @return OutputPage object
75 */
76 public function getOutput() {
77 if ( !isset($this->output) ) {
78 $this->output = new OutputPage;
79 $this->output->setContext( $this );
80 }
81 return $this->output;
82 }
83
84 /**
85 * Set the User object
86 *
87 * @param $u User
88 */
89 public function setUser( User $u ) {
90 $this->user = $u;
91 }
92
93 /**
94 * Get the User object
95 *
96 * @return User
97 */
98 public function getUser() {
99 if ( !isset($this->user) ) {
100 global $wgCommandLineMode;
101 $this->user = $wgCommandLineMode ? new User : User::newFromSession( $this->getRequest() );
102 }
103 return $this->user;
104 }
105
106 /**
107 * Get the Language object
108 *
109 * @return Language
110 */
111 public function getLang() {
112 if ( !isset($this->lang) ) {
113 global $wgLanguageCode, $wgContLang;
114 $code = $this->getRequest()->getVal( 'uselang', $this->getUser()->getOption( 'language' ) );
115 // BCP 47 - letter case MUST NOT carry meaning
116 $code = strtolower( $code );
117
118 # Validate $code
119 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
120 wfDebug( "Invalid user language code\n" );
121 $code = $wgLanguageCode;
122 }
123
124 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
125
126 if( $code === $wgLanguageCode ) {
127 $this->lang = $wgContLang;
128 } else {
129 $obj = Language::factory( $code );
130 $this->lang = $obj;
131 }
132 }
133 return $this->lang;
134 }
135
136 /**
137 * Get the Skin object
138 *
139 * @return Skin
140 */
141 public function getSkin() {
142 if ( !isset($this->skin) ) {
143 wfProfileIn( __METHOD__ . '-createskin' );
144
145 global $wgHiddenPrefs;
146 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
147 # get the user skin
148 $userSkin = $this->getUser()->getOption( 'skin' );
149 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
150 } else {
151 # if we're not allowing users to override, then use the default
152 global $wgDefaultSkin;
153 $userSkin = $wgDefaultSkin;
154 }
155
156 $this->skin = Skin::newFromKey( $userSkin );
157 $this->skin->setContext( $this );
158 wfProfileOut( __METHOD__ . '-createskin' );
159 }
160 return $this->skin;
161 }
162
163 /** Helpful methods **/
164
165 /**
166 * Get a Message object with context set
167 * Parameters are the same as wfMessage()
168 *
169 * @return Message object
170 */
171 public function msg() {
172 $args = func_get_args();
173 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
174 }
175
176 /** Static methods **/
177
178 /**
179 * Get the RequestContext object associated with the main request
180 *
181 * @return RequestContext object
182 */
183 public static function getMain() {
184 static $instance = null;
185 if ( !isset($instance) ) {
186 $instance = new self;
187 }
188 return $instance;
189 }
190 }
191