View source
<?php
define('FEEDS_UNPUBLISH_NON_EXISTENT', 'unpublish');
class FeedsNodeProcessor extends FeedsProcessor {
public function entityType() {
return 'node';
}
protected function entityInfo() {
$info = parent::entityInfo();
$info['label plural'] = t('Nodes');
return $info;
}
public function languageOptions() {
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;
}
return parent::languageOptions();
}
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);
$node->log = 'Created by FeedsNodeProcessor';
$node->uid = $this->config['author'];
return $node;
}
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);
if (!empty($node->menu)) {
$node->menu['enabled'] = (int) (bool) $node->menu['mlid'];
}
if ($this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {
$node->log = 'Updated by FeedsNodeProcessor';
}
else {
$node->log = 'Replaced by FeedsNodeProcessor';
}
return $node;
}
protected function entitySaveAccess($entity) {
if ($this->config['authorize'] && !empty($entity->uid)) {
$author = user_load($entity->uid);
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);
}
}
}
protected function entityValidate($entity, FeedsSource $source = NULL) {
if (!isset($entity->uid) || !is_numeric($entity->uid)) {
$entity->uid = $this->config['author'];
}
elseif (!is_int($entity->uid)) {
$entity->uid = (int) $entity->uid;
}
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) {
}
if (!empty($switcher)) {
$switcher
->switchBack();
}
if (!empty($e)) {
throw $e;
}
}
public function entitySave($entity) {
node_save($entity);
}
protected function entityDeleteMultiple($nids) {
node_delete_multiple($nids);
}
protected function expiryQuery(FeedsSource $source, $time) {
$select = parent::expiryQuery($source, $time);
$select
->condition('e.created', REQUEST_TIME - $time, '<');
return $select;
}
public function expiryTime() {
return $this->config['expire'];
}
public function configDefaults() {
return array(
'expire' => FEEDS_EXPIRE_NEVER,
'author' => 0,
'authorize' => TRUE,
) + parent::configDefaults();
}
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'],
);
if (isset($form['update_non_existent'])) {
$form['update_non_existent']['#options'][FEEDS_UNPUBLISH_NON_EXISTENT] = t('Unpublish non-existent nodes');
}
return $form;
}
public function configFormValidate(&$values) {
if ($author = user_load_by_name($values['author'])) {
$values['author'] = $author->uid;
}
else {
$values['author'] = 0;
}
}
public function configFormSubmit(&$values) {
if ($this->config['expire'] != $values['expire']) {
feeds_reschedule($this->id);
}
parent::configFormSubmit($values);
}
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':
$target_node->feeds_item->node_changed = feeds_to_unixtime($value, REQUEST_TIME);
break;
case 'feeds_source':
if ($id = feeds_get_importer_id($this
->bundle())) {
$class = get_class(feeds_importer($id)->fetcher);
$target_node->feeds[$class]['source'] = $value;
$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;
}
}
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)'),
);
if (module_exists('locale')) {
$targets['language'] = array(
'name' => t('Language'),
'description' => t('The two-character language code of the node.'),
);
}
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 ($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;
}
protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
if ($nid = parent::existingEntityId($source, $result)) {
return $nid;
}
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 $nid;
}
}
return 0;
}
protected function clean(FeedsState $state) {
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);
$node->feeds_item->hash = $this->config['update_non_existent'];
node_unpublish_action($node);
node_save($node);
$state->unpublished++;
}
}
}
}