r72220 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72219‎ | r72220 | r72221 >
Date:19:58, 2 September 2010
Author:tparscal
Status:ok
Tags:
Comment:
Added more support for preventing loading in incompatible browsers
Modified paths:
  • /branches/resourceloader/phase3/includes/OutputPage.php (modified) (history)
  • /branches/resourceloader/phase3/includes/ResourceLoader.php (modified) (history)
  • /branches/resourceloader/phase3/includes/ResourceLoaderModule.php (modified) (history)
  • /branches/resourceloader/phase3/resources/mediawiki/mediawiki.js (modified) (history)
  • /branches/resourceloader/phase3/resources/startup.js (added) (history)

Diff [purge]

Index: branches/resourceloader/phase3/includes/OutputPage.php
@@ -2343,8 +2343,9 @@
23442344 }
23452345 if ( $this->getModules() ) {
23462346 // Modules - let the client calculate dependencies and batch requests as it likes
 2347+ $modules = FormatJson::encode( $this->getModules() );
23472348 $scripts .= Html::inlineScript(
2348 - 'mediaWiki.loader.load( ' . FormatJson::encode( $this->getModules() ) . ' );'
 2349+ "if ( mediaWiki !== undefined ) { mediaWiki.loader.load( {$modules} ); }"
23492350 );
23502351 }
23512352 // TODO: User Scripts should be included using the resource loader
@@ -2367,7 +2368,7 @@
23682369 }
23692370 $scripts .= "\n" . $this->mScripts;
23702371 // This should be at the bottom of the body - below ALL other scripts
2371 - $scripts .= Html::inlineScript( "if ( typeof mediaWiki.loader.go === 'function' ) { mediaWiki.loader.go(); }" );
 2372+ $scripts .= Html::inlineScript( "if ( mediaWiki !== undefined ) { mediaWiki.loader.go(); }" );
23722373 return $scripts;
23732374 }
23742375
Index: branches/resourceloader/phase3/includes/ResourceLoader.php
@@ -190,7 +190,7 @@
191191 } else {
192192 // Support module loader scripts
193193 if ( ( $loader = $module->getLoaderScript() ) !== false ) {
194 - $scripts .= "\n" . $loader;
 194+ $scripts .= $loader;
195195 }
196196 // Automatically register module
197197 else {
@@ -206,7 +206,7 @@
207207 }
208208 }
209209 }
210 - return $scripts . "\nmediaWiki.loader.register( " . FormatJson::encode( $registrations ) . " );\n";
 210+ return $scripts . "mediaWiki.loader.register( " . FormatJson::encode( $registrations ) . " );";
