class FeedImport in Feed Import 7.3
Same name and namespace in other branches
- 7 feed_import.inc.php \FeedImport
- 7.2 feed_import.inc.php \FeedImport
This class provides helper functions for feed import.
Hierarchy
- class \FeedImport
Expanded class hierarchy of FeedImport
File
- feed_import_base/
inc/ feed_import.inc, line 11 - This file contains Feed Import helpers.
View source
class FeedImport {
// Constants
const FEED_OK = 1;
const FEED_SOURCE_ERR = 2;
const FEED_ITEMS_ERR = 3;
const FEED_OVERLAP_ERR = 4;
const FEED_CONFIG_ERR = 5;
// Entity info cache.
protected static $entityInfo = array();
// Default compare function.
/**
* Gets info about an entity.
* This info is cached.
*
* @param string $entity_name The entity name
*
* @return object An object that describes entity
*/
public static function getEntityInfo($entity_name) {
if (empty($entity_name)) {
return FALSE;
}
elseif (isset(static::$entityInfo[$entity_name])) {
return static::$entityInfo[$entity_name];
}
if (!($entity = entity_get_info($entity_name))) {
return FALSE;
}
// Set main entity info.
$info = (object) array(
'name' => $entity_name,
'idKey' => isset($entity['entity keys']['id']) ? $entity['entity keys']['id'] : NULL,
'langKey' => isset($entity['entity keys']['language']) ? $entity['entity keys']['language'] : 'language',
'bundleKey' => isset($entity['entity keys']['bundle']) ? $entity['entity keys']['bundle'] : NULL,
'createCallback' => isset($entity['creation callback']) ? $entity['creation callback'] : NULL,
'saveCallback' => isset($entity['save callback']) ? $entity['save callback'] : NULL,
'deleteCallback' => isset($entity['deletion callback']) ? $entity['deletion callback'] : NULL,
'controller' => entity_get_controller($entity_name),
'properties' => array(),
'fields' => array(),
);
if ($info->controller) {
$info->controller->canCreate = method_exists($info->controller, 'create');
$info->controller->canSave = method_exists($info->controller, 'save');
$info->controller->canDelete = method_exists($info->controller, 'delete');
$info->controller->canResetCache = method_exists($info->controller, 'resetCache');
}
else {
$info->controller = (object) array(
'canCreate' => FALSE,
'canSave' => FALSE,
'canDelete' => FALSE,
'canResetCache' => FALSE,
);
}
if (!$info->saveCallback && function_exists($entity_name . '_save')) {
$info->saveCallback = $entity_name . '_save';
}
if (!$info->deleteCallback && function_exists($entity_name . '_delete')) {
$info->deleteCallback = $entity_name . '_delete';
}
// Get fields info.
if ($fieldlist = field_info_instances($entity_name)) {
foreach ($fieldlist as &$fields) {
foreach ($fields as &$field) {
if (!empty($field['deleted']) || isset($info->fields[$field['field_name']])) {
continue;
}
$field_info = field_info_field($field['field_name']);
$info->fields[$field['field_name']] = array(
'name' => $field['field_name'],
'column' => key($field_info['columns']),
'columns' => array_keys($field_info['columns']),
'cardinality' => $field_info['cardinality'],
'type' => $field_info['type'],
'module' => $field_info['module'],
);
}
}
}
if (function_exists('entity_get_property_info')) {
// Get properties info.
$prop_info = entity_get_property_info($entity_name);
if (isset($prop_info['properties'])) {
foreach ($prop_info['properties'] as $pname => $pval) {
if (isset($pval['schema field'])) {
$pval = $pval['schema field'];
}
else {
$pval = $pname;
}
if (!isset($info->fields[$pval])) {
$info->properties[] = $pval;
}
}
$info->properties = array_unique($info->properties);
}
}
elseif (isset($entity['schema_fields_sql']['base table'])) {
$info->properties = array_diff($entity['schema_fields_sql']['base table'], array_keys($info->fields));
}
return static::$entityInfo[$entity_name] = $info;
}
/**
* Cache used for entity names.
*/
protected static $entityNames = array();
/**
* Get all entity names.
*
* @return array
* List of all entity names.
*/
public static function getAllEntities() {
if (!static::$entityNames) {
foreach (entity_get_info() as $entity => $info) {
static::$entityNames[$entity] = $info['label'];
}
}
return static::$entityNames;
}
// Default field compare function.
public static $defaultFieldCompareFunction = '_feed_import_base_compare_other_fields';
/**
* Sets processor settings.
*
* @param FeedImportProcessor $fi
* The processor
* @param object $feed
* Feed settings
* @param string $filter_dir
* Path to extra filters dir
*
* @return FeedImportProcessor
* An instance of FeedImportProcessor or FALSE on error
*/
public static function setProcessorSettings(FeedImportProcessor $fi, $feed, $filter_dir) {
$s = $feed->settings;
$ok = TRUE;
$ok = $ok && $fi
->setEntityInfo(static::getEntityInfo($feed->entity));
$ok = $ok && $fi
->setOptions($s['processor']['options']);
if (!isset($s['functions'])) {
$s['functions'] = NULL;
}
$ok = $ok && $fi
->setFilter(new $s['filter']['class']($filter_dir, $s['filter']['options']), $s['functions']);
$ok = $ok && $fi
->setReader(new $s['reader']['class']($s['reader']['options']));
$hm = new $s['hashes']['class']($feed->entity, $feed->machine_name);
$hm
->setOptions($s['hashes']['options']);
$ok = $ok && $fi
->setHashManager($hm);
$ok = $ok && $fi
->setUniq($s['uniq_path']);
$ok = $ok && $fi
->setFields($s['fields'], $s['static_fields'], static::getFieldsCompareFunctions(), static::getMergeFieldClasses(), static::$defaultFieldCompareFunction);
$ok = $ok && $fi
->setCustomSettings(array_diff_key($s, array(
'uniq_path' => 1,
'processor' => 1,
'reader' => 1,
'hashes' => 1,
'filter' => 1,
'fields' => 1,
'static_fields' => 1,
'feed' => 1,
'functions' => 1,
)));
return $ok ? TRUE : $fi
->getErrors();
}
/**
* Returns field compare functions.
*/
public static function getFieldsCompareFunctions() {
$funcs = module_invoke_all('feed_import_field_compare_functions');
foreach ($funcs as $type => &$f) {
if (is_array($f)) {
for ($i = 0, $max = count($f); $i < $max; $i++) {
if (is_callable($f[$i])) {
$f = $f[$i];
continue 2;
}
}
unset($funcs[$type]);
}
elseif (!is_callable($f)) {
unset($funcs[$type]);
}
}
return $funcs;
}
/**
* Gets the classes used to merge field values.
*
* @return array
* An array of classes keyed by merge mode.
*/
public static function getMergeFieldClasses() {
$classes = module_invoke_all('feed_import_field_merge_classes');
foreach ($classes as $key => &$class) {
if (isset($class['class'])) {
$class = $class['class'];
}
else {
unset($classes[$key]);
}
}
return $classes;
}
// A list of all feeds configuration.
protected static $feedsList = array();
/**
* Get a list with all feeds.
*/
public static function loadAllFeeds() {
if (static::$feedsList) {
return static::$feedsList;
}
$feeds = db_select('feed_import_settings', 'f')
->fields('f')
->orderBy('id', 'DESC')
->execute()
->fetchAll();
$ret = array();
foreach ($feeds as &$feed) {
$feed->settings = unserialize($feed->settings);
$ret[$feed->machine_name] = $feed;
}
return static::$feedsList = $ret;
}
/**
* Loads a feed from database
*
* @param int|string $name
* Feed machine name or id
*
* @return object
* Feed object or FALSE
*/
public static function loadFeed($name) {
$feed = db_select('feed_import_settings', 'f')
->fields('f')
->condition((int) $name ? 'id' : 'machine_name', $name, '=')
->range(0, 1)
->execute()
->fetchObject();
if (!$feed) {
return FALSE;
}
$feed->settings = unserialize($feed->settings);
return $feed;
}
/**
* Deletes a feed.
*
* @param object $feed
* Feed info
* @param bool $hashes
* Also remove hashes
*/
public static function deleteFeed($feed, $hashes = TRUE) {
db_delete('feed_import_settings')
->condition('machine_name', $feed->machine_name)
->execute();
if ($hashes && isset($feed->settings['hashes']['class'])) {
$class = $feed->settings['hashes']['class'];
$class::deleteByFeed($feed->machine_name);
}
}
/**
* Gets a new empty feed configuration.
*
* @return array
* An empty feed configuration.
*/
public static function getEmptyFeed() {
return array(
'name' => NULL,
'machine_name' => NULL,
'entity' => NULL,
'cron_import' => 0,
'last_run' => 0,
'last_run_duration' => 0,
'last_run_items' => 0,
'settings' => array(
'uniq_path' => NULL,
'preprocess' => NULL,
'feed' => array(
'protect_on_invalid_source' => FALSE,
'protect_on_fewer_items' => 0,
),
'processor' => array(
'name' => 'default',
'class' => 'FeedImportProcessor',
'options' => array(
'items_count' => 0,
'skip_imported' => FALSE,
'reset_cache' => 100,
'break_on_undefined_filter' => TRUE,
'skip_defined_functions_check' => FALSE,
'updates_only' => FALSE,
),
),
'reader' => array(
'name' => 'xml',
'class' => 'SimpleXMLFIReader',
'options' => array(),
),
'hashes' => array(
'name' => 'sql',
'class' => 'FeedImportSQLHashes',
'options' => array(
'ttl' => 0,
'insert_chunk' => 300,
'update_chunk' => 300,
'group' => '',
),
),
'filter' => array(
'name' => 'default',
'class' => 'FeedImportMultiFilter',
'options' => array(
'param' => '[field]',
'include' => NULL,
),
),
'fields' => array(),
'static_fields' => array(),
'functions' => array(),
),
);
}
/**
* Saves a feed in database
*
* @param object $feed
* Feed info
*/
public static function saveFeed($feed) {
if (empty($feed->name) || empty($feed->machine_name) || empty($feed->entity) || empty($feed->settings)) {
return FALSE;
}
if (!isset($feed->cron_import)) {
$feed->cron_import = 0;
}
$fields = array(
'name' => $feed->name,
'machine_name' => $feed->machine_name,
'entity' => $feed->entity,
'cron_import' => (int) $feed->cron_import,
'settings' => serialize($feed->settings),
);
if (isset($feed->id)) {
db_update('feed_import_settings')
->fields($fields)
->condition('id', $feed->id)
->execute();
}
else {
$fields += array(
'last_run' => 0,
'last_run_duration' => 0,
'last_run_items' => 0,
);
db_insert('feed_import_settings')
->fields($fields)
->execute();
}
return TRUE;
}
/**
* Saves feed import status
*/
public static function saveFeedImportStatus($feed) {
db_update('feed_import_settings')
->fields(array(
'last_run' => (int) $feed->last_run,
'last_run_duration' => (int) $feed->last_run_duration,
'last_run_items' => (int) $feed->last_run_items,
))
->condition('machine_name', $feed->machine_name)
->execute();
}
// Active import
public static $activeImport = NULL;
/**
* Imports a feed.
*
* @param object $feed
* Feed info
* @param string $filters_dir
* Path to filters dir
*/
public static function import($feed, $filters_dir) {
if (!empty($feed->settings['preprocess']) && function_exists($feed->settings['preprocess'])) {
// Preprocess feed before import.
call_user_func($feed->settings['preprocess'], $feed);
}
$class = $feed->settings['processor']['class'];
$fi = new $class($feed->machine_name);
$fi
->setErrorHandler(TRUE);
$f = static::setProcessorSettings($fi, $feed, $filters_dir);
if ($f !== TRUE) {
$fi
->setErrorHandler(FALSE);
return array(
'init_error' => TRUE,
'errors' => $f,
);
}
unset($f);
$lastimport = static::$activeImport;
static::$activeImport = $fi;
$f = $fi
->process();
static::$activeImport = $lastimport;
$fi
->setErrorHandler(FALSE);
return $f;
}
/**
* Gets all hash manager classes used by feeds.
*
* @return array
* An array of classes
*/
public static function getHashManagers() {
static $hm = array();
if (!$hm) {
foreach (static::loadAllFeeds() as $feed) {
if (!empty($feed->settings['hashes']['class'])) {
$hm[] = $feed->settings['hashes']['class'];
}
}
$hm = array_unique($hm);
}
return $hm;
}
/**
* Delete all expired items.
*
* @param int $max
* Max number of entity ids to delete from
* a hash manager
*
* @return int
* Number of deleted entities
*/
public static function deleteExpired($max = 0) {
$deleted = 0;
foreach (static::loadAllFeeds() as $feed) {
if (empty($feed->settings['hashes']['class'])) {
continue;
}
$class = $feed->settings['hashes']['class'];
$items = $class::getExpired($feed->machine_name, $max);
foreach ($items as $e => &$ids) {
$entity = static::getEntityInfo($e);
// Delete entities.
if ($entity->deleteCallback) {
$f = $entity->deleteCallback . '_multiple';
if (function_exists($f)) {
$f($ids);
}
else {
array_map($entity->deleteCallback, $ids);
}
}
else {
$entity->controller
->delete($ids);
}
// Delete hashes.
$class::delete(array_keys($ids));
$deleted += count($ids);
unset($items[$e], $entity);
}
}
return $deleted;
}
// Deleted entities.
protected static $deletedEntities = array();
/**
* Schedule a deleted entity to be removed from hashes.
*
* @param string $entity_type
* Entity type
* @param int $id
* Entity id
*/
public static function addDeletedEntity($entity_type, $id) {
static $unregistered = TRUE;
if ($unregistered) {
$unregistered = FALSE;
register_shutdown_function(array(
__CLASS__,
'removeEntityHashes',
));
}
static::$deletedEntities[$entity_type][] = $id;
}
/**
* Removes entity hashes.
*/
public static function removeEntityHashes($entities = NULL) {
if ($entities == NULL) {
$entities =& static::$deletedEntities;
}
foreach (static::getHashManagers() as $hm) {
array_walk($entities, array(
$hm,
'deleteEntities',
));
}
$entities = NULL;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FeedImport:: |
public static | property | ||
FeedImport:: |
public static | property | ||
FeedImport:: |
protected static | property | ||
FeedImport:: |
protected static | property | ||
FeedImport:: |
protected static | property | Cache used for entity names. | |
FeedImport:: |
protected static | property | ||
FeedImport:: |
public static | function | Schedule a deleted entity to be removed from hashes. | |
FeedImport:: |
public static | function | Delete all expired items. | |
FeedImport:: |
public static | function | Deletes a feed. | |
FeedImport:: |
constant | |||
FeedImport:: |
constant | |||
FeedImport:: |
constant | |||
FeedImport:: |
constant | |||
FeedImport:: |
constant | |||
FeedImport:: |
public static | function | Get all entity names. | |
FeedImport:: |
public static | function | Gets a new empty feed configuration. | |
FeedImport:: |
public static | function | Gets info about an entity. This info is cached. | |
FeedImport:: |
public static | function | Returns field compare functions. | |
FeedImport:: |
public static | function | Gets all hash manager classes used by feeds. | |
FeedImport:: |
public static | function | Gets the classes used to merge field values. | |
FeedImport:: |
public static | function | Imports a feed. | |
FeedImport:: |
public static | function | Get a list with all feeds. | |
FeedImport:: |
public static | function | Loads a feed from database | |
FeedImport:: |
public static | function | Removes entity hashes. | |
FeedImport:: |
public static | function | Saves a feed in database | |
FeedImport:: |
public static | function | Saves feed import status | |
FeedImport:: |
public static | function | Sets processor settings. |