/[drupal]/contributions/modules/blog_reactions/blog_reactions.module
ViewVC logotype

Contents of /contributions/modules/blog_reactions/blog_reactions.module

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


Revision 1.18 - (show annotations) (download) (as text)
Mon Oct 6 20:00:36 2008 UTC (13 months, 2 weeks ago) by sanduhrs
Branch: MAIN
CVS Tags: HEAD
Changes since 1.17: +5 -5 lines
File MIME type: text/x-php
fixed empty block warning.
1 <?php
2 /* $Id: blog_reactions.module,v 1.17 2008/10/06 19:36:30 sanduhrs Exp $ */
3
4 /**
5 * Blog reactions
6 *
7 * Fetch blog reactions from Technorati, Blogsearch and Bloglines
8 *
9 * @author Stefan Auditor <stefan.auditor@erdfisch.de>
10 */
11
12 /**
13 * Implementation of hook_menu().
14 */
15 function blog_reactions_menu() {
16 $items['admin/settings/blog_reactions'] = array(
17 'title' => t('Blog reactions'),
18 'description' => t('Change blog reactions settings.'),
19 'page callback' => 'drupal_get_form',
20 'page arguments' => array('blog_reactions_admin_settings'),
21 'access arguments' => array('administer blog reactions'),
22 'file' => 'blog_reactions.admin.inc',
23 );
24 return $items;
25 }
26
27 /**
28 * Implementation of hook_block().
29 */
30 function blog_reactions_block($op = 'list', $delta = 0, $edit = array()) {
31 if ($op == 'list') {
32 $blocks[0] = array(
33 'info' => t('Blog reactions'),
34 'cache' => BLOCK_CACHE_PER_PAGE,
35 );
36 return $blocks;
37 }
38 else if ($op == 'configure' && $delta == 0) {
39 $form['items'] = array(
40 '#type' => 'select',
41 '#title' => t('Number of items'),
42 '#default_value' => variable_get('blog_reactions_block_items', 10),
43 '#options' => drupal_map_assoc(array('0', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100')),
44 '#description' => t('Number of reactions to show. Choose 0 for no limit.'),
45 );
46 return $form;
47 }
48 else if ($op == 'save' && $delta == 0) {
49 variable_set('blog_reactions_block_items', $edit['items']);
50 }
51 else if ($op == 'view') {
52 switch($delta) {
53 case 0:
54 if (arg(0) == 'node' AND is_numeric(arg(1))) {
55 $blog_reactions_node_types = variable_get('blog_reactions_node_types', array('blog'));
56 if (!in_array($node->type, $blog_reactions_node_types)) {
57 $node = node_load(array('nid' => arg(1)));
58 $blog_reactions_node_types = variable_get('blog_reactions_node_types', array('blog'));
59 $blog_reactions_node_display = variable_get('blog_reactions_node_display', FALSE);
60 if (!in_array($node->type, $blog_reactions_node_types) AND !$blog_reactions_node_display) {
61 return;
62 }
63
64 if (user_access('access blog reactions') || user_access('administer blog reactions')) {
65 $block = array(
66 'subject' => t('Blog reactions'),
67 'content' => blog_reactions_block_1($node->nid),
68 );
69 }
70 }
71 }
72 break;
73 }
74 return $block;
75 }
76 }
77
78 /**
79 * Implementation of hook_perm().
80 *
81 * @return an array of permissions
82 */
83 function blog_reactions_perm() {
84 return array('administer blog reactions', 'access blog reactions');
85 }
86
87 /**
88 * Implementation of hook_nodeapi().
89 */
90 function blog_reactions_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
91 $blog_reactions_node_types = variable_get('blog_reactions_node_types', array('blog'));
92 $blog_reactions_node_display = variable_get('blog_reactions_node_display', FALSE);
93 if (!in_array($node->type, $blog_reactions_node_types)) {
94 return;
95 }
96
97 switch ($op) {
98 case 'view':
99 if ((user_access('access blog reactions') || user_access('administer blog reactions')) && $blog_reactions_node_display && !$a3) {
100 $node->content['blog_reactions'] = array(
101 '#value' => theme('blog_reactions', $node->blog_reactions['items']),
102 '#weight' => 10,
103 );
104 }
105 break;
106 case 'load':
107 $blog_reactions_node_types = variable_get('blog_reactions_node_types', array('blog'));
108 if (in_array($node->type, $blog_reactions_node_types)) {
109 $node->blog_reactions['items'] = blog_reactions_get_items($node->nid);
110 $node->blog_reactions['last_update'] = blog_reactions_last_update($node->nid);
111 }
112 break;
113 }
114 }
115
116 /**
117 * Block to display reactions
118 */
119 function blog_reactions_block_1($nid) {
120 $node = node_load(array('nid' => $nid));
121 return theme('blog_reactions_block', $node->blog_reactions['items']);
122 }
123
124 /**
125 * Load all reactions on a node
126 */
127 function blog_reactions_get_items($nid, $limit = 0) {
128 if (!$limit) {
129 $result = db_query("SELECT * FROM {blog_reactions} WHERE nid=%d ORDER BY published DESC", $nid);
130 }
131 else {
132 $result = db_query_range("SELECT * FROM {blog_reactions} WHERE nid=%d ORDER BY published DESC", $nid, 0, $limit);
133 }
134 while ($reaction = db_fetch_object($result)) {
135 $reactions[$reaction->brid]->brid = $reaction->brid;
136 $reactions[$reaction->brid]->title = $reaction->title;
137 $reactions[$reaction->brid]->link = array(
138 'rel' => $reaction->rel,
139 'href' => $reaction->href,
140 'content_type' => $reaction->content_type,
141 );
142 $reactions[$reaction->brid]->comments = $reaction->comments;
143 $reactions[$reaction->brid]->content = $reaction->content;
144 $reactions[$reaction->brid]->source = array(
145 $reaction->author,
146 $reaction->uri,
147 );
148 $reactions[$reaction->brid]->published = $reaction->published;
149 $reactions[$reaction->brid]->updated = $reaction->updated;
150 }
151 return $reactions ? $reactions : array();
152 }
153
154 /**
155 * Save multiple reaction items
156 */
157 function blog_reactions_set_items($reactions = array()) {
158 foreach ($reactions as $key => $item) {
159 $exists = blog_reactions_item_exists($item->brid);
160 if ($exists) {
161 blog_reactions_update_item($item);
162 }
163 else {
164 blog_reactions_set_item($item);
165 }
166 }
167 }
168
169 /**
170 * Check last update time
171 */
172 function blog_reactions_last_update($nid) {
173 return db_result(db_query("SELECT timestamp FROM {blog_reactions} WHERE nid=%d LIMIT 1", $nid));
174 }
175
176 /**
177 * Save single reaction item
178 */
179 function blog_reactions_set_item($item) {
180 return db_query("INSERT INTO {blog_reactions} (nid, brid, service, title, rel, href, content_type, comments, content, author, uri, published, updated, timestamp) VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d)", $item->nid, $item->brid, $item->service, $item->title, $item->link['rel'], $item->link['href'], $item->link['content_type'], $item->comments, $item->content, $item->source['author'], $item->source['uri'], $item->published, $item->updated, time());
181 }
182
183 /**
184 * Load single reaction item
185 */
186 function blog_reactions_get_item($brid) {
187 return db_query("SELECT brid FROM {blog_reactions} WHERE brid='%s'", $brid);
188 }
189
190 /**
191 * Load single reaction item
192 */
193 function blog_reactions_item_exists($brid) {
194 $result = db_result(db_query("SELECT COUNT(brid) as count FROM {blog_reactions} WHERE brid='%s'", $brid));
195 return $result ? TRUE : FALSE;
196 }
197
198 /**
199 * Update single reaction item
200 */
201 function blog_reactions_update_item($item) {
202 return db_query("UPDATE {blog_reactions} SET nid=%d, title='%s', rel='%s', href='%s', content_type='%s', comments='%s', author='%s', uri='%s', published=%d, updated=%d, timestamp=%d WHERE brid='%s'", $item->nid, $item->title, $item->link['rel'], $item->link['href'], $item->link['content_type'], $item->comments, $item->source['author'], $item->source['uri'], $item->published, $item->updated, time(), $item->brid);
203 }
204
205 /**
206 * Fetch XML-feed from Technorati, Blogsearch and Bloglines
207 */
208 function blog_reactions_fetch($nid, $uri = NULL) {
209 $reactions_technorati = array();
210 $reactions_blogsearch = array();
211 $reactions_bloglines = array();
212 $services = variable_get('blog_reactions_services', array('technorati', 'blogsearch', 'bloglines'));
213
214 if ($services['technorati']) {
215 $reactions_technorati = blog_reactions_fetch_technorati($nid, $uri);
216 }
217 if ($services['blogsearch']) {
218 $reactions_blogsearch = blog_reactions_fetch_blogsearch($nid, $uri);
219 }
220 if ($services['bloglines']) {
221 $reactions_bloglines = blog_reactions_fetch_bloglines($nid, $uri);
222 }
223
224 $reactions = array_merge($reactions_technorati, $reactions_blogsearch, $reactions_bloglines);
225 return $reactions;
226 }
227
228 /**
229 * Fetch XML-Feed from feeds.technorati.com
230 */
231 function blog_reactions_fetch_technorati($nid = NULL, $uri = NULL) {
232 $feed_url = 'http://feeds.technorati.com/search/'. ($uri ? $uri : url('node/'. $nid, array('absolute' => TRUE)));
233 $comment_url = 'http://technorati.com/search/';
234 $response = drupal_http_request($feed_url);
235 $xml = simplexml_load_string($response->data);
236
237 $data = array();
238 if ($xml->channel->item) {
239 foreach ($xml->channel->item as $key => $value) {
240 $data["$value->guid"]->nid = $nid;
241 $data["$value->guid"]->brid = (string) trim($value->guid);
242 $data["$value->guid"]->service = 'technorati';
243 $data["$value->guid"]->title = (string) trim($value->title);
244 $data["$value->guid"]->link = array(
245 'rel' => 'alternative',
246 'href' => (string) $value->link,
247 'content_type' => 'text/html',
248 );
249 $data["$value->guid"]->comments = $comment_url .(string) $value->link;
250 $data["$value->guid"]->content = (string) trim($value->description);
251 $data["$value->guid"]->source = array (
252 'author' => (string) trim($value->source),
253 'uri' => (string) trim(parse_url($value->link, PHP_URL_HOST)),
254 );
255 $data["$value->guid"]->published = (integer) strtotime(trim($value->pubDate));
256 $data["$value->guid"]->updated = (integer) strtotime(trim($value->pubDate));
257 }
258 }
259 if (($xml->channel->item->title == 'No results' || $xml->channel->item->title == 'No reactions') && strpos($xml->channel->item->link, 'technorati.com')) {
260 unset($data);
261 }
262 return $data ? $data : array();
263 }
264
265 /**
266 * Fetch XML-Feed from blogsearch.google.com
267 */
268 function blog_reactions_fetch_blogsearch($nid = NULL, $uri = NULL) {
269 $feed_url = 'http://blogsearch.google.com/blogsearch_feeds?q=link:'. ($uri ? $uri : url('node/'. $nid, array('absolute' => TRUE)));
270 $comment_url = 'http://blogsearch.google.com/blogsearch?q=link:';
271 $response = drupal_http_request($feed_url);
272 $xml = simplexml_load_string($response->data);
273
274 $data = array();
275 if ($xml->entry) {
276 foreach ($xml->entry as $key => $value) {
277 $data["$value->id"]->nid = $nid;
278 $data["$value->id"]->brid = (string) trim($value->id);
279 $data["$value->id"]->service = 'blogsearch';
280 $data["$value->id"]->title = (string) trim($value->title);
281
282 $link = (array) $value->link;
283 $data["$value->id"]->link = array(
284 'rel' => (string) $link['@attributes']['rel'],
285 'href' => (string) $link['@attributes']['href'],
286 'content_type' => (string) $link['@attributes']['type'],
287 );
288 $data["$value->id"]->comments = $comment_url .(string) $link['@attributes']['href'];
289 $data["$value->id"]->content = (string) trim($value->content);
290
291 $author = (array) $value->author;
292 $data["$value->id"]->source = array (
293 'author' => (string) trim($author['name']),
294 'uri' => (string) trim($author['uri']),
295 );
296 $data["$value->id"]->published = (integer) strtotime(trim($value->published));
297 $data["$value->id"]->updated = (integer) strtotime(trim($value->updated));
298 $data["$value->id"]->link = array(
299 'rel' => (string) $link['@attributes']['rel'],
300 'href' => (string) $link['@attributes']['href'],
301 'content_type' => (string) $link['@attributes']['type'],
302 );
303 }
304 }
305
306 return $data ? $data : array();
307 }
308
309 /**
310 * Fetch XML-Feed from bloglines.com
311 */
312 function blog_reactions_fetch_bloglines($nid = NULL, $uri = NULL) {
313 $feed_url = 'http://www.bloglines.com/search?q=bcite:'. ($uri ? $uri : url('node/'. $nid, NULL, NULL, TRUE)) .'&format=rss';
314 $comment_url = 'http://www.bloglines.com/search?q=bcite:';
315 $response = drupal_http_request($feed_url);
316 $xml = simplexml_load_string($response->data);
317
318 $data = array();
319 if ($xml->channel->item) {
320 foreach ($xml->channel->item as $key => $value) {
321 $data["$value->guid"]->nid = $nid;
322 $data["$value->guid"]->brid = (string) trim($value->guid);
323 $data["$value->guid"]->service = 'bloglines';
324 $data["$value->guid"]->title = (string) trim($value->title);
325 $data["$value->guid"]->link = array(
326 'rel' => 'alternative',
327 'href' => (string) $value->link,
328 'content_type' => 'text/html',
329 );
330 $data["$value->guid"]->comments = $comment_url .(string) $value->link;
331 $data["$value->guid"]->content = (string) trim($value->description);
332 $data["$value->guid"]->source = array (
333 'author' => NULL,
334 'uri' => (string) check_plain(trim(parse_url($value->link, PHP_URL_HOST))),
335 );
336 $data["$value->guid"]->published = (integer) strtotime(trim($value->pubDate));
337 $data["$value->guid"]->updated = (integer) strtotime(trim($value->pubDate));
338 }
339 }
340
341 return $data ? $data : array();
342 }
343
344 /**
345 * Implementation of hook_cron().
346 */
347 function blog_reactions_cron() {
348 $limit = variable_get('blog_reactions_cron_quantity', 20);
349 $types = variable_get('blog_reactions_node_types', array('blog'));
350 $last_update_nid = variable_get('blog_reactions_last_update_nid', 0);
351 $where = implode("', '", $types);
352
353 $result = db_query_range("SELECT DISTINCT(n.nid), MAX(br.timestamp) as timestamp FROM {node} n
354 LEFT JOIN {blog_reactions} br ON br.nid = n.nid
355 WHERE n.type IN('". $where ."')
356 AND n.nid > %d
357 GROUP BY n.nid
358 ORDER BY n.nid ASC", $last_update_nid, 0, $limit);
359 if (!db_affected_rows()) {
360 variable_set('blog_reactions_last_update_nid', 0);
361 }
362 while ($row = db_fetch_object($result)) {
363 $uri = url('node/'. $row->nid, array('absolute' => TRUE));
364 $reactions = blog_reactions_fetch($row->nid, $uri);
365 blog_reactions_set_items($reactions);
366 watchdog('blog_reactions', t('Node !nid has been updated', array('!nid' => $row->nid)), WATCHDOG_NOTICE, l('View', 'node/'. $row->nid));
367 variable_set('blog_reactions_last_update_nid', $row->nid);
368 }
369 }
370
371 /**
372 * Implementation of hook_theme().
373 */
374 function blog_reactions_theme($existing, $type, $theme, $path) {
375 return array(
376 'blog_reactions' => array(
377 'arguments' => array('data' => NULL),
378 ),
379 'blog_reactions_item' => array(
380 'arguments' => array('data' => NULL),
381 ),
382 'blog_reactions_block' => array(
383 'arguments' => array('data' => NULL),
384 ),
385 'blog_reactions_block_item' => array(
386 'arguments' => array('data' => NULL),
387 ),
388 );
389 }
390
391 /**
392 * Theme container for all reactions
393 */
394 function theme_blog_reactions($data = array()) {
395 $blog_reactions_quantity = variable_get('blog_reactions_quantity', 0);
396 if ($blog_reactions_quantity && count($data)) {
397 $result = array_chunk($data, $blog_reactions_quantity, TRUE);
398 $data = $result[0];
399 }
400 $output = '<div class="blog-reactions">';
401 $output .= '<h3 class="title">'. t('Blog reactions') .'</h3>';
402 foreach ($data as $key => $values) {
403 $elements .= theme('blog_reactions_item', $values);
404 }
405 $output .= $elements ? $elements : t('No reactions yet.');
406 $output .= '</div>';
407 return $output;
408 }
409
410 /**
411 * Theme a reaction item
412 */
413 function theme_blog_reactions_item($data) {
414 $output = '<div class="blog-reactions-item">';
415 $output .= '<div class="reaction">';
416 $output .= '<div class="title">'. l($data->title ? strip_tags($data->title) : 'n/a', url($data->link['href'])) .'</div>';
417 $output .= '<span class="submitted">'. format_date($data->published, 'medium') .' — </span>';
418 $output .= '<span class="description">'. truncate_utf8(strip_tags($data->content), 180, TRUE, TRUE) .'</span>';
419 $output .= '</div>';
420 $output .= '</div>';
421 return $output;
422 }
423
424 /**
425 * Theme container for all reactions
426 */
427 function theme_blog_reactions_block($data = array()) {
428 $blog_reactions_block_items = variable_get('blog_reactions_block_items', 10);
429
430 $output = '<div class="blog-reactions-block">';
431 if (count($data)) {
432 if ($blog_reactions_block_items) {
433 $result = array_chunk($data, $blog_reactions_block_items, TRUE);
434 $data = $result[0];
435 }
436
437 foreach ($data as $key => $values) {
438 $output .= theme('blog_reactions_block_item', $values);
439 }
440 }
441 else {
442 $output .= t('No reactions yet.');
443 }
444 $output .= '</div>';
445 return $output;
446 }
447
448 /**
449 * Theme a reaction item
450 */
451 function theme_blog_reactions_block_item($data) {
452 $items[] = l($data->title ? strip_tags($data->title) : 'n/a', url($data->link['href'])) .'<br />'. t('@time ago', array('@time' => format_interval(time() - $data->published)));
453 return theme('item_list', $items);
454 }

  ViewVC Help
Powered by ViewVC 1.1.2