Ejegg has submitted this change and it was merged.
Change subject: finish moving Twig support into wmf_communication
......................................................................
finish moving Twig support into wmf_communication
* Render Twig templates using Sandbox extension for protection.
* Use Twig from Composer.
Change-Id: Iae504abc33b4aa78d0f92628d8721a93f7c0044e
---
M sites/all/modules/exchange_rates/exchange_rates.info
M sites/all/modules/exchange_rates/make_exchange_refs.drush.inc
D sites/all/modules/wmf_common/twig.inc
M sites/all/modules/wmf_common/wmf_common.info
M sites/all/modules/wmf_common/wmf_common.module
M sites/all/modules/wmf_communication/Templating.php
R sites/all/modules/wmf_communication/twigext_l10n/CldrData.php
R sites/all/modules/wmf_communication/twigext_l10n/TwigLocalization.php
R
sites/all/modules/wmf_communication/twigext_l10n/tests/TwigLocalizationTest.test
R sites/all/modules/wmf_communication/twigext_l10n/tests/twigext_l10n_tests.info
R
sites/all/modules/wmf_communication/twigext_l10n/tests/twigext_l10n_tests.module
M sites/all/modules/wmf_communication/wmf_communication.info
M sites/all/modules/wmf_unsubscribe/wmf_unsubscribe.module
13 files changed, 65 insertions(+), 90 deletions(-)
Approvals:
Ejegg: Looks good to me, approved
diff --git a/sites/all/modules/exchange_rates/exchange_rates.info
b/sites/all/modules/exchange_rates/exchange_rates.info
index 37b6d62..9605b13 100644
--- a/sites/all/modules/exchange_rates/exchange_rates.info
+++ b/sites/all/modules/exchange_rates/exchange_rates.info
@@ -2,6 +2,7 @@
description = Downloads exchange rates and converts currencies.
core = 7.x
version = 2.x-dev
+dependencies[] = wmf_communication
files[] = retrievers/EcbRetriever.php
files[] = retrievers/ExchangeRateRetriever.php
-files[] = retrievers/OandaRetriever.php
\ No newline at end of file
+files[] = retrievers/OandaRetriever.php
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 510a29a..3c0244a 100644
--- a/sites/all/modules/exchange_rates/make_exchange_refs.drush.inc
+++ b/sites/all/modules/exchange_rates/make_exchange_refs.drush.inc
@@ -1,4 +1,7 @@
<?php
+
+use wmf_communication\Templating;
+
/**
* Implementation of hook_drush_command()
*/
@@ -44,17 +47,14 @@
/* protected */
function exchange_rates_make_ref_content( $format, $rates ) {
- static $twig = null;
+ $twig = Templating::twig_from_directory( __DIR__ . '/templates' );
- if ( !$twig ) {
- module_load_include( 'inc', 'wmf_common', 'twig' );
- $twig = wmf_common_get_twig( __DIR__ . "/templates" );
- }
-
- $filename = "ref_source.{$format}.twig";
- return $twig->render( $filename, array(
+ $params = array(
'generated_by' => basename( __FILE__ ) . ":templates/{$filename}",
'last_updated' => variable_get( 'exchange_rates_bank_update', 0 ),
'currency_rates' => $rates,
- ) );
+ );
+
+ $filename = "ref_source.{$format}.twig";
+ return $twig->render( $filename, $params );
}
diff --git a/sites/all/modules/wmf_common/twig.inc
b/sites/all/modules/wmf_common/twig.inc
deleted file mode 100644
index f3bd13a..0000000
--- a/sites/all/modules/wmf_common/twig.inc
+++ /dev/null
@@ -1,33 +0,0 @@
-<?php
-/**
- * Low-level interface to Symfony Twig. Please use the
wmf_communication::Templating class instead.
- */
-
-function wmf_common_get_twig_path()
-{
- return variable_get(
- 'wmf_common_twig_location',
- drupal_get_path('module', 'wmf_common') . DIRECTORY_SEPARATOR . "Twig"
- );
-}
-
-function wmf_common_get_twig( $template_dir )
-{
- $twig_path = wmf_common_get_twig_path();
- require_once "{$twig_path}/lib/Twig/Autoloader.php";
-
- Twig_Autoloader::register();
-
- $cache_dir = drupal_realpath( file_default_scheme() . '://' ) .
'/twig/cache';
-
- $loader = new Twig_Loader_Filesystem( $template_dir );
- $twig = new Twig_Environment( $loader, array(
- 'cache' => $cache_dir,
- 'auto_reload' => true,
- 'charset' => 'utf-8',
- ) );
-
- $twig->addExtension( new TwigLocalization() );
-
- return $twig;
-}
diff --git a/sites/all/modules/wmf_common/wmf_common.info
b/sites/all/modules/wmf_common/wmf_common.info
index 559fa7b..44419b9 100755
--- a/sites/all/modules/wmf_common/wmf_common.info
+++ b/sites/all/modules/wmf_common/wmf_common.info
@@ -7,6 +7,4 @@
files[] = Queue.php
files[] = WmfDatabase.php
files[] = WmfException.php
-files[] = twigext_l10n/CldrData.php
-files[] = twigext_l10n/TwigLocalization.php
files[] = tests/include/BaseWmfDrupalPhpUnitTestCase.php
diff --git a/sites/all/modules/wmf_common/wmf_common.module
b/sites/all/modules/wmf_common/wmf_common.module
index 810f20d..3d12077 100644
--- a/sites/all/modules/wmf_common/wmf_common.module
+++ b/sites/all/modules/wmf_common/wmf_common.module
@@ -84,18 +84,6 @@
'#description' => t('Comma-delimited (no spaces) list of donor email
addresses that will never trigger failmail'),
);
- $twig_path = variable_get('wmf_common_twig_location',
drupal_get_path('module', 'wmf_common') . DIRECTORY_SEPARATOR . "Twig");
- $twig_installed = file_exists(implode(DIRECTORY_SEPARATOR, array($twig_path,
'lib', 'Twig', 'Autoloader.php')));
- $twig_msg = $twig_installed ? '<span style="color:green;">Installed
Correctly</span>' : '<span style="color:red;">Please install</span>';
-
- $form['wmf_common_twig_location'] = array(
- '#type' => 'textfield',
- '#title' => t('Twig location'),
- '#default_value' => $twig_path,
- '#required' => TRUE,
- '#description' => "{$twig_msg} " . t('Filesystem path of the Twig
library. Try: <code>git clone
https://gerrit.wikimedia.org/r/wikimedia/fundraising/twig</code>'),
- );
-
$phpmailer_path = variable_get('wmf_common_phpmailer_location',
drupal_get_path('module', 'wmf_common') . DIRECTORY_SEPARATOR . "PHPMailer");
$phpmailer_installed = file_exists(implode(DIRECTORY_SEPARATOR,
array($phpmailer_path, 'class.phpmailer.php')));
$phpmailer_msg = $phpmailer_installed ? '<span
style="color:green;">Installed Correctly</span>' : '<span
style="color:red;">Please install. Try: <code>git clone
https://gerrit.wikimedia.org/r/wikimedia/fundraising/phpmailer</code></span>';
diff --git a/sites/all/modules/wmf_communication/Templating.php
b/sites/all/modules/wmf_communication/Templating.php
index ef0594e..1550120 100644
--- a/sites/all/modules/wmf_communication/Templating.php
+++ b/sites/all/modules/wmf_communication/Templating.php
@@ -2,9 +2,11 @@
use \Exception;
-// FIXME: encapsulate here, not in wmf_common
use \Twig_Environment;
+use \Twig_Extension_Sandbox;
+use \Twig_Loader_Filesystem;
use \Twig_Loader_String;
+use \Twig_Sandbox_SecurityPolicy;
use \TwigLocalization;
@@ -35,12 +37,7 @@
* @param string $format initial rendering mode (TODO: review usage)
*/
function __construct( $templates_dir, $template_name, $lang_code,
$template_params, $format = null ) {
- // TODO: autoloaded Twig::get
- if ( !function_exists( 'wmf_common_get_twig' ) ) {
- module_load_include( 'inc', 'wmf_common', 'twig' );
- }
- $this->twig = wmf_common_get_twig( $templates_dir );
- //TODO: class TemplatingTwig ; $this->twig = WmfTwig( $templates_dir );
+ $this->twig = Templating::twig_from_directory( $templates_dir );
$this->templates_dir = $templates_dir;
$this->template_name = $template_name;
@@ -52,6 +49,49 @@
$this->language = $lang_code;
$this->template_params = $template_params;
$this->format = $format;
+ }
+
+ static function twig_from_directory( $template_dir ) {
+ # FIXME: Autoload in settings.php rather than assuming dir structure
here.
+ require_once DRUPAL_ROOT . '/../vendor/autoload.php';
+
+ $loader = new Twig_Loader_Filesystem( $template_dir );
+ return Templating::twig_from_loader( $loader );
+ }
+
+ static protected function twig_from_loader( $loader ) {
+ $cache_dir = drupal_realpath( file_default_scheme() . '://' ) .
'/twig/cache';
+
+ $twig = new Twig_Environment( $loader, array(
+ 'cache' => $cache_dir,
+ 'auto_reload' => true,
+ 'charset' => 'utf-8',
+ ) );
+
+ $twig->addExtension( new TwigLocalization() );
+
+ $tags = array(
+ 'if'
+ );
+ $filters = array(
+ 'escape',
+ 'l10n_currency',
+ 'raw',
+ );
+ $methods = array(
+ //'Article' => array('getTitle', 'getBody'),
+ );
+ $properties = array(
+ //'Article' => array('title', 'body'),
+ );
+ $functions = array(
+ //'range'
+ );
+ $policy = new Twig_Sandbox_SecurityPolicy( $tags, $filters, $methods,
$properties, $functions );
+ $sandbox = new Twig_Extension_Sandbox( $policy, true );
+ $twig->addExtension( $sandbox );
+
+ return $twig;
}
/**
@@ -141,17 +181,10 @@
* TODO: clean up interface
*/
static function renderStringTemplate( $template, $params ) {
- // TODO: autoload instead
- if ( !class_exists( 'Twig_Loader_String' ) ) {
- module_load_include( 'inc', 'wmf_common', 'twig' );
-
- // FIXME: throwaway call to initialize
- wmf_common_get_twig( __DIR__ );
- }
+ require_once DRUPAL_ROOT . '/../vendor/autoload.php';
$loader = new Twig_Loader_String();
- $twig = new Twig_Environment( $loader );
- $twig->addExtension( new TwigLocalization() );
+ $twig = Templating::twig_from_loader( $loader );
return $twig->render( $template, $params );
}
diff --git a/sites/all/modules/wmf_common/twigext_l10n/CldrData.php
b/sites/all/modules/wmf_communication/twigext_l10n/CldrData.php
similarity index 100%
rename from sites/all/modules/wmf_common/twigext_l10n/CldrData.php
rename to sites/all/modules/wmf_communication/twigext_l10n/CldrData.php
diff --git a/sites/all/modules/wmf_common/twigext_l10n/TwigLocalization.php
b/sites/all/modules/wmf_communication/twigext_l10n/TwigLocalization.php
similarity index 100%
rename from sites/all/modules/wmf_common/twigext_l10n/TwigLocalization.php
rename to sites/all/modules/wmf_communication/twigext_l10n/TwigLocalization.php
diff --git
a/sites/all/modules/wmf_common/twigext_l10n/tests/TwigLocalizationTest.test
b/sites/all/modules/wmf_communication/twigext_l10n/tests/TwigLocalizationTest.test
similarity index 100%
rename from
sites/all/modules/wmf_common/twigext_l10n/tests/TwigLocalizationTest.test
rename to
sites/all/modules/wmf_communication/twigext_l10n/tests/TwigLocalizationTest.test
diff --git
a/sites/all/modules/wmf_common/twigext_l10n/tests/twigext_l10n_tests.info
b/sites/all/modules/wmf_communication/twigext_l10n/tests/twigext_l10n_tests.info
similarity index 100%
rename from
sites/all/modules/wmf_common/twigext_l10n/tests/twigext_l10n_tests.info
rename to
sites/all/modules/wmf_communication/twigext_l10n/tests/twigext_l10n_tests.info
diff --git
a/sites/all/modules/wmf_common/twigext_l10n/tests/twigext_l10n_tests.module
b/sites/all/modules/wmf_communication/twigext_l10n/tests/twigext_l10n_tests.module
similarity index 100%
rename from
sites/all/modules/wmf_common/twigext_l10n/tests/twigext_l10n_tests.module
rename to
sites/all/modules/wmf_communication/twigext_l10n/tests/twigext_l10n_tests.module
diff --git a/sites/all/modules/wmf_communication/wmf_communication.info
b/sites/all/modules/wmf_communication/wmf_communication.info
index f35cf06..1d06663 100644
--- a/sites/all/modules/wmf_communication/wmf_communication.info
+++ b/sites/all/modules/wmf_communication/wmf_communication.info
@@ -12,6 +12,8 @@
files[] = Recipient.php
files[] = Templating.php
files[] = Translation.php
+files[] = twigext_l10n/CldrData.php
+files[] = twigext_l10n/TwigLocalization.php
files[] = CiviMail/CiviMailBulkStore.php
files[] = CiviMail/CiviMailStore.php
files[] = CiviMail/CiviMailingRecord.php
diff --git a/sites/all/modules/wmf_unsubscribe/wmf_unsubscribe.module
b/sites/all/modules/wmf_unsubscribe/wmf_unsubscribe.module
index 90721a3..0745f1b 100755
--- a/sites/all/modules/wmf_unsubscribe/wmf_unsubscribe.module
+++ b/sites/all/modules/wmf_unsubscribe/wmf_unsubscribe.module
@@ -1,5 +1,7 @@
<?php
+use wmf_communication\Templating;
+
/**
* Implements hook_menu
*/
@@ -165,23 +167,7 @@
*/
function render_page( $page, $params=array(), $language='en' ){
- $twig_include = implode(DIRECTORY_SEPARATOR,
array(variable_get('wmf_common_twig_location', ''), 'lib', 'Twig',
'Autoloader.php'));
- if (!file_exists($twig_include))
- {
- watchdog("wmf_unsubscribe", "Twig libraries not found", array(),
WATCHDOG_ERROR);
- return "";
-
- }
- require_once $twig_include;
-
- Twig_Autoloader::register();
-
- $loader = new Twig_Loader_Filesystem( drupal_get_path( 'module',
'wmf_unsubscribe' ) . '/templates/i18n/' );
- $twig = new Twig_Environment( $loader, array(
- 'cache' => '/tmp/twig/cache/wmf_unsubscribe',
- 'auto_reload' => TRUE,
- 'charset' => 'utf-8',
- ));
+ $twig = Templating::twig_from_directory( __DIR__ . '/templates/i18n/' );
try{
$template = $twig->loadTemplate( "$page.$language.html" );
--
To view, visit https://gerrit.wikimedia.org/r/161412
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Iae504abc33b4aa78d0f92628d8721a93f7c0044e
Gerrit-PatchSet: 6
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: Katie Horn <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits