user_import.install 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * @file
  4. * Import and update users from a comma separated file (csv).
  5. */
  6. /**
  7. * Implementation of hook_schema().
  8. */
  9. function user_import_schema() {
  10. $schema['user_import'] = array(
  11. 'description' => t("Settings for each import, and import setting templates."),
  12. 'fields' => array(
  13. 'import_id' => array(
  14. 'description' => t("ID key of import or template."),
  15. 'type' => 'serial',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. 'disp-width' => '10'
  19. ),
  20. 'name' => array(
  21. 'description' => t("Label of import template, only used if this is an import template."),
  22. 'type' => 'varchar',
  23. 'length' => '25',
  24. 'not null' => TRUE,
  25. 'default' => ''
  26. ),
  27. 'auto_import_directory' => array(
  28. 'description' => t("Name of directory associated with an import template."),
  29. 'type' => 'varchar',
  30. 'length' => '255',
  31. 'not null' => TRUE,
  32. 'default' => ''
  33. ),
  34. 'filename' => array(
  35. 'description' => t("Name of file being used as source of data for import."),
  36. 'type' => 'varchar',
  37. 'length' => '50',
  38. 'not null' => TRUE,
  39. 'default' => ''
  40. ),
  41. 'oldfilename' => array(
  42. 'description' => t("Original name of file being used as source of data for import."),
  43. 'type' => 'varchar',
  44. 'length' => '50',
  45. 'not null' => TRUE,
  46. 'default' => ''
  47. ),
  48. 'filepath' => array(
  49. 'description' => t("Path to file being used as source of data for import."),
  50. 'type' => 'text',
  51. 'size' => 'small',
  52. 'not null' => TRUE
  53. ),
  54. 'started' => array(
  55. 'description' => t("Datestamp of when import was started."),
  56. 'type' => 'int',
  57. 'not null' => TRUE,
  58. 'default' => 0,
  59. 'disp-width' => '11'
  60. ),
  61. 'pointer' => array(
  62. 'description' => t("Pointer to where test/import last finished."),
  63. 'type' => 'int',
  64. 'not null' => TRUE,
  65. 'default' => 0,
  66. 'disp-width' => '10'
  67. ),
  68. 'processed' => array(
  69. 'description' => t("Number of users processed by import."),
  70. 'type' => 'int',
  71. 'not null' => TRUE,
  72. 'default' => 0,
  73. 'disp-width' => '10'
  74. ),
  75. 'valid' => array(
  76. 'description' => t("Number of users processed without errors."),
  77. 'type' => 'int',
  78. 'not null' => TRUE,
  79. 'default' => 0,
  80. 'disp-width' => '10'
  81. ),
  82. 'field_match' => array(
  83. 'description' => t("Settings for how data matches to Drupal fields."),
  84. 'type' => 'text',
  85. 'size' => 'big',
  86. 'not null' => TRUE,
  87. 'serialize' => TRUE
  88. ),
  89. 'roles' => array(
  90. 'description' => t("Roles to give imported users."),
  91. 'type' => 'text',
  92. 'size' => 'big',
  93. 'not null' => TRUE,
  94. 'serialize' => TRUE
  95. ),
  96. 'options' => array(
  97. 'description' => t("Store of all other options for import. Most of the other settings in this table will be moved into here in future."),
  98. 'type' => 'text',
  99. 'size' => 'big',
  100. 'not null' => TRUE,
  101. 'serialize' => TRUE
  102. ),
  103. 'setting' => array(
  104. 'description' => t("Status of import, or whether it is an import template."),
  105. 'type' => 'varchar',
  106. 'length' => '10',
  107. 'not null' => TRUE,
  108. 'default' => ''
  109. )
  110. ),
  111. 'primary key' => array('import_id'),
  112. );
  113. $schema['user_import_errors'] = array(
  114. 'description' => t("Record of errors encountered during an import."),
  115. 'fields' => array(
  116. 'import_id' => array(
  117. 'description' => t("ID key of import or template."),
  118. 'type' => 'int',
  119. 'not null' => TRUE,
  120. 'default' => 0,
  121. 'disp-width' => '10'
  122. ),
  123. 'data' => array(
  124. 'description' => t("Data (matched to fields) for user that failed to import due to error."),
  125. 'type' => 'text',
  126. 'size' => 'big',
  127. 'not null' => TRUE,
  128. 'serialize' => TRUE
  129. ),
  130. 'errors' => array(
  131. 'description' => t("Error(s) encountered for user that failed to import."),
  132. 'type' => 'text',
  133. 'size' => 'big',
  134. 'not null' => TRUE,
  135. 'serialize' => TRUE
  136. )
  137. ),
  138. 'indexes' => array(
  139. 'import_id' => array('import_id')
  140. ),
  141. );
  142. return $schema;
  143. }
  144. function user_import_update_1() {
  145. $ret = array();
  146. _system_update_utf8(array('user_import', 'user_import_errors'));
  147. return $ret;
  148. }
  149. function user_import_update_2() {
  150. $ret = array();
  151. db_add_column($ret, 'user_import', 'options', 'longtext');
  152. return $ret;
  153. }
  154. function user_import_update_3() {
  155. $ret = array();
  156. db_drop_primary_key($ret, 'user_import');
  157. db_change_field($ret, 'user_import', 'iid', 'import_id', array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'disp-width' => '10'), array('primary key' => array('import_id')));
  158. db_change_field($ret, 'user_import', 'first_line', 'first_line_skip', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'), array('primary key' => array('import_id')));
  159. db_drop_index($ret, 'user_import_errors', 'import_id');
  160. db_change_field($ret, 'user_import_errors', 'iid', 'import_id', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'));
  161. db_add_index($ret, 'user_import_errors', 'import_id', array('import_id'));
  162. return $ret;
  163. }
  164. function user_import_update_4() {
  165. $ret = array();
  166. db_drop_index($ret, 'user_import_errors', 'import_id');
  167. db_change_field($ret, 'user_import_errors', 'error', 'errors', array('type' => 'text', 'size' => 'big', 'not null' => TRUE, 'serialize' => TRUE));
  168. db_add_index($ret, 'user_import_errors', 'import_id', array('import_id'));
  169. return $ret;
  170. }
  171. function user_import_update_5() {
  172. $ret = array();
  173. db_drop_index($ret, 'user_import_errors', 'import_id');
  174. db_change_field($ret, 'user_import_errors', 'errors', 'errors', array('type' => 'text', 'size' => 'big', 'not null' => TRUE, 'serialize' => TRUE));
  175. db_add_index($ret, 'user_import_errors', 'import_id', array('import_id'));
  176. return $ret;
  177. }
  178. function user_import_update_6001() {
  179. // Rebuild schema cache
  180. drupal_get_schema('user_import', TRUE);
  181. return array();
  182. }
  183. /**
  184. * Move settings into the 'options' column.
  185. */
  186. function user_import_update_6002() {
  187. $ret = array();
  188. $result = db_query("SELECT * FROM {user_import}");
  189. // Update each import.
  190. while ($import = db_fetch_array($result)) {
  191. $options = unserialize($import['options']);
  192. $options['first_line_skip'] = $import['first_line_skip'];
  193. $options['contact'] = $import['contact'];
  194. $options['username_space'] = $import['username_space'];
  195. $options['send_email'] = $import['send_email'];
  196. //Avoid using update_sql() as it has issues with serialized data.
  197. db_query("UPDATE {user_import} SET options = '%s' WHERE import_id = %d", serialize($options), $import['import_id']);
  198. }
  199. $ret[] = update_sql('ALTER TABLE {user_import} DROP COLUMN first_line_skip');
  200. $ret[] = update_sql('ALTER TABLE {user_import} DROP COLUMN contact');
  201. $ret[] = update_sql('ALTER TABLE {user_import} DROP COLUMN username_space');
  202. $ret[] = update_sql('ALTER TABLE {user_import} DROP COLUMN send_email');
  203. return $ret;
  204. }
  205. /**
  206. * Change the Roles column to LOMGTEXT.
  207. */
  208. function user_import_update_6003() {
  209. $ret = array();
  210. db_change_field($ret, 'user_import', 'roles', 'roles', array('type' => 'text', 'size' => 'big', 'not null' => TRUE, 'serialize' => TRUE));
  211. return $ret;
  212. }
  213. /**
  214. * Add database field to store the name of a directory associated with an import template.
  215. *
  216. **/
  217. function user_import_update_7200(&$sandbox) {
  218. $field = array(
  219. 'description' => t("Name of directory associated with an import template."),
  220. 'type' => 'varchar',
  221. 'length' => '255',
  222. 'not null' => TRUE,
  223. 'default' => '',
  224. );
  225. db_add_field('user_import', 'auto_import_directory', $field);
  226. }
  227. /**
  228. * Implementation of hook_uninstall().
  229. */
  230. function user_import_uninstall() {
  231. variable_del('user_import_settings');
  232. variable_del('user_import_max');
  233. variable_del('user_import_line_max');
  234. variable_del('user_export_checked_usernames');
  235. variable_del('user_import_profile_date_format');
  236. }