| 1 |
<?php
|
| 2 |
// $Id: feedapi_item.module,v 1.11 2007/07/23 15:40:19 alexb Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Handle how the feed items are represented as a content
|
| 7 |
* Handle the processing of the feed items
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_help().
|
| 12 |
*/
|
| 13 |
function feedapi_item_help($section) {
|
| 14 |
switch ($section) {
|
| 15 |
case 'admin/help#feedapi_item':
|
| 16 |
return t('Provides an item processor for the FeedAPI. It transforms items into nodes.');
|
| 17 |
}
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_menu().
|
| 22 |
*/
|
| 23 |
function feedapi_item_menu($may_cache) {
|
| 24 |
if ($may_cache) {
|
| 25 |
$items[] = array('path' => 'admin/settings/feedapi_item',
|
| 26 |
'title' => t('Node-Item processor'),
|
| 27 |
'callback' => 'drupal_get_form',
|
| 28 |
'callback arguments' => array('feedapi_item_admin_settings'),
|
| 29 |
'type' => MENU_NORMAL_ITEM,
|
| 30 |
'access' => user_access('administer feedapi'),
|
| 31 |
);
|
| 32 |
}
|
| 33 |
return $items;
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Implementation of hook_menu().
|
| 38 |
*/
|
| 39 |
function feedapi_item_nodeapi(&$node, $op, $teaser, $page) {
|
| 40 |
if ($op == 'delete') {
|
| 41 |
$result = db_query("SELECT fiid FROM {feedapi_node_item} WHERE nid = %d", $node->nid);
|
| 42 |
if (db_num_rows($result) > 0) {
|
| 43 |
$feed = db_fetch_object($result);
|
| 44 |
feedapi_item_feedapi_item_delete($feed);
|
| 45 |
}
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Implementation of hook_link().
|
| 51 |
*/
|
| 52 |
function feedapi_item_link($type, $node = NULL, $teaser = FALSE) {
|
| 53 |
if ($type == 'node') {
|
| 54 |
$result = db_query("SELECT item.guid, item.url, feed.fid FROM feedapi_node_feed as feed INNER JOIN feedapi_node_item as item ON item.fid = feed.fid WHERE item.nid = %d", $node->nid);
|
| 55 |
if (db_num_rows($result) > 0) {
|
| 56 |
$feed = db_fetch_object($result);
|
| 57 |
$links['feedapi_article'] = array(
|
| 58 |
'title' => t('Original article'),
|
| 59 |
'href' => strlen($feed->url) == 0 ? $feed->guid : $feed->url,
|
| 60 |
);
|
| 61 |
feedapi_invoke_feedapi("load", $feed);
|
| 62 |
$links['feedapi_original'] = array(
|
| 63 |
'title' => t('Original site: ') .$feed->title,
|
| 64 |
'href' => $feed->options->original_url,
|
| 65 |
);
|
| 66 |
return $links;
|
| 67 |
}
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Implements hook_form_alter().
|
| 73 |
*/
|
| 74 |
function feedapi_item_form_alter($form_id, &$form) {
|
| 75 |
// Extend the content-type form
|
| 76 |
if ($form_id == 'node_type_form') {
|
| 77 |
$node_type = $form['old_type']['#value'];
|
| 78 |
$form['workflow']['feedapi_item'] = array(
|
| 79 |
'#type' => 'fieldset',
|
| 80 |
'#title' => t('Node-Feed item processor settings'),
|
| 81 |
);
|
| 82 |
$form['workflow']['feedapi_item']['feedapi_item_promote'] = array(
|
| 83 |
'#type' => 'textfield',
|
| 84 |
'#title' => t('Promoted items'),
|
| 85 |
'#description' => t('The newest N items will be promoted on the front page per feed'),
|
| 86 |
'#default_value' => variable_get('feedapi_item_promote_'. $node_type, 15),
|
| 87 |
);
|
| 88 |
}
|
| 89 |
// Extend the feed editing form
|
| 90 |
if ($form_id == 'feedapi_edit_page') {
|
| 91 |
$feed = new stdClass();
|
| 92 |
if (isset($form['fid']['#value'])) {
|
| 93 |
$feed->fid = $form['fid']['#value'];
|
| 94 |
feedapi_invoke_feedapi('load', $feed);
|
| 95 |
if (in_array('feedapi_item', $feed->processors_item)) {
|
| 96 |
$settings = db_fetch_object(db_query("SELECT * FROM {feedapi_node_item_settings} WHERE fid = %d", $feed->fid));
|
| 97 |
$user_current = user_load(array('uid' => $settings->uid));
|
| 98 |
if ($user_current->uid == 0) {
|
| 99 |
global $user;
|
| 100 |
$user_current = $user;
|
| 101 |
}
|
| 102 |
$form['processors_settings']['item_content_type'] = array(
|
| 103 |
'#type' => 'select',
|
| 104 |
'#title' => t('The content-type of the items'),
|
| 105 |
'#default_value' => strlen($settings->content_type) > 0 ? $settings->content_type : variable_get('feedapi_item_type', 'story'),
|
| 106 |
'#options' => drupal_map_assoc(array_keys(node_get_types())),
|
| 107 |
'#description' => t('All of the items related to this feed will be created with this type.'),
|
| 108 |
);
|
| 109 |
$form['processors_settings']['item_node_user'] = array(
|
| 110 |
'#type' => 'textfield',
|
| 111 |
'#title' => t('The author of the item nodes'),
|
| 112 |
'#default_value' => isset($user_current->name) ? $user_current->name : $user->name,
|
| 113 |
'#autocomplete_path' => 'user/autocomplete',
|
| 114 |
);
|
| 115 |
$form['processors_settings']['item_node_date'] = array(
|
| 116 |
'#type' => 'radios',
|
| 117 |
'#title' => t('Date of the item nodes'),
|
| 118 |
'#options' => array('feed' => t('Comes from the feed'), 'current' => t('The actual server time')),
|
| 119 |
'#default_value' => isset($settings->date) ? $settings->date : variable_get('feedapi_item_node_date', 'feed'),
|
| 120 |
);
|
| 121 |
}
|
| 122 |
}
|
| 123 |
}
|
| 124 |
}
|
| 125 |
|
| 126 |
/**
|
| 127 |
* Tell which feed types are compatible with this submodule.
|
| 128 |
* For eg. "XML feed", "FTP", "mailing list"
|
| 129 |
* Implementation of hook_feedapi_type().
|
| 130 |
*
|
| 131 |
* @return
|
| 132 |
* A string array of compatible types
|
| 133 |
*/
|
| 134 |
function feedapi_item_feedapi_type() {
|
| 135 |
return array("XML feed");
|
| 136 |
}
|
| 137 |
|
| 138 |
/**
|
| 139 |
* Create a node from the feed item
|
| 140 |
* Store the relationship between the node and the feed item
|
| 141 |
* Implementation of hook_feedapi_item_save().
|
| 142 |
* @todo User shouldn't be the user of the time when item is saved.
|
| 143 |
* User should be the owner of the feed.
|
| 144 |
*/
|
| 145 |
function feedapi_item_feedapi_item_save($feed_item, $fid) {
|
| 146 |
// Get our feed specific settings related to items, avoid to query the database all the time when the function is called
|
| 147 |
global $_feedapi_item_feed_settings;
|
| 148 |
if (!isset($_feedapi_item_feed_settings['f'. $fid])) {
|
| 149 |
$settings = db_fetch_array(db_query("SELECT * FROM {feedapi_node_item_settings} WHERE fid = %d", $fid));
|
| 150 |
$_feedapi_item_feed_settings['f'. $fid] = $settings;
|
| 151 |
}
|
| 152 |
$settings = $_feedapi_item_feed_settings['f'. $fid];
|
| 153 |
// Construct the node object
|
| 154 |
$node = new stdClass();
|
| 155 |
$node->type = isset($settings['content_type']) ? $settings['content_type'] : variable_get('feedapi_item_type', 'story');
|
| 156 |
// Get the default options from the cont
|
| 157 |
$options = variable_get('node_options_'. $node->type, FALSE);
|
| 158 |
if (is_array($options)) {
|
| 159 |
$node->status = in_array('status', $options) ? 1 : 0;
|
| 160 |
$node->promote = in_array('promote', $options) ? 1 : 0;
|
| 161 |
$node->sticky = in_array('sticky', $options) ? 1 : 0;
|
| 162 |
}
|
| 163 |
|
| 164 |
$node->title = $feed_item->title;
|
| 165 |
global $user;
|
| 166 |
$node->uid = isset($settings['uid']) ? $settings['uid'] : $user->uid;
|
| 167 |
$node->created = (isset($settings['date']) && $settings['date'] == 'feed') ? $feed_item->options->timestamp : time();
|
| 168 |
$node->body = $feed_item->description;
|
| 169 |
$node->status = 1;
|
| 170 |
if (isset($feed_item->options->teaser)) {
|
| 171 |
$node->teaser = $feed_item->options->teaser;
|
| 172 |
}
|
| 173 |
if (!isset($feed_item->nid)) {
|
| 174 |
node_save($node);
|
| 175 |
$feed_item->nid = $node->nid;
|
| 176 |
db_query("INSERT INTO {feedapi_node_item} (fid, nid, url, timestamp, arrived, guid) VALUES (%d, %d, '%s', %d, %d, '%s')", $fid, $feed_item->nid, $feed_item->options->original_url, $feed_item->options->timestamp, time(), $feed_item->options->guid);
|
| 177 |
}
|
| 178 |
else {
|
| 179 |
$node->nid = $feed_item->nid;
|
| 180 |
node_save($node);
|
| 181 |
db_query("UPDATE {feedapi_node_item} SET url = '%s', timestamp = %d, guid = '%s' WHERE fiid = %d", $feed_item->options->original_url, $feed_item->options->timestamp, $feed_item->options->guid, $feed_item->fiid);
|
| 182 |
}
|
| 183 |
return $feed_item;
|
| 184 |
}
|
| 185 |
|
| 186 |
/**
|
| 187 |
* Update a node which already assigned to a feed item
|
| 188 |
* Implementation of hook_feedapi_item_update().
|
| 189 |
*/
|
| 190 |
function feedapi_item_feedapi_item_update($feed_item, $fid) {
|
| 191 |
// Determine which node is assigned to this item
|
| 192 |
if ($feed_item->options->guid) {
|
| 193 |
$node = db_fetch_object(db_query("SELECT nid FROM {feedapi_node_item} WHERE guid = '%s'", $feed_item->options->guid));
|
| 194 |
}
|
| 195 |
else {
|
| 196 |
$node = db_fetch_object(db_query("SELECT nid FROM {feedapi_node_item} WHERE url = '%s'", $feed_item->options->original_url));
|
| 197 |
}
|
| 198 |
$feed_item->nid = $node->nid;
|
| 199 |
feedapi_item_feedapi_item_save($feed_item, $fid);
|
| 200 |
return $feed_item;
|
| 201 |
}
|
| 202 |
|
| 203 |
/**
|
| 204 |
* Delete a node which already assigned to a feed item
|
| 205 |
* Implementation of hook_feedapi_item_delete().
|
| 206 |
*
|
| 207 |
*/
|
| 208 |
function feedapi_item_feedapi_item_delete($feed_item) {
|
| 209 |
if (isset($feed_item->nid)) {
|
| 210 |
node_delete($feed_item->nid);
|
| 211 |
}
|
| 212 |
db_query("DELETE FROM {feedapi_node_item} WHERE fiid = %d", $feed_item->fiid);
|
| 213 |
}
|
| 214 |
|
| 215 |
/**
|
| 216 |
* Add to the feed item object some data to identify the
|
| 217 |
* node assigned
|
| 218 |
* Implementation of hook_feedapi_item_load().
|
| 219 |
*
|
| 220 |
*/
|
| 221 |
function feedapi_item_feedapi_item_load($feed_item) {
|
| 222 |
$item = db_fetch_object(db_query("SELECT * FROM {feedapi_node_item} WHERE fiid = %d", $feed_item->fiid));
|
| 223 |
$feed_item->fiid = $item->fiid;
|
| 224 |
$feed_item->nid = $item->nid;
|
| 225 |
$feed_item->arrived = $item->arrived;
|
| 226 |
$feed_item->options->original_url = $item->url;
|
| 227 |
$feed_item->options->guid = $item->guid;
|
| 228 |
$feed_item->options->timestamp = $item->timestamp;
|
| 229 |
$content = node_load($feed_item->nid);
|
| 230 |
$feed_item->description = $content->body;
|
| 231 |
$feed_item->title = $content->title;
|
| 232 |
$feed_item->teaser = $content->teaser;
|
| 233 |
return $feed_item;
|
| 234 |
}
|
| 235 |
|
| 236 |
/**
|
| 237 |
* Implementation of hook_feedapi_item_fetch_items().
|
| 238 |
*
|
| 239 |
* Construct the basic information (nid, fiid, fid) of all feeds into an array.
|
| 240 |
*
|
| 241 |
* @param $feed
|
| 242 |
* Feed object
|
| 243 |
* @return
|
| 244 |
* The array of feed elements with basic information
|
| 245 |
*/
|
| 246 |
function feedapi_item_feedapi_item_fetch_items($feed) {
|
| 247 |
$result = db_query("SELECT nid, fiid, fid, arrived FROM {feedapi_node_item} WHERE fid = %d", $feed->fid);
|
| 248 |
$items = array();
|
| 249 |
while ($item = db_fetch_object($result)) {
|
| 250 |
$items[] = $item;
|
| 251 |
}
|
| 252 |
return $items;
|
| 253 |
}
|
| 254 |
|
| 255 |
/**
|
| 256 |
* Implementation of hook_feedapi_item_unique().
|
| 257 |
*
|
| 258 |
* Tell if the feed item was seen before or not at the feed
|
| 259 |
*
|
| 260 |
* @param $feed_item
|
| 261 |
* Feed item object
|
| 262 |
* @param $fid
|
| 263 |
* Feed ID
|
| 264 |
* @return
|
| 265 |
* TRUE if the item is new, FALSE if the item is a duplicated one
|
| 266 |
*/
|
| 267 |
function feedapi_item_feedapi_item_unique($feed_item, $fid) {
|
| 268 |
// If the GUID is set, it have to be used to consider two feed items different or not
|
| 269 |
if (isset($feed_item->options->guid)) {
|
| 270 |
$count = db_num_rows(db_query("SELECT fiid FROM {feedapi_node_item} WHERE guid = '%s' AND fid = %d", $feed_item->options->guid, $fid));
|
| 271 |
if ($count > 0) {
|
| 272 |
return FALSE;
|
| 273 |
}
|
| 274 |
}
|
| 275 |
// Fallback on link
|
| 276 |
else {
|
| 277 |
$count = db_num_rows(db_query("SELECT fiid FROM {feedapi_node_item} WHERE url = '%s'", $feed_item->options->original_url));
|
| 278 |
if ($count > 0) {
|
| 279 |
return FALSE;
|
| 280 |
}
|
| 281 |
}
|
| 282 |
// Seems that the item is new
|
| 283 |
return TRUE;
|
| 284 |
}
|
| 285 |
|
| 286 |
/**
|
| 287 |
* Implements hook_feedapi_after_refresh($feed).
|
| 288 |
* Handle the promote N items to the frontpage setting
|
| 289 |
*/
|
| 290 |
function feedapi_item_feedapi_after_refresh($feed) {
|
| 291 |
$type = variable_get('feedapi_item_type', 'story');
|
| 292 |
$promote = variable_get('feedapi_item_promote_'. $type, 15);
|
| 293 |
$result = db_query("SELECT n.nid FROM {node} n JOIN {feedapi_node_item} fi ON fi.nid = n.nid WHERE fi.fid = %d ORDER BY fi.timestamp DESC", $feed->fid);
|
| 294 |
$to_promote = array();
|
| 295 |
$to_demote = array();
|
| 296 |
while ($item = db_fetch_object($result)) {
|
| 297 |
if ($promote-- > 0) {
|
| 298 |
$to_promote[] = $item->nid;
|
| 299 |
}
|
| 300 |
else {
|
| 301 |
$to_demote[] = $item->nid;
|
| 302 |
}
|
| 303 |
}
|
| 304 |
if (count($to_promote) > 0) {
|
| 305 |
db_query("UPDATE {node} SET promote = 1 WHERE nid IN (%s)", implode(',', $to_promote));
|
| 306 |
}
|
| 307 |
if (count($to_demote) > 0) {
|
| 308 |
db_query("UPDATE {node} SET promote = 0 WHERE nid IN (%s)", implode(',', $to_demote));
|
| 309 |
}
|
| 310 |
}
|
| 311 |
|
| 312 |
/**
|
| 313 |
* Implements hook_feedapi_after_update($feed).
|
| 314 |
* Save the options coming from the feed editing
|
| 315 |
*/
|
| 316 |
function feedapi_item_feedapi_after_update($feed) {
|
| 317 |
$user = user_load(array('name' => $feed->item_node_user));
|
| 318 |
if ($user->uid == 0) {
|
| 319 |
global $user;
|
| 320 |
}
|
| 321 |
if (db_num_rows(db_query("SELECT uid FROM {feedapi_node_item_settings} WHERE fid = %d", $feed->fid)) == 0) {
|
| 322 |
db_query("INSERT INTO {feedapi_node_item_settings} (fid, content_type, uid, date) VALUES (%d, '%s', %d, '%s')",
|
| 323 |
$feed->fid, $feed->item_content_type, $user->uid, $feed->item_node_date);
|
| 324 |
}
|
| 325 |
else {
|
| 326 |
db_query("UPDATE {feedapi_node_item_settings} SET content_type = '%s', uid = %d, date = '%s' WHERE fid = %d",
|
| 327 |
$feed->item_content_type, $user->uid, $feed->item_node_date, $feed->fid);
|
| 328 |
}
|
| 329 |
}
|
| 330 |
|
| 331 |
/**
|
| 332 |
* Implements hook_feedapi_after_update($feed).
|
| 333 |
*/
|
| 334 |
function feedapi_item_feedapi_after_purge($feed) {
|
| 335 |
db_query("DELETE FROM {feedapi_node_item_settings} WHERE fid = %d", $feed->fid);
|
| 336 |
}
|
| 337 |
|
| 338 |
/**
|
| 339 |
* Settings:
|
| 340 |
* Content-type of feeds
|
| 341 |
*/
|
| 342 |
function feedapi_item_admin_settings() {
|
| 343 |
$form['feedapi_item_type'] = array(
|
| 344 |
'#type' => 'select', '#title' => t('Item content-type'),
|
| 345 |
'#default_value' => variable_get('feedapi_item_type', 'story'),
|
| 346 |
'#options' => drupal_map_assoc(array_keys(node_get_types())),
|
| 347 |
'#description' => t('The default type of the feed items.')
|
| 348 |
);
|
| 349 |
$form['processors_settings']['feedapi_item_node_date'] = array(
|
| 350 |
'#type' => 'radios',
|
| 351 |
'#title' => t('Date of the item nodes'),
|
| 352 |
'#options' => array('feed' => t('Comes from the feed'), 'current' => t('The current server time')),
|
| 353 |
'#default_value' => variable_get('feedapi_item_node_date', 'feed'),
|
| 354 |
);
|
| 355 |
return system_settings_form($form);
|
| 356 |
}
|