211211 }
212212
213213 /**
@@ -327,29 +327,38 @@
328328 );
329329 // Special meta-information for the 'startup' module
330330 if ( $name === 'startup' && $parameters['only'] === 'scripts' ) {
331 - $scripts .= self::getModuleRegistrations(
 331+ // Get all module registrations
 332+ $registration = self::getModuleRegistrations(
332333 $parameters['lang'], $parameters['skin'], $parameters['debug']
333334 );
334 - $scripts .= "mediaWiki.config.set( " .
335 - FormatJson::encode( array( 'server' => $server, 'debug' => $parameters['debug'] ) ) . " );\n";
336 - // Wrap in a closure
337 - $scripts = "window.mediaWikiStartUp = function() {" . $scripts . "};";
338 - $query = wfArrayToCGI( $parameters + array( 'version' => wfTimestamp(
339 - TS_ISO_8601,
340 - round(
341 - max(
342 - self::$modules['jquery']->getModifiedTime(
343 - $parameters['lang'], $parameters['skin'], $parameters['debug']
 335+ // Build configuration
 336+ $config = FormatJson::encode( array( 'server' => $server, 'debug' => $parameters['debug'] ) );
 337+ // Add a well-known start-up function
 338+ $scripts .= "window.startUp = function() { {$registration} mediaWiki.config.set( {$config} ); };";
 339+ // Build load query for jquery and mediawiki modules
 340+ $query = wfArrayToCGI( $parameters + array(
 341+ 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
 342+ 'version' => wfTimestamp(
 343+ TS_ISO_8601,
 344+ round(
 345+ max(
 346+ self::$modules['jquery']->getModifiedTime(
 347+ $parameters['lang'], $parameters['skin'], $parameters['debug']
 348+ ),
 349+ self::$modules['mediawiki']->getModifiedTime(
 350+ $parameters['lang'], $parameters['skin'], $parameters['debug']
 351+ )
344352 ),
345 - self::$modules['mediawiki']->getModifiedTime(
346 - $parameters['lang'], $parameters['skin'], $parameters['debug']
347 - )
348 - ),
349 - -2
 353+ -2
 354+ )
350355 )
351 - ) ) );
352 - $scripts .= "document.write('<script type=\"text/javascript\" " .
353 - "src=\"{$server}?modules=jquery|mediawiki&{$query}\"></script>');";
 356+ ) );
 357+ // Build HTML code for loading jquery and mediawiki modules
 358+ $linkedScript = Html::linkedScript( "{$server}?{$query}" );
 359+ // Add code to add jquery and mediawiki loading code; only if the current client is compatible
 360+ $scripts .= "if ( isCompatible() ) { document.write( '{$linkedScript}' ); }";
 361+ // Delete the compatible function - it's not needed anymore
 362+ $scripts .= "delete window['isCompatible'];";
354363 }
355364 }
356365 // Styles
Index: branches/resourceloader/phase3/includes/ResourceLoaderModule.php
@@ -595,7 +595,7 @@
596596 private $modifiedTime = null;
597597
598598 public function getScript( $lang, $skin, $debug ) {
599 - return '';
 599+ return file_get_contents( 'resources/startup.js' );
600600 }
601601
602602 public function getModifiedTime( $lang, $skin, $debug ) {
Index: branches/resourceloader/phase3/resources/mediawiki/mediawiki.js
@@ -624,6 +624,8 @@
625625 that.work();
626626 }
627627
 628+ /* Cache document ready status */
 629+
628630 $(document).ready( function() { ready = true; } );
629631 } )();
630632
@@ -634,9 +636,10 @@
635637
636638 } )( jQuery );
637639
 640+
638641 /* Auto-register from pre-loaded startup scripts */
639642
640 -if ( typeof mediaWikiStartUp === 'function' ) {
641 - mediaWikiStartUp();
642 - delete mediaWikiStartUp;
 643+if ( typeof window['startUp'] === 'function' ) {
 644+ window['startUp']();
 645+ delete window['startUp'];
643646 }
\ No newline at end of file
Index: branches/resourceloader/phase3/resources/startup.js
@@ -0,0 +1,30 @@
 2+/**
 3+ * This script provides a function which is run to evaluate whether or not to continue loading the jquery and mediawiki
 4+ * modules. This code should work on even the most anchient of browsers, so be very careful when editing.
 5+ */
 6+/**
 7+ * Returns false when run in a black-listed browser
 8+ *
 9+ * This function will be deleted after it's used, so do not expand it to be generally useful beyond startup
 10+ *
 11+ * jQuery has minimum requirements of:
 12+ * * Firefox 2.0+
 13+ * * Internet Explorer 6+
 14+ * * Safari 3+
 15+ * * Opera 9+
 16+ * * Chrome 1+
 17+ */
 18+window.isCompatible = function() {
 19+ // IE < 6
 20+ if ( navigator.appVersion.indexOf( 'MSIE' ) !== -1 && parseFloat( navigator.appVersion.split( 'MSIE' )[1] ) < 6 ) {
 21+ return false;
 22+ }
 23+ // TODO: Firefox < 2
 24+ // TODO: Safari < 3
 25+ // TODO: Opera < 9
 26+ // TODO: Chrome < 1
 27+ return true;
 28+};
 29+/**
 30+ * The startUp() function will be generated and added here (at the bottom)
 31+ */
Property changes on: branches/resourceloader/phase3/resources/startup.js
___________________________________________________________________
Added: svn:eol-style
132 + native

Status & tagging log