The branch, master has been updated via 4a4933c294ca719d8c9a825fcc65c5c0f1d5600c (commit) via c13898ade1cca07fcf962239b5e402c9517f0af0 (commit) via e7cc67a5f6b6c7e71b3c79ff501cf04cca443cc9 (commit) via 9be2507a58d4a32b4daaa55f39bd0cd844f99533 (commit) from 621d17957310868eaa4f497be5846c550dcd622b (commit)
- Log ----------------------------------------------------------------- commit 4a4933c294ca719d8c9a825fcc65c5c0f1d5600c Author: Michal Čihař michal@cihar.com Date: Mon Aug 15 09:22:03 2011 +0200
Add testscase for advisor escaping
commit c13898ade1cca07fcf962239b5e402c9517f0af0 Author: Michal Čihař michal@cihar.com Date: Mon Aug 15 09:19:04 2011 +0200
Escape %
commit e7cc67a5f6b6c7e71b3c79ff501cf04cca443cc9 Author: Michal Čihař michal@cihar.com Date: Mon Aug 15 09:18:15 2011 +0200
Always escape % for gettext
Otherwise it is treated like format string.
commit 9be2507a58d4a32b4daaa55f39bd0cd844f99533 Author: Michal Čihař michal@cihar.com Date: Mon Aug 15 08:56:02 2011 +0200
Factor out percent escaping
-----------------------------------------------------------------------
Summary of changes: libraries/advisor.class.php | 37 +++++++++++++++++++++++++++---------- po/ja.po | 4 ++-- scripts/advisor2php | 13 ++++--------- test/classes/Advisor_test.php | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 21 deletions(-) create mode 100644 test/classes/Advisor_test.php
diff --git a/libraries/advisor.class.php b/libraries/advisor.class.php index 75c8a8d..71d3a82 100644 --- a/libraries/advisor.class.php +++ b/libraries/advisor.class.php @@ -82,13 +82,34 @@ class Advisor }
/** + * Escapes percent string to be used in format string. + */ + function escapePercent($str) + { + return preg_replace('/%( |,|.|$)/','%%\1', $str); + } + + /** + * Wrapper function for translating. + */ + function translate($str, $param = null) + { + if (is_null($param)) { + return sprintf(_gettext(Advisor::escapePercent($str))); + } else { + $value = $this->ruleExprEvaluate($jst[1]); + return sprintf(_gettext(Advisor::escapePercent($str)), $value); + } + } + + /** * Splits justification to text and formula. */ function splitJustification($rule) { $jst = preg_split('/\s*|\s*/', $rule['justification'], 2); if (count($jst) > 1) { - $jst[0] = preg_replace('/%( |,|.|$)/','%%\1',$jst[0]); + $jst[0] = Advisor::escapePercent($jst[0]); return array($jst[0], $jst[1]); } return array($rule['justification']); @@ -104,11 +125,7 @@ class Advisor if (count($jst) > 1) { try { /* Translate */ - $jst[0] = _gettext($jst[0]); - $str = $this->ruleExprEvaluate( - 'sprintf("'.$jst[0].'",'.$jst[1].')', - strlen('sprintf("'.$jst[0].'"') - ); + $str = Advisor::translate($jst[0], $jst[1]); } catch (Exception $e) { $this->runResult['errors'][] = 'Failed formattingstring for rule ''.$rule['name'].''. PHP threw following error: '.$e->getMessage(); return; @@ -116,15 +133,15 @@ class Advisor
$rule['justification'] = $str; } else { - $rule['justification'] = _gettext($rule['justification']); + $rule['justification'] = Advisor::translate($rule['justification']); } - $rule['name'] = _gettext($rule['name']); - $rule['issue'] = _gettext($rule['issue']); + $rule['name'] = Advisor::translate($rule['name']); + $rule['issue'] = Advisor::translate($rule['issue']);
$rule['recommendation'] = preg_replace( '/{([a-z_0-9]+)}/Ui', '<a href="server_variables.php' . PMA_generate_common_url() . '#filter=\1">\1</a>', - _gettext($rule['recommendation'])); + Advisor::translate($rule['recommendation']));
break; } diff --git a/po/ja.po b/po/ja.po index 6699fa8..b78754a 100644 --- a/po/ja.po +++ b/po/ja.po @@ -11681,8 +11681,8 @@ msgid "Query Cache usage" msgstr "クエリキャッシュの使用状況"
#: po/advisory_rules.php:81 -msgid "Less than 80% of the query cache is being utilized." -msgstr "クエリキャッシュの利用率が 80% 未満です。" +msgid "Less than 80%% of the query cache is being utilized." +msgstr "クエリキャッシュの利用率が 80%% 未満です。"
#: po/advisory_rules.php:82 msgid "" diff --git a/scripts/advisor2php b/scripts/advisor2php index 28a5817..7bc8567 100644 --- a/scripts/advisor2php +++ b/scripts/advisor2php @@ -18,16 +18,11 @@ echo "/* This is automatically generated file from libraries/advisory_rules.txt
foreach($rules['rules'] as $rule) { echo "\n"; - echo "echo __('" . addslashes($rule['name']) . "');\n"; - echo "echo __('" . addslashes($rule['issue']) . "');\n"; - echo "echo __('" . addslashes($rule['recommendation']) . "');\n"; + echo "printf(__('" . addslashes(Advisor::escapePercent($rule['name'])) . "'));\n"; + echo "printf(__('" . addslashes(Advisor::escapePercent($rule['issue'])) . "'));\n"; + echo "printf(__('" . addslashes(Advisor::escapePercent($rule['recommendation'])) . "'));\n"; $jst = Advisor::splitJustification($rule); - if (count($jst) > 1) { - /* printf is used here just to ensure proper type of string */ - echo "printf(__('" . addslashes($jst[0]) . "'), 0);\n"; - } else { - echo "echo __('" . addslashes($jst[0]) . "');\n"; - } + echo "printf(__('" . addslashes(Advisor::escapePercent($jst[0])) . "'));\n"; }
?> diff --git a/test/classes/Advisor_test.php b/test/classes/Advisor_test.php new file mode 100644 index 0000000..9bcba01 --- /dev/null +++ b/test/classes/Advisor_test.php @@ -0,0 +1,33 @@ +<?php +/* vim: set expandtab sw=4 ts=4 sts=4: */ +/** + * tests for Advisor class + * + * @package phpMyAdmin-test + */ + +/* + * Include to test. + */ +require_once 'libraries/advisor.class.php'; + +class Advisor_test extends PHPUnit_Framework_TestCase +{ + /** + * @dataProvider escapeStrings + */ + public function testEscape($text, $expected) + { + $this->assertEquals(Advisor::escapePercent($text), $expected); + } + + public function escapeStrings() { + return array( + array('80%', '80%%'), + array('%s%', '%s%%'), + array('80% foo', '80%% foo'), + array('%s% foo', '%s%% foo'), + ); + } +} +?>
hooks/post-receive