2008-08-09 06:53:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Utility class to facilitate complex CSV-imports by defining column-mappings
|
|
|
|
* and custom converters.
|
2013-05-14 12:01:15 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Uses the fgetcsv() function to process CSV input. Accepts a file-handler as
|
2013-05-14 12:01:15 +02:00
|
|
|
* input.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-02-05 02:42:27 +01:00
|
|
|
* @see http://tools.ietf.org/html/rfc4180
|
2013-05-14 12:01:15 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-08-09 06:53:34 +02:00
|
|
|
* @subpackage bulkloading
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @todo Support for deleting existing records not matched in the import
|
2013-05-14 12:01:15 +02:00
|
|
|
* (through relation checks)
|
2008-08-09 06:53:34 +02:00
|
|
|
*/
|
|
|
|
class CsvBulkLoader extends BulkLoader {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
/**
|
|
|
|
* Delimiter character (Default: comma).
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $delimiter = ',';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
/**
|
|
|
|
* Enclosure character (Default: doublequote)
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $enclosure = '"';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
/**
|
2013-05-11 08:05:53 +02:00
|
|
|
* Identifies if csv the has a header row.
|
|
|
|
*
|
2008-08-09 06:53:34 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-10-28 04:17:22 +01:00
|
|
|
public $hasHeaderRow = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-30 15:00:25 +02:00
|
|
|
/**
|
|
|
|
* Number of lines to split large CSV files into.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $lines = 1000;
|
|
|
|
|
2013-03-19 11:57:51 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function preview($filepath) {
|
|
|
|
return $this->processAll($filepath, true);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-11 08:05:53 +02:00
|
|
|
/**
|
|
|
|
* @param string $filepath
|
|
|
|
* @param boolean $preview
|
2015-08-30 15:00:25 +02:00
|
|
|
*
|
|
|
|
* @return null|BulkLoader_Result
|
2013-05-11 08:05:53 +02:00
|
|
|
*/
|
2008-08-09 06:53:34 +02:00
|
|
|
protected function processAll($filepath, $preview = false) {
|
2015-11-03 01:06:39 +01:00
|
|
|
$filepath = Director::getAbsFile($filepath);
|
2015-08-30 15:00:25 +02:00
|
|
|
$files = $this->splitFile($filepath);
|
|
|
|
|
|
|
|
$result = null;
|
|
|
|
$last = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$last = $file;
|
|
|
|
|
|
|
|
$next = $this->processChunk($file, false);
|
|
|
|
|
|
|
|
if ($result instanceof BulkLoader_Result) {
|
|
|
|
$result->merge($next);
|
|
|
|
} else {
|
|
|
|
$result = $next;
|
|
|
|
}
|
|
|
|
|
|
|
|
@unlink($file);
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
print "Failed to parse {$last}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Splits a large file up into many smaller files.
|
|
|
|
*
|
|
|
|
* @param string $path Path to large file to split
|
|
|
|
* @param int $lines Number of lines per file
|
|
|
|
*
|
|
|
|
* @return array List of file paths
|
|
|
|
*/
|
|
|
|
protected function splitFile($path, $lines = null) {
|
|
|
|
$previous = ini_get('auto_detect_line_endings');
|
|
|
|
|
|
|
|
ini_set('auto_detect_line_endings', true);
|
|
|
|
|
|
|
|
if (!is_int($lines)) {
|
|
|
|
$lines = $this->config()->get("lines");
|
|
|
|
}
|
|
|
|
|
|
|
|
$new = $this->getNewSplitFileName();
|
|
|
|
|
|
|
|
$to = fopen($new, 'w+');
|
|
|
|
$from = fopen($path, 'r');
|
|
|
|
|
|
|
|
$header = null;
|
|
|
|
|
|
|
|
if ($this->hasHeaderRow) {
|
|
|
|
$header = fgets($from);
|
|
|
|
fwrite($to, $header);
|
|
|
|
}
|
|
|
|
|
|
|
|
$files = array();
|
|
|
|
$files[] = $new;
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
while (!feof($from)) {
|
|
|
|
fwrite($to, fgets($from));
|
|
|
|
|
|
|
|
$count++;
|
|
|
|
|
|
|
|
if ($count >= $lines) {
|
|
|
|
fclose($to);
|
|
|
|
|
|
|
|
// get a new temporary file name, to write the next lines to
|
|
|
|
$new = $this->getNewSplitFileName();
|
|
|
|
|
|
|
|
$to = fopen($new, 'w+');
|
|
|
|
|
|
|
|
if ($this->hasHeaderRow) {
|
|
|
|
// add the headers to the new file
|
|
|
|
fwrite($to, $header);
|
|
|
|
}
|
|
|
|
|
|
|
|
$files[] = $new;
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($to);
|
|
|
|
|
|
|
|
ini_set('auto_detect_line_endings', $previous);
|
|
|
|
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getNewSplitFileName() {
|
|
|
|
return TEMP_FOLDER . '/' . uniqid('BulkLoader', true) . '.csv';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $filepath
|
|
|
|
* @param boolean $preview
|
|
|
|
*
|
|
|
|
* @return BulkLoader_Result
|
|
|
|
*/
|
|
|
|
protected function processChunk($filepath, $preview = false) {
|
2008-10-03 02:27:58 +02:00
|
|
|
$results = new BulkLoader_Result();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-11 08:05:53 +02:00
|
|
|
$csv = new CSVParser(
|
2014-08-15 08:53:05 +02:00
|
|
|
$filepath,
|
|
|
|
$this->delimiter,
|
2013-05-11 08:05:53 +02:00
|
|
|
$this->enclosure
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 02:27:58 +02:00
|
|
|
// ColumnMap has two uses, depending on whether hasHeaderRow is set
|
|
|
|
if($this->columnMap) {
|
2013-05-11 08:05:53 +02:00
|
|
|
// if the map goes to a callback, use the same key value as the map
|
2014-08-15 08:53:05 +02:00
|
|
|
// value, rather than function name as multiple keys may use the
|
2013-05-11 08:05:53 +02:00
|
|
|
// same callback
|
|
|
|
foreach($this->columnMap as $k => $v) {
|
|
|
|
if(strpos($v, "->") === 0) {
|
|
|
|
$map[$k] = $k;
|
|
|
|
} else {
|
|
|
|
$map[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->hasHeaderRow) {
|
|
|
|
$csv->mapColumns($map);
|
|
|
|
} else {
|
|
|
|
$csv->provideHeaderRow($map);
|
|
|
|
}
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-03 02:27:58 +02:00
|
|
|
foreach($csv as $row) {
|
2008-11-18 02:48:37 +01:00
|
|
|
$this->processRecord($row, $this->columnMap, $results, $preview);
|
2008-10-03 02:27:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-15 16:21:43 +02:00
|
|
|
return $results;
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-15 16:21:43 +02:00
|
|
|
/**
|
|
|
|
* @todo Better messages for relation checks and duplicate detection
|
2013-05-14 12:01:15 +02:00
|
|
|
* Note that columnMap isn't used.
|
|
|
|
*
|
|
|
|
* @param array $record
|
|
|
|
* @param array $columnMap
|
|
|
|
* @param BulkLoader_Result $results
|
|
|
|
* @param boolean $preview
|
|
|
|
*
|
|
|
|
* @return int
|
2008-09-15 16:21:43 +02:00
|
|
|
*/
|
|
|
|
protected function processRecord($record, $columnMap, &$results, $preview = false) {
|
2008-08-09 06:53:34 +02:00
|
|
|
$class = $this->objectClass;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
// find existing object, or create new one
|
2008-08-09 07:45:43 +02:00
|
|
|
$existingObj = $this->findExistingObject($record, $columnMap);
|
2014-08-15 08:53:05 +02:00
|
|
|
$obj = ($existingObj) ? $existingObj : new $class();
|
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
// first run: find/create any relations and store them on the object
|
|
|
|
// we can't combine runs, as other columns might rely on the relation being present
|
|
|
|
$relations = array();
|
2008-10-03 02:27:58 +02:00
|
|
|
foreach($record as $fieldName => $val) {
|
2008-08-09 07:45:43 +02:00
|
|
|
// don't bother querying of value is not set
|
|
|
|
if($this->isNullValue($val)) continue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
// checking for existing relations
|
2008-08-09 07:45:43 +02:00
|
|
|
if(isset($this->relationCallbacks[$fieldName])) {
|
2008-08-09 06:53:34 +02:00
|
|
|
// trigger custom search method for finding a relation based on the given value
|
|
|
|
// and write it back to the relation (or create a new object)
|
2008-08-09 07:45:43 +02:00
|
|
|
$relationName = $this->relationCallbacks[$fieldName]['relationname'];
|
2008-11-22 04:33:00 +01:00
|
|
|
if($this->hasMethod($this->relationCallbacks[$fieldName]['callback'])) {
|
2008-12-04 23:38:32 +01:00
|
|
|
$relationObj = $this->{$this->relationCallbacks[$fieldName]['callback']}($obj, $val, $record);
|
2008-11-22 04:33:00 +01:00
|
|
|
} elseif($obj->hasMethod($this->relationCallbacks[$fieldName]['callback'])) {
|
|
|
|
$relationObj = $obj->{$this->relationCallbacks[$fieldName]['callback']}($val, $record);
|
|
|
|
}
|
2008-08-09 06:53:34 +02:00
|
|
|
if(!$relationObj || !$relationObj->exists()) {
|
2015-02-25 16:21:33 +01:00
|
|
|
$relationClass = $obj->hasOneComponent($relationName);
|
2008-08-09 06:53:34 +02:00
|
|
|
$relationObj = new $relationClass();
|
2013-03-19 12:23:58 +01:00
|
|
|
//write if we aren't previewing
|
|
|
|
if (!$preview) $relationObj->write();
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
|
|
|
$obj->{"{$relationName}ID"} = $relationObj->ID;
|
2013-03-19 12:23:58 +01:00
|
|
|
//write if we are not previewing
|
|
|
|
if (!$preview) {
|
2013-04-19 05:45:43 +02:00
|
|
|
$obj->write();
|
|
|
|
$obj->flushCache(); // avoid relation caching confusion
|
2013-03-19 12:23:58 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 07:45:43 +02:00
|
|
|
} elseif(strpos($fieldName, '.') !== false) {
|
2008-08-09 06:53:34 +02:00
|
|
|
// we have a relation column with dot notation
|
2013-04-19 05:45:43 +02:00
|
|
|
list($relationName, $columnName) = explode('.', $fieldName);
|
2012-09-26 23:34:00 +02:00
|
|
|
// always gives us an component (either empty or existing)
|
|
|
|
$relationObj = $obj->getComponent($relationName);
|
2013-03-19 12:23:58 +01:00
|
|
|
if (!$preview) $relationObj->write();
|
2008-08-09 06:53:34 +02:00
|
|
|
$obj->{"{$relationName}ID"} = $relationObj->ID;
|
2013-05-14 12:01:15 +02:00
|
|
|
|
2013-03-19 12:23:58 +01:00
|
|
|
//write if we are not previewing
|
|
|
|
if (!$preview) {
|
2013-04-19 05:45:43 +02:00
|
|
|
$obj->write();
|
|
|
|
$obj->flushCache(); // avoid relation caching confusion
|
|
|
|
}
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// second run: save data
|
2013-05-11 08:05:53 +02:00
|
|
|
|
2008-10-03 02:27:58 +02:00
|
|
|
foreach($record as $fieldName => $val) {
|
2013-05-11 08:05:53 +02:00
|
|
|
// break out of the loop if we are previewing
|
|
|
|
if ($preview) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// look up the mapping to see if this needs to map to callback
|
|
|
|
$mapped = $this->columnMap && isset($this->columnMap[$fieldName]);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-11 08:05:53 +02:00
|
|
|
if($mapped && strpos($this->columnMap[$fieldName], '->') === 0) {
|
|
|
|
$funcName = substr($this->columnMap[$fieldName], 2);
|
|
|
|
|
2008-11-18 02:48:37 +01:00
|
|
|
$this->$funcName($obj, $val, $record);
|
|
|
|
} else if($obj->hasMethod("import{$fieldName}")) {
|
2008-08-09 07:45:43 +02:00
|
|
|
$obj->{"import{$fieldName}"}($val, $record);
|
|
|
|
} else {
|
2008-10-03 02:27:58 +02:00
|
|
|
$obj->update(array($fieldName => $val));
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-18 02:48:37 +01:00
|
|
|
|
2008-09-15 16:21:43 +02:00
|
|
|
// write record
|
2008-08-09 06:53:34 +02:00
|
|
|
$id = ($preview) ? 0 : $obj->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-15 16:21:43 +02:00
|
|
|
// @todo better message support
|
2008-08-09 06:53:34 +02:00
|
|
|
$message = '';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-15 16:21:43 +02:00
|
|
|
// save to results
|
|
|
|
if($existingObj) {
|
|
|
|
$results->addUpdated($obj, $message);
|
|
|
|
} else {
|
|
|
|
$results->addCreated($obj, $message);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
$objID = $obj->ID;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
$obj->destroy();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
// memory usage
|
2008-08-09 07:00:42 +02:00
|
|
|
unset($existingObj);
|
2008-08-09 06:53:34 +02:00
|
|
|
unset($obj);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-04 04:44:11 +01:00
|
|
|
return $objID;
|
2008-08-09 06:53:34 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Find an existing objects based on one or more uniqueness columns
|
2013-05-14 12:01:15 +02:00
|
|
|
* specified via {@link self::$duplicateChecks}.
|
2008-08-09 07:00:42 +02:00
|
|
|
*
|
|
|
|
* @param array $record CSV data column
|
2013-05-14 12:01:15 +02:00
|
|
|
*
|
|
|
|
* @return mixed
|
2008-08-09 07:00:42 +02:00
|
|
|
*/
|
2008-10-03 02:27:58 +02:00
|
|
|
public function findExistingObject($record) {
|
2009-02-18 03:37:00 +01:00
|
|
|
$SNG_objectClass = singleton($this->objectClass);
|
2008-08-09 07:00:42 +02:00
|
|
|
// checking for existing records (only if not already found)
|
2013-05-14 12:01:15 +02:00
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
foreach($this->duplicateChecks as $fieldName => $duplicateCheck) {
|
|
|
|
if(is_string($duplicateCheck)) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Skip current duplicate check if field value is empty
|
|
|
|
if(empty($record[$duplicateCheck])) continue;
|
2013-05-14 12:01:15 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Check existing record with this value
|
|
|
|
$dbFieldValue = $record[$duplicateCheck];
|
|
|
|
$existingRecord = DataObject::get($this->objectClass)
|
|
|
|
->filter($duplicateCheck, $dbFieldValue)
|
|
|
|
->first();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
if($existingRecord) return $existingRecord;
|
2008-08-09 07:00:42 +02:00
|
|
|
} elseif(is_array($duplicateCheck) && isset($duplicateCheck['callback'])) {
|
2009-02-18 03:37:00 +01:00
|
|
|
if($this->hasMethod($duplicateCheck['callback'])) {
|
|
|
|
$existingRecord = $this->{$duplicateCheck['callback']}($record[$fieldName], $record);
|
|
|
|
} elseif($SNG_objectClass->hasMethod($duplicateCheck['callback'])) {
|
|
|
|
$existingRecord = $SNG_objectClass->{$duplicateCheck['callback']}($record[$fieldName], $record);
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("CsvBulkLoader::processRecord():"
|
|
|
|
. " {$duplicateCheck['callback']} not found on importer or object class.", E_USER_ERROR);
|
2009-02-18 03:37:00 +01:00
|
|
|
}
|
2013-05-14 12:01:15 +02:00
|
|
|
|
2013-04-19 05:45:43 +02:00
|
|
|
if($existingRecord) {
|
|
|
|
return $existingRecord;
|
|
|
|
}
|
2008-08-09 07:00:42 +02:00
|
|
|
} else {
|
2009-02-18 03:37:00 +01:00
|
|
|
user_error('CsvBulkLoader::processRecord(): Wrong format for $duplicateChecks', E_USER_ERROR);
|
2008-08-09 07:00:42 +02:00
|
|
|
}
|
|
|
|
}
|
2013-05-14 12:01:15 +02:00
|
|
|
|
2008-08-09 07:00:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Determine whether any loaded files should be parsed with a
|
2013-05-14 12:01:15 +02:00
|
|
|
* header-row (otherwise we rely on {@link self::$columnMap}.
|
2008-08-09 06:53:34 +02:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function hasHeaderRow() {
|
|
|
|
return ($this->hasHeaderRow || isset($this->columnMap));
|
|
|
|
}
|
|
|
|
}
|