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
{
72 * @param IContextSource $context Context to inherit from
74 public function __construct( IContextSource
$context ) {
75 $this->setContext( $context );
79 * Set the SiteConfiguration object
83 public function setConfig( Config
$s ) {
88 * Get the Config object
92 public function getConfig() {
93 if ( !is_null( $this->config
) ) {
96 return $this->getContext()->getConfig();
101 * Get the stats object
103 * @return BufferingStatsdDataFactory
105 public function getStats() {
106 if ( !is_null( $this->stats
) ) {
109 return $this->getContext()->getStats();
114 * Set the WebRequest object
116 * @param WebRequest $r
118 public function setRequest( WebRequest
$r ) {
123 * Get the WebRequest object
127 public function getRequest() {
128 if ( !is_null( $this->request
) ) {
129 return $this->request
;
131 return $this->getContext()->getRequest();
136 * Set the Title object
140 public function setTitle( Title
$t ) {
145 * Get the Title object
149 public function getTitle() {
150 if ( !is_null( $this->title
) ) {
153 return $this->getContext()->getTitle();
158 * Check whether a WikiPage object can be get with getWikiPage().
159 * Callers should expect that an exception is thrown from getWikiPage()
160 * if this method returns false.
165 public function canUseWikiPage() {
166 if ( $this->wikipage
!== null ) {
168 } elseif ( $this->title
!== null ) {
169 return $this->title
->canExist();
171 return $this->getContext()->canUseWikiPage();
176 * Set the WikiPage object
181 public function setWikiPage( WikiPage
$p ) {
182 $this->wikipage
= $p;
186 * Get the WikiPage object.
187 * May throw an exception if there's no Title object set or the Title object
188 * belongs to a special namespace that doesn't have WikiPage, so use first
189 * canUseWikiPage() to check whether this method can be called safely.
194 public function getWikiPage() {
195 if ( !is_null( $this->wikipage
) ) {
196 return $this->wikipage
;
198 return $this->getContext()->getWikiPage();
203 * Set the OutputPage object
205 * @param OutputPage $o
207 public function setOutput( OutputPage
$o ) {
212 * Get the OutputPage object
216 public function getOutput() {
217 if ( !is_null( $this->output
) ) {
218 return $this->output
;
220 return $this->getContext()->getOutput();
225 * Set the User object
229 public function setUser( User
$u ) {
234 * Get the User object
238 public function getUser() {
239 if ( !is_null( $this->user
) ) {
242 return $this->getContext()->getUser();
247 * Set the Language object
249 * @param Language|string $l Language instance or language code
250 * @throws MWException
253 public function setLanguage( $l ) {
254 if ( $l instanceof Language
) {
256 } elseif ( is_string( $l ) ) {
257 $l = RequestContext
::sanitizeLangCode( $l );
258 $obj = Language
::factory( $l );
261 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
266 * Get the Language object
271 public function getLanguage() {
272 if ( !is_null( $this->lang
) ) {
275 return $this->getContext()->getLanguage();
280 * Set the Skin object
284 public function setSkin( Skin
$s ) {
285 $this->skin
= clone $s;
286 $this->skin
->setContext( $this );
290 * Get the Skin object
294 public function getSkin() {
295 if ( !is_null( $this->skin
) ) {
298 return $this->getContext()->getSkin();
303 * Get a message using the current context.
305 * This can't just inherit from ContextSource, since then
306 * it would set only the original context, and not take
307 * into account any changes.
309 * @param mixed $args,... Arguments to wfMessage
312 public function msg() {
313 $args = func_get_args();
315 return call_user_func_array( 'wfMessage', $args )->setContext( $this );