diff --git a/plugins/FeedsNodeProcessor.inc b/plugins/FeedsNodeProcessor.inc index 37f8afe..82ca1aa 100644 --- a/plugins/FeedsNodeProcessor.inc +++ b/plugins/FeedsNodeProcessor.inc @@ -29,16 +29,23 @@ class FeedsNodeProcessor extends FeedsProcessor { $batch_size = variable_get('feeds_node_batch_size', FEEDS_NODE_BATCH_SIZE); if (!$batch->getTotal(FEEDS_PROCESSING)) { $batch->setTotal(FEEDS_PROCESSING, count($batch->items)); + if($this->config['synchronize']) { + $batch->processing_started = FEEDS_REQUEST_TIME; + } } while ($item = $batch->shiftItem()) { // Create/update if item does not exist or update existing is enabled. - if (!($nid = $this->existingItemId($batch, $source)) || ($this->config['update_existing'] != FEEDS_SKIP_EXISTING)) { - // Only proceed if item has actually changed. + + $skip_update = ($nid = $this->existingItemId($batch, $source)) && $this->config['update_existing'] === FEEDS_SKIP_EXISTING; + if (!$skip_update || $this->config['synchronize']) { $hash = $this->hash($item); - if (!empty($nid) && $hash == $this->getHash($nid)) { - continue; + // Only proceed if item has actually changed. + if(!$this->config['synchronize']) { + if (!empty($nid) && $hash == $this->getHash($nid)) { + continue; + } } $node = $this->buildNode($nid, $source->feed_nid); @@ -47,25 +54,30 @@ class FeedsNodeProcessor extends FeedsProcessor { // Map and save node. If errors occur don't stop but report them. try { $this->map($batch, $node); - if ($this->config['authorize']) { - if (empty($node->nid)) { - $op = 'create'; + if($skip_update && $this->config['synchronize']) { + _feeds_nodeapi_node_processor($node, 'update'); + } + else { + if ($this->config['authorize']) { + if (empty($node->nid)) { + $op = 'create'; + } + else { + $op = 'update'; + } + $account = user_load($node->uid); + if (!node_access($op, $node, $account)) { + throw new Exception('User ' . $account->uid . ' not authorized to ' . $op . ' content type ' . $node->type); + } } - else { - $op = 'update'; + node_save($node); + if (!empty($nid)) { + $batch->updated++; } - $account = user_load($node->uid); - if (!node_access($op, $node, $account)) { - throw new Exception('User ' . $account->uid . ' not authorized to ' . $op . ' content type ' . $node->type); + else { + $batch->created++; } } - node_save($node); - if (!empty($nid)) { - $batch->updated++; - } - else { - $batch->created++; - } } catch (Exception $e) { drupal_set_message($e->getMessage(), 'warning'); @@ -81,7 +93,22 @@ class FeedsNodeProcessor extends FeedsProcessor { } } + if($this->config['synchronize']) { + $result = db_query("SELECT nid FROM {feeds_node_item} WHERE id = '%s' AND imported < %d", $this->id, $batch->processing_started); + $nids = array(); + while($nid = db_result($result)) { + $nids[] = $nid; + } + $batch->deleted = count($nids); + foreach($nids as $nid) { + _feeds_node_delete($nid); + } + } + // Set messages. + if ($batch->deleted) { + drupal_set_message(format_plural($batch->created, 'Deleted @number @type node.', 'Deleted @number @type nodes.', array('@number' => $batch->created, '@type' => node_get_types('name', $this->config['content_type'])))); + } if ($batch->created) { drupal_set_message(format_plural($batch->created, 'Created @number @type node.', 'Created @number @type nodes.', array('@number' => $batch->created, '@type' => node_get_types('name', $this->config['content_type'])))); } @@ -164,6 +191,7 @@ class FeedsNodeProcessor extends FeedsProcessor { 'mappings' => array(), 'author' => 0, 'authorize' => 0, + 'synchronize' => FALSE, ); } @@ -225,6 +253,15 @@ class FeedsNodeProcessor extends FeedsProcessor { ), '#default_value' => $this->config['update_existing'], ); + $form['synchronize'] = array( + '#type' => 'checkbox', + '#title' => t('Synchronize nodes'), + '#description' => t( + 'Nodes will be deleted if they can no logner be found in source feed, requires that guid is mapped and set as a "unique target".' + ), + '#default_value' => $this->config['synchronize'], + ); + return $form; }