Index: import_export.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/import_export/import_export.module,v
retrieving revision 1.4
diff -u -r1.4 import_export.module
--- import_export.module 4 Sep 2005 23:10:46 -0000 1.4
+++ import_export.module 12 Sep 2006 19:05:53 -0000
@@ -7,34 +7,34 @@
function import_export_menu($may_cache) {
if ($may_cache) {
- $items[] = array('path' => 'import',
- 'title' => t('import/export'),
+ $items[] = array('path' => 'admin/import_export',
+ 'title' => t('import_export'),
'callback' => 'import_export_page',
'access' => user_access('access import_export'),
'type' => MENU_NORMAL_ITEM);
- $items[] = array('path' => 'import/cvs/export',
- 'title' => t('CVS export'),
- 'callback' => 'import_export_page_cvs_export',
+ $items[] = array('path' => 'admin/import_export/csv/export',
+ 'title' => t('CSV export'),
+ 'callback' => 'import_export_page_csv_export',
'access' => user_access('access import_export'),
'type' => MENU_CALLBACK);
- $items[] = array('path' => 'import/xml',
+ $items[] = array('path' => 'admin/import_export/xml',
'title' => t('XML import/export'),
'callback' => 'import_export_page_xml',
'access' => user_access('access import_export'),
'type' => MENU_CALLBACK);
- $items[] = array('path' => 'import/xml/vocabulary',
+ $items[] = array('path' => 'admin/import_export/xml/vocabulary',
'title' => t('XML export (vocabulary)'),
'callback' => 'import_export_page_xml_vocabulary',
'access' => user_access('access import_export'),
'type' => MENU_CALLBACK);
- $items[] = array('path' => 'import/xml/book',
+ $items[] = array('path' => 'admin/import_export/xml/book',
'title' => t('XML export (book)'),
'callback' => 'import_export_page_xml_book',
'access' => user_access('access import_export'),
'type' => MENU_CALLBACK);
- $items[] = array('path' => 'import/cvs/import',
- 'title' => t('CVS import'),
- 'callback' => 'import_export_page_cvs_import',
+ $items[] = array('path' => 'admin/import_export/csv/import',
+ 'title' => t('CSV import'),
+ 'callback' => 'import_export_page_csv_import',
'access' => user_access('access import_export'),
'type' => MENU_CALLBACK);
}
@@ -48,10 +48,12 @@
function import_export_help($section) {
switch ($section) {
+ case 'admin/help#import_export':
+ return t('
Import_export module imports and exports nodes as XML or CSV files.
Import/Export from a CSV file or a XML formated file is supported. The CSV is suitable for simple nodes (weblinks, images, etc.), the XML for large ones (stories, books, etc.). There is support for common nodes, stories, books, pages, images, flexinodes directly into module.
Note that files must be UTF-8 encoded.
Form groups are divided into four actions in the first page of the wizard you can choose from:
XML import/export requires XML support in PHP, see PHP documentation. Generally http://www.jclark.com/xml/expat.html support should be installed.
This patch is provided by Turhan Ercil,
this work is sponsored by Ashoka.org and CoopTools.ca
Import_export module imports and exports nodes as XML or CSV files.
Import/Export from a CSV file or a XML formated file is supported. The CSV is suitable for simple nodes (weblinks, images, etc.), the XML for large ones (stories, books, etc.). There is support for common nodes, stories, books, pages, images, flexinodes directly into module.
Note that files must be UTF-8 encoded.
'); } } @@ -61,19 +63,19 @@ function import_export_page() { - $types = node_invoke_nodeapi($node/*dummy*/, 'import_export', 'types', IMPORT_EXPORT_CVS); + $types = node_invoke_nodeapi($node/*dummy*/, 'import_export', 'types', IMPORT_EXPORT_CSV); asort($types); if (count($types)) { $group = form_select(t('Node type'), 'node_type', -1, $types, t('Node type to export'), 0, false, true); $group .= form_submit(t('Next')); - $group = form($group, 'post', '?q=import/cvs/export'); - $output .= form_group(t('CVS export'), $group); + $group = form($group, 'post', '?q=admin/import_export/csv/export'); + $output .= form_group(t('CSV export'), $group); - $group = l(t('Import from CVS file'), 'import/cvs/import'); - $output .= form_group(t('CVS import'), $group); + $group = l(t('Import from CSV file'), 'admin/import_export/csv/import'); + $output .= form_group(t('CSV import'), $group); } else { - $output .= t('CVS import/export not supported by any module').'\n";
if ($n >= 0) {
- $output .= sprintf(t('%d exported record(s)'), $n)."\n\n";
+ $output .= sprintf(t('%d exported record(s)'), $n)."\n"; // Deleted /n for symmetry
}
$output .= "\n";
}
- _import_export_print($output, $out, $_POST['op_cvs']['friendly'], 'text/plain', 'export.txt');
+ _import_export_print($output, $out, $_POST['op_csv']['friendly'], 'text/plain', 'export.txt');
}
/**
@@ -378,27 +387,27 @@
}
/**
- * Implementation of common CVS import page
+ * Implementation of common CSV import page
*/
-function import_export_page_cvs_import() {
+function import_export_page_csv_import() {
$output = '';
- $group = form_textarea(t('CVS data'), 'cvs_data', $_POST['edit']['cvs_data'], 80, 10, t('CVS data to be imported, first raw is header containing field names (TAB/\'|\'..field delimiter, CR/LF..record delimiter, \':\'..array delimiter).'));
+ $group = form_textarea(t('CSV data'), 'csv_data', $_POST['edit']['csv_data'], 80, 10, t('CSV data to be imported, first raw is header containing field names (TAB/\'|\'..field delimiter, CR/LF..record delimiter, \':\'..array delimiter).'));
$group .= _import_export_form_radios_action();
- $types = node_invoke_nodeapi($node, 'import_export', 'types', IMPORT_EXPORT_CVS);
+ $types = node_invoke_nodeapi($node, 'import_export', 'types', IMPORT_EXPORT_CSV);
asort($types);
$types = array_merge(array(''=>'<'.t('none').'>'), $types);
- $group .= form_select(t('Node type'), 'def_type', $_POST['edit']['def_type'], $types, t('Default node type used unless specified in CVS field'), 0, false, true);
- $group .= form_submit(t('Preview'), 'op_cvs[preview]');
- $group .= form_submit(t('Process'), 'op_cvs[import]');
+ $group .= form_select(t('Node type'), 'def_type', $_POST['edit']['def_type'], $types, t('Default node type used unless specified in CSV field'), 0, false, true);
+ $group .= form_submit(t('Preview'), 'op_csv[preview]');
+ $group .= form_submit(t('Process'), 'op_csv[import]');
$group = form($group, 'post', 0);
$output .= form_group(t('Import'), $group);
- if ($_POST['op_cvs']) {
- _import_export_parse_cvs($_POST['edit']['cvs_data'], $_POST['op_cvs']['preview'], _import_export_form_radio2text(), $_POST['edit']['def_type'], $out, $cnt, $cnt1);
+ if ($_POST['op_csv']) {
+ _import_export_parse_csv($_POST['edit']['csv_data'], $_POST['op_csv']['preview'], _import_export_form_radio2text(), $_POST['edit']['def_type'], $out, $cnt, $cnt1);
$output .= "\n";
$output .= sprintf(t('Processed %d of %d line(s)'), $cnt1, $cnt)."\n\n".$out.''; } @@ -406,10 +415,10 @@ print theme('page', $output); } -// CVS delimiter definitions -define('CVS_FIELD_DELIM', '|'); -define('CVS_RECORD_DELIM', "\r\n"); -define('CVS_ARRAY_DELIM', ':'); +// CSV delimiter definitions +define('CSV_FIELD_DELIM', $_POST['edit']['delim']); +define('CSV_RECORD_DELIM', "\r\n"); +define('CSV_ARRAY_DELIM', $_POST['edit']['delim_array']); function _import_export_node2str(&$node, $fid, $fld, &$data) { @@ -428,7 +437,7 @@ } /** - * Export node data in CVS format + * Export node data in CSV format * @param $node * array od nids * @@ -439,7 +448,7 @@ * max. length of exported fields, leave blank or null to unlimited length * * @param $output - * exported CVS data + * exported CSV data * * @param field_delimiter * char delimiting fields @@ -448,7 +457,8 @@ * number of exported lines, -1..error */ -function _import_export_prepare_cvs($nodeids, $fields, $maxlen, &$output, $field_delimiter = CVS_FIELD_DELIM) { +function _import_export_prepare_csv($nodeids, $fields, $maxlen, &$output, $field_delimiter = CSV_FIELD_DELIM) { + $taxonomy_format = $_POST['edit']['taxonomy_format']; $cnt = 0; $output = ''; if (count($fields) == 0) { @@ -459,10 +469,12 @@ $arr = array(); foreach($fields as $fid=>$fld) { if ($fld['order']) { + $delim_text = $_POST['edit']['delim_text']; + $fld['name'] = $delim_text.str_replace('"', "'", $fld['name']).$delim_text; $arr[] = $fld['name']; } } - $output .= Implode($arr, $field_delimiter).CVS_RECORD_DELIM; + $output .= Implode($arr, $field_delimiter).CSV_RECORD_DELIM; foreach ($nodeids as $nodeid) { $node = _import_export_node_load($nodeid); @@ -472,6 +484,7 @@ $f_arr = array(); foreach($fields as $fid=>$fld) { + $delim_text = $_POST['edit']['delim_text']; if (!$fld['order']) { continue; } @@ -482,33 +495,37 @@ $tax = taxonomy_node_get_terms_by_vocabulary($node->nid, $fld['vid']); $arr = array(); foreach ($tax as $tid=>$term) { - switch ($fld['format']) { + switch ($taxonomy_format) { case IMPORT_EXPORT_TERM_FORMAT_NAME: $arr[] = preg_replace('/:/', '-', $term->name); break; case IMPORT_EXPORT_TERM_FORMAT_ID_NAME: $arr[] = sprintf('%d=%s', $tid, preg_replace('/:/', '-', $term->name)); break; - default: + case IMPORT_EXPORT_TERM_FORMAT_ID: $arr[] = $tid; break; } } - $s = Implode($arr, CVS_ARRAY_DELIM); + + $s = Implode($arr, CSV_ARRAY_DELIM); + $s = $delim_text.str_replace('"', "'", "$s").$delim_text; //Wrap around text delimiter } break; default: _import_export_node2str($node, $fid, $fld, $s); + $s = str_replace('"', "'", "$s"); if ($maxlen) { $s = SubStr($s, 0, $maxlen); } + $s = $delim_text.$s.$delim_text; //Wrap around text delimiter break; } - $f_arr[] = preg_replace(array('/\\'.CVS_FIELD_DELIM.'/', '/\\\\/', "/\r\n/", "/\r/", "/\n/"), - array(' ', '\\\\', '\\n', '', '\\n'), + $f_arr[] = preg_replace(array('/\\\\/', "/\r\n/", "/\r/", "/\n/"), + array('\\\\', '\\n', '', '\\n'), $s); } - $output .= Implode($f_arr, CVS_FIELD_DELIM).CVS_RECORD_DELIM; + $output .= Implode($f_arr, CSV_FIELD_DELIM).CSV_RECORD_DELIM; Unset($f_arr); $cnt++; } @@ -612,7 +629,7 @@ } if ($type!='' && $node->type!=$type) { - $output .= t('CVS/node type does not match'); + $output .= t('CSV/node type does not match'); return false; } $node->_taxonomy = array(); @@ -811,8 +828,8 @@ /** * Load current node (if exists) and check if $action can be processed * - * @param $cvs_data - * raw CVS data + * @param $csv_data + * raw CSV data * * @param $preview * do not save changes, only write log @@ -827,23 +844,23 @@ * text process log * * @param $num_lines - * counter of CVS data lines + * counter of CSV data lines * * @param $num_proc * counter of processed records/lines */ -function _import_export_parse_cvs($cvs_data, $preview, $action, $def_type, &$output, &$num_lines, &$num_proc) { +function _import_export_parse_csv($csv_data, $preview, $action, $def_type, &$output, &$num_lines, &$num_proc) { $output = ''; if ($preview) { $output .= '*** '.t('Preview mode, no data has been affected')." ***\n\n"; } - $cvs_data = preg_replace(array("/\t/", "/\r/", "/\n\n/"), - array(CVS_FIELD_DELIM, "\n", "\n"), - $cvs_data); - $rows = Explode("\n", $cvs_data); - Unset($cvs_data); - $flds = Explode(CVS_FIELD_DELIM, $rows[0]); + $csv_data = preg_replace(array("/\t/", "/\r/", "/\n\n/"), + array(CSV_FIELD_DELIM, "\n", "\n"), + $csv_data); + $rows = Explode("\n", $csv_data); + Unset($csv_data); + $flds = Explode(CSV_FIELD_DELIM, $rows[0]); $mandatory_idx = array(); $mandatory_import_fields = _import_export_get_mandatory_import_fields(); @@ -858,13 +875,13 @@ $fixups = _import_export_node_init_fixup(); $num_lines = $num_proc = 0; - $supported_types = node_invoke_nodeapi($node/*dummy*/, 'import_export', 'types', IMPORT_EXPORT_CVS); + $supported_types = node_invoke_nodeapi($node/*dummy*/, 'import_export', 'types', IMPORT_EXPORT_CSV); for ($i=1; $i