taxonomy_csv.drush.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * @file
  4. * Drush commands for taxonomy CSV import/export.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function taxonomy_csv_drush_command() {
  10. $items = array();
  11. $items['taxocsv-import'] = array(
  12. 'aliases' => array('vocimp'),
  13. 'callback' => 'drush_taxonomy_csv_import',
  14. 'description' => 'Import taxonomies and hierarchical structures with CSV file.',
  15. 'examples' => array(
  16. 'drush taxocsv-import my_file flat' => 'Import my_file using the default settings.',
  17. 'drush taxocsv-import my_file tree --keep_order --delimiter=";" --check_line --vocabulary_target=existing --vocabulary_id=3 --update_or_ignore=update --result_terms' => 'Import my_file as a tree structure into vocabulary with vid 2, a semicolon for delimiter and default settings for other options, and display imported terms after process.',
  18. ),
  19. 'arguments' => array(
  20. 'file_path' => 'Required. The full path or url to CSV file to import',
  21. 'import_format' => "Optional. CSV format: 'flat' (default), 'tree', 'polyhierarchy', 'fields', 'translate'",
  22. ),
  23. 'options' => array(
  24. 'fields_format' => "List of comma separated fields (default: 'name')",
  25. 'translate_by' => "First item ('name' or 'tid') of a translation import (default: 'name')",
  26. 'translate_languages' => "List of comma separated languages of terms (default: '')",
  27. 'keep_order' => 'Keep order of imported terms',
  28. 'delimiter' => 'CSV delimiter (default: ",")',
  29. 'enclosure' => "CSV enclosure (default: none or '\"')",
  30. 'filter_format' => 'Default format of the description (default: "plain_text")',
  31. 'filter_format_custom' => 'Default format of custom fields (default: "none", as fixed plain text)',
  32. 'language' => 'Default language of terms (default: "und" (undefined))',
  33. 'check_line' => 'Check format of lines',
  34. 'check_utf8' => 'Check utf8 format',
  35. 'locale_custom' => 'Specific locale of imported file',
  36. 'vocabulary_target' => "'autocreate' (default), 'duplicate' or 'existing'",
  37. 'vocabulary_id' => 'vid or machine_name of the vocabulary to duplicate or to import into',
  38. 'i18n_mode' => 'Internationalization mode of autocreated voc (default: 0 (none))',
  39. 'vocabulary_language' => 'Language of autocreated voc (default: "und" (undefined))',
  40. 'fields_custom' => 'Custom comma separated fields to add or create',
  41. 'delete_terms' => 'Delete all terms before import',
  42. 'check_hierarchy' => 'Check vocabulary hierarchy',
  43. 'set_hierarchy' => "If hierarchy isn't checked, set it (0, 1 or 2 (default))",
  44. 'update_or_ignore' => "What to do with existing items: 'update' (default) or 'ignore'",
  45. // Level of result process infos.
  46. 'result_stats' => 'Display import stats',
  47. 'result_terms' => 'Display list of imported terms',
  48. 'result_level' => "Level of displayed messages: 'first' (default), 'warnings', 'notices' or 'infos'",
  49. 'result_type' => "Group infos 'by_message' (default) or 'by_line'",
  50. ),
  51. );
  52. $items['taxocsv-export'] = array(
  53. 'aliases' => array('vocexp'),
  54. 'callback' => 'drush_taxonomy_csv_export',
  55. 'description' => 'Export terms and properties to a CSV file.',
  56. 'examples' => array(
  57. 'drush taxocsv-export 2 flat' => "Export all terms names of the vocabulary with vid 2, using the default settings.",
  58. 'drush taxocsv-export 2 tree --delimiter=";" --order=name' => 'Export vocabulary with vid 2 as a CSV tree structure ordered by name, with a semicolon for delimiter, and default settings for other options.',
  59. ),
  60. 'arguments' => array(
  61. 'vocabulary_id' => 'Required. vid or machine_name of the vocabulary to export (0 means all)',
  62. 'export_format' => "Optional. CSV format: 'flat' (default), 'tree', 'fields', 'fields_first'",
  63. 'file_path' => "Optional. Full path or filename of exported csv file (default: [current directory]/taxocsv.csv)",
  64. ),
  65. 'options' => array(
  66. 'fields_format' => "List of comma separated fields (default: 'name')",
  67. 'delimiter' => 'One character csv delimiter (default: ",")',
  68. 'enclosure' => "Zero or one character csv enclosure (default: '\"')",
  69. 'line_ending' => "'Unix' (default), 'Mac' or 'MS-DOS'",
  70. 'order' => "Order of terms: 'name' (default), 'tid' or 'weight'",
  71. // Level of result process infos.
  72. 'result_duplicates' => "Display duplicate terms",
  73. ),
  74. );
  75. return $items;
  76. }
  77. /**
  78. * Implements hook_drush_help().
  79. */
  80. function taxonomy_csv_drush_help($section) {
  81. switch ($section) {
  82. case 'drush:taxocsv-import':
  83. return dt('Import taxonomies and hierarchical structures with CSV file.');
  84. case 'drush:taxocsv-export':
  85. return dt('Export terms and properties to a CSV file.');
  86. }
  87. }
  88. /**
  89. * Process the import of an input.
  90. *
  91. * @see drush_invoke()
  92. * @see drush.api.php
  93. */
  94. function drush_taxonomy_csv_import($file_path, $import_format = 'alone_terms') {
  95. // Start process.
  96. drush_print('Checking options...');
  97. if (!$file_path || !file_exists($file_path)) {
  98. drush_log('You need to set the correct path or url of the file to import.', 'error');
  99. return;
  100. }
  101. require_once(drupal_get_path('module', 'taxonomy_csv') . '/import/taxonomy_csv.import.api.inc');
  102. // Set arguments.
  103. $options = array();
  104. $options['url'] = $file_path;
  105. $options['import_format'] = $import_format;
  106. // Get the defaults options and update them with user ones.
  107. $options += _taxonomy_csv_values('import_default_api');
  108. // Set simple options.
  109. foreach (array(
  110. 'delimiter',
  111. 'enclosure',
  112. 'locale_custom',
  113. 'vocabulary_target',
  114. 'vocabulary_id',
  115. 'set_hierarchy',
  116. 'update_or_ignore',
  117. // Level of result process infos.
  118. 'result_level',
  119. 'result_type',
  120. ) as $value) {
  121. $option = drush_get_option($value);
  122. if ($option !== NULL) {
  123. $options[$value] = $option;
  124. }
  125. }
  126. // Set boolean options.
  127. foreach (array(
  128. 'keep_order',
  129. 'check_line',
  130. 'check_utf8',
  131. 'delete_terms',
  132. 'check_hierarchy',
  133. 'set_hierarchy',
  134. // Level of result process infos.
  135. 'result_stats',
  136. 'result_terms',
  137. ) as $value) {
  138. $options[$value] = (drush_get_option($value) === NULL) ? FALSE : TRUE;
  139. }
  140. // Set array options.
  141. foreach (array(
  142. 'translate_languages',
  143. 'fields_format',
  144. 'fields_custom',
  145. ) as $value) {
  146. $option = drush_get_option_list($value);
  147. if ($option !== array()) {
  148. $options[$value] = $option;
  149. }
  150. }
  151. // Set general options.
  152. $options['check_options'] = TRUE;
  153. $options['result_display'] = TRUE;
  154. // Prepare import.
  155. $messages = taxonomy_csv_import($options);
  156. // Last check: unknown options.
  157. $other_options = array_diff(array_keys(drush_get_merged_options()), array_keys($options));
  158. // Compatibility with drush 4.5. See https://drupal.org/node/1395090.
  159. if (($i = array_search('drush-make-update-default-url', $other_options)) !== FALSE) {
  160. unset($other_options[$i]);
  161. }
  162. if ($other_options) {
  163. $messages[] = 'Unknown options:' . ' ' . implode(', ', $other_options) . '.';
  164. }
  165. if (count($messages)) {
  166. foreach ($messages as $message) {
  167. $msg = str_replace('<br />', " \n", $message);
  168. drush_log('- ' . $msg, 'error');
  169. }
  170. return;
  171. }
  172. // Process the batch.
  173. drush_log('Options are good.', 'status');
  174. drush_print('Launch import process. Please wait...');
  175. $batch =& batch_get();
  176. $batch['progressive'] = FALSE;
  177. drush_backend_batch_process();
  178. // End of drush process.
  179. drush_print('End of drush import batch.');
  180. }
  181. /**
  182. * Process the export of a vocabulary.
  183. *
  184. * @see drush_invoke()
  185. * @see drush.api.php
  186. */
  187. function drush_taxonomy_csv_export($vocabulary_id, $export_format = 'alone_terms', $file_path = '') {
  188. // Start process.
  189. drush_print('Checking options...');
  190. if ($vocabulary_id === NULL) {
  191. drush_log('You need to set a vocabulary id (0 means all).', 'error');
  192. return;
  193. }
  194. // Set the destination file path.
  195. if ($file_path == '') {
  196. $file_path = getcwd() . '/taxocsv.csv';
  197. }
  198. require_once(drupal_get_path('module', 'taxonomy_csv') . '/export/taxonomy_csv.export.api.inc');
  199. // Set arguments.
  200. $options = array();
  201. $options['vocabulary_id'] = $vocabulary_id;
  202. $options['export_format'] = $export_format;
  203. // Get the defaults options and update them with user ones.
  204. $options += _taxonomy_csv_values('export_default_api');
  205. // Set simple options.
  206. foreach (array(
  207. 'delimiter',
  208. 'enclosure',
  209. 'line_ending',
  210. 'order',
  211. // Level of result process infos.
  212. ) as $value) {
  213. $option = drush_get_option($value);
  214. if ($option !== NULL) {
  215. $options[$value] = $option;
  216. }
  217. }
  218. // Set boolean options.
  219. foreach (array(
  220. 'result_duplicates',
  221. ) as $value) {
  222. $options[$value] = (drush_get_option($value) === NULL) ? FALSE : TRUE;
  223. }
  224. // Set array options.
  225. foreach (array(
  226. ) as $value) {
  227. $option = drush_get_option_list($value);
  228. if ($option !== array()) {
  229. $options[$value] = $option;
  230. }
  231. }
  232. // Set general options.
  233. $options['check_options'] = TRUE;
  234. $options['result_display'] = TRUE;
  235. // Prepare export.
  236. $messages = taxonomy_csv_export($options);
  237. $file = '';
  238. if (is_object($messages)) {
  239. $file = $messages;
  240. $messages = array();
  241. }
  242. // Last check: unknown options.
  243. $other_options = array_diff(array_keys(drush_get_merged_options()), array_keys($options));
  244. // Compatibility with drush 4.5. See https://drupal.org/node/1395090.
  245. if (($i = array_search('drush-make-update-default-url', $other_options)) !== FALSE) {
  246. unset($other_options[$i]);
  247. }
  248. if ($other_options) {
  249. $messages[] = 'Unknown options:' . ' ' . implode(', ', $other_options) . '.';
  250. }
  251. if (count($messages)) {
  252. foreach ($messages as $message) {
  253. $msg = str_replace('<br />', " \n", $message);
  254. drush_log('- ' . $msg, 'error');
  255. }
  256. return;
  257. }
  258. // Process the batch.
  259. drush_log('Options are good.', 'status');
  260. drush_print('Launch export process. Please wait...');
  261. $batch =& batch_get();
  262. $batch['progressive'] = FALSE;
  263. drush_backend_batch_process();
  264. // Move file to chosen directory if needed.
  265. if (file_exists($file->filepath)) {
  266. rename($file->filepath, $file_path);
  267. drush_log("The vocabulary has been exported to $file_path.", 'status');
  268. }
  269. // End of drush process.
  270. drush_print('End of drush export batch.');
  271. }