- Patch #319210 by Dave Reid: DBTNG'ed actions.inc.

merge-requests/26/head
Dries Buytaert 2008-10-10 08:49:51 +00:00
parent 6828096e7e
commit 3ab972cd4f
1 changed files with 54 additions and 44 deletions

View File

@ -49,12 +49,11 @@ function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a
$available_actions = actions_list(); $available_actions = actions_list();
$result = array(); $result = array();
if (is_array($action_ids)) { if (is_array($action_ids)) {
$where = array();
$where_values = array(); $conditions = array();
foreach ($action_ids as $action_id) { foreach ($action_ids as $action_id) {
if (is_numeric($action_id)) { if (is_numeric($action_id)) {
$where[] = "OR aid = '%s'"; $conditions[] = $action_id;
$where_values[] = $action_id;
} }
elseif (isset($available_actions[$action_id])) { elseif (isset($available_actions[$action_id])) {
$actions[$action_id] = $available_actions[$action_id]; $actions[$action_id] = $available_actions[$action_id];
@ -63,12 +62,15 @@ function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a
// When we have action instances we must go to the database to // When we have action instances we must go to the database to
// retrieve instance data. // retrieve instance data.
if ($where) { if (!empty($conditions)) {
$where_clause = implode(' ', $where); $query = db_select('actions');
// Strip off leading 'OR '. $query->addField('actions', 'aid');
$where_clause = '(' . strstr($where_clause, " ") . ')'; $query->addField('actions', 'type');
$result_db = db_query('SELECT * FROM {actions} WHERE ' . $where_clause, $where_values); $query->addField('actions', 'callback');
while ($action = db_fetch_object($result_db)) { $query->addField('actions', 'parameters');
$query->condition('aid', $conditions, 'IN');
$result = $query->execute();
foreach ($result as $action) {
$actions[$action->aid] = $action->parameters ? unserialize($action->parameters) : array(); $actions[$action->aid] = $action->parameters ? unserialize($action->parameters) : array();
$actions[$action->aid]['callback'] = $action->callback; $actions[$action->aid]['callback'] = $action->callback;
$actions[$action->aid]['type'] = $action->type; $actions[$action->aid]['type'] = $action->type;
@ -92,7 +94,7 @@ function actions_do($action_ids, $object = NULL, $context = NULL, $a1 = NULL, $a
else { else {
// If it's a configurable action, retrieve stored parameters. // If it's a configurable action, retrieve stored parameters.
if (is_numeric($action_ids)) { if (is_numeric($action_ids)) {
$action = db_fetch_object(db_query("SELECT * FROM {actions} WHERE aid = '%s'", $action_ids)); $action = db_query("SELECT callback, parameters FROM {actions} WHERE aid = :aid", array(':aid' => $action_ids))->fetchObject();
$function = $action->callback; $function = $action->callback;
$context = array_merge($context, unserialize($action->parameters)); $context = array_merge($context, unserialize($action->parameters));
$result[$action_ids] = $function($object, $context, $a1, $a2); $result[$action_ids] = $function($object, $context, $a1, $a2);
@ -176,15 +178,11 @@ function actions_list($reset = FALSE) {
* 'type' and 'configurable'. * 'type' and 'configurable'.
*/ */
function actions_get_all_actions() { function actions_get_all_actions() {
$actions = array(); $actions = db_query("SELECT aid, type, callback, parameters, description FROM {actions}")->fetchAllAssoc('aid', PDO::FETCH_ASSOC);
$result = db_query("SELECT * FROM {actions}"); foreach ($actions as &$action) {
while ($action = db_fetch_object($result)) { $action['configurable'] = (bool) $action['parameters'];
$actions[$action->aid] = array( unset($action['parameters']);
'callback' => $action->callback, unset($action['aid']);
'description' => $action->description,
'type' => $action->type,
'configurable' => (bool) $action->parameters,
);
} }
return $actions; return $actions;
} }
@ -238,8 +236,7 @@ function actions_function_lookup($hash) {
} }
// Must be an instance; must check database. // Must be an instance; must check database.
$aid = db_result(db_query("SELECT aid FROM {actions} WHERE MD5(aid) = '%s' AND parameters <> ''", $hash)); return db_query("SELECT aid FROM {actions} WHERE MD5(aid) = :hash AND parameters <> ''", array(':hash' => $hash))->fetchField();
return $aid;
} }
/** /**
@ -254,11 +251,7 @@ function actions_synchronize($actions_in_code = array(), $delete_orphans = FALSE
if (!$actions_in_code) { if (!$actions_in_code) {
$actions_in_code = actions_list(TRUE); $actions_in_code = actions_list(TRUE);
} }
$actions_in_db = array(); $actions_in_db = db_query("SELECT aid, callback, description FROM {actions} WHERE parameters = ''")->fetchAllAssoc('callback', PDO::FETCH_ASSOC);
$result = db_query("SELECT * FROM {actions} WHERE parameters = ''");
while ($action = db_fetch_object($result)) {
$actions_in_db[$action->callback] = array('aid' => $action->aid, 'description' => $action->description);
}
// Go through all the actions provided by modules. // Go through all the actions provided by modules.
foreach ($actions_in_code as $callback => $array) { foreach ($actions_in_code as $callback => $array) {
@ -271,7 +264,15 @@ function actions_synchronize($actions_in_code = array(), $delete_orphans = FALSE
} }
else { else {
// This is a new singleton that we don't have an aid for; assign one. // This is a new singleton that we don't have an aid for; assign one.
db_query("INSERT INTO {actions} (aid, type, callback, parameters, description) VALUES ('%s', '%s', '%s', '%s', '%s')", $callback, $array['type'], $callback, '', $array['description']); db_insert('actions')
->fields(array(
'aid' => $callback,
'type' => $array['type'],
'callback' => $callback,
'parameters' => '',
'description' => $array['description'],
))
->execute();
watchdog('actions', "Action '%action' added.", array('%action' => filter_xss_admin($array['description']))); watchdog('actions', "Action '%action' added.", array('%action' => filter_xss_admin($array['description'])));
} }
} }
@ -282,8 +283,12 @@ function actions_synchronize($actions_in_code = array(), $delete_orphans = FALSE
$orphaned = array_keys($actions_in_db); $orphaned = array_keys($actions_in_db);
if ($delete_orphans) { if ($delete_orphans) {
$results = db_query("SELECT a.aid, a.description FROM {actions} a WHERE callback IN (" . db_placeholders($orphaned, 'varchar') . ")", $orphaned); $results = db_select('actions')
while ($action = db_fetch_object($results)) { ->addField('actions', 'aid')
->addField('actions', 'description')
->condition('callback', $orphaned, 'IN')
->execute();
foreach ($results as $action) {
actions_delete($action->aid); actions_delete($action->aid);
watchdog('actions', "Removed orphaned action '%action' from database.", array('%action' => filter_xss_admin($action->description))); watchdog('actions', "Removed orphaned action '%action' from database.", array('%action' => filter_xss_admin($action->description)));
} }
@ -315,20 +320,23 @@ function actions_synchronize($actions_in_code = array(), $delete_orphans = FALSE
* The ID of the action. * The ID of the action.
*/ */
function actions_save($function, $type, $params, $desc, $aid = NULL) { function actions_save($function, $type, $params, $desc, $aid = NULL) {
$serialized = serialize($params);
if ($aid) {
db_query("UPDATE {actions} SET callback = '%s', type = '%s', parameters = '%s', description = '%s' WHERE aid = '%s'", $function, $type, $serialized, $desc, $aid);
watchdog('actions', 'Action %action saved.', array('%action' => $desc));
}
else {
// aid is the callback for singleton actions so we need to keep a // aid is the callback for singleton actions so we need to keep a
// separate table for numeric aids. // separate table for numeric aids.
db_query('INSERT INTO {actions_aid} VALUES (default)'); if (!$aid) {
$aid = db_last_insert_id('actions_aid', 'aid'); $aid = db_insert('actions_aid')->execute();
db_query("INSERT INTO {actions} (aid, callback, type, parameters, description) VALUES ('%s', '%s', '%s', '%s', '%s')", $aid, $function, $type, $serialized, $desc);
watchdog('actions', 'Action %action created.', array('%action' => $desc));
} }
db_merge('actions')
->key(array('aid' => $aid))
->fields(array(
'callback' => $function,
'type' => $type,
'parameters' => serialize($params),
'description' => $desc,
))
->execute();
watchdog('actions', 'Action %action saved.', array('%action' => $desc));
return $aid; return $aid;
} }
@ -342,7 +350,7 @@ function actions_save($function, $type, $params, $desc, $aid = NULL) {
* The appropriate action row from the database as an object. * The appropriate action row from the database as an object.
*/ */
function actions_load($aid) { function actions_load($aid) {
return db_fetch_object(db_query("SELECT * FROM {actions} WHERE aid = '%s'", $aid)); return db_query("SELECT aid, type, callback, parameters, description FROM {actions} WHERE aid = :aid", array(':aid' => $aid))->fetchObject();
} }
/** /**
@ -352,6 +360,8 @@ function actions_load($aid) {
* integer The ID of the action to delete. * integer The ID of the action to delete.
*/ */
function actions_delete($aid) { function actions_delete($aid) {
db_query("DELETE FROM {actions} WHERE aid = '%s'", $aid); db_delete('actions')
->condition('aid', $aid)
->execute();
module_invoke_all('actions_delete', $aid); module_invoke_all('actions_delete', $aid);
} }