diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2015-12-17 09:15:42 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2015-12-17 09:44:51 +0100 |
commit | a1789ddde42033f1b05cc4929491214ee6e79383 (patch) | |
tree | 63615735c4ddffaaabf2428946bb26f90899f7bf /extensions/Gadgets/includes | |
parent | 9e06a62f265e3a2aaabecc598d4bc617e06fa32d (diff) |
Update to MediaWiki 1.26.0
Diffstat (limited to 'extensions/Gadgets/includes')
3 files changed, 357 insertions, 0 deletions
diff --git a/extensions/Gadgets/includes/GadgetRepo.php b/extensions/Gadgets/includes/GadgetRepo.php new file mode 100644 index 00000000..18bf5b51 --- /dev/null +++ b/extensions/Gadgets/includes/GadgetRepo.php @@ -0,0 +1,63 @@ +<?php + +abstract class GadgetRepo { + + /** + * @var GadgetRepo|null + */ + private static $instance; + + /** + * Get the ids of the gadgets provided by this repository + * + * @return string[] + */ + abstract public function getGadgetIds(); + + /** + * Get the Gadget object for a given gadget id + * + * @param string $id + * @throws InvalidArgumentException + * @return Gadget + */ + abstract public function getGadget( $id ); + + /** + * Get a list of gadgets sorted by category + * + * @return array array( 'category' => array( 'name' => $gadget ) ) + */ + public function getStructuredList() { + $list = array(); + foreach ( $this->getGadgetIds() as $id ) { + $gadget = $this->getGadget( $id ); + $list[$gadget->getCategory()][$gadget->getName()] = $gadget; + } + + return $list; + } + + /** + * Get the configured default GadgetRepo. Currently + * this hardcodes MediaWikiGadgetsDefinitionRepo since + * that is the only implementation + * + * @return GadgetRepo + */ + public static function singleton() { + if ( self::$instance === null ) { + self::$instance = new MediaWikiGadgetsDefinitionRepo(); + } + return self::$instance; + } + + /** + * Should only be used by unit tests + * + * @param GadgetRepo|null $repo + */ + public static function setSingleton( $repo = null ) { + self::$instance = $repo; + } +} diff --git a/extensions/Gadgets/includes/GadgetResourceLoaderModule.php b/extensions/Gadgets/includes/GadgetResourceLoaderModule.php new file mode 100644 index 00000000..7f00e4cc --- /dev/null +++ b/extensions/Gadgets/includes/GadgetResourceLoaderModule.php @@ -0,0 +1,61 @@ +<?php + +/** + * Class representing a list of resources for one gadget + */ +class GadgetResourceLoaderModule extends ResourceLoaderWikiModule { + private $pages, $dependencies, $messages; + + /** + * Creates an instance of this class + * + * @param $pages Array: Associative array of pages in ResourceLoaderWikiModule-compatible + * format, for example: + * array( + * 'MediaWiki:Gadget-foo.js' => array( 'type' => 'script' ), + * 'MediaWiki:Gadget-foo.css' => array( 'type' => 'style' ), + * ) + * @param $dependencies Array: Names of resources this module depends on + * @param $targets Array: List of targets this module support + * @param $position String: 'bottom' or 'top' + * @param $messages Array + */ + public function __construct( $pages, $dependencies, $targets, $position, $messages ) { + $this->pages = $pages; + $this->dependencies = $dependencies; + $this->targets = $targets; + $this->position = $position; + $this->isPositionDefined = true; + $this->messages = $messages; + } + + /** + * Overrides the abstract function from ResourceLoaderWikiModule class + * @param $context ResourceLoaderContext + * @return Array: $pages passed to __construct() + */ + protected function getPages( ResourceLoaderContext $context ) { + return $this->pages; + } + + /** + * Overrides ResourceLoaderModule::getDependencies() + * @param $context ResourceLoaderContext + * @return Array: Names of resources this module depends on + */ + public function getDependencies( ResourceLoaderContext $context = null ) { + return $this->dependencies; + } + + /** + * Overrides ResourceLoaderModule::getPosition() + * @return String: 'bottom' or 'top' + */ + public function getPosition() { + return $this->position; + } + + public function getMessages() { + return $this->messages; + } +} diff --git a/extensions/Gadgets/includes/MediaWikiGadgetsDefinitionRepo.php b/extensions/Gadgets/includes/MediaWikiGadgetsDefinitionRepo.php new file mode 100644 index 00000000..eb141a91 --- /dev/null +++ b/extensions/Gadgets/includes/MediaWikiGadgetsDefinitionRepo.php @@ -0,0 +1,233 @@ +<?php + +/** + * Gadgets repo powered by MediaWiki:Gadgets-definition + */ +class MediaWikiGadgetsDefinitionRepo extends GadgetRepo { + + const CACHE_VERSION = 1; + + private $definitionCache; + + public function getGadget( $id ) { + $gadgets = $this->loadGadgets(); + if ( !isset( $gadgets[$id] ) ) { + throw new InvalidArgumentException( "No gadget registered for '$id'" ); + } + + return $gadgets[$id]; + } + + public function getGadgetIds() { + $gadgets = $this->loadGadgets(); + if ( $gadgets ) { + return array_keys( $gadgets ); + } else { + return array(); + } + } + + /** + * Purge the definitions cache, for example if MediaWiki:Gadgets-definition + * was edited. + */ + public function purgeDefinitionCache() { + ObjectCache::getMainWANInstance()->touchCheckKey( $this->getCheckKey() ); + } + + private function getCheckKey() { + return wfMemcKey( 'gadgets-definition', Gadget::GADGET_CLASS_VERSION, self::CACHE_VERSION ); + } + + /** + * Loads list of gadgets and returns it as associative array of sections with gadgets + * e.g. array( 'sectionnname1' => array( $gadget1, $gadget2 ), + * 'sectionnname2' => array( $gadget3 ) ); + * @return array|bool Gadget array or false on failure + */ + protected function loadGadgets() { + if ( $this->definitionCache !== null ) { + return $this->definitionCache; // process cache hit + } + + // Ideally $t1Cache is APC, and $wanCache is memcached + $t1Cache = ObjectCache::newAccelerator( array(), 'hash' ); + $wanCache = ObjectCache::getMainWANInstance(); + + $key = $this->getCheckKey(); + + // (a) Check the tier 1 cache + $value = $t1Cache->get( $key ); + // Check if it passes a blind TTL check (avoids I/O) + if ( $value && ( microtime( true ) - $value['time'] ) < 10 ) { + $this->definitionCache = $value['gadgets']; // process cache + return $this->definitionCache; + } + // Cache generated after the "check" time should be up-to-date + $ckTime = $wanCache->getCheckKeyTime( $key ) + WANObjectCache::HOLDOFF_TTL; + if ( $value && $value['time'] > $ckTime ) { + $this->definitionCache = $value['gadgets']; // process cache + return $this->definitionCache; + } + + // (b) Fetch value from WAN cache or regenerate if needed. + // This is hit occasionally and more so when the list changes. + $us = $this; + $value = $wanCache->getWithSetCallback( + $key, + function( $old, &$ttl ) use ( $us ) { + $now = microtime( true ); + $gadgets = $us->fetchStructuredList(); + if ( $gadgets === false ) { + $ttl = WANObjectCache::TTL_UNCACHEABLE; + } + + return array( 'gadgets' => $gadgets, 'time' => $now ); + }, + Gadget::CACHE_TTL, + array( $key ), + array( 'lockTSE' => 300 ) + ); + + // Update the tier 1 cache as needed + if ( $value['gadgets'] !== false && $value['time'] > $ckTime ) { + // Set a modest TTL to keep the WAN key in cache + $t1Cache->set( $key, $value, mt_rand( 300, 600 ) ); + } + + $this->definitionCache = $value['gadgets']; + + return $this->definitionCache; + } + + /** + * Fetch list of gadgets and returns it as associative array of sections with gadgets + * e.g. array( $name => $gadget1, etc. ) + * @param $forceNewText String: Injected text of MediaWiki:gadgets-definition [optional] + * @return array|bool + */ + public function fetchStructuredList( $forceNewText = null ) { + if ( $forceNewText === null ) { + $g = wfMessage( "gadgets-definition" )->inContentLanguage(); + if ( !$g->exists() ) { + return false; // don't cache + } + + $g = $g->plain(); + } else { + $g = $forceNewText; + } + + $gadgets = $this->listFromDefinition( $g ); + if ( !count( $gadgets ) ) { + return false; // don't cache; Bug 37228 + } + + $source = $forceNewText !== null ? 'input text' : 'MediaWiki:Gadgets-definition'; + wfDebug( __METHOD__ . ": $source parsed, cache entry should be updated\n" ); + + return $gadgets; + } + + /** + * Generates a structured list of Gadget objects from a definition + * + * @param $definition + * @return array Array( name => Gadget ) + */ + private function listFromDefinition( $definition ) { + $definition = preg_replace( '/<!--.*?-->/s', '', $definition ); + $lines = preg_split( '/(\r\n|\r|\n)+/', $definition ); + + $gadgets = array(); + $section = ''; + + foreach ( $lines as $line ) { + $m = array(); + if ( preg_match( '/^==+ *([^*:\s|]+?)\s*==+\s*$/', $line, $m ) ) { + $section = $m[1]; + } else { + $gadget = $this->newFromDefinition( $line, $section ); + if ( $gadget ) { + $gadgets[$gadget->getName()] = $gadget; + } + } + } + + return $gadgets; + } + + /** + * Creates an instance of this class from definition in MediaWiki:Gadgets-definition + * @param string $definition Gadget definition + * @param string $category + * @return Gadget|bool Instance of Gadget class or false if $definition is invalid + */ + public function newFromDefinition( $definition, $category ) { + $m = array(); + if ( !preg_match( '/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)(\s*\[.*?\])?\s*((\|[^|]*)+)\s*$/', $definition, $m ) ) { + return false; + } + // NOTE: the gadget name is used as part of the name of a form field, + // and must follow the rules defined in http://www.w3.org/TR/html4/types.html#type-cdata + // Also, title-normalization applies. + $info = array( 'category' => $category ); + $info['name'] = trim( str_replace( ' ', '_', $m[1] ) ); + // If the name is too long, then RL will throw an MWException when + // we try to register the module + if ( !Gadget::isValidGadgetID( $info['name'] ) ) { + return false; + } + $info['definition'] = $definition; + $options = trim( $m[2], ' []' ); + + foreach ( preg_split( '/\s*\|\s*/', $options, -1, PREG_SPLIT_NO_EMPTY ) as $option ) { + $arr = preg_split( '/\s*=\s*/', $option, 2 ); + $option = $arr[0]; + if ( isset( $arr[1] ) ) { + $params = explode( ',', $arr[1] ); + $params = array_map( 'trim', $params ); + } else { + $params = array(); + } + + switch ( $option ) { + case 'ResourceLoader': + $info['resourceLoaded'] = true; + break; + case 'dependencies': + $info['dependencies'] = $params; + break; + case 'rights': + $info['requiredRights'] = $params; + break; + case 'skins': + $info['requiredSkins'] = $params; + break; + case 'default': + $info['onByDefault'] = true; + break; + case 'targets': + $info['targets'] = $params; + break; + case 'top': + $info['position'] = 'top'; + break; + } + } + + foreach ( preg_split( '/\s*\|\s*/', $m[3], -1, PREG_SPLIT_NO_EMPTY ) as $page ) { + $page = "Gadget-$page"; + + if ( preg_match( '/\.js/', $page ) ) { + $info['scripts'][] = $page; + } elseif ( preg_match( '/\.css/', $page ) ) { + $info['styles'][] = $page; + } + } + + return new Gadget( $info ); + } + + +} |