Separate logic for loading messages to javascript.

This way they can be cached in browser and not loaded on each request.
This commit is contained in:
Michal Čihař
2010-04-01 15:02:23 +02:00
parent 1c83d8129b
commit 12482d58eb
3 changed files with 62 additions and 54 deletions

52
js/messages.php Normal file
View File

@@ -0,0 +1,52 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Exporting of translated messages from PHP to Javascript
*
* @package phpMyAdmin
*/
chdir('..');
// Send correct type:
header('Content-Type: text/javascript; charset=UTF-8');
// Cache output in client - the nocache query parameter makes sure that this
// file is reloaded when config changes
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 3600) . ' GMT');
require_once './libraries/common.inc.php';
$js_messages['strFormEmpty'] = $GLOBALS['strFormEmpty'];
$js_messages['strNotNumber'] = $GLOBALS['strNotNumber'];
$js_messages['strClickToSelect'] = $GLOBALS['strClickToSelect'];
$js_messages['strClickToUnselect'] = $GLOBALS['strClickToUnselect'];
$js_messages['strNoDropDatabases'] = $GLOBALS['strNoDropDatabases'];
/* For confirmations */
$js_messages['strDoYouReally'] = $GLOBALS['strDoYouReally'];
$js_messages['strDropDatabaseStrongWarning'] = $GLOBALS['strDropDatabaseStrongWarning'];
/* For blobstreaming */
$js_messages['strBLOBRepositoryDisableStrongWarning'] = $GLOBALS['strBLOBRepositoryDisableStrongWarning'];
$js_messages['strBLOBRepositoryDisableAreYouSure'] = sprintf($GLOBALS['strBLOBRepositoryDisableAreYouSure'], $GLOBALS['db']);
/* For indexes */
$js_messages['strFormEmpty'] = $GLOBALS['strFormEmpty'];
$js_messages['strNotNumber'] = $GLOBALS['strNotNumber'];
/* For server_privileges.js */
$js_messages['strHostEmpty'] = $GLOBALS['strHostEmpty'];
$js_messages['strUserEmpty'] = $GLOBALS['strUserEmpty'];
$js_messages['strPasswordEmpty'] = $GLOBALS['strPasswordEmpty'];
$js_messages['strPasswordNotSame'] = $GLOBALS['strPasswordNotSame'];
/* For inline query editing */
$js_messages['strGo'] = __('Go');
$js_messages['strCancel'] = __('Cancel');
echo "var PMA_messages = new Array();\n";
foreach ($js_messages as $name => $js_message) {
echo "PMA_messages['" . $name . "'] = '" . PMA_escapeJsString($js_message) . "';\n";
}
?>

View File

