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