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