? .patch
? files
? mw.patch
? tmp.patch
? sites/all/modules
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.698
diff -u -F^f -r1.698 common.inc
--- includes/common.inc	8 Oct 2007 14:08:19 -0000	1.698
+++ includes/common.inc	11 Oct 2007 01:38:02 -0000
@@ -3140,6 +3140,7 @@ function drupal_write_record($table, &$o
     }
 
     // Build arrays for the fields, placeholders, and values in our query.
+    $fields = $placeholders = $values = array();
     if (isset($object->$field)) {
       $fields[] = $field;
       $placeholders[] = db_type_placeholder($info['type']);
@@ -3152,6 +3153,15 @@ function drupal_write_record($table, &$o
       }
     }
   }
+  
+  if (empty($fields)) {
+    // No changes requested. 
+    // If we began with an array, convert back so we don't surprise the caller.
+    if ($array) {
+      $object = (array)$object;
+    }
+    return;
+  }
 
   // Build the SQL.
   $query = '';
Index: modules/profile/profile.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/profile/profile.module,v
retrieving revision 1.220
diff -u -F^f -r1.220 profile.module
--- modules/profile/profile.module	5 Oct 2007 13:26:53 -0000	1.220
+++ modules/profile/profile.module	11 Oct 2007 01:38:02 -0000
@@ -344,7 +344,8 @@ function profile_field_form_validate($fo
     form_set_error('name', t('The specified form name contains one or more illegal characters. Spaces or any other special characters except dash (-) and underscore (_) are not allowed.'));
   }
 
