/[drupal]/contributions/modules/atom/atom.pages.inc
ViewVC logotype

Contents of /contributions/modules/atom/atom.pages.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.11 - (show annotations) (download) (as text)
Wed Oct 7 02:23:04 2009 UTC (7 weeks, 3 days ago) by davereid
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +2 -2 lines
File MIME type: text/x-php
by Dave Reid: Title should be text format, not HTML.
1 <?php
2 // $Id: atom.pages.inc,v 1.10 2009/09/21 22:32:17 davereid Exp $
3
4 /**
5 * @file
6 * Various non-administration page callbacks for the atom module.
7 */
8
9 /**
10 * Produces an atom 1.0 feed for the front page content.
11 */
12 function atom_feed() {
13 $query = db_select('node');
14 $query->addField('node', 'nid');
15 $query->condition('promote', 1);
16 $query->condition('status', 1);
17 $query->orderBy('sticky', 'DESC');
18 $query->orderBy('created', 'DESC');
19 $query->range(0, variable_get('atom_feed_entries', 15));
20 $query->addTag('node_access');
21 $nodes = $query->execute()->fetchCol();
22
23 $feed_info = array();
24 $feed_info['html_url'] = url('', array('absolute' => TRUE));
25 $feed_info['atom_url'] = url('atom.xml', array('absolute' => TRUE));
26 _atom_print_feed($nodes, $feed_info);
27 }
28
29 function atom_blog_feed() {
30 $query = db_select('node');
31 $query->addField('node', 'nid');
32 $query->condition('type', 'blog');
33 $query->condition('status', 1);
34 $query->orderBy('sticky', 'DESC');
35 $query->orderBy('created', 'DESC');
36 $query->range(0, variable_get('atom_feed_entries', 15));
37 $query->addTag('node_access');
38 $nodes = $query->execute()->fetchCol();
39
40 $feed_info = array();
41 $feed_info['title'] = t('!site_name blogs', array('!site_name' => variable_get('site_name', 'Drupal')));
42 $feed_info['html_url'] = url('blog', array('absolute' => TRUE));
43 $feed_info['atom_url'] = url('blog/atom.xml', array('absolute' => TRUE));
44 _atom_print_feed($nodes, $feed_info);
45 }
46
47 function atom_node_feed(stdClass $node) {
48 if (!node_access('view', $node)) {
49 return drupal_access_denied();
50 }
51
52 $nodes = array($node->nid);
53
54 $feed_info = array();
55 $feed_info['html_url'] = url('node/' . $node->nid, array('absolute' => TRUE));
56 $feed_info['atom_url'] = url('node/' . $node->nid . '/atom.xml', array('absolute' => TRUE));
57 _atom_print_feed($nodes, $feed_info);
58 }
59
60 function atom_user_blog_feed(stdClass $account) {
61 $query = db_select('node');
62 $query->addField('node', 'nid');
63 $query->condition('type', 'blog');
64 $query->condition('uid', $account->uid);
65 $query->condition('status', 1);
66 $query->orderBy('sticky', 'DESC');
67 $query->orderBy('created', 'DESC');
68 $query->range(0, variable_get('atom_feed_entries', 15));
69 $query->addTag('node_access');
70 $nodes = $query->execute()->fetchCol();
71
72 $feed_info = array();
73 $feed_info['title'] = t("!name's blog", array('!name' => $account->name));
74 $feed_info['subtitle'] = '';
75 $feed_info['html_url'] = url('blog/' . $account->uid, array('absolute' => TRUE));
76 $feed_info['atom_url'] = url("blog/' . $account->uid . '/atom.xml", array('absolute' => TRUE));
77 _atom_print_feed($nodes, $feed_info);
78 }
79
80 function atom_taxonomy_feed(stdClass $term) {
81 $nodes = taxonomy_select_nodes(array($term->tid), NULL, NULL, TRUE);
82
83 $feed_info['title'] = $term->name;
84 $feed_info['subtitle'] = $term->description;
85 $feed_info['html_url'] = url('taxonomy/term/' . $term->tid, array('absolute' => TRUE));
86 $feed_info['atom_url'] = url("taxonomy/term/' . $term->tid . '/atom.xml", array('absolute' => TRUE));
87 _atom_print_feed($nodes, $feed_info);
88 }
89
90 function _atom_print_feed(array $nids, array $feed_info) {
91 _atom_contrib_load();
92 $feed_info['extra_ns'] = _atom_contrib_get_ns();
93 $output = '';
94 $last_mod = 0;
95
96 $nodes = node_load_multiple($nids);
97 foreach ($nodes as $node) {
98 $item_text = '';
99
100 $node->link = url("node/$node->nid", array('absolute' => TRUE));
101
102 node_build_content($node, 'rss');
103
104 // Allow modules to change $node, or add elements,
105 // specifically for an atom feed.
106 $extra = module_invoke_all('atom_feed', $node);
107
108 if (!empty($node->content)) {
109 // We render node contents and force links to be last.
110 $links = drupal_render($node->content['links']);
111 $item_text .= drupal_render($node->content) . $links;
112 }
113
114 $item = array(
115 'published' => $node->created,
116 'updated' => $node->changed,
117 'author' => $node->name ? $node->name : variable_get('anonymous', 'Anonymous'),
118 'content' => $item_text,
119 // @todo How to have summary and/or full body?
120 );
121
122 // @todo Now that taxonomy terms are fields, this does not work.
123 //if (module_exists('taxonomy')) {
124 // $terms = taxonomy_node_get_terms($node);
125 // foreach ($terms as $term) {
126 // $extra[] = array('key' => 'category', 'attributes' => array('term' => $term->name));
127 // }
128 //}
129
130 $output .= theme('atom_feed_item', $node->title, $node->link, $item, $extra);
131 }
132
133 // Merge some default values.
134 $feed_info += array(
135 'title' => variable_get('site_name', 'Drupal'),
136 'subtitle' => variable_get('site_slogan', ''),
137 );
138
139 $output = theme('atom_feed', $feed_info, $output);
140
141 drupal_set_header('Content-Type: application/atom+xml; charset=utf-8');
142 print $output;
143 }
144
145 function theme_atom_feed(array $feed_info, $items) {
146 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
147 $output .= '<feed xmlns="http://www.w3.org/2005/Atom">'."\n";
148 $output .= ' <title type="text">'. check_plain($feed_info['title']) ."</title>\n";
149 if ($feed_info['subtitle']) {
150 $output .= ' <subtitle type="text">' . check_plain($feed_info['subtitle']) . "</subtitle>\n";
151 }
152 $output .= ' <link rel="alternate" type="text/html" href="'. check_plain($feed_info['html_url']) .'" />'."\n";
153 $output .= ' <link rel="self" type="application/atom+xml" href="'. check_plain($feed_info['atom_url']) .'" />'."\n";
154 //$output .= ' <generator version="' . VERSION . '" uri="http://drupal.org">Drupal</generator>'."\n";
155 $output .= ' <id>'. check_plain($feed_info['atom_url']) ."</id>\n";
156 $output .= ' <updated>'. gmdate(DATE_ATOM, time()) ."</updated>\n";
157 $output .= $items;
158 $output .= "</feed>\n";
159 return $output;
160 }
161
162 function theme_atom_feed_item($title, $link, array $item, array $extra) {
163 $item += array(
164 'id' => $link,
165 'summary' => '',
166 'content' => '',
167 'author' => '',
168 );
169
170 $output = " <entry>\n";
171 $output .= " <id>" . check_plain($item['id']) . "</id>\n";
172 $output .= " <link rel=\"alternate\" type=\"text/html\" href=\"" . check_plain($link) . "\" />\n";
173 $output .= " <published>" . gmdate(DATE_ATOM, $item['published']) . "</published>\n";
174 $output .= " <updated>" . gmdate(DATE_ATOM, $item['updated']) . "</updated>\n";
175 $output .= " <title type=\"text\">" . check_plain($title) . "</title>\n";
176 if ($item['author']) {
177 $output .= " <author><name>" . check_plain($item['author']) . "</name></author>\n";
178 }
179 if ($item['summary']) {
180 $output .= " <summary type=\"xhtml\"><div xmlns=\"http://www.w3.org/1999/xhtml\">" . $item['summary'] . "</div></summary>\n";
181 }
182 if ($item['content']) {
183 $output .= " <content type=\"xhtml\"><div xmlns=\"http://www.w3.org/1999/xhtml\">" . $item['content'] . "</div></content>\n";
184 }
185 if ($extra) {
186 $output .= format_xml_elements($extra);
187 }
188 $output .= " </entry>\n";
189 return $output;
190 }
191
192 /**
193 * @return string any additional namespaces used by contrib modules.
194 */
195 function _atom_contrib_get_ns() {
196 _atom_contrib_load();
197
198 $ns_array = module_invoke_all('atom_ns');
199 return count($ns_array) > 0 ? "\n " . implode("\n ", $ns_array) : '';
200 }

  ViewVC Help
Powered by ViewVC 1.1.2