You are here

user_revision.install in User Revision 7

Same filename and directory in other branches
  1. 8 user_revision.install
  2. 7.2 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_disable().
 */
function user_revision_disable() {
  module_load_include('install', 'user_revision');
  $schema['users'] = array();
  user_revision_schema_alter($schema);
  foreach ($schema['users']['unique keys'] as $name => $spec) {
    db_drop_unique_key('users', $name);
  }
}

/**
 * Implements hook_enable().
 */
function user_revision_enable() {
  $users = db_select('users', 'u')
    ->fields('u', array(
    'uid',
    'vid',
    'name',
    'mail',
    'theme',
    'signature',
    'signature_format',
    'status',
    'timezone',
    'language',
    'picture',
    'data',
  ))
    ->condition('u.uid', 0, '!=')
    ->execute()
    ->fetchAll();
  foreach ($users as $user) {
    db_insert('user_revision')
      ->fields(array(
      'uid' => $user->uid,
      'log' => '',
      'timestamp' => REQUEST_TIME,
      'authorid' => $user->uid,
      'name' => $user->name,
      'mail' => $user->mail,
      'theme' => $user->theme,
      'signature' => $user->signature,
      'signature_format' => $user->signature_format,
      'status' => $user->status,
      'timezone' => $user->timezone,
      'language' => $user->language,
      'picture' => $user->picture,
      'data' => $user->data,
    ))
      ->execute();
  }
  $user_revisions = db_select('user_revision', 'ur')
    ->fields('ur', array(
    'uid',
    'vid',
  ))
    ->condition('ur.uid', 0, '!=')
    ->execute()
    ->fetchAll();

  // Update revision id of all non-anonymous users.
  foreach ($user_revisions as $user_revision) {
    db_update('users')
      ->condition('uid', $user_revision->uid)
      ->fields(array(
      'vid' => $user_revision->vid,
    ))
      ->execute();
  }
  $schema['users'] = array();
  user_revision_schema_alter($schema);
  foreach ($schema['users']['unique keys'] as $name => $spec) {
    db_add_unique_key('users', $name, $spec);
  }
}

/**
 * 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',
        ),
      ),
    ),
  );
  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);
  }
  db_update('users')
    ->expression('vid', 'uid')
    ->execute();

  // Handle anonymous user first.
  // Auto-increment id can be either 0 (MYSQL), 1 (PostgreSQL) or who knows
  // what else on different database engines.
  $anonymous_vid = db_insert('user_revision')
    ->fields(array(
    'uid' => 0,
    'log' => '',
    'timestamp' => 0,
    'authorid' => 0,
    'name' => '',
    'mail' => '',
    'theme' => '',
    'signature' => '',
    'status' => 0,
    'language' => '',
    'picture' => 0,
  ))
    ->execute();

  // Make sure data is correct for anonymous user.
  db_update('users')
    ->condition('uid', 0)
    ->fields(array(
    'vid' => $anonymous_vid,
  ))
    ->execute();

  // 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',
  );
  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();
}

Functions

Namesort descending Description
user_revision_disable Implements hook_disable().
user_revision_enable Implements hook_enable().
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.