Documentation and type hinting.
[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 * Get the OutputPage object
64 *
65 * @return OutputPage object
66 */
67 public function getOutput() {
68 if ( !isset( $this->mOutput ) ) {
69 $this->mOutput = new OutputPage( $this );
70 }
71 return $this->mOutput;
72 }
73
74 /**
75 * Set the User object
76 *
77 * @param $u User
78 */
79 public function setUser( User $u ) {
80 $this->mUser = $u;
81 }
82
83 /**
84 * Get the User object
85 *
86 * @return User
87 */
88 public function getUser() {
89 if ( !isset( $this->mUser ) ) {
90 global $wgCommandLineMode;
91 $this->mUser = $wgCommandLineMode
92 ? new User
93 : User::newFromSession( $this->getRequest() );
94 }
95 return $this->mUser;
96 }
97
98 /**
99 * Get the Language object
100 *
101 * @return Language
102 */
103 public function getLang() {
104 if ( !isset( $this->mLang ) ) {
105 global $wgLanguageCode, $wgContLang;
106 $code = $this->getRequest()->getVal(
107 'uselang',
108 $this->getUser()->getOption( 'language' )
109 );
110 // BCP 47 - letter case MUST NOT carry meaning
111 $code = strtolower( $code );
112
113 # Validate $code
114 if ( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) {
115 wfDebug( "Invalid user language code\n" );
116 $code = $wgLanguageCode;
117 }
118
119 wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) );
120
121 if ( $code === $wgLanguageCode ) {
122 $this->mLang = $wgContLang;
123 } else {
124 $obj = Language::factory( $code );
125 $this->mLang = $obj;
126 }
127 }
128 return $this->mLang;
129 }
130
131 /**
132 * Get the Skin object
133 *
134 * @return Skin
135 */
136 public function getSkin() {
137 if ( !isset( $this->mSkin ) ) {
138 wfProfileIn( __METHOD__ . '-createskin' );
139
140 global $wgHiddenPrefs;
141 if ( !in_array( 'skin', $wgHiddenPrefs ) ) {
142 # get the user skin
143 $userSkin = $this->getUser()->getOption( 'skin' );
144 $userSkin = $this->getRequest()->getVal( 'useskin', $userSkin );
145 } else {
146 # if we're not allowing users to override, then use the default
147 global $wgDefaultSkin;
148 $userSkin = $wgDefaultSkin;
149 }
150
151 $this->mSkin = Skin::newFromKey( $userSkin );
152 $this->mSkin->setContext( $this );
153 wfProfileOut( __METHOD__ . '-createskin' );
154 }
155 return $this->mSkin;
156 }
157
158 /** Helpful methods **/
159
160 /**
161 * Get a Message object with context set
162 * Parameters are the same as wfMessage()
163 *
164 * @return Message object
165 */
166 public function msg() {
167 $args = func_get_args();
168 return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
169 }
170
171 /** Static methods **/
172
173 /**
174 * Get the RequestContext object associated with the main request
175 *
176 * @return RequestContext object
177 */
178 public static function getMain() {
179 static $instance = null;
180 if ( !isset( $instance ) ) {
181 $instance = new self;
182 }
183 return $instance;
184 }
185
186 /**
187 * Make these C#-style accessors, so you can do $context->user->getName() which is
188 * internally mapped to $context->__get('user')->getName() which is mapped to
189 * $context->getUser()->getName()
190 */
191 public function __get( $name ) {
192 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
193 $fname = 'get' . ucfirst( $name );
194 return $this->$fname();
195 }
196 trigger_error( "Undefined property {$name}", E_NOTICE );
197 }
198
199 public function __set( $name, $value ) {
200 if ( in_array( $name, array( 'request', 'title', 'output', 'user', 'lang', 'skin' ) ) ) {
201 $fname = 'set' . ucfirst( $name );
202 return $this->$fname( $value );
203 }
204 trigger_error( "Undefined property {$name}", E_NOTICE );
205 }
206 }
207