You are here

uuid.install in Universally Unique IDentifier 7

Same filename and directory in other branches
  1. 5 uuid.install
  2. 6 uuid.install

Install, update and uninstall functions for the uuid module.

File

uuid.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the uuid module.
 */
define('UUID_UPGRADE_VAR', 'uuid_upgrade_in_progress');

/**
 * Include some helper functions for the Entity API.
 */
module_load_include('inc', 'uuid', 'uuid.entity');

/**
 * Helper function that returns a schema field definition for UUID fields.
 *
 * @see uuid_schema_alter()
 * @see uuid_install()
 */
function uuid_schema_field_definition() {
  return array(
    'type' => 'char',
    'length' => 36,
    'not null' => TRUE,
    'default' => '',
    'description' => 'The Universally Unique Identifier.',
  );
}

/**
 * Implements hook_schema_alter().
 */
function uuid_schema_alter(array &$schema) {
  $field_info = uuid_schema_field_definition();
  $key_names = array(
    'base table' => 'uuid',
    'revision table' => 'revision uuid',
  );
  foreach (uuid_get_core_entity_info() as $entity_info) {
    foreach ($key_names as $table_type => $key_name) {
      if (isset($entity_info[$table_type], $entity_info['entity keys'][$key_name])) {
        $field_name = $entity_info['entity keys'][$key_name];
        $properties = array(
          'fields' => $field_info,
          'indexes' => array(
            $field_name,
          ),
        );
        foreach ($properties as $property => $value) {
          $schema[$entity_info[$table_type]][$property][$field_name] = $value;
        }
      }
    }
  }
}

/**
 * Implements hook_install().
 */
function uuid_install() {
  _uuid_install_uuid_fields();
  module_load_include('inc', 'uuid');
  uuid_sync_all();
}

/**
 * Install the uuid and vuuid fields for Drupal core entity tables where needed.
 *
 * IMPORTANT:  This function is called both at install and update time.  If this
 * method is modified to add additional fields in the future, the update
 * strategy must be considered.  See the comment in uuid_update_7102.
 */
function _uuid_install_uuid_fields() {
  $field = uuid_schema_field_definition();
  foreach (uuid_get_core_entity_info() as $info) {
    if (!db_field_exists($info['base table'], $info['entity keys']['uuid'])) {
      db_add_field($info['base table'], $info['entity keys']['uuid'], $field);
      db_add_index($info['base table'], $info['entity keys']['uuid'], array(
        $info['entity keys']['uuid'],
      ));
    }
    if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
      if (!db_field_exists($info['revision table'], $info['entity keys']['revision uuid'])) {
        db_add_field($info['revision table'], $info['entity keys']['revision uuid'], $field);
        db_add_index($info['revision table'], $info['entity keys']['revision uuid'], array(
          $info['entity keys']['revision uuid'],
        ));
      }
    }
  }
}

/**
 * Implements hook_uninstall().
 */
function uuid_uninstall() {
  foreach (uuid_get_core_entity_info() as $info) {
    if (db_field_exists($info['base table'], $info['entity keys']['uuid'])) {
      db_drop_field($info['base table'], $info['entity keys']['uuid']);
      db_drop_index($info['base table'], $info['entity keys']['uuid']);
    }
    if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
      if (db_field_exists($info['revision table'], $info['entity keys']['revision uuid'])) {
        db_drop_field($info['revision table'], $info['entity keys']['revision uuid']);
        db_drop_index($info['revision table'], $info['entity keys']['revision uuid']);
      }
    }
  }
}

/**
 * Implements hook_modules_installed().
 */
function uuid_modules_installed($modules) {

  // Run the installation hook. This makes sure that the schema for all
  // supported core entity types is set correct.
  uuid_install();
}

/**
 * Create uuid_vocabulary and uuid_term_data tables.
 */
function uuid_update_6001() {
  $ret = array();
  db_create_table($ret, 'uuid_vocabulary', uuid_table_schema('vocabulary', 'vid'));
  db_create_table($ret, 'uuid_term_data', uuid_table_schema('term_data', 'tid'));
  return $ret;
}

/**
 * Make all uuid columns unique keys instead of indexes.
 */
function uuid_update_6002() {
  $ret = array();
  foreach (uuid_schema() as $table => $schema) {
    db_drop_index($ret, $table, $table . '_uuid_idx');
    db_add_unique_key($ret, $table, $table . '_uuid_key', array(
      'uuid',
    ));
  }
  return $ret;
}

/**
 * Create uuid_comment table.
 */
function uuid_update_6003() {
  $ret = array();
  db_create_table($ret, 'uuid_comments', uuid_table_schema('comments', 'cid'));
  return $ret;
}

/**
 * Change column definitions for uuid columns to more efficient char spec.
 */
function uuid_update_6004() {
  $ret = array();

  // Use what's in uuid_table_schema in order to be consistent.
  $tables = uuid_schema();
  $spec = $tables['uuid_node']['fields']['uuid'];
  foreach ($tables as $tablename => $schema) {
    if (db_table_exists($tablename)) {
      db_change_field($ret, $tablename, 'uuid', 'uuid', $spec);
    }
  }
  return $ret;
}

/**
 * Support deleting node revision.
 *
 * Modify existing uuid_node_revisions table to support revision deletion, and
 * add in as much legacy data as possible.
 */
