You are here

function flag_update_5000 in Flag 5

Delete obsolete actions provided by beta3.

Note: To ease maintenance, this code is compatible with both D5 and D6.

Check out #305391 for a discussion about this code.

File

./flag.install, line 238
Flag module install/update hooks.

Code

function flag_update_5000() {
  $ret = array();

  // This conditional ensures we have Actions 2.x, not Actions 1.x.
  //
  // At the beginning we had only db_table_exists() checks here. However, it
  // turned out that db_table_exists() fails when the $db_prefix string contains
  // a dot. So module_exists() and function_exists() are added, as backup.
  if (module_exists('trigger') || function_exists('_actions_get_hook_aids') || db_table_exists('actions_assignments') || db_table_exists('trigger_assignments')) {

    // Step 1: Find all actions we need to delete.
    $aids = array();

    // We can't just search for "actions of type flag" because this isn't future-compatible.
    $res = db_query("SELECT aid FROM {actions} WHERE callback IN ('flag_action_email', 'flag_action_delete', 'flag_action_unpublish', 'flag_action_moderate')");
    while ($row = db_fetch_object($res)) {
      $aids[] = $row->aid;
    }
    if (!$aids) {
      $ret[] = array(
        'success' => TRUE,
        'query' => t('No old actions to remove.'),
      );
    }
    else {
      $ret[] = array(
        'success' => TRUE,
        'query' => t('Deleting the following actions: @aids', array(
          '@aids' => implode(', ', $aids),
        )),
      );
    }

    // Step 2: Delete them through API.
    // We can't do `if (module_exists('actions'))`: this code should work for D6 as
    // well, and it doesn't have an Actions module.
    if (function_exists('actions_delete')) {
      foreach ($aids as $aid) {
        actions_delete($aid);
        $ret[] = array(
          'success' => TRUE,
          'query' => t('actions_delete("@aid") called.', array(
            '@aid' => $aid,
          )),
        );
      }
    }

    // Step 3: Delete them through SQL, in case Actions/Trigger aren't enabled.
    foreach ($aids as $aid) {
      foreach (array(
        'actions',
        'actions_assignments',
        'trigger_assignments',
      ) as $table) {
        if (db_table_exists($table)) {
          $ret[] = _flag_update_sql("DELETE FROM {{$table}} WHERE aid = '%s'", $aid);
        }
      }
    }

    // Note: No need to delete from the {actions_aid} table; Actions doesn't do that.
    // Step 4: Remove a bogus record possibly created because of a bug (see
    // http://drupal.org/node/271460).
    foreach (array(
      'actions_assignments',
      'trigger_assignments',
    ) as $table) {
      if (db_table_exists($table)) {
        $ret[] = _flag_update_sql("DELETE FROM {{$table}} WHERE hook = 'flag' AND aid = ''");
      }
    }
  }
  else {
    $ret[] = array(
      'success' => TRUE,
      'query' => t('Cleanup of Actions tables is unneeded.'),
    );
  }
  return $ret;
}