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
21 use MediaWiki\MediaWikiServices
;
24 * The simplest way of implementing IContextSource is to hold a RequestContext as a
25 * member variable and provide accessors to it.
29 abstract class ContextSource
implements IContextSource
{
36 * Get the base IContextSource object
38 * @return IContextSource
40 public function getContext() {
41 if ( $this->context
=== null ) {
42 $class = static::class;
43 wfDebug( __METHOD__
. " ($class): called and \$context is null. " .
44 "Using RequestContext::getMain() for sanity\n" );
45 $this->context
= RequestContext
::getMain();
48 return $this->context
;
53 * @param IContextSource $context
55 public function setContext( IContextSource
$context ) {
56 $this->context
= $context;
63 public function getConfig() {
64 return $this->getContext()->getConfig();
71 public function getRequest() {
72 return $this->getContext()->getRequest();
79 public function getTitle() {
80 return $this->getContext()->getTitle();
84 * Check whether a WikiPage object can be get with getWikiPage().
85 * Callers should expect that an exception is thrown from getWikiPage()
86 * if this method returns false.
91 public function canUseWikiPage() {
92 return $this->getContext()->canUseWikiPage();
96 * Get the WikiPage object.
97 * May throw an exception if there's no Title object set or the Title object
98 * belongs to a special namespace that doesn't have WikiPage, so use first
99 * canUseWikiPage() to check whether this method can be called safely.
104 public function getWikiPage() {
105 return $this->getContext()->getWikiPage();
112 public function getOutput() {
113 return $this->getContext()->getOutput();
120 public function getUser() {
121 return $this->getContext()->getUser();
128 public function getLanguage() {
129 return $this->getContext()->getLanguage();
136 public function getSkin() {
137 return $this->getContext()->getSkin();
144 public function getTiming() {
145 return $this->getContext()->getTiming();
149 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
152 * @return IBufferingStatsdDataFactory
154 public function getStats() {
155 return MediaWikiServices
::getInstance()->getStatsdDataFactory();
159 * Get a Message object with context set
160 * Parameters are the same as wfMessage()
163 * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
164 * or a MessageSpecifier.
165 * @param mixed $args,...
168 public function msg( $key /* $args */ ) {
169 $args = func_get_args();
171 return $this->getContext()->msg( ...$args );
175 * Export the resolved user IP, HTTP headers, user ID, and session ID.
176 * The result will be reasonably sized to allow for serialization.
181 public function exportSession() {
182 return $this->getContext()->exportSession();