public function MigrateSourceCSV::__construct in Migrate 6.2
Same name and namespace in other branches
- 7.2 plugins/sources/csv.inc \MigrateSourceCSV::__construct()
Simple initialization.
Parameters
string $path: The path to the source file
array $csvcolumns: Keys are integers. values are array(field name, description).
array $options: Options applied to this source.
array $fields: Optional - keys are field names, values are descriptions. Use to override the default descriptions, or to add additional source fields which the migration will add via other means (e.g., prepareRow()).
Overrides MigrateSource::__construct
File
- plugins/
sources/ csv.inc, line 57 - Define a MigrateSource for importing from comma separated values files.
Class
- MigrateSourceCSV
- Implementation of MigrateSource, to handle imports from CSV files.
Code
public function __construct($path, array $csvcolumns = array(), array $options = array(), array $fields = array()) {
parent::__construct($options);
$this->file = $path;
if (!empty($options['header_rows'])) {
$this->headerRows = $options['header_rows'];
}
else {
$this->headerRows = 0;
}
$this->options = $options;
$this->fields = $fields;
// fgetcsv specific options
foreach (array(
'length' => NULL,
'delimiter' => ',',
'enclosure' => '"',
'escape' => '\\',
) as $key => $default) {
$this->fgetcsv[$key] = isset($options[$key]) ? $options[$key] : $default;
}
// One can either pass in an explicit list of column names to use, or if we have
// a header row we can use the names from that
if ($this->headerRows && empty($csvcolumns)) {
$this->csvcolumns = array();
$this->csvHandle = fopen($this->file, 'r');
// Skip all but the last header
for ($i = 0; $i < $this->headerRows - 1; $i++) {
$this
->getNextLine();
}
$row = $this
->getNextLine();
foreach ($row as $header) {
$header = trim($header);
$this->csvcolumns[] = array(
$header,
$header,
);
}
fclose($this->csvHandle);
$this->csvHandle = NULL;
}
else {
$this->csvcolumns = $csvcolumns;
}
}