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