r55421 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55420‎ | r55421 | r55422 >
Date:18:34, 21 August 2009
Author:tparscal
Status:deferred (Comments)
Tags:
Comment:
Added support for outputting global javascript variables to the browser in a unified way.
Modified paths:
  • /trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php
@@ -12,6 +12,7 @@
1313
1414 private static $doOutput = false;
1515 private static $messages = array();
 16+ private static $variables = array();
1617 private static $styles = array();
1718 private static $styleFiles = array(
1819 'base_sets' => array(
@@ -143,17 +144,32 @@
144145 self::$messages[$i] =
145146 "'{$escapedMessageKey}':'{$escapedMessageValue}'";
146147 }
147 - // Converts array of object members to a comma delimited list
148 - $messagesList = implode( ',', self::$messages );
149148 // Add javascript to document
150 - $out->addScript(
151 - Xml::tags(
152 - 'script',
153 - array( 'type' => $wgJsMimeType ),
154 - "loadGM({{$messagesList}});"
155 - )
156 - );
157 -
 149+ if ( count( self::$messages ) > 0 ) {
 150+ $out->addScript(
 151+ Xml::tags(
 152+ 'script',
 153+ array( 'type' => $wgJsMimeType ),
 154+ 'loadGM({' . implode( ',', self::$messages ) . '});'
 155+ )
 156+ );
 157+ }
 158+ // Transforms variables into javascript global variables
 159+ foreach ( self::$variables as $key => $value ) {
 160+ $escapedVariableValue = Xml::escapeJsString( $value );
 161+ $escapedVariableKey = Xml::escapeJsString( $key );
 162+ self::$variables[$key] =
 163+ "var {$escapedVariableKey} = '{$escapedVariableValue}';";
 164+ }
 165+ if ( count( self::$variables ) > 0 ) {
 166+ $out->addScript(
 167+ Xml::tags(
 168+ 'script',
 169+ array( 'type' => $wgJsMimeType ),
 170+ implode( self::$variables )
 171+ )
 172+ );
 173+ }
158174 // Loops over each style
159175 foreach ( self::$styles as $style ) {
160176 // Add css for various styles
@@ -196,4 +212,13 @@
197213 public static function addMessages( $messages ) {
198214 self::$messages = array_merge( self::$messages, $messages );
199215 }
 216+
 217+ /**
 218+ * Adds internationalized message definitions to the document for access
 219+ * via javascript using the gM() function
 220+ * @param array $messages Key names of messages to load
 221+ */
 222+ public static function addVariables( $variables ) {
 223+ self::$variables = array_merge( self::$variables, $variables );
 224+ }
200225 }

Comments

#Comment by Catrope (talk | contribs)   17:50, 23 August 2009

What about using the MakeGlobalVariablesScript hook (see commit summary of r55498 for example usage)?

Status & tagging log