mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Gadgets
synced 2024-11-28 09:10:07 +00:00
1f7b9d90df
The parsed content of JSON files in the gadget is made available from the gadget's JS files via require(). That is, MediaWiki:Gadget-data.json (or Gadget:data.json) is available as `require('./data.json')`. This is supported for both MediaWikiGadgetsDefinitionRepo and GadgetDefinitionNamespaceRepo. The JSON parsing is done server-side. JSON can only be used in "package" gadgets - in which the JS files can also be invoked via require(). Also added a test for GadgetResourceLoaderModule. Bug: T198758 Depends-On: Ib4556d09c4d393269e32771aab00f59a5f630e1b Depends-On: Id4589a597ccfc4266b3e63d10f75b146aa7a287a Change-Id: I21acb46cdd244a39b5cc6963aa763f0113bd1e38
115 lines
2.6 KiB
PHP
115 lines
2.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Class representing a list of resources for one gadget, basically a wrapper
|
|
* around the Gadget class.
|
|
*/
|
|
class GadgetResourceLoaderModule extends ResourceLoaderWikiModule {
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $id;
|
|
|
|
/**
|
|
* @var Gadget
|
|
*/
|
|
private $gadget;
|
|
|
|
/**
|
|
* @param array $options
|
|
*/
|
|
public function __construct( array $options ) {
|
|
$this->id = $options['id'];
|
|
}
|
|
|
|
/**
|
|
* @return Gadget instance this module is about
|
|
*/
|
|
private function getGadget() {
|
|
if ( !$this->gadget ) {
|
|
try {
|
|
$this->gadget = GadgetRepo::singleton()->getGadget( $this->id );
|
|
} catch ( InvalidArgumentException $e ) {
|
|
// Fallback to a placeholder object...
|
|
$this->gadget = Gadget::newEmptyGadget( $this->id );
|
|
}
|
|
}
|
|
|
|
return $this->gadget;
|
|
}
|
|
|
|
/**
|
|
* Overrides the function from ResourceLoaderWikiModule class
|
|
* @param ResourceLoaderContext $context
|
|
* @return array
|
|
*/
|
|
protected function getPages( ResourceLoaderContext $context ) {
|
|
$gadget = $this->getGadget();
|
|
$pages = [];
|
|
|
|
foreach ( $gadget->getStyles() as $style ) {
|
|
$pages[$style] = [ 'type' => 'style' ];
|
|
}
|
|
|
|
if ( $gadget->supportsResourceLoader() ) {
|
|
foreach ( $gadget->getScripts() as $script ) {
|
|
$pages[$script] = [ 'type' => 'script' ];
|
|
}
|
|
foreach ( $gadget->getJSONs() as $json ) {
|
|
$pages[$json] = [ 'type' => 'data' ];
|
|
}
|
|
}
|
|
|
|
return $pages;
|
|
}
|
|
|
|
/**
|
|
* Overrides ResourceLoaderWikiModule::getRequireKey()
|
|
* @param string $titleText
|
|
* @return string
|
|
*/
|
|
public function getRequireKey( $titleText ): string {
|
|
return GadgetRepo::singleton()->titleWithoutPrefix( $titleText );
|
|
}
|
|
|
|
/**
|
|
* Overrides ResourceLoaderWikiModule::isPackaged()
|
|
* Returns whether this gadget is packaged.
|
|
* @return bool
|
|
*/
|
|
public function isPackaged(): bool {
|
|
return $this->gadget->isPackaged();
|
|
}
|
|
|
|
/**
|
|
* Overrides ResourceLoaderModule::getDependencies()
|
|
* @param ResourceLoaderContext|null $context
|
|
* @return string[] Names of resources this module depends on
|
|
*/
|
|
public function getDependencies( ResourceLoaderContext $context = null ) {
|
|
return $this->getGadget()->getDependencies();
|
|
}
|
|
|
|
/**
|
|
* Overrides ResourceLoaderWikiModule::getType()
|
|
* @return string ResourceLoaderModule::LOAD_STYLES or ResourceLoaderModule::LOAD_GENERAL
|
|
*/
|
|
public function getType() {
|
|
return $this->getGadget()->getType() === 'styles'
|
|
? ResourceLoaderModule::LOAD_STYLES
|
|
: ResourceLoaderModule::LOAD_GENERAL;
|
|
}
|
|
|
|
public function getMessages() {
|
|
return $this->getGadget()->getMessages();
|
|
}
|
|
|
|
public function getTargets() {
|
|
return $this->getGadget()->getTargets();
|
|
}
|
|
|
|
public function getGroup() {
|
|
return 'site';
|
|
}
|
|
}
|