diff options
author | Dries Buytaert <dries@buytaert.net> | 2005-05-07 11:39:54 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2005-05-07 11:39:54 +0000 |
commit | 39d0fe9a3882f26ff1abe2bca2c28f94b0a8cec0 (patch) | |
tree | b2186e04ba329254abd00b8a866d16e47b9e64b3 | |
parent | 898e2b668879e584a13202e38a2366a0de2aabb5 (diff) | |
download | brdo-39d0fe9a3882f26ff1abe2bca2c28f94b0a8cec0.tar.gz brdo-39d0fe9a3882f26ff1abe2bca2c28f94b0a8cec0.tar.bz2 |
- User module improvements: added an 'access' column to the users-table to
keep track of the user's last access. In turn, this allowed me to:
1. Optimize the "Who's online" block. On drupal.org, the "Who's online"
block requires 32 SQL queries. With this patch, only 2 queries are
left (eliminated 30 SQL queries), and one of the two remaining queries
became appr. 20 times faster.
2. Correct the "Last access" column in the user administration overview
table. The presented data was not accurate, which led to the column
being removed. You can now sort users by 'last access'.
-rw-r--r-- | database/database.mysql | 4 | ||||
-rw-r--r-- | database/database.pgsql | 2 | ||||
-rw-r--r-- | database/updates.inc | 11 | ||||
-rw-r--r-- | includes/session.inc | 1 | ||||
-rw-r--r-- | modules/user.module | 18 | ||||
-rw-r--r-- | modules/user/user.module | 18 |
6 files changed, 32 insertions, 22 deletions
diff --git a/database/database.mysql b/database/database.mysql index 94e449f64..79d0b9c2c 100644 --- a/database/database.mysql +++ b/database/database.mysql @@ -692,7 +692,7 @@ CREATE TABLE users ( theme varchar(255) NOT NULL default '', signature varchar(255) NOT NULL default '', created int(11) NOT NULL default '0', - changed int(11) NOT NULL default '0', + access int(11) NOT NULL default '0', login int(11) NOT NULL default '0', status tinyint(4) NOT NULL default '0', timezone varchar(8) default NULL, @@ -702,7 +702,7 @@ CREATE TABLE users ( data longtext, PRIMARY KEY (uid), UNIQUE KEY name (name), - KEY changed (changed) + KEY access (access) ) TYPE=MyISAM; -- diff --git a/database/database.pgsql b/database/database.pgsql index 67f7ed631..f4d4b831a 100644 --- a/database/database.pgsql +++ b/database/database.pgsql @@ -687,7 +687,7 @@ CREATE TABLE users ( theme varchar(255) NOT NULL default '', signature varchar(255) NOT NULL default '', created integer NOT NULL default '0', - changed integer NOT NULL default '0', + access integer NOT NULL default '0', login integer NOT NULL default '0', status smallint NOT NULL default '0', timezone varchar(8) default NULL, diff --git a/database/updates.inc b/database/updates.inc index 084036657..2ee4acf70 100644 --- a/database/updates.inc +++ b/database/updates.inc @@ -111,7 +111,8 @@ $sql_updates = array( "2005-04-14" => "update_132", "2005-04-24" => "update_133", "2005-04-30" => "update_134", - "2005-05-06" => "update_135" + "2005-05-06" => "update_135", + "2005-05-08" => "update_136" ); function update_32() { @@ -2447,6 +2448,14 @@ function update_135() { return array(); } +function update_136() { + $ret = array(); + $ret[] = update_sql("ALTER TABLE {users} CHANGE COLUMN changed access int(11) NOT NULL default '0'"); + $ret[] = update_sql('UPDATE {users} SET access = login WHERE login > created'); + $ret[] = update_sql('UPDATE {users} SET access = created WHERE access = 0'); + return $ret; +} + function update_sql($sql) { $edit = $_POST["edit"]; $result = db_query($sql); diff --git a/includes/session.inc b/includes/session.inc index 517651704..126c56f02 100644 --- a/includes/session.inc +++ b/includes/session.inc @@ -46,6 +46,7 @@ function sess_write($key, $value) { global $user; db_query("UPDATE {sessions} SET uid = %d, cache = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, $user->cache, $_SERVER["REMOTE_ADDR"], $value, time(), $key); + db_query("UPDATE {users} SET access = %d WHERE uid = %d", time(), $user->uid); return ''; } diff --git a/modules/user.module b/modules/user.module index b965cf51a..0c3a13bb0 100644 --- a/modules/user.module +++ b/modules/user.module @@ -126,7 +126,7 @@ function user_save($account, $array = array(), $category = 'account') { $query .= "data = '%s', "; $v[] = serialize($data); - db_query("UPDATE {users} SET $query changed = %d WHERE uid = %d", array_merge($v, array(time(), $account->uid))); + db_query("UPDATE {users} SET $query WHERE uid = %d", array_merge($v, array($account->uid))); // Reload user roles if provided if (is_array($array['roles'])) { @@ -147,7 +147,6 @@ function user_save($account, $array = array(), $category = 'account') { } else { $array['created'] = time(); - $array['changed'] = time(); $array['uid'] = db_next_id('{users}_uid'); // Note, we wait with saving the data column to prevent module-handled @@ -392,7 +391,7 @@ function user_fields() { } else { // Make sure we return the default fields at least - $fields = array('uid', 'name', 'pass', 'mail', 'picture', 'mode', 'sort', 'threshold', 'theme', 'signature', 'created', 'changed', 'login', 'status', 'timezone', 'language', 'init', 'data'); + $fields = array('uid', 'name', 'pass', 'mail', 'picture', 'mode', 'sort', 'threshold', 'theme', 'signature', 'created', 'access', 'login', 'status', 'timezone', 'language', 'init', 'data'); } } @@ -549,7 +548,7 @@ function user_block($op = 'list', $delta = 0, $edit = array()) { // Perform database queries to gather online user lists. $guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period)); - $users = db_query('SELECT DISTINCT(uid), MAX(timestamp) AS max_timestamp FROM {sessions} WHERE timestamp >= %d AND uid != 0 GROUP BY uid ORDER BY max_timestamp DESC', time() - $time_period ); + $users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period); $total_users = db_num_rows($users); // Format the output with proper grammar. @@ -565,8 +564,8 @@ function user_block($op = 'list', $delta = 0, $edit = array()) { if ($max_users) { $items = array(); - while ($max_users-- && $uid = db_fetch_object($users)) { - $items[] = format_name(user_load(array('uid' => $uid->uid))); + while ($max_users-- && $account = db_fetch_object($users)) { + $items[] = format_name($account); } if ($items) { @@ -1667,9 +1666,10 @@ function user_admin_account() { array('data' => t('Status'), 'field' => 'u.status'), array('data' => t('Roles')), array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'), + array('data' => t('Last access'), 'field' => 'u.access'), t('Operations') ); - $sql = 'SELECT u.uid, u.name, u.status, u.created, u.login FROM {users} u WHERE uid != 0'; + $sql = 'SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u WHERE uid != 0'; $sql .= tablesort_sql($header); $result = pager_query($sql, 50); @@ -1683,13 +1683,13 @@ function user_admin_account() { $roles[] = $role->name; } - $rows[] = array(format_name($account), $status[$account->status], implode(', ', $roles), format_interval(time() - $account->created), l(t('edit'), "user/$account->uid/edit", array(), $destination)); + $rows[] = array(format_name($account), $status[$account->status], implode(', ', $roles), format_interval(time() - $account->created), t('%time ago', array('%time' => format_interval(time() - $account->access))), l(t('edit'), "user/$account->uid/edit", array(), $destination)); } $pager = theme('pager', NULL, 50, 0, tablesort_pager()); if (!empty($pager)) { - $rows[] = array(array('data' => $pager, 'colspan' => '5')); + $rows[] = array(array('data' => $pager, 'colspan' => '6')); } return theme('table', $header, $rows); } diff --git a/modules/user/user.module b/modules/user/user.module index b965cf51a..0c3a13bb0 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -126,7 +126,7 @@ function user_save($account, $array = array(), $category = 'account') { $query .= "data = '%s', "; $v[] = serialize($data); - db_query("UPDATE {users} SET $query changed = %d WHERE uid = %d", array_merge($v, array(time(), $account->uid))); + db_query("UPDATE {users} SET $query WHERE uid = %d", array_merge($v, array($account->uid))); // Reload user roles if provided if (is_array($array['roles'])) { @@ -147,7 +147,6 @@ function user_save($account, $array = array(), $category = 'account') { } else { $array['created'] = time(); - $array['changed'] = time(); $array['uid'] = db_next_id('{users}_uid'); // Note, we wait with saving the data column to prevent module-handled @@ -392,7 +391,7 @@ function user_fields() { } else { // Make sure we return the default fields at least - $fields = array('uid', 'name', 'pass', 'mail', 'picture', 'mode', 'sort', 'threshold', 'theme', 'signature', 'created', 'changed', 'login', 'status', 'timezone', 'language', 'init', 'data'); + $fields = array('uid', 'name', 'pass', 'mail', 'picture', 'mode', 'sort', 'threshold', 'theme', 'signature', 'created', 'access', 'login', 'status', 'timezone', 'language', 'init', 'data'); } } @@ -549,7 +548,7 @@ function user_block($op = 'list', $delta = 0, $edit = array()) { // Perform database queries to gather online user lists. $guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period)); - $users = db_query('SELECT DISTINCT(uid), MAX(timestamp) AS max_timestamp FROM {sessions} WHERE timestamp >= %d AND uid != 0 GROUP BY uid ORDER BY max_timestamp DESC', time() - $time_period ); + $users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period); $total_users = db_num_rows($users); // Format the output with proper grammar. @@ -565,8 +564,8 @@ function user_block($op = 'list', $delta = 0, $edit = array()) { if ($max_users) { $items = array(); - while ($max_users-- && $uid = db_fetch_object($users)) { - $items[] = format_name(user_load(array('uid' => $uid->uid))); + while ($max_users-- && $account = db_fetch_object($users)) { + $items[] = format_name($account); } if ($items) { @@ -1667,9 +1666,10 @@ function user_admin_account() { array('data' => t('Status'), 'field' => 'u.status'), array('data' => t('Roles')), array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'), + array('data' => t('Last access'), 'field' => 'u.access'), t('Operations') ); - $sql = 'SELECT u.uid, u.name, u.status, u.created, u.login FROM {users} u WHERE uid != 0'; + $sql = 'SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u WHERE uid != 0'; $sql .= tablesort_sql($header); $result = pager_query($sql, 50); @@ -1683,13 +1683,13 @@ function user_admin_account() { $roles[] = $role->name; } - $rows[] = array(format_name($account), $status[$account->status], implode(', ', $roles), format_interval(time() - $account->created), l(t('edit'), "user/$account->uid/edit", array(), $destination)); + $rows[] = array(format_name($account), $status[$account->status], implode(', ', $roles), format_interval(time() - $account->created), t('%time ago', array('%time' => format_interval(time() - $account->access))), l(t('edit'), "user/$account->uid/edit", array(), $destination)); } $pager = theme('pager', NULL, 50, 0, tablesort_pager()); if (!empty($pager)) { - $rows[] = array(array('data' => $pager, 'colspan' => '5')); + $rows[] = array(array('data' => $pager, 'colspan' => '6')); } return theme('table', $header, $rows); } |