Index: trunk/extensions/Gadgets/Gadgets_body.php |
— | — | @@ -1,4 +1,4 @@ |
2 | | -<?php |
| 2 | + <?php |
3 | 3 | /** |
4 | 4 | * Gadgets extension - lets users select custom javascript gadgets |
5 | 5 | * |
— | — | @@ -192,11 +192,12 @@ |
193 | 193 | /** |
194 | 194 | * Increment this when changing class structure |
195 | 195 | */ |
196 | | - const GADGET_CLASS_VERSION = 1; |
| 196 | + const GADGET_CLASS_VERSION = 2; |
197 | 197 | |
198 | 198 | private $version = self::GADGET_CLASS_VERSION, |
199 | 199 | $scripts = array(), |
200 | 200 | $styles = array(), |
| 201 | + $dependencies = array(), |
201 | 202 | $name, |
202 | 203 | $definition, |
203 | 204 | $resourceLoaded = false; |
— | — | @@ -218,7 +219,15 @@ |
219 | 220 | $gadget->definition = $definition; |
220 | 221 | $params = trim( $m[2], ' []' ); |
221 | 222 | 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 | + } |
223 | 232 | } |
224 | 233 | foreach ( preg_split( '/\s*\|\s*/', $m[3], -1, PREG_SPLIT_NO_EMPTY ) as $page ) { |
225 | 234 | $page = "Gadget-$page"; |
— | — | @@ -323,7 +332,7 @@ |
324 | 333 | if ( !count( $pages ) ) { |
325 | 334 | return null; |
326 | 335 | } |
327 | | - return new GadgetResourceLoaderModule( $pages ); |
| 336 | + return new GadgetResourceLoaderModule( $pages, $this->dependencies ); |
328 | 337 | } |
329 | 338 | |
330 | 339 | /** |
— | — | @@ -338,6 +347,14 @@ |
339 | 348 | } |
340 | 349 | |
341 | 350 | /** |
| 351 | + * Returns names of resources this gadget depends on |
| 352 | + * @return Array |
| 353 | + */ |
| 354 | + public function getDependencies() { |
| 355 | + return $this->dependencies; |
| 356 | + } |
| 357 | + |
| 358 | + /** |
342 | 359 | * Loads and returns a list of all gadgets |
343 | 360 | * @return Mixed: Array of gadgets or false |
344 | 361 | */ |
— | — | @@ -449,7 +466,7 @@ |
450 | 467 | * Class representing a list of resources for one gadget |
451 | 468 | */ |
452 | 469 | class GadgetResourceLoaderModule extends ResourceLoaderWikiModule { |
453 | | - private $pages; |
| 470 | + private $pages, $dependencies; |
454 | 471 | |
455 | 472 | /** |
456 | 473 | * Creates an instance of this class |
— | — | @@ -459,9 +476,11 @@ |
460 | 477 | * 'Gadget-foo.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ), |
461 | 478 | * 'Gadget-foo.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ), |
462 | 479 | * ) |
| 480 | + * @param $dependencies Array: Names of resources this module depends on |
463 | 481 | */ |
464 | | - public function __construct( $pages ) { |
| 482 | + public function __construct( $pages, $dependencies ) { |
465 | 483 | $this->pages = $pages; |
| 484 | + $this->dependencies = $dependencies; |
466 | 485 | } |
467 | 486 | |
468 | 487 | /** |
— | — | @@ -471,4 +490,12 @@ |
472 | 491 | protected function getPages( ResourceLoaderContext $context ) { |
473 | 492 | return $this->pages; |
474 | 493 | } |
| 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 | + } |
475 | 502 | } |
\ No newline at end of file |
Index: trunk/extensions/Gadgets/Gadgets_tests.php |
— | — | @@ -36,4 +36,11 @@ |
37 | 37 | $this->assertTrue( $g->supportsResourceLoader() ); |
38 | 38 | $this->assertEquals(0, count( $g->getLegacyScripts() ) ); |
39 | 39 | } |
| 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 | + } |
40 | 47 | } |
\ No newline at end of file |