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
{
78 * @param IContextSource $context Context to inherit from
80 public function __construct( IContextSource
$context ) {
81 $this->setContext( $context );
85 * Set the SiteConfiguration object
89 public function setConfig( Config
$s ) {
94 * Get the Config object
98 public function getConfig() {
99 if ( !is_null( $this->config
) ) {
100 return $this->config
;
102 return $this->getContext()->getConfig();
107 * Get the stats object
109 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
111 * @return IBufferingStatsdDataFactory
113 public function getStats() {
114 return MediaWikiServices
::getInstance()->getStatsdDataFactory();
118 * Get the timing object
122 public function getTiming() {
123 if ( !is_null( $this->timing
) ) {
124 return $this->timing
;
126 return $this->getContext()->getTiming();
131 * Set the WebRequest object
133 * @param WebRequest $r
135 public function setRequest( WebRequest
$r ) {
140 * Get the WebRequest object
144 public function getRequest() {
145 if ( !is_null( $this->request
) ) {
146 return $this->request
;
148 return $this->getContext()->getRequest();
153 * Set the Title object
157 public function setTitle( Title
$t ) {
162 * Get the Title object
166 public function getTitle() {
167 if ( !is_null( $this->title
) ) {
170 return $this->getContext()->getTitle();
175 * Check whether a WikiPage object can be get with getWikiPage().
176 * Callers should expect that an exception is thrown from getWikiPage()
177 * if this method returns false.
182 public function canUseWikiPage() {
183 if ( $this->wikipage
!== null ) {
185 } elseif ( $this->title
!== null ) {
186 return $this->title
->canExist();
188 return $this->getContext()->canUseWikiPage();
193 * Set the WikiPage object
198 public function setWikiPage( WikiPage
$p ) {
199 $this->wikipage
= $p;
203 * Get the WikiPage object.
204 * May throw an exception if there's no Title object set or the Title object
205 * belongs to a special namespace that doesn't have WikiPage, so use first
206 * canUseWikiPage() to check whether this method can be called safely.
211 public function getWikiPage() {
212 if ( !is_null( $this->wikipage
) ) {
213 return $this->wikipage
;
215 return $this->getContext()->getWikiPage();
220 * Set the OutputPage object
222 * @param OutputPage $o
224 public function setOutput( OutputPage
$o ) {
229 * Get the OutputPage object
233 public function getOutput() {
234 if ( !is_null( $this->output
) ) {
235 return $this->output
;
237 return $this->getContext()->getOutput();
242 * Set the User object
246 public function setUser( User
$u ) {
251 * Get the User object
255 public function getUser() {
256 if ( !is_null( $this->user
) ) {
259 return $this->getContext()->getUser();
264 * Set the Language object
266 * @param Language|string $l Language instance or language code
267 * @throws MWException
270 public function setLanguage( $l ) {
271 if ( $l instanceof Language
) {
273 } elseif ( is_string( $l ) ) {
274 $l = RequestContext
::sanitizeLangCode( $l );
275 $obj = Language
::factory( $l );
278 throw new MWException( __METHOD__
. " was passed an invalid type of data." );
283 * Get the Language object
288 public function getLanguage() {
289 if ( !is_null( $this->lang
) ) {
292 return $this->getContext()->getLanguage();
297 * Set the Skin object
301 public function setSkin( Skin
$s ) {
302 $this->skin
= clone $s;
303 $this->skin
->setContext( $this );
307 * Get the Skin object
311 public function getSkin() {
312 if ( !is_null( $this->skin
) ) {
315 return $this->getContext()->getSkin();
320 * Get a message using the current context.
322 * This can't just inherit from ContextSource, since then
323 * it would set only the original context, and not take
324 * into account any changes.
326 * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
327 * or a MessageSpecifier.
328 * @param mixed $args,... Arguments to wfMessage
331 public function msg( $key ) {
332 $args = func_get_args();
334 return call_user_func_array( 'wfMessage', $args )->setContext( $this );