r73680 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73679‎ | r73680 | r73681 >
Date:21:03, 24 September 2010
Author:tparscal
Status:ok (Comments)
Tags:
Comment:
Moved more generated javascript to ResourceLoader functions. Cleaned some things up in the startup module.
Modified paths:
  • /trunk/phase3/includes/ResourceLoader.php (modified) (history)
  • /trunk/phase3/includes/ResourceLoaderModule.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/ResourceLoader.php
@@ -437,4 +437,47 @@
438438 return "mediaWiki.loader.state( '$name', '$state' );\n";
439439 }
440440 }
 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+ }
441484 }
Index: trunk/phase3/includes/ResourceLoaderModule.php
@@ -1048,7 +1048,7 @@
10491049 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
10501050 wfProfileIn( __METHOD__ );
10511051
1052 - $scripts = '';
 1052+ $out = '';
10531053 $registrations = array();
10541054 foreach ( ResourceLoader::getModules() as $name => $module ) {
10551055 // Support module loader scripts
@@ -1056,8 +1056,7 @@
10571057 $deps = FormatJson::encode( $module->getDependencies() );
10581058 $group = FormatJson::encode( $module->getGroup() );
10591059 $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 );
10621061 }
10631062 // Automatically register module
10641063 else {
@@ -1080,7 +1079,7 @@
10811080 }
10821081 }
10831082 }
1084 - $out = $scripts . "mediaWiki.loader.register( " . FormatJson::encode( $registrations ) . " );";
 1083+ $out .= ResourceLoader::makeLoaderRegisterScript( $registrations );
10851084
10861085 wfProfileOut( __METHOD__ );
10871086 return $out;
@@ -1091,14 +1090,8 @@
10921091 public function getScript( ResourceLoaderContext $context ) {
10931092 global $IP, $wgLoadScript;
10941093
1095 - $scripts = file_get_contents( "$IP/resources/startup.js" );
 1094+ $out = file_get_contents( "$IP/resources/startup.js" );
10961095 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";
11031096 // Build load query for jquery and mediawiki modules
11041097 $query = array(
11051098 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
@@ -1111,17 +1104,20 @@
11121105 ResourceLoader::getModule( 'mediawiki' )->getModifiedTime( $context )
11131106 ), -2 ) )
11141107 );
1115 - // Uniform query order
 1108+ // Ensure uniform query order
11161109 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'];";
11231119 }
11241120
1125 - return $scripts;
 1121+ return $out;
11261122 }
11271123
11281124 public function getModifiedTime( ResourceLoaderContext $context ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r74679Simplifies r73680 by letting FormatJson::encode do it's magic with handling n...tparscal17:52, 12 October 2010

Comments

#Comment by Catrope (talk | contribs)   21:29, 1 October 2010
+		if ( is_array( $dependencies ) ) {
+			$dependencies = FormatJson::encode( $dependencies );
+		} else if ( is_string( $dependencies ) ) {
+			$dependencies = "'" . Xml::escapeJsString( $dependencies ) . "'";
+		} else {
+			$dependencies = 'null';
+		}

Strings and even null will be encoded by FormatJson::encode() just fine, no need to reinvent the wheel here.

#Comment by Trevor Parscal (WMF) (talk | contribs)   22:10, 1 October 2010

Yeah, I caught this while testing out custom loader scripts. r74107 fixes it.

#Comment by Catrope (talk | contribs)   14:31, 3 October 2010

That's not what I meant; I hadn't noticed that it was double-encoding things, good catch.

What I meant is that you're treating strings and null specially and avoid running them through FormatJson::encode(), encoding them yourself instead. This is unnecessary because FormatJson::encode() encodes them exactly the same way your DIY way does.

Status & tagging log