user.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /**
  3. * @file
  4. * Support for user destinations.
  5. */
  6. // TODO:
  7. // Make sure this works with updates, explicit destination keys
  8. // Speed up password generation a ton: $conf['password_count_log2'] = 1;
  9. /**
  10. * Destination class implementing migration into users.
  11. */
  12. class MigrateDestinationUser extends MigrateDestinationEntity {
  13. /**
  14. * Indicates whether incoming passwords are md5-encrypted - if so, we will
  15. * rehash them similarly to the D6->D7 upgrade path.
  16. *
  17. * @var boolean
  18. */
  19. protected $md5Passwords = FALSE;
  20. static public function getKeySchema() {
  21. return array(
  22. 'uid' => array(
  23. 'type' => 'int',
  24. 'unsigned' => TRUE,
  25. 'description' => 'ID of destination user',
  26. ),
  27. );
  28. }
  29. /**
  30. * Return an options array for user destinations.
  31. *
  32. * @param string $language
  33. * Default language for usrs created via this destination class.
  34. * @param string $text_format
  35. * Default text format for users created via this destination class.
  36. * @param boolean $md5_passwords
  37. * Set TRUE to indicate incoming passwords are md5-encrypted.
  38. */
  39. static public function options($language, $text_format, $md5_passwords) {
  40. return compact('language', 'text_format', 'md5_passwords');
  41. }
  42. /**
  43. * Basic initialization
  44. *
  45. * @param array $options
  46. * Options applied to comments.
  47. */
  48. public function __construct(array $options = array()) {
  49. parent::__construct('user', 'user', $options);
  50. if (!empty($options['md5_passwords'])) {
  51. $this->md5Passwords = $options['md5_passwords'];
  52. }
  53. // Reduce hash count so import runs in a reasonable time (use same value as
  54. // the standard Drupal 6=>Drupal 7 upgrade path).
  55. global $conf;
  56. $conf['password_count_log2'] = 11;
  57. }
  58. /**
  59. * Returns a list of fields available to be mapped for users
  60. *
  61. * @param Migration $migration
  62. * Optionally, the migration containing this destination.
  63. * @return array
  64. * Keys: machine names of the fields (to be passed to addFieldMapping)
  65. * Values: Human-friendly descriptions of the fields.
  66. */
  67. public function fields($migration = NULL) {
  68. $fields = array();
  69. // First the core (users table) properties
  70. $fields['uid'] = t('User: <a href="@doc">Existing user ID</a>',
  71. array('@doc' => 'http://drupal.org/node/1349632#uid'));
  72. $fields['mail'] = t('User: <a href="@doc">Email address</a>',
  73. array('@doc' => 'http://drupal.org/node/1349632#mail'));
  74. $fields['name'] = t('User: <a href="@doc">Username</a>',
  75. array('@doc' => 'http://drupal.org/node/1349632#name'));
  76. $fields['pass'] = t('User: <a href="@doc">Password (plain text)</a>',
  77. array('@doc' => 'http://drupal.org/node/1349632#pass'));
  78. $fields['status'] = t('User: <a href="@doc">Status</a>',
  79. array('@doc' => 'http://drupal.org/node/1349632#status'));
  80. $fields['created'] = t('User: <a href="@doc">Registered timestamp</a>',
  81. array('@doc' => 'http://drupal.org/node/1349632#created'));
  82. $fields['access'] = t('User: <a href="@doc">Last access timestamp</a>',
  83. array('@doc' => 'http://drupal.org/node/1349632#access'));
  84. $fields['login'] = t('User: <a href="@doc">Last login timestamp</a>',
  85. array('@doc' => 'http://drupal.org/node/1349632#login'));
  86. $fields['roles'] = t('User: <a href="@doc">Role IDs</a>',
  87. array('@doc' => 'http://drupal.org/node/1349632#roles'));
  88. $fields['role_names'] = t('User: <a href="@doc">Role Names</a>',
  89. array('@doc' => 'http://drupal.org/node/1349632#role_names'));
  90. $fields['picture'] = t('User: <a href="@doc">Picture</a>',
  91. array('@doc' => 'http://drupal.org/node/1349632#picture'));
  92. $fields['signature'] = t('User: <a href="@doc">Signature</a>',
  93. array('@doc' => 'http://drupal.org/node/1349632#signature'));
  94. $fields['signature_format'] = t('User: <a href="@doc">Signature format</a>',
  95. array('@doc' => 'http://drupal.org/node/1349632#signature_format'));
  96. $fields['timezone'] = t('User: <a href="@doc">Timezone</a>',
  97. array('@doc' => 'http://drupal.org/node/1349632#timezone'));
  98. $fields['language'] = t('User: <a href="@doc">Language</a>',
  99. array('@doc' => 'http://drupal.org/node/1349632#language'));
  100. $fields['theme'] = t('User: <a href="@doc">Default theme</a>',
  101. array('@doc' => 'http://drupal.org/node/1349632#theme'));
  102. $fields['init'] = t('User: <a href="@doc">Init</a>',
  103. array('@doc' => 'http://drupal.org/node/1349632#init'));
  104. $fields['is_new'] = t('Option: <a href="@doc">Indicates a new user with the specified uid should be created</a>',
  105. array('@doc' => 'http://drupal.org/node/1349632#is_new'));
  106. // Then add in anything provided by handlers
  107. $fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle, $migration);
  108. $fields += migrate_handler_invoke_all('User', 'fields', $this->entityType, $this->bundle, $migration);
  109. return $fields;
  110. }
  111. /**
  112. * Delete a batch of users at once.
  113. *
  114. * @param $uids
  115. * Array of user IDs to be deleted.
  116. */
  117. public function bulkRollback(array $uids) {
  118. migrate_instrument_start('user_delete_multiple');
  119. $this->prepareRollback($uids);
  120. user_delete_multiple($uids);
  121. $this->completeRollback($uids);
  122. migrate_instrument_stop('user_delete_multiple');
  123. }
  124. /**
  125. * Import a single user.
  126. *
  127. * @param $account
  128. * Account object to build. Prefilled with any fields mapped in the Migration.
  129. * @param $row
  130. * Raw source data object - passed through to prepare/complete handlers.
  131. * @return array
  132. * Array of key fields (uid only in this case) of the user that was saved if
  133. * successful. FALSE on failure.
  134. */
  135. public function import(stdClass $account, stdClass $row) {
  136. $migration = Migration::currentMigration();
  137. // Updating previously-migrated content?
  138. if (isset($row->migrate_map_destid1)) {
  139. // Make sure is_new is off
  140. $account->is_new = FALSE;
  141. if (isset($account->uid)) {
  142. if ($account->uid != $row->migrate_map_destid1) {
  143. throw new MigrateException(t("Incoming uid !uid and map destination uid !destid1 don't match",
  144. array('!uid' => $account->uid, '!destid1' => $row->migrate_map_destid1)));
  145. }
  146. }
  147. else {
  148. $account->uid = $row->migrate_map_destid1;
  149. }
  150. }
  151. if ($migration->getSystemOfRecord() == Migration::DESTINATION) {
  152. if (!isset($account->uid)) {
  153. throw new MigrateException(t('System-of-record is DESTINATION, but no destination uid provided'));
  154. }
  155. $old_account = user_load($account->uid, TRUE);
  156. if (empty($old_account)) {
  157. throw new MigrateException(t('System-of-record is DESTINATION, but user !uid does not exist',
  158. array('!uid' => $account->uid)));
  159. }
  160. }
  161. else {
  162. $old_account = $account;
  163. }
  164. // Roles must be arrays keyed by the role id, which isn't how the data
  165. // naturally comes in. Fix them up.
  166. // First, if names instead of IDs are presented, translate them
  167. if (!empty($account->role_names)) {
  168. $role_names = is_array($account->role_names) ? $account->role_names : array($account->role_names);
  169. foreach ($role_names as $role_name) {
  170. $role = user_role_load_by_name($role_name);
  171. if ($role) {
  172. $account->roles[] = $role->rid;
  173. }
  174. }
  175. }
  176. if (!empty($account->roles)) {
  177. if (!is_array($account->roles)) {
  178. $account->roles = array($account->roles);
  179. }
  180. $account->roles = drupal_map_assoc($account->roles);
  181. }
  182. if (empty($account->roles) && empty($old_account->roles)) {
  183. $account->roles = array();
  184. }
  185. $this->prepare($account, $row);
  186. if (isset($account->uid) && !(isset($account->is_new) && $account->is_new)) {
  187. $updating = TRUE;
  188. }
  189. else {
  190. $updating = FALSE;
  191. }
  192. // While user_save is happy to see a fid in $account->picture on insert,
  193. // when updating an existing account it wants a file object.
  194. if ($updating && ($fid = $account->picture)) {
  195. $account->picture = file_load($fid);
  196. }
  197. // Normalize MD5 passwords to lowercase, as generated by Drupal 6 and previous
  198. if ($this->md5Passwords) {
  199. $account->pass = drupal_strtolower($account->pass);
  200. }
  201. // If any datetime values were included, ensure that they're in timestamp format.
  202. if (isset($account->created)) {
  203. $account->created = MigrationBase::timestamp($account->created);
  204. }
  205. if (isset($account->access)) {
  206. $account->access = MigrationBase::timestamp($account->access);
  207. }
  208. if (isset($account->login)) {
  209. $account->login = MigrationBase::timestamp($account->login);
  210. }
  211. migrate_instrument_start('user_save');
  212. $newaccount = user_save($old_account, (array)$account);
  213. migrate_instrument_stop('user_save');
  214. if ($newaccount) {
  215. if ($this->md5Passwords && !empty($account->pass)) {
  216. // Ape the Drupal 6 -> Drupal 7 upgrade, which encrypts the MD5 text in the
  217. // modern way, and marks it with a prepended U so it recognizes and fixes it
  218. // up at login time.
  219. $password = 'U' . $newaccount->pass;
  220. db_update('users')
  221. ->fields(array('pass' => $password))
  222. ->condition('uid', $newaccount->uid)
  223. ->execute();
  224. }
  225. if ($updating) {
  226. $this->numUpdated++;
  227. }
  228. else {
  229. $this->numCreated++;
  230. }
  231. $this->complete($newaccount, $row);
  232. $return = array($newaccount->uid);
  233. }
  234. else {
  235. $return = FALSE;
  236. }
  237. return $return;
  238. }
  239. }
  240. class MigrateDestinationRole extends MigrateDestinationTable {
  241. public function __construct() {
  242. parent::__construct('role');
  243. }
  244. /**
  245. * Get the key definition for the role table.
  246. *
  247. * @param $dummy
  248. * PHP is picky - it throws E_STRICT notices if we don't have a parameter
  249. * because MigrateDestinationTable has one.
  250. */
  251. static public function getKeySchema($dummy = NULL) {
  252. return MigrateDestinationTable::getKeySchema('role');
  253. }
  254. /**
  255. * Delete a single row.
  256. *
  257. * @param $id
  258. * Primary key values.
  259. */
  260. public function rollback(array $id) {
  261. migrate_instrument_start('role rollback');
  262. $rid = reset($id);
  263. user_role_delete((int)$rid);
  264. migrate_instrument_stop('role rollback');
  265. }
  266. /**
  267. * Import a single row.
  268. *
  269. * @param $entity
  270. * Object object to build. Prefilled with any fields mapped in the Migration.
  271. * @param $row
  272. * Raw source data object - passed through to prepare/complete handlers.
  273. * @return array
  274. * Array of key fields of the object that was saved if
  275. * successful. FALSE on failure.
  276. */
  277. public function import(stdClass $entity, stdClass $row) {
  278. $migration = Migration::currentMigration();
  279. // Updating previously-migrated content?
  280. if (isset($row->migrate_map_destid1)) {
  281. if (isset($entity->rid)) {
  282. if ($entity->rid != $row->migrate_map_destid1) {
  283. throw new MigrateException(t("Incoming id !id and map destination id !destid don't match",
  284. array('!id' => $entity->rid, '!destid' => $row->migrate_map_destid1)));
  285. }
  286. else {
  287. $entity->rid = $row->migrate_map_destid1;
  288. }
  289. }
  290. }
  291. if ($migration->getSystemOfRecord() == Migration::DESTINATION) {
  292. if (!isset($entity->rid)) {
  293. throw new MigrateException(t('System-of-record is DESTINATION, but no destination id provided'));
  294. }
  295. $old_entity = user_role_load($entity->rid);
  296. foreach ($entity as $field => $value) {
  297. $old_entity->$field = $entity->$field;
  298. }
  299. $entity = $old_entity;
  300. }
  301. $this->prepare($entity, $row);
  302. user_role_save($entity);
  303. $this->complete($entity, $row);
  304. if (!empty($entity->rid)) {
  305. $id = array($entity->rid);
  306. }
  307. else {
  308. $id = FALSE;
  309. }
  310. return $id;
  311. }
  312. }