2015-07-21 09:16:18 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Objects;
|
|
|
|
|
|
2016-04-22 09:31:02 -04:00
|
|
|
use Icinga\Application\Benchmark;
|
2023-05-25 09:44:56 -04:00
|
|
|
use Icinga\Data\Filter\Filter;
|
2016-07-13 04:19:03 -04:00
|
|
|
use Icinga\Exception\NotFoundError;
|
2019-09-25 07:09:19 -04:00
|
|
|
use Icinga\Module\Director\Application\MemoryLimit;
|
2016-02-17 15:23:24 -05:00
|
|
|
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
|
2018-05-29 06:34:18 -04:00
|
|
|
use Icinga\Module\Director\Db;
|
2018-10-06 11:13:53 -04:00
|
|
|
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
|
2018-06-11 15:09:15 -04:00
|
|
|
use Icinga\Module\Director\Exception\DuplicateKeyException;
|
2023-05-25 09:44:56 -04:00
|
|
|
use Icinga\Module\Director\Filter\FilterEnrichment;
|
2017-05-03 04:39:49 -04:00
|
|
|
use Icinga\Module\Director\Hook\PropertyModifierHook;
|
2016-04-22 09:31:02 -04:00
|
|
|
use Icinga\Module\Director\Import\Import;
|
2016-10-27 15:01:49 -04:00
|
|
|
use Icinga\Module\Director\Import\SyncUtils;
|
2018-10-08 00:29:33 -04:00
|
|
|
use InvalidArgumentException;
|
2016-04-22 09:31:02 -04:00
|
|
|
use Exception;
|
2015-07-21 09:16:18 -04:00
|
|
|
|
2018-10-06 11:13:53 -04:00
|
|
|
class ImportSource extends DbObjectWithSettings implements ExportInterface
|
2015-07-21 09:16:18 -04:00
|
|
|
{
|
|
|
|
|
protected $table = 'import_source';
|
|
|
|
|
|
2018-10-06 11:13:53 -04:00
|
|
|
protected $keyName = 'source_name';
|
2015-07-21 09:16:18 -04:00
|
|
|
|
|
|
|
|
protected $autoincKeyName = 'id';
|
|
|
|
|
|
2018-10-08 02:37:02 -04:00
|
|
|
protected $protectAutoinc = false;
|
|
|
|
|
|
2018-05-29 06:34:18 -04:00
|
|
|
protected $defaultProperties = [
|
2016-04-22 09:31:02 -04:00
|
|
|
'id' => null,
|
|
|
|
|
'source_name' => null,
|
|
|
|
|
'provider_class' => null,
|
|
|
|
|
'key_column' => null,
|
2016-05-02 04:44:12 -04:00
|
|
|
'import_state' => 'unknown',
|
2016-04-22 09:31:02 -04:00
|
|
|
'last_error_message' => null,
|
|
|
|
|
'last_attempt' => null,
|
2017-07-14 08:27:05 -04:00
|
|
|
'description' => null,
|
2018-05-29 06:34:18 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $stateProperties = [
|
|
|
|
|
'import_state',
|
|
|
|
|
'last_error_message',
|
|
|
|
|
'last_attempt',
|
|
|
|
|
];
|
2015-07-21 09:16:18 -04:00
|
|
|
|
2016-02-17 15:23:24 -05:00
|
|
|
protected $settingsTable = 'import_source_setting';
|
2015-07-21 09:16:18 -04:00
|
|
|
|
2016-02-17 15:23:24 -05:00
|
|
|
protected $settingsRemoteId = 'source_id';
|
2016-02-18 20:47:12 -05:00
|
|
|
|
2016-07-20 07:40:43 -04:00
|
|
|
private $rowModifiers;
|
|
|
|
|
|
2019-09-19 18:10:30 -04:00
|
|
|
private $loadedRowModifiers;
|
|
|
|
|
|
2018-06-11 15:09:15 -04:00
|
|
|
private $newRowModifiers;
|
|
|
|
|
|
2019-09-19 18:10:30 -04:00
|
|
|
public function setModifiers(array $modifiers)
|
|
|
|
|
{
|
2019-11-05 10:49:59 -05:00
|
|
|
if ($this->loadedRowModifiers === null && $this->hasBeenLoadedFromDb()) {
|
2019-09-19 18:10:30 -04:00
|
|
|
$this->loadedRowModifiers = $this->fetchRowModifiers();
|
|
|
|
|
}
|
2020-01-09 08:15:50 -05:00
|
|
|
$current = (array) $this->loadedRowModifiers;
|
|
|
|
|
if (\count($current) !== \count($modifiers)) {
|
2019-09-19 18:10:30 -04:00
|
|
|
$this->newRowModifiers = $modifiers;
|
|
|
|
|
} else {
|
|
|
|
|
$i = 0;
|
|
|
|
|
$modified = false;
|
|
|
|
|
foreach ($modifiers as $props) {
|
|
|
|
|
$this->loadedRowModifiers[$i]->setProperties((array) $props);
|
|
|
|
|
if ($this->loadedRowModifiers[$i]->hasBeenModified()) {
|
|
|
|
|
$modified = true;
|
|
|
|
|
}
|
2020-01-09 08:08:52 -05:00
|
|
|
$i++;
|
2019-09-19 18:10:30 -04:00
|
|
|
}
|
|
|
|
|
if ($modified) {
|
|
|
|
|
$this->newRowModifiers = $modifiers;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function hasBeenModified()
|
|
|
|
|
{
|
|
|
|
|
return $this->newRowModifiers !== null
|
|
|
|
|
|| parent::hasBeenModified();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-06 11:13:53 -04:00
|
|
|
public function getUniqueIdentifier()
|
|
|
|
|
{
|
|
|
|
|
return $this->get('source_name');
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-08 00:29:33 -04:00
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param Db $connection
|
|
|
|
|
* @return ImportSource
|
|
|
|
|
* @throws NotFoundError
|
|
|
|
|
*/
|
2018-06-11 15:09:15 -04:00
|
|
|
public static function loadByName($name, Db $connection)
|
2018-05-29 06:34:18 -04:00
|
|
|
{
|
2018-06-11 15:09:15 -04:00
|
|
|
$db = $connection->getDbAdapter();
|
|
|
|
|
$properties = $db->fetchRow(
|
|
|
|
|
$db->select()->from('import_source')->where('source_name = ?', $name)
|
|
|
|
|
);
|
2018-10-08 00:29:33 -04:00
|
|
|
if ($properties === false) {
|
|
|
|
|
throw new NotFoundError(sprintf(
|
|
|
|
|
'There is no such Import Source: "%s"',
|
|
|
|
|
$name
|
|
|
|
|
));
|
|
|
|
|
}
|
2018-06-11 15:09:15 -04:00
|
|
|
|
|
|
|
|
return static::create([], $connection)->setDbProperties($properties);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-12 15:45:11 -04:00
|
|
|
public static function existsWithName($name, Db $connection)
|
2018-06-11 15:09:15 -04:00
|
|
|
{
|
|
|
|
|
$db = $connection->getDbAdapter();
|
|
|
|
|
|
|
|
|
|
return (string) $name === (string) $db->fetchOne(
|
|
|
|
|
$db->select()
|
|
|
|
|
->from('import_source', 'source_name')
|
|
|
|
|
->where('source_name = ?', $name)
|
|
|
|
|
);
|
2018-05-29 06:34:18 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-08 00:29:33 -04:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param int $id
|
|
|
|
|
* @param Db $connection
|
|
|
|
|
* @api internal
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2018-05-29 06:34:18 -04:00
|
|
|
protected static function existsWithNameAndId($name, $id, Db $connection)
|
|
|
|
|
{
|
|
|
|
|
$db = $connection->getDbAdapter();
|
2024-10-22 08:31:14 -04:00
|
|
|
$dummy = new static();
|
2018-10-08 00:29:33 -04:00
|
|
|
$idCol = $dummy->autoincKeyName;
|
|
|
|
|
$keyCol = $dummy->keyName;
|
2018-05-29 06:34:18 -04:00
|
|
|
|
|
|
|
|
return (string) $id === (string) $db->fetchOne(
|
|
|
|
|
$db->select()
|
2018-10-08 00:29:33 -04:00
|
|
|
->from($dummy->table, $idCol)
|
|
|
|
|
->where("$idCol = ?", $id)
|
|
|
|
|
->where("$keyCol = ?", $name)
|
2018-05-29 06:34:18 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 03:49:34 -04:00
|
|
|
/**
|
|
|
|
|
* @deprecated please use \Icinga\Module\Director\Data\FieldReferenceLoader
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2018-05-29 06:34:18 -04:00
|
|
|
protected function exportRowModifiers()
|
|
|
|
|
{
|
|
|
|
|
$modifiers = [];
|
|
|
|
|
foreach ($this->fetchRowModifiers() as $modifier) {
|
|
|
|
|
$modifiers[] = $modifier->export();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $modifiers;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-18 04:48:58 -04:00
|
|
|
/**
|
|
|
|
|
* @param bool $required
|
|
|
|
|
* @return ImportRun|null
|
2018-10-08 00:29:33 -04:00
|
|
|
* @throws NotFoundError
|
2017-08-18 04:48:58 -04:00
|
|
|
*/
|
2016-07-13 04:19:03 -04:00
|
|
|
public function fetchLastRun($required = false)
|
2016-06-24 05:48:54 -04:00
|
|
|
{
|
2016-07-13 04:19:03 -04:00
|
|
|
return $this->fetchLastRunBefore(time() + 1, $required);
|
2016-06-24 05:48:54 -04:00
|
|
|
}
|
|
|
|
|
|
2017-08-18 04:48:58 -04:00
|
|
|
/**
|
2018-06-11 15:09:15 -04:00
|
|
|
* @throws DuplicateKeyException
|
|
|
|
|
*/
|
|
|
|
|
protected function onStore()
|
|
|
|
|
{
|
|
|
|
|
parent::onStore();
|
|
|
|
|
if ($this->newRowModifiers !== null) {
|
|
|
|
|
$connection = $this->getConnection();
|
|
|
|
|
$db = $connection->getDbAdapter();
|
|
|
|
|
$myId = $this->get('id');
|
|
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
|
|
|
|
$db->delete(
|
|
|
|
|
'import_row_modifier',
|
|
|
|
|
$db->quoteInto('source_id = ?', $myId)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($this->newRowModifiers as $modifier) {
|
|
|
|
|
$modifier = ImportRowModifier::create((array) $modifier, $connection);
|
|
|
|
|
$modifier->set('source_id', $myId);
|
|
|
|
|
$modifier->store();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-18 04:48:58 -04:00
|
|
|
* @param $timestamp
|
|
|
|
|
* @param bool $required
|
|
|
|
|
* @return ImportRun|null
|
2018-10-08 00:29:33 -04:00
|
|
|
* @throws NotFoundError
|
2017-08-18 04:48:58 -04:00
|
|
|
*/
|
2016-07-13 04:19:03 -04:00
|
|
|
public function fetchLastRunBefore($timestamp, $required = false)
|
2016-06-24 05:48:54 -04:00
|
|
|
{
|
|
|
|
|
if (! $this->hasBeenLoadedFromDb()) {
|
2016-07-13 04:19:03 -04:00
|
|
|
return $this->nullUnlessRequired($required);
|
2016-06-24 05:48:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($timestamp === null) {
|
|
|
|
|
$timestamp = time();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$db = $this->getDb();
|
|
|
|
|
$query = $db->select()->from(
|
2018-05-29 06:34:18 -04:00
|
|
|
['ir' => 'import_run'],
|
2016-06-24 05:48:54 -04:00
|
|
|
'ir.id'
|
2018-10-08 00:29:33 -04:00
|
|
|
)->where('ir.source_id = ?', $this->get('id'))
|
2016-06-24 11:01:47 -04:00
|
|
|
->where('ir.start_time < ?', date('Y-m-d H:i:s', $timestamp))
|
|
|
|
|
->order('ir.start_time DESC')
|
2016-06-24 05:48:54 -04:00
|
|
|
->limit(1);
|
|
|
|
|
|
|
|
|
|
$runId = $db->fetchOne($query);
|
|
|
|
|
|
|
|
|
|
if ($runId) {
|
2016-06-24 11:01:47 -04:00
|
|
|
return ImportRun::load($runId, $this->getConnection());
|
2016-06-24 05:48:54 -04:00
|
|
|
} else {
|
2016-07-13 04:19:03 -04:00
|
|
|
return $this->nullUnlessRequired($required);
|
2016-06-24 05:48:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-08 00:29:33 -04:00
|
|
|
/**
|
|
|
|
|
* @param $required
|
|
|
|
|
* @return null
|
|
|
|
|
* @throws NotFoundError
|
|
|
|
|
*/
|
2016-07-13 04:19:03 -04:00
|
|
|
protected function nullUnlessRequired($required)
|
|
|
|
|
{
|
|
|
|
|
if ($required) {
|
|
|
|
|
throw new NotFoundError(
|
|
|
|
|
'No data has been imported for "%s" yet',
|
2018-10-08 00:29:33 -04:00
|
|
|
$this->get('source_name')
|
2016-07-13 04:19:03 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 00:52:09 -05:00
|
|
|
public function applyModifiers(&$data)
|
2016-07-20 08:25:18 -04:00
|
|
|
{
|
2018-01-26 09:20:33 -05:00
|
|
|
$modifiers = $this->fetchFlatRowModifiers();
|
|
|
|
|
|
|
|
|
|
if (empty($modifiers)) {
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2016-07-20 08:25:18 -04:00
|
|
|
|
2018-01-26 09:20:33 -05:00
|
|
|
foreach ($modifiers as $modPair) {
|
|
|
|
|
/** @var PropertyModifierHook $modifier */
|
2023-05-25 09:44:56 -04:00
|
|
|
/** @var ?Filter $filter */
|
|
|
|
|
list($property, $modifier, $filter) = $modPair;
|
2018-01-26 09:20:33 -05:00
|
|
|
$rejected = [];
|
2020-01-17 05:32:04 -05:00
|
|
|
$newRows = [];
|
2018-01-26 09:20:33 -05:00
|
|
|
foreach ($data as $key => $row) {
|
2023-05-25 09:44:56 -04:00
|
|
|
if ($filter && ! $filter->matches($row)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-01-26 09:20:33 -05:00
|
|
|
$this->applyPropertyModifierToRow($modifier, $property, $row);
|
|
|
|
|
if ($modifier->rejectsRow()) {
|
2018-01-26 07:04:32 -05:00
|
|
|
$rejected[] = $key;
|
2018-01-26 09:20:33 -05:00
|
|
|
$modifier->rejectRow(false);
|
2018-01-25 06:16:49 -05:00
|
|
|
}
|
2020-01-17 05:32:04 -05:00
|
|
|
if ($modifier->expandsRows()) {
|
|
|
|
|
$target = $modifier->getTargetProperty($property);
|
|
|
|
|
|
|
|
|
|
$newValue = $row->$target;
|
|
|
|
|
if (\is_array($newValue)) {
|
|
|
|
|
foreach ($newValue as $val) {
|
|
|
|
|
$newRow = clone $row;
|
|
|
|
|
$newRow->$target = $val;
|
|
|
|
|
$newRows[] = $newRow;
|
|
|
|
|
}
|
|
|
|
|
$rejected[] = $key;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-20 08:25:18 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-26 09:20:33 -05:00
|
|
|
foreach ($rejected as $key) {
|
|
|
|
|
unset($data[$key]);
|
|
|
|
|
}
|
2020-01-17 05:32:04 -05:00
|
|
|
foreach ($newRows as $row) {
|
|
|
|
|
$data[] = $row;
|
|
|
|
|
}
|
2018-01-25 06:16:49 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-20 08:25:18 -04:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-29 06:34:18 -04:00
|
|
|
public function getObjectName()
|
|
|
|
|
{
|
|
|
|
|
return $this->get('source_name');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getKeyColumnName()
|
|
|
|
|
{
|
|
|
|
|
return 'source_name';
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 09:20:33 -05:00
|
|
|
protected function applyPropertyModifierToRow(PropertyModifierHook $modifier, $key, $row)
|
2018-01-25 04:58:59 -05:00
|
|
|
{
|
2019-09-19 11:24:31 -04:00
|
|
|
if (! is_object($row)) {
|
|
|
|
|
throw new InvalidArgumentException('Every imported row MUST be an object');
|
|
|
|
|
}
|
2018-01-25 04:58:59 -05:00
|
|
|
if ($modifier->requiresRow()) {
|
|
|
|
|
$modifier->setRow($row);
|
|
|
|
|
}
|
2016-07-20 09:21:07 -04:00
|
|
|
|
2018-01-25 04:58:59 -05:00
|
|
|
if (property_exists($row, $key)) {
|
|
|
|
|
$value = $row->$key;
|
|
|
|
|
} elseif (strpos($key, '.') !== false) {
|
|
|
|
|
$value = SyncUtils::getSpecificValue($row, $key);
|
|
|
|
|
} else {
|
|
|
|
|
$value = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$target = $modifier->getTargetProperty($key);
|
|
|
|
|
if (strpos($target, '.') !== false) {
|
2019-03-25 03:33:41 -04:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
2018-01-25 04:58:59 -05:00
|
|
|
'Cannot set value for nested key "%s"',
|
|
|
|
|
$target
|
2019-03-25 03:33:41 -04:00
|
|
|
));
|
2018-01-25 04:58:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_array($value) && ! $modifier->hasArraySupport()) {
|
|
|
|
|
$new = [];
|
|
|
|
|
foreach ($value as $k => $v) {
|
|
|
|
|
$new[$k] = $modifier->transform($v);
|
2016-07-20 08:19:20 -04:00
|
|
|
}
|
2018-01-25 04:58:59 -05:00
|
|
|
$row->$target = $new;
|
|
|
|
|
} else {
|
|
|
|
|
$row->$target = $modifier->transform($value);
|
2016-07-20 08:19:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-20 07:40:43 -04:00
|
|
|
public function getRowModifiers()
|
|
|
|
|
{
|
|
|
|
|
if ($this->rowModifiers === null) {
|
|
|
|
|
$this->prepareRowModifiers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->rowModifiers;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-20 08:19:20 -04:00
|
|
|
public function hasRowModifiers()
|
|
|
|
|
{
|
|
|
|
|
return count($this->getRowModifiers()) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-29 06:34:18 -04:00
|
|
|
/**
|
|
|
|
|
* @return ImportRowModifier[]
|
|
|
|
|
*/
|
2016-02-18 20:47:12 -05:00
|
|
|
public function fetchRowModifiers()
|
|
|
|
|
{
|
|
|
|
|
$db = $this->getDb();
|
2017-08-20 10:02:47 -04:00
|
|
|
$modifiers = ImportRowModifier::loadAll(
|
2016-02-18 20:47:12 -05:00
|
|
|
$this->getConnection(),
|
|
|
|
|
$db->select()
|
|
|
|
|
->from('import_row_modifier')
|
2018-10-08 00:29:33 -04:00
|
|
|
->where('source_id = ?', $this->get('id'))
|
2017-08-20 10:02:47 -04:00
|
|
|
->order('priority ASC')
|
2016-02-18 20:47:12 -05:00
|
|
|
);
|
2017-08-20 10:02:47 -04:00
|
|
|
|
2019-11-05 10:49:59 -05:00
|
|
|
if ($modifiers) {
|
|
|
|
|
return $modifiers;
|
|
|
|
|
} else {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2016-02-18 20:47:12 -05:00
|
|
|
}
|
2016-04-22 09:31:02 -04:00
|
|
|
|
2018-01-26 09:20:33 -05:00
|
|
|
protected function fetchFlatRowModifiers()
|
|
|
|
|
{
|
|
|
|
|
$mods = [];
|
|
|
|
|
foreach ($this->fetchRowModifiers() as $mod) {
|
2023-05-25 09:44:56 -04:00
|
|
|
if ($filterExpression = $mod->get('filter_expression')) {
|
|
|
|
|
$filter = FilterEnrichment::enrichFilter(Filter::fromQueryString($filterExpression));
|
|
|
|
|
} else {
|
|
|
|
|
$filter = null;
|
|
|
|
|
}
|
|
|
|
|
$mods[] = [$mod->get('property_name'), $mod->getInstance(), $filter];
|
2018-01-26 09:20:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $mods;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-20 07:40:43 -04:00
|
|
|
protected function prepareRowModifiers()
|
|
|
|
|
{
|
2018-05-29 06:34:18 -04:00
|
|
|
$modifiers = [];
|
2016-07-20 07:40:43 -04:00
|
|
|
|
|
|
|
|
foreach ($this->fetchRowModifiers() as $mod) {
|
2018-10-08 00:29:33 -04:00
|
|
|
$name = $mod->get('property_name');
|
|
|
|
|
if (! array_key_exists($name, $modifiers)) {
|
|
|
|
|
$modifiers[$name] = [];
|
2016-07-20 07:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-08 00:29:33 -04:00
|
|
|
$modifiers[$name][] = $mod->getInstance();
|
2016-07-20 07:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->rowModifiers = $modifiers;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-20 09:21:07 -04:00
|
|
|
public function listModifierTargetProperties()
|
|
|
|
|
{
|
2018-05-29 06:34:18 -04:00
|
|
|
$list = [];
|
2016-07-20 09:21:07 -04:00
|
|
|
foreach ($this->getRowModifiers() as $rowMods) {
|
2017-08-20 10:02:47 -04:00
|
|
|
/** @var PropertyModifierHook $mod */
|
2016-07-20 09:21:07 -04:00
|
|
|
foreach ($rowMods as $mod) {
|
|
|
|
|
if ($mod->hasTargetProperty()) {
|
|
|
|
|
$list[$mod->getTargetProperty()] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array_keys($list);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-08 00:29:33 -04:00
|
|
|
/**
|
|
|
|
|
* @param bool $runImport
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws DuplicateKeyException
|
|
|
|
|
*/
|
2016-04-22 09:31:02 -04:00
|
|
|
public function checkForChanges($runImport = false)
|
|
|
|
|
{
|
|
|
|
|
$hadChanges = false;
|
|
|
|
|
|
2018-10-08 00:29:33 -04:00
|
|
|
$name = $this->get('source_name');
|
|
|
|
|
Benchmark::measure("Starting with import $name");
|
2019-09-25 07:09:19 -04:00
|
|
|
$this->raiseLimits();
|
2016-04-22 09:31:02 -04:00
|
|
|
try {
|
|
|
|
|
$import = new Import($this);
|
2018-10-08 00:29:33 -04:00
|
|
|
$this->set('last_attempt', date('Y-m-d H:i:s'));
|
2016-04-22 09:31:02 -04:00
|
|
|
if ($import->providesChanges()) {
|
2018-10-08 00:29:33 -04:00
|
|
|
Benchmark::measure("Found changes for $name");
|
2016-06-28 04:10:15 -04:00
|
|
|
$hadChanges = true;
|
2018-10-08 00:29:33 -04:00
|
|
|
$this->set('import_state', 'pending-changes');
|
2016-04-22 09:31:02 -04:00
|
|
|
|
|
|
|
|
if ($runImport && $import->run()) {
|
2018-10-08 00:29:33 -04:00
|
|
|
Benchmark::measure("Import succeeded for $name");
|
|
|
|
|
$this->set('import_state', 'in-sync');
|
2016-04-22 09:31:02 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
2018-10-08 00:29:33 -04:00
|
|
|
$this->set('import_state', 'in-sync');
|
2016-04-22 09:31:02 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-08 00:29:33 -04:00
|
|
|
$this->set('last_error_message', null);
|
2016-04-22 09:31:02 -04:00
|
|
|
} catch (Exception $e) {
|
2018-10-08 00:29:33 -04:00
|
|
|
$this->set('import_state', 'failing');
|
|
|
|
|
Benchmark::measure("Import failed for $name");
|
|
|
|
|
$this->set('last_error_message', $e->getMessage());
|
2016-04-22 09:31:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($this->hasBeenModified()) {
|
|
|
|
|
$this->store();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $hadChanges;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-08 00:29:33 -04:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws DuplicateKeyException
|
|
|
|
|
*/
|
2016-04-22 09:31:02 -04:00
|
|
|
public function runImport()
|
|
|
|
|
{
|
|
|
|
|
return $this->checkForChanges(true);
|
|
|
|
|
}
|
2019-09-25 07:09:19 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Raise PHP resource limits
|
|
|
|
|
*
|
|
|
|
|
* @return $this;
|
|
|
|
|
*/
|
|
|
|
|
protected function raiseLimits()
|
|
|
|
|
{
|
|
|
|
|
MemoryLimit::raiseTo('1024M');
|
2024-01-18 06:25:54 -05:00
|
|
|
ini_set('max_execution_time', '0');
|
2019-09-25 07:09:19 -04:00
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
2015-07-21 09:16:18 -04:00
|
|
|
}
|