3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @author Daniel Friesen
23 * An IContextSource implementation which will inherit context from another source
24 * but allow individual pieces of context to be changed locally
25 * eg: A ContextSource that can inherit from the main RequestContext but have
26 * a different Title instance set on it.
29 class DerivativeContext
extends ContextSource
implements MutableContext
{
82 * @param IContextSource $context Context to inherit from
84 public function __construct( IContextSource
$context ) {
85 $this->setContext( $context );
89 * Set the SiteConfiguration object
93 public function setConfig( Config
$s ) {
98 * Get the Config object
102 public function getConfig() {
103 if ( !is_null( $this->config
) ) {
104 return $this->config
;
106 return $this->getContext()->getConfig();
111 * Get the stats object
113 * @return BufferingStatsdDataFactory
115 public function getStats() {
116 if ( !is_null( $this->stats
) ) {
119 return $this->getContext()->getStats();
124 * Get the timing object
128 public function getTiming() {
129 if ( !is_null( $this->timing
) ) {
130 return $this->timing
;
132 return $this->getContext()->getTiming();
137 * Set the WebRequest object
139 * @param WebRequest $r
141 public function setRequest( WebRequest
$r ) {
146 * Get the WebRequest object
150 public function getRequest() {
151 if ( !is_null( $this->request
) ) {
152 return $this->request
;
154 return $this->getContext()->getRequest();
159 * Set the Title object
163 public function setTitle( Title
$t ) {
168 * Get the Title object
172 public function getTitle() {
173 if ( !is_null( $this->title
) ) {
176 return $this->getContext()->getTitle();
181 * Check whether a WikiPage object can be get with getWikiPage().
182 * Callers should expect that an exception is thrown from getWikiPage()
183 * if this method returns false.
188 public function canUseWikiPage() {
189 if ( $this->wikipage
!== null ) {
191 } elseif ( $this->title
!== null ) {
192 return $this->title
->canExist();
194 return $this->getContext()->canUseWikiPage();
199 * Set the WikiPage object
204 public function setWikiPage( WikiPage
$p ) {
205 $this->wikipage
= $p;
209 * Get the WikiPage object.
210 * May throw an exception if there's no Title object set or the Title object
211 * belongs to a special namespace that doesn't have WikiPage, so use first
212 * canUseWikiPage() to check whether this method can be called safely.
217 public function getWikiPage() {
218 if ( !is_null( $this->wikipage
) ) {
219 return $this->wikipage
;
221 return $this->getContext()->getWikiPage();
226 * Set the OutputPage object
228 * @param OutputPage $o
230 public function setOutput( OutputPage
$o ) {
235 * Get the OutputPage object
239 public function getOutput() {
240 if ( !is_null( $this->output
) ) {
241 return $this->output
;
243 return $this->getContext()->getOutput();
248 * Set the User object
252 public function setUser( User
$u ) {
257 * Get the User object
261 public function getUser() {
262 if ( !is_null( $this->user
) ) {
265 return $this->getContext()->getUser();
270 * Set the Language object
272 * @param Language|string $l Language instance or language code
273 * @throws MWException
276 public function setLanguage( $l ) {
277 if ( $l instanceof Language
) {
279 } elseif ( is_string( $l ) ) {
280 $l = RequestContext
::sanitizeLangCode( $l );
281 $obj = Language
::factory( $l );
284 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
289 * Get the Language object
294 public function getLanguage() {
295 if ( !is_null( $this->lang
) ) {
298 return $this->getContext()->getLanguage();
303 * Set the Skin object
307 public function setSkin( Skin
$s ) {
308 $this->skin
= clone $s;
309 $this->skin
->setContext( $this );
313 * Get the Skin object
317 public function getSkin() {
318 if ( !is_null( $this->skin
) ) {
321 return $this->getContext()->getSkin();
326 * Get a message using the current context.
328 * This can't just inherit from ContextSource, since then
329 * it would set only the original context, and not take
330 * into account any changes.
332 * @param mixed $args,... Arguments to wfMessage
335 public function msg() {
336 $args = func_get_args();
338 return call_user_func_array( 'wfMessage', $args )->setContext( $this );