-  if (in_array($form_state['values']['name'], user_fields())) {
+  $user_fields = drupal_get_schema('users');
+  if (in_array($form_state['values']['name'], array_keys($user_fields['fields']))) {
     form_set_error('name', t('The specified form name is reserved for use by Drupal.'));
   }
   // Validate the category:
Index: modules/user/user.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.module,v
retrieving revision 1.852
diff -u -F^f -r1.852 user.module
--- modules/user/user.module	7 Oct 2007 19:27:40 -0000	1.852
+++ modules/user/user.module	11 Oct 2007 01:38:03 -0000
@@ -197,38 +197,37 @@ function user_load($array = array()) {
  *   (optional) The category for storing profile information in.
  */
 function user_save($account, $array = array(), $category = 'account') {
-  // Dynamically compose a SQL query:
-  $user_fields = user_fields();
+  $fields = drupal_get_schema('users');
+  $user_fields = array_keys($fields['fields']);
+
+  if (!empty($array['pass'])) {
+    $array['pass'] = md5($array['pass']);
+  }
+  else {
+    // Avoid overwriting an existing password with a blank password.
+    unset($array['pass']);
+  }
+
   if (is_object($account) && $account->uid) {
     user_module_invoke('update', $array, $account, $category);
-    $query = '';
     $data = unserialize(db_result(db_query('SELECT data FROM {users} WHERE uid = %d', $account->uid)));
     foreach ($array as $key => $value) {
-      if ($key == 'pass' && !empty($value)) {
-        $query .= "$key = '%s', ";
-        $v[] = md5($value);
-      }
-      else if ((substr($key, 0, 4) !== 'auth') && ($key != 'pass')) {
-        if (in_array($key, $user_fields)) {
-          // Save standard fields
-          $query .= "$key = '%s', ";
-          $v[] = $value;
+      // Fields that are not in the users table and are not used by roles or
+      // for ext. auth are saved into users.data.
+      if ($key != 'roles' && substr($key, 0, 4) !== 'auth' && !in_array($key, $user_fields)) {
+        if ($value === NULL) {
+          unset($data[$key]);
         }
-        else if ($key != 'roles') {
-          // Roles is a special case: it used below.
-          if ($value === NULL) {
-            unset($data[$key]);
-          }
-          else {
-            $data[$key] = $value;
-          }
+        else {
+          $data[$key] = $value;
         }
       }
     }
-    $query .= "data = '%s' ";
-    $v[] = serialize($data);
-
-    db_query("UPDATE {users} SET $query WHERE uid = %d", array_merge($v, array($account->uid)));
+    if (!empty($data)) {
+      $array['data'] = $data;
+    }
+    $array['uid'] = $account->uid;
+    drupal_write_record('users', $array, 'uid');
 
     // Reload user roles if provided
     if (isset($array['roles']) && is_array($array['roles'])) {
@@ -270,48 +269,25 @@ function user_save($account, $array = ar
       $array['created'] = time();
     }
 
-    // Note, we wait with saving the data column to prevent module-handled
-    // fields from being saved there. We cannot invoke hook_user('insert') here
-    // because we don't have a fully initialized user object yet.
-    foreach ($array as $key => $value) {
-      switch ($key) {
-        case 'pass':
-          $fields[] = $key;
-          $values[] = md5($value);
-          $s[] = "'%s'";
-          break;
-        case 'mode':       case 'sort':     case 'timezone':
-        case 'threshold':  case 'created':  case 'access':
-        case 'login':      case 'status':
-          $fields[] = $key;
-          $values[] = $value;
-          $s[] = "%d";
-          break;
-        default:
-          if (substr($key, 0, 4) !== 'auth' && in_array($key, $user_fields)) {
-            $fields[] = $key;
-            $values[] = $value;
-            $s[] = "'%s'";
-          }
-          break;
-      }
-    }
-    db_query('INSERT INTO {users} ('. implode(', ', $fields) .') VALUES ('. implode(', ', $s) .')', $values);
-    $array['uid'] = db_last_insert_id('users', 'uid');
+    drupal_write_record('users', $array);
 
     // Build the initial user object.
     $user = user_load(array('uid' => $array['uid']));
 
     user_module_invoke('insert', $array, $user, $category);
 
-    // Build and save the serialized data field now
+    // Note, we wait with saving the data column to prevent module-handled
+    // fields from being saved there.
     $data = array();
     foreach ($array as $key => $value) {
       if ((substr($key, 0, 4) !== 'auth') && ($key != 'roles') && (!in_array($key, $user_fields)) && ($value !== NULL)) {
         $data[$key] = $value;
       }
     }
-    db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $user->uid);
+    if (!empty($data)) {
+      $data_array = array('uid' => $user->uid, 'data' => $data);
+      drupal_write_record('users', $data_array, 'uid');
+    }
 
     // Save user roles (delete just to be safe).
     if (isset($array['roles']) && is_array($array['roles'])) {
@@ -476,23 +452,6 @@ function user_is_blocked($name) {
   return $deny;
 }
 
-function user_fields() {
-  static $fields;
-
-  if (!$fields) {
-    $result = db_query('SELECT * FROM {users} WHERE uid = 1');
-    if ($field = db_fetch_array($result)) {
-      $fields = array_keys($field);
-    }
-    else {
-      // Make sure we return the default fields at least
-      $fields = array('uid', 'name', 'pass', 'mail', 'picture', 'mode', 'sort', 'threshold', 'theme', 'signature', 'created', 'access', 'login', 'status', 'timezone', 'language', 'init', 'data');
-    }
-  }
-
-  return $fields;
-}
-
 /**
  * Implementation of hook_perm().
  */
@@ -2093,7 +2052,7 @@ function user_register_submit($form, &$f
     return;
   }
   //the unset below is needed to prevent these form values from being saved as user data
-  unset($form_state['values']['form_token'], $form_state['values']['submit'], $form_state['values']['op'], $form_state['values']['notify'], $form_state['values']['form_id'], $form_state['values']['affiliates'], $form_state['values']['destination']);
+  unset($form_state['values']['form_token'], $form_state['values']['submit'], $form_state['values']['op'], $form_state['values']['notify'], $form_state['values']['form_id'], $form_state['values']['affiliates'], $form_state['values']['destination'], $form_state['values']['form_build_id']);
 
   $merge_data = array('pass' => $pass, 'init' => $mail, 'roles' => $roles);
   if (!$admin) {
Index: modules/user/user.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.pages.inc,v
retrieving revision 1.1
diff -u -F^f -r1.1 user.pages.inc
--- modules/user/user.pages.inc	10 Sep 2007 13:14:38 -0000	1.1
+++ modules/user/user.pages.inc	11 Oct 2007 01:38:03 -0000
@@ -272,7 +272,7 @@ function user_profile_form_validate($for
 function user_profile_form_submit($form, &$form_state) {
   $account = $form_state['values']['_account'];
   $category = $form_state['values']['_category'];
-  unset($form_state['values']['_account'], $form_state['values']['op'], $form_state['values']['submit'], $form_state['values']['delete'], $form_state['values']['form_token'], $form_state['values']['form_id'], $form_state['values']['_category']);
+  unset($form_state['values']['_account'], $form_state['values']['op'], $form_state['values']['submit'], $form_state['values']['delete'], $form_state['values']['form_token'], $form_state['values']['form_id'], $form_state['values']['_category'], $form_state['values']['form_build_id']);
   user_module_invoke('submit', $form_state['values'], $account, $category);
   user_save($account, $form_state['values'], $category);
 
@@ -336,7 +336,7 @@ function user_edit_validate($form, &$for
 function user_edit_submit($form, &$form_state) {
   $account = $form_state['values']['_account'];
   $category = $form_state['values']['_category'];
-  unset($form_state['values']['_account'], $form_state['values']['op'], $form_state['values']['submit'], $form_state['values']['delete'], $form_state['values']['form_token'], $form_state['values']['form_id'], $form_state['values']['_category']);
+  unset($form_state['values']['_account'], $form_state['values']['op'], $form_state['values']['submit'], $form_state['values']['delete'], $form_state['values']['form_token'], $form_state['values']['form_id'], $form_state['values']['_category'], $form_state['values']['form_build_id']);
   user_module_invoke('submit', $form_state['values'], $account, $category);
   user_save($account, $form_state['values'], $category);
 
