You are here

function user_revision_install in User Revision 7

Same name and namespace in other branches
  1. 8 user_revision.install \user_revision_install()
  2. 7.2 user_revision.install \user_revision_install()

Implements hook_install().

File

./user_revision.install, line 222
Install, update and uninstall functions for the user_revision module.

Code

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);
}