Skip to content
Snippets Groups Projects
Commit f8062437 authored by Alex Barth's avatar Alex Barth
Browse files

#850652 alex_b: Make ParserCSV (instead of FeedsCSVParser) populate column names.

parent 24c755d9
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
Feeds 6.x 1.X XXXX Feeds 6.x 1.X XXXX
------------------ ------------------
- #850652 alex_b: Make ParserCSV (instead of FeedsCSVParser) populate column
names.
- #850638 alex_b: Introduce FeedsSource::preview(). - #850638 alex_b: Introduce FeedsSource::preview().
- #850298 alex_b: ParserCSV: Support batching (only affects library, full parser - #850298 alex_b: ParserCSV: Support batching (only affects library, full parser
level batch support to be added later with #744660). level batch support to be added later with #744660).
......
...@@ -11,35 +11,30 @@ class FeedsCSVParser extends FeedsParser { ...@@ -11,35 +11,30 @@ class FeedsCSVParser extends FeedsParser {
*/ */
public function parse(FeedsImportBatch $batch, FeedsSource $source) { public function parse(FeedsImportBatch $batch, FeedsSource $source) {
// Parse. // Load and configure parser.
feeds_include_library('ParserCSV.inc', 'ParserCSV'); feeds_include_library('ParserCSV.inc', 'ParserCSV');
$iterator = new ParserCSVIterator(realpath($batch->getFilePath())); $iterator = new ParserCSVIterator(realpath($batch->getFilePath()));
$source_config = $source->getConfigFor($this); $source_config = $source->getConfigFor($this);
$parser = new ParserCSV(); $parser = new ParserCSV();
$delimiter = $source_config['delimiter'] == 'TAB' ? "\t" : $source_config['delimiter']; $delimiter = $source_config['delimiter'] == 'TAB' ? "\t" : $source_config['delimiter'];
$parser->setDelimiter($delimiter); $parser->setDelimiter($delimiter);
$parser->setSkipFirstLine(FALSE);
$rows = $parser->parse($iterator);
unset($parser);
// Apply titles in lower case. // Get first line and use it for column names, convert them to lower case.
// @todo Push this functionality into ParserCSV. $parser->setLineLimit(1);
$rows = $parser->parse($iterator);
$header = array_shift($rows); $header = array_shift($rows);
foreach ($header as $i => $title) { foreach ($header as $i => $title) {
$header[$i] = trim(drupal_strtolower($title)); // Use lower case only. $header[$i] = trim(drupal_strtolower($title));
}
$result_rows = array();
foreach ($rows as $i => $row) {
$result_row = array();
foreach ($row as $j => $col) {
$result_row[$header[$j]] = $col;
}
$result_rows[$i] = $result_row;
} }
unset($rows); $parser->setColumnNames($header);
// Set line limit to 0 and start byte to last position and parse rest.
$parser->setLineLimit(0);
$parser->setStartByte($parser->lastLinePos());
$rows = $parser->parse($iterator);
// Populate batch. // Populate batch.
$batch->setItems($result_rows); $batch->setItems($rows);
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment