r76639 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76638‎ | r76639 | r76640 >
Date:18:45, 13 November 2010
Author:maxsem
Status:ok
Tags:
Comment:
Support for specifying extra modules as gadget's dependencies
Modified paths:
  • /trunk/extensions/Gadgets/Gadgets_body.php (modified) (history)
  • /trunk/extensions/Gadgets/Gadgets_tests.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Gadgets/Gadgets_body.php
@@ -1,4 +1,4 @@
2 -<?php
 2+ <?php
33 /**
44 * Gadgets extension - lets users select custom javascript gadgets
55 *
@@ -192,11 +192,12 @@
193193 /**
194194 * Increment this when changing class structure
195195 */
196 - const GADGET_CLASS_VERSION = 1;
 196+ const GADGET_CLASS_VERSION = 2;
197197
198198 private $version = self::GADGET_CLASS_VERSION,
199199 $scripts = array(),
200200 $styles = array(),
 201+ $dependencies = array(),
201202 $name,
202203 $definition,
203204 $resourceLoaded = false;
@@ -218,7 +219,15 @@
219220 $gadget->definition = $definition;
220221 $params = trim( $m[2], ' []' );
221222 foreach ( preg_split( '/\s*\|\s*/', $params, -1, PREG_SPLIT_NO_EMPTY ) as $option ) {
222 - if ( $option == 'ResourceLoader' ) $gadget->resourceLoaded = true;
 223+ if ( $option == 'ResourceLoader' ) {
 224+ $gadget->resourceLoaded = true;
 225+ } elseif ( preg_match( '/dependencies\s*=/', $option ) ) {
 226+ $option = preg_replace( '/dependencies\s*=\s*/', '', $option );
 227+ $deps = preg_split( '/\s*,\s*/', $option, -1, PREG_SPLIT_NO_EMPTY );
 228+ if ( $deps ) {
 229+ $gadget->dependencies = $deps;
 230+ }
 231+ }
223232 }
224233 foreach ( preg_split( '/\s*\|\s*/', $m[3], -1, PREG_SPLIT_NO_EMPTY ) as $page ) {
225234 $page = "Gadget-$page";
@@ -323,7 +332,7 @@
324333 if ( !count( $pages ) ) {
325334 return null;
326335 }
327 - return new GadgetResourceLoaderModule( $pages );
 336+ return new GadgetResourceLoaderModule( $pages, $this->dependencies );
328337 }
329338
330339 /**
@@ -338,6 +347,14 @@
339348 }
340349
341350 /**
 351+ * Returns names of resources this gadget depends on
 352+ * @return Array
 353+ */
 354+ public function getDependencies() {
 355+ return $this->dependencies;
 356+ }
 357+
 358+ /**
342359 * Loads and returns a list of all gadgets
343360 * @return Mixed: Array of gadgets or false
344361 */
@@ -449,7 +466,7 @@
450467 * Class representing a list of resources for one gadget
451468 */
452469 class GadgetResourceLoaderModule extends ResourceLoaderWikiModule {
453 - private $pages;
 470+ private $pages, $dependencies;
454471
455472 /**
456473 * Creates an instance of this class
@@ -459,9 +476,11 @@
460477 * 'Gadget-foo.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ),
461478 * 'Gadget-foo.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ),
462479 * )
 480+ * @param $dependencies Array: Names of resources this module depends on
463481 */
464 - public function __construct( $pages ) {
 482+ public function __construct( $pages, $dependencies ) {
465483 $this->pages = $pages;
 484+ $this->dependencies = $dependencies;
466485 }
467486
468487 /**
@@ -471,4 +490,12 @@
472491 protected function getPages( ResourceLoaderContext $context ) {
473492 return $this->pages;
474493 }
 494+
 495+ /**
 496+ * Overrides ResourceLoaderModule::getDependencies()
 497+ * @return Array: Names of resources this module depends on
 498+ */
 499+ public function getDependencies() {
 500+ return $this->dependencies;
 501+ }
475502 }
\ No newline at end of file
Index: trunk/extensions/Gadgets/Gadgets_tests.php
@@ -36,4 +36,11 @@
3737 $this->assertTrue( $g->supportsResourceLoader() );
3838 $this->assertEquals(0, count( $g->getLegacyScripts() ) );
3939 }
 40+
 41+ function testDependencies() {
 42+ $g = $this->create( '* foo[ResourceLoader|dependencies=jquery.ui]|bar.js' );
 43+ $this->assertEquals( array( 'Gadget-bar.js' ), $g->getScripts() );
 44+ $this->assertTrue( $g->supportsResourceLoader() );
 45+ $this->assertEquals( array( 'jquery.ui' ), $g->getDependencies() );
 46+ }
4047 }
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r76642rm stray tab from r76639maxsem19:13, 13 November 2010

Status & tagging log