| 1 |
<?php
|
| 2 |
// $Id: commentrss.module,v 1.17 2007/07/15 20:44:59 tangent Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Publishes RSS feeds for comments.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_help().
|
| 11 |
*/
|
| 12 |
function commentrss_help($section) {
|
| 13 |
$output = '';
|
| 14 |
switch ($section) {
|
| 15 |
case 'admin/modules#description':
|
| 16 |
$output = t('Provides RSS feeds for comments');
|
| 17 |
break;
|
| 18 |
case 'admin/content/commentrss':
|
| 19 |
$output = '<p>' . t('Select the types of comment feeds to be enabled.') . '</p>';
|
| 20 |
break;
|
| 21 |
case 'admin/help#commentrss':
|
| 22 |
$output = '<p>' . t('The commentrss module provides RSS feeds for comments. Comment feeds are published on pages which also provide a feed of nodes. Some comment feeds may be disabled if the are not needed.') . '</p>';
|
| 23 |
$output .= '<p>' . t('If comment feeds are enabled for individual nodes then a "wfw:commentRss" element is added to node feeds so that aggregators may discover the corresponding comment feed.') . '</p>';
|
| 24 |
$output .= '<p>' . t('If the !link module is installed then a comment feed will be added to complement the node feed provided by that module.', array('!link' => l('vocabulary_list', 'http://drupal.org/project/vocabulary_list'))) . '</p>';
|
| 25 |
break;
|
| 26 |
}
|
| 27 |
return $output;
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Implementation of hook_menu().
|
| 32 |
*/
|
| 33 |
function commentrss_menu($may_cache) {
|
| 34 |
$items = array();
|
| 35 |
if ($may_cache) {
|
| 36 |
$items[] = array(
|
| 37 |
'path' => 'admin/content/commentrss',
|
| 38 |
'title' => t('Comment RSS publishing'),
|
| 39 |
'description' => t('Configure RSS feeds for comments.'),
|
| 40 |
'callback' => 'drupal_get_form',
|
| 41 |
'callback arguments' => 'commentrss_admin_settings',
|
| 42 |
'access' => user_access('administer site configuration'),
|
| 43 |
'type' => MENU_NORMAL_ITEM);
|
| 44 |
$items[] = array('path' => 'crss', 'title' => '',
|
| 45 |
'callback' => 'commentrss_handler',
|
| 46 |
'access' => user_access('access content'),
|
| 47 |
'type' => MENU_CALLBACK);
|
| 48 |
}
|
| 49 |
else {
|
| 50 |
if (arg(0) == 'node' && is_numeric(arg(1)) && variable_get('commentrss_node', TRUE)) {
|
| 51 |
$node = node_load(arg(1));
|
| 52 |
if ($node->nid && $node->comment != COMMENT_NODE_DISABLED) {
|
| 53 |
drupal_add_feed(url('crss/node/'. $node->nid),
|
| 54 |
t('Comments for @title', array('@title' => $node->title)));
|
| 55 |
}
|
| 56 |
}
|
| 57 |
elseif (arg(0) == 'node' && variable_get('commentrss_site', TRUE)) {
|
| 58 |
drupal_add_feed(url('crss'),
|
| 59 |
t('@site - All comments', array('@site' => variable_get('site_name', 'Drupal'))));
|
| 60 |
}
|
| 61 |
elseif (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) && variable_get('commentrss_term', FALSE)) {
|
| 62 |
$term = taxonomy_get_term(arg(2));
|
| 63 |
if ($term->tid) {
|
| 64 |
drupal_add_feed(url('crss/term/'. $term->tid),
|
| 65 |
t('Comments for category "@title"', array('@title' => $term->name)));
|
| 66 |
}
|
| 67 |
}
|
| 68 |
elseif (arg(0) == 'taxonomy' && arg(1) == 'vocabulary' && is_numeric(arg(2)) && variable_get('commentrss_node', FALSE)) {
|
| 69 |
// vocabulary_list module is serving this page
|
| 70 |
drupal_add_feed(url('crss/vocab/'. arg(2)), t('Comments for this vocabulary'));
|
| 71 |
}
|
| 72 |
}
|
| 73 |
return $items;
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Menu callback; publish an RSS feed
|
| 78 |
*/
|
| 79 |
function commentrss_handler($type = 'site') {
|
| 80 |
if ($type && variable_get('commentrss_'. $type, FALSE)) {
|
| 81 |
if (call_user_func('commentrss_feed_'. $type)) {
|
| 82 |
return;
|
| 83 |
}
|
| 84 |
}
|
| 85 |
drupal_not_found();
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Publish a feed for all comments
|
| 90 |
*/
|
| 91 |
function commentrss_feed_site() {
|
| 92 |
$items = commentrss_format_items('n.nid');
|
| 93 |
commentrss_format_feed($items);
|
| 94 |
return TRUE;
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Publish a feed for all comments of a specified node
|
| 99 |
*/
|
| 100 |
function commentrss_feed_node() {
|
| 101 |
global $base_url;
|
| 102 |
|
| 103 |
if (is_numeric(arg(2))) {
|
| 104 |
$node = node_load(arg(2));
|
| 105 |
if ($node->nid) {
|
| 106 |
$items = commentrss_format_items('n.nid', '', 'n.nid = %d AND', $node->nid);
|
| 107 |
$extra_defaults = array(
|
| 108 |
array('key' => 'pubDate', 'value' => date('r', $node->created)),
|
| 109 |
array('key' => 'dc:creator', 'value' => $node->name),
|
| 110 |
array('key' => 'guid', 'attributes' => array('isPermaLink' => 'false'),
|
| 111 |
'value' => url('node/'. $node->nid))
|
| 112 |
);
|
| 113 |
$extra = node_invoke_nodeapi($node, 'rss item');
|
| 114 |
$extra = array_merge($extra, $extra_defaults);
|
| 115 |
$namespaces = array();
|
| 116 |
foreach ($extra as $element) {
|
| 117 |
if ($element['namespace']) {
|
| 118 |
$namespaces[] = $element['namespace'];
|
| 119 |
}
|
| 120 |
}
|
| 121 |
$channel = array(
|
| 122 |
'title' => t('@site - Comments for "@title"', array(
|
| 123 |
'@site' => check_plain(variable_get('site_name', 'Drupal')),
|
| 124 |
'@title' => check_plain($node->title))),
|
| 125 |
'description' => t('Comments for "@title"', array('@title' => $node->title)),
|
| 126 |
'link' => url('node/'. $node->nid, NULL, NULL, TRUE),
|
| 127 |
);
|
| 128 |
|
| 129 |
commentrss_format_feed($items, $channel, $namespaces);
|
| 130 |
return TRUE;
|
| 131 |
}
|
| 132 |
}
|
| 133 |
return FALSE;
|
| 134 |
}
|
| 135 |
|
| 136 |
/**
|
| 137 |
* Publish a feed for all comments with a specified term
|
| 138 |
*/
|
| 139 |
function commentrss_feed_term() {
|
| 140 |
if (is_numeric(arg(2))) {
|
| 141 |
$term = taxonomy_get_term(arg(2));
|
| 142 |
if ($term->tid) {
|
| 143 |
$items = commentrss_format_items('DISTINCT(n.nid)', 'INNER JOIN {term_node} r ON n.nid = r.nid', 'r.tid = %d AND', $term->tid);
|
| 144 |
$channel = array(
|
| 145 |
'title' => t('@site - Comments for "@term"', array(
|
| 146 |
'@site' => check_plain(variable_get('site_name', 'Drupal')),
|
| 147 |
'@term' => check_plain($term->name))),
|
| 148 |
'description' => t('Comments for the category "@term"', array('@term' => $term->name)),
|
| 149 |
'link' => url('taxonomy/term/'. $term->tid, NULL, NULL, TRUE),
|
| 150 |
);
|
| 151 |
|
| 152 |
commentrss_format_feed($items, $channel);
|
| 153 |
return TRUE;
|
| 154 |
}
|
| 155 |
}
|
| 156 |
return FALSE;
|
| 157 |
}
|
| 158 |
|
| 159 |
/**
|
| 160 |
* Publish a feed for all comments with any term in a specified vocabulary
|
| 161 |
*
|
| 162 |
* Requires vocabulary_list.module
|
| 163 |
*/
|
| 164 |
function commentrss_feed_vocab() {
|
| 165 |
if (module_exists('vocabulary_list') && is_numeric(arg(2))) {
|
| 166 |
$vocab = taxonomy_get_vocabulary(arg(2));
|
| 167 |
if ($vocab->vid) {
|
| 168 |
$items = commentrss_format_items('DISTINCT(n.nid)', 'INNER JOIN {term_node} r ON n.nid = r.nid INNER JOIN {term_data} d ON r.tid = d.tid', 'd.vid = %d AND', $vid);
|
| 169 |
$channel = array(
|
| 170 |
'title' => t('@site - Comments for "@vocab"', array(
|
| 171 |
'@site' => check_plain(variable_get('site_name', 'Drupal')),
|
| 172 |
'@vocab' => check_plain($vocab->name))),
|
| 173 |
'description' => t('Comments for all categories in "@vocab"', array('@vocab' => $vocab->name)),
|
| 174 |
'link' => url('taxonomy/vocabulary/'. $vocab->vid, NULL, NULL, TRUE),
|
| 175 |
);
|
| 176 |
|
| 177 |
commentrss_format_feed($items, $channel);
|
| 178 |
return TRUE;
|
| 179 |
}
|
| 180 |
}
|
| 181 |
return FALSE;
|
| 182 |
}
|
| 183 |
|
| 184 |
/**
|
| 185 |
* Format and print a comment feed
|
| 186 |
*/
|
| 187 |
function commentrss_format_feed($items, $channel = array(), $namespaces = array()) {
|
| 188 |
global $base_url, $locale;
|
| 189 |
|
| 190 |
$channel_defaults = array(
|
| 191 |
'version' => '2.0',
|
| 192 |
'title' => check_plain(variable_get('site_name', 'Drupal')) .' - '. t('Comments'),
|
| 193 |
'link' => $base_url,
|
| 194 |
'description' => t('Comments'),
|
| 195 |
'language' => $locale
|
| 196 |
);
|
| 197 |
$channel = array_merge($channel_defaults, $channel);
|
| 198 |
$namespaces = array_merge(array('xmlns:dc="http://purl.org/dc/elements/1.1/"'), $namespaces);
|
| 199 |
|
| 200 |
$output = '<?xml version="1.0" encoding="utf-8"?>'."\n";
|
| 201 |
$output .= '<rss version="'. $channel['version'] .'" xml:base="'. $base_url .'" '. implode(' ', $namespaces) . ">\n";
|
| 202 |
$output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language']);
|
| 203 |
$output .= "</rss>\n";
|
| 204 |
|
| 205 |
drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
|
| 206 |
print $output;
|
| 207 |
}
|
| 208 |
|
| 209 |
/**
|
| 210 |
* Format RSS for comments sent in reply to the nodes selected by the given SQL selector
|
| 211 |
*/
|
| 212 |
function commentrss_format_items($nidselector, $joins = '', $where = '', $param = NULL) {
|
| 213 |
global $base_url;
|
| 214 |
|
| 215 |
$items = '';
|
| 216 |
|
| 217 |
// Build SQL query from the passed elements
|
| 218 |
$SQL = 'SELECT '. $nidselector .', c.cid, c.subject, c.comment, c.timestamp, c.uid, c.name, c.format FROM {node} n ' . $joins . ' INNER JOIN {comments} c ON c.nid = n.nid WHERE ' . $where . ' n.status = 1 AND c.status = %d ORDER BY c.timestamp DESC';
|
| 219 |
|
| 220 |
// Add passed query parameter; plus we are looking for published comments
|
| 221 |
$params = isset($param) ? array($param) : array();
|
| 222 |
$params[] = COMMENT_PUBLISHED;
|
| 223 |
|
| 224 |
$comments = db_query_range(db_rewrite_sql($SQL), $params, COMMENT_PUBLISHED, 0, 15);
|
| 225 |
while ($comment = db_fetch_object($comments)) {
|
| 226 |
if ($comment->uid) {
|
| 227 |
if ($user = user_load(array('uid' => $comment->uid))) {
|
| 228 |
$comment->name = $user->name;
|
| 229 |
}
|
| 230 |
}
|
| 231 |
if (!$comment->name) {
|
| 232 |
$comment->name = variable_get('anonymous', 'Anonymous');
|
| 233 |
}
|
| 234 |
$extra = array(array('key' => 'pubDate', 'value' => date('r', $comment->timestamp)), array('key' => 'dc:creator', 'value' => $comment->name), array('key' => 'guid', 'value' => 'comment ' . $comment->cid . ' at ' . $base_url, 'attributes' => array('isPermaLink' => 'false')));
|
| 235 |
$link = url("node/{$comment->nid}", NULL, "comment-{$comment->cid}", TRUE);
|
| 236 |
$items .= format_rss_item($comment->subject, $link, check_markup($comment->comment, $comment->format, FALSE), $extra);
|
| 237 |
}
|
| 238 |
return $items;
|
| 239 |
}
|
| 240 |
|
| 241 |
/**
|
| 242 |
* Implementation of hook_nodeapi().
|
| 243 |
*/
|
| 244 |
function commentrss_nodeapi(&$node, $op) {
|
| 245 |
if ($op == 'rss item') {
|
| 246 |
// Add a wfw:commentRss element to node feed entries if:
|
| 247 |
// node comments are enabled for the node
|
| 248 |
// comment feeds for individual nodes are enabled
|
| 249 |
if ($node->comment != COMMENT_NODE_DISABLED && variable_get('commentrss_node', TRUE)) {
|
| 250 |
// The "wfw" namespace is added to each element due to a bug in node_feed().
|
| 251 |
// See http://drupal.org/node/157709
|
| 252 |
return array(array(
|
| 253 |
'key' => 'wfw:commentRss',
|
| 254 |
'attributes' => array('xmlns:wfw' => 'http://wellformedweb.org/CommentAPI/'),
|
| 255 |
'value' => url('crss/node/'. $node->nid, NULL, NULL, TRUE)));
|
| 256 |
}
|
| 257 |
}
|
| 258 |
}
|
| 259 |
|
| 260 |
/**
|
| 261 |
* Menu callback; displays the commentrss module settings page.
|
| 262 |
*/
|
| 263 |
function commentrss_admin_settings() {
|
| 264 |
$form['commentrss_site'] = array('#type' => 'checkbox',
|
| 265 |
'#title' => t('Node comments for entire site'),
|
| 266 |
'#default_value' => variable_get('commentrss_site', FALSE));
|
| 267 |
$form['commentrss_node'] = array('#type' => 'checkbox',
|
| 268 |
'#title' => t('Node comments for individual nodes'),
|
| 269 |
'#default_value' => variable_get('commentrss_node', FALSE));
|
| 270 |
$form['commentrss_term'] = array('#type' => 'checkbox',
|
| 271 |
'#title' => t('Node comments for a term'),
|
| 272 |
'#default_value' => variable_get('commentrss_term', FALSE));
|
| 273 |
if (module_exists('vocabulary_list')) {
|
| 274 |
$form['commentrss_vocab'] = array('#type' => 'checkbox',
|
| 275 |
'#title' => t('Node comments for a vocabulary'),
|
| 276 |
'#default_value' => variable_get('commentrss_vocab', FALSE));
|
| 277 |
}
|
| 278 |
|
| 279 |
return system_settings_form($form);
|
| 280 |
}
|