r111378 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111377‎ | r111378 | r111379 >
Date:15:17, 13 February 2012
Author:krinkle
Status:fixme (Comments)
Tags:
Comment:
[RL] Comment mod and other minor changes
_ Add comment about why it casts to (object)
- Modify function comment
- Whitespace (start the function body on a new line in debug mode. In production mode this is trimmed away afterwards)
- Remove the jQuery->$ passage from loader. There is already a global alias for $ by jQuery, and aside from that every module has it's own (function(){}) wrapper that aliases it from jQuery (not from $), so there is no performance gain either by having it locally here since it doesn't use that.
Modified paths:
  • /trunk/phase3/includes/resourceloader/ResourceLoader.php (modified) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.js (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/resourceloader/ResourceLoader.php
@@ -354,6 +354,7 @@
355355 * @return Array
356356 */
357357 public function getTestModuleNames( $framework = 'all' ) {
 358+ /// @TODO: api siteinfo prop testmodulenames modulenames
358359 if ( $framework == 'all' ) {
359360 return $this->testModuleNames;
360361 } elseif ( isset( $this->testModuleNames[$framework] ) && is_array( $this->testModuleNames[$framework] ) ) {
@@ -797,7 +798,7 @@
798799 *
799800 * @param $name string Module name
800801 * @param $scripts Mixed: List of URLs to JavaScript files or String of JavaScript code
801 - * @param $styles Mixed: List of CSS strings keyed by media type, or list of lists of URLs to
 802+ * @param $styles Mixed: Array of CSS strings keyed by media type, or an array of lists of URLs to
802803 * CSS files keyed by media type
803804 * @param $messages Mixed: List of messages associated with this module. May either be an
804805 * associative array mapping message key to value, or a JSON-encoded message blob containing
@@ -807,7 +808,7 @@
808809 */
809810 public static function makeLoaderImplementScript( $name, $scripts, $styles, $messages ) {
810811 if ( is_string( $scripts ) ) {
811 - $scripts = new XmlJsCode( "function( $ ) {{$scripts}}" );
 812+ $scripts = new XmlJsCode( "function () {\n{$scripts}\n}" );
812813 } elseif ( !is_array( $scripts ) ) {
813814 throw new MWException( 'Invalid scripts error. Array of URLs or string of code expected.' );
814815 }
@@ -816,6 +817,11 @@
817818 array(
818819 $name,
819820 $scripts,
 821+ // Force objects. mw.loader.implement requires them to be javascript objects.
 822+ // Although these variables are associative arrays, which become javascript
 823+ // objects through json_encode. In many cases they will be empty arrays, and
 824+ // PHP/json_encode() consider empty arrays to be numerical arrays and
 825+ // output javascript "[]" instead of "{}". This fixes that.
820826 (object)$styles,
821827 (object)$messages
822828 ) );
@@ -901,7 +907,7 @@
902908 public static function makeCustomLoaderScript( $name, $version, $dependencies, $group, $source, $script ) {
903909 $script = str_replace( "\n", "\n\t", trim( $script ) );
904910 return Xml::encodeJsCall(
905 - "( function( name, version, dependencies, group, source ) {\n\t$script\n} )",
 911+ "( function ( name, version, dependencies, group, source ) {\n\t$script\n} )",
906912 array( $name, $version, $dependencies, $group, $source ) );
907913 }
908914
Index: trunk/phase3/resources/mediawiki/mediawiki.js
@@ -315,7 +315,7 @@
316316 /**
317317 * Client-side module loader which integrates with the MediaWiki ResourceLoader
318318 */
319 - loader: ( function() {
 319+ loader: ( function () {
320320
321321 /* Private Members */
322322
@@ -756,7 +756,7 @@
757757 registry[module].state = 'loading';
758758 nestedAddScript( script, markModuleReady, registry[module].blocking, 0 );
759759 } else if ( $.isFunction( script ) ) {
760 - script( $ );
 760+ script();
761761 markModuleReady();
762762 }
763763 } catch ( e ) {
@@ -1418,7 +1418,7 @@
14191419 return s;
14201420 }
14211421 };
1422 - })()
 1422+ }() )
14231423 };
14241424
14251425 })( jQuery );

Comments

#Comment by Catrope (talk | contribs)   23:36, 15 February 2012

Please don't break the automagic $ aliasing in the closure. I agree that in theory it shouldn't be needed (and obviously it doesn't work in debug mode), but in practice hiding errors in production is nice.

#Comment by Catrope (talk | contribs)   10:43, 18 February 2012

See e.g. bug 33903

#Comment by Krinkle (talk | contribs)   23:59, 18 February 2012

Well, if that's the use case of this alias. Then what about jQuery, mw, mediaWiki, document or perhaps even window ?

#Comment by Krinkle (talk | contribs)   17:05, 28 February 2012

To get back at this one, I prefer having a script fail in both production and debug so it be noticed and fixed (it's a very simple fix), instead of having it silently drag on in production mode but fail in debug mode. Especially since it's unlikely that a wiki re-setting $ to something else will not notice any breakage at all. Sooner or later something will break.

#Comment by 😂 (talk | contribs)   05:06, 21 March 2012

+1

#Comment by Krinkle (talk | contribs)   17:06, 28 February 2012

But if we're going the safe way, then what do you think about adding the other vars as well ?

#Comment by Hashar (talk | contribs)   10:10, 6 March 2012

Roan, Timo, can you please take a decision? If you can not make a decision, maybe raise the issue on wikitech-l to get others people input?

#Comment by Krinkle (talk | contribs)   00:10, 12 March 2012

Very well.

Status & tagging log