* Removed break in first loop of generateTableHTML(), which caused:
[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 * @since 1.18
6 *
7 * @author IAlex
8 * @author Daniel Friesen
9 * @file
10 */
11
12 class RequestContext {
13
14 /**
15 * @var WebRequest
16 */
17 private $mRequest;
18
19 /**
20 * @var Title
21 */
22 private $mTitle;
23
24 /**
25 * @var OutputPage
26 */
27 private $mOutput;
28
29 /**
30 * @var User
31 */
32 private $mUser;
33
34 /**
35 * @var Language
36 */
37 private $mLang;
38
39 /**
40 * @var Skin
41 */
42 private $mSkin;
43
44 /**
45 * Set the WebRequest object
46 *
47 * @param $r WebRequest object
48 */
49 public function setRequest( WebRequest $r ) {
50 $this->mRequest = $r;
51 }
52
53 /**
54 * Get the WebRequest object
55 *
56 * @return WebRequest
57 */
58 public function getRequest() {
59 if ( !isset( $this->mRequest ) ) {
60 global $wgRequest; # fallback to $wg till we can improve this
61 $this->mRequest = $wgRequest;
62 }
63 return $this->mRequest;
64 }
65
66 /**
67 * Set the Title object
68 *
69 * @param $t Title object
70 */
71 public function setTitle( Title $t ) {
72 $this->mTitle = $t;
73 }
74
75 /**
76 * Get the Title object
77 *
78 * @return Title
79 */
80 public function getTitle() {
81 if ( !isset( $this->mTitle ) ) {
82 global $wgTitle; # fallback to $wg till we can improve this
83 $this->mTitle = $wgTitle;
84 }
85 return $this->mTitle;
86 }
87
88 /**
89 * @param $o OutputPage
90 */
91 public function setOutput( OutputPage $o ) {
92 $this->mOutput = $o;
93 }
94
95 /**
96 * Get the OutputPage object
97 *
98 * @return OutputPage object
99 */
100 public function getOutput() {
101 if ( !isset( $this->mOutput ) ) {
102 $this->mOutput = new OutputPage( $this );
103 }
104 return $this->mOutput;
105 }
106
107 /**
108 * Set the User object
109 *
110 * @param $u User
111 */
112 public function setUser( User $u ) {
113 $this->mUser = $u;
114 }
115
116 /**
117 * Get the User object
118 *
119 * @return User
120 */
121 public function getUser() {
122 if ( !isset( $this->mUser ) ) {
123 global $wgCommandLineMode;
124 $this->mUser = $wgCommandLineMode
125 ? new User
126 : User::newFromSession( $this->getRequest() );
127 }
128 return $this->mUser;
129 }
130
131 /**
132 * Get the Language object
133 *
134 * @return Language
135 */
136 public function getLang() {
137 if ( !isset( $this->mLang ) ) {
138 global $wgLanguageCode, $wgContLang;
139 $code = $this->getRequest()->getVal(
140 'uselang',
141 $this->getUser()->getOption( 'language' )
142 );
143 // BCP 47 - letter case MUST NOT carry meaning
144 $code = strtolower( $code );
145
146 # Validate $code
147 if ( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
148 wfDebug( "Invalid user language code\n" );
149 $code = $wgLanguageCode;
150 }
151
152 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
153
154 if ( $code === $wgLanguageCode ) {
155 $this->mLang = $wgContLang;
156 } else {
157 $obj = Language::factory( $code );
158 $this->mLang = $obj;
159 }
160 }
161 return $this->mLang;
162 }
163
164 /**
165 * Get the Skin object
166 *
167 * @return Skin
168 */
169 public function getSkin() {
170 if ( !isset( $this->mSkin ) ) {
171 wfProfileIn( __METHOD__ . '-createskin' );
172
173 global $wgHiddenPrefs;
174 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
175 # get the user skin
176 $userSkin = $this->getUser()->getOption( 'skin' );
177 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
178 } else {
179 # if we're not allowing users to override, then use the default
180 global $wgDefaultSkin;
181 $userSkin = $wgDefaultSkin;
182 }
183
184 $this->mSkin = Skin::newFromKey( $userSkin );
185 $this->mSkin->setContext( $this );
186 wfProfileOut( __METHOD__ . '-createskin' );
187 }
188 return $this->mSkin;
189 }
190
191 /** Helpful methods **/
192
193 /**
194 * Get a Message object with context set
195 * Parameters are the same as wfMessage()
196 *
197 * @return Message object
198 */
199 public function msg() {
200 $args = func_get_args();
201 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
202 }
203
204 /** Static methods **/
205
206 /**
207 * Get the RequestContext object associated with the main request
208 *
209 * @return RequestContext object
210 */
211 public static function getMain() {
212 static $instance = null;
213 if ( !isset( $instance ) ) {
214 $instance = new self;
215 }
216 return $instance;
217 }
218
219 /**
220 * Make these C#-style accessors, so you can do $context->user->getName() which is
221 * internally mapped to $context->__get('user')->getName() which is mapped to
222 * $context->getUser()->getName()
223 *
224 * @param $name string
225 *
226 * @return string
227 */
228 public function __get( $name ) {
229 wfDeprecated( 'RequestContext::__get() is deprecated; use $context->getFoo() instead' );
230 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
231 $fname = 'get' . ucfirst( $name );
232 return $this->$fname();
233 }
234 trigger_error( "Undefined property {$name}", E_NOTICE );
235 }
236
237 /**
238 * @param $name string
239 * @param $value
240 * @return string
241 */
242 public function __set( $name, $value ) {
243 wfDeprecated( 'RequestContext::__set() is deprecated; use $context->setFoo() instead' );
244 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
245 $fname = 'set' . ucfirst( $name );
246 return $this->$fname( $value );
247 }
248 trigger_error( "Undefined property {$name}", E_NOTICE );
249 }
250 }
251