From f8062437be067768041954b1ed0cbd690e9099f5 Mon Sep 17 00:00:00 2001
From: Alex Barth <alex_b@53995.no-reply.drupal.org>
Date: Sun, 11 Jul 2010 01:36:39 +0000
Subject: [PATCH] #850652 alex_b: Make ParserCSV (instead of FeedsCSVParser)
 populate column names.

---
 CHANGELOG.txt              |  2 ++
 plugins/FeedsCSVParser.inc | 29 ++++++++++++-----------------
 2 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 4df33105..b92434cb 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -3,6 +3,8 @@
 Feeds 6.x 1.X XXXX
 ------------------
 
+- #850652 alex_b: Make ParserCSV (instead of FeedsCSVParser) populate column
+  names.
 - #850638 alex_b: Introduce FeedsSource::preview().
 - #850298 alex_b: ParserCSV: Support batching (only affects library, full parser
   level batch support to be added later with #744660).
diff --git a/plugins/FeedsCSVParser.inc b/plugins/FeedsCSVParser.inc
index 6b0ae31c..96e74718 100644
--- a/plugins/FeedsCSVParser.inc
+++ b/plugins/FeedsCSVParser.inc
@@ -11,35 +11,30 @@ class FeedsCSVParser extends FeedsParser {
    */
   public function parse(FeedsImportBatch $batch, FeedsSource $source) {
 
-    // Parse.
+    // Load and configure parser.
     feeds_include_library('ParserCSV.inc', 'ParserCSV');
     $iterator = new ParserCSVIterator(realpath($batch->getFilePath()));
     $source_config = $source->getConfigFor($this);
     $parser = new ParserCSV();
     $delimiter = $source_config['delimiter'] == 'TAB' ? "\t" : $source_config['delimiter'];
     $parser->setDelimiter($delimiter);
-    $parser->setSkipFirstLine(FALSE);
-    $rows = $parser->parse($iterator);
-    unset($parser);
 
-    // Apply titles in lower case.
-    // @todo Push this functionality into ParserCSV.
+    // Get first line and use it for column names, convert them to lower case.
+    $parser->setLineLimit(1);
+    $rows = $parser->parse($iterator);
     $header = array_shift($rows);
     foreach ($header as $i => $title) {
-      $header[$i] = trim(drupal_strtolower($title)); // Use lower case only.
-    }
-    $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;
+      $header[$i] = trim(drupal_strtolower($title));
     }
-    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.
-    $batch->setItems($result_rows);
+    $batch->setItems($rows);
   }
 
   /**
-- 
GitLab