Index: realname.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/realname/realname.install,v
retrieving revision 1.4.4.10
diff -u -r1.4.4.10 realname.install
--- realname.install	27 Oct 2010 06:52:01 -0000	1.4.4.10
+++ realname.install	31 Dec 2010 19:34:19 -0000
@@ -53,7 +53,14 @@
  * Implementation of hook_enable().
  */
 function realname_enable() {
-  drupal_set_message(t('The RealName module has been enabled. You may wish to proceed to <a href="!url">the settings page</a>.', array('!url' => url('admin/user/realname'))));  
+  // Clean up orphaned entries from users removed while module disabled
+  db_query("DELETE rn.* FROM {realname} rn WHERE NOT EXISTS (SELECT * FROM {users} u WHERE u.uid=rn.uid)");
+
+  // Create entries in the realname table for any users added while disabled.
+  // Values will be generated and stored on first use
+  db_query('INSERT INTO {realname}(uid) SELECT uid FROM {user}');
+
+  drupal_set_message(t('The RealName module has been enabled. You may wish to proceed to <a href="!url">the settings page</a>.', array('!url' => url('admin/user/realname'))));
 }
 
 /**
@@ -168,3 +175,13 @@
 
   return $ret;
 }
+/**
+ * Implementation of hook_update_N().
+ *
+ * Make sure every user has an entry in the {realname} table.
+ */
+function realname_update_6103() {
+  // Create entries in the realname table for all users
+  // Values will be generated and stored on first use
+  db_query('INSERT INTO {realname}(uid) SELECT uid FROM {user}');
+}
Index: realname.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/realname/realname.module,v
retrieving revision 1.4.4.71
diff -u -r1.4.4.71 realname.module
--- realname.module	10 Dec 2010 16:13:45 -0000	1.4.4.71
+++ realname.module	31 Dec 2010 19:34:20 -0000
@@ -464,10 +464,8 @@
 
     case 'insert':
     case 'after_update':
-      $account->realname = _realname_make_name($account);
-      // If we delete it first, then drupal_write_record is always an insert.
-      db_query("DELETE FROM {realname} WHERE uid = %d", $account->uid);
-      drupal_write_record('realname', $account);
+      // force regenerating the realname, and use the new value
+      $account->realname = realname_make_name($account, TRUE);
   }
 }
 
@@ -482,13 +480,13 @@
   }
 
   // NickSI: Yes, we will have another switch later but I would like to separate table update with other processing
-  // Just delete the old nickname from the table. New one is automatically generated on next request.
+  // Update the realname for the author of a content_profile node when it is edited.
   if ( variable_get('realname_profile_module', NULL) == 'content_profile' && module_exists('content_profile') && is_content_profile($node->type)) {
     switch ($op) {
       case 'update':
       case 'insert':
       case 'delete':
-        db_query('DELETE FROM {realname} WHERE uid = %d', $node->uid);
+        realname_make_name(user_load($node->uid), TRUE);
     }
   }
 
@@ -769,11 +767,12 @@
  *
  * @param
  *   $account - the user object to update.
+ *   $reset - Bypass the caches and regenerate the realname
  *
  * @return
- *   The retreived "real name" string.
+ *   The retrieved "real name" string.
  */
-function realname_make_name(&$account) {
+function realname_make_name(&$account, $reset = FALSE) {
   $users = &drupal_static('users',array());
   $links = &drupal_static('links',array());
 
@@ -782,22 +781,31 @@
     return isset($account->name) ? $account->name : variable_get('anonymous', t('Anonymous'));
   }
 
-  // Check static cache first.
-  if (isset($users[$account->uid])) {
-    $account->homepage = isset($links[$account->uid]) ? $links[$account->uid] : NULL;
-    return $users[$account->uid];
+  if (!$reset){
+    // Check the static cache.
+    if (isset($users[$account->uid])) {
+      $account->homepage = isset($links[$account->uid]) ? $links[$account->uid] : NULL;
+      return $users[$account->uid];
+    }
+
+    // Check the DB cache.
+    $result = db_result(db_query("SELECT realname FROM {realname} WHERE uid=%d", $account->uid));
+    if (!empty($result)){
+      $users[$account->uid] = $result;
+      return $result;
+    }
   }
 
   $result = _realname_make_name($account);
+  // If empty, set to username.
   if (empty($result)) {
-    return $account->name;
-  }
-  if (!db_result(db_query("SELECT uid FROM {realname} WHERE uid = %d", $account->uid))) {
-    db_query("INSERT INTO {realname} (uid, realname) VALUES (%d, '%s')", $account->uid, $result);
-  }
-  else {
-    db_query("UPDATE {realname} SET realname = '%s' WHERE uid = %d", $result, $account->uid);
+    $result = $account->name;
   }
+
+  // update static and DB cache
+  $users[$account->uid] = $result;
+  db_query("UPDATE {realname} SET realname = '%s' WHERE uid = %d", $result, $account->uid);
+
   return $result;
 }
 
