You are here

user_stats.install in User Stats 6

Same filename and directory in other branches
  1. 5 user_stats.install
  2. 7 user_stats.install

Install hooks for the User Stats module.

File

user_stats.install
View source
<?php

/**
 * @file
 * Install hooks for the User Stats module.
 */

/**
 * Implementation of hook_schema().
 */
function user_stats_schema() {
  $schema['user_stats_values'] = array(
    'description' => 'User Stats data.',
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The name of the statistic.',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {users}.uid of the statistic user.',
      ),
      'value' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The value of the statistic.',
      ),
    ),
    'primary key' => array(
      'name',
      'uid',
    ),
  );
  $schema['user_stats_ips'] = array(
    'description' => 'IP address storage, links timestamps and uids to IP',
    'fields' => array(
      'iid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary key: IP address unique ID.',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {users}.uid of the user.',
      ),
      'ip_address' => array(
        'type' => 'varchar',
        'length' => 40,
        'not null' => TRUE,
        'default' => '',
        'description' => "The user's IP address.",
      ),
      'first_seen_timestamp' => array(
        'description' => 'The Unix timestamp when the IP address was first used by this user.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'iid',
    ),
    'indexes' => array(
      'uid' => array(
        'uid',
      ),
      'first_seen_timestamp' => array(
        'first_seen_timestamp',
      ),
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_install().
 */
function user_stats_install() {
  drupal_install_schema('user_stats');
}

/**
 * Implementation of hook_uninstall().
 */
function user_stats_uninstall() {
  variable_del('user_stats_rebuild_stats');
  variable_del('user_stats_last_cron_check');
  variable_del('user_stats_included_content_types');
  variable_del('user_stats_reset_login_count');
  variable_del('user_stats_reset_post_count');
  variable_del('user_stats_user_per_cron');
  variable_del('user_stats_count_posts');
  variable_del('user_stats_count_comments');
  variable_del('user_stats_count_logins');
  drupal_uninstall_schema('user_stats');
}

/**
 * Implementation of hook_update_N().
 */
function user_stats_update_6100() {
  $ret = array();

  // Making variable names consistent: 'postcount' becomes 'post_count'.
  $post_count_field = variable_get('user_stats_postcount_profile_field', 'user_post_count');
  variable_set('user_stats_post_count_profile_field', $post_count_field);
  variable_del('user_stats_postcount_profile_field');
  return $ret;
}

/**
 * Implementation of hook_update_N().
 */
function user_stats_update_6101() {
  variable_del('user_stats_post_count_profile_field');
  variable_del('user_stats_postcount_profile_field');

  // Need to reset statistics.
  variable_set('user_stats_rebuild_stats', TRUE);
  $ret = array();

  // Install new table schema.
  $schema['user_stats_values'] = array(
    'description' => 'User Stats data.',
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The name of the statistic.',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {users}.uid of the statistic user.',
      ),
      'value' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The value of the statistic.',
      ),
    ),
    'primary key' => array(
      'name',
      'uid',
    ),
  );
  $schema['user_stats_ips'] = array(
    'description' => 'IP address storage, links timestamps and uids to IP',
    'fields' => array(
      'iid' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary key: IP address unique ID.',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {users}.uid of the user.',
      ),
      'ip_address' => array(
        'type' => 'varchar',
        'length' => 15,
        'not null' => TRUE,
        'default' => '',
        'description' => "The user's IP address.",
      ),
      'first_seen_timestamp' => array(
        'description' => 'The Unix timestamp when the IP address was first used by this user.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'iid',
    ),
    'indexes' => array(
      'uid' => array(
        'uid',
      ),
      'first_seen_timestamp' => array(
        'first_seen_timestamp',
      ),
    ),
  );
  db_create_table($ret, 'user_stats_values', $schema['user_stats_values']);
  db_create_table($ret, 'user_stats_ips', $schema['user_stats_ips']);
  $post_count_profile_field = variable_get('user_stats_post_count_profile_field', 'user_post_count');

  // Migrate old data, only the login count is necessary, post counts are reset.
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("INSERT INTO {user_stats_values} (name, uid, value)\n        SELECT 'login_count', pv.uid, CAST(pv.value AS SIGNED)\n        FROM {profile_fields} pf INNER JOIN {profile_values} pv ON pf.fid = pv.fid\n        WHERE pf.name = 'user_login_count'");
      break;
    case 'pgsql':
      $ret[] = update_sql("INSERT INTO {user_stats_values} (name, uid, value)\n        SELECT 'login_count', pv.uid, CAST(pv.value AS INT)\n        FROM {profile_fields} pf INNER JOIN {profile_values} pv ON pf.fid = pv.fid\n        WHERE pf.name = 'user_login_count'");
      break;
  }

  // IP Addresses to new dedicated table, unfortunately we don't have timestamps.
  $ret[] = update_sql("INSERT INTO {user_stats_ips} (uid, ip_address)\n    SELECT pv.uid, pv.value\n    FROM {profile_fields} pf INNER JOIN {profile_values} pv ON pf.fid = pv.fid\n    WHERE pf.name = 'user_ip_address'");

  // Delete old data.
  $query = db_query("SELECT fid FROM {profile_fields}\n    WHERE name IN ('%s', 'user_ip_address', 'user_login_count')", $post_count_profile_field);
  while ($fid = db_fetch_object($query)) {
    $ret[] = update_sql("DELETE FROM {profile_values} WHERE fid = " . (int) $fid->fid);
  }
  $ret[] = update_sql("DELETE FROM {profile_fields}\n    WHERE name IN ('" . db_escape_string($post_count_profile_field) . "', 'user_ip_address', 'user_login_count')");
  return $ret;
}

/**
 * Implementation of hook_update_N().
 * Expands the width of the field table to 40.
 */
function user_stats_update_6102(&$sandbox) {
  $ip_address = array(
    'type' => 'varchar',
    'length' => 40,
    'not null' => TRUE,
    'default' => '',
    'description' => "The user's IP address.",
  );
  $ret = array();
  db_change_field($ret, 'user_stats_ips', 'ip_address', 'ip_address', $ip_address);
  return $ret;
}

Functions

Namesort descending Description
user_stats_install Implementation of hook_install().
user_stats_schema Implementation of hook_schema().
user_stats_uninstall Implementation of hook_uninstall().
user_stats_update_6100 Implementation of hook_update_N().
user_stats_update_6101 Implementation of hook_update_N().
user_stats_update_6102 Implementation of hook_update_N(). Expands the width of the field table to 40.