You are here

function user_stats_update_6101 in User Stats 6

Implementation of hook_update_N().

File

./user_stats.install, line 116
Install hooks for the User Stats module.

Code

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