r76101 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76100‎ | r76101 | r76102 >
Date:15:05, 5 November 2010
Author:maxsem
Status:deferred (Comments)
Tags:
Comment:
* Now gadgets can specify that their JS supports ResourceLoader
* Tests
Modified paths:
  • /branches/Gadgets-work/Gadgets.php (modified) (history)
  • /branches/Gadgets-work/Gadgets_body.php (modified) (history)
  • /branches/Gadgets-work/Gadgets_tests.php (added) (history)
  • /branches/Gadgets-work/SpecialGadgets.php (modified) (history)

Diff [purge]

Index: branches/Gadgets-work/Gadgets_body.php
@@ -12,8 +12,6 @@
1313 * @license GNU General Public Licence 2.0 or later
1414 */
1515
16 -// @todo: Support specifying RL-awareness per gadget
17 -
1816 class GadgetHooks {
1917
2018 /**
@@ -168,6 +166,15 @@
169167 //switched to addScriptFile call to support scriptLoader
170168 $out->addScriptFile( $u, $t->getLatestRevID() );
171169 }
 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+ }
172179 }
173180
174181 /**
@@ -183,6 +190,7 @@
184191 $scripts = array(),
185192 $styles = array(),
186193 $name,
 194+ $definition,
187195 $resourceLoaded = false;
188196
189197 /**
@@ -191,15 +199,20 @@
192200 * @return Mixed: Instance of Gadget class or false if $definition is invalid
193201 */
194202 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 ) ) {
196204 return false;
197205 }
198206 //NOTE: the gadget name is used as part of the name of a form field,
199207 // and must follow the rules defined in http://www.w3.org/TR/html4/types.html#type-cdata
200208 // Also, title-normalization applies.
201209 $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 ) {
204217 $page = "Gadget-$page";
205218 if ( preg_match( '/\.js/', $page ) ) {
206219 $gadget->scripts[] = $page;
@@ -257,6 +270,13 @@
258271 }
259272
260273 /**
 274+ * @return String: Definition for this gadget from MediaWiki:gadgets-definition
 275+ */
 276+ public function getDefinition() {
 277+ return $this->definition;
 278+ }
 279+
 280+ /**
261281 * @return Array: Array of pages with JS not prefixed with namespace
262282 */
263283 public function getScripts() {
Index: branches/Gadgets-work/Gadgets.php
@@ -33,6 +33,7 @@
3434 $wgHooks['BeforePageDisplay'][] = 'GadgetHooks::beforePageDisplay';
3535 $wgHooks['GetPreferences'][] = 'GadgetHooks::getPreferences';
3636 $wgHooks['ResourceLoaderRegisterModules'][] = 'GadgetHooks::registerModules';
 37+$wgHooks['UnitTestsList'][] = 'GadgetHooks::unitTestsList';
3738
3839 $dir = dirname(__FILE__) . '/';
3940 $wgExtensionMessagesFiles['Gadgets'] = $dir . 'Gadgets.i18n.php';
Index: branches/Gadgets-work/SpecialGadgets.php
@@ -123,10 +123,6 @@
124124 }
125125 }
126126
127 - private static function stripName( $name ) {
128 - return preg_replace( '/Gadget-/i', '', $name );
129 - }
130 -
131127 /**
132128 * Exports a gadget with its dependencies in a serialized form
133129 * @param $gadget String Name of gadget to export
@@ -141,12 +137,9 @@
142138 }
143139
144140 $g = $gadgets[$gadget];
145 - $pages = $g->getScriptsAndStyles();
146 - $pages = array_map( 'SpecialGadgets::stripName', $pages );
147 - $ourDefinition = "* $gadget|" . implode('|', $pages );
148141 $this->setHeaders();
149142 $wgOut->setPagetitle( wfMsg( "gadgets-export-title" ) );
150 - $wgOut->addWikiMsg( 'gadgets-export-text', $gadget, $ourDefinition );
 143+ $wgOut->addWikiMsg( 'gadgets-export-text', $gadget, $g->getDefinition() );
151144
152145 $exportList = "MediaWiki:gadget-$gadget\n";
153146 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
141 + native

Comments

#Comment by Nikerabbit (talk | contribs)   15:14, 5 November 2010

$name,

+			$definition,

Mixed indentation.

Yay for tets.

Status & tagging log