Debian Codegraph @

extensions/Gadgets/includes/GadgetResourceLoaderModule.php

raw symbols SCIP
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 }