Index: trunk/phase3/includes/RequestContext.php |
— | — | @@ -0,0 +1,167 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Group all the pieces relevant to the context of a request into one instance |
| 5 | + * |
| 6 | + * @author IAlex |
| 7 | + * @author Daniel Friesen |
| 8 | + * @file |
| 9 | + */ |
| 10 | + |
| 11 | +class RequestContext { |
| 12 | + private $request; /// WebRequest object |
| 13 | + private $title; /// Title object |
| 14 | + private $out; /// OutputPage object |
| 15 | + private $user; /// User object |
| 16 | + private $lang; /// Language object |
| 17 | + private $skin; /// Skin object |
| 18 | + |
| 19 | + /** |
| 20 | + * Set the WebRequest object |
| 21 | + * |
| 22 | + * @param $r WebRequest object |
| 23 | + */ |
| 24 | + public function setRequest( WebRequest $r ) { |
| 25 | + $this->request = $r; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Get the WebRequest object |
| 30 | + * |
| 31 | + * @return WebRequest |
| 32 | + */ |
| 33 | + public function getRequest() { |
| 34 | + if ( !isset($this->request) ) { |
| 35 | + global $wgRequest; # fallback to $wg till we can improve this |
| 36 | + $this->request = $wgRequest; |
| 37 | + } |
| 38 | + return $this->request; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Set the Title object |
| 43 | + * |
| 44 | + * @param $t Title object |
| 45 | + */ |
| 46 | + public function setTitle( Title $t ) { |
| 47 | + $this->title = $t; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get the Title object |
| 52 | + * |
| 53 | + * @return Title |
| 54 | + */ |
| 55 | + public function getTitle() { |
| 56 | + if ( !isset($this->title) ) { |
| 57 | + global $wgTitle; # fallback to $wg till we can improve this |
| 58 | + $this->title = $wgTitle; |
| 59 | + } |
| 60 | + return $this->title; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the OutputPage object |
| 65 | + * |
| 66 | + * @return OutputPage object |
| 67 | + */ |
| 68 | + public function getOutput() { |
| 69 | + if ( !isset($this->output) ) { |
| 70 | + $this->output = new OutputPage; |
| 71 | + $this->output->setContext( $this ); |
| 72 | + } |
| 73 | + return $this->output; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Set the User object |
| 78 | + * |
| 79 | + * @param $u User |
| 80 | + */ |
| 81 | + public function setUser( User $u ) { |
| 82 | + $this->user = $u; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the User object |
| 87 | + * |
| 88 | + * @return User |
| 89 | + */ |
| 90 | + public function getUser() { |
| 91 | + if ( !isset($this->user) ) { |
| 92 | + global $wgCommandLineMode; |
| 93 | + $this->user = $wgCommandLineMode ? new User : User::newFromSession( $this->getRequest() ); |
| 94 | + } |
| 95 | + return $this->user; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Get the Language object |
| 100 | + * |
| 101 | + * @return Language |
| 102 | + */ |
| 103 | + public function getLang() { |
| 104 | + if ( !isset($this->lang) ) { |
| 105 | + global $wgLanguageCode, $wgContLang; |
| 106 | + $code = $this->getRequest()->getVal( 'uselang', $this->getUser()->getOption( 'language' ) ); |
| 107 | + // BCP 47 - letter case MUST NOT carry meaning |
| 108 | + $code = strtolower( $code ); |
| 109 | + |
| 110 | + # Validate $code |
| 111 | + if( empty( $code ) || !Language::isValidCode( $code ) || ( $code === 'qqq' ) ) { |
| 112 | + wfDebug( "Invalid user language code\n" ); |
| 113 | + $code = $wgLanguageCode; |
| 114 | + } |
| 115 | + |
| 116 | + wfRunHooks( 'UserGetLanguageObject', array( $this->getUser(), &$code ) ); |
| 117 | + |
| 118 | + if( $code === $wgLanguageCode ) { |
| 119 | + $this->lang = $wgContLang; |
| 120 | + } else { |
| 121 | + $obj = Language::factory( $code ); |
| 122 | + $this->lang = $obj; |
| 123 | + } |
| 124 | + } |
| 125 | + return $this->lang; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Get the Skin object |
| 130 | + * |
| 131 | + * @return Skin |
| 132 | + */ |
| 133 | + public function getSkin() { |
| 134 | + // For now we'll just proxy to the user. In the future a saner location for |
| 135 | + // organizing what skin to use may be chosen |
| 136 | + return $this->getUser()->getSkin(); |
| 137 | + } |
| 138 | + |
| 139 | + /** Helpful methods **/ |
| 140 | + |
| 141 | + /** |
| 142 | + * Get a Message object with context set |
| 143 | + * Parameters are the same as wfMessage() |
| 144 | + * |
| 145 | + * @return Message object |
| 146 | + */ |
| 147 | + public function msg() { |
| 148 | + $args = function_get_args(); |
| 149 | + return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->outputPage( $this->getOut() ); |
| 150 | + } |
| 151 | + |
| 152 | + /** Static methods **/ |
| 153 | + |
| 154 | + /** |
| 155 | + * Get the RequestContext object associated with the main request |
| 156 | + * |
| 157 | + * @return RequestContext object |
| 158 | + */ |
| 159 | + public static function getMain() { |
| 160 | + static $instance = null; |
| 161 | + if ( !isset($instance) ) { |
| 162 | + $instance = new self; |
| 163 | + } |
| 164 | + return $instance; |
| 165 | + } |
| 166 | + |
| 167 | +} |
| 168 | + |
Property changes on: trunk/phase3/includes/RequestContext.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 169 | + native |