]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/context/ContextSource.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / context / ContextSource.php
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.
7  *
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.
12  *
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
17  *
18  * @author Happy-melon
19  * @file
20  */
21 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
22 use MediaWiki\MediaWikiServices;
23
24 /**
25  * The simplest way of implementing IContextSource is to hold a RequestContext as a
26  * member variable and provide accessors to it.
27  *
28  * @since 1.18
29  */
30 abstract class ContextSource implements IContextSource {
31         /**
32          * @var IContextSource
33          */
34         private $context;
35
36         /**
37          * Get the base IContextSource object
38          * @since 1.18
39          * @return IContextSource
40          */
41         public function getContext() {
42                 if ( $this->context === null ) {
43                         $class = static::class;
44                         wfDebug( __METHOD__ . " ($class): called and \$context is null. " .
45                                 "Using RequestContext::getMain() for sanity\n" );
46                         $this->context = RequestContext::getMain();
47                 }
48
49                 return $this->context;
50         }
51
52         /**
53          * Set the IContextSource object
54          *
55          * @since 1.18
56          * @param IContextSource $context
57          */
58         public function setContext( IContextSource $context ) {
59                 $this->context = $context;
60         }
61
62         /**
63          * Get the Config object
64          *
65          * @since 1.23
66          * @return Config
67          */
68         public function getConfig() {
69                 return $this->getContext()->getConfig();
70         }
71
72         /**
73          * Get the WebRequest object
74          *
75          * @since 1.18
76          * @return WebRequest
77          */
78         public function getRequest() {
79                 return $this->getContext()->getRequest();
80         }
81
82         /**
83          * Get the Title object
84          *
85          * @since 1.18
86          * @return Title|null
87          */
88         public function getTitle() {
89                 return $this->getContext()->getTitle();
90         }
91
92         /**
93          * Check whether a WikiPage object can be get with getWikiPage().
94          * Callers should expect that an exception is thrown from getWikiPage()
95          * if this method returns false.
96          *
97          * @since 1.19
98          * @return bool
99          */
100         public function canUseWikiPage() {
101                 return $this->getContext()->canUseWikiPage();
102         }
103
104         /**
105          * Get the WikiPage object.
106          * May throw an exception if there's no Title object set or the Title object
107          * belongs to a special namespace that doesn't have WikiPage, so use first
108          * canUseWikiPage() to check whether this method can be called safely.
109          *
110          * @since 1.19
111          * @return WikiPage
112          */
113         public function getWikiPage() {
114                 return $this->getContext()->getWikiPage();
115         }
116
117         /**
118          * Get the OutputPage object
119          *
120          * @since 1.18
121          * @return OutputPage
122          */
123         public function getOutput() {
124                 return $this->getContext()->getOutput();
125         }
126
127         /**
128          * Get the User object
129          *
130          * @since 1.18
131          * @return User
132          */
133         public function getUser() {
134                 return $this->getContext()->getUser();
135         }
136
137         /**
138          * Get the Language object
139          *
140          * @since 1.19
141          * @return Language
142          */
143         public function getLanguage() {
144                 return $this->getContext()->getLanguage();
145         }
146
147         /**
148          * Get the Skin object
149          *
150          * @since 1.18
151          * @return Skin
152          */
153         public function getSkin() {
154                 return $this->getContext()->getSkin();
155         }
156
157         /**
158          * Get the Timing object
159          *
160          * @since 1.27
161          * @return Timing
162          */
163         public function getTiming() {
164                 return $this->getContext()->getTiming();
165         }
166
167         /**
168          * Get the Stats object
169          *
170          * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
171          *
172          * @since 1.25
173          * @return IBufferingStatsdDataFactory
174          */
175         public function getStats() {
176                 return MediaWikiServices::getInstance()->getStatsdDataFactory();
177         }
178
179         /**
180          * Get a Message object with context set
181          * Parameters are the same as wfMessage()
182          *
183          * @since 1.18
184          * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
185          *   or a MessageSpecifier.
186          * @param mixed $args,...
187          * @return Message
188          */
189         public function msg( $key /* $args */ ) {
190                 $args = func_get_args();
191
192                 return call_user_func_array( [ $this->getContext(), 'msg' ], $args );
193         }
194
195         /**
196          * Export the resolved user IP, HTTP headers, user ID, and session ID.
197          * The result will be reasonably sized to allow for serialization.
198          *
199          * @return array
200          * @since 1.21
201          */
202         public function exportSession() {
203                 return $this->getContext()->exportSession();
204         }
205 }