| 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 |
|
| 11 |
|
| 12 |
|
| 13 |
class GadgetResourceLoaderModule extends RL\WikiModule { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
private $id; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
private $gadget; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function __construct( array $options ) { |
| 28 |
$this->id = $options['id']; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private function getGadget() { |
| 35 |
if ( !$this->gadget ) { |
| 36 |
|
| 37 |
$repo = MediaWikiServices::getInstance()->getService( 'GadgetsRepo' ); |
| 38 |
try { |
| 39 |
$this->gadget = $repo->getGadget( $this->id ); |
| 40 |
} catch ( InvalidArgumentException $e ) { |
| 41 |
|
| 42 |
$this->gadget = Gadget::newEmptyGadget( $this->id ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
return $this->gadget; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function getRequireKey( $titleText ): string { |
| 82 |
|
| 83 |
$repo = MediaWikiServices::getInstance()->getService( 'GadgetsRepo' ); |
| 84 |
return $repo->titleWithoutPrefix( $titleText, $this->id ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
protected function validateScriptFile( $fileName, $contents ) { |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
if ( $this->requiresES6() ) { |
| 97 |
return $contents; |
| 98 |
} |
| 99 |
return parent::validateScriptFile( $fileName, $contents ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function isPackaged(): bool { |
| 108 |
return $this->getGadget()->isPackaged(); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function getDependencies( ?RL\Context $context = null ) { |
| 117 |
return $this->getGadget()->getDependencies(); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 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 |
} |