Index: branches/Gadgets-work/Gadgets_body.php |
— | — | @@ -12,8 +12,6 @@ |
13 | 13 | * @license GNU General Public Licence 2.0 or later |
14 | 14 | */ |
15 | 15 | |
16 | | -// @todo: Support specifying RL-awareness per gadget |
17 | | - |
18 | 16 | class GadgetHooks { |
19 | 17 | |
20 | 18 | /** |
— | — | @@ -168,6 +166,15 @@ |
169 | 167 | //switched to addScriptFile call to support scriptLoader |
170 | 168 | $out->addScriptFile( $u, $t->getLatestRevID() ); |
171 | 169 | } |
| 170 | + |
| 171 | + /** |
| 172 | + * UnitTestsList hook handler |
| 173 | + * @param $files Array: List of extension test files |
| 174 | + */ |
| 175 | + public static function unitTestsList( $files ) { |
| 176 | + $files[] = dirname( __FILE__ ) . '/Gadgets_tests.php'; |
| 177 | + return true; |
| 178 | + } |
172 | 179 | } |
173 | 180 | |
174 | 181 | /** |
— | — | @@ -183,6 +190,7 @@ |
184 | 191 | $scripts = array(), |
185 | 192 | $styles = array(), |
186 | 193 | $name, |
| 194 | + $definition, |
187 | 195 | $resourceLoaded = false; |
188 | 196 | |
189 | 197 | /** |
— | — | @@ -191,15 +199,20 @@ |
192 | 200 | * @return Mixed: Instance of Gadget class or false if $definition is invalid |
193 | 201 | */ |
194 | 202 | public static function newFromDefinition( $definition ) { |
195 | | - if ( !preg_match( '/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)\s*((\|[^|]*)+)\s*$/', $definition, $m ) ) { |
| 203 | + if ( !preg_match( '/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)(\s*\[.*?\])?\s*((\|[^|]*)+)\s*$/', $definition, $m ) ) { |
196 | 204 | return false; |
197 | 205 | } |
198 | 206 | //NOTE: the gadget name is used as part of the name of a form field, |
199 | 207 | // and must follow the rules defined in http://www.w3.org/TR/html4/types.html#type-cdata |
200 | 208 | // Also, title-normalization applies. |
201 | 209 | $gadget = new Gadget(); |
202 | | - $gadget->name = str_replace(' ', '_', $m[1] ); |
203 | | - foreach( preg_split( '/\s*\|\s*/', $m[2], -1, PREG_SPLIT_NO_EMPTY ) as $page ) { |
| 210 | + $gadget->name = trim( str_replace(' ', '_', $m[1] ) ); |
| 211 | + $gadget->definition = $definition; |
| 212 | + $params = trim( $m[2], ' []' ); |
| 213 | + foreach ( preg_split( '/\s*\|\s*/', $params, -1, PREG_SPLIT_NO_EMPTY ) as $option ) { |
| 214 | + if ( $option == 'ResourceLoader' ) $gadget->resourceLoaded = true; |
| 215 | + } |
| 216 | + foreach ( preg_split( '/\s*\|\s*/', $m[3], -1, PREG_SPLIT_NO_EMPTY ) as $page ) { |
204 | 217 | $page = "Gadget-$page"; |
205 | 218 | if ( preg_match( '/\.js/', $page ) ) { |
206 | 219 | $gadget->scripts[] = $page; |
— | — | @@ -257,6 +270,13 @@ |
258 | 271 | } |
259 | 272 | |
260 | 273 | /** |
| 274 | + * @return String: Definition for this gadget from MediaWiki:gadgets-definition |
| 275 | + */ |
| 276 | + public function getDefinition() { |
| 277 | + return $this->definition; |
| 278 | + } |
| 279 | + |
| 280 | + /** |
261 | 281 | * @return Array: Array of pages with JS not prefixed with namespace |
262 | 282 | */ |
263 | 283 | public function getScripts() { |
Index: branches/Gadgets-work/Gadgets.php |
— | — | @@ -33,6 +33,7 @@ |
34 | 34 | $wgHooks['BeforePageDisplay'][] = 'GadgetHooks::beforePageDisplay'; |
35 | 35 | $wgHooks['GetPreferences'][] = 'GadgetHooks::getPreferences'; |
36 | 36 | $wgHooks['ResourceLoaderRegisterModules'][] = 'GadgetHooks::registerModules'; |
| 37 | +$wgHooks['UnitTestsList'][] = 'GadgetHooks::unitTestsList'; |
37 | 38 | |
38 | 39 | $dir = dirname(__FILE__) . '/'; |
39 | 40 | $wgExtensionMessagesFiles['Gadgets'] = $dir . 'Gadgets.i18n.php'; |
Index: branches/Gadgets-work/SpecialGadgets.php |
— | — | @@ -123,10 +123,6 @@ |
124 | 124 | } |
125 | 125 | } |
126 | 126 | |
127 | | - private static function stripName( $name ) { |
128 | | - return preg_replace( '/Gadget-/i', '', $name ); |
129 | | - } |
130 | | - |
131 | 127 | /** |
132 | 128 | * Exports a gadget with its dependencies in a serialized form |
133 | 129 | * @param $gadget String Name of gadget to export |
— | — | @@ -141,12 +137,9 @@ |
142 | 138 | } |
143 | 139 | |
144 | 140 | $g = $gadgets[$gadget]; |
145 | | - $pages = $g->getScriptsAndStyles(); |
146 | | - $pages = array_map( 'SpecialGadgets::stripName', $pages ); |
147 | | - $ourDefinition = "* $gadget|" . implode('|', $pages ); |
148 | 141 | $this->setHeaders(); |
149 | 142 | $wgOut->setPagetitle( wfMsg( "gadgets-export-title" ) ); |
150 | | - $wgOut->addWikiMsg( 'gadgets-export-text', $gadget, $ourDefinition ); |
| 143 | + $wgOut->addWikiMsg( 'gadgets-export-text', $gadget, $g->getDefinition() ); |
151 | 144 | |
152 | 145 | $exportList = "MediaWiki:gadget-$gadget\n"; |
153 | 146 | foreach ( $g->getScriptsAndStyles() as $page ) { |
Index: branches/Gadgets-work/Gadgets_tests.php |
— | — | @@ -0,0 +1,39 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * @group Gadgets |
| 6 | + */ |
| 7 | +class GadgetsTest extends PHPUnit_Framework_TestCase { |
| 8 | + |
| 9 | + private function create( $line ) { |
| 10 | + $g = Gadget::newFromDefinition( $line ); |
| 11 | + // assertInstanceOf() is available since PHPUnit 3.5 |
| 12 | + $this->assertEquals( 'Gadget', get_class( $g ) ); |
| 13 | + return $g; |
| 14 | + } |
| 15 | + |
| 16 | + function testInvalidLines() { |
| 17 | + $this->assertFalse( Gadget::newFromDefinition( '' ) ); |
| 18 | + $this->assertFalse( Gadget::newFromDefinition( '<foo|bar>' ) ); |
| 19 | + } |
| 20 | + |
| 21 | + function testSimpleCases() { |
| 22 | + $g = $this->create( '* foo bar| foo.css|foo.js|foo.bar' ); |
| 23 | + $this->assertEquals( 'foo_bar', $g->getName() ); |
| 24 | + $this->assertEquals( 'ext.gadget.foo_bar', $g->getModuleName() ); |
| 25 | + $this->assertEquals( array( 'Gadget-foo.js' ), $g->getScripts() ); |
| 26 | + $this->assertEquals( array( 'Gadget-foo.css' ), $g->getStyles() ); |
| 27 | + $this->assertEquals( array( 'Gadget-foo.js', 'Gadget-foo.css' ), |
| 28 | + $g->getScriptsAndStyles() ); |
| 29 | + $this->assertEquals( array( 'Gadget-foo.js' ), $g->getLegacyScripts() ); |
| 30 | + $this->assertFalse( $g->supportsResourceLoader() ); |
| 31 | + $this->assertTrue( $g->hasModule() ); |
| 32 | + } |
| 33 | + |
| 34 | + function testRLtag() { |
| 35 | + $g = $this->create( '*foo [ResourceLoader]|foo.js|foo.css' ); |
| 36 | + $this->assertEquals( 'foo', $g->getName() ); |
| 37 | + $this->assertTrue( $g->supportsResourceLoader() ); |
| 38 | + $this->assertEquals(0, count( $g->getLegacyScripts() ) ); |
| 39 | + } |
| 40 | +} |
\ No newline at end of file |
Property changes on: branches/Gadgets-work/Gadgets_tests.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 41 | + native |