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 namespace MediaWiki\Context
;
23 use MediaWiki\Config\Config
;
24 use MediaWiki\Language\Language
;
25 use MediaWiki\Message\Message
;
26 use MediaWiki\Output\OutputPage
;
27 use MediaWiki\Permissions\Authority
;
28 use MediaWiki\Request\WebRequest
;
29 use MediaWiki\Session\CsrfTokenSet
;
30 use MediaWiki\Title\Title
;
31 use MediaWiki\User\User
;
34 use Wikimedia\Bcp47Code\Bcp47Code
;
35 use Wikimedia\Message\MessageParam
;
36 use Wikimedia\Message\MessageSpecifier
;
37 use Wikimedia\NonSerializable\NonSerializableTrait
;
41 * The simplest way of implementing IContextSource is to hold a RequestContext as a
42 * member variable and provide accessors to it.
48 abstract class ContextSource
implements IContextSource
{
49 use NonSerializableTrait
;
57 * Get the base IContextSource object
60 * @return IContextSource
62 public function getContext() {
63 if ( $this->context
=== null ) {
64 $class = static::class;
65 wfDebug( __METHOD__
. " ($class): called and \$context is null. " .
66 "Using RequestContext::getMain()" );
67 $this->context
= RequestContext
::getMain();
70 return $this->context
;
76 * @param IContextSource $context
78 public function setContext( IContextSource
$context ) {
79 $this->context
= $context;
87 public function getConfig() {
88 return $this->getContext()->getConfig();
96 public function getRequest() {
97 return $this->getContext()->getRequest();
102 * @stable to override
105 public function getTitle() {
106 return $this->getContext()->getTitle();
110 * Check whether a WikiPage object can be get with getWikiPage().
111 * Callers should expect that an exception is thrown from getWikiPage()
112 * if this method returns false.
115 * @stable to override
118 public function canUseWikiPage() {
119 return $this->getContext()->canUseWikiPage();
123 * Get the WikiPage object.
124 * May throw an exception if there's no Title object set or the Title object
125 * belongs to a special namespace that doesn't have WikiPage, so use first
126 * canUseWikiPage() to check whether this method can be called safely.
129 * @stable to override
132 public function getWikiPage() {
133 return $this->getContext()->getWikiPage();
137 * Get the action name for the current web request.
140 * @stable to override
143 public function getActionName(): string {
144 return $this->getContext()->getActionName();
149 * @stable to override
152 public function getOutput() {
153 return $this->getContext()->getOutput();
157 * @stable to override
159 * @stable to override
162 public function getUser() {
163 return $this->getContext()->getUser();
170 public function getAuthority(): Authority
{
171 return $this->getContext()->getAuthority();
176 * @stable to override
179 public function getLanguage() {
180 return $this->getContext()->getLanguage();
185 * @stable to override
186 * @note When overriding, keep consistent with getLanguage()!
189 public function getLanguageCode(): Bcp47Code
{
190 return $this->getLanguage();
195 * @stable to override
198 public function getSkin() {
199 return $this->getContext()->getSkin();
204 * @stable to override
207 public function getTiming() {
208 return $this->getContext()->getTiming();
212 * Get a Message object with context set
213 * Parameters are the same as wfMessage()
216 * @stable to override
217 * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
218 * or a MessageSpecifier.
219 * @phpcs:ignore Generic.Files.LineLength
220 * @param MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float> ...$params
221 * See Message::params()
224 public function msg( $key, ...$params ) {
225 return $this->getContext()->msg( $key, ...$params );
229 * Export the resolved user IP, HTTP headers, user ID, and session ID.
230 * The result will be reasonably sized to allow for serialization.
233 * @stable to override
236 public function exportSession() {
237 return $this->getContext()->exportSession();
241 * Get a repository to obtain and match CSRF tokens.
243 * @return CsrfTokenSet
246 public function getCsrfTokenSet(): CsrfTokenSet
{
247 return $this->getContext()->getCsrfTokenSet();
251 /** @deprecated class alias since 1.42 */
252 class_alias( ContextSource
::class, 'ContextSource' );