Newer
Older
Alex Barth
committed
<?php
/**
* @file
* Contains FeedsProcessor and related classes.
*/
fietserwin
committed
// Insert mode for new items.
define('FEEDS_SKIP_NEW', 0);
define('FEEDS_INSERT_NEW', 1);
// Update mode for existing items.
define('FEEDS_SKIP_EXISTING', 0);
define('FEEDS_REPLACE_EXISTING', 1);
define('FEEDS_UPDATE_EXISTING', 2);
// Options for handling content in Drupal but not in source data.
define('FEEDS_SKIP_NON_EXISTENT', 'skip');
define('FEEDS_DELETE_NON_EXISTENT', 'delete');
// Default limit for creating items on a page load, not respected by all
// processors.
define('FEEDS_PROCESS_LIMIT', 50);
/**
* Thrown if a validation fails.
*/
class FeedsValidationException extends Exception {}
/**
* Thrown if a an access check fails.
*/
class FeedsAccessException extends Exception {}
Alex Barth
committed
/**
* Abstract class, defines interface for processors.
*/
abstract class FeedsProcessor extends FeedsPlugin {
Chris Leppanen
committed
/**
* Implements FeedsPlugin::pluginType().
*/
Chris Leppanen
committed
return 'processor';
}
/**
* @defgroup entity_api_wrapper Entity API wrapper.
*/
/**
* Entity type this processor operates on.
*/
public abstract function entityType();
/**
* Bundle type this processor operates on.
*
* Defaults to the entity type for entities that do not define bundles.
*
* @return string|null
* The bundle type this processor operates on, or NULL if it is undefined.
*/
public function bundle() {
return $this->config['bundle'];
}
/**
* Provides a list of bundle options for use in select lists.
*
* @return array
* A keyed array of bundle => label.
*/
public function bundleOptions() {
$options = array();
foreach (field_info_bundles($this->entityType()) as $bundle => $info) {
if (!empty($info['label'])) {
$options[$bundle] = $info['label'];
}
else {
$options[$bundle] = $bundle;
}
}
return $options;
}
git
committed
/**
* Provides a list of languages available on the site.
*
* @return array
* A keyed array of language_key => language_name (example: 'en' => 'English').
*/
public function languageOptions() {
$languages = array(
LANGUAGE_NONE => t('Language neutral'),
);
$language_list = language_list('enabled');
if (!empty($language_list[1])) {
foreach ($language_list[1] as $language) {
$languages[$language->language] = $language->name;
}
}
return $languages;
}
/**
* Create a new entity.
*
MegaChriz
committed
* @param FeedsSource $source
* The feeds source that spawns this entity.
*
MegaChriz
committed
* @return object
* A new entity object.
*/
MegaChriz
committed
protected function newEntity(FeedsSource $source) {
$entity = new stdClass();
$info = $this->entityInfo();
if (!empty($info['entity keys']['language'])) {
$entity->{$info['entity keys']['language']} = $this->entityLanguage();
}
return $entity;
}
/**
* Load an existing entity.
*
* @param $source
* The feeds source that spawns this entity.
* @param $entity_id
* The unique id of the entity that should be loaded.
*
* @return
* A new entity object.
*
* @todo We should be able to batch load these, if we found all of the
* existing ids first.
protected function entityLoad(FeedsSource $source, $entity_id) {
MegaChriz
committed
$info = $this->entityInfo();
if ($this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {
$entities = entity_load($this->entityType(), array($entity_id));
MegaChriz
committed
$entity = reset($entities);
}
else {
$args = array(':entity_id' => $entity_id);
$table = db_escape_table($info['base table']);
$key = db_escape_field($info['entity keys']['id']);
$entity = db_query("SELECT * FROM {" . $table . "} WHERE $key = :entity_id", $args)->fetchObject();
MegaChriz
committed
if ($entity && !empty($info['entity keys']['language'])) {
$entity->{$info['entity keys']['language']} = $this->entityLanguage();
}
MegaChriz
committed
return $entity;
MegaChriz
committed
* Validates an entity.
megachriz
committed
* @param object $entity
* The entity to validate.
*
* @throws FeedsValidationException $e
MegaChriz
committed
* Thrown if validation fails.
MegaChriz
committed
protected function entityValidate($entity) {
$info = $this->entityInfo();
if (!empty($info['entity keys']['language'])) {
// Ensure that a valid language is always set.
$key = $info['entity keys']['language'];
git
committed
$languages = $this->languageOptions();
git
committed
if (empty($entity->$key) || !isset($languages[$entity->$key])) {
$entity->$key = $this->entityLanguage();
git
committed
}
}
// Perform field validation if entity is fieldable.
if (!empty($info['fieldable'])) {
try {
megachriz
committed
$this->validateFields($entity);
}
catch (FieldValidationException $e) {
$errors = array();
// Unravel the errors inside the FieldValidationException.
foreach ($e->errors as $field_name => $field_errors) {
foreach ($field_errors as $langcode => $field_item_errors) {
$errors = array_merge($errors, $this->unravelFieldValidationExceptionErrors($field_item_errors));
}
}
// Compose error message. If available, use the entity label to indicate
// which item failed. Fallback to the GUID value (if available) or else
// no indication.
$label = entity_label($this->entityType(), $entity);
Loading
Loading full blame...