Index: trunk/phase3/includes/ResourceLoader.php |
— | — | @@ -437,4 +437,47 @@ |
438 | 438 | return "mediaWiki.loader.state( '$name', '$state' );\n"; |
439 | 439 | } |
440 | 440 | } |
| 441 | + |
| 442 | + public static function makeCustomLoaderScript( $name, $version, $dependencies, $group, $script ) { |
| 443 | + $name = Xml::escapeJsString( $name ); |
| 444 | + $version = (int) $version > 1 ? (int) $version : 1; |
| 445 | + if ( is_array( $dependencies ) ) { |
| 446 | + $dependencies = FormatJson::encode( $dependencies ); |
| 447 | + } else if ( is_string( $dependencies ) ) { |
| 448 | + $dependencies = "'" . Xml::escapeJsString( $dependencies ) . "'"; |
| 449 | + } else { |
| 450 | + $dependencies = 'null'; |
| 451 | + } |
| 452 | + if ( is_string( $group ) ) { |
| 453 | + $group = "'" . Xml::escapeJsString( $group ) . "'"; |
| 454 | + } else { |
| 455 | + $group = 'null'; |
| 456 | + } |
| 457 | + $script = str_replace( "\n", "\n\t", trim( $script ) ); |
| 458 | + return "( function( name, version, dependencies ) {\t$script\t} )" . |
| 459 | + "( '$name', $version, $dependencies, $group );\n"; |
| 460 | + } |
| 461 | + |
| 462 | + public static function makeLoaderRegisterScript( $name, $version = null, $dependencies = null, $group = null ) { |
| 463 | + if ( is_array( $name ) ) { |
| 464 | + $registrations = FormatJson::encode( $name ); |
| 465 | + return "mediaWiki.loader.register( $registrations );\n"; |
| 466 | + } else { |
| 467 | + $name = Xml::escapeJsString( $name ); |
| 468 | + $version = (int) $version > 1 ? (int) $version : 1; |
| 469 | + if ( is_array( $dependencies ) ) { |
| 470 | + $dependencies = FormatJson::encode( $dependencies ); |
| 471 | + } else if ( is_string( $dependencies ) ) { |
| 472 | + $dependencies = "'" . Xml::escapeJsString( $dependencies ) . "'"; |
| 473 | + } else { |
| 474 | + $dependencies = 'null'; |
| 475 | + } |
| 476 | + if ( is_string( $group ) ) { |
| 477 | + $group = "'" . Xml::escapeJsString( $group ) . "'"; |
| 478 | + } else { |
| 479 | + $group = 'null'; |
| 480 | + } |
| 481 | + return "mediaWiki.loader.register( '$name', $version, $dependencies, $group );\n"; |
| 482 | + } |
| 483 | + } |
441 | 484 | } |
Index: trunk/phase3/includes/ResourceLoaderModule.php |
— | — | @@ -1048,7 +1048,7 @@ |
1049 | 1049 | public static function getModuleRegistrations( ResourceLoaderContext $context ) { |
1050 | 1050 | wfProfileIn( __METHOD__ ); |
1051 | 1051 | |
1052 | | - $scripts = ''; |
| 1052 | + $out = ''; |
1053 | 1053 | $registrations = array(); |
1054 | 1054 | foreach ( ResourceLoader::getModules() as $name => $module ) { |
1055 | 1055 | // Support module loader scripts |
— | — | @@ -1056,8 +1056,7 @@ |
1057 | 1057 | $deps = FormatJson::encode( $module->getDependencies() ); |
1058 | 1058 | $group = FormatJson::encode( $module->getGroup() ); |
1059 | 1059 | $version = wfTimestamp( TS_ISO_8601, round( $module->getModifiedTime( $context ), -2 ) ); |
1060 | | - $scripts .= "( function( name, version, dependencies ) { $loader } )\n" . |
1061 | | - "( '$name', '$version', $deps, $group );\n"; |
| 1060 | + $out .= ResourceLoader::makeCustomLoaderScript( $name, $version, $deps, $group, $loader ); |
1062 | 1061 | } |
1063 | 1062 | // Automatically register module |
1064 | 1063 | else { |
— | — | @@ -1080,7 +1079,7 @@ |
1081 | 1080 | } |
1082 | 1081 | } |
1083 | 1082 | } |
1084 | | - $out = $scripts . "mediaWiki.loader.register( " . FormatJson::encode( $registrations ) . " );"; |
| 1083 | + $out .= ResourceLoader::makeLoaderRegisterScript( $registrations ); |
1085 | 1084 | |
1086 | 1085 | wfProfileOut( __METHOD__ ); |
1087 | 1086 | return $out; |
— | — | @@ -1091,14 +1090,8 @@ |
1092 | 1091 | public function getScript( ResourceLoaderContext $context ) { |
1093 | 1092 | global $IP, $wgLoadScript; |
1094 | 1093 | |
1095 | | - $scripts = file_get_contents( "$IP/resources/startup.js" ); |
| 1094 | + $out = file_get_contents( "$IP/resources/startup.js" ); |
1096 | 1095 | if ( $context->getOnly() === 'scripts' ) { |
1097 | | - // Get all module registrations |
1098 | | - $registration = self::getModuleRegistrations( $context ); |
1099 | | - // Build configuration |
1100 | | - $config = FormatJson::encode( $this->getConfig( $context ) ); |
1101 | | - // Add a well-known start-up function |
1102 | | - $scripts .= "window.startUp = function() {\n\t$registration\n\tmediaWiki.config.set( $config );\n};\n"; |
1103 | 1096 | // Build load query for jquery and mediawiki modules |
1104 | 1097 | $query = array( |
1105 | 1098 | 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ), |
— | — | @@ -1111,17 +1104,20 @@ |
1112 | 1105 | ResourceLoader::getModule( 'mediawiki' )->getModifiedTime( $context ) |
1113 | 1106 | ), -2 ) ) |
1114 | 1107 | ); |
1115 | | - // Uniform query order |
| 1108 | + // Ensure uniform query order |
1116 | 1109 | ksort( $query ); |
1117 | | - // Build HTML code for loading jquery and mediawiki modules |
1118 | | - $loadScript = Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ); |
1119 | | - // Add code to add jquery and mediawiki loading code; only if the current client is compatible |
1120 | | - $scripts .= "if ( isCompatible() ) {\n\tdocument.write( " . FormatJson::encode( $loadScript ) . ");\n}\n"; |
1121 | | - // Delete the compatible function - it's not needed anymore |
1122 | | - $scripts .= "delete window['isCompatible'];\n"; |
| 1110 | + |
| 1111 | + // Startup function |
| 1112 | + $configuration = FormatJson::encode( $this->getConfig( $context ) ); |
| 1113 | + $registrations = self::getModuleRegistrations( $context ); |
| 1114 | + $out .= "window.startUp = function() {\n\t$registrations\n\tmediaWiki.config.set( $configuration );\n};"; |
| 1115 | + |
| 1116 | + // Conditional script injection |
| 1117 | + $scriptTag = Xml::escapeJsString( Html::linkedScript( $wgLoadScript . '?' . wfArrayToCGI( $query ) ) ); |
| 1118 | + $out .= "if ( isCompatible() ) {\n\tdocument.write( '$scriptTag' );\n}\ndelete window['isCompatible'];"; |
1123 | 1119 | } |
1124 | 1120 | |
1125 | | - return $scripts; |
| 1121 | + return $out; |
1126 | 1122 | } |
1127 | 1123 | |
1128 | 1124 | public function getModifiedTime( ResourceLoaderContext $context ) { |