| 1 | | <?php |
| 2 | | // $Id: actions.inc,v 1.21 2008/10/10 08:49:51 dries Exp $ |
| 3 | | |
| 4 | | /** |
| 5 | | * @file |
| 6 | | * This is the actions engine for executing stored actions. |
| 7 | | */ |
| 8 | | |
| 9 | | /** |
| 10 | | * Perform a given list of actions by executing their callback functions. |
| 11 | | * |
| 12 | | * Given the IDs of actions to perform, find out what the callbacks |
| 13 | | * for the actions are by querying the database. Then call each callback |
| 14 | | * using the function call $function($object, $context, $a1, $a2) |
| 15 | | * where $function is the name of a function written in compliance with |
| 16 | | * the action specification; that is, foo($object, $context). |
| 17 | | * |
| 18 | | * @param $action_ids |
| 19 | | * The ID of the action to perform. Can be a single action ID or an array |
| 20 | | * of IDs. IDs of instances will be numeric; IDs of singletons will be |
| 21 | | * function names. |
| 22 | | * @param $object |
| 23 | | * Parameter that will be passed along to the callback. Typically the |
| 24 | | * object that the action will act on; a node, user or comment object. |
| 25 | | * @param $context |
| 26 | | * Parameter that will be passed along to the callback. $context is a |
| 27 | | * keyed array containing extra information about what is currently |
| 28 | | * happening at the time of the call. Typically $context['hook'] and |
| 29 | | * $context['op'] will tell which hook-op combination resulted in this |
| 30 | | * call to actions_do(). |
| 31 | | * @param $a1 |
| 32 | | * Parameter that will be passed along to the callback. |
| 33 | | * @param $a2 |
| 34 | | * Parameter that will be passed along to the callback. |
| 35 | | * |
| 36 | | * @return |
| 37 | | * An associative array containing the result of the function that |
| 38 | | * performs the action, keyed on action ID. |
| 39 | | */ |
| 40 | 2366 | function actions_do($action_ids, $object = NULL, $context = NULL, $a1 =
NULL, $a2 = NULL) { |
| 41 | | // $stack tracks the number of recursive calls. |
| 42 | 6 | static $stack; |
| 43 | 6 | $stack++; |
| 44 | 6 | if ($stack > variable_get('actions_max_stack', 35)) { |
| 45 | 0 | watchdog('actions', 'Stack overflow: too many calls to actions_do().
Aborting to prevent infinite recursion.', array(), WATCHDOG_ERROR); |
| 46 | 0 | return; |
| 47 | 0 | } |
| 48 | 6 | $actions = array(); |
| 49 | 6 | $available_actions = actions_list(); |
| 50 | 6 | $result = array(); |
| 51 | 6 | if (is_array($action_ids)) { |
| 52 | | |
| 53 | 0 | $conditions = array(); |
| 54 | 0 | foreach ($action_ids as $action_id) { |
| 55 | 0 | if (is_numeric($action_id)) { |
| 56 | 0 | $conditions[] = $action_id; |
| 57 | 0 | } |
| 58 | 0 | elseif (isset($available_actions[$action_id])) { |
| 59 | 0 | $actions[$action_id] = $available_actions[$action_id]; |
| 60 | 0 | } |
| 61 | 0 | } |
| 62 | | |
| 63 | | // When we have action instances we must go to the database to |
| 64 | | // retrieve instance data. |
| 65 | 0 | if (!empty($conditions)) { |
| 66 | 0 | $query = db_select('actions'); |
| 67 | 0 | $query->addField('actions', 'aid'); |
| 68 | 0 | $query->addField('actions', 'type'); |
| 69 | 0 | $query->addField('actions', 'callback'); |
| 70 | 0 | $query->addField('actions', 'parameters'); |
| 71 | 0 | $query->condition('aid', $conditions, 'IN'); |
| 72 | 0 | $result = $query->execute(); |
| 73 | 0 | foreach ($result as $action) { |
| 74 | 0 | $actions[$action->aid] = $action->parameters ?
unserialize($action->parameters) : array(); |
| 75 | 0 | $actions[$action->aid]['callback'] = $action->callback; |
| 76 | 0 | $actions[$action->aid]['type'] = $action->type; |
| 77 | 0 | } |
| 78 | 0 | } |
| 79 | | |
| 80 | | // Fire actions, in no particular order. |
| 81 | 0 | foreach ($actions as $action_id => $params) { |
| 82 | 0 | if (is_numeric($action_id)) { // Configurable actions need
parameters. |
| 83 | 0 | $function = $params['callback']; |
| 84 | 0 | $context = array_merge($context, $params); |
| 85 | 0 | $result[$action_id] = $function($object, $context, $a1, $a2); |
| 86 | 0 | } |
| 87 | | // Singleton action; $action_id is the function name. |
| 88 | | else { |
| 89 | 0 | $result[$action_id] = $action_id($object, $context, $a1, $a2); |
| 90 | | } |
| 91 | 0 | } |
| 92 | 0 | } |
| 93 | | // Optimized execution of single action. |
| 94 | | else { |
| 95 | | // If it's a configurable action, retrieve stored parameters. |
| 96 | 6 | if (is_numeric($action_ids)) { |
| 97 | 0 | $action = db_query("SELECT callback, parameters FROM {actions} WHERE
aid = :aid", array(':aid' => $action_ids))->fetchObject(); |
| 98 | 0 | $function = $action->callback; |
| 99 | 0 | $context = array_merge($context, unserialize($action->parameters)); |
| 100 | 0 | $result[$action_ids] = $function($object, $context, $a1, $a2); |
| 101 | 0 | } |
| 102 | | // Singleton action; $action_ids is the function name. |
| 103 | | else { |
| 104 | 6 | $result[$action_ids] = $action_ids($object, $context, $a1, $a2); |
| 105 | | } |
| 106 | | } |
| 107 | 6 | $stack--; |
| 108 | 6 | return $result; |
| 109 | 0 | } |
| 110 | | |
| 111 | | |
| 112 | | /** |
| 113 | | * Discover all action functions by invoking hook_action_info(). |
| 114 | | * |
| 115 | | * mymodule_action_info() { |
| 116 | | * return array( |
| 117 | | * 'mymodule_functiondescription_action' => array( |
| 118 | | * 'type' => 'node', |
| 119 | | * 'description' => t('Save node'), |
| 120 | | * 'configurable' => FALSE, |
| 121 | | * 'hooks' => array( |
| 122 | | * 'nodeapi' => array('delete', 'insert', 'update', 'view'), |
| 123 | | * 'comment' => array('delete', 'insert', 'update', 'view'), |
| 124 | | * ) |
| 125 | | * ) |
| 126 | | * ); |
| 127 | | * } |
| 128 | | * |
| 129 | | * The description is used in presenting possible actions to the user for |
| 130 | | * configuration. The type is used to present these actions in a logical |
| 131 | | * grouping and to denote context. Some types are 'node', 'user',
'comment', |
| 132 | | * and 'system'. If an action is configurable it will provide form, |
| 133 | | * validation and submission functions. The hooks the action supports |
| 134 | | * are declared in the 'hooks' array. |
| 135 | | * |
| 136 | | * @param $reset |
| 137 | | * Reset the action info static cache. |
| 138 | | * |
| 139 | | * @return |
| 140 | | * An associative array keyed on function name. The value of each key is |
| 141 | | * an array containing information about the action, such as type of |
| 142 | | * action and description of the action, e.g., |
| 143 | | * |
| 144 | | * @code |
| 145 | | * $actions['node_publish_action'] = array( |
| 146 | | * 'type' => 'node', |
| 147 | | * 'description' => t('Publish post'), |
| 148 | | * 'configurable' => FALSE, |
| 149 | | * 'hooks' => array( |
| 150 | | * 'nodeapi' => array('presave', 'insert', 'update', 'view'), |
| 151 | | * 'comment' => array('delete', 'insert', 'update', 'view'), |
| 152 | | * ), |
| 153 | | * ); |
| 154 | | * @endcode |
| 155 | | */ |
| 156 | 2366 | function actions_list($reset = FALSE) { |
| 157 | 215 | static $actions; |
| 158 | 215 | if (!isset($actions) || $reset) { |
| 159 | 215 | $actions = module_invoke_all('action_info'); |
| 160 | 215 | drupal_alter('action_info', $actions); |
| 161 | 215 | } |
| 162 | | |
| 163 | | // See module_implements for explanations of this cast. |
| 164 | 215 | return (array)$actions; |
| 165 | 0 | } |
| 166 | | |
| 167 | | /** |
| 168 | | * Retrieve all action instances from the database. |
| 169 | | * |
| 170 | | * Compare with actions_list() which gathers actions by |
| 171 | | * invoking hook_action_info(). The two are synchronized |
| 172 | | * by visiting /admin/build/actions (when actions.module is |
| 173 | | * enabled) which runs actions_synchronize(). |
| 174 | | * |
| 175 | | * @return |
| 176 | | * Associative array keyed by action ID. Each value is |
| 177 | | * an associative array with keys 'callback', 'description', |
| 178 | | * 'type' and 'configurable'. |
| 179 | | */ |
| 180 | 2366 | function actions_get_all_actions() { |
| 181 | 60 | $actions = db_query("SELECT aid, type, callback, parameters, description
FROM {actions}")->fetchAllAssoc('aid', PDO::FETCH_ASSOC); |
| 182 | 60 | foreach ($actions as &$action) { |
| 183 | 60 | $action['configurable'] = (bool) $action['parameters']; |
| 184 | 60 | unset($action['parameters']); |
| 185 | 60 | unset($action['aid']); |
| 186 | 60 | } |
| 187 | 60 | return $actions; |
| 188 | 0 | } |
| 189 | | |
| 190 | | /** |
| 191 | | * Create an associative array keyed by md5 hashes of function names. |
| 192 | | * |
| 193 | | * Hashes are used to prevent actual function names from going out into |
| 194 | | * HTML forms and coming back. |
| 195 | | * |
| 196 | | * @param $actions |
| 197 | | * An associative array with function names as keys and associative |
| 198 | | * arrays with keys 'description', 'type', etc. as values. Generally |
| 199 | | * the output of actions_list() or actions_get_all_actions() is given |
| 200 | | * as input to this function. |
| 201 | | * |
| 202 | | * @return |
| 203 | | * An associative array keyed on md5 hash of function name. The value of |
| 204 | | * each key is an associative array of function, description, and type |
| 205 | | * for the action. |
| 206 | | */ |
| 207 | 2366 | function actions_actions_map($actions) { |
| 208 | 60 | $actions_map = array(); |
| 209 | 60 | foreach ($actions as $callback => $array) { |
| 210 | 60 | $key = md5($callback); |
| 211 | 60 | $actions_map[$key]['callback'] = isset($array['callback']) ?
$array['callback'] : $callback; |
| 212 | 60 | $actions_map[$key]['description'] = $array['description']; |
| 213 | 60 | $actions_map[$key]['type'] = $array['type']; |
| 214 | 60 | $actions_map[$key]['configurable'] = $array['configurable']; |
| 215 | 60 | } |
| 216 | 60 | return $actions_map; |
| 217 | 0 | } |
| 218 | | |
| 219 | | /** |
| 220 | | * Given an md5 hash of a function name, return the function name. |
| 221 | | * |
| 222 | | * Faster than actions_actions_map() when you only need the function name. |
| 223 | | * |
| 224 | | * @param $hash |
| 225 | | * MD5 hash of a function name |
| 226 | | * |
| 227 | | * @return |
| 228 | | * Function name |
| 229 | | */ |
| 230 | 2366 | function actions_function_lookup($hash) { |
| 231 | 32 | $actions_list = actions_list(); |
| 232 | 32 | foreach ($actions_list as $function => $array) { |
| 233 | 32 | if (md5($function) == $hash) { |
| 234 | 32 | return $function; |
| 235 | 0 | } |
| 236 | 32 | } |
| 237 | | |
| 238 | | // Must be an instance; must check database. |
| 239 | 0 | return db_query("SELECT aid FROM {actions} WHERE MD5(aid) = :hash AND
parameters <> ''", array(':hash' => $hash))->fetchField(); |
| 240 | 0 | } |
| 241 | | |
| 242 | | /** |
| 243 | | * Synchronize actions that are provided by modules. |
| 244 | | * |
| 245 | | * They are synchronized with actions that are stored in the actions table. |
| 246 | | * This is necessary so that actions that do not require configuration can |
| 247 | | * receive action IDs. This is not necessarily the best approach, |
| 248 | | * but it is the most straightforward. |
| 249 | | */ |
| 250 | 2366 | function actions_synchronize($actions_in_code = array(), $delete_orphans =
FALSE) { |
| 251 | 143 | if (!$actions_in_code) { |
| 252 | 137 | $actions_in_code = actions_list(TRUE); |
| 253 | 137 | } |
| 254 | 143 | $actions_in_db = db_query("SELECT aid, callback, description FROM
{actions} WHERE parameters = ''")->fetchAllAssoc('callback',
PDO::FETCH_ASSOC); |
| 255 | | |
| 256 | | // Go through all the actions provided by modules. |
| 257 | 143 | foreach ($actions_in_code as $callback => $array) { |
| 258 | | // Ignore configurable actions since their instances get put in |
| 259 | | // when the user adds the action. |
| 260 | 143 | if (!$array['configurable']) { |
| 261 | | // If we already have an action ID for this action, no need to assign
aid. |
| 262 | 143 | if (array_key_exists($callback, $actions_in_db)) { |
| 263 | 10 | unset($actions_in_db[$callback]); |
| 264 | 10 | } |
| 265 | | else { |
| 266 | | // This is a new singleton that we don't have an aid for; assign
one. |
| 267 | 134 | db_insert('actions') |
| 268 | 134 | ->fields(array( |
| 269 | 134 | 'aid' => $callback, |
| 270 | 134 | 'type' => $array['type'], |
| 271 | 134 | 'callback' => $callback, |
| 272 | 134 | 'parameters' => '', |
| 273 | 134 | 'description' => $array['description'], |
| 274 | 134 | )) |
| 275 | 134 | ->execute(); |
| 276 | 134 | watchdog('actions', "Action '%action' added.", array('%action' =>
filter_xss_admin($array['description']))); |
| 277 | | } |
| 278 | 143 | } |
| 279 | 143 | } |
| 280 | | |
| 281 | | // Any actions that we have left in $actions_in_db are orphaned. |
| 282 | 143 | if ($actions_in_db) { |
| 283 | 0 | $orphaned = array_keys($actions_in_db); |
| 284 | | |
| 285 | 0 | if ($delete_orphans) { |
| 286 | 0 | $results = db_select('actions') |
| 287 | 0 | ->addField('actions', 'aid') |
| 288 | 0 | ->addField('actions', 'description') |
| 289 | 0 | ->condition('callback', $orphaned, 'IN') |
| 290 | 0 | ->execute(); |
| 291 | 0 | foreach ($results as $action) { |
| 292 | 0 | actions_delete($action->aid); |
| 293 | 0 | watchdog('actions', "Removed orphaned action '%action' from
database.", array('%action' => filter_xss_admin($action->description))); |
| 294 | 0 | } |
| 295 | 0 | } |
| 296 | | else { |
| 297 | 0 | $link = l(t('Remove orphaned actions'),
'admin/settings/actions/orphan'); |
| 298 | 0 | $count = count($actions_in_db); |
| 299 | 0 | $orphans = implode(', ', $orphaned); |
| 300 | 0 | watchdog('actions', format_plural($count, 'One orphaned action
(%orphans) exists in the actions table. !link', '@count orphaned actions
(%orphans) exist in the actions table. !link'), array('@count' => $count,
'%orphans' => $orphans, '!link' => $link), WATCHDOG_WARNING); |
| 301 | | } |
| 302 | 0 | } |
| 303 | 143 | } |
| 304 | | |
| 305 | | /** |
| 306 | | * Save an action and its associated user-supplied parameter values to the
database. |
| 307 | | * |
| 308 | | * @param $function |
| 309 | | * The name of the function to be called when this action is performed. |
| 310 | | * @param $params |
| 311 | | * An associative array with parameter names as keys and parameter values |
| 312 | | * as values. |
| 313 | | * @param $desc |
| 314 | | * A user-supplied description of this particular action, e.g., 'Send |
| 315 | | * e-mail to Jim'. |
| 316 | | * @param $aid |
| 317 | | * The ID of this action. If omitted, a new action is created. |
| 318 | | * |
| 319 | | * @return |
| 320 | | * The ID of the action. |
| 321 | | */ |
| 322 | 2366 | function actions_save($function, $type, $params, $desc, $aid = NULL) { |
| 323 | | // aid is the callback for singleton actions so we need to keep a |
| 324 | | // separate table for numeric aids. |
| 325 | 2 | if (!$aid) { |
| 326 | 1 | $aid = db_insert('actions_aid')->execute(); |
| 327 | 1 | } |
| 328 | | |
| 329 | 2 | db_merge('actions') |
| 330 | 2 | ->key(array('aid' => $aid)) |
| 331 | 2 | ->fields(array( |
| 332 | 2 | 'callback' => $function, |
| 333 | 2 | 'type' => $type, |
| 334 | 2 | 'parameters' => serialize($params), |
| 335 | 2 | 'description' => $desc, |
| 336 | 2 | )) |
| 337 | 2 | ->execute(); |
| 338 | | |
| 339 | 2 | watchdog('actions', 'Action %action saved.', array('%action' => $desc)); |
| 340 | 2 | return $aid; |
| 341 | 0 | } |
| 342 | | |
| 343 | | /** |
| 344 | | * Retrieve a single action from the database. |
| 345 | | * |
| 346 | | * @param $aid |
| 347 | | * integer The ID of the action to retrieve. |
| 348 | | * |
| 349 | | * @return |
| 350 | | * The appropriate action row from the database as an object. |
| 351 | | */ |
| 352 | 2366 | function actions_load($aid) { |
| 353 | 3 | return db_query("SELECT aid, type, callback, parameters, description FROM
{actions} WHERE aid = :aid", array(':aid' => $aid))->fetchObject(); |
| 354 | 0 | } |
| 355 | | |
| 356 | | /** |
| 357 | | * Delete a single action from the database. |
| 358 | | * |
| 359 | | * @param $aid |
| 360 | | * integer The ID of the action to delete. |
| 361 | | */ |
| 362 | 2366 | function actions_delete($aid) { |
| 363 | 1 | db_delete('actions') |
| 364 | 1 | ->condition('aid', $aid) |
| 365 | 1 | ->execute(); |
| 366 | 1 | module_invoke_all('actions_delete', $aid); |
| 367 | 1 | } |
| 368 | 2366 | |