@@ -549,12 +549,6 @@ $GLOBALS['js_include'][] = 'mootools.js';
$GLOBALS['js_include'][] = 'jquery/jquery-1.4.2-min.js'; $GLOBALS['js_include'][] = 'jquery/jquery-1.4.2-min.js';
$GLOBALS['js_include'][] = 'update-location.js'; $GLOBALS['js_include'][] = 'update-location.js';
/**
* holds locale messages required by JavaScript function
* @global array $js_messages
*/
$GLOBALS['js_messages'] = array();
/** /**
* JavaScript events that will be registered * JavaScript events that will be registered
* @global array $js_events * @global array $js_events

View File

@@ -65,49 +65,12 @@ $title = str_replace(
// here, the function does not exist with this configuration: $cfg['ServerDefault'] = 0; // here, the function does not exist with this configuration: $cfg['ServerDefault'] = 0;
$is_superuser = function_exists('PMA_isSuperuser') && PMA_isSuperuser(); $is_superuser = function_exists('PMA_isSuperuser') && PMA_isSuperuser();
if (in_array('functions.js', $GLOBALS['js_include'])) {
$GLOBALS['js_messages']['strFormEmpty'] = $GLOBALS['strFormEmpty'];
$GLOBALS['js_messages']['strNotNumber'] = $GLOBALS['strNotNumber'];
$GLOBALS['js_messages']['strClickToSelect'] = $GLOBALS['strClickToSelect'];
$GLOBALS['js_messages']['strClickToUnselect'] = $GLOBALS['strClickToUnselect'];
if (!$is_superuser && !$GLOBALS['cfg']['AllowUserDropDatabase']) {
$GLOBALS['js_messages']['strNoDropDatabases'] = $GLOBALS['strNoDropDatabases'];
} else {
$GLOBALS['js_messages']['strNoDropDatabases'] = '';
}
if ($GLOBALS['cfg']['Confirm']) {
$GLOBALS['js_messages']['strDoYouReally'] = $GLOBALS['strDoYouReally'];
$GLOBALS['js_messages']['strDropDatabaseStrongWarning'] = $GLOBALS['strDropDatabaseStrongWarning'];
// rajk - for blobstreaming
$GLOBALS['js_messages']['strBLOBRepositoryDisableStrongWarning'] = $GLOBALS['strBLOBRepositoryDisableStrongWarning'];
$GLOBALS['js_messages']['strBLOBRepositoryDisableAreYouSure'] = sprintf($GLOBALS['strBLOBRepositoryDisableAreYouSure'], $GLOBALS['db']);
} else {
$GLOBALS['js_messages']['strDoYouReally'] = '';
$GLOBALS['js_messages']['strDropDatabaseStrongWarning'] = '';
// rajk - for blobstreaming
$GLOBALS['js_messages']['strBLOBRepositoryDisableStrongWarning'] = '';
$GLOBALS['js_messages']['strBLOBRepositoryDisableAreYouSure'] = '';
}
} elseif (in_array('indexes.js', $GLOBALS['js_include'])) {
$GLOBALS['js_messages']['strFormEmpty'] = $GLOBALS['strFormEmpty'];
$GLOBALS['js_messages']['strNotNumber'] = $GLOBALS['strNotNumber'];
}
if (in_array('server_privileges.js', $GLOBALS['js_include'])) {
$GLOBALS['js_messages']['strHostEmpty'] = $GLOBALS['strHostEmpty'];
$GLOBALS['js_messages']['strUserEmpty'] = $GLOBALS['strUserEmpty'];
$GLOBALS['js_messages']['strPasswordEmpty'] = $GLOBALS['strPasswordEmpty'];
$GLOBALS['js_messages']['strPasswordNotSame'] = $GLOBALS['strPasswordNotSame'];
}
$GLOBALS['js_messages']['strGo'] = __('Go');
$GLOBALS['js_messages']['strCancel'] = __('Cancel');
$GLOBALS['js_include'][] = 'tooltip.js'; $GLOBALS['js_include'][] = 'tooltip.js';
$params = array('lang' => $GLOBALS['lang']);
if (isset($GLOBALS['db'])) {
$params['db'] = $GLOBALS['db'];
}
$GLOBALS['js_include'][] = 'messages.php' . PMA_generate_common_url($params);
$GLOBALS['js_events'][] = array( $GLOBALS['js_events'][] = array(
'object' => 'window', 'object' => 'window',
@@ -121,7 +84,11 @@ $GLOBALS['js_events'][] = array(
* browser cache. This produces an HTTP 304 request for each file. * browser cache. This produces an HTTP 304 request for each file.
*/ */
foreach ($GLOBALS['js_include'] as $js_script_file) { foreach ($GLOBALS['js_include'] as $js_script_file) {
if (strpos($js_script_file, '?') === FALSE) {
echo '<script src="./js/' . $js_script_file . '?ts=' . filemtime('./js/' . $js_script_file) . '" type="text/javascript"></script>' . "\n"; echo '<script src="./js/' . $js_script_file . '?ts=' . filemtime('./js/' . $js_script_file) . '" type="text/javascript"></script>' . "\n";
} else {
echo '<script src="./js/' . $js_script_file . '" type="text/javascript"></script>' . "\n";
}
} }
?> ?>
<script type="text/javascript"> <script type="text/javascript">
@@ -132,12 +99,7 @@ if (typeof(parent.document) != 'undefined' && typeof(parent.document) != 'unknow
parent.document.title = '<?php echo PMA_sanitize(PMA_escapeJsString($title)); ?>'; parent.document.title = '<?php echo PMA_sanitize(PMA_escapeJsString($title)); ?>';
} }
var PMA_messages = new Array();
<?php <?php
foreach ($GLOBALS['js_messages'] as $name => $js_message) {
echo "PMA_messages['" . $name . "'] = '" . PMA_escapeJsString($js_message) . "';\n";
}
foreach ($GLOBALS['js_events'] as $js_event) { foreach ($GLOBALS['js_events'] as $js_event) {
echo "window.parent.addEvent(" . $js_event['object'] . ", '" . $js_event['event'] . "', " echo "window.parent.addEvent(" . $js_event['object'] . ", '" . $js_event['event'] . "', "
. $js_event['function'] . ");\n"; . $js_event['function'] . ");\n";