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

#913672 andrewlevine: Break out CSV Parser into submethods so it is more easily overridable.

parent 17d40965
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,8 @@
Feeds 6.x xxxxxxxxxxxxxxxxxxxxxx
--------------------------------
- #913672 andrewlevine: Break out CSV Parser into submethods so it is more
easily overridable
- #853974 snoldak924, alex_b: Fix XSS vulnerabilities in module.
- #887846 ekes: Make FeedsSimplePieEnclosure (un)serialization safe.
- #908582 XiaN Vizjereij, alex_b: Fix "Cannot use object of type stdClass as
......
......@@ -19,25 +19,53 @@ class FeedsCSVParser extends FeedsParser {
$delimiter = $source_config['delimiter'] == 'TAB' ? "\t" : $source_config['delimiter'];
$parser->setDelimiter($delimiter);
// Get first line and use it for column names, convert them to lower case.
$header = $this->parseHeader($parser, $iterator);
if (!header) {
return;
}
$parser->setColumnNames($header);
// Populate batch.
$batch->setItems($this->parseItems($parser, $iterator));
}
/**
* Get first line and use it for column names, convert them to lower case.
* Be aware that the $parser and iterator objects can be modified in this
* function since they are passed in by reference
*
* @param ParserCSV $parser
* @param ParserCSVIterator $iterator
* @return
* An array of lower-cased column names to use as keys for the parsed items.
*/
protected function parseHeader(ParserCSV $parser, ParserCSVIterator $iterator) {
$parser->setLineLimit(1);
$rows = $parser->parse($iterator);
if (!count($rows)) {
return;
return FALSE;
}
$header = array_shift($rows);
foreach ($header as $i => $title) {
$header[$i] = trim(drupal_strtolower($title));
}
$parser->setColumnNames($header);
return $header;
}
/**
* Parse all of the items from the CSV.
*
* @param ParserCSV $parser
* @param ParserCSVIterator $iterator
* @return
* An array of rows of the CSV keyed by the column names previously set
*/
protected function parseItems(ParserCSV $parser, ParserCSVIterator $iterator) {
// 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.
$batch->setItems($rows);
return $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