Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.50% covered (warning)
72.50%
29 / 40
36.36% covered (danger)
36.36%
4 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
GadgetResourceLoaderModule
72.50% covered (warning)
72.50%
29 / 40
36.36% covered (danger)
36.36%
4 / 11
35.98
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getGadget
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
5.67
 getPages
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
8.02
 getScript
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 getRequireKey
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 isPackaged
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDependencies
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getType
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getMessages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSkins
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 getGroup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Gadgets;
4
5use InvalidArgumentException;
6use MediaWiki\MainConfigNames;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\ResourceLoader as RL;
9
10/**
11 * Class representing a list of resources for one gadget, basically a wrapper
12 * around the Gadget class.
13 */
14class GadgetResourceLoaderModule extends RL\WikiModule {
15
16    private string $id;
17    private ?Gadget $gadget = null;
18
19    public function __construct( array $options ) {
20        parent::__construct( $options );
21        $this->id = $options['id'];
22    }
23
24    /**
25     * @return Gadget instance this module is about
26     */
27    private function getGadget(): Gadget {
28        if ( !$this->gadget ) {
29            /** @var GadgetRepo $repo */
30            $repo = MediaWikiServices::getInstance()->getService( 'GadgetsRepo' );
31            try {
32                $this->gadget = $repo->getGadget( $this->id );
33            } catch ( InvalidArgumentException ) {
34                // Fallback to a placeholder object...
35                $this->gadget = Gadget::newEmptyGadget( $this->id );
36            }
37        }
38
39        return $this->gadget;
40    }
41
42    /**
43     * @param RL\Context $context
44     * @return array
45     */
46    protected function getPages( RL\Context $context ) {
47        // In theory we could support these separately, but
48        // * gadget definitions assume both
49        // * it doesn't make sense to load half a gadget
50        // * it's poor UX to have some gadgets work and some not
51        // * historically this extension is only used when
52        //   a community permits both.
53        $config = $this->getConfig();
54        if ( !$config->get( MainConfigNames::UseSiteJs ) || !$config->get( MainConfigNames::UseSiteCss ) ) {
55            return [];
56        }
57
58        $gadget = $this->getGadget();
59        $pages = [];
60
61        foreach ( $gadget->getStyles() as $style ) {
62            $pages[$style] = [ 'type' => 'style' ];
63        }
64
65        foreach ( $gadget->getScripts() as $script ) {
66            $pages[$script] = [ 'type' => 'script' ];
67        }
68        if ( $gadget->isPackaged() ) {
69            foreach ( $gadget->getVues() as $vue ) {
70                $pages[$vue] = [ 'type' => 'script-vue' ];
71            }
72            foreach ( $gadget->getJSONs() as $json ) {
73                $pages[$json] = [ 'type' => 'data' ];
74            }
75        }
76
77        return $pages;
78    }
79
80    /**
81     * @inheritDoc
82     */
83    public function getScript( RL\Context $context ) {
84        $module = parent::getScript( $context );
85
86        if ( $this->isPackaged() && $this->gadget->getCodexIcons() ) {
87            // Add codex icons to the gadget module
88            $module['files']['icons.json'] = [
89                'type' => 'data',
90                'content' => RL\CodexModule::getIcons( $context, $this->getConfig(), $this->gadget->getCodexIcons() ),
91            ];
92        }
93        return $module;
94    }
95
96    /**
97     * @param string $titleText
98     * @return string
99     */
100    public function getRequireKey( $titleText ): string {
101        /** @var GadgetRepo $repo */
102        $repo = MediaWikiServices::getInstance()->getService( 'GadgetsRepo' );
103        return $repo->titleWithoutPrefix( $titleText, $this->id );
104    }
105
106    /**
107     * Returns whether this gadget is packaged.
108     */
109    public function isPackaged(): bool {
110        return $this->getGadget()->isPackaged();
111    }
112
113    /**
114     * @param RL\Context|null $context
115     * @return string[] Names of resources this module depends on
116     */
117    public function getDependencies( ?RL\Context $context = null ) {
118        return $this->getGadget()->getDependencies();
119    }
120
121    /**
122     * @return string RL\Module::LOAD_STYLES or RL\Module::LOAD_GENERAL
123     */
124    public function getType() {
125        return $this->getGadget()->getType() === 'styles'
126            ? RL\Module::LOAD_STYLES
127            : RL\Module::LOAD_GENERAL;
128    }
129
130    /** @inheritDoc */
131    public function getMessages() {
132        return $this->getGadget()->getMessages();
133    }
134
135    /** @inheritDoc */
136    public function getSkins(): ?array {
137        return $this->getGadget()->getRequiredSkins() ?: null;
138    }
139
140    /** @inheritDoc */
141    public function getGroup() {
142        return self::GROUP_SITE;
143    }
144}