user_import.drush.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration for the User Import module.
  5. */
  6. function user_import_drush_help($command) {
  7. switch ($command) {
  8. case 'drush:user-import':
  9. return dt('Queue an import of users from a CSV file');
  10. case 'error:template-not-found':
  11. return dt('Template not found');
  12. case 'error:file-not-found':
  13. return dt('File not found');
  14. case 'error:no-default-template':
  15. return dt('There is no default template configured');
  16. }
  17. }
  18. /**
  19. * Implementation of hook_drush_command().
  20. *
  21. * In this hook, you specify which commands your
  22. * drush module makes available, what it does and
  23. * description.
  24. *
  25. * Notice how this structure closely resembles how
  26. * you define menu hooks.
  27. *
  28. * See `drush topic docs-commands` for a list of recognized keys.
  29. *
  30. * @return
  31. * An associative array describing your command(s).
  32. */
  33. function user_import_drush_command() {
  34. $commands = array();
  35. $commands['user-import'] = array(
  36. 'description' => dt('Queue an import of users'),
  37. 'arguments' => array(
  38. 'file' => dt('The CSV file with user data'),
  39. 'template' => dt('Name of the template to use (optional, if not provided the default template is used)'),
  40. ),
  41. 'examples' => array(
  42. dt('standard example') => 'drush user-import users.csv',
  43. ),
  44. );
  45. return $commands;
  46. }
  47. function drush_user_import($original_file = NULL, $template_name = NULL) {
  48. if (!file_exists($original_file)) {
  49. return drush_set_error('file-not-found');
  50. }
  51. $original_file = realpath($original_file);
  52. if ($template_name) {
  53. $template = db_query("SELECT * FROM {user_import} WHERE name = :name AND setting = 'template'", array(':name' => $template_name))->fetchObject();
  54. if (!$template) {
  55. return drush_set_error('template-not-found');
  56. }
  57. }
  58. else {
  59. $template_id = variable_get('user_import_settings', '0');
  60. if (!$template_id) {
  61. return drush_set_error('no-default-template');
  62. }
  63. $template = db_query("SELECT * FROM {user_import} WHERE import_id = :import_id AND setting = 'template'", array(':import_id' => $template_id))->fetchObject();
  64. if (!$template) {
  65. return drush_set_error('template-not-found');
  66. }
  67. drush_print(dt('Using default settings template "!template"', array('!template' => $template->name)));
  68. }
  69. $template->options = unserialize($template->options);
  70. $template->field_match = unserialize($template->field_match);
  71. $template->roles = unserialize($template->roles);
  72. $file = new stdClass();
  73. $file->filepath = $original_file;
  74. $file->filename = basename($original_file);
  75. $destination = 'private://user_import/processing';
  76. $filepath = file_unmanaged_copy($file->filepath, $destination, FILE_EXISTS_RENAME);
  77. // initialize import from template
  78. $import = new stdClass();
  79. $import->oldfilename = basename($original_file);
  80. $import->filename = $file->filename;
  81. $import->filepath = $filepath;
  82. $import->started = time();
  83. $import->field_match = $template->field_match;
  84. $import->roles = $template->roles;
  85. $import->options = $template->options;
  86. $import->setting = 'import';
  87. $result = drupal_write_record('user_import', $import);
  88. if ($result == SAVED_NEW) {
  89. drush_print(dt('Successfully queued user import. The original CSV file !file has been copied to the Drupal installation and can be removed.', array('!file' => $original_file)));
  90. }
  91. else {
  92. drush_set_error('import_failed', dt('Unable to queue the user import.'));
  93. }
  94. }