Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.04% covered (warning)
66.04%
35 / 53
66.67% covered (warning)
66.67%
16 / 24
CRAP
0.00% covered (danger)
0.00%
0 / 1
DerivativeContext
67.31% covered (warning)
67.31%
35 / 52
66.67% covered (warning)
66.67%
16 / 24
111.65
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setConfig
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getConfig
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getTiming
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 setRequest
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRequest
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 setTitle
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getTitle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 canUseWikiPage
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 setWikiPage
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getWikiPage
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
4.59
 setActionName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActionName
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 setOutput
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOutput
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 setUser
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getUser
40.00% covered (danger)
40.00%
2 / 5
0.00% covered (danger)
0.00%
0 / 1
7.46
 setAuthority
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getAuthority
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 setLanguage
50.00% covered (danger)
50.00%
3 / 6
0.00% covered (danger)
0.00%
0 / 1
2.50
 getLanguage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 setSkin
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getSkin
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 msg
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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
22namespace MediaWiki\Context;
23
24use MediaWiki\Config\Config;
25use MediaWiki\Language\Language;
26use MediaWiki\MediaWikiServices;
27use MediaWiki\Message\Message;
28use MediaWiki\Output\OutputPage;
29use MediaWiki\Permissions\Authority;
30use MediaWiki\Request\WebRequest;
31use MediaWiki\Title\Title;
32use MediaWiki\User\User;
33use Skin;
34use Timing;
35use Wikimedia\Assert\Assert;
36use Wikimedia\Message\MessageParam;
37use Wikimedia\Message\MessageSpecifier;
38use WikiPage;
39
40/**
41 * An IContextSource implementation which will inherit context from another source
42 * but allow individual pieces of context to be changed locally
43 * eg: A ContextSource that can inherit from the main RequestContext but have
44 *     a different Title instance set on it.
45 * @newable
46 * @since 1.19
47 */
48class DerivativeContext extends ContextSource implements MutableContext {
49    /**
50     * @var WebRequest
51     */
52    private $request;
53
54    /**
55     * @var Title
56     */
57    private $title;
58
59    /**
60     * @var WikiPage
61     */
62    private $wikipage;
63
64    /**
65     * @var string|null|false
66     */
67    private $action = false;
68
69    /**
70     * @var OutputPage
71     */
72    private $output;
73
74    /**
75     * @var User|null
76     */
77    private $user;
78
79    /**
80     * @var Authority
81     */
82    private $authority;
83
84    /**
85     * @var Language
86     */
87    private $lang;
88
89    /**
90     * @var Skin
91     */
92    private $skin;
93
94    /**
95     * @var Config
96     */
97    private $config;
98
99    /**
100     * @var Timing
101     */
102    private $timing;
103
104    /**
105     * @stable to call
106     * @param IContextSource $context Context to inherit from
107     */
108    public function __construct( IContextSource $context ) {
109        $this->setContext( $context );
110    }
111
112    /**
113     * @param Config $config
114     */
115    public function setConfig( Config $config ) {
116        $this->config = $config;
117    }
118
119    /**
120     * @return Config
121     */
122    public function getConfig() {
123        return $this->config ?: $this->getContext()->getConfig();
124    }
125
126    /**
127     * @return Timing
128     */
129    public function getTiming() {
130        return $this->timing ?: $this->getContext()->getTiming();
131    }
132
133    /**
134     * @param WebRequest $request
135     */
136    public function setRequest( WebRequest $request ) {
137        $this->request = $request;
138    }
139
140    /**
141     * @return WebRequest
142     */
143    public function getRequest() {
144        return $this->request ?: $this->getContext()->getRequest();
145    }
146
147    /**
148     * @param Title $title
149     */
150    public function setTitle( Title $title ) {
151        $this->title = $title;
152        $this->action = null;
153    }
154
155    /**
156     * @return Title|null
157     */
158    public function getTitle() {
159        return $this->title ?: $this->getContext()->getTitle();
160    }
161
162    /**
163     * Check whether a WikiPage object can be get with getWikiPage().
164     * Callers should expect that an exception is thrown from getWikiPage()
165     * if this method returns false.
166     *
167     * @since 1.19
168     * @return bool
169     */
170    public function canUseWikiPage() {
171        if ( $this->wikipage !== null ) {
172            return true;
173        }
174
175        if ( $this->title !== null ) {
176            return $this->title->canExist();
177        }
178
179        return $this->getContext()->canUseWikiPage();
180    }
181
182    /**
183     * @since 1.19
184     * @param WikiPage $wikiPage
185     */
186    public function setWikiPage( WikiPage $wikiPage ) {
187        $pageTitle = $wikiPage->getTitle();
188        if ( !$this->title || !$pageTitle->equals( $this->title ) ) {
189            $this->setTitle( $pageTitle );
190        }
191        $this->wikipage = $wikiPage;
192        $this->action = null;
193    }
194
195    /**
196     * Get the WikiPage object.
197     * May throw an exception if there's no Title object set or the Title object
198     * belongs to a special namespace that doesn't have WikiPage, so use first
199     * canUseWikiPage() to check whether this method can be called safely.
200     *
201     * @since 1.19
202     * @return WikiPage
203     */
204    public function getWikiPage() {
205        if ( !$this->wikipage && $this->title ) {
206            $this->wikipage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $this->title );
207        }
208
209        return $this->wikipage ?: $this->getContext()->getWikiPage();
210    }
211
212    /**
213     * @since 1.38
214     * @param string $action
215     */
216    public function setActionName( string $action ): void {
217        $this->action = $action;
218    }
219
220    /**
221     * Get the action name for the current web request.
222     *
223     * @since 1.38
224     * @return string Action
225     */
226    public function getActionName(): string {
227        if ( $this->action === false ) {
228            return $this->getContext()->getActionName();
229        }
230
231        $this->action ??= MediaWikiServices::getInstance()
232            ->getActionFactory()
233            ->getActionName( $this );
234
235        return $this->action;
236    }
237
238    /**
239     * @param OutputPage $output
240     */
241    public function setOutput( OutputPage $output ) {
242        $this->output = $output;
243    }
244
245    /**
246     * @return OutputPage
247     */
248    public function getOutput() {
249        return $this->output ?: $this->getContext()->getOutput();
250    }
251
252    /**
253     * @param User $user
254     */
255    public function setUser( User $user ) {
256        $this->authority = $user;
257        $this->user = $user;
258    }
259
260    /**
261     * @return User
262     */
263    public function getUser() {
264        if ( !$this->user && $this->authority ) {
265            // Keep user consistent by using a possible set authority
266            $this->user = MediaWikiServices::getInstance()
267                ->getUserFactory()
268                ->newFromAuthority( $this->authority );
269        }
270        return $this->user ?: $this->getContext()->getUser();
271    }
272
273    public function setAuthority( Authority $authority ) {
274        $this->authority = $authority;
275        // If needed, a User object is constructed from this authority
276        $this->user = null;
277    }
278
279    /**
280     * @since 1.36
281     * @return Authority
282     */
283    public function getAuthority(): Authority {
284        return $this->authority ?: $this->getContext()->getAuthority();
285    }
286
287    /**
288     * @param Language|string $language Language instance or language code
289     * @since 1.19
290     */
291    public function setLanguage( $language ) {
292        Assert::parameterType( [ Language::class, 'string' ], $language, '$language' );
293        if ( $language instanceof Language ) {
294            $this->lang = $language;
295        } else {
296            $language = RequestContext::sanitizeLangCode( $language );
297            $obj = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $language );
298            $this->lang = $obj;
299        }
300    }
301
302    /**
303     * @return Language
304     * @since 1.19
305     */
306    public function getLanguage() {
307        return $this->lang ?: $this->getContext()->getLanguage();
308    }
309
310    /**
311     * @param Skin $skin
312     */
313    public function setSkin( Skin $skin ) {
314        $this->skin = clone $skin;
315        $this->skin->setContext( $this );
316    }
317
318    /**
319     * @return Skin
320     */
321    public function getSkin() {
322        return $this->skin ?: $this->getContext()->getSkin();
323    }
324
325    /**
326     * Get a message using the current context.
327     *
328     * This can't just inherit from ContextSource, since then
329     * it would set only the original context, and not take
330     * into account any changes.
331     *
332     * @param string|string[]|MessageSpecifier $key Message key, or array of keys,
333     *   or a MessageSpecifier.
334     * @phpcs:ignore Generic.Files.LineLength
335     * @param MessageParam|MessageSpecifier|string|int|float|list<MessageParam|MessageSpecifier|string|int|float> ...$params
336     *   See Message::params()
337     * @return Message
338     */
339    public function msg( $key, ...$params ) {
340        // phpcs:ignore MediaWiki.Usage.ExtendClassUsage.FunctionVarUsage
341        return wfMessage( $key, ...$params )->setContext( $this );
342    }
343}
344
345/** @deprecated class alias since 1.42 */
346class_alias( DerivativeContext::class, 'DerivativeContext' );