Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / context / ContextSource.php
blob4f28433fa3d8beb0ae23f6a4d107b127b894f76a
1 <?php
2 /**
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 * @file
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;
32 use Skin;
33 use Timing;
34 use Wikimedia\Bcp47Code\Bcp47Code;
35 use Wikimedia\Message\MessageParam;
36 use Wikimedia\Message\MessageSpecifier;
37 use Wikimedia\NonSerializable\NonSerializableTrait;
38 use WikiPage;
40 /**
41 * The simplest way of implementing IContextSource is to hold a RequestContext as a
42 * member variable and provide accessors to it.
44 * @stable to extend
45 * @since 1.18
46 * @author Happy-melon
48 abstract class ContextSource implements IContextSource {
49 use NonSerializableTrait;
51 /**
52 * @var IContextSource
54 private $context;
56 /**
57 * Get the base IContextSource object
58 * @since 1.18
59 * @stable to override
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;
73 /**
74 * @since 1.18
75 * @stable to override
76 * @param IContextSource $context
78 public function setContext( IContextSource $context ) {
79 $this->context = $context;
82 /**
83 * @since 1.23
84 * @stable to override
85 * @return Config
87 public function getConfig() {
88 return $this->getContext()->getConfig();
91 /**
92 * @since 1.18
93 * @stable to override
94 * @return WebRequest
96 public function getRequest() {
97 return $this->getContext()->getRequest();
101 * @since 1.18
102 * @stable to override
103 * @return Title|null
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.
114 * @since 1.19
115 * @stable to override
116 * @return bool
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.
128 * @since 1.19
129 * @stable to override
130 * @return WikiPage
132 public function getWikiPage() {
133 return $this->getContext()->getWikiPage();
137 * Get the action name for the current web request.
139 * @since 1.38
140 * @stable to override
141 * @return string
143 public function getActionName(): string {
144 return $this->getContext()->getActionName();
148 * @since 1.18
149 * @stable to override
150 * @return OutputPage
152 public function getOutput() {
153 return $this->getContext()->getOutput();
157 * @stable to override
158 * @since 1.18
159 * @stable to override
160 * @return User
162 public function getUser() {
163 return $this->getContext()->getUser();
167 * @since 1.36
168 * @return Authority
170 public function getAuthority(): Authority {
171 return $this->getContext()->getAuthority();
175 * @since 1.19
176 * @stable to override
177 * @return Language
179 public function getLanguage() {
180 return $this->getContext()->getLanguage();
184 * @since 1.42
185 * @stable to override
186 * @note When overriding, keep consistent with getLanguage()!
187 * @return Bcp47Code
189 public function getLanguageCode(): Bcp47Code {
190 return $this->getLanguage();
194 * @since 1.18
195 * @stable to override
196 * @return Skin
198 public function getSkin() {
199 return $this->getContext()->getSkin();
203 * @since 1.27
204 * @stable to override
205 * @return Timing
207 public function getTiming() {
208 return $this->getContext()->getTiming();
212 * Get a Message object with context set
213 * Parameters are the same as wfMessage()
215 * @since 1.18
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()
222 * @return Message
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.
232 * @since 1.21
233 * @stable to override
234 * @return array
236 public function exportSession() {
237 return $this->getContext()->exportSession();
241 * Get a repository to obtain and match CSRF tokens.
243 * @return CsrfTokenSet
244 * @since 1.37
246 public function getCsrfTokenSet(): CsrfTokenSet {
247 return $this->getContext()->getCsrfTokenSet();
251 /** @deprecated class alias since 1.42 */
252 class_alias( ContextSource::class, 'ContextSource' );