public static function FeedImport::saveEntities in Feed Import 7.2
Same name and namespace in other branches
- 7 feed_import.inc.php \FeedImport::saveEntities()
Saves/updates all created entities
Parameters
array &$feed: Feed info array
array &$items: An array with entities
3 calls to FeedImport::saveEntities()
- FeedImport::processFeed in ./
feed_import.inc.php - This function is choosing process function and executes it
- FeedImport::processXMLChunked in ./
feed_import.inc.php - Imports and process a huge xml in chunks
- FeedImport::processXMLReader in ./
feed_import.inc.php - Process large xml file with XmlReader
File
- ./
feed_import.inc.php, line 640 - Feed import class for parsing and processing content.
Class
- FeedImport
- @file Feed import class for parsing and processing content.
Code
public static function saveEntities(&$feed, &$items) {
// Get existing items for update.
if (!empty(self::$generatedHashes)) {
$ids = self::getEntityIdsFromHash(self::$generatedHashes);
// Reset all generated hashes.
self::$generatedHashes = array();
}
else {
$ids = array();
}
// This sets expire timestamp.
$feed['time'] = (int) $feed['time'];
// Report data.
self::$report['total'] += count($items);
// Now we create real entityes or update existent.
foreach ($items as &$item) {
// Check if item is skipped.
if ($item == NULL) {
continue;
}
// Save hash and remove from item.
$hash = $item->{self::$tempHash};
unset($item->{self::$tempHash});
// Check if item is already imported or is not monitorized.
if ($hash !== NULL && isset($ids[$hash])) {
// Check if is used option to skip item if already imported.
if (!empty($feed['xpath']['#skip_imported_items'])) {
$item = NULL;
continue;
}
$changed = FALSE;
// Load entity.
try {
$entity = call_user_func(self::$functionLoad, $ids[$hash]->entity_id);
} catch (Exception $e) {
$item = NULL;
unset($ids[$hash]);
continue;
}
// If entity is missing then skip.
if (empty($entity)) {
$item = NULL;
unset($ids[$hash]);
continue;
}
$lang = $item->language;
// Find if entity is different from last feed.
foreach ($item as $key => &$value) {
if (is_array($value)) {
if (empty($entity->{$key}[$lang]) || count($entity->{$key}[$lang]) != count($value[$lang])) {
$changed = TRUE;
$entity->{$key} = $value;
}
elseif (count($value[$lang]) <= 1) {
$col = isset($value[$lang][0]) ? key($value[$lang][0]) : '';
if ($entity->{$key}[$lang][0][$col] != $value[$lang][0][$col]) {
$changed = TRUE;
$entity->{$key} = $value;
}
unset($col);
}
else {
$col = key($value[$lang][0]);
$temp = array();
foreach ($entity->{$key}[$lang] as &$ev) {
$temp[][$col] = $ev[$col];
}
if ($temp != $value[$lang]) {
$changed = TRUE;
$entity->{$key} = $value;
}
unset($temp, $col);
}
}
else {
if (!isset($entity->{$key}) || $entity->{$key} != $value) {
$changed = TRUE;
$entity->{$key} = $value;
}
}
}
$ok = TRUE;
// Check if entity is changed and save changes.
if ($changed) {
try {
call_user_func(self::$functionSave, $entity);
// Set report about updated items.
self::$report['updated']++;
} catch (Exception $e) {
$ok = FALSE;
}
}
else {
// Set report about rescheduled items.
self::$report['rescheduled']++;
}
if ($ok) {
// Add to update ids.
self::updateIds($ids[$hash]->id);
}
// Free some memory.
unset($ids[$hash], $entity, $lang);
}
else {
// Mark as new.
$item->{$feed['entity_info']['#table_pk']} = NULL;
$ok = TRUE;
try {
// Save imported item.
call_user_func(self::$functionSave, $item);
} catch (Exception $e) {
$ok = FALSE;
}
if ($ok) {
// Check if is monitorized.
if ($hash !== NULL) {
$vars = array(
$feed['machine_name'],
$feed['entity_info']['#entity'],
$item->{$feed['entity_info']['#table_pk']},
$hash,
$feed['time'] ? time() + $feed['time'] : 0,
);
// Insert into feed import hash table.
self::insertItem($vars);
}
// Set report about new items.
self::$report['new']++;
}
}
// No need anymore.
$item = NULL;
}
// No need anymore.
unset($items, $ids);
// Only monitorized items are inserted or updated.
if (!empty($feed['xpath']['#uniq'])) {
// Insert left items.
self::insertItem(NULL);
$vars = array(
'expire' => $feed['time'] ? time() + $feed['time'] : 0,
'feed_machine_name' => $feed['machine_name'],
);
// Update ids for existing items.
self::updateIds($vars);
}
}