Change calls from Xml::namespaceSelector() to Html::namespaceSelector() since the...
[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 WikiPage
44 */
45 private $wikipage;
46
47 /**
48 * @var OutputPage
49 */
50 private $output;
51
52 /**
53 * @var User
54 */
55 private $user;
56
57 /**
58 * @var Language
59 */
60 private $lang;
61
62 /**
63 * @var Skin
64 */
65 private $skin;
66
67 /**
68 * Set the WebRequest object
69 *
70 * @param $r WebRequest object
71 */
72 public function setRequest( WebRequest $r ) {
73 $this->request = $r;
74 }
75
76 /**
77 * Get the WebRequest object
78 *
79 * @return WebRequest
80 */
81 public function getRequest() {
82 if ( $this->request === null ) {
83 global $wgRequest; # fallback to $wg till we can improve this
84 $this->request = $wgRequest;
85 }
86 return $this->request;
87 }
88
89 /**
90 * Set the Title object
91 *
92 * @param $t Title object
93 */
94 public function setTitle( Title $t ) {
95 $this->title = $t;
96 }
97
98 /**
99 * Get the Title object
100 *
101 * @return Title
102 */
103 public function getTitle() {
104 if ( $this->title === null ) {
105 global $wgTitle; # fallback to $wg till we can improve this
106 $this->title = $wgTitle;
107 }
108 return $this->title;
109 }
110
111 /**
112 * Check whether a WikiPage object can be get with getWikiPage().
113 * Callers should expect that an exception is thrown from getWikiPage()
114 * if this method returns false.
115 *
116 * @since 1.19
117 * @return bool
118 */
119 public function canUseWikiPage() {
120 if ( $this->wikipage !== null ) {
121 # If there's a WikiPage object set, we can for sure get it
122 return true;
123 }
124 $title = $this->getTitle();
125 if ( $title === null ) {
126 # No Title, no WikiPage
127 return false;
128 } else {
129 # Only namespaces whose pages are stored in the database can have WikiPage
130 return $title->canExist();
131 }
132 }
133
134 /**
135 * Set the WikiPage object
136 *
137 * @since 1.19
138 * @param $p WikiPage object
139 */
140 public function setWikiPage( WikiPage $p ) {
141 $this->wikipage = $p;
142 }
143
144 /**
145 * Get the WikiPage object.
146 * May throw an exception if there's no Title object set or the Title object
147 * belongs to a special namespace that doesn't have WikiPage, so use first
148 * canUseWikiPage() to check whether this method can be called safely.
149 *
150 * @since 1.19
151 * @return WikiPage
152 */
153 public function getWikiPage() {
154 if ( $this->wikipage === null ) {
155 $title = $this->getTitle();
156 if ( $title === null ) {
157 throw new MWException( __METHOD__ . ' called without Title object set' );
158 }
159 $this->wikipage = WikiPage::factory( $title );
160 }
161 return $this->wikipage;
162 }
163
164 /**
165 * @param $o OutputPage
166 */
167 public function setOutput( OutputPage $o ) {
168 $this->output = $o;
169 }
170
171 /**
172 * Get the OutputPage object
173 *
174 * @return OutputPage object
175 */
176 public function getOutput() {
177 if ( $this->output === null ) {
178 $this->output = new OutputPage( $this );
179 }
180 return $this->output;
181 }
182
183 /**
184 * Set the User object
185 *
186 * @param $u User
187 */
188 public function setUser( User $u ) {
189 $this->user = $u;
190 }
191
192 /**
193 * Get the User object
194 *
195 * @return User
196 */
197 public function getUser() {
198 if ( $this->user === null ) {
199 $this->user = User::newFromSession( $this->getRequest() );
200 }
201 return $this->user;
202 }
203
204 /**
205 * Accepts a language code and ensures it's sane. Outputs a cleaned up language
206 * code and replaces with $wgLanguageCode if not sane.
207 * @param $code string
208 * @return string
209 */
210 public static function sanitizeLangCode( $code ) {
211 global $wgLanguageCode;
212
213 // BCP 47 - letter case MUST NOT carry meaning
214 $code = strtolower( $code );
215
216 # Validate $code
217 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
218 wfDebug( "Invalid user language code\n" );
219 $code = $wgLanguageCode;
220 }
221
222 return $code;
223 }
224
225 /**
226 * Set the Language object
227 *
228 * @deprecated 1.19 Use setLanguage instead
229 * @param $l Mixed Language instance or language code
230 */
231 public function setLang( $l ) {
232 wfDeprecated( __METHOD__, '1.19' );
233 $this->setLanguage( $l );
234 }
235
236 /**
237 * Set the Language object
238 *
239 * @param $l Mixed Language instance or language code
240 * @since 1.19
241 */
242 public function setLanguage( $l ) {
243 if ( $l instanceof Language ) {
244 $this->lang = $l;
245 } elseif ( is_string( $l ) ) {
246 $l = self::sanitizeLangCode( $l );
247 $obj = Language::factory( $l );
248 $this->lang = $obj;
249 } else {
250 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
251 }
252 }
253
254 /**
255 * @deprecated 1.19 Use getLanguage instead
256 * @return Language
257 */
258 public function getLang() {
259 wfDeprecated( __METHOD__, '1.19' );
260 return $this->getLanguage();
261 }
262
263 /**
264 * Get the Language object
265 *
266 * @return Language
267 * @since 1.19
268 */
269 public function getLanguage() {
270 if ( $this->lang === null ) {
271 global $wgLanguageCode, $wgContLang;
272 $code = $this->getRequest()->getVal(
273 'uselang',
274 $this->getUser()->getOption( 'language' )
275 );
276 $code = self::sanitizeLangCode( $code );
277
278 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
279
280 if( $code === $wgLanguageCode ) {
281 $this->lang = $wgContLang;
282 } else {
283 $obj = Language::factory( $code );
284 $this->lang = $obj;
285 }
286 }
287 return $this->lang;
288 }
289
290 /**
291 * Set the Skin object
292 *
293 * @param $s Skin
294 */
295 public function setSkin( Skin $s ) {
296 $this->skin = clone $s;
297 $this->skin->setContext( $this );
298 }
299
300 /**
301 * Get the Skin object
302 *
303 * @return Skin
304 */
305 public function getSkin() {
306 if ( $this->skin === null ) {
307 wfProfileIn( __METHOD__ . '-createskin' );
308
309 $skin = null;
310 wfRunHooks( 'RequestContextCreateSkin', array( $this, &$skin ) );
311
312 // If the hook worked try to set a skin from it
313 if ( $skin instanceof Skin ) {
314 $this->skin = $skin;
315 } elseif ( is_string($skin) ) {
316 $this->skin = Skin::newFromKey( $skin );
317 }
318
319 // If this is still null (the hook didn't run or didn't work)
320 // then go through the normal processing to load a skin
321 if ( $this->skin === null ) {
322 global $wgHiddenPrefs;
323 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
324 # get the user skin
325 $userSkin = $this->getUser()->getOption( 'skin' );
326 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
327 } else {
328 # if we're not allowing users to override, then use the default
329 global $wgDefaultSkin;
330 $userSkin = $wgDefaultSkin;
331 }
332
333 $this->skin = Skin::newFromKey( $userSkin );
334 }
335
336 // After all that set a context on whatever skin got created
337 $this->skin->setContext( $this );
338 wfProfileOut( __METHOD__ . '-createskin' );
339 }
340 return $this->skin;
341 }
342
343 /** Helpful methods **/
344
345 /**
346 * Get a Message object with context set
347 * Parameters are the same as wfMessage()
348 *
349 * @return Message object
350 */
351 public function msg() {
352 $args = func_get_args();
353 return call_user_func_array( 'wfMessage', $args )->setContext( $this );
354 }
355
356 /** Static methods **/
357
358 /**
359 * Get the RequestContext object associated with the main request
360 *
361 * @return RequestContext object
362 */
363 public static function getMain() {
364 static $instance = null;
365 if ( $instance === null ) {
366 $instance = new self;
367 }
368 return $instance;
369 }
370
371 /**
372 * Create a new extraneous context. The context is filled with information
373 * external to the current session.
374 * - Title is specified by argument
375 * - Request is a FauxRequest, or a FauxRequest can be specified by argument
376 * - User is an anonymous user, for separation IPv4 localhost is used
377 * - Language will be based on the anonymous user and request, may be content
378 * language or a uselang param in the fauxrequest data may change the lang
379 * - Skin will be based on the anonymous user, should be the wiki's default skin
380 *
381 * @param $title Title Title to use for the extraneous request
382 * @param $request Mixed A WebRequest or data to use for a FauxRequest
383 * @return RequestContext
384 */
385 public static function newExtraneousContext( Title $title, $request=array() ) {
386 $context = new self;
387 $context->setTitle( $title );
388 if ( $request instanceof WebRequest ) {
389 $context->setRequest( $request );
390 } else {
391 $context->setRequest( new FauxRequest( $request ) );
392 }
393 $context->user = User::newFromName( '127.0.0.1', false );
394 return $context;
395 }
396
397 }
398