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