class FeedsNodeProcessor in Feeds 7.2
Same name and namespace in other branches
- 6 plugins/FeedsNodeProcessor.inc \FeedsNodeProcessor
- 7 plugins/FeedsNodeProcessor.inc \FeedsNodeProcessor
Creates nodes from feed items.
Hierarchy
- class \FeedsConfigurable
- class \FeedsPlugin implements FeedsSourceInterface
- class \FeedsProcessor
- class \FeedsNodeProcessor
- class \FeedsProcessor
- class \FeedsPlugin implements FeedsSourceInterface
Expanded class hierarchy of FeedsNodeProcessor
71 string references to 'FeedsNodeProcessor'
- FeedsAccountSwitcherTest::testAuthorizedImport in tests/
FeedsAccountSwitcherTest.test - Tests if an extra account switch happens on authorized imports.
- FeedsCSVParserTestCase::testEncodingFailure in tests/
feeds_parser_csv.test - Tests an encoding failure during parsing a CSV.
- FeedsCSVParserTestCase::testGetTemplate in tests/
feeds_parser_csv.test - Tests if a CSV template is generated properly using various settings.
- FeedsCSVParserTestCase::testMbstringExtensionDisabled in tests/
feeds_parser_csv.test - Tests parsing a CSV when the mbstring extension is not available.
- FeedsFileHTTPTestCase::setUpImporter in tests/
feeds_fetcher_http.test - Setup importer to import items from testing/feeds/nodes.csv.
File
- plugins/
FeedsNodeProcessor.inc, line 17 - Class definition of FeedsNodeProcessor.
View source
class FeedsNodeProcessor extends FeedsProcessor {
/**
* Define entity type.
*/
public function entityType() {
return 'node';
}
/**
* Implements parent::entityInfo().
*/
protected function entityInfo() {
$info = parent::entityInfo();
$info['label plural'] = t('Nodes');
return $info;
}
/**
* Overrides parent::languageOptions().
*/
public function languageOptions() {
// Content types can have "extended" language enabled, allowing all
// available languages, not just enabled. Account for this here.
if (module_exists('i18n_node') && $this
->bundle()) {
$node = new stdClass();
$node->type = $this
->bundle();
$node->is_new = TRUE;
node_object_prepare($node);
$languages = array(
LANGUAGE_NONE => t('Language neutral'),
) + i18n_node_language_list($node);
return $languages;
}
// If i18n_node is not enabled, default to enabled languages.
return parent::languageOptions();
}
/**
* Creates a new node in memory and returns it.
*/
protected function newEntity(FeedsSource $source) {
$node = parent::newEntity($source);
$node->type = $this
->bundle();
$node->changed = REQUEST_TIME;
$node->created = REQUEST_TIME;
$node->is_new = TRUE;
node_object_prepare($node);
// Populate properties that are set by node_object_prepare().
$node->log = 'Created by FeedsNodeProcessor';
$node->uid = $this->config['author'];
return $node;
}
/**
* Loads an existing node.
*
* If the update existing method is not FEEDS_UPDATE_EXISTING, only the node
* table will be loaded, foregoing the node_load API for better performance.
*
* @todo Reevaluate the use of node_object_prepare().
*/
protected function entityLoad(FeedsSource $source, $nid) {
$node = parent::entityLoad($source, $nid);
if ($this->config['update_existing'] != FEEDS_UPDATE_EXISTING) {
$node->uid = $this->config['author'];
}
node_object_prepare($node);
// Workaround for issue #1247506. See #1245094 for backstory.
if (!empty($node->menu)) {
// If the node has a menu item(with a valid mlid) it must be flagged
// 'enabled'.
$node->menu['enabled'] = (int) (bool) $node->menu['mlid'];
}
// Populate properties that are set by node_object_prepare().
if ($this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {
$node->log = 'Updated by FeedsNodeProcessor';
}
else {
$node->log = 'Replaced by FeedsNodeProcessor';
}
return $node;
}
/**
* Check that the user has permission to save a node.
*/
protected function entitySaveAccess($entity) {
// The check will be skipped for anonymous nodes.
if ($this->config['authorize'] && !empty($entity->uid)) {
$author = user_load($entity->uid);
// If the uid was mapped directly, rather than by email or username, it
// could be invalid.
if (!$author) {
throw new FeedsAccessException(t('User %uid is not a valid user.', array(
'%uid' => $entity->uid,
)));
}
if (empty($entity->nid) || !empty($entity->is_new)) {
$op = 'create';
$access = node_access($op, $entity->type, $author);
}
else {
$op = 'update';
$access = node_access($op, $entity, $author);
}
if (!$access) {
$message = t('The user %name is not authorized to %op content of type %content_type. To import this item, either the user "@name" (author of the item) must be given the permission to @op content of type @content_type, or the option "Authorize" on the Node processor settings must be turned off.', array(
'%name' => $author->name,
'%op' => $op,
'%content_type' => $entity->type,
'@name' => $author->name,
'@op' => $op,
'@content_type' => $entity->type,
));
throw new FeedsAccessException($message);
}
}
}
/**
* Validates a node.
*/
protected function entityValidate($entity, FeedsSource $source = NULL) {
// Set or correct user ID.
if (!isset($entity->uid) || !is_numeric($entity->uid)) {
$entity->uid = $this->config['author'];
}
elseif (!is_int($entity->uid)) {
// Cast user ID to an integer to prevent array_flip() notices in
// DrupalDefaultEntityController::load() which occur when an user ID is
// not an integer nor a string.
$entity->uid = (int) $entity->uid;
}
// When the import should be authorized, make an extra account switch.
if ($source && $this->config['authorize'] && !empty($entity->uid)) {
$author = user_load($entity->uid);
$switcher = $source->accountSwitcher
->switchTo($author);
}
try {
parent::entityValidate($entity, $source);
} catch (Exception $e) {
// Catch any exceptions, throw these at the end.
}
// When an user switch happened because of an authorized import, be sure to
// switch back to the previous logged in account.
if (!empty($switcher)) {
$switcher
->switchBack();
}
if (!empty($e)) {
throw $e;
}
}
/**
* Save a node.
*/
public function entitySave($entity) {
node_save($entity);
}
/**
* Delete a series of nodes.
*/
protected function entityDeleteMultiple($nids) {
node_delete_multiple($nids);
}
/**
* Overrides parent::expiryQuery().
*/
protected function expiryQuery(FeedsSource $source, $time) {
$select = parent::expiryQuery($source, $time);
$select
->condition('e.created', REQUEST_TIME - $time, '<');
return $select;
}
/**
* Return expiry time.
*/
public function expiryTime() {
return $this->config['expire'];
}
/**
* Override parent::configDefaults().
*/
public function configDefaults() {
return array(
'expire' => FEEDS_EXPIRE_NEVER,
'author' => 0,
'authorize' => TRUE,
) + parent::configDefaults();
}
/**
* Override parent::configForm().
*/
public function configForm(&$form_state) {
$form = parent::configForm($form_state);
$author = user_load($this->config['author']);
$form['author'] = array(
'#type' => 'textfield',
'#title' => t('Author'),
'#description' => t('Select the author of the nodes to be created - leave empty to assign "anonymous".'),
'#autocomplete_path' => 'user/autocomplete',
'#default_value' => empty($author->name) ? 'anonymous' : check_plain($author->name),
);
$form['authorize'] = array(
'#type' => 'checkbox',
'#title' => t('Authorize'),
'#description' => t('Check that the author has permission to create the node.'),
'#default_value' => $this->config['authorize'],
);
$period = drupal_map_assoc(array(
FEEDS_EXPIRE_NEVER,
3600,
10800,
21600,
43200,
86400,
259200,
604800,
2592000,
2592000 * 3,
2592000 * 6,
31536000,
), 'feeds_format_expire');
$form['expire'] = array(
'#type' => 'select',
'#title' => t('Expire nodes'),
'#options' => $period,
'#description' => t("Select after how much time nodes should be deleted. The node's published date will be used for determining the node's age, see Mapping settings."),
'#default_value' => $this->config['expire'],
);
// Add on the "Unpublish" option for nodes, update wording.
if (isset($form['update_non_existent'])) {
$form['update_non_existent']['#options'][FEEDS_UNPUBLISH_NON_EXISTENT] = t('Unpublish non-existent nodes');
}
return $form;
}
/**
* Override parent::configFormValidate().
*/
public function configFormValidate(&$values) {
if ($author = user_load_by_name($values['author'])) {
$values['author'] = $author->uid;
}
else {
$values['author'] = 0;
}
}
/**
* Reschedule if expiry time changes.
*/
public function configFormSubmit(&$values) {
if ($this->config['expire'] != $values['expire']) {
feeds_reschedule($this->id);
}
parent::configFormSubmit($values);
}
/**
* Override setTargetElement to operate on a target item that is a node.
*/
public function setTargetElement(FeedsSource $source, $target_node, $target_element, $value) {
switch ($target_element) {
case 'created':
$target_node->created = feeds_to_unixtime($value, REQUEST_TIME);
break;
case 'changed':
// The 'changed' value will be set on the node in feeds_node_presave().
// This is because node_save() always overwrites this value (though
// before invoking hook_node_presave()).
$target_node->feeds_item->node_changed = feeds_to_unixtime($value, REQUEST_TIME);
break;
case 'feeds_source':
// Get the class of the feed node importer's fetcher and set the source
// property. See feeds_node_update() how $node->feeds gets stored.
if ($id = feeds_get_importer_id($this
->bundle())) {
$class = get_class(feeds_importer($id)->fetcher);
$target_node->feeds[$class]['source'] = $value;
// This effectively suppresses 'import on submission' feature.
// See feeds_node_insert().
$target_node->feeds['suppress_import'] = TRUE;
}
break;
case 'user_name':
if ($user = user_load_by_name($value)) {
$target_node->uid = $user->uid;
}
break;
case 'user_mail':
if ($user = user_load_by_mail($value)) {
$target_node->uid = $user->uid;
}
break;
default:
parent::setTargetElement($source, $target_node, $target_element, $value);
break;
}
}
/**
* Return available mapping targets.
*/
public function getMappingTargets() {
$type = node_type_get_type($this
->bundle());
$targets = parent::getMappingTargets();
if ($type && $type->has_title) {
$targets['title'] = array(
'name' => t('Title'),
'description' => t('The title of the node.'),
'optional_unique' => TRUE,
);
}
$targets['nid'] = array(
'name' => t('Node ID'),
'description' => t('The nid of the node. NOTE: use this feature with care, node ids are usually assigned by Drupal.'),
'optional_unique' => TRUE,
);
$targets['uid'] = array(
'name' => t('User ID'),
'description' => t('The Drupal user ID of the node author.'),
);
$targets['user_name'] = array(
'name' => t('Username'),
'description' => t('The Drupal username of the node author.'),
);
$targets['user_mail'] = array(
'name' => t('User email'),
'description' => t('The email address of the node author.'),
);
$targets['status'] = array(
'name' => t('Published status'),
'description' => t('Whether a node is published or not. 1 stands for published, 0 for not published.'),
);
$targets['created'] = array(
'name' => t('Published date'),
'description' => t('The UNIX time when a node has been published.'),
);
$targets['changed'] = array(
'name' => t('Updated date'),
'description' => t('The Unix timestamp when a node has been last updated.'),
);
$targets['promote'] = array(
'name' => t('Promoted to front page'),
'description' => t('Boolean value, whether or not node is promoted to front page. (1 = promoted, 0 = not promoted)'),
);
$targets['sticky'] = array(
'name' => t('Sticky'),
'description' => t('Boolean value, whether or not node is sticky at top of lists. (1 = sticky, 0 = not sticky)'),
);
// Include language field if Locale module is enabled.
if (module_exists('locale')) {
$targets['language'] = array(
'name' => t('Language'),
'description' => t('The two-character language code of the node.'),
);
}
// Include comment field if Comment module is enabled.
if (module_exists('comment')) {
$targets['comment'] = array(
'name' => t('Comments'),
'description' => t('Whether comments are allowed on this node: 0 = no, 1 = read only, 2 = read/write.'),
);
}
// If the target content type is a Feed node, expose its source field.
if ($id = feeds_get_importer_id($this
->bundle())) {
$name = feeds_importer($id)->config['name'];
$targets['feeds_source'] = array(
'name' => t('Feed source'),
'description' => t('The content type created by this processor is a Feed Node, it represents a source itself. Depending on the fetcher selected on the importer "@importer", this field is expected to be for example a URL or a path to a file.', array(
'@importer' => $name,
)),
'optional_unique' => TRUE,
);
}
$this
->getHookTargets($targets);
return $targets;
}
/**
* Get nid of an existing feed item node if available.
*/
protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
if ($nid = parent::existingEntityId($source, $result)) {
return $nid;
}
// Iterate through all unique targets and test whether they do already
// exist in the database.
foreach ($this
->uniqueTargets($source, $result) as $target => $value) {
switch ($target) {
case 'nid':
$nid = db_query("SELECT nid FROM {node} WHERE nid = :nid", array(
':nid' => $value,
))
->fetchField();
break;
case 'title':
$nid = db_query("SELECT nid FROM {node} WHERE title = :title AND type = :type", array(
':title' => $value,
':type' => $this
->bundle(),
))
->fetchField();
break;
case 'feeds_source':
if ($id = feeds_get_importer_id($this
->bundle())) {
$nid = db_query("SELECT fs.feed_nid FROM {node} n JOIN {feeds_source} fs ON n.nid = fs.feed_nid WHERE fs.id = :id AND fs.source = :source", array(
':id' => $id,
':source' => $value,
))
->fetchField();
}
break;
}
if ($nid) {
// Return with the first nid found.
return $nid;
}
}
return 0;
}
/**
* Overrides FeedsProcessor::clean().
*
* Allow unpublish instead of delete.
*
* @param FeedsState $state
* The FeedsState object for the given stage.
*/
protected function clean(FeedsState $state) {
// Delegate to parent if not unpublishing or option not set.
if (!isset($this->config['update_non_existent']) || $this->config['update_non_existent'] != FEEDS_UNPUBLISH_NON_EXISTENT) {
return parent::clean($state);
}
$total = count($state->removeList);
if ($total) {
$nodes = node_load_multiple($state->removeList);
foreach ($nodes as &$node) {
$this
->loadItemInfo($node);
// Update the hash value of the feed item to ensure that the item gets
// updated in case it reappears in the feed.
$node->feeds_item->hash = $this->config['update_non_existent'];
node_unpublish_action($node);
node_save($node);
$state->unpublished++;
}
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FeedsConfigurable:: |
protected | property | Holds the actual configuration information. | |
FeedsConfigurable:: |
protected | property | CTools export enabled status of this object. | |
FeedsConfigurable:: |
protected | property | CTools export type of this object. | |
FeedsConfigurable:: |
protected | property | An unique identifier for the configuration. | |
FeedsConfigurable:: |
public | function | Similar to setConfig but adds to existing configuration. | |
FeedsConfigurable:: |
public | function | Copy a configuration. | 1 |
FeedsConfigurable:: |
public | function | Determine whether this object is persistent. | 1 |
FeedsConfigurable:: |
public | function | Determines whether this object is persistent and enabled. | 1 |
FeedsConfigurable:: |
public | function | Implements getConfig(). | 1 |
FeedsConfigurable:: |
public | function | Returns whether or not the configurable has a config form. | |
FeedsConfigurable:: |
public | function | Determine whether this object is enabled. | |
FeedsConfigurable:: |
public | function | Set configuration. | |
FeedsConfigurable:: |
public | function | Overrides magic method __get(). | |
FeedsConfigurable:: |
public | function | Override magic method __isset(). This is needed due to overriding __get(). | |
FeedsNodeProcessor:: |
protected | function |
Overrides FeedsProcessor::clean(). Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
public | function |
Override parent::configDefaults(). Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
public | function |
Override parent::configForm(). Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
public | function |
Reschedule if expiry time changes. Overrides FeedsConfigurable:: |
|
FeedsNodeProcessor:: |
public | function |
Override parent::configFormValidate(). Overrides FeedsConfigurable:: |
|
FeedsNodeProcessor:: |
protected | function |
Delete a series of nodes. Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
protected | function |
Implements parent::entityInfo(). Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
protected | function |
Loads an existing node. Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
public | function |
Save a node. Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
protected | function |
Check that the user has permission to save a node. Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
public | function |
Define entity type. Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
protected | function |
Validates a node. Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
protected | function |
Get nid of an existing feed item node if available. Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
protected | function |
Overrides parent::expiryQuery(). Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
public | function |
Return expiry time. Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
public | function |
Return available mapping targets. Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
public | function |
Overrides parent::languageOptions(). Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
protected | function |
Creates a new node in memory and returns it. Overrides FeedsProcessor:: |
|
FeedsNodeProcessor:: |
public | function |
Override setTargetElement to operate on a target item that is a node. Overrides FeedsProcessor:: |
|
FeedsPlugin:: |
protected | property | The plugin definition. | |
FeedsPlugin:: |
public static | function | Get all available plugins. | |
FeedsPlugin:: |
public static | function | Gets all available plugins of a particular type. | |
FeedsPlugin:: |
public static | function | Determines whether given plugin is derived from given base plugin. | |
FeedsPlugin:: |
public | function |
Returns TRUE if $this->sourceForm() returns a form. Overrides FeedsSourceInterface:: |
|
FeedsPlugin:: |
public static | function |
Instantiates a FeedsPlugin object. Overrides FeedsConfigurable:: |
|
FeedsPlugin:: |
public static | function | Loads on-behalf implementations from mappers/ directory. | |
FeedsPlugin:: |
public | function | Returns the plugin definition. | |
FeedsPlugin:: |
public | function |
Save changes to the configuration of this object.
Delegate saving to parent (= Feed) which will collect
information from this object by way of getConfig() and store it. Overrides FeedsConfigurable:: |
1 |
FeedsPlugin:: |
protected | function | Sets the plugin definition. | |
FeedsPlugin:: |
public | function |
Implements FeedsSourceInterface::sourceDefaults(). Overrides FeedsSourceInterface:: |
1 |
FeedsPlugin:: |
public | function |
A source is being deleted. Overrides FeedsSourceInterface:: |
2 |
FeedsPlugin:: |
public | function |
Callback methods, exposes source form. Overrides FeedsSourceInterface:: |
3 |
FeedsPlugin:: |
public | function |
Validation handler for sourceForm. Overrides FeedsSourceInterface:: |
2 |
FeedsPlugin:: |
public | function |
A source is being saved. Overrides FeedsSourceInterface:: |
2 |
FeedsPlugin:: |
public static | function | Determines the type of a plugin. | |
FeedsPlugin:: |
protected | function |
Constructs a FeedsPlugin object. Overrides FeedsConfigurable:: |
|
FeedsProcessor:: |
public | function | Bundle type this processor operates on. | |
FeedsProcessor:: |
public | function | Provides a list of bundle options for use in select lists. | |
FeedsProcessor:: |
public | function | Removes all imported items for a source. | |
FeedsProcessor:: |
protected | function | Creates a log entry for when an exception occurred during import. | |
FeedsProcessor:: |
protected | function | DEPRECATED: Creates a log message for exceptions during import. | |
FeedsProcessor:: |
public | function |
Overrides FeedsPlugin::dependencies(). Overrides FeedsPlugin:: |
1 |
FeedsProcessor:: |
protected | function | Returns the current language for entities. | |
FeedsProcessor:: |
public | function | Deletes feed items older than REQUEST_TIME - $time. | |
FeedsProcessor:: |
protected | function | Returns a string representation of an object or array for log messages. | |
FeedsProcessor:: |
protected | function | Returns a statically cached version of the source mappings. | |
FeedsProcessor:: |
protected | function | Returns a statically cached version of the target mappings. | |
FeedsProcessor:: |
protected | function | Retrieves the MD5 hash of $entity_id from the database. | |
FeedsProcessor:: |
protected | function | Allows other modules to expose targets. | |
FeedsProcessor:: |
public | function | Report number of items that can be processed per call. | |
FeedsProcessor:: |
public | function | Get mappings. | |
FeedsProcessor:: |
protected | function | Returns the values from the parser, or callback. | |
FeedsProcessor:: |
protected | function | Create MD5 hash of item and mappings array. | |
FeedsProcessor:: |
protected | function | Initializes the list of entities to remove. | 1 |
FeedsProcessor:: |
public | function | Counts the number of items imported by this processor. | |
FeedsProcessor:: |
protected | function | Loads existing entity information and places it on $entity->feeds_item. | |
FeedsProcessor:: |
protected | function | Execute mapping on an item. | 1 |
FeedsProcessor:: |
protected | function | Maps values onto the target item. | |
FeedsProcessor:: |
protected | function | Adds Feeds specific information on $entity->feeds_item. | |
FeedsProcessor:: |
public | function |
Implements FeedsPlugin::pluginType(). Overrides FeedsPlugin:: |
|
FeedsProcessor:: |
public | function | Process the result of the parsing stage. | |
FeedsProcessor:: |
protected | function | Returns all mapping targets that are marked as unique. | |
FeedsProcessor:: |
protected | function | Helper function to unravel error messages hidden in a FieldValidationException. | |
FeedsProcessor:: |
public | function |
Validates the configuration. Overrides FeedsConfigurable:: |
|
FeedsProcessor:: |
protected | function | Validates fields of an entity. |