|
@@ -1,20 +1,66 @@
|
|
|
<?php
|
|
|
|
|
|
/**
|
|
|
- * @file Rules engine module
|
|
|
+ * @file
|
|
|
+ * Rules engine module.
|
|
|
*/
|
|
|
|
|
|
+// The class autoloader may fail for classes added in 7.x-2.4 (Issue 2090511).
|
|
|
+if (!drupal_autoload_class('RulesEventHandlerEntityBundle')) {
|
|
|
+ require_once dirname(__FILE__) . '/includes/rules.event.inc';
|
|
|
+}
|
|
|
+
|
|
|
+// Include our hook implementations early, as they can be called even before
|
|
|
+// hook_init().
|
|
|
+require_once dirname(__FILE__) . '/modules/events.inc';
|
|
|
+
|
|
|
+/**
|
|
|
+ * Implements hook_module_implements_alter().
|
|
|
+ */
|
|
|
+function rules_module_implements_alter(&$implementations, $hook) {
|
|
|
+ // Ensures the invocation of hook_menu_get_item_alter() triggers
|
|
|
+ // rules_menu_get_item_alter() first so the rules invocation is ready for all
|
|
|
+ // sub-sequent hook implementations.
|
|
|
+ if ($hook == 'menu_get_item_alter' && array_key_exists('rules', $implementations)) {
|
|
|
+ $group = $implementations['rules'];
|
|
|
+ unset($implementations['rules']);
|
|
|
+ $implementations = array_merge(array('rules' => $group), $implementations);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Implements hook_menu_get_item_alter().
|
|
|
+ */
|
|
|
+function rules_menu_get_item_alter() {
|
|
|
+ // Make sure that event invocation is enabled before menu items are loaded.
|
|
|
+ // But make sure later calls to menu_get_item() won't automatically re-enabled
|
|
|
+ // the rules invocation.
|
|
|
+ // Example: modules that implement hook_entity_ENTITY_TYPE_load() might want
|
|
|
+ // to invoke Rules events in that load hook, which is also invoked for menu
|
|
|
+ // item loading. Since this can happen even before hook_init() we need to make
|
|
|
+ // sure that firing Rules events is enabled at that point. A typical use case
|
|
|
+ // for this is Drupal Commerce with commerce_cart_commerce_order_load().
|
|
|
+ if (!drupal_static('rules_init', FALSE)) {
|
|
|
+ rules_event_invocation_enabled(TRUE);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* Implements hook_init().
|
|
|
*/
|
|
|
function rules_init() {
|
|
|
- module_load_include('inc', 'rules', 'modules/events');
|
|
|
+ // See rules_menu_get_item_alter().
|
|
|
+ $rules_init = &drupal_static(__FUNCTION__, FALSE);
|
|
|
+ $rules_init = TRUE;
|
|
|
+ // Enable event invocation once hook_init() was invoked for Rules.
|
|
|
+ rules_event_invocation_enabled(TRUE);
|
|
|
rules_invoke_event('init');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns an instance of the rules UI controller, which eases re-using the Rules UI.
|
|
|
+ * Returns an instance of the rules UI controller.
|
|
|
*
|
|
|
+ * This function is for convenience, to ease re-using the Rules UI.
|
|
|
* See the rules_admin.module for example usage.
|
|
|
*
|
|
|
* @return RulesUIController
|
|
@@ -32,8 +78,9 @@ function rules_ui() {
|
|
|
*
|
|
|
* @param $name
|
|
|
* The action's name.
|
|
|
- * @param $settings
|
|
|
+ * @param array $settings
|
|
|
* The action's settings array.
|
|
|
+ *
|
|
|
* @return RulesAction
|
|
|
*/
|
|
|
function rules_action($name, $settings = array()) {
|
|
@@ -45,8 +92,9 @@ function rules_action($name, $settings = array()) {
|
|
|
*
|
|
|
* @param $name
|
|
|
* The condition's name.
|
|
|
- * @param $settings
|
|
|
+ * @param array $settings
|
|
|
* The condition's settings array.
|
|
|
+ *
|
|
|
* @return RulesCondition
|
|
|
*/
|
|
|
function rules_condition($name, $settings = array()) {
|
|
@@ -56,9 +104,9 @@ function rules_condition($name, $settings = array()) {
|
|
|
/**
|
|
|
* Creates a new rule.
|
|
|
*
|
|
|
- * @param $variables
|
|
|
+ * @param array $variables
|
|
|
* The array of variables to setup in the evaluation state, making them
|
|
|
- * available for the configuraion elements. Values for the variables need to
|
|
|
+ * available for the configuration elements. Values for the variables need to
|
|
|
* be passed as argument when the rule is executed. Only Rule instances with
|
|
|
* no variables can be embedded in other configurations, e.g. rule sets.
|
|
|
* The array has to be keyed by variable name and contain a sub-array for each
|
|
@@ -71,9 +119,10 @@ function rules_condition($name, $settings = array()) {
|
|
|
* initially, but the "Set data value" action may be used to do so. This is
|
|
|
* in particular useful for defining variables which can be provided to the
|
|
|
* caller (see $provides argument) but need not be passed in as parameter.
|
|
|
- * @param $provides
|
|
|
+ * @param array $provides
|
|
|
* The names of variables which should be provided to the caller. Only
|
|
|
* variables contained in $variables may be specified.
|
|
|
+ *
|
|
|
* @return Rule
|
|
|
*/
|
|
|
function rule($variables = NULL, $provides = array()) {
|
|
@@ -92,8 +141,9 @@ function rules_reaction_rule() {
|
|
|
/**
|
|
|
* Creates a logical OR condition container.
|
|
|
*
|
|
|
- * @param $variables
|
|
|
+ * @param array $variables
|
|
|
* An optional array as for rule().
|
|
|
+ *
|
|
|
* @return RulesOr
|
|
|
*/
|
|
|
function rules_or($variables = NULL) {
|
|
@@ -103,8 +153,9 @@ function rules_or($variables = NULL) {
|
|
|
/**
|
|
|
* Creates a logical AND condition container.
|
|
|
*
|
|
|
- * @param $variables
|
|
|
+ * @param array $variables
|
|
|
* An optional array as for rule().
|
|
|
+ *
|
|
|
* @return RulesAnd
|
|
|
*/
|
|
|
function rules_and($variables = NULL) {
|
|
@@ -114,13 +165,14 @@ function rules_and($variables = NULL) {
|
|
|
/**
|
|
|
* Creates a loop.
|
|
|
*
|
|
|
- * @param $settings
|
|
|
+ * @param array $settings
|
|
|
* The loop settings, containing
|
|
|
* 'list:select': The data selector for the list to loop over.
|
|
|
* 'item:var': Optionally a name for the list item variable.
|
|
|
- * 'item:label': Optionally a lebel for the list item variable.
|
|
|
- * @param $variables
|
|
|
+ * 'item:label': Optionally a label for the list item variable.
|
|
|
+ * @param array $variables
|
|
|
* An optional array as for rule().
|
|
|
+ *
|
|
|
* @return RulesLoop
|
|
|
*/
|
|
|
function rules_loop($settings = array(), $variables = NULL) {
|
|
@@ -130,10 +182,11 @@ function rules_loop($settings = array(), $variables = NULL) {
|
|
|
/**
|
|
|
* Creates a rule set.
|
|
|
*
|
|
|
- * @param $variables
|
|
|
+ * @param array $variables
|
|
|
* An array as for rule().
|
|
|
- * @param $provides
|
|
|
+ * @param array $provides
|
|
|
* The names of variables which should be provided to the caller. See rule().
|
|
|
+ *
|
|
|
* @return RulesRuleSet
|
|
|
*/
|
|
|
function rules_rule_set($variables = array(), $provides = array()) {
|
|
@@ -143,10 +196,11 @@ function rules_rule_set($variables = array(), $provides = array()) {
|
|
|
/**
|
|
|
* Creates an action set.
|
|
|
*
|
|
|
- * @param $variables
|
|
|
+ * @param array $variables
|
|
|
* An array as for rule().
|
|
|
- * @param $provides
|
|
|
+ * @param array $provides
|
|
|
* The names of variables which should be provided to the caller. See rule().
|
|
|
+ *
|
|
|
* @return RulesActionSet
|
|
|
*/
|
|
|
function rules_action_set($variables = array(), $provides = array()) {
|
|
@@ -158,15 +212,15 @@ function rules_action_set($variables = array(), $provides = array()) {
|
|
|
*
|
|
|
* @param $msg
|
|
|
* The message to log.
|
|
|
- * @param $args
|
|
|
+ * @param array $args
|
|
|
* An array of placeholder arguments as used by t().
|
|
|
* @param $priority
|
|
|
* A priority as defined by the RulesLog class.
|
|
|
* @param RulesPlugin $element
|
|
|
- * (optional) The RulesElement causing the log entry.
|
|
|
- * @param boolean $scope
|
|
|
- * (optional) This may be used to denote the beginning (TRUE) or the end
|
|
|
- * (FALSE) of a new execution scope.
|
|
|
+ * (optional) The RulesElement causing the log entry.
|
|
|
+ * @param bool $scope
|
|
|
+ * (optional) This may be used to denote the beginning (TRUE) or the end
|
|
|
+ * (FALSE) of a new execution scope.
|
|
|
*/
|
|
|
function rules_log($msg, $args = array(), $priority = RulesLog::INFO, RulesPlugin $element = NULL, $scope = NULL) {
|
|
|
static $logger, $settings;
|
|
@@ -183,7 +237,11 @@ function rules_log($msg, $args = array(), $priority = RulesLog::INFO, RulesPlugi
|
|
|
if (isset($element) && isset($element->root()->name)) {
|
|
|
$link = l(t('edit configuration'), RulesPluginUI::path($element->root()->name, 'edit', $element));
|
|
|
}
|
|
|
+ // Disabled rules invocation to avoid an endless loop when using
|
|
|
+ // watchdog - which would trigger a rules event.
|
|
|
+ rules_event_invocation_enabled(FALSE);
|
|
|
watchdog('rules', $msg, $args, $priority == RulesLog::WARN ? WATCHDOG_WARNING : WATCHDOG_ERROR, $link);
|
|
|
+ rules_event_invocation_enabled(TRUE);
|
|
|
}
|
|
|
// Do nothing in case debugging is totally disabled.
|
|
|
if (!$settings['rules_debug_log'] && !$settings['rules_debug']) {
|
|
@@ -199,15 +257,21 @@ function rules_log($msg, $args = array(), $priority = RulesLog::INFO, RulesPlugi
|
|
|
/**
|
|
|
* Fetches module definitions for the given hook name.
|
|
|
*
|
|
|
- * Used for collecting events, rules, actions and condtions from other modules.
|
|
|
+ * Used for collecting events, rules, actions and condition from other modules.
|
|
|
*
|
|
|
* @param $hook
|
|
|
* The hook of the definitions to get from invoking hook_rules_{$hook}.
|
|
|
*/
|
|
|
function rules_fetch_data($hook) {
|
|
|
$data = &drupal_static(__FUNCTION__, array());
|
|
|
+ static $discover = array(
|
|
|
+ 'action_info' => 'RulesActionHandlerInterface',
|
|
|
+ 'condition_info' => 'RulesConditionHandlerInterface',
|
|
|
+ 'event_info' => 'RulesEventHandlerInterface',
|
|
|
+ );
|
|
|
|
|
|
if (!isset($data[$hook])) {
|
|
|
+ $data[$hook] = array();
|
|
|
foreach (module_implements('rules_' . $hook) as $module) {
|
|
|
$result = call_user_func($module . '_rules_' . $hook);
|
|
|
if (isset($result) && is_array($result)) {
|
|
@@ -217,11 +281,90 @@ function rules_fetch_data($hook) {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- drupal_alter('rules_'. $hook, $data[$hook]);
|
|
|
+ // Support class discovery.
|
|
|
+ if (isset($discover[$hook])) {
|
|
|
+ $data[$hook] += rules_discover_plugins($discover[$hook]);
|
|
|
+ }
|
|
|
+ drupal_alter('rules_' . $hook, $data[$hook]);
|
|
|
}
|
|
|
return $data[$hook];
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Discover plugin implementations.
|
|
|
+ *
|
|
|
+ * Class based plugin handlers must be loaded when rules caches are rebuilt,
|
|
|
+ * such that they get discovered properly. You have the following options:
|
|
|
+ * - Put it into a regular module file (discouraged)
|
|
|
+ * - Put it into your module.rules.inc file
|
|
|
+ * - Put it in any file and declare it using hook_rules_file_info()
|
|
|
+ * - Put it in any file and declare it using hook_rules_directory()
|
|
|
+ *
|
|
|
+ * In addition to that, the class must be loadable via regular class
|
|
|
+ * auto-loading, thus put the file holding the class in your info file or use
|
|
|
+ * another class-loader.
|
|
|
+ *
|
|
|
+ * @param string $class
|
|
|
+ * The class or interface the plugins must implement. For a plugin to be
|
|
|
+ * discovered it must have a static getInfo() method also.
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ * An info-hook style array containing info about discovered plugins.
|
|
|
+ *
|
|
|
+ * @see RulesActionHandlerInterface
|
|
|
+ * @see RulesConditionHandlerInterface
|
|
|
+ * @see RulesEventHandlerInterface
|
|
|
+ */
|
|
|
+function rules_discover_plugins($class) {
|
|
|
+ // Make sure all files possibly holding plugins are included.
|
|
|
+ RulesAbstractPlugin::includeFiles();
|
|
|
+
|
|
|
+ $items = array();
|
|
|
+ foreach (get_declared_classes() as $plugin_class) {
|
|
|
+ if (is_subclass_of($plugin_class, $class) && method_exists($plugin_class, 'getInfo')) {
|
|
|
+ $info = call_user_func(array($plugin_class, 'getInfo'));
|
|
|
+ $info['class'] = $plugin_class;
|
|
|
+ $info['module'] = _rules_discover_module($plugin_class);
|
|
|
+ $items[$info['name']] = $info;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $items;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Determines the module providing the given class.
|
|
|
+ *
|
|
|
+ * @param string $class
|
|
|
+ * The name of the class or interface plugins to discover.
|
|
|
+ *
|
|
|
+ * @return string|false
|
|
|
+ * The path of the class, relative to the Drupal installation root,
|
|
|
+ * or FALSE if not discovered.
|
|
|
+ */
|
|
|
+function _rules_discover_module($class) {
|
|
|
+ $paths = &drupal_static(__FUNCTION__);
|
|
|
+
|
|
|
+ if (!isset($paths)) {
|
|
|
+ // Build up a map of modules keyed by their directory.
|
|
|
+ foreach (system_list('module_enabled') as $name => $module_info) {
|
|
|
+ $paths[dirname($module_info->filename)] = $name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Retrieve the class file and convert its absolute path to a regular Drupal
|
|
|
+ // path relative to the installation root.
|
|
|
+ $reflection = new ReflectionClass($class);
|
|
|
+ $path = str_replace(realpath(DRUPAL_ROOT) . DIRECTORY_SEPARATOR, '', realpath(dirname($reflection->getFileName())));
|
|
|
+ $path = DIRECTORY_SEPARATOR != '/' ? str_replace(DIRECTORY_SEPARATOR, '/', $path) : $path;
|
|
|
+
|
|
|
+ // Go up the path until we match a module.
|
|
|
+ $parts = explode('/', $path);
|
|
|
+ while (!isset($paths[$path]) && array_pop($parts)) {
|
|
|
+ $path = dirname($path);
|
|
|
+ }
|
|
|
+ return isset($paths[$path]) ? $paths[$path] : FALSE;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* Gets a rules cache entry.
|
|
|
*/
|
|
@@ -241,24 +384,39 @@ function &rules_get_cache($cid = 'data') {
|
|
|
if ($get = cache_get($cid . $cid_suffix, 'cache_rules')) {
|
|
|
$cache[$cid] = $get->data;
|
|
|
}
|
|
|
- elseif ($cid === 'data') {
|
|
|
- // There is no 'data' cache so we need to rebuild it. Make sure subsequent
|
|
|
- // cache gets of the main 'data' cache during rebuild get the interim
|
|
|
- // cache by passing in the reference of the static cache variable.
|
|
|
- _rules_rebuild_cache($cache['data']);
|
|
|
- }
|
|
|
- elseif (strpos($cid, 'comp_') === 0) {
|
|
|
- $cache[$cid] = FALSE;
|
|
|
- _rules_rebuild_component_cache();
|
|
|
- return $cache[$cid];
|
|
|
- }
|
|
|
- elseif (strpos($cid, 'event_') === 0) {
|
|
|
- $cache[$cid] = FALSE;
|
|
|
- RulesEventSet::rebuildEventCache();
|
|
|
- return $cache[$cid];
|
|
|
- }
|
|
|
else {
|
|
|
- $cache[$cid] = FALSE;
|
|
|
+ // Prevent stampeding by ensuring the cache is rebuilt just once at the
|
|
|
+ // same time.
|
|
|
+ while (!lock_acquire(__FUNCTION__ . $cid . $cid_suffix, 60)) {
|
|
|
+ // Now wait until the lock is released.
|
|
|
+ lock_wait(__FUNCTION__ . $cid . $cid_suffix, 30);
|
|
|
+ // If the lock is released it's likely the cache was rebuild. Thus check
|
|
|
+ // again if we can fetch it from the persistent cache.
|
|
|
+ if ($get = cache_get($cid . $cid_suffix, 'cache_rules')) {
|
|
|
+ $cache[$cid] = $get->data;
|
|
|
+ return $cache[$cid];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($cid === 'data') {
|
|
|
+ // There is no 'data' cache so we need to rebuild it. Make sure
|
|
|
+ // subsequent cache gets of the main 'data' cache during rebuild get
|
|
|
+ // the interim cache by passing in the reference of the static cache
|
|
|
+ // variable.
|
|
|
+ _rules_rebuild_cache($cache['data']);
|
|
|
+ }
|
|
|
+ elseif (strpos($cid, 'comp_') === 0) {
|
|
|
+ $cache[$cid] = FALSE;
|
|
|
+ _rules_rebuild_component_cache();
|
|
|
+ }
|
|
|
+ elseif (strpos($cid, 'event_') === 0 || $cid == 'rules_event_whitelist') {
|
|
|
+ $cache[$cid] = FALSE;
|
|
|
+ RulesEventSet::rebuildEventCache();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $cache[$cid] = FALSE;
|
|
|
+ }
|
|
|
+ // Ensure a set lock is released.
|
|
|
+ lock_release(__FUNCTION__ . $cid . $cid_suffix);
|
|
|
}
|
|
|
}
|
|
|
return $cache[$cid];
|
|
@@ -279,7 +437,7 @@ function &rules_get_cache($cid = 'data') {
|
|
|
* @see entity_defaults_rebuild()
|
|
|
*/
|
|
|
function _rules_rebuild_cache(&$cache) {
|
|
|
- foreach(array('data_info', 'plugin_info') as $hook) {
|
|
|
+ foreach (array('data_info', 'plugin_info') as $hook) {
|
|
|
$cache[$hook] = rules_fetch_data($hook);
|
|
|
}
|
|
|
foreach ($cache['plugin_info'] as $name => &$info) {
|
|
@@ -322,7 +480,7 @@ function _rules_rebuild_component_cache() {
|
|
|
*
|
|
|
* In addition to calling cache_set(), this function makes sure the cache item
|
|
|
* is immediately available via rules_get_cache() by keeping all cache items
|
|
|
- * in memory. That way we can garantuee rules_get_cache() is able to retrieve
|
|
|
+ * in memory. That way we can guarantee rules_get_cache() is able to retrieve
|
|
|
* any cache item, even if all cache gets fail.
|
|
|
*
|
|
|
* @see rules_get_cache()
|
|
@@ -337,16 +495,14 @@ function rules_set_cache($cid, $data) {
|
|
|
* Implements hook_flush_caches().
|
|
|
*/
|
|
|
function rules_flush_caches() {
|
|
|
- variable_del('rules_empty_sets');
|
|
|
return array('cache_rules');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Clears the rule set cache
|
|
|
+ * Clears the rule set cache.
|
|
|
*/
|
|
|
function rules_clear_cache() {
|
|
|
cache_clear_all('*', 'cache_rules', TRUE);
|
|
|
- variable_del('rules_empty_sets');
|
|
|
drupal_static_reset('rules_get_cache');
|
|
|
drupal_static_reset('rules_fetch_data');
|
|
|
drupal_static_reset('rules_config_update_dirty_flag');
|
|
@@ -356,16 +512,17 @@ function rules_clear_cache() {
|
|
|
/**
|
|
|
* Imports the given export and returns the imported configuration.
|
|
|
*
|
|
|
- * @param $export
|
|
|
+ * @param string $export
|
|
|
* A serialized string in JSON format as produced by the RulesPlugin::export()
|
|
|
* method, or the PHP export as usual PHP array.
|
|
|
+ * @param string $error_msg
|
|
|
+ *
|
|
|
* @return RulesPlugin
|
|
|
*/
|
|
|
function rules_import($export, &$error_msg = '') {
|
|
|
return entity_get_controller('rules_config')->import($export, $error_msg);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* Wraps the given data.
|
|
|
*
|
|
@@ -373,9 +530,10 @@ function rules_import($export, &$error_msg = '') {
|
|
|
* If available, the actual data, else NULL.
|
|
|
* @param $info
|
|
|
* An array of info about this data.
|
|
|
- * @param $force
|
|
|
+ * @param bool $force
|
|
|
* Usually data is only wrapped if really needed. If set to TRUE, wrapping the
|
|
|
* data is forced, so primitive data types are also wrapped.
|
|
|
+ *
|
|
|
* @return EntityMetadataWrapper
|
|
|
* An EntityMetadataWrapper or the unwrapped data.
|
|
|
*
|
|
@@ -416,11 +574,12 @@ function &rules_wrap_data($data = NULL, $info, $force = FALSE) {
|
|
|
/**
|
|
|
* Unwraps the given data, if it's wrapped.
|
|
|
*
|
|
|
- * @param $data
|
|
|
+ * @param array $data
|
|
|
* An array of wrapped data.
|
|
|
- * @param $info
|
|
|
+ * @param array $info
|
|
|
* Optionally an array of info about how to unwrap the data. Keyed as $data.
|
|
|
- * @return
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
* An array containing unwrapped or passed through data.
|
|
|
*/
|
|
|
function rules_unwrap_data(array $data, $info = array()) {
|
|
@@ -460,6 +619,70 @@ function rules_unwrap_data(array $data, $info = array()) {
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Gets event info for a given event.
|
|
|
+ *
|
|
|
+ * @param string $event_name
|
|
|
+ * A (configured) event name.
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ * An array of event info. If the event is unknown, a suiting info array is
|
|
|
+ * generated and returned
|
|
|
+ */
|
|
|
+function rules_get_event_info($event_name) {
|
|
|
+ $base_event_name = rules_get_event_base_name($event_name);
|
|
|
+ $events = rules_fetch_data('event_info');
|
|
|
+ if (isset($events[$base_event_name])) {
|
|
|
+ return $events[$base_event_name] + array('name' => $base_event_name);
|
|
|
+ }
|
|
|
+ return array(
|
|
|
+ 'label' => t('Unknown event "!event_name"', array('!event_name' => $base_event_name)),
|
|
|
+ 'name' => $base_event_name,
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Returns the base name of a configured event name.
|
|
|
+ *
|
|
|
+ * For a configured event name like node_view--article the base event name
|
|
|
+ * node_view is returned.
|
|
|
+ *
|
|
|
+ * @param string $event_name
|
|
|
+ * A (configured) event name.
|
|
|
+ *
|
|
|
+ * @return string
|
|
|
+ * The event base name.
|
|
|
+ */
|
|
|
+function rules_get_event_base_name($event_name) {
|
|
|
+ // Cut off any suffix from a configured event name.
|
|
|
+ if (strpos($event_name, '--') !== FALSE) {
|
|
|
+ $parts = explode('--', $event_name, 2);
|
|
|
+ return $parts[0];
|
|
|
+ }
|
|
|
+ return $event_name;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Returns the rule event handler for the given event.
|
|
|
+ *
|
|
|
+ * Events having no settings are handled via the class RulesEventSettingsNone.
|
|
|
+ *
|
|
|
+ * @param string $event_name
|
|
|
+ * The event name (base or configured).
|
|
|
+ * @param array $settings
|
|
|
+ * (optional) An array of event settings to set on the handler.
|
|
|
+ *
|
|
|
+ * @return RulesEventHandlerInterface
|
|
|
+ * The event handler.
|
|
|
+ */
|
|
|
+function rules_get_event_handler($event_name, array $settings = NULL) {
|
|
|
+ $event_name = rules_get_event_base_name($event_name);
|
|
|
+ $event_info = rules_get_event_info($event_name);
|
|
|
+ $class = !empty($event_info['class']) ? $event_info['class'] : 'RulesEventDefaultHandler';
|
|
|
+ $handler = new $class($event_name, $event_info);
|
|
|
+ return isset($settings) ? $handler->setSettings($settings) : $handler;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* Creates a new instance of a the given rules plugin.
|
|
|
*
|
|
@@ -473,7 +696,7 @@ function rules_plugin_factory($plugin_name, $arg1 = NULL, $arg2 = NULL) {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Implementation of hook_rules_plugin_info().
|
|
|
+ * Implements hook_rules_plugin_info().
|
|
|
*
|
|
|
* Note that the cache is rebuilt in the order of the plugins. Therefore the
|
|
|
* condition and action plugins must be at the top, so that any components
|
|
@@ -485,11 +708,11 @@ function rules_rules_plugin_info() {
|
|
|
'condition' => array(
|
|
|
'class' => 'RulesCondition',
|
|
|
'embeddable' => 'RulesConditionContainer',
|
|
|
- 'extenders' => array (
|
|
|
+ 'extenders' => array(
|
|
|
'RulesPluginImplInterface' => array(
|
|
|
'class' => 'RulesAbstractPluginDefaults',
|
|
|
),
|
|
|
- 'RulesPluginFeaturesIntegrationInterace' => array(
|
|
|
+ 'RulesPluginFeaturesIntegrationInterface' => array(
|
|
|
'methods' => array(
|
|
|
'features_export' => 'rules_features_abstract_default_features_export',
|
|
|
),
|
|
@@ -502,11 +725,11 @@ function rules_rules_plugin_info() {
|
|
|
'action' => array(
|
|
|
'class' => 'RulesAction',
|
|
|
'embeddable' => 'RulesActionContainer',
|
|
|
- 'extenders' => array (
|
|
|
+ 'extenders' => array(
|
|
|
'RulesPluginImplInterface' => array(
|
|
|
'class' => 'RulesAbstractPluginDefaults',
|
|
|
),
|
|
|
- 'RulesPluginFeaturesIntegrationInterace' => array(
|
|
|
+ 'RulesPluginFeaturesIntegrationInterface' => array(
|
|
|
'methods' => array(
|
|
|
'features_export' => 'rules_features_abstract_default_features_export',
|
|
|
),
|
|
@@ -598,7 +821,7 @@ function rules_rules_plugin_info() {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Implementation of hook_entity_info().
|
|
|
+ * Implements hook_entity_info().
|
|
|
*/
|
|
|
function rules_entity_info() {
|
|
|
return array(
|
|
@@ -627,10 +850,10 @@ function rules_entity_info() {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Implementation of hook_hook_info().
|
|
|
+ * Implements hook_hook_info().
|
|
|
*/
|
|
|
function rules_hook_info() {
|
|
|
- foreach(array('plugin_info', 'data_info', 'condition_info', 'action_info', 'event_info', 'file_info', 'evaluator_info', 'data_processor_info') as $hook) {
|
|
|
+ foreach (array('plugin_info', 'rules_directory', 'data_info', 'condition_info', 'action_info', 'event_info', 'file_info', 'evaluator_info', 'data_processor_info') as $hook) {
|
|
|
$hooks['rules_' . $hook] = array(
|
|
|
'group' => 'rules',
|
|
|
);
|
|
@@ -657,12 +880,12 @@ function rules_hook_info() {
|
|
|
* @see hook_entity_info()
|
|
|
* @see RulesEntityController
|
|
|
*
|
|
|
- * @param $names
|
|
|
+ * @param array|false $names
|
|
|
* An array of rules configuration names or FALSE to load all.
|
|
|
- * @param $conditions
|
|
|
+ * @param array $conditions
|
|
|
* An array of conditions in the form 'field' => $value.
|
|
|
*
|
|
|
- * @return
|
|
|
+ * @return array
|
|
|
* An array of rule configurations indexed by their ids.
|
|
|
*/
|
|
|
function rules_config_load_multiple($names = array(), $conditions = array()) {
|
|
@@ -690,10 +913,10 @@ function rules_config_load($name) {
|
|
|
* Whether to return only the label or the whole component object.
|
|
|
* @param $type
|
|
|
* Optionally filter for 'action' or 'condition' components.
|
|
|
- * @param $conditions
|
|
|
+ * @param array $conditions
|
|
|
* An array of additional conditions as required by rules_config_load().
|
|
|
*
|
|
|
- * @return
|
|
|
+ * @return array
|
|
|
* An array keyed by component name containing either the label or the config.
|
|
|
*/
|
|
|
function rules_get_components($label = FALSE, $type = NULL, $conditions = array()) {
|
|
@@ -716,7 +939,7 @@ function rules_get_components($label = FALSE, $type = NULL, $conditions = array(
|
|
|
/**
|
|
|
* Delete rule configurations from database.
|
|
|
*
|
|
|
- * @param $ids
|
|
|
+ * @param array $ids
|
|
|
* An array of entity IDs.
|
|
|
*/
|
|
|
function rules_config_delete(array $ids) {
|
|
@@ -726,13 +949,13 @@ function rules_config_delete(array $ids) {
|
|
|
/**
|
|
|
* Ensures the configuration's 'dirty' flag is up to date by running an integrity check.
|
|
|
*
|
|
|
- * @param $update
|
|
|
+ * @param bool $update
|
|
|
* (optional) Whether the dirty flag is also updated in the database if
|
|
|
* necessary. Defaults to TRUE.
|
|
|
*/
|
|
|
function rules_config_update_dirty_flag($rules_config, $update = TRUE) {
|
|
|
// Keep a log of already check configurations to avoid repetitive checks on
|
|
|
- // oftent used components.
|
|
|
+ // often used components.
|
|
|
// @see rules_element_invoke_component_validate()
|
|
|
$checked = &drupal_static(__FUNCTION__, array());
|
|
|
if (!empty($checked[$rules_config->name])) {
|
|
@@ -779,7 +1002,7 @@ function rules_config_update_dirty_flag($rules_config, $update = TRUE) {
|
|
|
* @param ...
|
|
|
* Arguments to pass to the hook / event.
|
|
|
*
|
|
|
- * @return
|
|
|
+ * @return array
|
|
|
* An array of return values of the hook implementations. If modules return
|
|
|
* arrays from their implementations, those are merged into one array.
|
|
|
*/
|
|
@@ -822,15 +1045,16 @@ function rules_invoke_all() {
|
|
|
* @see rules_invoke_event_by_args()
|
|
|
*/
|
|
|
function rules_invoke_event() {
|
|
|
- global $conf;
|
|
|
-
|
|
|
$args = func_get_args();
|
|
|
$event_name = $args[0];
|
|
|
unset($args[0]);
|
|
|
- // For invoking the rules event we directly acccess the global $conf. This is
|
|
|
- // fast without having to introduce another static cache.
|
|
|
- if (!defined('MAINTENANCE_MODE') && !isset($conf['rules_empty_sets'][$event_name]) && $event = rules_get_cache('event_' . $event_name)) {
|
|
|
- $event->executeByArgs($args);
|
|
|
+ // We maintain a whitelist of configured events to reduces the number of cache
|
|
|
+ // reads. If the whitelist is not in the cache we proceed and it is rebuilt.
|
|
|
+ if (rules_event_invocation_enabled()) {
|
|
|
+ $whitelist = rules_get_cache('rules_event_whitelist');
|
|
|
+ if ((($whitelist === FALSE) || isset($whitelist[$event_name])) && $event = rules_get_cache('event_' . $event_name)) {
|
|
|
+ $event->executeByArgs($args);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -839,7 +1063,7 @@ function rules_invoke_event() {
|
|
|
*
|
|
|
* @param $event_name
|
|
|
* The event's name.
|
|
|
- * @param $args
|
|
|
+ * @param array $args
|
|
|
* An array of parameters for the variables provided by the event, as defined
|
|
|
* in hook_rules_event_info(). Either pass an array keyed by the variable
|
|
|
* names or a numerically indexed array, in which case the ordering of the
|
|
@@ -852,12 +1076,13 @@ function rules_invoke_event() {
|
|
|
* @see rules_invoke_event()
|
|
|
*/
|
|
|
function rules_invoke_event_by_args($event_name, $args = array()) {
|
|
|
- global $conf;
|
|
|
-
|
|
|
- // For invoking the rules event we directly acccess the global $conf. This is
|
|
|
- // fast without having to introduce another static cache.
|
|
|
- if (!defined('MAINTENANCE_MODE') && !isset($conf['rules_empty_sets'][$event_name]) && $event = rules_get_cache('event_' . $event_name)) {
|
|
|
- $event->executeByArgs($args);
|
|
|
+ // We maintain a whitelist of configured events to reduces the number of cache
|
|
|
+ // reads. If the whitelist is empty we proceed and it is rebuilt.
|
|
|
+ if (rules_event_invocation_enabled()) {
|
|
|
+ $whitelist = rules_get_cache('rules_event_whitelist');
|
|
|
+ if ((empty($whitelist) || isset($whitelist[$event_name])) && $event = rules_get_cache('event_' . $event_name)) {
|
|
|
+ $event->executeByArgs($args);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -869,7 +1094,7 @@ function rules_invoke_event_by_args($event_name, $args = array()) {
|
|
|
* @param $args
|
|
|
* Pass further parameters as required for the invoked component.
|
|
|
*
|
|
|
- * @return
|
|
|
+ * @return array
|
|
|
* An array of variables as provided by the component, or FALSE in case the
|
|
|
* component could not be executed.
|
|
|
*/
|
|
@@ -883,10 +1108,12 @@ function rules_invoke_component() {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Filters the given array of arrays by keeping only entries which have $key set
|
|
|
- * to the value of $value.
|
|
|
+ * Filters the given array of arrays.
|
|
|
+ *
|
|
|
+ * This filter operates by keeping only entries which have $key set to the
|
|
|
+ * value of $value.
|
|
|
*
|
|
|
- * @param $array
|
|
|
+ * @param array $array
|
|
|
* The array of arrays to filter.
|
|
|
* @param $key
|
|
|
* The key used for the comparison.
|
|
@@ -908,10 +1135,11 @@ function rules_filter_array($array, $key, $value) {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Merges the $update array into $array making sure no values of $array not
|
|
|
- * appearing in $update are lost.
|
|
|
+ * Merges the $update array into $array.
|
|
|
+ *
|
|
|
+ * Makes sure no values of $array not appearing in $update are lost.
|
|
|
*
|
|
|
- * @return
|
|
|
+ * @return array
|
|
|
* The updated array.
|
|
|
*/
|
|
|
function rules_update_array(array $array, array $update) {
|
|
@@ -929,12 +1157,13 @@ function rules_update_array(array $array, array $update) {
|
|
|
/**
|
|
|
* Extracts the property with the given name.
|
|
|
*
|
|
|
- * @param $arrays
|
|
|
+ * @param array $arrays
|
|
|
* An array of arrays from which a property is to be extracted.
|
|
|
* @param $key
|
|
|
* The name of the property to extract.
|
|
|
*
|
|
|
- * @return An array of extracted properties, keyed as in $arrays-
|
|
|
+ * @return array
|
|
|
+ * An array of extracted properties, keyed as in $arrays.
|
|
|
*/
|
|
|
function rules_extract_property($arrays, $key) {
|
|
|
$data = array();
|
|
@@ -959,9 +1188,9 @@ function rules_array_key($array) {
|
|
|
*
|
|
|
* @param $replacements
|
|
|
* An array of token replacements that need to be "cleaned" for use in the URL.
|
|
|
- * @param $data
|
|
|
+ * @param array $data
|
|
|
* An array of objects used to generate the replacements.
|
|
|
- * @param $options
|
|
|
+ * @param array $options
|
|
|
* An array of options used to generate the replacements.
|
|
|
*
|
|
|
* @see rules_path_action_info()
|
|
@@ -1068,6 +1297,7 @@ function rules_permissions_by_component(array $components = array()) {
|
|
|
|
|
|
/**
|
|
|
* Menu callback for loading rules configuration elements.
|
|
|
+ *
|
|
|
* @see RulesUIController::config_menu()
|
|
|
*/
|
|
|
function rules_element_load($element_id, $config_name) {
|
|
@@ -1077,6 +1307,7 @@ function rules_element_load($element_id, $config_name) {
|
|
|
|
|
|
/**
|
|
|
* Menu callback for getting the title as configured.
|
|
|
+ *
|
|
|
* @see RulesUIController::config_menu()
|
|
|
*/
|
|
|
function rules_get_title($text, $element) {
|
|
@@ -1095,6 +1326,7 @@ function rules_get_title($text, $element) {
|
|
|
* Menu callback for getting the title for the add element page.
|
|
|
*
|
|
|
* Uses a work-a-round for accessing the plugin name.
|
|
|
+ *
|
|
|
* @see RulesUIController::config_menu()
|
|
|
*/
|
|
|
function rules_menu_add_element_title($array) {
|
|
@@ -1187,17 +1419,24 @@ function rules_drupal_goto_alter(&$path, &$options, &$http_response_code) {
|
|
|
* Returns whether the debug log should be shown.
|
|
|
*/
|
|
|
function rules_show_debug_output() {
|
|
|
- if (variable_get('rules_debug', FALSE) == RulesLog::INFO && user_access('access rules debug')) {
|
|
|
+ // For performance avoid unnecessary auto-loading of the RulesLog class.
|
|
|
+ if (!class_exists('RulesLog', FALSE)) {
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+ if (variable_get('rules_debug', 0) == RulesLog::INFO && user_access('access rules debug')) {
|
|
|
return TRUE;
|
|
|
}
|
|
|
- // For performance avoid unnecessary auto-loading of the RulesLog class.
|
|
|
- return variable_get('rules_debug', FALSE) == RulesLog::WARN && user_access('access rules debug') && class_exists('RulesLog', FALSE) && RulesLog::logger()->hasErrors();
|
|
|
+ return variable_get('rules_debug', 0) == RulesLog::WARN && user_access('access rules debug') && RulesLog::logger()->hasErrors();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Implements hook_exit().
|
|
|
*/
|
|
|
function rules_exit() {
|
|
|
+ // Bail out if this is cached request and modules are not loaded.
|
|
|
+ if (!module_exists('rules') || !module_exists('user')) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
if (rules_show_debug_output()) {
|
|
|
if ($log = RulesLog::logger()->render()) {
|
|
|
// Keep the log in the session so we can show it on the next page.
|
|
@@ -1294,11 +1533,13 @@ function rules_modules_disabled($modules) {
|
|
|
->fields('r')
|
|
|
->condition('id', $ids, 'IN')
|
|
|
->condition('active', 1)
|
|
|
- ->execute()->rowCount();
|
|
|
+ ->countQuery()
|
|
|
+ ->execute()
|
|
|
+ ->fetchField();
|
|
|
if ($count > 0) {
|
|
|
$message = format_plural($count,
|
|
|
'1 Rules configuration requires some of the disabled modules to function and cannot be executed any more.',
|
|
|
- '@count Rules configuration require some of the disabled modules to function and cannot be executed any more.'
|
|
|
+ '@count Rules configurations require some of the disabled modules to function and cannot be executed any more.'
|
|
|
);
|
|
|
drupal_set_message($message, 'warning');
|
|
|
}
|
|
@@ -1315,10 +1556,32 @@ function rules_config_access($op, $rules_config = NULL, $account = NULL) {
|
|
|
if (user_access('bypass rules access', $account)) {
|
|
|
return TRUE;
|
|
|
}
|
|
|
- if (!isset($rules_config) || (isset($account) && $account->uid != $GLOBALS['user']->uid)) {
|
|
|
+ // Allow modules to grant / deny access.
|
|
|
+ $access = module_invoke_all('rules_config_access', $op, $rules_config, $account);
|
|
|
+
|
|
|
+ // Only grant access if at least one module granted access and no one denied
|
|
|
+ // access.
|
|
|
+ if (in_array(FALSE, $access, TRUE)) {
|
|
|
return FALSE;
|
|
|
}
|
|
|
- return user_access('administer rules', $account) && ($op == 'view' || $rules_config->access());
|
|
|
+ elseif (in_array(TRUE, $access, TRUE)) {
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
+ return FALSE;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Implements hook_rules_config_access().
|
|
|
+ */
|
|
|
+function rules_rules_config_access($op, $rules_config = NULL, $account = NULL) {
|
|
|
+ // Instead of returning FALSE return nothing, so others still can grant
|
|
|
+ // access.
|
|
|
+ if (!isset($rules_config) || (isset($account) && $account->uid != $GLOBALS['user']->uid)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (user_access('administer rules', $account) && ($op == 'view' || $rules_config->access())) {
|
|
|
+ return TRUE;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -1357,25 +1620,25 @@ function rules_menu() {
|
|
|
/**
|
|
|
* Helper function to keep track of external documentation pages for Rules.
|
|
|
*
|
|
|
- * @param $topic
|
|
|
+ * @param string $topic
|
|
|
* The topic key for used for identifying help pages.
|
|
|
*
|
|
|
- * @return
|
|
|
+ * @return string|array|false
|
|
|
* Either a URL for the given page, or the full list of external help pages.
|
|
|
*/
|
|
|
function rules_external_help($topic = NULL) {
|
|
|
$help = array(
|
|
|
- 'rules' => 'http://drupal.org/node/298480',
|
|
|
- 'terminology' => 'http://drupal.org/node/1299990',
|
|
|
- 'condition-components' => 'http://drupal.org/node/1300034',
|
|
|
- 'data-selection' => 'http://drupal.org/node/1300042',
|
|
|
- 'chained-tokens' => 'http://drupal.org/node/1300042',
|
|
|
- 'loops' => 'http://drupal.org/node/1300058',
|
|
|
- 'components' => 'http://drupal.org/node/1300024',
|
|
|
- 'component-types' => 'http://drupal.org/node/1300024',
|
|
|
- 'variables' => 'http://drupal.org/node/1300024',
|
|
|
- 'scheduler' => 'http://drupal.org/node/1300068',
|
|
|
- 'coding' => 'http://drupal.org/node/878720',
|
|
|
+ 'rules' => 'https://www.drupal.org/node/298480',
|
|
|
+ 'terminology' => 'https://www.drupal.org/node/1299990',
|
|
|
+ 'condition-components' => 'https://www.drupal.org/node/1300034',
|
|
|
+ 'data-selection' => 'https://www.drupal.org/node/1300042',
|
|
|
+ 'chained-tokens' => 'https://www.drupal.org/node/1300042',
|
|
|
+ 'loops' => 'https://www.drupal.org/node/1300058',
|
|
|
+ 'components' => 'https://www.drupal.org/node/1300024',
|
|
|
+ 'component-types' => 'https://www.drupal.org/node/1300024',
|
|
|
+ 'variables' => 'https://www.drupal.org/node/1300024',
|
|
|
+ 'scheduler' => 'https://www.drupal.org/node/1300068',
|
|
|
+ 'coding' => 'https://www.drupal.org/node/878720',
|
|
|
);
|
|
|
|
|
|
if (isset($topic)) {
|
|
@@ -1448,3 +1711,46 @@ function rules_tokens($type, $tokens, $data, $options = array()) {
|
|
|
return entity_token_tokens('struct', $tokens, array('struct' => $wrapper), $options);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Helper function that retrieves a metadata wrapper with all properties.
|
|
|
+ *
|
|
|
+ * Note that without this helper, bundle-specific properties aren't added.
|
|
|
+ */
|
|
|
+function rules_get_entity_metadata_wrapper_all_properties(RulesAbstractPlugin $element) {
|
|
|
+ return entity_metadata_wrapper($element->settings['type'], NULL, array(
|
|
|
+ 'property info alter' => 'rules_entity_metadata_wrapper_all_properties_callback',
|
|
|
+ ));
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Callback that returns a metadata wrapper with all properties.
|
|
|
+ */
|
|
|
+function rules_entity_metadata_wrapper_all_properties_callback(EntityMetadataWrapper $wrapper, $property_info) {
|
|
|
+ $info = $wrapper->info();
|
|
|
+ $properties = entity_get_all_property_info($info['type']);
|
|
|
+ $property_info['properties'] += $properties;
|
|
|
+ return $property_info;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Helper to enable or disable the invocation of rules events.
|
|
|
+ *
|
|
|
+ * Rules invocation is disabled by default, such that Rules does not operate
|
|
|
+ * when Drupal is not fully bootstrapped. It gets enabled in rules_init() and
|
|
|
+ * rules_enable().
|
|
|
+ *
|
|
|
+ * @param bool|null $enable
|
|
|
+ * NULL to leave the setting as is and TRUE / FALSE to change the behaviour.
|
|
|
+ *
|
|
|
+ * @return bool
|
|
|
+ * Whether the rules invocation is enabled or disabled.
|
|
|
+ */
|
|
|
+function rules_event_invocation_enabled($enable = NULL) {
|
|
|
+ static $invocation_enabled = FALSE;
|
|
|
+ if (isset($enable)) {
|
|
|
+ $invocation_enabled = (bool) $enable;
|
|
|
+ }
|
|
|
+ // Disable invocation if configured or if site runs in maintenance mode.
|
|
|
+ return $invocation_enabled && !defined('MAINTENANCE_MODE');
|
|
|
+}
|