Index: feedapi_inherit.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/feedapi/feedapi_inherit/Attic/feedapi_inherit.module,v
retrieving revision 1.1.2.10
diff -u -r1.1.2.10 feedapi_inherit.module
--- feedapi_inherit.module	30 Oct 2007 22:11:22 -0000	1.1.2.10
+++ feedapi_inherit.module	27 Nov 2007 20:17:53 -0000
@@ -1,16 +1,15 @@
 <?php
+// $Id:
 
 /**
  * @file
  * Inherit group and taxonomy settings from feeds to news items
- * This is an additional processor for FeedAPI
+ * This is an add on processor for FeedAPI
  * 
  * @author
  *  Aron Novak <aron at novaak dot net>
  * 
  * Sponsored by Development Seed
- * 
- * Todo: use nodeapi() hooks instead of feedapi() hooks. 
  */
 
 /**
@@ -19,7 +18,7 @@
 function feedapi_inherit_help($section) {
   switch ($section) {
     case 'admin/help#feedapi_inherit':
-      return t('Emulates the Drupal core aggregator with the FeedAPI module.');
+      return t('FeedAPI Inherit - pass on taxonomy and organic group information from feed to feed items.');
       break;
     case 'feedapi/full_name':
       return t('FeedAPI Inherit - pass on taxonomy and organic group information from feed to feed items.');
@@ -31,123 +30,25 @@
  * Implementation of hook_nodeapi().
  */
 function feedapi_inherit_nodeapi(&$node, $op) {
-  if (isset($node->feed) || feedapi_enabled($node->type)) {
-    switch ($op) {
-      case 'update':
-        // It is updated. All the childrens should be updated
-        $feed = $node;
-        // Save them ...
-        $taxonomy = $feed->taxonomy;
-        $groups_names = $feed->og_groups_names;
-        $groups = $feed->og_groups;
-        // , because this call causes taxonomy and group changes to disapper, we have to save them
-        feedapi_invoke_feedapi('load', $feed);
-        // Restore
-        $feed->taxonomy = $taxonomy;
-        $feed->og_groups = $groups;
-        $feed->og_groups_names = $groups_names;
-        foreach ($feed->items as $item) {
-          _feedapi_inherit_do_inherit($item, $feed);
+  switch ($op) {
+    case 'prepare':
+      if ($node->feedapi->feed_nid) {
+        $feed_node = node_load($node->feedapi->feed_nid);
+        if (feedapi_enabled($feed_node->type, 'feedapi_inherit')) {
+          _feedapi_inherit_do_inherit($node, $feed_node);
         }
-        break;
-    }
+      }
+      break;
   }
 }
 
 /**
  * Implementation of hook_feedapi_item().
+ * Do nothing here, this function only makes us a feedapi processor,
+ * which means that we can be enabled/disabled on a per node type 
+ * basis.
  */
 function feedapi_inherit_feedapi_item($op) {
-  switch ($op) {
-    case 'type':
-      return array("XML feed");
-      break;
-    case 'unique':
-      return TRUE;
-      break;
-    case 'load':
-      break;
-    case 'fetch':
-      return array();
-    default:
-      $args = array_slice(func_get_args(), 1);
-      return call_user_func_array('_feedapi_inherit_do_inherit', $args);
-  }
-}
-
-/**
- * Inherit the feed's taxonomy and og data to the given feed item
- */
-function _feedapi_inherit_do_inherit($feed_item, $feed_nid) {
-  // Get the feed node object
-  if (is_numeric($feed_nid)) {
-    $feed_node = node_load($feed_nid);
-  }
-  else if (is_object($feed_nid)) {
-    $feed_node = $feed_nid;
-    $feed_nid = $feed_node->nid;
-  }
-  else {
-    return;
-  }
-  
-  $inherit_taxonomy = TRUE;
-  $inherit_og = TRUE;
-  
-  if ($feed_node->feed->settings['processors']['feedapi_inherit']) {
-    $inherit_taxonomy = $feed_node->feed->settings['processors']['feedapi_inherit']['inherit_taxonomy'];
-    $inherit_og = $feed_node->feed->settings['processors']['feedapi_inherit']['inherit_og'];
-  }
-  
-  // Todo: pass on results from previous stages so that we don't need to look up
-  // URL in the db - think of duplicates!
-  if ($feed_item = _feedapi_inherit_locate_feedapi_node($feed_item)) {
-    $update = FALSE;
-    // Pass on taxonomy data
-    if ($inherit_taxonomy && module_exists('taxonomy')) {
-      $terms = taxonomy_node_get_terms($feed_nid);
-      $tids = array();
-      foreach ($terms as $tid => $term) {
-        if (!$feed_item->taxonomy[$tid]) {
-          $feed_item->taxonomy[$tid] = $term;
-          $update = TRUE;
-        }
-      }
-    }
-    // Pass on group data
-    if ($inherit_og && module_exists('og')) {
-      if (isset($feed_node->og_public) ) {
-        if (!isset($feed_item->og_public)) {
-          $feed_item->og_public = 1;
-        }
-        $feed_item->og_public = $feed_item->og_public & $feed_node->og_public;
-      }
-      if (isset($feed_node->og_groups)) {
-        if (!is_array($feed_item->og_groups)) {
-          $feed_item->og_groups = array();
-        }
-        $feed_item->og_groups = array_merge($feed_node->og_groups, $feed_item->og_groups);
-        foreach ($feed_item->og_groups as $gid) {
-          if ($gid != 0) {
-            $transformed_groups[$gid] = $gid;
-          }
-        }
-        $feed_item->og_groups = $transformed_groups;
-      }
-      if (isset($feed_node->og_groups_names)) {
-        if (!is_array($feed_item->og_groups_names)) {
-          $feed_item->og_groups_names = array();
-        }
-        $feed_item->og_groups_names = array_merge($feed_node->og_groups_names, $feed_item->og_groups_names);
-      }
-      // update in any case when the previous or this pass has an effect
-      $update = TRUE;
-    }
-  }
-  // node_save is a long process, only call when it's a must
-  if ($update) {
-    node_save($feed_item);
-  }
 }
 
 /**
@@ -177,19 +78,53 @@
 }
 
 /**
- * Get the node of the news item which is created by feedapi_node
+ * Inherit the feed's taxonomy and og data to the given feed item.
+ * Todo: split this into a function for taxonomy and another one for og - easier maintainable.
  */
-function _feedapi_inherit_locate_feedapi_node($feed_item) {
-  if (empty($feed_item->options->guid) || empty($feed_item->options->original_url)) {
-    $nid = $feed_item->nid;
-  }
-  else if ($feed_item->options->guid) {
-    $nid = db_result(db_query("SELECT nid FROM {feedapi_node_item} WHERE guid = '%s'", $feed_item->options->guid));
+function _feedapi_inherit_do_inherit(&$item_node, $feed_node) {
+  $inherit_taxonomy = TRUE;
+  $inherit_og = TRUE;
+  if ($feed_node->feed->settings['processors']['feedapi_inherit']) {
+    $inherit_taxonomy = $feed_node->feed->settings['processors']['feedapi_inherit']['inherit_taxonomy'];
+    $inherit_og = $feed_node->feed->settings['processors']['feedapi_inherit']['inherit_og'];
   }
-  else {
-    $nid = db_result(db_query("SELECT nid FROM {feedapi_node_item} WHERE url = '%s'", $feed_item->options->original_url));
+
+  // Pass on taxonomy data
+  if ($inherit_taxonomy && module_exists('taxonomy')) {
+    $terms = taxonomy_node_get_terms($feed_node->nid);
+    $tids = array();
+    foreach ($terms as $tid => $term) {
+      if (!$item_node->taxonomy[$tid]) {
+        $item_node->taxonomy[$tid] = $term;
+      }
+    }
   }
-  if ($nid) {
-    return node_load($nid);
+  // Pass on group data
+  if ($inherit_og && module_exists('og')) {
+    if (isset($feed_node->og_public) ) {
+      if (!isset($item_node->og_public)) {
+        $item_node->og_public = 1;
+      }
+      $item_node->og_public = $item_node->og_public & $feed_node->og_public;
+    }
+    if (isset($feed_node->og_groups)) {
+      if (!is_array($item_node->og_groups)) {
+        $item_node->og_groups = array();
+      }
+      $item_node->og_groups = array_merge($feed_node->og_groups, $item_node->og_groups);
+      foreach ($item_node->og_groups as $gid) {
+        if ($gid != 0) {
+          $transformed_groups[$gid] = $gid;
+        }
+      }
+      $item_node->og_groups = $transformed_groups;
+    }
+    if (isset($feed_node->og_groups_names)) {
+      if (!is_array($item_node->og_groups_names)) {
+        $item_node->og_groups_names = array();
+      }
+      $item_node->og_groups_names = array_merge($feed_node->og_groups_names, $item_node->og_groups_names);
+    }
   }
 }
+
