Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
GadgetLoadConditions
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 check
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
9
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 * @file
19 */
20
21namespace MediaWiki\Extension\Gadgets;
22
23use MediaWiki\Output\OutputPage;
24use MediaWiki\Skin\Skin;
25use MediaWiki\User\User;
26
27/**
28 * @author Siddharth VP
29 */
30class GadgetLoadConditions {
31
32    private User $user;
33    private Skin $skin;
34    private string $action;
35    private int $namespace;
36    /** @var array<string,int> */
37    private array $categories;
38    private string $contentModel;
39    private ?string $withGadgetParam;
40
41    public function __construct( OutputPage $out ) {
42        $this->user = $out->getUser();
43        $this->skin = $out->getSkin();
44        $this->action = $out->getContext()->getActionName();
45        $this->namespace = $out->getTitle()->getNamespace();
46        $this->categories = array_fill_keys( $out->getCategories(), 1 );
47        $this->contentModel = $out->getTitle()->getContentModel();
48        $this->withGadgetParam = $out->getRequest()->getRawVal( 'withgadget' );
49    }
50
51    public function check( Gadget $gadget ): bool {
52        $urlLoad = $this->withGadgetParam === $gadget->getName() && $gadget->supportsUrlLoad();
53
54        return ( $gadget->isEnabled( $this->user ) || $urlLoad )
55            && $gadget->isAllowed( $this->user )
56            && $gadget->isActionSupported( $this->action )
57            && $gadget->isSkinSupported( $this->skin )
58            && $gadget->isNamespaceSupported( $this->namespace )
59            && $gadget->isCategorySupported( $this->categories )
60            && $gadget->isContentModelSupported( $this->contentModel );
61    }
62}