You are here

user_revision.install in User Revision 7.2

Same filename and directory in other branches
  1. 8 user_revision.install
  2. 7 user_revision.install

Install, update and uninstall functions for the user_revision module.

File

user_revision.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the user_revision module.
 */

/**
 * Implements hook_schema().
 */
function user_revision_schema() {
  $schema['user_revision'] = array(
    'description' => 'Stores user_revision data.',
    'fields' => array(
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary Key: Unique user ID.',
        'default' => 0,
      ),
      'vid' => array(
        'description' => 'The primary identifier for this version.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'log' => array(
        'description' => 'The log entry explaining the changes in this version.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
      ),
      'timestamp' => array(
        'description' => 'A Unix timestamp indicating when this version was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'authorid' => array(
        'description' => 'The {users}.uid that created this version.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 60,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Unique user name.',
      ),
      'mail' => array(
        'type' => 'varchar',
        'length' => 254,
        'not null' => FALSE,
        'default' => '',
        'description' => "User's e-mail address.",
      ),
      'theme' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "User's default theme.",
      ),
      'signature' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "User's signature.",
      ),
      'signature_format' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'The {filter_format}.format of the signature.',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Whether the user is active(1) or blocked(0).',
      ),
      'timezone' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => FALSE,
        'description' => "User's time zone.",
      ),
      'language' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
        'description' => "User's default language.",
      ),
      'picture' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => "Foreign key: {file_managed}.fid of user's picture.",
      ),
      'data' => array(
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of name value pairs that are related to the user. Any form values posted during user edit are stored and are loaded into the $user object during user_load(). Use of this field is discouraged and it will likely disappear in a future version of Drupal.',
      ),
      'ip' => array(
        'type' => 'varchar',
        'description' => 'The users\'s ip address.',
        'length' => 256,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'indexes' => array(
      'uid' => array(
        'uid',
      ),
    ),
    'primary key' => array(
      'vid',
    ),
    'foreign keys' => array(
      'versioned_user' => array(
        'table' => 'users',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
      'version_author' => array(
        'table' => 'users',
        'columns' => array(
          'authorid' => 'uid',
        ),
      ),
      'signature_format' => array(
        'table' => 'filter_format',
        'columns' => array(
          'signature_format' => 'format',
        ),
      ),
    ),
  );
  $schema['user_revision_roles'] = array(
    'description' => 'Stores user_revision roles data.',
    'fields' => array(
      'uid' => array(
        'description' => 'Primary Key: Unique user ID.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => 'Primary Key: Unique version ID.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'rid' => array(
        'description' => 'Primary Key: Role ID.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      'uid' => array(
        'uid',
      ),
    ),
    'primary key' => array(
      'uid',
      'vid',
      'rid',
    ),
    'foreign keys' => array(
      'versioned_user' => array(
        'table' => 'users',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
      'version_id' => array(
        'table' => 'user_revision',
        'columns' => array(
          'vid' => 'vid',
        ),
      ),
      'role_id' => array(
        'table' => 'role',
        'columns' => array(
          'rid' => 'rid',
        ),
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function user_revision_install() {
  $schema['users'] = array();
  include_once 'user_revision.module';
  user_revision_schema_alter($schema);
  foreach ($schema['users']['fields'] as $name => $spec) {
    db_add_field('users', $name, $spec);
  }

  // Set all initial vid values equal to uid values.
  db_update('users')
    ->expression('vid', 'uid')
    ->execute();

  // Add unique keys.
  foreach ($schema['users']['unique keys'] as $key => $spec) {
    db_add_unique_key('users', $key, $spec);
  }

  // Update weight of module in system table.
  db_update('system')
    ->fields(array(
    'weight' => 99,
  ))
    ->condition('name', 'user_revision', '=')
    ->execute();

  // Prepare Batch API to add all users to the revision table.
  $total_count = db_select('users', 'u')
    ->condition('u.uid', 0, '!=')
    ->countQuery()
    ->execute()
    ->fetchField();
  $batch = array(
    'operations' => array(
      array(
        'user_revision_table_init_data_batch_process',
        array(
          $total_count,
        ),
      ),
    ),
    'finished' => 'user_revision_table_init_data_batch_finished',
    'title' => t('Updating revision table'),
    'init_message' => t('Initializing.'),
    'progress_message' => t('Completed @current of @total.'),
    'error_message' => t('An error has occurred.'),
    'file' => drupal_get_path('module', 'user_revision') . '/user_revision.batch.inc',
  );

  // Execute Batch API.
  batch_set($batch);
}

/**
 * Implements hook_uninstall().
 */
function user_revision_uninstall() {
  module_load_include('module', 'user_revision');
  $schema['users'] = array();
  user_revision_schema_alter($schema);
  foreach ($schema['users']['unique keys'] as $name => $spec) {
    db_drop_unique_key('users', $name);
  }
  foreach ($schema['users']['fields'] as $name => $spec) {
    db_drop_field('users', $name);
  }
}

/**
 * Change the weight of User Revision Module so all hooks are executed last.
 *
 * This way all changes made in hook_user_presave etc. won't be lost.
 */
function user_revision_update_7001() {
  db_update('system')
    ->fields(array(
    'weight' => 99,
  ))
    ->condition('name', 'user_revision', '=')
    ->execute();
}

/**
 * Create revisions with user roles.
 */
function user_revision_update_7002() {

  // Create the new revision role table.
  if (!db_table_exists('user_revision_roles')) {
    $user_role_revision_schema = drupal_get_schema_unprocessed('user_revision', 'user_revision_roles');
    db_create_table('user_revision_roles', $user_role_revision_schema);
  }

  // Fetch all user revision records.
  $data = db_select('user_revision', 'ur')
    ->fields('ur', array(
    'uid',
    'vid',
  ))
    ->orderBy('ur.uid')
    ->execute()
    ->fetchAll();
  foreach ($data as $row) {

    // Search each role for the user.
    $roles = db_select('users_roles', 'ur')
      ->fields('ur', array(
      'rid',
    ))
      ->condition('uid', $row->uid)
      ->execute()
      ->fetchAll();
    if (!empty($roles)) {
      foreach ($roles as $role) {

        // Add roles to user_revision_roles table.
        user_revision_add_role($row->uid, $row->vid, $role->rid);
      }
    }
  }
}

Functions

Namesort descending Description
user_revision_install Implements hook_install().
user_revision_schema Implements hook_schema().
user_revision_uninstall Implements hook_uninstall().
user_revision_update_7001 Change the weight of User Revision Module so all hooks are executed last.
user_revision_update_7002 Create revisions with user roles.