r75137 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75136‎ | r75137 | r75138 >
Date:01:03, 21 October 2010
Author:tparscal
Status:resolved (Comments)
Tags:
Comment:
Changed the way that ResourceLoaderFileModule and mediaWiki.loader work in debug mode. As of this commit, in debug mode, ResourceLoaderFileModule will emit code which appends a a script tag for each raw JavaScript file in the module, instead of the concatenated code of the module's scripts. This eliminates the need to disable batch loading on the client in debug mode, since it effectively supersedes the effect of turning batch loading off.
Modified paths:
  • /trunk/phase3/includes/resourceloader/ResourceLoaderFileModule.php (modified) (history)
  • /trunk/phase3/resources/Resources.php (modified) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.js (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/resourceloader/ResourceLoaderFileModule.php
@@ -79,6 +79,8 @@
8080 * @format array( [message-key], [message-key], ... )
8181 */
8282 protected $group;
 83+ /** @var {boolean} Link to raw files in debug mode */
 84+ protected $debugRaw = true;
8385 /**
8486 * @var {array} Cache for mtime
8587 * @format array( [hash] => [mtime], [hash] => [mtime], ... )
@@ -147,8 +149,12 @@
148150 break;
149151 // Single strings
150152 case 'group':
151 - $this->group = (string) $option;
 153+ $this->{$member} = (string) $option;
152154 break;
 155+ // Single booleans
 156+ case 'debugRaw':
 157+ $this->{$member} = (bool) $option;
 158+ break;
153159 }
154160 }
155161 }
@@ -160,13 +166,24 @@
161167 * @return {string} JavaScript code for $context
162168 */
163169 public function getScript( ResourceLoaderContext $context ) {
164 - $script = self::readScriptFiles( $this->scripts ) . "\n" .
165 - self::readScriptFiles( self::tryForKey( $this->languageScripts, $context->getLanguage() ) ) . "\n" .
166 - self::readScriptFiles( self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' ) ) . "\n";
 170+ global $wgScriptPath;
 171+
 172+ $files = array_merge(
 173+ $this->scripts,
 174+ self::tryForKey( $this->languageScripts, $context->getLanguage() ),
 175+ self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' )
 176+ );
167177 if ( $context->getDebug() ) {
168 - $script .= "\n" . self::readScriptFiles( $this->debugScripts );
 178+ $files = array_merge( $files, $this->debugScripts );
 179+ if ( $this->debugRaw ) {
 180+ $tags = '';
 181+ foreach ( $files as $file ) {
 182+ $tags .= "<script type=\"text/javascript\" src=\"$wgScriptPath/$file\"></script>";
 183+ }
 184+ return "\n\tdocument.write( " . FormatJson::encode( $tags ) . ' );';
 185+ }
169186 }
170 - return $script;
 187+ return self::readScriptFiles( $files );
171188 }
172189
173190 /**
Index: trunk/phase3/resources/Resources.php
@@ -25,7 +25,7 @@
2626
2727 /* jQuery */
2828
29 - 'jquery' => new ResourceLoaderFileModule( array( 'scripts' => 'resources/jquery/jquery.js' ) ),
 29+ 'jquery' => new ResourceLoaderFileModule( array( 'scripts' => 'resources/jquery/jquery.js', 'debugRaw' => false ) ),
3030
3131 /* jQuery Plugins */
3232
@@ -314,6 +314,7 @@
315315 'mediawiki' => new ResourceLoaderFileModule( array(
316316 'scripts' => 'resources/mediawiki/mediawiki.js',
317317 'debugScripts' => 'resources/mediawiki/mediawiki.log.js',
 318+ 'debugRaw' => false
318319 ) ),
319320 'mediawiki.specials.preferences' => new ResourceLoaderFileModule( array(
320321 'scripts' => 'resources/mediawiki/mediawiki.specials.preferences.js',
Index: trunk/phase3/resources/mediawiki/mediawiki.js
@@ -495,34 +495,26 @@
496496 };
497497 // Extend request parameters with a list of modules in the batch
498498 var requests = [];
499 - if ( base.debug == '1' ) {
500 - for ( var b = 0; b < batch.length; b++ ) {
501 - requests[requests.length] = $.extend(
502 - { 'modules': batch[b], 'version': registry[batch[b]].version }, base
503 - );
 499+ // Split into groups
 500+ var groups = {};
 501+ for ( var b = 0; b < batch.length; b++ ) {
 502+ var group = registry[batch[b]].group;
 503+ if ( !( group in groups ) ) {
 504+ groups[group] = [];
504505 }
505 - } else {
506 - // Split into groups
507 - var groups = {};
508 - for ( var b = 0; b < batch.length; b++ ) {
509 - var group = registry[batch[b]].group;
510 - if ( !( group in groups ) ) {
511 - groups[group] = [];
 506+ groups[group][groups[group].length] = batch[b];
 507+ }
 508+ for ( var group in groups ) {
 509+ // Calculate the highest timestamp
 510+ var version = 0;
 511+ for ( var g = 0; g < groups[group].length; g++ ) {
 512+ if ( registry[groups[group][g]].version > version ) {
 513+ version = registry[groups[group][g]].version;
512514 }
513 - groups[group][groups[group].length] = batch[b];
514515 }
515 - for ( var group in groups ) {
516 - // Calculate the highest timestamp
517 - var version = 0;
518 - for ( var g = 0; g < groups[group].length; g++ ) {
519 - if ( registry[groups[group][g]].version > version ) {
520 - version = registry[groups[group][g]].version;
521 - }
522 - }
523 - requests[requests.length] = $.extend(
524 - { 'modules': groups[group].join( '|' ), 'version': formatVersionNumber( version ) }, base
525 - );
526 - }
 516+ requests[requests.length] = $.extend(
 517+ { 'modules': groups[group].join( '|' ), 'version': formatVersionNumber( version ) }, base
 518+ );
527519 }
528520 // Clear the batch - this MUST happen before we append the script element to the body or it's
529521 // possible that the script will be locally cached, instantly load, and work the batch again,

Follow-up revisions

RevisionCommit summaryAuthorDate
r75138Merged from trunk r75121 through r75137awjrichards01:30, 21 October 2010
r75169Synced files from r75137 to r75167 of trunkawjrichards20:55, 21 October 2010

Comments

#Comment by Catrope (talk | contribs)   19:44, 26 October 2010
+					$tags .= "<script type=\"text/javascript\" src=\"$wgScriptPath/$file\"></script>";

You should escape the filename for HTML, preferably using an Xml:: function to build the tag.

+	'jquery' => new ResourceLoaderFileModule( array( 'scripts' => 'resources/jquery/jquery.js', 'debugRaw' => false ) ),

Out of curiosity, why exactly can't jquery and mediawiki be loaded in raw mode?

#Comment by Trevor Parscal (WMF) (talk | contribs)   05:56, 9 November 2010

Well, as of r75170 we use the mediaWiki.loader.load to do this.

#Comment by Krinkle (talk | contribs)   05:48, 9 November 2010
+				return "\n\tdocument.write( " . FormatJson::encode( $tags ) . ' );';

The use of document.write in any situation is direcommended afaik (has all sorts of down sides, google for "avoid document.write") and is also blacklisted by JSLint. Use DOM Manipulation to append script tags to the <head>. Whether or not with jQuery.

#Comment by Trevor Parscal (WMF) (talk | contribs)   05:55, 9 November 2010

The side effects of using dom manipulation are that the scripts will load after document ready. The side effects of document.write have to do with using it after document ready. I'm open to solutions, but blacklisting something that's working just fine based on prejudice against a technique doesn't make the software better.

Yes, document.write should be used very carefully and the user should know what they are getting into.


Also - that said the next revision (r75170) changes this to use mediaWiki.loader.load - which automatically detects if it's safe to use dom/document.write and acts accordingly.

Status & tagging log