2015-07-22 10:12:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Import;
|
|
|
|
|
2016-02-17 11:49:56 +01:00
|
|
|
use Icinga\Exception\IcingaException;
|
|
|
|
use Icinga\Module\Director\Hook\ImportSourceHook;
|
2015-07-22 10:12:50 +02:00
|
|
|
use Icinga\Module\Director\Objects\ImportSource;
|
2015-08-28 23:47:35 +02:00
|
|
|
use Icinga\Module\Director\Util;
|
2015-10-28 22:31:37 +01:00
|
|
|
use stdClass;
|
2015-07-22 10:12:50 +02:00
|
|
|
|
|
|
|
class Import
|
|
|
|
{
|
2015-11-03 10:31:44 +01:00
|
|
|
/**
|
|
|
|
* @var ImportSource
|
|
|
|
*/
|
|
|
|
protected $source;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Icinga\Data\Db\DbConnection
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Zend_Db_Adapter_Abstract
|
|
|
|
*/
|
2015-07-22 10:12:50 +02:00
|
|
|
protected $db;
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
/**
|
|
|
|
* Raw data that should be imported, array of stdClass objects
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-11-03 10:31:44 +01:00
|
|
|
protected $data;
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
/**
|
|
|
|
* Checksum of the rowset that should be imported
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $rowsetChecksum;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checksum-indexed rows
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $rows;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checksum-indexed row -> property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $rowProperties;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this rowset exists, for caching purposes
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private $rowsetExists;
|
|
|
|
|
|
|
|
protected $properties = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checksums of all rows
|
|
|
|
*/
|
|
|
|
private $rowChecksums;
|
|
|
|
|
|
|
|
public function __construct(ImportSource $source)
|
2015-07-22 10:12:50 +02:00
|
|
|
{
|
2015-11-03 10:31:44 +01:00
|
|
|
$this->source = $source;
|
|
|
|
$this->connection = $source->getConnection();
|
2015-11-03 12:52:38 +01:00
|
|
|
$this->db = $this->connection->getDbAdapter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this import provides modified data
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function providesChanges()
|
|
|
|
{
|
|
|
|
return ! $this->rowsetExists()
|
|
|
|
|| ! $this->lastRowsetIs($this->rowsetChecksum());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger an import run
|
|
|
|
*
|
|
|
|
* @return int Last import run ID
|
|
|
|
*/
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
if ($this->providesChanges() && ! $this->rowsetExists()) {
|
|
|
|
$this->storeRowset();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->insert(
|
|
|
|
'import_run',
|
|
|
|
array(
|
|
|
|
'source_id' => $this->source->id,
|
2016-03-05 20:01:31 +01:00
|
|
|
'rowset_checksum' => $this->quoteBinary($this->rowsetChecksum()),
|
2015-11-03 12:52:38 +01:00
|
|
|
'start_time' => date('Y-m-d H:i:s'),
|
|
|
|
'succeeded' => 'y'
|
|
|
|
)
|
|
|
|
);
|
2016-03-05 20:01:31 +01:00
|
|
|
if ($this->connection->isPgsql()) {
|
|
|
|
return $this->db->lastInsertId('import_run', 'id');
|
|
|
|
} else {
|
|
|
|
return $this->db->lastInsertId();
|
|
|
|
}
|
2015-07-22 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
/**
|
|
|
|
* Whether there are no rows to be fetched from import source
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isEmpty()
|
2015-07-22 10:12:50 +02:00
|
|
|
{
|
2015-11-03 12:52:38 +01:00
|
|
|
$rows = $this->checksummedRows();
|
|
|
|
return empty($rows);
|
2015-07-22 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
/**
|
|
|
|
* Checksum of all available rows
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function & rowsetChecksum()
|
|
|
|
{
|
|
|
|
if ($this->rowsetChecksum === null) {
|
|
|
|
$this->prepareChecksummedRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->rowsetChecksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checksum of all available rows
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function & checksummedRows()
|
|
|
|
{
|
|
|
|
if ($this->rows === null) {
|
|
|
|
$this->prepareChecksummedRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checksum of all available rows
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function & rawData()
|
2015-07-22 10:12:50 +02:00
|
|
|
{
|
2015-11-03 10:31:44 +01:00
|
|
|
if ($this->data === null) {
|
|
|
|
$this->data = ImportSourceHook::loadByName(
|
|
|
|
$this->source->source_name,
|
|
|
|
$this->connection
|
|
|
|
)->fetchData();
|
|
|
|
}
|
|
|
|
|
2016-02-19 09:15:47 +01:00
|
|
|
$modifiers = $this->prepareModifiers();
|
|
|
|
|
|
|
|
if (empty($modifiers)) {
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->data as & $row) {
|
|
|
|
foreach ($modifiers as $key => $mods) {
|
|
|
|
foreach ($mods as $mod) {
|
|
|
|
if (is_array($row->$key)) {
|
|
|
|
foreach ($row->$key as & $k) {
|
|
|
|
$k = $mod->transform($k);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$row->$key = $mod->transform($row->$key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
return $this->data;
|
2015-11-03 10:31:44 +01:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
/**
|
|
|
|
* Prepare and remember an ImportedProperty
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function prepareImportedProperty($key, $rawValue)
|
2015-11-03 10:31:44 +01:00
|
|
|
{
|
2015-11-03 12:52:38 +01:00
|
|
|
if (is_array($rawValue)) {
|
|
|
|
$value = json_encode($rawValue);
|
|
|
|
$format = 'json';
|
|
|
|
} elseif ($rawValue instanceof stdClass) {
|
|
|
|
$value = json_encode($this->sortObject($rawValue));
|
|
|
|
$format = 'json';
|
|
|
|
} else {
|
|
|
|
$value = $rawValue;
|
|
|
|
$format = 'string';
|
|
|
|
}
|
|
|
|
|
|
|
|
$checksum = sha1(sprintf('%s=(%s)%s', $key, $format, $value), true);
|
|
|
|
|
|
|
|
if (! array_key_exists($checksum, $this->properties)) {
|
|
|
|
$this->properties[$checksum] = array(
|
2016-03-05 20:01:31 +01:00
|
|
|
'checksum' => $this->quoteBinary($checksum),
|
2015-11-03 12:52:38 +01:00
|
|
|
'property_name' => $key,
|
|
|
|
'property_value' => $value,
|
|
|
|
'format' => $format
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->properties[$checksum];
|
|
|
|
}
|
2015-07-22 10:12:50 +02:00
|
|
|
|
2016-02-19 09:15:47 +01:00
|
|
|
// TODO: move to ImportSource, this duplicates logic from preview
|
|
|
|
protected function prepareModifiers()
|
|
|
|
{
|
|
|
|
$modifiers = $this->source->fetchRowModifiers();
|
|
|
|
$propertyModifiers = array();
|
|
|
|
|
|
|
|
foreach ($modifiers as $mod) {
|
|
|
|
if (! array_key_exists($mod->property_name, $propertyModifiers)) {
|
|
|
|
$propertyModifiers[$mod->property_name] = array();
|
|
|
|
}
|
|
|
|
$obj = new $mod->provider_class;
|
|
|
|
$obj->setSettings($mod->getSettings());
|
2016-03-08 22:09:59 +01:00
|
|
|
$obj->setDb($this->connection);
|
2016-02-19 09:15:47 +01:00
|
|
|
$propertyModifiers[$mod->property_name][] = $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $propertyModifiers;
|
|
|
|
}
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
/**
|
|
|
|
* Walk through each row, prepare properties and calculate checksums
|
|
|
|
*/
|
|
|
|
protected function prepareChecksummedRows()
|
|
|
|
{
|
|
|
|
$keyColumn = $this->source->key_column;
|
|
|
|
$this->rows = array();
|
|
|
|
$this->rowProperties = array();
|
|
|
|
$objects = array();
|
|
|
|
$rowCount = 0;
|
2015-07-22 10:12:50 +02:00
|
|
|
|
2015-11-03 10:31:44 +01:00
|
|
|
foreach ($this->rawData() as $row) {
|
2015-11-03 12:52:38 +01:00
|
|
|
$rowCount++;
|
|
|
|
|
|
|
|
// Key column must be set
|
2015-07-22 10:12:50 +02:00
|
|
|
if (! isset($row->$keyColumn)) {
|
2015-10-21 07:45:08 +02:00
|
|
|
throw new IcingaException(
|
2015-11-06 09:20:13 +01:00
|
|
|
'No key column "%s" in row %d',
|
2015-10-21 07:45:08 +02:00
|
|
|
$keyColumn,
|
2015-11-06 09:20:13 +01:00
|
|
|
$rowCount
|
2015-07-26 15:38:29 +02:00
|
|
|
);
|
2015-07-22 10:12:50 +02:00
|
|
|
}
|
2015-07-26 15:38:29 +02:00
|
|
|
|
2015-07-22 10:12:50 +02:00
|
|
|
$object_name = $row->$keyColumn;
|
2015-11-03 12:52:38 +01:00
|
|
|
|
|
|
|
// Check for name collision
|
|
|
|
if (array_key_exists($object_name, $objects)) {
|
|
|
|
throw new IcingaException(
|
|
|
|
'Duplicate entry: %s',
|
|
|
|
$object_name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-07-22 10:12:50 +02:00
|
|
|
$rowChecksums = array();
|
|
|
|
$keys = array_keys((array) $row);
|
|
|
|
sort($keys);
|
|
|
|
|
|
|
|
foreach ($keys as $key) {
|
2015-07-24 15:26:09 +02:00
|
|
|
// TODO: Specify how to treat NULL values. Ignoring for now.
|
2015-11-03 12:52:38 +01:00
|
|
|
// One option might be to import null (checksum '(null)')
|
|
|
|
// and to provide a flag at sync time
|
2015-07-24 15:26:09 +02:00
|
|
|
if ($row->$key === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
$property = $this->prepareImportedProperty($key, $row->$key);
|
|
|
|
$rowChecksums[] = $property['checksum'];
|
2015-07-22 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$checksum = sha1($object_name . ';' . implode(';', $rowChecksums), true);
|
2015-11-03 12:52:38 +01:00
|
|
|
if (array_key_exists($checksum, $this->rows)) {
|
2015-07-22 10:12:50 +02:00
|
|
|
die('WTF, collision?');
|
|
|
|
}
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
$this->rows[$checksum] = array(
|
2016-03-05 20:01:31 +01:00
|
|
|
'checksum' => $this->quoteBinary($checksum),
|
2015-07-22 10:12:50 +02:00
|
|
|
'object_name' => $object_name
|
|
|
|
);
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
$this->rowProperties[$checksum] = $rowChecksums;
|
|
|
|
|
|
|
|
$objects[$object_name] = $checksum;
|
2015-07-22 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
$this->rowChecksums = array_keys($this->rows);
|
|
|
|
$this->rowsetChecksum = sha1(implode(';', $this->rowChecksums), true);
|
|
|
|
return $this;
|
|
|
|
}
|
2015-07-22 10:12:50 +02:00
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
/**
|
|
|
|
* Store our new rowset
|
|
|
|
*/
|
|
|
|
protected function storeRowset()
|
|
|
|
{
|
|
|
|
$db = $this->db;
|
|
|
|
$rowset = $this->rowsetChecksum();
|
|
|
|
$rows = $this->checksummedRows();
|
2015-08-28 23:47:35 +02:00
|
|
|
|
2015-07-22 10:12:50 +02:00
|
|
|
$db->beginTransaction();
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
if ($this->isEmpty()) {
|
|
|
|
$newRows = array();
|
|
|
|
$newProperties = array();
|
|
|
|
} else {
|
|
|
|
$newRows = $this->newChecksums('imported_row', $this->rowChecksums);
|
|
|
|
$newProperties = $this->newChecksums('imported_property', array_keys($this->properties));
|
|
|
|
}
|
2015-07-22 10:12:50 +02:00
|
|
|
|
2016-03-05 20:01:31 +01:00
|
|
|
$db->insert('imported_rowset', array('checksum' => $this->quoteBinary($rowset)));
|
2015-07-22 10:12:50 +02:00
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
foreach ($newProperties as $checksum) {
|
|
|
|
$db->insert('imported_property', $this->properties[$checksum]);
|
|
|
|
}
|
2015-07-22 10:12:50 +02:00
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
foreach ($newRows as $row) {
|
|
|
|
$db->insert('imported_row', $rows[$row]);
|
|
|
|
foreach ($this->rowProperties[$row] as $property) {
|
|
|
|
$db->insert('imported_row_property', array(
|
2016-03-05 20:01:31 +01:00
|
|
|
'row_checksum' => $this->quoteBinary($row),
|
2015-11-03 12:52:38 +01:00
|
|
|
'property_checksum' => $property
|
|
|
|
));
|
2015-07-22 10:12:50 +02:00
|
|
|
}
|
2015-11-03 12:52:38 +01:00
|
|
|
}
|
2015-07-22 13:13:00 +02:00
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
foreach (array_keys($rows) as $row) {
|
|
|
|
$db->insert(
|
|
|
|
'imported_rowset_row',
|
|
|
|
array(
|
2016-03-05 20:01:31 +01:00
|
|
|
'rowset_checksum' => $this->quoteBinary($rowset),
|
|
|
|
'row_checksum' => $this->quoteBinary($row)
|
2015-11-03 12:52:38 +01:00
|
|
|
)
|
|
|
|
);
|
2015-07-22 10:12:50 +02:00
|
|
|
}
|
2015-11-03 12:52:38 +01:00
|
|
|
|
2015-07-22 10:12:50 +02:00
|
|
|
$db->commit();
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
$this->rowsetExists = true;
|
2015-07-22 10:12:50 +02:00
|
|
|
}
|
|
|
|
|
2015-11-03 10:31:44 +01:00
|
|
|
/**
|
|
|
|
* Whether the last run of this import matches the given checksum
|
|
|
|
*/
|
|
|
|
protected function lastRowsetIs($checksum)
|
|
|
|
{
|
|
|
|
return $this->connection->getLatestImportedChecksum($this->source->id)
|
|
|
|
=== Util::binary2hex($checksum);
|
|
|
|
}
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
/**
|
|
|
|
* Whether our rowset already exists in the database
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
protected function rowsetExists()
|
2015-11-03 10:18:24 +01:00
|
|
|
{
|
2015-11-03 12:52:38 +01:00
|
|
|
if (null === $this->rowsetExists) {
|
|
|
|
$this->rowsetExists = 0 === count(
|
|
|
|
$this->newChecksums(
|
|
|
|
'imported_rowset',
|
|
|
|
array($this->rowsetChecksum())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->rowsetExists;
|
2015-11-03 10:18:24 +01:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:52:38 +01:00
|
|
|
/**
|
|
|
|
* Finde new checksums for a specific table
|
|
|
|
*
|
|
|
|
* Accepts an array of checksums and gives you an array with those checksums
|
|
|
|
* that are missing in the given table
|
|
|
|
*
|
|
|
|
* @param string $table Database table name
|
|
|
|
* @param array $checksums Array with the checksums that should be verified
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-11-03 10:18:24 +01:00
|
|
|
protected function newChecksums($table, $checksums)
|
|
|
|
{
|
|
|
|
$db = $this->db;
|
|
|
|
|
2015-12-10 17:17:49 +01:00
|
|
|
// TODO: The following is a quickfix for binary data corrpution reported
|
|
|
|
// in https://github.com/zendframework/zf1/issues/655 caused by
|
|
|
|
// https://github.com/zendframework/zf1/commit/2ac9c30f
|
|
|
|
//
|
|
|
|
// Should be reverted once fixed, eventually with a check continueing
|
|
|
|
// to use this workaround for specific ZF versions (1.12.16 and 1.12.17
|
|
|
|
// so far). Alternatively we could also use a custom quoteInto method.
|
|
|
|
|
|
|
|
// The former query looked as follows:
|
|
|
|
//
|
|
|
|
// $query = $db->select()->from($table, 'checksum')
|
|
|
|
// ->where('checksum IN (?)', $checksums)
|
2016-03-14 12:07:57 +01:00
|
|
|
// ...
|
|
|
|
// return array_diff($checksums, $existing);
|
2015-12-10 17:17:49 +01:00
|
|
|
|
|
|
|
$hexed = array_map('Icinga\Module\Director\Util::binary2hex', $checksums);
|
2016-03-14 12:07:57 +01:00
|
|
|
|
|
|
|
$conn = $this->connection;
|
2015-12-10 17:17:49 +01:00
|
|
|
$query = $db
|
|
|
|
->select()
|
2016-03-14 12:07:57 +01:00
|
|
|
->from(
|
|
|
|
$table,
|
|
|
|
array('checksum' => $conn->dbHexFunc('checksum'))
|
|
|
|
)->where(
|
|
|
|
$conn->dbHexFunc('checksum') . ' IN (?)',
|
2016-03-05 20:01:31 +01:00
|
|
|
$hexed
|
|
|
|
);
|
2015-12-10 17:17:49 +01:00
|
|
|
|
|
|
|
$existing = $db->fetchCol($query);
|
2016-03-14 12:07:57 +01:00
|
|
|
$new = array_diff($hexed, $existing);
|
2015-11-03 10:18:24 +01:00
|
|
|
|
2016-03-14 12:07:57 +01:00
|
|
|
return array_map('Icinga\Module\Director\Util::hex2binary', $new);
|
2015-11-03 10:18:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort a given stdClass object by property name
|
|
|
|
*/
|
2015-11-02 09:25:39 +01:00
|
|
|
protected function sortObject($object)
|
|
|
|
{
|
|
|
|
$array = (array) $object;
|
|
|
|
foreach ($array as $key => $val) {
|
|
|
|
$this->sortElement($val);
|
|
|
|
}
|
|
|
|
ksort($array);
|
|
|
|
return (object) $array;
|
|
|
|
}
|
|
|
|
|
2015-11-03 10:18:24 +01:00
|
|
|
/**
|
|
|
|
* Walk through a given array and sort all children
|
|
|
|
*
|
|
|
|
* Please note that the array itself will NOT be sorted, as arrays must
|
|
|
|
* keep their ordering
|
|
|
|
*/
|
2015-11-02 09:25:39 +01:00
|
|
|
protected function sortArrayObject(& $array)
|
|
|
|
{
|
|
|
|
foreach ($array as $key => $val) {
|
|
|
|
$this->sortElement($val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 10:18:24 +01:00
|
|
|
/**
|
|
|
|
* Recursively sort a given property
|
|
|
|
*/
|
2015-11-02 09:25:39 +01:00
|
|
|
protected function sortElement(& $el)
|
|
|
|
{
|
|
|
|
if (is_array($el)) {
|
|
|
|
$this->sortArrayObject($el);
|
|
|
|
} elseif ($el instanceof stdClass) {
|
|
|
|
$el = $this->sortObject($el);
|
|
|
|
}
|
|
|
|
}
|
2016-03-05 20:01:31 +01:00
|
|
|
|
|
|
|
protected function quoteBinary($bin)
|
|
|
|
{
|
|
|
|
return $this->connection->quoteBinary($bin);
|
|
|
|
}
|
2015-07-22 10:12:50 +02:00
|
|
|
}
|