From 68de746cf756e299aadb14aeb7247a0aefc58c29 Mon Sep 17 00:00:00 2001 From: Alex Barth <alex_b@53995.no-reply.drupal.org> Date: Thu, 8 Jul 2010 17:39:27 +0000 Subject: [PATCH] #753426 Monkey Master, andrewlevine, alex_b: Partial update of nodes. --- CHANGELOG.txt | 1 + plugins/FeedsNodeProcessor.inc | 56 +++++++++++++++++++++++----------- tests/feeds.test | 17 ++++++++--- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index c2da7f71..cae39247 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -3,6 +3,7 @@ Feeds 6.x 1.X XXXX ------------------ +- #753426 Monkey Master, andrewlevine, alex_b: Partial update of nodes. - #840626 alevine, alex_b: Support using same mapping target multiple times. - #624464 lyricnz, alex_b: Fix to "support tabs as delimiters". - #840350 lyricnz: (Optionally) Transliterate enclosure filenames to provide diff --git a/plugins/FeedsNodeProcessor.inc b/plugins/FeedsNodeProcessor.inc index 2580febd..d3e8c080 100644 --- a/plugins/FeedsNodeProcessor.inc +++ b/plugins/FeedsNodeProcessor.inc @@ -9,6 +9,11 @@ // Create or delete FEEDS_NODE_BATCH_SIZE at a time. define('FEEDS_NODE_BATCH_SIZE', 50); +// Updating mode for existing nodes. +define('FEEDS_NODE_SKIP_EXISTING', 0); +define('FEEDS_NODE_REPLACE_EXISTING', 1); +define('FEEDS_NODE_UPDATE_EXISTING', 2); + /** * Creates nodes from feed items. */ @@ -25,7 +30,7 @@ class FeedsNodeProcessor extends FeedsProcessor { while ($item = $batch->shiftItem()) { // Create/update if item does not exist or update existing is enabled. - if (!($nid = $this->existingItemId($item, $source)) || $this->config['update_existing']) { + if (!($nid = $this->existingItemId($item, $source)) || ($this->config['update_existing'] != FEEDS_NODE_SKIP_EXISTING)) { $node = $this->buildNode($nid, $source->feed_nid); // Only proceed if item has actually changed. @@ -142,7 +147,7 @@ class FeedsNodeProcessor extends FeedsProcessor { $type = isset($types['story']) ? 'story' : key($types); return array( 'content_type' => $type, - 'update_existing' => 0, + 'update_existing' => FEEDS_NODE_SKIP_EXISTING, 'expire' => FEEDS_EXPIRE_NEVER, 'mappings' => array(), 'author' => 0, @@ -179,9 +184,14 @@ class FeedsNodeProcessor extends FeedsProcessor { '#default_value' => $this->config['expire'], ); $form['update_existing'] = array( - '#type' => 'checkbox', + '#type' => 'radios', '#title' => t('Update existing items'), - '#description' => t('Check if existing items should be updated from the feed.'), + '#description' => t('Choose how existing items should be updated from the feed.'), + '#options' => array( + FEEDS_NODE_SKIP_EXISTING => 'Do not update existing item nodes', + FEEDS_NODE_REPLACE_EXISTING => 'Replace existing item nodes', + FEEDS_NODE_UPDATE_EXISTING => 'Update existing item nodes (slower than replacing them)', + ), '#default_value' => $this->config['update_existing'], ); return $form; @@ -291,21 +301,31 @@ class FeedsNodeProcessor extends FeedsProcessor { */ protected function buildNode($nid, $feed_nid) { $node = new stdClass(); - if (!empty($nid)) { - $node->nid = $nid; - $node->vid = db_result(db_query("SELECT vid FROM {node} WHERE nid = %d", $nid)); + if (empty($nid)) { + $node->created = FEEDS_REQUEST_TIME; + $populate = TRUE; } else { - $node->created = FEEDS_REQUEST_TIME; + if ($this->config['update_existing'] == FEEDS_NODE_UPDATE_EXISTING) { + $node = node_load($nid, NULL, TRUE); + } + else { + $node->nid = $nid; + $node->vid = db_result(db_query("SELECT vid FROM {node} WHERE nid = %d", $nid)); + $populate = TRUE; + } + } + if ($populate) { + $node->type = $this->config['content_type']; + $node->changed = FEEDS_REQUEST_TIME; + $node->feeds_node_item = new stdClass(); + $node->feeds_node_item->id = $this->id; + $node->feeds_node_item->imported = FEEDS_REQUEST_TIME; + $node->feeds_node_item->feed_nid = $feed_nid; + $node->feeds_node_item->url = ''; + $node->feeds_node_item->guid = ''; } - $node->type = $this->config['content_type']; - $node->changed = FEEDS_REQUEST_TIME; - $node->feeds_node_item = new stdClass(); - $node->feeds_node_item->id = $this->id; - $node->feeds_node_item->imported = FEEDS_REQUEST_TIME; - $node->feeds_node_item->feed_nid = $feed_nid; - $node->feeds_node_item->url = ''; - $node->feeds_node_item->guid = ''; + static $included; if (!$included) { module_load_include('inc', 'node', 'node.pages'); @@ -315,7 +335,9 @@ class FeedsNodeProcessor extends FeedsProcessor { // Populate properties that are set by node_object_prepare(). $node->log = 'Created/updated by FeedsNodeProcessor'; - $node->uid = $this->config['author']; + if ($populate) { + $node->uid = $this->config['author']; + } return $node; } diff --git a/tests/feeds.test b/tests/feeds.test index 694427b1..2b40c5b2 100644 --- a/tests/feeds.test +++ b/tests/feeds.test @@ -144,8 +144,8 @@ class FeedsRSStoNodesTest extends FeedsWebTestCase { $count = db_result(db_query("SELECT COUNT(*) FROM {feeds_node_item}")); $this->assertEqual($count, 10, 'Accurate number of items in database.'); - // Enable update existing and import updated feed file. - $this->setSettings('syndication', 'FeedsNodeProcessor', array('update_existing' => TRUE)); + // Enable replace existing and import updated feed file. + $this->setSettings('syndication', 'FeedsNodeProcessor', array('update_existing' => 1)); $feed_url = $GLOBALS['base_url'] .'/'. drupal_get_path('module', 'feeds') .'/tests/feeds/developmentseed_changes.rss2'; $this->editFeedNode($nid, $feed_url); $this->drupalPost('node/' . $nid . '/import', array(), 'Import'); @@ -185,6 +185,15 @@ class FeedsRSStoNodesTest extends FeedsWebTestCase { $count = db_result(db_query("SELECT COUNT(*) FROM {feeds_node_item} fi JOIN {node} n ON fi.nid = n.nid WHERE n.uid = %d", $author->uid)); $this->assertEqual($count, 10, 'Accurate number of items in database.'); + // Set to update existing, remove authorship of above nodes and import again. + $this->setSettings('syndication', 'FeedsNodeProcessor', array('update_existing' => 2)); + db_query("UPDATE {node} n JOIN {feeds_node_item} fi ON n.nid = fi.nid SET n.uid = 0, fi.hash=''"); + $this->drupalPost('node/'. $nid .'/import', array(), 'Import'); + $this->drupalGet('node'); + $this->assertNoPattern('/<span class="submitted">(.*?)'. check_plain($author->name) .'<\/span>/'); + $count = db_result(db_query("SELECT COUNT(*) FROM {feeds_node_item} fi JOIN {node} n ON fi.nid = n.nid WHERE n.uid = %d", $author->uid)); + $this->assertEqual($count, 0, 'Accurate number of items in database.'); + // Login with new user with only access content permissions. $this->drupalLogin( $this->drupalCreateUser() @@ -289,8 +298,8 @@ class FeedsRSStoNodesTest extends FeedsWebTestCase { $count = db_result(db_query("SELECT COUNT(*) FROM {feeds_node_item}")); $this->assertEqual($count, 10, 'Accurate number of items in database.'); - // Enable update existing and import updated feed file. - $this->setSettings('syndication_standalone', 'FeedsNodeProcessor', array('update_existing' => TRUE)); + // Enable replace existing and import updated feed file. + $this->setSettings('syndication_standalone', 'FeedsNodeProcessor', array('update_existing' => 1)); $feed_url = $GLOBALS['base_url'] .'/'. drupal_get_path('module', 'feeds') . '/tests/feeds/developmentseed_changes.rss2'; $this->importURL('syndication_standalone', $feed_url); $this->assertText('Updated 2 Story nodes.'); -- GitLab