Index: trunk/phase3/includes/RequestContext.php |
— | — | @@ -192,12 +192,39 @@ |
193 | 193 | } |
194 | 194 | |
195 | 195 | /** |
| 196 | + * Accepts a language code and ensures it's sane. Outputs a cleaned up language |
| 197 | + * code and replaces with $wgLanguageCode if not sane. |
| 198 | + */ |
| 199 | + private static function sanitizeLangCode( $code ) { |
| 200 | + global $wgLanguageCode; |
| 201 | + |
| 202 | + // BCP 47 - letter case MUST NOT carry meaning |
| 203 | + $code = strtolower( $code ); |
| 204 | + |
| 205 | + # Validate $code |
| 206 | + if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) { |
| 207 | + wfDebug( "Invalid user language code\n" ); |
| 208 | + $code = $wgLanguageCode; |
| 209 | + } |
| 210 | + |
| 211 | + return $code; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
196 | 215 | * Set the Language object |
197 | 216 | * |
198 | | - * @param $l Language |
| 217 | + * @param $l Mixed Language instance or language code |
199 | 218 | */ |
200 | | - public function setLang( Language $l ) { |
201 | | - $this->lang = $l; |
| 219 | + public function setLang( $l ) { |
| 220 | + if ( $l instanceof Language ) { |
| 221 | + $this->lang = $l; |
| 222 | + } elseif ( is_string( $l ) ) { |
| 223 | + $l = self::sanitizeLangCode( $l ); |
| 224 | + $obj = Language::factory( $l ); |
| 225 | + $this->lang = $obj; |
| 226 | + } else { |
| 227 | + throw new MWException( __METHOD__ . " was passed an invalid type of data." ); |
| 228 | + } |
202 | 229 | } |
203 | 230 | |
204 | 231 | /** |
— | — | @@ -212,15 +239,8 @@ |
213 | 240 | 'uselang', |
214 | 241 | $this->getUser()->getOption( 'language' ) |
215 | 242 | ); |
216 | | - // BCP 47 - letter case MUST NOT carry meaning |
217 | | - $code = strtolower( $code ); |
| 243 | + $code = self::sanitizeLangCode( $code ); |
218 | 244 | |
219 | | - # Validate $code |
220 | | - if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) { |
221 | | - wfDebug( "Invalid user language code\n" ); |
222 | | - $code = $wgLanguageCode; |
223 | | - } |
224 | | - |
225 | 245 | wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) ); |
226 | 246 | |
227 | 247 | if( $code === $wgLanguageCode ) { |
— | — | @@ -297,6 +317,33 @@ |
298 | 318 | } |
299 | 319 | return $instance; |
300 | 320 | } |
| 321 | + |
| 322 | + /** |
| 323 | + * Create a new extraneous context. The context is filled with information |
| 324 | + * external to the current session. |
| 325 | + * - Title is specified by argument |
| 326 | + * - Request is a FauxRequest, or a FauxRequest can be specified by argument |
| 327 | + * - User is an anonymous user, for separation IPv4 localhost is used |
| 328 | + * - Language will be based on the anonymous user and request, may be content |
| 329 | + * language or a uselang param in the fauxrequest data may change the lang |
| 330 | + * - Skin will be based on the anonymous user, should be the wiki's default skin |
| 331 | + * |
| 332 | + * @param $title Title Title to use for the extraneous request |
| 333 | + * @param $request Mixed A WebRequest or data to use for a FauxRequest |
| 334 | + * @return RequestContext |
| 335 | + */ |
| 336 | + public static function newExtraneousContext( Title $title, $request=array() ) { |
| 337 | + $context = new self; |
| 338 | + $context->setTitle( $title ); |
| 339 | + if ( $request instanceof WebRequest ) { |
| 340 | + $context->setRequest( $request ); |
| 341 | + } else { |
| 342 | + $context->setRequest( new FauxRequest( $request ) ); |
| 343 | + } |
| 344 | + $context->user = User::newFromName( '127.0.0.1', false ); |
| 345 | + return $context; |
| 346 | + } |
| 347 | + |
301 | 348 | } |
302 | 349 | |
303 | 350 | /** |