| 1 |
<?php
|
| 2 |
// $Id: aggregator_node.module,v 1.5 2009/03/29 18:51:30 alexb Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Simple node processor implementation. Saves feed items as nodes by
|
| 6 |
* implementing the aggregator API.
|
| 7 |
*
|
| 8 |
* * New feed items are stored as nodes.
|
| 9 |
* * Changes in feed items are not updated.
|
| 10 |
* * Only URL, GUID and feed association are stored.
|
| 11 |
* * Determination of uniqueness of a feed item is very simple - see
|
| 12 |
* aggregator_node_unique().
|
| 13 |
*/
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_aggregator_process().
|
| 17 |
*
|
| 18 |
* Save nodes from new feed items.
|
| 19 |
*/
|
| 20 |
function aggregator_node_aggregator_process($feed) {
|
| 21 |
foreach ($feed->items as $item) {
|
| 22 |
if (aggregator_node_unique($feed, $item)) {
|
| 23 |
aggregator_node_save($feed, $item);
|
| 24 |
}
|
| 25 |
}
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Implementation of hook_aggregator_remove().
|
| 30 |
*
|
| 31 |
* Delete all nodes when a feed is deleted or when a user removes all feed items.
|
| 32 |
*/
|
| 33 |
function aggregator_node_aggregator_remove($feed) {
|
| 34 |
$result = db_query("SELECT nid FROM {aggregator_node} WHERE fid = :fid", array(':fid' => $feed->nid));
|
| 35 |
foreach ($result as $node) {
|
| 36 |
node_delete($node->nid);
|
| 37 |
}
|
| 38 |
db_query('DELETE FROM {aggregator_node} WHERE fid = :fid', array(':fid' => $feed->nid));
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Implementation of hook_aggregator_process_info().
|
| 43 |
*/
|
| 44 |
function aggregator_node_aggregator_process_info() {
|
| 45 |
return array('title' => t('Node processor'), 'description' => t('Creates nodes from feed items.'));
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Implementation of hook_form_node_type_form_alter().
|
| 50 |
*/
|
| 51 |
function aggregator_node_form_node_type_form_alter(&$form) {
|
| 52 |
$node_type = empty($form['#node_type']->type) ? '' : $form['#node_type']->type;
|
| 53 |
$form['aggregator']['modules']['aggregator_node'] = array(
|
| 54 |
'#type' => 'fieldset',
|
| 55 |
'#title' => t('Node processor settings'),
|
| 56 |
'#collapsible' => TRUE,
|
| 57 |
'#collapsed' => !in_array('aggregator_node', variable_get('aggregator_processors_' . $node_type, array('aggregator'))),
|
| 58 |
);
|
| 59 |
$node_types = node_get_types('names');
|
| 60 |
$form['aggregator']['modules']['aggregator_node']['aggregator_node_type'] = array(
|
| 61 |
'#type' => 'select',
|
| 62 |
'#title' => t('Content type of feed items') ,
|
| 63 |
'#default_value' => variable_get('aggregator_node_type_' . $node_type, key($node_types)),
|
| 64 |
'#options' => $node_types,
|
| 65 |
'#description' => t('Choose the content type that should be used when creating nodes from feed items.'),
|
| 66 |
);
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Determines wether the given $item is unique or not.
|
| 71 |
*
|
| 72 |
* Simple implementation that prevents entries with the same URL or GUID.
|
| 73 |
*/
|
| 74 |
function aggregator_node_unique($feed, $item) {
|
| 75 |
if (!empty($item['GUID'])) {
|
| 76 |
$entry = db_query("SELECT nid FROM {aggregator_node} WHERE fid = :fid AND guid = :guid", array(':fid' => $feed->nid, ':guid' => $item['GUID']))->fetchObject();
|
| 77 |
}
|
| 78 |
elseif ($item['LINK'] && $item['LINK'] != $feed->link && $item['LINK'] != $feed->url) {
|
| 79 |
$entry = db_query("SELECT nid FROM {aggregator_node} WHERE fid = :fid AND link = :link", array(':fid' => $feed->nid, ':link' => $item['LINK']))->fetchObject();
|
| 80 |
}
|
| 81 |
if (empty($entry->nid)) {
|
| 82 |
return TRUE;
|
| 83 |
}
|
| 84 |
return FALSE;
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Takes an aggregator style feed item and saves it as a node.
|
| 89 |
*/
|
| 90 |
function aggregator_node_save($feed, $item) {
|
| 91 |
if ($item['TITLE']) {
|
| 92 |
// Create a new node object.
|
| 93 |
$node = new stdClass();
|
| 94 |
$node->title = $item['TITLE'];
|
| 95 |
$node->body = $item['DESCRIPTION'];
|
| 96 |
$node->teaser = $node->body;
|
| 97 |
$node->created = $item['TIMESTAMP'];
|
| 98 |
$node->type = variable_get('aggregator_node_type_' .$feed->type, 'article');
|
| 99 |
$node_options = variable_get('node_options_' . $node->type, array('status', 'promote'));
|
| 100 |
$node->promote = in_array('promote', $node_options) ? 1 : 0;
|
| 101 |
// Save it.
|
| 102 |
node_save($node);
|
| 103 |
|
| 104 |
// Now save a record to the aggregator node table to store meta information.
|
| 105 |
if (!empty($node->nid)) {
|
| 106 |
db_insert('aggregator_node')
|
| 107 |
->fields(array(
|
| 108 |
'nid' => $node->nid,
|
| 109 |
'fid' => $feed->nid,
|
| 110 |
'link' => $item['LINK'],
|
| 111 |
'guid' => $item['GUID'],
|
| 112 |
))
|
| 113 |
->execute();
|
| 114 |
}
|
| 115 |
}
|
| 116 |
}
|