#347988: remove user.data. From: <> --- modules/contact/contact.install | 97 +++++++++++++++++++++++++++++++++++++++ modules/profile/profile.module | 2 - modules/user/user.api.php | 3 - modules/user/user.install | 2 - modules/user/user.module | 45 +----------------- 5 files changed, 101 insertions(+), 48 deletions(-) diff --git modules/contact/contact.install modules/contact/contact.install index 63a533d..b792e24 100644 --- modules/contact/contact.install +++ modules/contact/contact.install @@ -7,15 +7,46 @@ */ /** + * Implement hook_install(). + */ +function contact_install() { + // Add an additional 'contact' column to the {user} table. + db_add_field(NULL, 'user', 'contact', array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'size' => 'tiny', + 'description' => "Whether the user's contact form is active (1) or not (0).", + )); +} + +/** * Implement hook_uninstall(). */ function contact_uninstall() { + // Remove the contact additional column. + db_drop_field($ret, 'user', 'contact'); + variable_del('contact_default_status'); variable_del('contact_form_information'); variable_del('contact_hourly_threshold'); } /** + * Implement hook_schema_alter(). + */ +function contact_schema_alter(&$schema) { + // Add field to existing schema. + $schema['users']['fields']['contact'] = array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'size' => 'tiny', + 'description' => "Whether the user's contact form is active (1) or not (0).", + ); +} + +/** * Implement hook_schema(). */ function contact_schema() { @@ -73,3 +104,69 @@ function contact_schema() { return $schema; } + +/** + * @defgroup user-updates-6.x-to-7.x User updates from 6.x to 7.x + * @{ + */ + +/** + * Add an additional 'contact' column to the {user} table. + */ +function contact_update_7000() { + db_add_field($ret, 'user', 'contact', array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'size' => 'tiny', + 'description' => "Whether the user's contact form is active (1) or not (0).", + )); + return $ret; +} + +/** + * Migrate data stored by the contact module from user.data. + */ +function contact_update_7001(&$sandbox) { + $ret = array('#finished' => 0); + + // Multi-part update. + if (!isset($sandbox['user_from'])) { + $sandbox['user_from'] = 0; + $sandbox['user_count'] = db_query("SELECT COUNT(uid) FROM {users}")->fetchField(); + } + else { + // Update this many per page load. + $count = 1000; + + // Prepare a multiple update query. + $query = db_update('user') + ->fields(array('contact' => 1)) + ->condition($matched_users = db_or()); + + $results = db_query_range("SELECT uid, data FROM {users} ORDER BY uid", $sandbox['user_from'], $count); + foreach ($results as $account) { + $data = unserialize($results->data); + + if (isset($data['contact']) && $data['contact']) { + $matched_users->condition('uid', $account->uid); + } + + $sandbox['user_from']++; + } + + // Execute the query. + $query->execute(); + + $ret['#finished'] = $sandbox['user_from'] / $sandbox['user_count']; + if ($sandbox['user_from'] == $sandbox['user_count']) { + $ret[] = array('success' => TRUE, 'query' => "Migrate data stored by the contact module."); + } + } + return $ret; +} + +/** + * @} End of "defgroup user-updates-6.x-to-7.x" + * The next series of updates should start at 8000. + */ diff --git modules/profile/profile.module modules/profile/profile.module index 6baedfd..99c6cf3 100644 --- modules/profile/profile.module +++ modules/profile/profile.module @@ -262,8 +262,6 @@ function profile_save_profile(&$edit, $account, $category, $register = FALSE) { )) ->fields(array('value' => $edit[$field->name])) ->execute(); - // Mark field as handled (prevents saving to user->data). - $edit[$field->name] = NULL; } } diff --git modules/user/user.api.php modules/user/user.api.php index a5e5668..26181be 100644 --- modules/user/user.api.php +++ modules/user/user.api.php @@ -289,9 +289,6 @@ function hook_user_submit(&$edit, $account, $category) { elseif (!empty($edit['picture_delete'])) { $edit['picture'] = NULL; } - // Remove these values so they don't end up serialized in the data field. - $edit['picture_upload'] = NULL; - $edit['picture_delete'] = NULL; if (isset($edit['roles'])) { $edit['roles'] = array_filter($edit['roles']); diff --git modules/user/user.install modules/user/user.install index 1936445..0e38d15 100644 --- modules/user/user.install +++ modules/user/user.install @@ -198,7 +198,7 @@ function user_schema() { 'not null' => FALSE, 'size' => 'big', 'serialize' => TRUE, - 'description' => 'A serialized array of name value pairs that are related to the user. Any form values posted during user edit are stored and are loaded into the $user object during user_load(). Use of this field is discouraged and it will likely disappear in a future version of Drupal.', + 'description' => 'Deprecated: A serialized array of name value pairs that are related to the user. Any form values posted during user edit are stored and are loaded into the $user object during user_load(). Use of this field is discouraged and it will likely disappear in a future version of Drupal.', ), ), 'indexes' => array( diff --git modules/user/user.module modules/user/user.module index b41f89b..d3e3b8c 100644 --- modules/user/user.module +++ modules/user/user.module @@ -200,7 +200,7 @@ class UserController extends DrupalDefaultEntityController { $picture_fids = array(); foreach ($queried_users as $key => $record) { $picture_fids[] = $record->picture; - $queried_users[$key] = drupal_unpack($record); + $queried_users[$key] = $record; $queried_users[$key]->roles = array(); if ($record->uid) { $queried_users[$record->uid]->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user'; @@ -296,9 +296,7 @@ function user_load_by_name($name) { * TRUE or omit the $account->uid field. * @param $edit * An array of fields and values to save. For example array('name' - * => 'My name'). Keys that do not belong to columns in the user-related - * tables are added to the a serialized array in the 'data' column - * and will be loaded in the $user->data array by user_load(). + * => 'My name'). * Setting a field to NULL deletes it from the data column, if you are * modifying an existing user account. * @param $category @@ -325,14 +323,8 @@ function user_save($account, $edit = array(), $category = 'account') { unset($edit['pass']); } - // Get the fields form so we can recognize the fields in the $edit - // form that should not go into the serialized data array. - $field_form = array(); - $field_form_state = array(); - $edit = (object) $edit; - field_attach_form('user', $edit, $field_form, $field_form_state); - // Presave fields. + $edit = (object) $edit; field_attach_presave('user', $edit); $edit = (array) $edit; @@ -342,25 +334,11 @@ function user_save($account, $edit = array(), $category = 'account') { } if (is_object($account) && !$account->is_new) { user_module_invoke('update', $edit, $account, $category); - $data = unserialize(db_query('SELECT data FROM {users} WHERE uid = :uid', array(':uid' => $account->uid))->fetchField()); // Consider users edited by an administrator as logged in, if they haven't // already, so anonymous users can view the profile (if allowed). if (empty($edit['access']) && empty($account->access) && user_access('administer users')) { $edit['access'] = REQUEST_TIME; } - foreach ($edit as $key => $value) { - // Form fields that don't pertain to the users, user_roles, or - // Field API are automatically serialized into the users.data - // column. - if (!in_array($key, array('roles', 'is_new')) && empty($user_fields[$key]) && empty($field_form[$key])) { - if ($value === NULL) { - unset($data[$key]); - } - else { - $data[$key] = $value; - } - } - } // Process picture uploads. if (!empty($edit['picture']->fid)) { @@ -383,7 +361,6 @@ function user_save($account, $edit = array(), $category = 'account') { } $edit['picture'] = empty($edit['picture']->fid) ? 0 : $edit['picture']->fid; - $edit['data'] = $data; // Do not allow 'uid' to be changed. $edit['uid'] = $account->uid; // Save changes to the user table. @@ -481,22 +458,6 @@ function user_save($account, $edit = array(), $category = 'account') { user_module_invoke('insert', $edit, $user, $category); - // Note, we wait with saving the data column to prevent module-handled - // fields from being saved there. - $data = array(); - foreach ($edit as $key => $value) { - // Form fields that don't pertain to the users, user_roles, or - // Field API are automatically serialized into the user.data - // column. - if ((!in_array($key, array('roles', 'is_new'))) && (empty($user_fields[$key]) && empty($field_form[$key])) && ($value !== NULL)) { - $data[$key] = $value; - } - } - 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($edit['roles']) && is_array($edit['roles'])) { db_delete('users_roles')