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 return $this->config ?
: $this->getContext()->getConfig();
98 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
100 * @return IBufferingStatsdDataFactory
102 public function getStats() {
103 return MediaWikiServices
::getInstance()->getStatsdDataFactory();
109 public function getTiming() {
110 return $this->timing ?
: $this->getContext()->getTiming();
114 * @param WebRequest $request
116 public function setRequest( WebRequest
$request ) {
117 $this->request
= $request;
123 public function getRequest() {
124 return $this->request ?
: $this->getContext()->getRequest();
128 * @param Title $title
130 public function setTitle( Title
$title ) {
131 $this->title
= $title;
137 public function getTitle() {
138 return $this->title ?
: $this->getContext()->getTitle();
142 * Check whether a WikiPage object can be get with getWikiPage().
143 * Callers should expect that an exception is thrown from getWikiPage()
144 * if this method returns false.
149 public function canUseWikiPage() {
150 if ( $this->wikipage
!== null ) {
154 if ( $this->title
!== null ) {
155 return $this->title
->canExist();
158 return $this->getContext()->canUseWikiPage();
163 * @param WikiPage $wikiPage
165 public function setWikiPage( WikiPage
$wikiPage ) {
166 $this->wikipage
= $wikiPage;
170 * Get the WikiPage object.
171 * May throw an exception if there's no Title object set or the Title object
172 * belongs to a special namespace that doesn't have WikiPage, so use first
173 * canUseWikiPage() to check whether this method can be called safely.
178 public function getWikiPage() {
179 return $this->wikipage ?
: $this->getContext()->getWikiPage();
183 * @param OutputPage $output
185 public function setOutput( OutputPage
$output ) {
186 $this->output
= $output;
192 public function getOutput() {
193 return $this->output ?
: $this->getContext()->getOutput();
199 public function setUser( User
$user ) {
206 public function getUser() {
207 return $this->user ?
: $this->getContext()->getUser();
211 * @param Language|string $language Language instance or language code
212 * @throws MWException
215 public function setLanguage( $language ) {
216 if ( $language instanceof Language
) {
217 $this->lang
= $language;
218 } elseif ( is_string( $language ) ) {
219 $language = RequestContext
::sanitizeLangCode( $language );
220 $obj = Language
::factory( $language );
223 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
231 public function getLanguage() {
232 return $this->lang ?
: $this->getContext()->getLanguage();
238 public function setSkin( Skin
$skin ) {
239 $this->skin
= clone $skin;
240 $this->skin
->setContext( $this );
246 public function getSkin() {
247 return $this->skin ?
: $this->getContext()->getSkin();
251 * Get a message using the current context.
253 * This can't just inherit from ContextSource, since then
254 * it would set only the original context, and not take
255 * into account any changes.
257 * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
258 * or a MessageSpecifier.
259 * @param mixed $args,... Arguments to wfMessage
260 * @suppress PhanCommentParamWithoutRealParam HHVM bug T228695#5450847
263 public function msg( $key ) {
264 $args = func_get_args();
266 // phpcs:ignore MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage
267 return wfMessage( ...$args )->setContext( $this );