Followup r103817, updating setLang usages
[lhc/web/wiklou.git] / includes / context / RequestContext.php
1 <?php
2 /**
3 * Request-dependant objects containers.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @since 1.18
21 *
22 * @author Alexandre Emsenhuber
23 * @author Daniel Friesen
24 * @file
25 */
26
27 /**
28 * Group all the pieces relevant to the context of a request into one instance
29 */
30 class RequestContext implements IContextSource {
31
32 /**
33 * @var WebRequest
34 */
35 private $request;
36
37 /**
38 * @var Title
39 */
40 private $title;
41
42 /**
43 * @var OutputPage
44 */
45 private $output;
46
47 /**
48 * @var User
49 */
50 private $user;
51
52 /**
53 * @var Language
54 */
55 private $lang;
56
57 /**
58 * @var Skin
59 */
60 private $skin;
61
62 /**
63 * Set the WebRequest object
64 *
65 * @param $r WebRequest object
66 */
67 public function setRequest( WebRequest $r ) {
68 $this->request = $r;
69 }
70
71 /**
72 * Get the WebRequest object
73 *
74 * @return WebRequest
75 */
76 public function getRequest() {
77 if ( $this->request === null ) {
78 global $wgRequest; # fallback to $wg till we can improve this
79 $this->request = $wgRequest;
80 }
81 return $this->request;
82 }
83
84 /**
85 * Set the Title object
86 *
87 * @param $t Title object
88 */
89 public function setTitle( Title $t ) {
90 $this->title = $t;
91 }
92
93 /**
94 * Get the Title object
95 *
96 * @return Title
97 */
98 public function getTitle() {
99 if ( $this->title === null ) {
100 global $wgTitle; # fallback to $wg till we can improve this
101 $this->title = $wgTitle;
102 }
103 return $this->title;
104 }
105
106 /**
107 * @param $o OutputPage
108 */
109 public function setOutput( OutputPage $o ) {
110 $this->output = $o;
111 }
112
113 /**
114 * Get the OutputPage object
115 *
116 * @return OutputPage object
117 */
118 public function getOutput() {
119 if ( $this->output === null ) {
120 $this->output = new OutputPage( $this );
121 }
122 return $this->output;
123 }
124
125 /**
126 * Set the User object
127 *
128 * @param $u User
129 */
130 public function setUser( User $u ) {
131 $this->user = $u;
132 }
133
134 /**
135 * Get the User object
136 *
137 * @return User
138 */
139 public function getUser() {
140 if ( $this->user === null ) {
141 $this->user = User::newFromSession( $this->getRequest() );
142 }
143 return $this->user;
144 }
145
146 /**
147 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
148 * code and replaces with $wgLanguageCode if not sane.
149 */
150 private static function sanitizeLangCode( $code ) {
151 global $wgLanguageCode;
152
153 // BCP 47 - letter case MUST NOT carry meaning
154 $code = strtolower( $code );
155
156 # Validate $code
157 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
158 wfDebug( "Invalid user language code\n" );
159 $code = $wgLanguageCode;
160 }
161
162 return $code;
163 }
164
165 /**
166 * Set the Language object
167 *
168 * @deprecated 1.19 Use setLanguage instead
169 * @param $l Mixed Language instance or language code
170 */
171 public function setLang( $l ) {
172 $this->setLanguage( $l );
173 }
174
175 /**
176 * Set the Language object
177 *
178 * @param $l Mixed Language instance or language code
179 */
180 public function setLanguage( $l ) {
181 if ( $l instanceof Language ) {
182 $this->lang = $l;
183 } elseif ( is_string( $l ) ) {
184 $l = self::sanitizeLangCode( $l );
185 $obj = Language::factory( $l );
186 $this->lang = $obj;
187 } else {
188 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
189 }
190 }
191
192 /**
193 * @deprecated 1.19 Use getLanguage instead
194 * @return Language
195 */
196 public function getLang() {
197 return $this->getLanguage();
198 }
199
200 /**
201 * Get the Language object
202 *
203 * @return Language
204 */
205 public function getLanguage() {
206 if ( $this->lang === null ) {
207 global $wgLanguageCode, $wgContLang;
208 $code = $this->getRequest()->getVal(
209 'uselang',
210 $this->getUser()->getOption( 'language' )
211 );
212 $code = self::sanitizeLangCode( $code );
213
214 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
215
216 if( $code === $wgLanguageCode ) {
217 $this->lang = $wgContLang;
218 } else {
219 $obj = Language::factory( $code );
220 $this->lang = $obj;
221 }
222 }
223 return $this->lang;
224 }
225
226 /**
227 * Set the Skin object
228 *
229 * @param $s Skin
230 */
231 public function setSkin( Skin $s ) {
232 $this->skin = clone $s;
233 $this->skin->setContext( $this );
234 }
235
236 /**
237 * Get the Skin object
238 *
239 * @return Skin
240 */
241 public function getSkin() {
242 if ( $this->skin === null ) {
243 wfProfileIn( __METHOD__ . '-createskin' );
244
245 global $wgHiddenPrefs;
246 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
247 # get the user skin
248 $userSkin = $this->getUser()->getOption( 'skin' );
249 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
250 } else {
251 # if we're not allowing users to override, then use the default
252 global $wgDefaultSkin;
253 $userSkin = $wgDefaultSkin;
254 }
255
256 $this->skin = Skin::newFromKey( $userSkin );
257 $this->skin->setContext( $this );
258 wfProfileOut( __METHOD__ . '-createskin' );
259 }
260 return $this->skin;
261 }
262
263 /** Helpful methods **/
264
265 /**
266 * Get a Message object with context set
267 * Parameters are the same as wfMessage()
268 *
269 * @return Message object
270 */
271 public function msg() {
272 $args = func_get_args();
273 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
274 }
275
276 /** Static methods **/
277
278 /**
279 * Get the RequestContext object associated with the main request
280 *
281 * @return RequestContext object
282 */
283 public static function getMain() {
284 static $instance = null;
285 if ( $instance === null ) {
286 $instance = new self;
287 }
288 return $instance;
289 }
290
291 /**
292 * Create a new extraneous context. The context is filled with information
293 * external to the current session.
294 * - Title is specified by argument
295 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
296 * - User is an anonymous user, for separation IPv4 localhost is used
297 * - Language will be based on the anonymous user and request, may be content
298 * language or a uselang param in the fauxrequest data may change the lang
299 * - Skin will be based on the anonymous user, should be the wiki's default skin
300 *
301 * @param $title Title Title to use for the extraneous request
302 * @param $request Mixed A WebRequest or data to use for a FauxRequest
303 * @return RequestContext
304 */
305 public static function newExtraneousContext( Title $title, $request=array() ) {
306 $context = new self;
307 $context->setTitle( $title );
308 if ( $request instanceof WebRequest ) {
309 $context->setRequest( $request );
310 } else {
311 $context->setRequest( new FauxRequest( $request ) );
312 }
313 $context->user = User::newFromName( '127.0.0.1', false );
314 return $context;
315 }
316
317 }
318