? atom_views_feed.patch
Index: atom.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/atom/atom.module,v
retrieving revision 1.20
diff -u -p -r1.20 atom.module
--- atom.module	13 Jan 2006 04:03:55 -0000	1.20
+++ atom.module	24 Jun 2006 07:44:02 -0000
@@ -134,53 +134,13 @@ function _atom_print_feed($nodes, $feed_
   $output = '';
   $last_mod = 0;
   while ($node = db_fetch_object($nodes)) {
-    $item = node_load(array('nid' => $node->nid));
-    $link = url("node/$node->nid", NULL, NULL, true);
+    $output .= _atom_print_node($node, $last_mod);
+  }
 
-    if (node_hook($item, 'view')) {
-      node_invoke($item, 'view', TRUE, FALSE);
-    }
-    else {
-      $item = node_prepare($item, TRUE);
-    }
+  _atom_print_feed_output($output, $feed_info, $last_mod);
+}
 
-    // Allow modules to change $node->teaser before viewing.
-    node_invoke_nodeapi($item, 'view', true, false);
-
-    $output .= "  <entry>\n";
-    $output .= '    <title>'. check_plain(strip_tags($item->title)) ."</title>\n";
-    $output .= '    <link rel="alternate" type="text/html" href="'. $link .'" />'. "\n";
-    $output .= '    <id>'. $link ."</id>\n";
-    $output .= '    <published>'. _atom_timestamp2w3dtf($item->created) ."</published>\n";
-    $output .= '    <updated>'. _atom_timestamp2w3dtf($item->changed) ."</updated>\n";
-    $last_mod = $item->changed;
-    $output .= "    <author>\n";
-    if ($item->name) {
-      $output .= '      <name>'. $item->name ."</name>\n";
-    }
-    else {
-      $output .= '      <name>'. variable_get('anonymous', 'Anonymous') ."</name>\n";
-    }
-    $output .= "    </author>\n";
-    if (module_exist('taxonomy')) {
-      $terms = taxonomy_node_get_terms($item->nid);
-      foreach ($terms as $term) {
-        $output .= '    <category term="'. check_plain(strip_tags($term->name)) . '" />'. "\n";
-      }
-    }
-    // Summary
-    $output .= '    <summary type="html"><![CDATA[';
-    $output .= check_markup($item->teaser);
-    $output .= "    ]]></summary>\n";
-
-    // Body
-    $output .= '    <content type="html"><![CDATA[';
-    $output .= variable_get('atom_ad_location', 'off') == 'prepend' ? str_replace('%link', urlencode($link), str_replace('%id', $node->nid, variable_get('atom_ad_code', ''))) : '';
-    $output .= check_markup($item->body);
-    $output .= variable_get('atom_ad_location', 'off') == 'append' ? str_replace('%link', urlencode($link), str_replace('%id', $node->nid, variable_get('atom_ad_code', ''))) : '';
-    $output .= "    ]]></content>\n";
-    $output .= "  </entry>\n";
-  }
+function _atom_print_feed_output($output, $feed_info, $last_mod) {
 
   drupal_set_header('Content-Type: application/xml');
 
@@ -196,6 +156,55 @@ function _atom_print_feed($nodes, $feed_
   print "</feed>\n";
 }
 
+function _atom_print_node($node, &$last_mod) {
+  $item = node_load(array('nid' => $node->nid));
+  $link = url("node/$node->nid", NULL, NULL, true);
+
+  if (node_hook($item, 'view')) {
+    node_invoke($item, 'view', TRUE, FALSE);
+  }
+  else {
+    $item = node_prepare($item, TRUE);
+  }
+
+  // Allow modules to change $node->teaser before viewing.
+  node_invoke_nodeapi($item, 'view', true, false);
+
+  $output .= "  <entry>\n";
+  $output .= '    <title>'. check_plain(strip_tags($item->title)) ."</title>\n";
+  $output .= '    <link rel="alternate" type="text/html" href="'. $link .'" />'. "\n";
+  $output .= '    <id>'. $link ."</id>\n";
+  $output .= '    <published>'. _atom_timestamp2w3dtf($item->created) ."</published>\n";
+  $output .= '    <updated>'. _atom_timestamp2w3dtf($item->changed) ."</updated>\n";
+  $last_mod = $item->changed;
+  $output .= "    <author>\n";
+  if ($item->name) {
+    $output .= '      <name>'. $item->name ."</name>\n";
+  }
+  else {
+    $output .= '      <name>'. variable_get('anonymous', 'Anonymous') ."</name>\n";
+  }
+  $output .= "    </author>\n";
+  if (module_exist('taxonomy')) {
+    $terms = taxonomy_node_get_terms($item->nid);
+    foreach ($terms as $term) {
+      $output .= '    <category term="'. check_plain(strip_tags($term->name)) . '" />'. "\n";
+    }
+  }
+  // Summary
+  $output .= '    <summary type="html"><![CDATA[';
+  $output .= check_markup($item->teaser);
+  $output .= "    ]]></summary>\n";
+
+  // Body
+  $output .= '    <content type="html"><![CDATA[';
+  $output .= variable_get('atom_ad_location', 'off') == 'prepend' ? str_replace('%link', urlencode($link), str_replace('%id', $node->nid, variable_get('atom_ad_code', ''))) : '';
+  $output .= check_markup($item->body);
+  $output .= variable_get('atom_ad_location', 'off') == 'append' ? str_replace('%link', urlencode($link), str_replace('%id', $node->nid, variable_get('atom_ad_code', ''))) : '';
+  $output .= "    ]]></content>\n";
+  $output .= "  </entry>\n";
+  return $output;
+}
 /**
  * @return string
  */
@@ -240,4 +249,64 @@ function atom_settings() {
   return $form;
 }
 
+
+/**
+ * Provide views plugins for the atom types we support.
+ */
+function atom_views_style_plugins() {
+  return array(
+    'atom' => array(
+      'name' => t('Atom: atom feed'),
+      'theme' => 'views_atom_feed',
+    ),
+  );
+}
+
+function atom_views_feed_argument($op, &$view, $arg) {
+  if ($op == 'argument' && $arg == 'atom') {
+    $view->page_type = 'atom';
+  }
+  else if ($op == 'post_view') {
+    $url = views_post_view_make_url($view, $arg, 'atom');
+    drupal_add_link(array('rel' => 'alternate',
+                          'type' => 'application/atom+xml',
+                          'title' => t('Atom'),
+                          'href' => url($url, NULL, NULL, TRUE)));
+    // There needs to be a separate atom feed icon!
+    return theme('feed_icon', url($url));
+  }
+}
+
+function theme_views_atom_feed($view, $nodes) {
+  // figure out the non-feed URL
+  $args = array();
+  foreach ($view->args as $id => $arg) {
+    if ($arg != 'atom') {
+      // This condition only happens if there are, for some odd reason,
+      // arguments *after* the feed. This is illogical but possible.
+      if ($skip) {
+        $args[] = '*';
+      }
+      $args[] = $arg;
+    }
+    else {
+      $skip = true;
+    }
+  }
+
+  $url = views_get_url($view, $args);
+  $feed_info = array();
+  $feed_info['title']    = views_get_title($view);
+  $feed_info['html_url'] = url($url, NULL, NULL, true);
+  $feed_info['atom_url'] = url($view->real_url, NULL, NULL, true);
+
+  $last_mod = 0;
+  foreach($nodes as $node) {
+    $output .= _atom_print_node($node, $last_mod);
+  }
+
+  _atom_print_feed_output($output, $feed_info, $last_mod);
+  exit;
+}
+
 ?>
