summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Ryan2011-08-02 19:54:27 (GMT)
committer Mike Ryan2011-08-02 19:54:27 (GMT)
commit383b7eff3881032636666f4b2eb5c1b5e2f25c57 (patch)
tree204b170cf1eb60a84b9e8b5c6734018acf7a9780
parentab89273803905fc0790bfb668a93ffb2f782cbed (diff)
Issue #1185632 by mikeryan: Handle (potentially multiple) headers in CSV source plugin
-rw-r--r--CHANGELOG.txt2
-rw-r--r--plugins/sources/csv.inc26
2 files changed, 16 insertions, 12 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 35acb86..6ddbf6c 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -4,7 +4,7 @@ Next release
Features and enhancements
- #1226768 - Improved exception handling.
-- #1185632 - Handle headers in CSV source plugin.
+- #1185632 - Handle (potentially multiple) headers in CSV source plugin.
Bug fixes
- #1227130 - On import, clear messages before applyMappings().
diff --git a/plugins/sources/csv.inc b/plugins/sources/csv.inc
index daca683..5a85d10 100644
--- a/plugins/sources/csv.inc
+++ b/plugins/sources/csv.inc
@@ -33,11 +33,11 @@ class MigrateSourceCSV extends MigrateSource {
protected $usingHighwater = FALSE;
/**
- * If TRUE, the first row of the CSV file is taken as the header.
+ * The number of rows in the CSV file before the data starts.
*
- * @var boolean
+ * @var integer
*/
- protected $headerRow = FALSE;
+ protected $headerRows = 0;
/**
* Simple initialization.
@@ -56,11 +56,11 @@ class MigrateSourceCSV extends MigrateSource {
public function __construct($path, array $csvcolumns = array(), array $options = array(), array $fields = array()) {
parent::__construct($options);
$this->file = $path;
- if (!empty($options['header_row'])) {
- $this->headerRow = TRUE;
+ if (!empty($options['header_rows'])) {
+ $this->headerRows = $options['header_rows'];
}
else {
- $this->headerRow = FALSE;
+ $this->headerRows = 0;
}
$this->options = $options;
$this->fields = $fields;
@@ -70,12 +70,18 @@ class MigrateSourceCSV extends MigrateSource {
}
// One can either pass in an explicit list of column names to use, or if we have
// a header row use the names from that
- if ($this->headerRow && empty($csvcolumns)) {
+ if ($this->headerRows && empty($csvcolumns)) {
$this->csvcolumns = array();
$result = fopen($this->file, 'r');
+ // Skip all but the last header
+ for ($i = 0; $i < $this->headerRows - 1; $i++) {
+ fgets($this->result);
+ }
+
$row = fgetcsv($result, $this->fgetcsv['length'], $this->fgetcsv['delimiter'],
$this->fgetcsv['enclosure']);
foreach ($row as $header) {
+ $header = trim($header);
$this->csvcolumns[] = array($header, $header);
}
fclose($result);
@@ -122,9 +128,7 @@ class MigrateSourceCSV extends MigrateSource {
public function computeCount() {
// TODO. If this takes too much time/memory, use exec('wc -l')
$count = count(file($this->file));
- if ($this->headerRow) {
- $count--;
- }
+ $count -= $this->headerRows;
return $count;
}
@@ -143,7 +147,7 @@ class MigrateSourceCSV extends MigrateSource {
migrate_instrument_stop('MigrateSourceCSV execute');
// Load up the first row, skipping the header if necessary
- if ($this->headerRow) {
+ for ($i = 0; $i < $this->headerRows; $i++) {
fgets($this->result);
}
$this->next();