function uuid_update_6005() {
  $ret = array();
  if (db_table_exists('uuid_node_revisions')) {

    // Use what's already defined in uuid schema in order to be consistent.
    $schema = uuid_schema();
    $spec = $schema['uuid_node_revisions']['fields']['nid'];
    db_add_field($ret, 'uuid_node_revisions', 'nid', $spec);

    // Add node ids to the new column, for revisions that exist, but now have a
    // default value of 0 in uuid_node_revisions.
    $result = db_query('SELECT nr.nid, nr.vid FROM {node_revisions} AS nr LEFT JOIN {uuid_node_revisions} AS unr ON unr.vid=nr.vid WHERE unr.nid=%d', 0);
    while ($item = db_fetch_object($result)) {
      $ret[] = update_sql('UPDATE {uuid_node_revisions} SET nid=' . (int) $item->nid . ' WHERE vid=' . (int) $item->vid);
    }

    // Add uuid_node_revision rows for rows that don't exist, but should.
    $result = db_query('SELECT nr.nid, nr.vid FROM {node_revisions} AS nr LEFT JOIN {uuid_node_revisions} AS unr ON unr.vid=nr.vid WHERE unr.nid IS NULL');
    while ($item = db_fetch_object($result)) {
      $ret[] = update_sql(sprintf("INSERT INTO {uuid_node_revisions} (vid, uuid, nid) VALUES(%d, '%s', %d)", $item->vid, uuid_uuid(), $item->nid));
    }

    // Delete any orphaned revision vid, uuid pairs.
    $ret[] = update_sql('DELETE FROM {uuid_node_revisions} WHERE nid=0');
  }
  return $ret;
}

/**
 * Remove old variables.
 */
function uuid_update_7100() {
  $types = array(
    'nodes',
    'users',
    'taxonomy',
    'comments',
  );
  foreach ($types as $type) {
    variable_del('uuid_automatic_for_' . $type);
  }
  return array();
}

/**
 * Clear cache for installations that used alpha1.
 *
 * Modules previously enabled in uuid_update_7100() don't exist any more. We
 * need to clear the cache so Drupal detects this change.
 */
function uuid_update_7101() {
  drupal_flush_all_caches();
}

/**
 * Ensure that the uuid and vuuid fields are added where needed.
 *
 * Note that update 7102 calls _uuid_install_uuid_fields(), which is an
 * idempotent function.  If _uuid_install_uuid_fields() is changed at some
 * point in the future (but remains idempotent), then some uuid users
 * will have run update 7102, and some will not.  A new uuid_update_7103()
 * function would would therefore be necessary to update all users to
 * the latest schema.  At the same time, uuid_update_7102() could become
 * an empty function, as it would not be necessary to call
 * _uuid_install_uuid_fields() twice.
 */
function uuid_update_7102() {

  // If the user have disabled the UUID module during upgrade (as UPGRADE.txt
  // instructs), some functions will be missing. So include the module file.
  module_load_include('module', 'uuid', 'uuid');
  _uuid_install_uuid_fields();
  uuid_sync_all();
}

/**
 * Clean up entities created by uuid_default_entities_example module.
 *
 * Modify the labels of all example entities created by the now removed
 * uuid_default_entities_example.module to make it clear they're examples. Also
 * remove the administrator role of any example user.
 */
function uuid_update_7103() {

  // These are UUIDs of all the example entities that might exist after having
  // installed uuid_default_entities_example.module.
  $info = entity_get_info();
  $uuids = array(
    'node' => array(
      'b0558664-c94b-3674-d9df-3e1696b2e471',
      '5e3d8bbe-a1f2-f2d4-fdc0-71e6c23aa837',
    ),
    'user' => array(
      '7cf875e6-dc15-4404-f190-5a7c3e91d14c',
    ),
  );

  // We can't assume taxonomy is enabled.
  if (isset($info['taxonomy_term'])) {
    $uuids['taxonomy_term'] = array(
      'bcb92ce8-2236-e264-65c8-0c163ae716d1',
      '4293a15c-531a-6164-7d1b-668ed019a6bd',
      'af738a46-f278-cf84-d94d-9e03879fd71e',
    );
  }
  foreach (array_keys($uuids) as $entity_type) {
    $info = entity_get_info($entity_type);
    $entity_ids = entity_get_id_by_uuid($entity_type, $uuids[$entity_type]);
    $entities = entity_load($entity_type, $entity_ids);
    foreach ($entities as $entity) {

      // Update the label to make it clear this is example content.
      $entity->{$info['entity keys']['label']} = $entity->{$info['entity keys']['label']} . ' (UUID example)';

      // Remove the administrator role from any user.
      if ($entity_type == 'user' && ($rid = array_search('administrator', $entity->roles))) {
        unset($entity->roles[$rid]);
      }
      entity_save($entity_type, $entity);
    }
  }
}

Functions

Namesort descending Description
uuid_install Implements hook_install().
uuid_modules_installed Implements hook_modules_installed().
uuid_schema_alter Implements hook_schema_alter().
uuid_schema_field_definition Helper function that returns a schema field definition for UUID fields.
uuid_uninstall Implements hook_uninstall().
uuid_update_6001 Create uuid_vocabulary and uuid_term_data tables.
uuid_update_6002 Make all uuid columns unique keys instead of indexes.
uuid_update_6003 Create uuid_comment table.
uuid_update_6004 Change column definitions for uuid columns to more efficient char spec.
uuid_update_6005 Support deleting node revision.
uuid_update_7100 Remove old variables.
uuid_update_7101 Clear cache for installations that used alpha1.
uuid_update_7102 Ensure that the uuid and vuuid fields are added where needed.
uuid_update_7103 Clean up entities created by uuid_default_entities_example module.
_uuid_install_uuid_fields Install the uuid and vuuid fields for Drupal core entity tables where needed.

Constants

Namesort descending Description
UUID_UPGRADE_VAR @file Install, update and uninstall functions for the uuid module.