Ejegg has submitted this change and it was merged.
Change subject: Generate CurrencyRates.php instead of old tables
......................................................................
Generate CurrencyRates.php instead of old tables
drush make-exchange-refs now generates CurrencyRates.php for
DonationInterface
Change-Id: I0ea2e5eb3656312aa8470036df983b99b815bdac
NOTE: had to loosen twig security policy to allow 'for' tags
---
M sites/all/modules/exchange_rates/make_exchange_refs.drush.inc
D sites/all/modules/exchange_rates/templates/ref_source.html.twig
D sites/all/modules/exchange_rates/templates/ref_source.js.twig
M sites/all/modules/exchange_rates/templates/ref_source.php.twig
M sites/all/modules/wmf_communication/Templating.php
5 files changed, 43 insertions(+), 62 deletions(-)
Approvals:
Awight: Looks good to me, approved
jenkins-bot: Verified
diff --git a/sites/all/modules/exchange_rates/make_exchange_refs.drush.inc
b/sites/all/modules/exchange_rates/make_exchange_refs.drush.inc
index 3c0244a..65f9994 100644
--- a/sites/all/modules/exchange_rates/make_exchange_refs.drush.inc
+++ b/sites/all/modules/exchange_rates/make_exchange_refs.drush.inc
@@ -7,7 +7,7 @@
*/
function make_exchange_refs_drush_command() {
$items['make-exchange-refs'] = array(
- 'description' => 'Generate reference tables of the currency exchange
rates, in multiple output formats.',
+ 'description' => 'Generate CurrencyRates.php static exchange rate
class.',
'examples' => array( 'drush make-exchange-refs' ),
);
return $items;
@@ -26,35 +26,43 @@
function drush_make_exchange_refs() {
$rates = module_invoke( 'exchange_rates', 'get_conversions' );
- $dir = __DIR__ . "/generated/";
+ $dir = __DIR__ . '/generated/';
+ if ( !is_dir( $dir ) ) {
+ mkdir( $dir );
+ }
file_put_contents(
- $dir . "ExchangeRates.html",
- exchange_rates_make_ref_content( 'html', $rates )
- );
- file_put_contents(
- $dir . "ExchangeRates.php",
+ $dir . 'CurrencyRates.php',
exchange_rates_make_ref_content( 'php', $rates )
);
- foreach ( $rates as $info ) {
- list ( $currency, $rate ) = $info;
- //TODO: a more compact template for this case
- file_put_contents(
- $dir . "ExchangeRates_{$currency}.html",
- exchange_rates_make_ref_content( 'html', array( $info ) )
- );
- }
}
/* protected */
function exchange_rates_make_ref_content( $format, $rates ) {
+ $inverse = array_map( 'exchange_rates_invert_and_round', $rates );
$twig = Templating::twig_from_directory( __DIR__ . '/templates' );
+
+ $filename = "ref_source.{$format}.twig";
$params = array(
'generated_by' => basename( __FILE__ ) . ":templates/{$filename}",
- 'last_updated' => variable_get( 'exchange_rates_bank_update', 0 ),
- 'currency_rates' => $rates,
+ 'last_updated' => date( 'Y-m-d', variable_get(
'exchange_rates_bank_update', 0 ) ),
+ 'currency_rates' => $inverse,
);
- $filename = "ref_source.{$format}.twig";
return $twig->render( $filename, $params );
}
+function exchange_rates_invert_and_round( $rate ) {
+ $value_in_usd = $rate[1];
+ if ( $value_in_usd === 0 ) {
+ return $rate;
+ }
+ $units_in_one_dollar = 1.0 / $value_in_usd;
+ if ( $units_in_one_dollar > 10 ) {
+ $rate[1] = round( $units_in_one_dollar );
+ } else if ( $units_in_one_dollar > 1 ) {
+ $rate[1] = round( $units_in_one_dollar, 2 );
+ } else {
+ $rate[1] = $units_in_one_dollar;
+ }
+ return $rate;
+}
diff --git a/sites/all/modules/exchange_rates/templates/ref_source.html.twig
b/sites/all/modules/exchange_rates/templates/ref_source.html.twig
deleted file mode 100644
index c48a193..0000000
--- a/sites/all/modules/exchange_rates/templates/ref_source.html.twig
+++ /dev/null
@@ -1,3 +0,0 @@
-<script type="text/javascript">
-{% include 'ref_source.js.twig' %}
-</script>
diff --git a/sites/all/modules/exchange_rates/templates/ref_source.js.twig
b/sites/all/modules/exchange_rates/templates/ref_source.js.twig
deleted file mode 100644
index ae9da99..0000000
--- a/sites/all/modules/exchange_rates/templates/ref_source.js.twig
+++ /dev/null
@@ -1,15 +0,0 @@
-/**
- * Automatically generated from {{ generated_by }}
- * -- do not edit! --
- * Rates retrieved on {{ last_updated|date }}, are specified in value USD.
- */
-mw.exchangeRates = {
- data: {
-{% for conversion in currency_rates %}
- '{{ conversion[0] }}' : {{ conversion[1] }}{{ loop.last ? '' : ',' }}
-{% endfor %}
- },
- get: function ( currency ) {
- return this.data[currency];
- }
-};
diff --git a/sites/all/modules/exchange_rates/templates/ref_source.php.twig
b/sites/all/modules/exchange_rates/templates/ref_source.php.twig
index 4981fd4..707bd60 100644
--- a/sites/all/modules/exchange_rates/templates/ref_source.php.twig
+++ b/sites/all/modules/exchange_rates/templates/ref_source.php.twig
@@ -2,34 +2,24 @@
/**
* Automatically generated from {{ generated_by }}
* -- do not edit! --
- * Rates retrieved on {{ last_updated|date }}, are specified in value USD.
- *
- * Currency conversion is pulled from dynamic sources if available,
- * otherwise the lookup is performed on inline reference data.
+ * Instead, run drush make_exchange_rates and look in the 'generated' folder.
*/
-class ExchangeRates {
- static function getConversionRate( $currency ) {
- if ( is_callable( 'exchange_rate_convert' ) ) {
- return exchange_rate_convert( $currency );
- }
- return self::getReferenceRate( $currency );
- }
- /**
- * Serve approximate exchange rates which might be out-of-date.
- */
- static protected function getReferenceRate( $currency ) {
- $referenceRates = self::getReferenceData();
- if ( array_key_exists( $currency, $referenceRates ) ) {
- return $referenceRates[$currency];
- }
- }
+class CurrencyRates {
+ /**
+ * Supplies rough (not up-to-date) conversion rates for currencies
+ */
- static protected function getReferenceData() {
- return array(
+ static public $lastUpdated = '{{ last_updated }}';
+
+ static public function getCurrencyRates() {
+ // Not rounding numbers under 1 because I don't think that's a
big issue and could cause issues with the max check.
+ $currencyRates = array(
{% for conversion in currency_rates %}
- '{{ conversion[0] }}' => {{ conversion[1] }},
+ '{{ conversion[0] }}' => {{ conversion[1] }},
{% endfor %}
- );
- }
+ );
+
+ return $currencyRates;
+ }
}
diff --git a/sites/all/modules/wmf_communication/Templating.php
b/sites/all/modules/wmf_communication/Templating.php
index 4d9efaf..71b9b71 100644
--- a/sites/all/modules/wmf_communication/Templating.php
+++ b/sites/all/modules/wmf_communication/Templating.php
@@ -171,7 +171,8 @@
class RestrictiveSecurityPolicy extends Twig_Sandbox_SecurityPolicy {
function __construct() {
$tags = array(
- 'if'
+ 'if',
+ 'for',
);
$filters = array(
'escape',
--
To view, visit https://gerrit.wikimedia.org/r/223971
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I0ea2e5eb3656312aa8470036df983b99b815bdac
Gerrit-PatchSet: 3
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits