e4340ce2db16b2b5fbee8b81a420cfeeaf0ef177
[lhc/web/wiklou.git] / includes / context / DerivativeContext.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 Daniel Friesen
19 * @file
20 */
21 use MediaWiki\MediaWikiServices;
22
23 /**
24 * An IContextSource implementation which will inherit context from another source
25 * but allow individual pieces of context to be changed locally
26 * eg: A ContextSource that can inherit from the main RequestContext but have
27 * a different Title instance set on it.
28 * @since 1.19
29 */
30 class DerivativeContext extends ContextSource implements MutableContext {
31 /**
32 * @var WebRequest
33 */
34 private $request;
35
36 /**
37 * @var Title
38 */
39 private $title;
40
41 /**
42 * @var WikiPage
43 */
44 private $wikipage;
45
46 /**
47 * @var OutputPage
48 */
49 private $output;
50
51 /**
52 * @var User
53 */
54 private $user;
55
56 /**
57 * @var Language
58 */
59 private $lang;
60
61 /**
62 * @var Skin
63 */
64 private $skin;
65
66 /**
67 * @var Config
68 */
69 private $config;
70
71 /**
72 * @var Timing
73 */
74 private $timing;
75
76 /**
77 * @param IContextSource $context Context to inherit from
78 */
79 public function __construct( IContextSource $context ) {
80 $this->setContext( $context );
81 }
82
83 /**
84 * @param Config $config
85 */
86 public function setConfig( Config $config ) {
87 $this->config = $config;
88 }
89
90 /**
91 * @return Config
92 */
93 public function getConfig() {
94 return $this->config ?: $this->getContext()->getConfig();
95 }
96
97 /**
98 * @deprecated since 1.27 use a StatsdDataFactory from MediaWikiServices (preferably injected)
99 *
100 * @return IBufferingStatsdDataFactory
101 */
102 public function getStats() {
103 return MediaWikiServices::getInstance()->getStatsdDataFactory();
104 }
105
106 /**
107 * @return Timing
108 */
109 public function getTiming() {
110 return $this->timing ?: $this->getContext()->getTiming();
111 }
112
113 /**
114 * @param WebRequest $request
115 */
116 public function setRequest( WebRequest $request ) {
117 $this->request = $request;
118 }
119
120 /**
121 * @return WebRequest
122 */
123 public function getRequest() {
124 return $this->request ?: $this->getContext()->getRequest();
125 }
126
127 /**
128 * @param Title $title
129 */
130 public function setTitle( Title $title ) {
131 $this->title = $title;
132 }
133
134 /**
135 * @return Title|null
136 */
137 public function getTitle() {
138 return $this->title ?: $this->getContext()->getTitle();
139 }
140
141 /**
142 * Check whether a WikiPage object can be get with getWikiPage().
143 * Callers should expect that an exception is thrown from getWikiPage()
144 * if this method returns false.
145 *
146 * @since 1.19
147 * @return bool
148 */
149 public function canUseWikiPage() {
150 if ( $this->wikipage !== null ) {
151 return true;
152 }
153
154 if ( $this->title !== null ) {
155 return $this->title->canExist();
156 }
157
158 return $this->getContext()->canUseWikiPage();
159 }
160
161 /**
162 * @since 1.19
163 * @param WikiPage $wikiPage
164 */
165 public function setWikiPage( WikiPage $wikiPage ) {
166 $this->wikipage = $wikiPage;
167 }
168
169 /**
170 * Get the WikiPage object.
171 * May throw an exception if there's no Title object set or the Title object
172 * belongs to a special namespace that doesn't have WikiPage, so use first
173 * canUseWikiPage() to check whether this method can be called safely.
174 *
175 * @since 1.19
176 * @return WikiPage
177 */
178 public function getWikiPage() {
179 return $this->wikipage ?: $this->getContext()->getWikiPage();
180 }
181
182 /**
183 * @param OutputPage $output
184 */
185 public function setOutput( OutputPage $output ) {
186 $this->output = $output;
187 }
188
189 /**
190 * @return OutputPage
191 */
192 public function getOutput() {
193 return $this->output ?: $this->getContext()->getOutput();
194 }
195
196 /**
197 * @param User $user
198 */
199 public function setUser( User $user ) {
200 $this->user = $user;
201 }
202
203 /**
204 * @return User
205 */
206 public function getUser() {
207 return $this->user ?: $this->getContext()->getUser();
208 }
209
210 /**
211 * @param Language|string $language Language instance or language code
212 * @throws MWException
213 * @since 1.19
214 */
215 public function setLanguage( $language ) {
216 if ( $language instanceof Language ) {
217 $this->lang = $language;
218 } elseif ( is_string( $language ) ) {
219 $language = RequestContext::sanitizeLangCode( $language );
220 $obj = Language::factory( $language );
221 $this->lang = $obj;
222 } else {
223 throw new MWException( __METHOD__ . " was passed an invalid type of data." );
224 }
225 }
226
227 /**
228 * @return Language
229 * @since 1.19
230 */
231 public function getLanguage() {
232 return $this->lang ?: $this->getContext()->getLanguage();
233 }
234
235 /**
236 * @param Skin $skin
237 */
238 public function setSkin( Skin $skin ) {
239 $this->skin = clone $skin;
240 $this->skin->setContext( $this );
241 }
242
243 /**
244 * @return Skin
245 */
246 public function getSkin() {
247 return $this->skin ?: $this->getContext()->getSkin();
248 }
249
250 /**
251 * Get a message using the current context.
252 *
253 * This can't just inherit from ContextSource, since then
254 * it would set only the original context, and not take
255 * into account any changes.
256 *
257 * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
258 * or a MessageSpecifier.
259 * @param mixed $args,... Arguments to wfMessage
260 * @suppress PhanCommentParamWithoutRealParam HHVM bug T228695#5450847
261 * @return Message
262 */
263 public function msg( $key ) {
264 $args = func_get_args();
265
266 // phpcs:ignore MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage
267 return wfMessage( ...$args )->setContext( $this );
268 }
269 }