public function import($format, $data, $options = array()) { if ($format == self::FORMAT_ARRAY) { if (isset($options['replace']) && $options['replace']) { throw new Kwf_Exception_NotYetImplemented(); } $ret = $this->getCollection()->batchInsert($data, array('safe' => true)); if (!$ret || !$ret['ok']) { throw new Kwf_Exception("import failed"); } } else { parent::import($format, $data, $options); } }
public function import($format, $data, $options = array()) { if ($format == self::FORMAT_SQL) { // if no data is recieved, quit if (!$data) { return; } $filename = tempnam('/tmp', 'modelimport'); file_put_contents($filename, $data); $systemData = $this->_getSystemData(); $cmd = "gunzip -c {$filename} "; if (isset($options['replace']) && $options['replace']) { $cmd .= "| sed -e \"s|INSERT INTO|REPLACE INTO|\""; } if (isset($options['ignorePrimaryKey']) && $options['ignorePrimaryKey']) { $primaryKey = $this->getPrimaryKey(); $cmd .= " | sed -e \"s|(\\`" . $primaryKey . "\\`,|(|\" -e \"s|([0-9]*,|(|g\""; } $cmd .= "| {$systemData['mysqlDir']}mysql {$systemData['mysqlOptions']} 2>&1"; exec($cmd, $output, $ret); unlink($filename); $this->_updateModelObserver($options, null); if ($ret != 0) { throw new Kwf_Exception("SQL import failed: " . implode("\n", $output)); } $this->_afterImport($format, $data, $options); } else { if ($format == self::FORMAT_CSV) { // if no data is recieved, quit if (!$data) { return; } $tmpImportFolder = tempnam('temp/', 'modelcsvim'); unlink($tmpImportFolder); mkdir($tmpImportFolder, 0777); $filename = $tmpImportFolder . '/csvimport'; file_put_contents($filename . '.gz', $data); $cmd = "gunzip -c {$filename}.gz > {$filename}" . " && head --lines=1 {$filename} | sed -e 's|\"|`|g'"; exec($cmd, $output, $ret); if ($ret != 0) { throw new Kwf_Exception("CSV-SQL export failed"); } $fieldNames = trim($output[0]); if ($fieldNames) { // set the character_set_database MySQL system variable to utf8 $this->executeSql("SET character_set_database = 'utf8'"); $sqlString = "LOAD DATA INFILE '{$filename}'"; if (isset($options['replace']) && $options['replace']) { $sqlString .= " REPLACE"; } $sqlString .= " INTO TABLE `" . $this->getTableName() . "`"; $sqlString .= " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n'"; $sqlString .= " IGNORE 1 LINES"; $sqlString .= " ({$fieldNames})"; $this->executeSql($sqlString); } unlink($filename . '.gz'); unlink($filename); rmdir($tmpImportFolder); $this->_updateModelObserver($options, null); $this->_afterImport($format, $data, $options); } else { if ($format == self::FORMAT_ARRAY) { $ids = array(); foreach ($data as $k => $v) { if (is_array($ids) && !is_array($this->getPrimaryKey()) && isset($v[$this->getPrimaryKey()])) { $ids[] = $v[$this->getPrimaryKey()]; } else { //if we don't know all imported ids, pass null $ids = null; } } if (isset($options['buffer']) && $options['buffer']) { if (isset($this->_importBuffer)) { if ($options != $this->_importBufferOptions) { throw new Kwf_Exception_NotYetImplemented("You can't buffer imports with different options (not yet implemented)"); } $this->_importBuffer = array_merge($this->_importBuffer, $data); // handling for not sending too much data to mysql in one query // is in _importArray() function } else { $this->_importBufferOptions = $options; $this->_importBuffer = $data; } if ($this->_getBufferLogFileName()) { file_put_contents($this->_getBufferLogFileName(), count($this->_importBuffer)); } if ($options['buffer'] !== true && count($this->_importBuffer) > $options['buffer']) { $this->writeBuffer(); } } else { $this->_importArray($data, $options); } $this->_updateModelObserver($options, $ids); $this->_afterImport($format, $data, $options); } else { parent::import($format, $data); } } } }