You are here

function uuid_nodeapi in Universally Unique IDentifier 6

Same name and namespace in other branches
  1. 5 uuid.module \uuid_nodeapi()

Implementation of hook_nodeapi().

File

./uuid.module, line 30
Main module functions for the uuid module.

Code

function uuid_nodeapi(&$node, $op, $teaser, $page) {
  $automatic_types = variable_get('uuid_automatic_for_nodes', array());

  // Return early if we don't automatically generate UUIDs for this type.
  if (empty($automatic_types[$node->type])) {
    return;
  }
  switch ($op) {
    case 'load':
      return uuid_node_load($node);
    case 'insert':

      // Handle the case where automatic UUID generation is turned OFF but a
      // node is created, node_save(), intentionally with the $node->uuid
      // attribute.
      if (isset($node->uuid) && uuid_is_valid($node->uuid)) {
        db_query("INSERT INTO {uuid_node} (nid, uuid) VALUES (%d, '%s')", $node->nid, $node->uuid);
        if (isset($node->revision_uuid) && uuid_is_valid($node->revision_uuid)) {
          db_query("INSERT INTO {uuid_node_revisions} (vid, uuid, nid) VALUES (%d, '%s', %d)", $node->vid, $node->revision_uuid, $node->nid);
        }
        else {

          // Attach new revision uuid to node, don't just insert it.
          $node->revision_uuid = uuid_uuid();
          db_query("INSERT INTO {uuid_node_revisions} (vid, uuid, nid) VALUES (%d, '%s', %d)", $node->vid, $node->revision_uuid, $node->nid);
        }
      }
      elseif (in_array($node->type, $automatic_types, TRUE)) {
        $node->uuid = uuid_uuid();
        $node->revision_uuid = uuid_uuid();
        db_query("INSERT INTO {uuid_node} (nid, uuid) VALUES (%d, '%s')", $node->nid, $node->uuid);
        db_query("INSERT INTO {uuid_node_revisions} (vid, uuid, nid) VALUES (%d, '%s', %d)", $node->vid, $node->revision_uuid, $node->nid);
      }
      break;
    case 'update':

      // Get existing UUID info from the DB.
      $uuid_information = uuid_node_load($node);

      // If the $node object is not fully loaded, set any existing UUID properties.
      // http://drupal.org/node/1065364
      if (!empty($uuid_information['uuid']) && empty($node->uuid)) {
        $node->uuid = $uuid_information['uuid'];
      }
      if (!empty($uuid_information['revision_uuid']) && empty($node->revision_uuid)) {
        $node->revision_uuid = $uuid_information['revision_uuid'];
      }
      $auto_create = in_array($node->type, $automatic_types, TRUE);
      $uuid_exists = isset($node->uuid) && uuid_is_valid($node->uuid);

      // Create a UUID if this is an automatic type but no UUID exists. Save the
      // UUID if it's valid but is not in the database.
      if ($uuid_exists || $auto_create) {
        $node->uuid = $uuid_exists ? $node->uuid : uuid_uuid();

        // Update the existing UUID.
        if ($uuid_information['uuid'] === FALSE) {
          db_query("INSERT INTO {uuid_node} (nid, uuid) VALUES (%d, '%s')", $node->nid, $node->uuid);
        }
        elseif ($uuid_information['uuid'] != $node->uuid) {
          db_query("UPDATE {uuid_node} SET uuid = '%s' WHERE nid = %d", $node->uuid, $node->nid);
        }
        $uuid_exists = TRUE;
      }
      $rev_uuid_exists = isset($node->revision_uuid) && uuid_is_valid($node->revision_uuid);
      $rev_uuid_in_db = $rev_uuid_exists ? db_result(db_query("SELECT 1 FROM {uuid_node_revisions} WHERE uuid = '%s'", $node->revision_uuid)) : FALSE;
      $rev_uuid_needs_saved = $rev_uuid_exists && !$rev_uuid_in_db;

      // If a UUID exists, save a revision UUID if none exists or this is a new
      // revision, or there's a programmatic revision set.
      if ($uuid_exists && (empty($uuid_information['revision_uuid']) || !empty($node->revision) || $rev_uuid_needs_saved)) {

        // We need to determine when to generate a new revision UUID. This
        // should only happen if this is a new revision and if the existing
        // revision uuid already exists in the database or doesn't exist at all.
        $generate_new = !empty($node->revision) && ($rev_uuid_in_db || !$rev_uuid_exists);
        $node->revision_uuid = $generate_new ? uuid_uuid() : $node->revision_uuid;

        // Be sure to consider the case where the revision_uuid may be changing
        // programmatically, so try updating first.
        db_query("UPDATE {uuid_node_revisions} SET uuid = '%s' WHERE vid = %d", $node->revision_uuid, $node->vid);
        if (!db_affected_rows()) {
          db_query("INSERT INTO {uuid_node_revisions} (vid, uuid, nid) VALUES (%d, '%s', %d)", $node->vid, $node->revision_uuid, $node->nid);
        }
      }
      break;
    case 'delete':
      db_query('DELETE FROM {uuid_node} WHERE nid = %d', $node->nid);

      // Delete the revision uuids for the node.
      db_query('DELETE FROM {uuid_node_revisions} WHERE nid = %d', $node->nid);
      break;
    case 'delete revision':
      db_query('DELETE FROM {uuid_node_revisions} WHERE vid = %d', $node->vid);
      break;
  }
}