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 Liuggio\StatsdClient\Factory\StatsdDataFactory
;
22 use MediaWiki\MediaWikiServices
;
25 * An IContextSource implementation which will inherit context from another source
26 * but allow individual pieces of context to be changed locally
27 * eg: A ContextSource that can inherit from the main RequestContext but have
28 * a different Title instance set on it.
31 class DerivativeContext
extends ContextSource
implements MutableContext
{
79 * @param IContextSource $context Context to inherit from
81 public function __construct( IContextSource
$context ) {
82 $this->setContext( $context );
86 * Set the SiteConfiguration object
90 public function setConfig( Config
$s ) {
95 * Get the Config object
99 public function getConfig() {
100 if ( !is_null( $this->config
) ) {
101 return $this->config
;
103 return $this->getContext()->getConfig();
108 * Get the stats object
110 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
112 * @return StatsdDataFactory
114 public function getStats() {
115 return MediaWikiServices
::getInstance()->getStatsdDataFactory();
119 * Get the timing object
123 public function getTiming() {
124 if ( !is_null( $this->timing
) ) {
125 return $this->timing
;
127 return $this->getContext()->getTiming();
132 * Set the WebRequest object
134 * @param WebRequest $r
136 public function setRequest( WebRequest
$r ) {
141 * Get the WebRequest object
145 public function getRequest() {
146 if ( !is_null( $this->request
) ) {
147 return $this->request
;
149 return $this->getContext()->getRequest();
154 * Set the Title object
158 public function setTitle( Title
$t ) {
163 * Get the Title object
167 public function getTitle() {
168 if ( !is_null( $this->title
) ) {
171 return $this->getContext()->getTitle();
176 * Check whether a WikiPage object can be get with getWikiPage().
177 * Callers should expect that an exception is thrown from getWikiPage()
178 * if this method returns false.
183 public function canUseWikiPage() {
184 if ( $this->wikipage
!== null ) {
186 } elseif ( $this->title
!== null ) {
187 return $this->title
->canExist();
189 return $this->getContext()->canUseWikiPage();
194 * Set the WikiPage object
199 public function setWikiPage( WikiPage
$p ) {
200 $this->wikipage
= $p;
204 * Get the WikiPage object.
205 * May throw an exception if there's no Title object set or the Title object
206 * belongs to a special namespace that doesn't have WikiPage, so use first
207 * canUseWikiPage() to check whether this method can be called safely.
212 public function getWikiPage() {
213 if ( !is_null( $this->wikipage
) ) {
214 return $this->wikipage
;
216 return $this->getContext()->getWikiPage();
221 * Set the OutputPage object
223 * @param OutputPage $o
225 public function setOutput( OutputPage
$o ) {
230 * Get the OutputPage object
234 public function getOutput() {
235 if ( !is_null( $this->output
) ) {
236 return $this->output
;
238 return $this->getContext()->getOutput();
243 * Set the User object
247 public function setUser( User
$u ) {
252 * Get the User object
256 public function getUser() {
257 if ( !is_null( $this->user
) ) {
260 return $this->getContext()->getUser();
265 * Set the Language object
267 * @param Language|string $l Language instance or language code
268 * @throws MWException
271 public function setLanguage( $l ) {
272 if ( $l instanceof Language
) {
274 } elseif ( is_string( $l ) ) {
275 $l = RequestContext
::sanitizeLangCode( $l );
276 $obj = Language
::factory( $l );
279 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
284 * Get the Language object
289 public function getLanguage() {
290 if ( !is_null( $this->lang
) ) {
293 return $this->getContext()->getLanguage();
298 * Set the Skin object
302 public function setSkin( Skin
$s ) {
303 $this->skin
= clone $s;
304 $this->skin
->setContext( $this );
308 * Get the Skin object
312 public function getSkin() {
313 if ( !is_null( $this->skin
) ) {
316 return $this->getContext()->getSkin();
321 * Get a message using the current context.
323 * This can't just inherit from ContextSource, since then
324 * it would set only the original context, and not take
325 * into account any changes.
327 * @param mixed $args,... Arguments to wfMessage
330 public function msg() {
331 $args = func_get_args();
333 return call_user_func_array( 'wfMessage', $args )->setContext( $this );