Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
75.00% |
24 / 32 |
|
50.00% |
6 / 12 |
CRAP | |
0.00% |
0 / 1 |
GadgetResourceLoaderModule | |
75.00% |
24 / 32 |
|
50.00% |
6 / 12 |
31.27 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getGadget | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
getPages | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 | |||
getRequireKey | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
validateScriptFile | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
isPackaged | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDependencies | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getType | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getMessages | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSkins | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
requiresES6 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getGroup | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Gadgets; |
4 | |
5 | use InvalidArgumentException; |
6 | use MediaWiki\MediaWikiServices; |
7 | use MediaWiki\ResourceLoader as RL; |
8 | |
9 | /** |
10 | * Class representing a list of resources for one gadget, basically a wrapper |
11 | * around the Gadget class. |
12 | */ |
13 | class GadgetResourceLoaderModule extends RL\WikiModule { |
14 | /** |
15 | * @var string |
16 | */ |
17 | private $id; |
18 | |
19 | /** |
20 | * @var Gadget |
21 | */ |
22 | private $gadget; |
23 | |
24 | /** |
25 | * @param array $options |
26 | */ |
27 | public function __construct( array $options ) { |
28 | $this->id = $options['id']; |
29 | } |
30 | |
31 | /** |
32 | * @return Gadget instance this module is about |
33 | */ |
34 | private function getGadget() { |
35 | if ( !$this->gadget ) { |
36 | /** @var GadgetRepo $repo */ |
37 | $repo = MediaWikiServices::getInstance()->getService( 'GadgetsRepo' ); |
38 | try { |
39 | $this->gadget = $repo->getGadget( $this->id ); |
40 | } catch ( InvalidArgumentException $e ) { |
41 | // Fallback to a placeholder object... |
42 | $this->gadget = Gadget::newEmptyGadget( $this->id ); |
43 | } |
44 | } |
45 | |
46 | return $this->gadget; |
47 | } |
48 | |
49 | /** |
50 | * Overrides the function from RL\WikiModule class |
51 | * @param RL\Context $context |
52 | * @return array |
53 | */ |
54 | protected function getPages( RL\Context $context ) { |
55 | $gadget = $this->getGadget(); |
56 | $pages = []; |
57 | |
58 | foreach ( $gadget->getStyles() as $style ) { |
59 | $pages[$style] = [ 'type' => 'style' ]; |
60 | } |
61 | |
62 | if ( $gadget->supportsResourceLoader() ) { |
63 | foreach ( $gadget->getScripts() as $script ) { |
64 | $pages[$script] = [ 'type' => 'script' ]; |
65 | } |
66 | if ( $gadget->isPackaged() ) { |
67 | foreach ( $gadget->getJSONs() as $json ) { |
68 | $pages[$json] = [ 'type' => 'data' ]; |
69 | } |
70 | } |
71 | } |
72 | |
73 | return $pages; |
74 | } |
75 | |
76 | /** |
77 | * Overrides RL\WikiModule::getRequireKey() |
78 | * @param string $titleText |
79 | * @return string |
80 | */ |
81 | public function getRequireKey( $titleText ): string { |
82 | /** @var GadgetRepo $repo */ |
83 | $repo = MediaWikiServices::getInstance()->getService( 'GadgetsRepo' ); |
84 | return $repo->titleWithoutPrefix( $titleText, $this->id ); |
85 | } |
86 | |
87 | /** |
88 | * @param string $fileName |
89 | * @param string $contents |
90 | * @return string |
91 | */ |
92 | protected function validateScriptFile( $fileName, $contents ) { |
93 | // Temporary solution to support gadgets in ES6 by disabling validation |
94 | // for them and putting them in a separate resource group to avoid a syntax error in them |
95 | // from corrupting core/extension-loaded scripts or other non-ES6 gadgets. |
96 | if ( $this->requiresES6() ) { |
97 | return $contents; |
98 | } |
99 | return parent::validateScriptFile( $fileName, $contents ); |
100 | } |
101 | |
102 | /** |
103 | * Overrides RL\WikiModule::isPackaged() |
104 | * Returns whether this gadget is packaged. |
105 | * @return bool |
106 | */ |
107 | public function isPackaged(): bool { |
108 | return $this->getGadget()->isPackaged(); |
109 | } |
110 | |
111 | /** |
112 | * Overrides RL\Module::getDependencies() |
113 | * @param RL\Context|null $context |
114 | * @return string[] Names of resources this module depends on |
115 | */ |
116 | public function getDependencies( RL\Context $context = null ) { |
117 | return $this->getGadget()->getDependencies(); |
118 | } |
119 | |
120 | /** |
121 | * Overrides RL\WikiModule::getType() |
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 | public function getMessages() { |
131 | return $this->getGadget()->getMessages(); |
132 | } |
133 | |
134 | public function getSkins(): ?array { |
135 | return $this->getGadget()->getRequiredSkins() ?: null; |
136 | } |
137 | |
138 | public function requiresES6(): bool { |
139 | return $this->getGadget()->requiresES6(); |
140 | } |
141 | |
142 | public function getGroup() { |
143 | return $this->requiresES6() ? 'es6-gadget' : self::GROUP_SITE; |
144 | } |
145 | } |