Continue with r85240; Move getSkin from User to RequestContext, do it without globals...
[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 $out; /// 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 * Get the OutputPage object
64 *
65 * @return OutputPage object
66 */
67 public function getOutput() {
68 if ( !isset($this->output) ) {
69 $this->output = new OutputPage;
70 $this->output->setContext( $this );
71 }
72 return $this->output;
73 }
74
75 /**
76 * Set the User object
77 *
78 * @param $u User
79 */
80 public function setUser( User $u ) {
81 $this->user = $u;
82 }
83
84 /**
85 * Get the User object
86 *
87 * @return User
88 */
89 public function getUser() {
90 if ( !isset($this->user) ) {
91 global $wgCommandLineMode;
92 $this->user = $wgCommandLineMode ? new User : User::newFromSession( $this->getRequest() );
93 }
94 return $this->user;
95 }
96
97 /**
98 * Get the Language object
99 *
100 * @return Language
101 */
102 public function getLang() {
103 if ( !isset($this->lang) ) {
104 global $wgLanguageCode, $wgContLang;
105 $code = $this->getRequest()->getVal( 'uselang', $this->getUser()->getOption( 'language' ) );
106 // BCP 47 - letter case MUST NOT carry meaning
107 $code = strtolower( $code );
108
109 # Validate $code
110 if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
111 wfDebug( "Invalid user language code\n" );
112 $code = $wgLanguageCode;
113 }
114
115 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
116
117 if( $code === $wgLanguageCode ) {
118 $this->lang = $wgContLang;
119 } else {
120 $obj = Language::factory( $code );
121 $this->lang = $obj;
122 }
123 }
124 return $this->lang;
125 }
126
127 /**
128 * Get the Skin object
129 *
130 * @return Skin
131 */
132 public function getSkin() {
133 if ( !isset($this->skin) ) {
134 wfProfileIn( __METHOD__ . '-createskin' );
135
136 global $wgHiddenPrefs;
137 if( !in_array( 'skin', $wgHiddenPrefs ) ) {
138 # get the user skin
139 $userSkin = $this->getUser()->getOption( 'skin' );
140 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
141 } else {
142 # if we're not allowing users to override, then use the default
143 global $wgDefaultSkin;
144 $userSkin = $wgDefaultSkin;
145 }
146
147 $this->skin = Skin::newFromKey( $userSkin );
148 $this->skin->setContext( $this );
149 wfProfileOut( __METHOD__ . '-createskin' );
150 }
151 return $this->skin;
152 }
153
154 /** Helpful methods **/
155
156 /**
157 * Get a Message object with context set
158 * Parameters are the same as wfMessage()
159 *
160 * @return Message object
161 */
162 public function msg() {
163 $args = function_get_args();
164 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->outputPage( $this->getOut() );
165 }
166
167 /** Static methods **/
168
169 /**
170 * Get the RequestContext object associated with the main request
171 *
172 * @return RequestContext object
173 */
174 public static function getMain() {
175 static $instance = null;
176 if ( !isset($instance) ) {
177 $instance = new self;
178 }
179 return $instance;
180 }
181
182 }
183