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 * @param Config $config
86 public function setConfig( Config
$config ) {
87 $this->config
= $config;
93 public function getConfig() {
94 if ( !is_null( $this->config
) ) {
97 return $this->getContext()->getConfig();
102 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
104 * @return IBufferingStatsdDataFactory
106 public function getStats() {
107 return MediaWikiServices
::getInstance()->getStatsdDataFactory();
113 public function getTiming() {
114 if ( !is_null( $this->timing
) ) {
115 return $this->timing
;
117 return $this->getContext()->getTiming();
122 * @param WebRequest $request
124 public function setRequest( WebRequest
$request ) {
125 $this->request
= $request;
131 public function getRequest() {
132 if ( !is_null( $this->request
) ) {
133 return $this->request
;
135 return $this->getContext()->getRequest();
140 * @param Title $title
142 public function setTitle( Title
$title ) {
143 $this->title
= $title;
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();
177 * @param WikiPage $wikiPage
179 public function setWikiPage( WikiPage
$wikiPage ) {
180 $this->wikipage
= $wikiPage;
184 * Get the WikiPage object.
185 * May throw an exception if there's no Title object set or the Title object
186 * belongs to a special namespace that doesn't have WikiPage, so use first
187 * canUseWikiPage() to check whether this method can be called safely.
192 public function getWikiPage() {
193 if ( !is_null( $this->wikipage
) ) {
194 return $this->wikipage
;
196 return $this->getContext()->getWikiPage();
201 * @param OutputPage $output
203 public function setOutput( OutputPage
$output ) {
204 $this->output
= $output;
210 public function getOutput() {
211 if ( !is_null( $this->output
) ) {
212 return $this->output
;
214 return $this->getContext()->getOutput();
221 public function setUser( User
$user ) {
228 public function getUser() {
229 if ( !is_null( $this->user
) ) {
232 return $this->getContext()->getUser();
237 * @param Language|string $language Language instance or language code
238 * @throws MWException
241 public function setLanguage( $language ) {
242 if ( $language instanceof Language
) {
243 $this->lang
= $language;
244 } elseif ( is_string( $language ) ) {
245 $language = RequestContext
::sanitizeLangCode( $language );
246 $obj = Language
::factory( $language );
249 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
257 public function getLanguage() {
258 if ( !is_null( $this->lang
) ) {
261 return $this->getContext()->getLanguage();
268 public function setSkin( Skin
$skin ) {
269 $this->skin
= clone $skin;
270 $this->skin
->setContext( $this );
276 public function getSkin() {
277 if ( !is_null( $this->skin
) ) {
280 return $this->getContext()->getSkin();
285 * Get a message using the current context.
287 * This can't just inherit from ContextSource, since then
288 * it would set only the original context, and not take
289 * into account any changes.
291 * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
292 * or a MessageSpecifier.
293 * @param mixed $args,... Arguments to wfMessage
296 public function msg( $key ) {
297 $args = func_get_args();
299 return call_user_func_array( 'wfMessage', $args )->setContext( $this );