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
21 use MediaWiki\MediaWikiServices
;
24 * An IContextSource implementation which will inherit context from another source
25 * but allow individual pieces of context to be changed locally
26 * eg: A ContextSource that can inherit from the main RequestContext but have
27 * a different Title instance set on it.
30 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 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
110 * @return IBufferingStatsdDataFactory
112 public function getStats() {
113 return MediaWikiServices
::getInstance()->getStatsdDataFactory();
117 * Get the timing object
121 public function getTiming() {
122 if ( !is_null( $this->timing
) ) {
123 return $this->timing
;
125 return $this->getContext()->getTiming();
130 * Set the WebRequest object
132 * @param WebRequest $r
134 public function setRequest( WebRequest
$r ) {
139 * Get the WebRequest object
143 public function getRequest() {
144 if ( !is_null( $this->request
) ) {
145 return $this->request
;
147 return $this->getContext()->getRequest();
152 * Set the Title object
156 public function setTitle( Title
$t ) {
161 * Get the Title object
165 public function getTitle() {
166 if ( !is_null( $this->title
) ) {
169 return $this->getContext()->getTitle();
174 * Check whether a WikiPage object can be get with getWikiPage().
175 * Callers should expect that an exception is thrown from getWikiPage()
176 * if this method returns false.
181 public function canUseWikiPage() {
182 if ( $this->wikipage
!== null ) {
184 } elseif ( $this->title
!== null ) {
185 return $this->title
->canExist();
187 return $this->getContext()->canUseWikiPage();
192 * Set the WikiPage object
197 public function setWikiPage( WikiPage
$p ) {
198 $this->wikipage
= $p;
202 * Get the WikiPage object.
203 * May throw an exception if there's no Title object set or the Title object
204 * belongs to a special namespace that doesn't have WikiPage, so use first
205 * canUseWikiPage() to check whether this method can be called safely.
210 public function getWikiPage() {
211 if ( !is_null( $this->wikipage
) ) {
212 return $this->wikipage
;
214 return $this->getContext()->getWikiPage();
219 * Set the OutputPage object
221 * @param OutputPage $o
223 public function setOutput( OutputPage
$o ) {
228 * Get the OutputPage object
232 public function getOutput() {
233 if ( !is_null( $this->output
) ) {
234 return $this->output
;
236 return $this->getContext()->getOutput();
241 * Set the User object
245 public function setUser( User
$u ) {
250 * Get the User object
254 public function getUser() {
255 if ( !is_null( $this->user
) ) {
258 return $this->getContext()->getUser();
263 * Set the Language object
265 * @param Language|string $l Language instance or language code
266 * @throws MWException
269 public function setLanguage( $l ) {
270 if ( $l instanceof Language
) {
272 } elseif ( is_string( $l ) ) {
273 $l = RequestContext
::sanitizeLangCode( $l );
274 $obj = Language
::factory( $l );
277 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
282 * Get the Language object
287 public function getLanguage() {
288 if ( !is_null( $this->lang
) ) {
291 return $this->getContext()->getLanguage();
296 * Set the Skin object
300 public function setSkin( Skin
$s ) {
301 $this->skin
= clone $s;
302 $this->skin
->setContext( $this );
306 * Get the Skin object
310 public function getSkin() {
311 if ( !is_null( $this->skin
) ) {
314 return $this->getContext()->getSkin();
319 * Get a message using the current context.
321 * This can't just inherit from ContextSource, since then
322 * it would set only the original context, and not take
323 * into account any changes.
325 * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
326 * or a MessageSpecifier.
327 * @param mixed $args,... Arguments to wfMessage
330 public function msg( $key ) {
331 $args = func_get_args();
333 return call_user_func_array( 'wfMessage', $args )->setContext( $this );