Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / context / ContextSource.php
blob829dd73f0ba26b59bac1280ccf79f19b6838a174
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 * @author Happy-melon
19 * @file
21 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
23 /**
24 * The simplest way of implementing IContextSource is to hold a RequestContext as a
25 * member variable and provide accessors to it.
27 * @since 1.18
29 abstract class ContextSource implements IContextSource {
30 /**
31 * @var IContextSource
33 private $context;
35 /**
36 * Get the base IContextSource object
37 * @since 1.18
38 * @return IContextSource
40 public function getContext() {
41 if ( $this->context === null ) {
42 $class = get_class( $this );
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;
51 /**
52 * Set the IContextSource object
54 * @since 1.18
55 * @param IContextSource $context
57 public function setContext( IContextSource $context ) {
58 $this->context = $context;
61 /**
62 * Get the Config object
64 * @since 1.23
65 * @return Config
67 public function getConfig() {
68 return $this->getContext()->getConfig();
71 /**
72 * Get the WebRequest object
74 * @since 1.18
75 * @return WebRequest
77 public function getRequest() {
78 return $this->getContext()->getRequest();
81 /**
82 * Get the Title object
84 * @since 1.18
85 * @return Title|null
87 public function getTitle() {
88 return $this->getContext()->getTitle();
91 /**
92 * Check whether a WikiPage object can be get with getWikiPage().
93 * Callers should expect that an exception is thrown from getWikiPage()
94 * if this method returns false.
96 * @since 1.19
97 * @return bool
99 public function canUseWikiPage() {
100 return $this->getContext()->canUseWikiPage();
104 * Get the WikiPage object.
105 * May throw an exception if there's no Title object set or the Title object
106 * belongs to a special namespace that doesn't have WikiPage, so use first
107 * canUseWikiPage() to check whether this method can be called safely.
109 * @since 1.19
110 * @return WikiPage
112 public function getWikiPage() {
113 return $this->getContext()->getWikiPage();
117 * Get the OutputPage object
119 * @since 1.18
120 * @return OutputPage
122 public function getOutput() {
123 return $this->getContext()->getOutput();
127 * Get the User object
129 * @since 1.18
130 * @return User
132 public function getUser() {
133 return $this->getContext()->getUser();
137 * Get the Language object
139 * @since 1.19
140 * @return Language
142 public function getLanguage() {
143 return $this->getContext()->getLanguage();
147 * Get the Skin object
149 * @since 1.18
150 * @return Skin
152 public function getSkin() {
153 return $this->getContext()->getSkin();
157 * Get the Timing object
159 * @since 1.27
160 * @return Timing
162 public function getTiming() {
163 return $this->getContext()->getTiming();
167 * Get the Stats object
169 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
171 * @since 1.25
172 * @return StatsdDataFactory
174 public function getStats() {
175 return $this->getContext()->getStats();
179 * Get a Message object with context set
180 * Parameters are the same as wfMessage()
182 * @since 1.18
183 * @param mixed ...
184 * @return Message
186 public function msg( /* $args */ ) {
187 $args = func_get_args();
189 return call_user_func_array( [ $this->getContext(), 'msg' ], $args );
193 * Export the resolved user IP, HTTP headers, user ID, and session ID.
194 * The result will be reasonably sized to allow for serialization.
196 * @return array
197 * @since 1.21
199 public function exportSession() {
200 return $this->getContext()->exportSession();