user.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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('<a href="@doc">Existing user ID</a>',
  71. array('@doc' => 'http://drupal.org/node/1349632#uid'));
  72. $fields['mail'] = t('<a href="@doc">Email address</a>',
  73. array('@doc' => 'http://drupal.org/node/1349632#mail'));
  74. $fields['name'] = t('<a href="@doc">Username</a>',
  75. array('@doc' => 'http://drupal.org/node/1349632#name'));
  76. $fields['pass'] = t('<a href="@doc">Password</a>',
  77. array('@doc' => 'http://drupal.org/node/1349632#pass'));
  78. $fields['status'] = t('<a href="@doc">Status</a>',
  79. array('@doc' => 'http://drupal.org/node/1349632#status'));
  80. $fields['created'] = t('<a href="@doc">Registered timestamp</a>',
  81. array('@doc' => 'http://drupal.org/node/1349632#created'));
  82. $fields['access'] = t('<a href="@doc">Last access timestamp</a>',
  83. array('@doc' => 'http://drupal.org/node/1349632#access'));
  84. $fields['login'] = t('<a href="@doc">Last login timestamp</a>',
  85. array('@doc' => 'http://drupal.org/node/1349632#login'));
  86. $fields['roles'] = t('<a href="@doc">Role IDs</a>',
  87. array('@doc' => 'http://drupal.org/node/1349632#roles'));
  88. $fields['role_names'] = t('<a href="@doc">Role Names</a>',
  89. array('@doc' => 'http://drupal.org/node/1349632#role_names'));
  90. $fields['picture'] = t('<a href="@doc">Picture</a>',
  91. array('@doc' => 'http://drupal.org/node/1349632#picture'));
  92. $fields['signature'] = t('<a href="@doc">Signature</a>',
  93. array('@doc' => 'http://drupal.org/node/1349632#signature'));
  94. $fields['signature_format'] = t('<a href="@doc">Signature format</a>',
  95. array('@doc' => 'http://drupal.org/node/1349632#signature_format'));
  96. $fields['timezone'] = t('<a href="@doc">Timezone</a>',
  97. array('@doc' => 'http://drupal.org/node/1349632#timezone'));
  98. $fields['language'] = t('<a href="@doc">Language</a>',
  99. array('@doc' => 'http://drupal.org/node/1349632#language'));
  100. $fields['theme'] = t('<a href="@doc">Default theme</a>',
  101. array('@doc' => 'http://drupal.org/node/1349632#theme'));
  102. $fields['init'] = t('<a href="@doc">Init</a>',
  103. array('@doc' => 'http://drupal.org/node/1349632#init'));
  104. $fields['data'] = t('<a href="@doc">Data</a>',
  105. array('@doc' => 'http://drupal.org/node/1349632#init'));
  106. $fields['is_new'] = t('Option: <a href="@doc">Indicates a new user with the specified uid should be created</a>',
  107. array('@doc' => 'http://drupal.org/node/1349632#is_new'));
  108. // Then add in anything provided by handlers
  109. $fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle, $migration);
  110. $fields += migrate_handler_invoke_all('User', 'fields', $this->entityType, $this->bundle, $migration);
  111. return $fields;
  112. }
  113. /**
  114. * Delete a batch of users at once.
  115. *
  116. * @param $uids
  117. * Array of user IDs to be deleted.
  118. */
  119. public function bulkRollback(array $uids) {
  120. migrate_instrument_start('user_delete_multiple');
  121. $this->prepareRollback($uids);
  122. user_delete_multiple($uids);
  123. $this->completeRollback($uids);
  124. migrate_instrument_stop('user_delete_multiple');
  125. }
  126. /**
  127. * Import a single user.
  128. *
  129. * @param $account
  130. * Account object to build. Prefilled with any fields mapped in the Migration.
  131. * @param $row
  132. * Raw source data object - passed through to prepare/complete handlers.
  133. * @return array
  134. * Array of key fields (uid only in this case) of the user that was saved if
  135. * successful. FALSE on failure.
  136. */
  137. public function import(stdClass $account, stdClass $row) {
  138. $migration = Migration::currentMigration();
  139. // Updating previously-migrated content?
  140. if (isset($row->migrate_map_destid1)) {
  141. // Make sure is_new is off
  142. $account->is_new = FALSE;
  143. if (isset($account->uid)) {
  144. if ($account->uid != $row->migrate_map_destid1) {
  145. throw new MigrateException(t("Incoming uid !uid and map destination uid !destid1 don't match",
  146. array('!uid' => $account->uid, '!destid1' => $row->migrate_map_destid1)));
  147. }
  148. }
  149. else {
  150. $account->uid = $row->migrate_map_destid1;
  151. }
  152. }
  153. if ($migration->getSystemOfRecord() == Migration::DESTINATION) {
  154. if (!isset($account->uid)) {
  155. throw new MigrateException(t('System-of-record is DESTINATION, but no destination uid provided'));
  156. }
  157. $old_account = user_load($account->uid, TRUE);
  158. if (empty($old_account)) {
  159. throw new MigrateException(t('System-of-record is DESTINATION, but user !uid does not exist',
  160. array('!uid' => $account->uid)));
  161. }
  162. }
  163. else {
  164. $old_account = $account;
  165. }
  166. // Roles must be arrays keyed by the role id, which isn't how the data
  167. // naturally comes in. Fix them up.
  168. // First, if names instead of IDs are presented, translate them
  169. if (!empty($account->role_names)) {
  170. $role_names = is_array($account->role_names) ? $account->role_names : array($account->role_names);
  171. foreach ($role_names as $role_name) {
  172. $role = user_role_load_by_name($role_name);
  173. if ($role) {
  174. $account->roles[] = $role->rid;
  175. }
  176. }
  177. }
  178. if (!empty($account->roles)) {
  179. if (!is_array($account->roles)) {
  180. $account->roles = array($account->roles);
  181. }
  182. $account->roles = drupal_map_assoc($account->roles);
  183. }
  184. if (empty($account->roles) && empty($old_account->roles)) {
  185. $account->roles = array();
  186. }
  187. $this->prepare($account, $row);
  188. if (isset($account->uid) && !(isset($account->is_new) && $account->is_new)) {
  189. $updating = TRUE;
  190. }
  191. else {
  192. $updating = FALSE;
  193. }
  194. // While user_save is happy to see a fid in $account->picture on insert,
  195. // when updating an existing account it wants a file object.
  196. if ($updating && isset($account->picture) && ($fid = $account->picture)) {
  197. $account->picture = file_load($fid);
  198. }
  199. // Normalize MD5 passwords to lowercase, as generated by Drupal 6 and previous
  200. if ($this->md5Passwords) {
  201. $account->pass = drupal_strtolower($account->pass);
  202. }
  203. // If any datetime values were included, ensure that they're in timestamp format.
  204. if (isset($account->created)) {
  205. $account->created = MigrationBase::timestamp($account->created);
  206. }
  207. if (isset($account->access)) {
  208. $account->access = MigrationBase::timestamp($account->access);
  209. }
  210. if (isset($account->login)) {
  211. $account->login = MigrationBase::timestamp($account->login);
  212. }
  213. // Validate field data prior to saving.
  214. MigrateDestinationEntity::fieldAttachValidate('user', $account);
  215. migrate_instrument_start('user_save');
  216. $newaccount = user_save($old_account, (array)$account);
  217. migrate_instrument_stop('user_save');
  218. if ($newaccount) {
  219. if ($this->md5Passwords && !empty($account->pass)) {
  220. // Ape the Drupal 6 -> Drupal 7 upgrade, which encrypts the MD5 text in the
  221. // modern way, and marks it with a prepended U so it recognizes and fixes it
  222. // up at login time.
  223. $password = 'U' . $newaccount->pass;
  224. db_update('users')
  225. ->fields(array('pass' => $password))
  226. ->condition('uid', $newaccount->uid)
  227. ->execute();
  228. }
  229. // Unlike nodes and taxonomy terms, core does not automatically save an
  230. // alias in a user entity, we must do it ourselves.
  231. if (module_exists('path')) {
  232. if (!empty($account->path['alias'])) {
  233. $path = array(
  234. 'source' => 'user/' . $account->uid,
  235. 'alias' => $account->path['alias'],
  236. );
  237. migrate_instrument_start('path_save');
  238. path_save($path);
  239. migrate_instrument_stop('path_save');
  240. }
  241. }
  242. if ($updating) {
  243. $this->numUpdated++;
  244. }
  245. else {
  246. $this->numCreated++;
  247. // user_save() doesn't update file_usage on account creation, we have
  248. // to do it ourselves.
  249. if (!empty($newaccount->picture)) {
  250. $file = file_load($newaccount->picture);
  251. if (is_object($file)) {
  252. file_usage_add($file, 'user', 'user', $newaccount->uid);
  253. }
  254. }
  255. }
  256. $this->complete($newaccount, $row);
  257. $return = array($newaccount->uid);
  258. }
  259. else {
  260. $return = FALSE;
  261. }
  262. return $return;
  263. }
  264. }
  265. class MigrateDestinationRole extends MigrateDestinationTable {
  266. public function __construct() {
  267. parent::__construct('role');
  268. }
  269. /**
  270. * Get the key definition for the role table.
  271. *
  272. * @param $dummy
  273. * PHP is picky - it throws E_STRICT notices if we don't have a parameter
  274. * because MigrateDestinationTable has one.
  275. */
  276. static public function getKeySchema($dummy = NULL) {
  277. return MigrateDestinationTable::getKeySchema('role');
  278. }
  279. /**
  280. * Delete a single row.
  281. *
  282. * @param $id
  283. * Primary key values.
  284. */
  285. public function rollback(array $id) {
  286. migrate_instrument_start('role rollback');
  287. $rid = reset($id);
  288. user_role_delete((int)$rid);
  289. migrate_instrument_stop('role rollback');
  290. }
  291. /**
  292. * Import a single row.
  293. *
  294. * @param $entity
  295. * Object object to build. Prefilled with any fields mapped in the Migration.
  296. * @param $row
  297. * Raw source data object - passed through to prepare/complete handlers.
  298. * @return array
  299. * Array of key fields of the object that was saved if
  300. * successful. FALSE on failure.
  301. */
  302. public function import(stdClass $entity, stdClass $row) {
  303. $migration = Migration::currentMigration();
  304. $updating = FALSE;
  305. // Updating previously-migrated content?
  306. if (isset($row->migrate_map_destid1)) {
  307. $updating = TRUE;
  308. if (isset($entity->rid)) {
  309. if ($entity->rid != $row->migrate_map_destid1) {
  310. throw new MigrateException(t("Incoming id !id and map destination id !destid don't match",
  311. array('!id' => $entity->rid, '!destid' => $row->migrate_map_destid1)));
  312. }
  313. }
  314. else {
  315. $entity->rid = $row->migrate_map_destid1;
  316. }
  317. }
  318. if ($migration->getSystemOfRecord() == Migration::DESTINATION) {
  319. $updating = TRUE;
  320. if (!isset($entity->rid)) {
  321. throw new MigrateException(t('System-of-record is DESTINATION, but no destination id provided'));
  322. }
  323. $old_entity = user_role_load($entity->rid);
  324. foreach ($entity as $field => $value) {
  325. $old_entity->$field = $entity->$field;
  326. }
  327. $entity = $old_entity;
  328. }
  329. $this->prepare($entity, $row);
  330. user_role_save($entity);
  331. $this->complete($entity, $row);
  332. if (!empty($entity->rid)) {
  333. $id = array($entity->rid);
  334. if ($updating) {
  335. $this->numUpdated++;
  336. }
  337. else {
  338. $this->numCreated++;
  339. }
  340. }
  341. else {
  342. $id = FALSE;
  343. }
  344. return $id;
  345. }
  346. }