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
{
77 * @param IContextSource $context Context to inherit from
79 public function __construct( IContextSource
$context ) {
80 $this->setContext( $context );
84 * Set the SiteConfiguration object
88 public function setConfig( Config
$s ) {
93 * Get the Config object
97 public function getConfig() {
98 if ( !is_null( $this->config
) ) {
101 return $this->getContext()->getConfig();
106 * Get the stats object
108 * @return BufferingStatsdDataFactory
110 public function getStats() {
111 if ( !is_null( $this->stats
) ) {
114 return $this->getContext()->getStats();
119 * Set the WebRequest object
121 * @param WebRequest $r
123 public function setRequest( WebRequest
$r ) {
128 * Get the WebRequest object
132 public function getRequest() {
133 if ( !is_null( $this->request
) ) {
134 return $this->request
;
136 return $this->getContext()->getRequest();
141 * Set the Title object
145 public function setTitle( Title
$t ) {
150 * Get the Title object
154 public function getTitle() {
155 if ( !is_null( $this->title
) ) {
158 return $this->getContext()->getTitle();
163 * Check whether a WikiPage object can be get with getWikiPage().
164 * Callers should expect that an exception is thrown from getWikiPage()
165 * if this method returns false.
170 public function canUseWikiPage() {
171 if ( $this->wikipage
!== null ) {
173 } elseif ( $this->title
!== null ) {
174 return $this->title
->canExist();
176 return $this->getContext()->canUseWikiPage();
181 * Set the WikiPage object
186 public function setWikiPage( WikiPage
$p ) {
187 $this->wikipage
= $p;
191 * Get the WikiPage object.
192 * May throw an exception if there's no Title object set or the Title object
193 * belongs to a special namespace that doesn't have WikiPage, so use first
194 * canUseWikiPage() to check whether this method can be called safely.
199 public function getWikiPage() {
200 if ( !is_null( $this->wikipage
) ) {
201 return $this->wikipage
;
203 return $this->getContext()->getWikiPage();
208 * Set the OutputPage object
210 * @param OutputPage $o
212 public function setOutput( OutputPage
$o ) {
217 * Get the OutputPage object
221 public function getOutput() {
222 if ( !is_null( $this->output
) ) {
223 return $this->output
;
225 return $this->getContext()->getOutput();
230 * Set the User object
234 public function setUser( User
$u ) {
239 * Get the User object
243 public function getUser() {
244 if ( !is_null( $this->user
) ) {
247 return $this->getContext()->getUser();
252 * Set the Language object
254 * @param Language|string $l Language instance or language code
255 * @throws MWException
258 public function setLanguage( $l ) {
259 if ( $l instanceof Language
) {
261 } elseif ( is_string( $l ) ) {
262 $l = RequestContext
::sanitizeLangCode( $l );
263 $obj = Language
::factory( $l );
266 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
271 * Get the Language object
276 public function getLanguage() {
277 if ( !is_null( $this->lang
) ) {
280 return $this->getContext()->getLanguage();
285 * Set the Skin object
289 public function setSkin( Skin
$s ) {
290 $this->skin
= clone $s;
291 $this->skin
->setContext( $this );
295 * Get the Skin object
299 public function getSkin() {
300 if ( !is_null( $this->skin
) ) {
303 return $this->getContext()->getSkin();
308 * Get a message using the current context.
310 * This can't just inherit from ContextSource, since then
311 * it would set only the original context, and not take
312 * into account any changes.
314 * @param mixed $args,... Arguments to wfMessage
317 public function msg() {
318 $args = func_get_args();
320 return call_user_func_array( 'wfMessage', $args )->setContext( $this );