function user_migrate_import_user in Migrate 6
Implementation of hook_migrate_import_user().
File
- modules/
user.migrate.inc, line 47 - Implementation of user destination handling
Code
function user_migrate_import_user($tblinfo, $row) {
$sourcekey = $tblinfo->sourcekey;
// Begin building user object...
$newuser = array();
// Handle an update operation
if ($row->destid) {
$newuser['uid'] = $row->destid;
}
else {
if (isset($tblinfo->fields['uid'])) {
$uidname = $tblinfo->fields['uid']['srcfield'];
$newuser = (array) user_load($row->{$uidname});
// The existing md5 password will have another md5 applied to it if
// we're not careful - save it here and reset it later if no one else
// has set a password
$original_password = $newuser['pass'];
}
}
// Data which might be useful for user hooks.
$newuser['migrate_tblinfo'] = $tblinfo;
foreach ($tblinfo->fields as $destfield => $values) {
// Ignore CCK fields - we assume another module such as content_profile will deal with them
if (!preg_match('/^field_/', $destfield)) {
if ($values['srcfield'] && isset($row->{$values}['srcfield'])) {
$newvalue = $row->{$values}['srcfield'];
}
else {
$newvalue = $values['default_value'];
}
// Make sense of timestamp fields
if ($destfield == 'created' || $destfield == 'access' || $destfield == 'login') {
$newvalue = _migrate_valid_date($newvalue);
}
elseif ($destfield == 'roles') {
static $user_roles;
if (!isset($user_roles)) {
$user_roles = array_flip(user_roles(TRUE));
}
// Break out roles
$roles = explode(',', $newvalue);
$newvalue = array();
// Accept either role IDs or role names (which need to be converted to role IDs)
// Note that a role name containing a comma will be broken up - in this case, you
// must use the role ID instead of the name
foreach ($roles as $role) {
$rid = $user_roles[trim($role)];
if ($rid) {
$newvalue[$rid] = $rid;
}
else {
$newvalue[$role] = $role;
}
}
}
$newuser[$destfield] = $newvalue;
}
}
// Prepare the user for import.
$errors = migrate_destination_invoke_all('prepare_user', $newuser, $tblinfo, $row);
$success = TRUE;
foreach ($errors as $error) {
if ($error['level'] != MIGRATE_MESSAGE_INFORMATIONAL) {
$success = FALSE;
break;
}
}
if ($success) {
// Generate a random username if none was provided (presumably we
// are migrating from a system using email addresses as account names)
if (!isset($newuser['name']) || !$newuser['name']) {
// There is a tiny risk that the generated name will not be unique
$newuser['name'] = user_password();
}
// This field appears to be obsolete, but little cost to setting it...
if (!isset($newuser['init'])) {
$newuser['init'] = $newuser['mail'];
}
// Default to enabled
if (!isset($newuser['status'])) {
$newuser['status'] = 1;
}
// If we loaded an existing (MD5) password and no one overwrote it, we'll
// need to set it directly in the DB
if (isset($original_password) && $original_password == $newuser['pass']) {
$reset_password = TRUE;
}
else {
$reset_password = FALSE;
}
timer_start('user_save');
$account = user_save((object) $newuser, $newuser);
timer_stop('user_save');
if ($reset_password) {
$sql = "UPDATE {users} SET pass='%s' WHERE uid=%d";
db_query($sql, $original_password, $account->uid);
}
// Call completion hooks, for any processing which needs to be done after user_save
timer_start('user completion hooks');
$errors = array_merge($errors, migrate_destination_invoke_all('complete_user', $account, $tblinfo, $row));
timer_stop('user completion hooks');
if ($account) {
migrate_add_mapping($tblinfo->mcsid, $row->{$sourcekey}, $account->uid);
}
else {
// User record wasn't created, but we weren't given back a reason
if (empty($errors)) {
$errors[] = migrate_message(t('user_save failed: Unknown reason'));
}
}
}
return $errors;
}