mediawiki-extensions-Gadgets/includes/GadgetResourceLoaderModule.php
Kunal Mehta 519f30355e Implement Gadgets definition namespace repo
Implements:
* Gadget definition content and content handler
* Basic validation for gadget definition content
* GadgetDefinitionNamespace implementation of GadgetRepo
* DataUpdates upon editing/deletion of Gadget definition pages
* EditFilterMerged hook for improved error messages
* 'GadgetsRepoClass' option to switch GadgetRepo implementation used
* Lazy-load the GadgetResourceLoaderModule class so we don't need to
load each individual gadget object unless its needed

Note that Special:Gadgets's export feature intentionally doesn't work
yet, and will be fixed in a follow up patch.

Bug: T106177
Change-Id: Ib11db5fb0f7b46793bfa956cf1367f1dc1059b1c
2015-11-20 12:01:21 -06:00

90 lines
1.9 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;
/**
* Creates an instance of this class
*
* @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 = array();
foreach ( $gadget->getStyles() as $style ) {
$pages[$style] = array( 'type' => 'style' );
}
if ( $gadget->supportsResourceLoader() ) {
foreach ( $gadget->getScripts() as $script ) {
$pages[$script] = array( 'type' => 'script' );
}
}
return $pages;
}
/**
* Overrides ResourceLoaderModule::getDependencies()
* @param $context ResourceLoaderContext
* @return Array: Names of resources this module depends on
*/
public function getDependencies( ResourceLoaderContext $context = null ) {
return $this->getGadget()->getDependencies();
}
/**
* Overrides ResourceLoaderModule::getPosition()
* @return String: 'bottom' or 'top'
*/
public function getPosition() {
return $this->getGadget()->getPosition();
}
public function getMessages() {
return $this->getGadget()->getMessages();
}
public function getTargets() {
return $this->getGadget()->getTargets();
}
}