- Patch #356074 by chx, Damien Tournoud: provide a sequences API.

merge-requests/26/head
Dries Buytaert 2009-10-16 03:01:55 +00:00
parent b965f7478f
commit 13d3072f41
11 changed files with 193 additions and 63 deletions

View File

@ -14,7 +14,7 @@
* appropriate places in the template. Processed e-mail templates are * appropriate places in the template. Processed e-mail templates are
* requested from hook_mail() from the module sending the e-mail. Any module * requested from hook_mail() from the module sending the e-mail. Any module
* can modify the composed e-mail message array using hook_mail_alter(). * can modify the composed e-mail message array using hook_mail_alter().
* Finally drupal_mail_sending_system()->mail() sends the e-mail, which can * Finally drupal_mail_system()->mail() sends the e-mail, which can
* be reused if the exact same composed e-mail is to be sent to multiple * be reused if the exact same composed e-mail is to be sent to multiple
* recipients. * recipients.
* *
@ -78,8 +78,8 @@
* @param $from * @param $from
* Sets From to this value, if given. * Sets From to this value, if given.
* @param $send * @param $send
* Send the message directly, without calling * Send the message directly, without calling drupal_mail_system()->mail()
* drupal_mail_sending_system()->mail() manually. * manually.
* @return * @return
* The $message array structure containing all details of the * The $message array structure containing all details of the
* message. If already sent ($send = TRUE), then the 'result' element * message. If already sent ($send = TRUE), then the 'result' element
@ -93,6 +93,8 @@ function drupal_mail($module, $key, $to, $language, $params = array(), $from = N
// Bundle up the variables into a structured array for altering. // Bundle up the variables into a structured array for altering.
$message = array( $message = array(
'id' => $module . '_' . $key, 'id' => $module . '_' . $key,
'module' => $module,
'key' => $key,
'to' => $to, 'to' => $to,
'from' => isset($from) ? $from : $default_from, 'from' => isset($from) ? $from : $default_from,
'language' => $language, 'language' => $language,
@ -129,12 +131,15 @@ function drupal_mail($module, $key, $to, $language, $params = array(), $from = N
// Invoke hook_mail_alter() to allow all modules to alter the resulting e-mail. // Invoke hook_mail_alter() to allow all modules to alter the resulting e-mail.
drupal_alter('mail', $message); drupal_alter('mail', $message);
// Concatenate and wrap the e-mail body. // Retrieve the responsible implementation for this message.
$message['body'] = is_array($message['body']) ? drupal_wrap_mail(implode("\n\n", $message['body'])) : drupal_wrap_mail($message['body']); $system = drupal_mail_system($module, $key);
// Format the message body.
$message = $system->format($message);
// Optionally send e-mail. // Optionally send e-mail.
if ($send) { if ($send) {
$message['result'] = drupal_mail_sending_system($module, $key)->mail($message); $message['result'] = $system->mail($message);
// Log errors // Log errors
if (!$message['result']) { if (!$message['result']) {
@ -149,11 +154,23 @@ function drupal_mail($module, $key, $to, $language, $params = array(), $from = N
/** /**
* Returns an object that implements the MailSystemInterface. * Returns an object that implements the MailSystemInterface.
* *
* Allows for one or more custom mail backends to send mail messages * Allows for one or more custom mail backends to format and send mail messages
* composed using drupal_mail(). * composed using drupal_mail().
* *
* An implementation needs to implement the following methods:
* - format: Allows to preprocess, format, and postprocess a mail
* message before it is passed to the sending system. By default, all messages
* may contain HTML and are converted to plain-text by the DefaultMailSystem
* implementation. For example, an alternative implementation could override
* the default implementation and additionally sanitize the HTML for usage in
* a MIME-encoded e-mail, but still invoking the DefaultMailSystem
* implementation to generate an alternate plain-text version for sending.
* - mail: Sends a message through a custom mail sending engine.
* By default, all messages are sent via PHP's mail() function by the
* DefaultMailSystem implementation.
*
* The selection of a particular implementation is controlled via the variable * The selection of a particular implementation is controlled via the variable
* 'mail_sending_system', which is a keyed array. The default implementation * 'mail_system', which is a keyed array. The default implementation
* is the class whose name is the value of 'default-system' key. A more specific * is the class whose name is the value of 'default-system' key. A more specific
* match first to key and then to module will be used in preference to the * match first to key and then to module will be used in preference to the
* default. To specificy a different class for all mail sent by one module, set * default. To specificy a different class for all mail sent by one module, set
@ -195,11 +212,12 @@ function drupal_mail($module, $key, $to, $language, $params = array(), $from = N
* A key to identify the e-mail sent. The final e-mail ID for the e-mail * A key to identify the e-mail sent. The final e-mail ID for the e-mail
* alter hook in drupal_mail() would have been {$module}_{$key}. * alter hook in drupal_mail() would have been {$module}_{$key}.
*/ */
function drupal_mail_sending_system($module, $key) { function drupal_mail_system($module, $key) {
$instances = &drupal_static(__FUNCTION__, array()); $instances = &drupal_static(__FUNCTION__, array());
$id = $module . '_' . $key; $id = $module . '_' . $key;
$configuration = variable_get('mail_sending_system', array('default-system' => 'DefaultMailSystem'));
$configuration = variable_get('mail_system', array('default-system' => 'DefaultMailSystem'));
// Look for overrides for the default class, starting from the most specific // Look for overrides for the default class, starting from the most specific
// id, and falling back to the module name. // id, and falling back to the module name.
@ -230,7 +248,18 @@ function drupal_mail_sending_system($module, $key) {
*/ */
interface MailSystemInterface { interface MailSystemInterface {
/** /**
* Send an e-mail message composed by drupal_mail(). * Format a message composed by drupal_mail() prior sending.
*
* @param $message
* A message array, as described in hook_mail_alter().
*
* @return
* The formatted $message.
*/
public function format(array $message);
/**
* Send a message composed by drupal_mail().
* *
* @param $message * @param $message
* Message array with at least the following elements: * Message array with at least the following elements:
@ -452,9 +481,10 @@ function drupal_html_to_text($string, $allowed_tags = NULL) {
} }
// Process blocks of text. // Process blocks of text.
else { else {
// Convert inline HTML text to plain text. // Convert inline HTML text to plain text; not removing line-breaks or
$value = trim(preg_replace('/\s+/', ' ', decode_entities($value))); // white-space, since that breaks newlines when sanitizing plain-text.
if (strlen($value)) { $value = trim(decode_entities($value));
if (drupal_strlen($value)) {
$chunk = $value; $chunk = $value;
} }
} }
@ -466,7 +496,7 @@ function drupal_html_to_text($string, $allowed_tags = NULL) {
$chunk = $casing($chunk); $chunk = $casing($chunk);
} }
// Format it and apply the current indentation. // Format it and apply the current indentation.
$output .= drupal_wrap_mail($chunk, implode('', $indent)) . "\n"; $output .= drupal_wrap_mail($chunk, implode('', $indent));
// Remove non-quotation markers from indentation. // Remove non-quotation markers from indentation.
$indent = array_map('_drupal_html_to_text_clean', $indent); $indent = array_map('_drupal_html_to_text_clean', $indent);
} }

View File

@ -361,6 +361,8 @@ function module_implements($hook, $sort = FALSE, $reset = FALSE) {
if ($reset) { if ($reset) {
$implementations = array(); $implementations = array();
cache_set('module_implements', array()); cache_set('module_implements', array());
drupal_static_reset('module_hook_info');
cache_clear_all('hook_info', 'cache');
return; return;
} }
@ -376,18 +378,25 @@ function module_implements($hook, $sort = FALSE, $reset = FALSE) {
} }
if (!isset($implementations[$hook])) { if (!isset($implementations[$hook])) {
$hook_info = module_hook_info();
$implementations[$hook] = array(); $implementations[$hook] = array();
$list = module_list(FALSE, FALSE, $sort); $list = module_list(FALSE, FALSE, $sort);
foreach ($list as $module) { foreach ($list as $module) {
if (module_hook($module, $hook)) { $include_file = FALSE;
$implementations[$hook][$module] = $module; if (module_hook($module, $hook) || (isset($hook_info[$hook]['group']) && $include_file = module_load_include('inc', $module, $module . '.' . $hook_info[$hook]['group']) && module_hook($module, $hook))) {
$implementations[$hook][$module] = $include_file ? $hook_info[$hook]['group'] : FALSE;
// We added something to the cache, so write it when we are done. // We added something to the cache, so write it when we are done.
$implementations['#write_cache'] = TRUE; $implementations['#write_cache'] = TRUE;
} }
} }
} }
else { else {
foreach ($implementations[$hook] as $module) { foreach ($implementations[$hook] as $module => $group) {
// If this hook implementation is stored in a lazy-loaded file, so include
// that file first.
if ($group) {
module_load_include('inc', $module, "$module.$group");
}
// It is possible that a module removed a hook implementation without the // It is possible that a module removed a hook implementation without the
// implementations cache being rebuilt yet, so we check module_hook() on // implementations cache being rebuilt yet, so we check module_hook() on
// each request to avoid undefined function errors. // each request to avoid undefined function errors.
@ -400,13 +409,45 @@ function module_implements($hook, $sort = FALSE, $reset = FALSE) {
} }
} }
// The explicit cast forces a copy to be made. This is needed because return array_keys($implementations[$hook]);
// $implementations[$hook] is only a reference to an element of }
// $implementations and if there are nested foreaches (due to nested node
// API calls, for example), they would both manipulate the same array's /**
// references, which causes some modules' hooks not to be called. * Retrieve a list of what hooks are explicitly declared.
// See also http://www.zend.com/zend/art/ref-count.php. */
return (array)$implementations[$hook]; function module_hook_info() {
$hook_info = &drupal_static(__FUNCTION__, array());
if (empty($hook_info)) {
$cache = cache_get('hook_info');
if ($cache === FALSE) {
// Rebuild the cache and save it.
// We can't use module_invoke_all() here or it would cause an infinite
// loop.
foreach (module_list() as $module) {
$function = $module . '_hook_info';
if (function_exists($function)) {
$result = $function();
if (isset($result) && is_array($result)) {
$hook_info = array_merge_recursive($hook_info, $result);
}
}
}
// We can't use drupal_alter() for the same reason as above.
foreach (module_list() as $module) {
$function = $module . '_hook_info_alter';
if (function_exists($function)) {
$function($hook_info);
}
}
cache_set('hook_info', $hook_info);
}
else {
$hook_info = $cache->data;
}
}
return $hook_info;
} }
/** /**

View File

@ -153,7 +153,6 @@ function token_scan($text) {
function token_generate($type, array $tokens, array $data = array(), array $options = array()) { function token_generate($type, array $tokens, array $data = array(), array $options = array()) {
$results = array(); $results = array();
$options += array('sanitize' => TRUE); $options += array('sanitize' => TRUE);
_token_initialize();
$result = module_invoke_all('tokens', $type, $tokens, $data, $options); $result = module_invoke_all('tokens', $type, $tokens, $data, $options);
foreach ($result as $original => $replacement) { foreach ($result as $original => $replacement) {
@ -227,25 +226,8 @@ function token_find_with_prefix(array $tokens, $prefix, $delimiter = ':') {
function token_info() { function token_info() {
$data = &drupal_static(__FUNCTION__); $data = &drupal_static(__FUNCTION__);
if (!isset($data)) { if (!isset($data)) {
_token_initialize();
$data = module_invoke_all('token_info'); $data = module_invoke_all('token_info');
drupal_alter('token_info', $data); drupal_alter('token_info', $data);
} }
return $data; return $data;
} }
/**
* Load modulename.tokens.inc for all enabled modules.
*/
function _token_initialize() {
$initialized = &drupal_static(__FUNCTION__);
if (!$initialized) {
foreach (module_list() as $module) {
$filename = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$module.tokens.inc";
if (file_exists($filename)) {
include_once $filename;
}
}
$initialized = TRUE;
}
}

View File

@ -174,7 +174,7 @@ class ContactSitewideTestCase extends DrupalWebTestCase {
// We are testing the auto-reply, so there should be one e-mail going to the sender. // We are testing the auto-reply, so there should be one e-mail going to the sender.
$captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'foo@example.com')); $captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'foo@example.com'));
$this->assertEqual(count($captured_emails), 1, t('Auto-reply e-mail was sent to the sender for category "foo".'), t('Contact')); $this->assertEqual(count($captured_emails), 1, t('Auto-reply e-mail was sent to the sender for category "foo".'), t('Contact'));
$this->assertEqual($captured_emails[0]['body'], $foo_autoreply, t('Auto-reply e-mail body is correct for category "foo".'), t('Contact')); $this->assertEqual($captured_emails[0]['body'], drupal_html_to_text($foo_autoreply), t('Auto-reply e-mail body is correct for category "foo".'), t('Contact'));
// Test the auto-reply for category 'bar'. // Test the auto-reply for category 'bar'.
$email = $this->randomName(32) . '@example.com'; $email = $this->randomName(32) . '@example.com';
@ -183,7 +183,7 @@ class ContactSitewideTestCase extends DrupalWebTestCase {
// Auto-reply for category 'bar' should result in one auto-reply e-mail to the sender. // Auto-reply for category 'bar' should result in one auto-reply e-mail to the sender.
$captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'bar@example.com')); $captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'bar@example.com'));
$this->assertEqual(count($captured_emails), 1, t('Auto-reply e-mail was sent to the sender for category "bar".'), t('Contact')); $this->assertEqual(count($captured_emails), 1, t('Auto-reply e-mail was sent to the sender for category "bar".'), t('Contact'));
$this->assertEqual($captured_emails[0]['body'], $bar_autoreply, t('Auto-reply e-mail body is correct for category "bar".'), t('Contact')); $this->assertEqual($captured_emails[0]['body'], drupal_html_to_text($bar_autoreply), t('Auto-reply e-mail body is correct for category "bar".'), t('Contact'));
// Verify that no auto-reply is sent when the auto-reply field is left blank. // Verify that no auto-reply is sent when the auto-reply field is left blank.
$email = $this->randomName(32) . '@example.com'; $email = $this->randomName(32) . '@example.com';

View File

@ -1107,7 +1107,7 @@ class DrupalWebTestCase extends DrupalTestCase {
$language = language_default(); $language = language_default();
// Use the test mail class instead of the default mail handler class. // Use the test mail class instead of the default mail handler class.
variable_set('mail_sending_system', array('default-system' => 'TestingMailSystem')); variable_set('mail_system', array('default-system' => 'TestingMailSystem'));
// Use temporary files directory with the same prefix as the database. // Use temporary files directory with the same prefix as the database.
$public_files_directory = $this->originalFileDirectory . '/' . $db_prefix; $public_files_directory = $this->originalFileDirectory . '/' . $db_prefix;

View File

@ -339,7 +339,7 @@ class SimpleTestMailCaptureTestCase extends DrupalWebTestCase {
$this->assertEqual(count($captured_emails), 0, t('The captured e-mails queue is empty.'), t('E-mail')); $this->assertEqual(count($captured_emails), 0, t('The captured e-mails queue is empty.'), t('E-mail'));
// Send the e-mail. // Send the e-mail.
$response = drupal_mail_sending_system('simpletest', 'drupal_mail_test')->mail($message); $response = drupal_mail_system('simpletest', 'drupal_mail_test')->mail($message);
// Ensure that there is one e-mail in the captured e-mails array. // Ensure that there is one e-mail in the captured e-mails array.
$captured_emails = $this->drupalGetMails(); $captured_emails = $this->drupalGetMails();
@ -360,7 +360,7 @@ class SimpleTestMailCaptureTestCase extends DrupalWebTestCase {
'to' => $this->randomName(32) . '@example.com', 'to' => $this->randomName(32) . '@example.com',
'body' => $this->randomString(512), 'body' => $this->randomString(512),
); );
drupal_mail_sending_system('drupal_mail_test', $index)->mail($message); drupal_mail_system('drupal_mail_test', $index)->mail($message);
} }
// There should now be 6 e-mails captured. // There should now be 6 e-mails captured.
@ -377,7 +377,7 @@ class SimpleTestMailCaptureTestCase extends DrupalWebTestCase {
// Send the last e-mail again, so we can confirm that the drupalGetMails-filter // Send the last e-mail again, so we can confirm that the drupalGetMails-filter
// correctly returns all e-mails with a given property/value. // correctly returns all e-mails with a given property/value.
drupal_mail_sending_system('drupal_mail_test', $index)->mail($message); drupal_mail_system('drupal_mail_test', $index)->mail($message);
$captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test_4')); $captured_emails = $this->drupalGetMails(array('id' => 'drupal_mail_test_4'));
$this->assertEqual(count($captured_emails), 2, t('All e-mails with the same id are returned when filtering by id.'), t('E-mail')); $this->assertEqual(count($captured_emails), 2, t('All e-mails with the same id are returned when filtering by id.'), t('E-mail'));
} }

View File

@ -25,7 +25,7 @@ class MailTestCase extends DrupalWebTestCase implements MailSystemInterface {
parent::setUp(); parent::setUp();
// Set MailTestCase (i.e. this class) as the SMTP library // Set MailTestCase (i.e. this class) as the SMTP library
variable_set('mail_sending_system', array('default-system' => 'MailTestCase')); variable_set('mail_system', array('default-system' => 'MailTestCase'));
} }
/** /**
@ -41,6 +41,21 @@ class MailTestCase extends DrupalWebTestCase implements MailSystemInterface {
$this->assertEqual(self::$sent_message['to'], 'testing@drupal.org', t('Pluggable mail system is extendable.')); $this->assertEqual(self::$sent_message['to'], 'testing@drupal.org', t('Pluggable mail system is extendable.'));
} }
/**
* Concatenate and wrap the e-mail body for plain-text mails.
*
* @see DefaultMailSystem
*/
public function format(array $message) {
// Join the body array into one string.
$message['body'] = implode("\n\n", $message['body']);
// Convert any HTML to plain-text.
$message['body'] = drupal_html_to_text($message['body']);
// Wrap the mail body for sending.
$message['body'] = drupal_wrap_mail($message['body']);
return $message;
}
/** /**
* Send function that is called through the mail system. * Send function that is called through the mail system.
*/ */

View File

@ -3,21 +3,40 @@
/** /**
* @file * @file
* Drupal core implementations of the DrupalMailSendingInterface. * Drupal core implementations of MailSystemInterface.
*/ */
/** /**
* The default Drupal mail sending library using PHP's mail function. * The default Drupal mail backend using PHP's mail function.
*/ */
class DefaultMailSystem implements MailSystemInterface { class DefaultMailSystem implements MailSystemInterface {
/** /**
* Send an e-mail message, using Drupal variables and default settings. * Concatenate and wrap the e-mail body for plain-text mails.
* @see http://php.net/manual/en/function.mail.php the PHP function reference
* for mail().
* @see drupal_mail() for information on how $message is composed.
* *
* @param $message * @param $message
* Message array as described by DrupalMailSendingInterface. * A message array, as described in hook_mail_alter().
*
* @return
* The formatted $message.
*/
public function format(array $message) {
// Join the body array into one string.
$message['body'] = implode("\n\n", $message['body']);
// Convert any HTML to plain-text.
$message['body'] = drupal_html_to_text($message['body']);
// Wrap the mail body for sending.
$message['body'] = drupal_wrap_mail($message['body']);
return $message;
}
/**
* Send an e-mail message, using Drupal variables and default settings.
*
* @see http://php.net/manual/en/function.mail.php
* @see drupal_mail()
*
* @param $message
* A message array, as described in hook_mail_alter().
* @return * @return
* TRUE if the mail was successfully accepted, otherwise FALSE. * TRUE if the mail was successfully accepted, otherwise FALSE.
*/ */
@ -44,8 +63,7 @@ class DefaultMailSystem implements MailSystemInterface {
* *
* This class is for running tests or for development. * This class is for running tests or for development.
*/ */
class TestingMailSystem implements MailSystemInterface { class TestingMailSystem extends DefaultMailSystem implements MailSystemInterface {
/** /**
* Accept an e-mail message and store it in a variable. * Accept an e-mail message and store it in a variable.
* *

View File

@ -11,6 +11,38 @@
* @{ * @{
*/ */
/**
* Defines one or more hooks that are exposed by a module.
*
* Normally hooks do not need to be explicitly defined. However, by declaring a
* hook explicitly, a module may define a "group" for it. Modules that implement
* a hook may then place their implementation in either $module.module or in
* $module.$group.inc. If the hook is located in $module.$group.inc, then that
* file will be automatically loaded when needed.
* In general, hooks that are rarely invoked and/or are very large should be
* placed in a separate include file, while hooks that are very short or very
* frequently called should be left in the main module file so that they are
* always available.
*
* @return
* An associative array whose keys are hook names and whose values are an
* associative array containing:
* - group: A string defining the group to which the hook belongs. The module
* system will determine whether a file with the name $module.$group.inc
* exists, and automatically load it when required.
*
* See system_hook_info() for all hook groups defined by Drupal core.
*/
function hook_hook_info() {
$hooks['token_info'] = array(
'group' => 'tokens',
);
$hooks['tokens'] = array(
'group' => 'tokens',
);
return $hooks;
}
/** /**
* Inform the base system and the Field API about one or more entity types. * Inform the base system and the Field API about one or more entity types.
* *
@ -700,7 +732,7 @@ function hook_image_toolkits() {
* *
* Email messages sent using functions other than drupal_mail() will not * Email messages sent using functions other than drupal_mail() will not
* invoke hook_mail_alter(). For example, a contributed module directly * invoke hook_mail_alter(). For example, a contributed module directly
* calling the drupal_mail_sending_system()->mail() or PHP mail() function * calling the drupal_mail_system()->mail() or PHP mail() function
* will not invoke this hook. All core modules use drupal_mail() for * will not invoke this hook. All core modules use drupal_mail() for
* messaging, it is best practice but not manditory in contributed modules. * messaging, it is best practice but not manditory in contributed modules.
* *

View File

@ -277,6 +277,19 @@ function system_rdf_namespaces() {
); );
} }
/**
* Implement hook_hook_info().
*/
function system_hook_info() {
$hooks['token_info'] = array(
'group' => 'tokens',
);
$hooks['tokens'] = array(
'group' => 'tokens',
);
return $hooks;
}
/** /**
* Implement hook_entity_info(). * Implement hook_entity_info().
*/ */
@ -2762,7 +2775,7 @@ function system_mail($key, &$message, $params) {
$body = token_replace($context['message'], $context); $body = token_replace($context['message'], $context);
$message['subject'] .= str_replace(array("\r", "\n"), '', $subject); $message['subject'] .= str_replace(array("\r", "\n"), '', $subject);
$message['body'][] = drupal_html_to_text($body); $message['body'][] = $body;
} }
function system_message_action_form($context) { function system_message_action_form($context) {

View File

@ -2910,8 +2910,7 @@ function user_preferred_language($account, $default = NULL) {
* @param $language * @param $language
* Optional language to use for the notification, overriding account language. * Optional language to use for the notification, overriding account language.
* @return * @return
* The return value from drupal_mail_sending_system()->mail(), if ends up * The return value from drupal_mail_system()->mail(), if ends up being called.
* being called.
*/ */
function _user_mail_notify($op, $account, $language = NULL) { function _user_mail_notify($op, $account, $language = NULL) {
// By default, we always notify except for canceled and blocked. // By default, we always notify except for canceled and blocked.