| 1 | | <?php |
| 2 | | // $Id: aggregator.module,v 1.398 2008/11/01 19:51:06 dries Exp $ |
| 3 | | |
| 4 | | /** |
| 5 | | * @file |
| 6 | | * Used to aggregate syndicated content (RSS, RDF, and Atom). |
| 7 | | */ |
| 8 | | |
| 9 | | /** |
| 10 | | * Implementation of hook_help(). |
| 11 | | */ |
| 12 | 126 | function aggregator_help($path, $arg) { |
| 13 | | switch ($path) { |
| 14 | 78 | case 'admin/help#aggregator': |
| 15 | 55 | $output = '<p>' . t('The aggregator is a powerful on-site syndicator
and news reader that gathers fresh content from RSS-, RDF-, and Atom-based
feeds made available across the web. Thousands of sites (particularly news
sites and blogs) publish their latest headlines and posts in feeds, using a
number of standardized XML-based formats. Formats supported by the
aggregator include <a href="@rss">RSS</a>, <a href="@rdf">RDF</a>, and <a
href="@atom">Atom</a>.', array('@rss' =>
'http://cyber.law.harvard.edu/rss/', '@rdf' => 'http://www.w3.org/RDF/',
'@atom' => 'http://www.atomenabled.org')) . '</p>'; |
| 16 | 55 | $output .= '<p>' . t('Feeds contain feed items, or individual posts
published by the site providing the feed. Feeds may be grouped in
categories, generally by topic. Users view feed items in the <a
href="@aggregator">main aggregator display</a> or by <a
href="@aggregator-sources">their source</a>. Administrators can <a
href="@feededit">add, edit and delete feeds</a> and choose how often to
check each feed for newly updated items. The most recent items in either a
feed or category can be displayed as a block through the <a
href="@admin-block">blocks administration page</a>. A <a
href="@aggregator-opml">machine-readable OPML file</a> of all feeds is
available. A correctly configured <a href="@cron">cron maintenance task</a>
is required to update feeds automatically.', array('@aggregator' =>
url('aggregator'), '@aggregator-sources' => url('aggregator/sources'),
'@feededit' => url('admin/content/aggregator'), '@admin-block' =>
url('admin/build/block'), '@aggregator-opml' => url('aggregator/opml'),
'@cron' => url('admin/reports/status'))) . '</p>'; |
| 17 | 55 | $output .= '<p>' . t('For more information, see the online handbook
entry for <a href="@aggregator">Aggregator module</a>.',
array('@aggregator' => 'http://drupal.org/handbook/modules/aggregator/')) .
'</p>'; |
| 18 | 55 | return $output; |
| 19 | 77 | case 'admin/content/aggregator': |
| 20 | 15 | $output = '<p>' . t('Thousands of sites (particularly news sites and
blogs) publish their latest headlines and posts in feeds, using a number of
standardized XML-based formats. Formats supported by the aggregator include
<a href="@rss">RSS</a>, <a href="@rdf">RDF</a>, and <a
href="@atom">Atom</a>.', array('@rss' =>
'http://cyber.law.harvard.edu/rss/', '@rdf' => 'http://www.w3.org/RDF/',
'@atom' => 'http://www.atomenabled.org')) . '</p>'; |
| 21 | 15 | $output .= '<p>' . t('Current feeds are listed below, and <a
href="@addfeed">new feeds may be added</a>. For each feed or feed category,
the <em>latest items</em> block may be enabled at the <a
href="@block">blocks administration page</a>.', array('@addfeed' =>
url('admin/content/aggregator/add/feed'), '@block' =>
url('admin/build/block'))) . '</p>'; |
| 22 | 15 | return $output; |
| 23 | 62 | case 'admin/content/aggregator/add/feed': |
| 24 | 14 | return '<p>' . t('Add a feed in RSS, RDF or Atom format. A feed may
only have one entry.') . '</p>'; |
| 25 | 48 | case 'admin/content/aggregator/add/category': |
| 26 | 2 | return '<p>' . t('Categories allow feed items from different feeds to
be grouped together. For example, several sport-related feeds may belong to
a category named <em>Sports</em>. Feed items may be grouped automatically
(by selecting a category when creating or editing a feed) or manually (via
the <em>Categorize</em> page available from feed item listings). Each
category provides its own feed page and block.') . '</p>'; |
| 27 | 46 | case 'admin/content/aggregator/add/opml': |
| 28 | 12 | return '<p>' . t('<acronym title="Outline Processor Markup
Language">OPML</acronym> is an XML format used to exchange multiple feeds
between aggregators. A single OPML document may contain a collection of
many feeds. Drupal can parse such a file and import all feeds at once,
saving you the effort of adding them manually. You may either upload a
local file from your computer or enter a URL where Drupal can download
it.') . '</p>'; |
| 29 | 0 | } |
| 30 | 34 | } |
| 31 | | |
| 32 | | /** |
| 33 | | * Implementation of hook_theme(). |
| 34 | | */ |
| 35 | 126 | function aggregator_theme() { |
| 36 | | return array( |
| 37 | | 'aggregator_wrapper' => array( |
| 38 | 10 | 'arguments' => array('content' => NULL), |
| 39 | 10 | 'file' => 'aggregator.pages.inc', |
| 40 | 10 | 'template' => 'aggregator-wrapper', |
| 41 | 10 | ), |
| 42 | | 'aggregator_categorize_items' => array( |
| 43 | 10 | 'arguments' => array('form' => NULL), |
| 44 | 10 | 'file' => 'aggregator.pages.inc', |
| 45 | 10 | ), |
| 46 | | 'aggregator_feed_source' => array( |
| 47 | 10 | 'arguments' => array('feed' => NULL), |
| 48 | 10 | 'file' => 'aggregator.pages.inc', |
| 49 | 10 | 'template' => 'aggregator-feed-source', |
| 50 | 10 | ), |
| 51 | | 'aggregator_block_item' => array( |
| 52 | 10 | 'arguments' => array('item' => NULL, 'feed' => 0), |
| 53 | 10 | ), |
| 54 | | 'aggregator_summary_items' => array( |
| 55 | 10 | 'arguments' => array('summary_items' => NULL, 'source' => NULL), |
| 56 | 10 | 'file' => 'aggregator.pages.inc', |
| 57 | 10 | 'template' => 'aggregator-summary-items', |
| 58 | 10 | ), |
| 59 | | 'aggregator_summary_item' => array( |
| 60 | 10 | 'arguments' => array('item' => NULL), |
| 61 | 10 | 'file' => 'aggregator.pages.inc', |
| 62 | 10 | 'template' => 'aggregator-summary-item', |
| 63 | 10 | ), |
| 64 | | 'aggregator_item' => array( |
| 65 | 10 | 'arguments' => array('item' => NULL), |
| 66 | 10 | 'file' => 'aggregator.pages.inc', |
| 67 | 10 | 'template' => 'aggregator-item', |
| 68 | 10 | ), |
| 69 | | 'aggregator_page_opml' => array( |
| 70 | 10 | 'arguments' => array('feeds' => NULL), |
| 71 | 10 | 'file' => 'aggregator.pages.inc', |
| 72 | 10 | ), |
| 73 | | 'aggregator_page_rss' => array( |
| 74 | 10 | 'arguments' => array('feeds' => NULL, 'category' => NULL), |
| 75 | 10 | 'file' => 'aggregator.pages.inc', |
| 76 | 10 | ), |
| 77 | 10 | ); |
| 78 | 0 | } |
| 79 | | |
| 80 | | /** |
| 81 | | * Implementation of hook_menu(). |
| 82 | | */ |
| 83 | 126 | function aggregator_menu() { |
| 84 | 11 | $items['admin/content/aggregator'] = array( |
| 85 | 11 | 'title' => 'Feed aggregator', |
| 86 | 11 | 'description' => "Configure which content your site aggregates from
other sites, how often it polls them, and how they're categorized.", |
| 87 | 11 | 'page callback' => 'aggregator_admin_overview', |
| 88 | 11 | 'access arguments' => array('administer news feeds'), |
| 89 | | ); |
| 90 | 11 | $items['admin/content/aggregator/add/feed'] = array( |
| 91 | 11 | 'title' => 'Add feed', |
| 92 | 11 | 'page callback' => 'drupal_get_form', |
| 93 | 11 | 'page arguments' => array('aggregator_form_feed'), |
| 94 | 11 | 'access arguments' => array('administer news feeds'), |
| 95 | 11 | 'type' => MENU_LOCAL_TASK, |
| 96 | 11 | 'parent' => 'admin/content/aggregator', |
| 97 | | ); |
| 98 | 11 | $items['admin/content/aggregator/add/category'] = array( |
| 99 | 11 | 'title' => 'Add category', |
| 100 | 11 | 'page callback' => 'drupal_get_form', |
| 101 | 11 | 'page arguments' => array('aggregator_form_category'), |
| 102 | 11 | 'access arguments' => array('administer news feeds'), |
| 103 | 11 | 'type' => MENU_LOCAL_TASK, |
| 104 | 11 | 'parent' => 'admin/content/aggregator', |
| 105 | | ); |
| 106 | 11 | $items['admin/content/aggregator/add/opml'] = array( |
| 107 | 11 | 'title' => 'Import OPML', |
| 108 | 11 | 'page callback' => 'drupal_get_form', |
| 109 | 11 | 'page arguments' => array('aggregator_form_opml'), |
| 110 | 11 | 'access arguments' => array('administer news feeds'), |
| 111 | 11 | 'type' => MENU_LOCAL_TASK, |
| 112 | 11 | 'parent' => 'admin/content/aggregator', |
| 113 | | ); |
| 114 | 11 | $items['admin/content/aggregator/remove/%aggregator_feed'] = array( |
| 115 | 11 | 'title' => 'Remove items', |
| 116 | 11 | 'page callback' => 'drupal_get_form', |
| 117 | 11 | 'page arguments' => array('aggregator_admin_remove_feed', 4), |
| 118 | 11 | 'access arguments' => array('administer news feeds'), |
| 119 | 11 | 'type' => MENU_CALLBACK, |
| 120 | | ); |
| 121 | 11 | $items['admin/content/aggregator/update/%aggregator_feed'] = array( |
| 122 | 11 | 'title' => 'Update items', |
| 123 | 11 | 'page callback' => 'aggregator_admin_refresh_feed', |
| 124 | 11 | 'page arguments' => array(4), |
| 125 | 11 | 'access arguments' => array('administer news feeds'), |
| 126 | 11 | 'type' => MENU_CALLBACK, |
| 127 | | ); |
| 128 | 11 | $items['admin/content/aggregator/list'] = array( |
| 129 | 11 | 'title' => 'List', |
| 130 | 11 | 'type' => MENU_DEFAULT_LOCAL_TASK, |
| 131 | 11 | 'weight' => -10, |
| 132 | | ); |
| 133 | 11 | $items['admin/content/aggregator/settings'] = array( |
| 134 | 11 | 'title' => 'Settings', |
| 135 | 11 | 'page callback' => 'drupal_get_form', |
| 136 | 11 | 'page arguments' => array('aggregator_admin_settings'), |
| 137 | 11 | 'type' => MENU_LOCAL_TASK, |
| 138 | 11 | 'weight' => 10, |
| 139 | 11 | 'access arguments' => array('administer news feeds'), |
| 140 | | ); |
| 141 | 11 | $items['aggregator'] = array( |
| 142 | 11 | 'title' => 'Feed aggregator', |
| 143 | 11 | 'page callback' => 'aggregator_page_last', |
| 144 | 11 | 'access arguments' => array('access news feeds'), |
| 145 | 11 | 'weight' => 5, |
| 146 | | ); |
| 147 | 11 | $items['aggregator/sources'] = array( |
| 148 | 11 | 'title' => 'Sources', |
| 149 | 11 | 'page callback' => 'aggregator_page_sources', |
| 150 | 11 | 'access arguments' => array('access news feeds'), |
| 151 | | ); |
| 152 | 11 | $items['aggregator/categories'] = array( |
| 153 | 11 | 'title' => 'Categories', |
| 154 | 11 | 'page callback' => 'aggregator_page_categories', |
| 155 | 11 | 'access callback' => '_aggregator_has_categories', |
| 156 | | ); |
| 157 | 11 | $items['aggregator/rss'] = array( |
| 158 | 11 | 'title' => 'RSS feed', |
| 159 | 11 | 'page callback' => 'aggregator_page_rss', |
| 160 | 11 | 'access arguments' => array('access news feeds'), |
| 161 | 11 | 'type' => MENU_CALLBACK, |
| 162 | | ); |
| 163 | 11 | $items['aggregator/opml'] = array( |
| 164 | 11 | 'title' => 'OPML feed', |
| 165 | 11 | 'page callback' => 'aggregator_page_opml', |
| 166 | 11 | 'access arguments' => array('access news feeds'), |
| 167 | 11 | 'type' => MENU_CALLBACK, |
| 168 | | ); |
| 169 | 11 | $items['aggregator/categories/%aggregator_category'] = array( |
| 170 | 11 | 'title callback' => '_aggregator_category_title', |
| 171 | 11 | 'title arguments' => array(2), |
| 172 | 11 | 'page callback' => 'aggregator_page_category', |
| 173 | 11 | 'page arguments' => array(2), |
| 174 | 11 | 'access callback' => 'user_access', |
| 175 | 11 | 'access arguments' => array('access news feeds'), |
| 176 | | ); |
| 177 | 11 | $items['aggregator/categories/%aggregator_category/view'] = array( |
| 178 | 11 | 'title' => 'View', |
| 179 | 11 | 'type' => MENU_DEFAULT_LOCAL_TASK, |
| 180 | 11 | 'weight' => -10, |
| 181 | | ); |
| 182 | 11 | $items['aggregator/categories/%aggregator_category/categorize'] = array( |
| 183 | 11 | 'title' => 'Categorize', |
| 184 | 11 | 'page callback' => 'drupal_get_form', |
| 185 | 11 | 'page arguments' => array('aggregator_page_category', 2), |
| 186 | 11 | 'access arguments' => array('administer news feeds'), |
| 187 | 11 | 'type' => MENU_LOCAL_TASK, |
| 188 | | ); |
| 189 | 11 | $items['aggregator/categories/%aggregator_category/configure'] = array( |
| 190 | 11 | 'title' => 'Configure', |
| 191 | 11 | 'page callback' => 'drupal_get_form', |
| 192 | 11 | 'page arguments' => array('aggregator_form_category', 2), |
| 193 | 11 | 'access arguments' => array('administer news feeds'), |
| 194 | 11 | 'type' => MENU_LOCAL_TASK, |
| 195 | 11 | 'weight' => 1, |
| 196 | | ); |
| 197 | 11 | $items['aggregator/sources/%aggregator_feed'] = array( |
| 198 | 11 | 'page callback' => 'aggregator_page_source', |
| 199 | 11 | 'page arguments' => array(2), |
| 200 | 11 | 'access arguments' => array('access news feeds'), |
| 201 | 11 | 'type' => MENU_CALLBACK, |
| 202 | | ); |
| 203 | 11 | $items['aggregator/sources/%aggregator_feed/view'] = array( |
| 204 | 11 | 'title' => 'View', |
| 205 | 11 | 'type' => MENU_DEFAULT_LOCAL_TASK, |
| 206 | 11 | 'weight' => -10, |
| 207 | | ); |
| 208 | 11 | $items['aggregator/sources/%aggregator_feed/categorize'] = array( |
| 209 | 11 | 'title' => 'Categorize', |
| 210 | 11 | 'page callback' => 'drupal_get_form', |
| 211 | 11 | 'page arguments' => array('aggregator_page_source', 2), |
| 212 | 11 | 'access arguments' => array('administer news feeds'), |
| 213 | 11 | 'type' => MENU_LOCAL_TASK, |
| 214 | | ); |
| 215 | 11 | $items['aggregator/sources/%aggregator_feed/configure'] = array( |
| 216 | 11 | 'title' => 'Configure', |
| 217 | 11 | 'page callback' => 'drupal_get_form', |
| 218 | 11 | 'page arguments' => array('aggregator_form_feed', 2), |
| 219 | 11 | 'access arguments' => array('administer news feeds'), |
| 220 | 11 | 'type' => MENU_LOCAL_TASK, |
| 221 | 11 | 'weight' => 1, |
| 222 | | ); |
| 223 | 11 | $items['admin/content/aggregator/edit/feed/%aggregator_feed'] = array( |
| 224 | 11 | 'title' => 'Edit feed', |
| 225 | 11 | 'page callback' => 'drupal_get_form', |
| 226 | 11 | 'page arguments' => array('aggregator_form_feed', 5), |
| 227 | 11 | 'access arguments' => array('administer news feeds'), |
| 228 | 11 | 'type' => MENU_CALLBACK, |
| 229 | | ); |
| 230 | 11 | $items['admin/content/aggregator/edit/category/%aggregator_category'] =
array( |
| 231 | 11 | 'title' => 'Edit category', |
| 232 | 11 | 'page callback' => 'drupal_get_form', |
| 233 | 11 | 'page arguments' => array('aggregator_form_category', 5), |
| 234 | 11 | 'access arguments' => array('administer news feeds'), |
| 235 | 11 | 'type' => MENU_CALLBACK, |
| 236 | | ); |
| 237 | | |
| 238 | 11 | return $items; |
| 239 | 0 | } |
| 240 | | |
| 241 | | /** |
| 242 | | * Menu callback. |
| 243 | | * |
| 244 | | * @return |
| 245 | | * An aggregator category title. |
| 246 | | */ |
| 247 | 126 | function _aggregator_category_title($category) { |
| 248 | 0 | return $category['title']; |
| 249 | 0 | } |
| 250 | | |
| 251 | | /** |
| 252 | | * Implementation of hook_init(). |
| 253 | | */ |
| 254 | 126 | function aggregator_init() { |
| 255 | 117 | drupal_add_css(drupal_get_path('module', 'aggregator') .
'/aggregator.css'); |
| 256 | 117 | } |
| 257 | | |
| 258 | | /** |
| 259 | | * Find out whether there are any aggregator categories. |
| 260 | | * |
| 261 | | * @return |
| 262 | | * TRUE if there is at least one category and the user has access to
them, FALSE otherwise. |
| 263 | | */ |
| 264 | 126 | function _aggregator_has_categories() { |
| 265 | 2 | return user_access('access news feeds') && db_query('SELECT COUNT(*) FROM
{aggregator_category}')->fetchField(); |
| 266 | 0 | } |
| 267 | | |
| 268 | | /** |
| 269 | | * Implementation of hook_perm(). |
| 270 | | */ |
| 271 | 126 | function aggregator_perm() { |
| 272 | | return array( |
| 273 | | 'administer news feeds' => array( |
| 274 | 7 | 'title' => t('Administer news feeds'), |
| 275 | 7 | 'description' => t('Add, edit or delete news feeds that are
aggregated to your site.'), |
| 276 | 7 | ), |
| 277 | | 'access news feeds' => array( |
| 278 | 7 | 'title' => t('Access news feeds'), |
| 279 | 7 | 'description' => t('View aggregated news feed items.'), |
| 280 | 7 | ), |
| 281 | 7 | ); |
| 282 | 0 | } |
| 283 | | |
| 284 | | /** |
| 285 | | * Implementation of hook_cron(). |
| 286 | | * |
| 287 | | * Checks news feeds for updates once their refresh interval has elapsed. |
| 288 | | */ |
| 289 | 126 | function aggregator_cron() { |
| 290 | 0 | $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked +
refresh < :time', array(':time' => REQUEST_TIME)); |
| 291 | 0 | foreach ($result as $feed) { |
| 292 | 0 | aggregator_refresh((array)$feed); |
| 293 | 0 | } |
| 294 | 0 | } |
| 295 | | |
| 296 | | /** |
| 297 | | * Implementation of hook_block(). |
| 298 | | * |
| 299 | | * Generates blocks for the latest news items in each category and feed. |
| 300 | | */ |
| 301 | 126 | function aggregator_block($op = 'list', $delta = '', $edit = array()) { |
| 302 | 0 | if (user_access('access news feeds')) { |
| 303 | 0 | if ($op == 'list') { |
| 304 | 0 | $result = db_query('SELECT cid, title FROM {aggregator_category}
ORDER BY title'); |
| 305 | 0 | foreach ($result as $category) { |
| 306 | 0 | $block['category-' . $category->cid]['info'] = t('!title category
latest items', array('!title' => $category->title)); |
| 307 | 0 | } |
| 308 | 0 | $result = db_query('SELECT fid, title FROM {aggregator_feed} WHERE
block <> 0 ORDER BY fid'); |
| 309 | 0 | foreach ($result as $feed) { |
| 310 | 0 | $block['feed-' . $feed->fid]['info'] = t('!title feed latest
items', array('!title' => $feed->title)); |
| 311 | 0 | } |
| 312 | 0 | } |
| 313 | 0 | elseif ($op == 'configure') { |
| 314 | 0 | list($type, $id) = explode('-', $delta); |
| 315 | 0 | if ($type == 'category') { |
| 316 | 0 | $value = db_query('SELECT block FROM {aggregator_category} WHERE
cid = :cid', array(':cid' => $id))->fetchField(); |
| 317 | 0 | $form['block'] = array( |
| 318 | 0 | '#type' => 'select', |
| 319 | 0 | '#title' => t('Number of news items in block'), |
| 320 | 0 | '#default_value' => $value, |
| 321 | 0 | '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20)) |
| 322 | 0 | ); |
| 323 | 0 | return $form; |
| 324 | 0 | } |
| 325 | 0 | } |
| 326 | 0 | elseif ($op == 'save') { |
| 327 | 0 | list($type, $id) = explode('-', $delta); |
| 328 | 0 | if ($type == 'category') { |
| 329 | 0 | db_merge('aggregator_category') |
| 330 | 0 | ->key(array('cid' => $id)) |
| 331 | 0 | ->fields(array('block' => $edit['block'])) |
| 332 | 0 | ->execute(); |
| 333 | 0 | } |
| 334 | 0 | } |
| 335 | 0 | elseif ($op == 'view') { |
| 336 | 0 | list($type, $id) = explode('-', $delta); |
| 337 | | switch ($type) { |
| 338 | 0 | case 'feed': |
| 339 | 0 | if ($feed = db_query('SELECT fid, title, block FROM
{aggregator_feed} WHERE block <> 0 AND fid = :fid', array(':fid' =>
$id))->fetchObject()) { |
| 340 | 0 | $block['subject'] = check_plain($feed->title); |
| 341 | 0 | $result = db_query_range("SELECT * FROM {aggregator_item} WHERE
fid = :fid ORDER BY timestamp DESC, iid DESC", array(':fid' => $id), 0,
$feed->block); |
| 342 | 0 | $read_more = theme('more_link', url('aggregator/sources/' .
$feed->fid), t("View this feed's recent news.")); |
| 343 | 0 | } |
| 344 | 0 | break; |
| 345 | | |
| 346 | 0 | case 'category': |
| 347 | 0 | if ($category = db_query('SELECT cid, title, block FROM
{aggregator_category} WHERE cid = :cid', array(':cid' =>
$id))->fetchObject()) { |
| 348 | 0 | $block['subject'] = check_plain($category->title); |
| 349 | 0 | $result = db_query_range('SELECT i.* FROM
{aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid =
i.iid WHERE ci.cid = :cid ORDER BY i.timestamp DESC, i.iid DESC',
array(':cid' => $category->cid), 0, $category->block); |
| 350 | 0 | $read_more = theme('more_link', url('aggregator/categories/' .
$category->cid), t("View this category's recent news.")); |
| 351 | 0 | } |
| 352 | 0 | break; |
| 353 | 0 | } |
| 354 | 0 | $items = array(); |
| 355 | 0 | foreach ($result as $item) { |
| 356 | 0 | $items[] = theme('aggregator_block_item', $item); |
| 357 | 0 | } |
| 358 | | |
| 359 | | // Only display the block if there are items to show. |
| 360 | 0 | if (count($items) > 0) { |
| 361 | 0 | $block['content'] = theme('item_list', $items) . $read_more; |
| 362 | 0 | } |
| 363 | 0 | } |
| 364 | 0 | if (isset($block)) { |
| 365 | 0 | return $block; |
| 366 | 0 | } |
| 367 | 0 | } |
| 368 | 0 | } |
| 369 | | |
| 370 | | /** |
| 371 | | * Add/edit/delete aggregator categories. |
| 372 | | * |
| 373 | | * @param $edit |
| 374 | | * An associative array describing the category to be
added/edited/deleted. |
| 375 | | */ |
| 376 | 126 | function aggregator_save_category($edit) { |
| 377 | 1 | $link_path = 'aggregator/categories/'; |
| 378 | 1 | if (!empty($edit['cid'])) { |
| 379 | 0 | $link_path .= $edit['cid']; |
| 380 | 0 | if (!empty($edit['title'])) { |
| 381 | 0 | db_merge('aggregator_category') |
| 382 | 0 | ->key(array('cid' => $edit['cid'])) |
| 383 | 0 | ->fields(array( |
| 384 | 0 | 'title' => $edit['title'], |
| 385 | 0 | 'description' => $edit['description'], |
| 386 | 0 | )) |
| 387 | 0 | ->execute(); |
| 388 | 0 | $op = 'update'; |
| 389 | 0 | } |
| 390 | | else { |
| 391 | 0 | db_delete('aggregator_category') |
| 392 | 0 | ->condition('cid', $edit['cid']) |
| 393 | 0 | ->execute(); |
| 394 | | // Make sure there is no active block for this category. |
| 395 | 0 | db_delete('blocks') |
| 396 | 0 | ->condition('module', 'aggregator') |
| 397 | 0 | ->condition('delta', 'category-' . $edit['cid']) |
| 398 | 0 | ->execute(); |
| 399 | 0 | $edit['title'] = ''; |
| 400 | 0 | $op = 'delete'; |
| 401 | | } |
| 402 | 0 | } |
| 403 | 1 | elseif (!empty($edit['title'])) { |
| 404 | | // A single unique id for bundles and feeds, to use in blocks. |
| 405 | 1 | $link_path .= db_insert('aggregator_category') |
| 406 | 1 | ->fields(array( |
| 407 | 1 | 'title' => $edit['title'], |
| 408 | 1 | 'description' => $edit['description'], |
| 409 | 1 | )) |
| 410 | 1 | ->execute(); |
| 411 | 1 | $op = 'insert'; |
| 412 | 1 | } |
| 413 | 1 | if (isset($op)) { |
| 414 | 1 | menu_link_maintain('aggregator', $op, $link_path, $edit['title']); |
| 415 | 1 | } |
| 416 | 1 | } |
| 417 | | |
| 418 | | /** |
| 419 | | * Add/edit/delete an aggregator feed. |
| 420 | | * |
| 421 | | * @param $edit |
| 422 | | * An associative array describing the feed to be added/edited/deleted. |
| 423 | | */ |
| 424 | 126 | function aggregator_save_feed($edit) { |
| 425 | 15 | if (!empty($edit['fid'])) { |
| 426 | | // An existing feed is being modified, delete the category listings. |
| 427 | 7 | db_delete('aggregator_category_feed') |
| 428 | 7 | ->condition('fid', $edit['fid']) |
| 429 | 7 | ->execute(); |
| 430 | 7 | } |
| 431 | 15 | if (!empty($edit['fid']) && !empty($edit['title'])) { |
| 432 | 1 | db_update('aggregator_feed') |
| 433 | 1 | ->condition('fid', $edit['fid']) |
| 434 | 1 | ->fields(array( |
| 435 | 1 | 'title' => $edit['title'], |
| 436 | 1 | 'url' => $edit['url'], |
| 437 | 1 | 'refresh' => $edit['refresh'], |
| 438 | 1 | 'block' => $edit['block'], |
| 439 | 1 | )) |
| 440 | 1 | ->execute(); |
| 441 | 1 | } |
| 442 | 14 | elseif (!empty($edit['fid'])) { |
| 443 | 6 | $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid',
array(':fid' => $edit['fid']))->fetchCol(); |
| 444 | 6 | if ($iids) { |
| 445 | 0 | db_delete('aggregator_category_item') |
| 446 | 0 | ->condition('iid', $iids, 'IN') |
| 447 | 0 | ->execute(); |
| 448 | 0 | } |
| 449 | 6 | db_delete('aggregator_feed')-> |
| 450 | 6 | condition('fid', $edit['fid']) |
| 451 | 6 | ->execute(); |
| 452 | 6 | db_delete('aggregator_item') |
| 453 | 6 | ->condition('fid', $edit['fid']) |
| 454 | 6 | ->execute(); |
| 455 | | // Make sure there is no active block for this feed. |
| 456 | 6 | db_delete('blocks') |
| 457 | 6 | ->condition('module', 'aggregator') |
| 458 | 6 | ->condition('delta', 'feed-' . $edit['fid']) |
| 459 | 6 | ->execute(); |
| 460 | 6 | } |
| 461 | 8 | elseif (!empty($edit['title'])) { |
| 462 | 8 | $edit['fid'] = db_insert('aggregator_feed') |
| 463 | 8 | ->fields(array( |
| 464 | 8 | 'title' => $edit['title'], |
| 465 | 8 | 'url' => $edit['url'], |
| 466 | 8 | 'refresh' => $edit['refresh'], |
| 467 | 8 | 'block' => $edit['block'], |
| 468 | 8 | 'description' => '', |
| 469 | 8 | 'image' => '', |
| 470 | 8 | )) |
| 471 | 8 | ->execute(); |
| 472 | | |
| 473 | 8 | } |
| 474 | 15 | if (!empty($edit['title'])) { |
| 475 | | // The feed is being saved, save the categories as well. |
| 476 | 9 | if (!empty($edit['category'])) { |
| 477 | 2 | foreach ($edit['category'] as $cid => $value) { |
| 478 | 2 | if ($value) { |
| 479 | 1 | db_merge('aggregator_category_feed') |
| 480 | 1 | ->fields(array( |
| 481 | 1 | 'fid' => $edit['fid'], |
| 482 | 1 | 'cid' => $cid, |
| 483 | 1 | )) |
| 484 | 1 | ->execute(); |
| 485 | 1 | } |
| 486 | 2 | } |
| 487 | 2 | } |
| 488 | 9 | } |
| 489 | 15 | } |
| 490 | | |
| 491 | | /** |
| 492 | | * Removes all items from a feed. |
| 493 | | * |
| 494 | | * @param $feed |
| 495 | | * An associative array describing the feed to be cleared. |
| 496 | | */ |
| 497 | 126 | function aggregator_remove($feed) { |
| 498 | 2 | $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid',
array(':fid' => $feed['fid']))->fetchCol(); |
| 499 | 2 | if ($iids) { |
| 500 | 0 | db_delete('aggregator_category_item') |
| 501 | 0 | ->condition('iid', $iids, 'IN') |
| 502 | 0 | ->execute(); |
| 503 | 0 | } |
| 504 | | |
| 505 | 2 | db_delete('aggregator_item') |
| 506 | 2 | ->condition('fid', $feed['fid']) |
| 507 | 2 | ->execute(); |
| 508 | 2 | db_merge('aggregator_feed') |
| 509 | 2 | ->key(array('fid' => $feed['fid'])) |
| 510 | 2 | ->fields(array( |
| 511 | 2 | 'checked' => 0, |
| 512 | 2 | 'hash' => '', |
| 513 | 2 | 'modified' => 0, |
| 514 | 2 | 'description' => $feed['description'], |
| 515 | 2 | 'image' => $feed['image'], |
| 516 | 2 | )) |
| 517 | 2 | ->execute(); |
| 518 | 2 | drupal_set_message(t('The news items from %site have been removed.',
array('%site' => $feed['title']))); |
| 519 | 2 | } |
| 520 | | |
| 521 | | /** |
| 522 | | * Callback function used by the XML parser. |
| 523 | | */ |
| 524 | 126 | function aggregator_element_start($parser, $name, $attributes) { |
| 525 | 5 | global $item, $element, $tag, $items, $channel; |
| 526 | | |
| 527 | | switch ($name) { |
| 528 | 5 | case 'IMAGE': |
| 529 | 5 | case 'TEXTINPUT': |
| 530 | 5 | case 'CONTENT': |
| 531 | 5 | case 'SUMMARY': |
| 532 | 5 | case 'TAGLINE': |
| 533 | 5 | case 'SUBTITLE': |
| 534 | 5 | case 'LOGO': |
| 535 | 5 | case 'INFO': |
| 536 | 2 | $element = $name; |
| 537 | 2 | break; |
| 538 | 5 | case 'ID': |
| 539 | 0 | if ($element != 'ITEM') { |
| 540 | 0 | $element = $name; |
| 541 | 0 | } |
| 542 | 5 | case 'LINK': |
| 543 | 5 | if (!empty($attributes['REL']) && $attributes['REL'] == 'alternate')
{ |
| 544 | 0 | if ($element == 'ITEM') { |
| 545 | 0 | $items[$item]['LINK'] = $attributes['HREF']; |
| 546 | 0 | } |
| 547 | | else { |
| 548 | 0 | $channel['LINK'] = $attributes['HREF']; |
| 549 | | } |
| 550 | 0 | } |
| 551 | 5 | break; |
| 552 | 5 | case 'ITEM': |
| 553 | 2 | $element = $name; |
| 554 | 2 | $item += 1; |
| 555 | 2 | break; |
| 556 | 5 | case 'ENTRY': |
| 557 | 0 | $element = 'ITEM'; |
| 558 | 0 | $item += 1; |
| 559 | 0 | break; |
| 560 | 0 | } |
| 561 | | |
| 562 | 5 | $tag = $name; |
| 563 | 5 | } |
| 564 | | |
| 565 | | /** |
| 566 | | * Call-back function used by the XML parser. |
| 567 | | */ |
| 568 | 126 | function aggregator_element_end($parser, $name) { |
| 569 | 5 | global $element; |
| 570 | | |
| 571 | | switch ($name) { |
| 572 | 5 | case 'IMAGE': |
| 573 | 5 | case 'TEXTINPUT': |
| 574 | 5 | case 'ITEM': |
| 575 | 5 | case 'ENTRY': |
| 576 | 5 | case 'CONTENT': |
| 577 | 5 | case 'INFO': |
| 578 | 2 | $element = ''; |
| 579 | 2 | break; |
| 580 | 5 | case 'ID': |
| 581 | 0 | if ($element == 'ID') { |
| 582 | 0 | $element = ''; |
| 583 | 0 | } |
| 584 | 0 | } |
| 585 | 5 | } |
| 586 | | |
| 587 | | /** |
| 588 | | * Callback function used by the XML parser. |
| 589 | | */ |
| 590 | 126 | function aggregator_element_data($parser, $data) { |
| 591 | 5 | global $channel, $element, $items, $item, $image, $tag; |
| 592 | 5 | $items += array($item => array()); |
| 593 | | switch ($element) { |
| 594 | 5 | case 'ITEM': |
| 595 | 2 | $items[$item] += array($tag => ''); |
| 596 | 2 | $items[$item][$tag] .= $data; |
| 597 | 2 | break; |
| 598 | 5 | case 'IMAGE': |
| 599 | 5 | case 'LOGO': |
| 600 | 2 | $image += array($tag => ''); |
| 601 | 2 | $image[$tag] .= $data; |
| 602 | 2 | break; |
| 603 | 5 | case 'LINK': |
| 604 | 0 | if ($data) { |
| 605 | 0 | $items[$item] += array($tag => ''); |
| 606 | 0 | $items[$item][$tag] .= $data; |
| 607 | 0 | } |
| 608 | 0 | break; |
| 609 | 5 | case 'CONTENT': |
| 610 | 0 | $items[$item] += array('CONTENT' => ''); |
| 611 | 0 | $items[$item]['CONTENT'] .= $data; |
| 612 | 0 | break; |
| 613 | 5 | case 'SUMMARY': |
| 614 | 0 | $items[$item] += array('SUMMARY' => ''); |
| 615 | 0 | $items[$item]['SUMMARY'] .= $data; |
| 616 | 0 | break; |
| 617 | 5 | case 'TAGLINE': |
| 618 | 5 | case 'SUBTITLE': |
| 619 | 0 | $channel += array('DESCRIPTION' => ''); |
| 620 | 0 | $channel['DESCRIPTION'] .= $data; |
| 621 | 0 | break; |
| 622 | 5 | case 'INFO': |
| 623 | 5 | case 'ID': |
| 624 | 5 | case 'TEXTINPUT': |
| 625 | | // The sub-element is not supported. However, we must recognize |
| 626 | | // it or its contents will end up in the item array. |
| 627 | 0 | break; |
| 628 | 5 | default: |
| 629 | 5 | $channel += array($tag => ''); |
| 630 | 5 | $channel[$tag] .= $data; |
| 631 | 5 | } |
| 632 | 5 | } |
| 633 | | |
| 634 | | /** |
| 635 | | * Checks a news feed for new items. |
| 636 | | * |
| 637 | | * @param $feed |
| 638 | | * An associative array describing the feed to be refreshed. |
| 639 | | */ |
| 640 | 126 | function aggregator_refresh($feed) { |
| 641 | 5 | global $channel, $image; |
| 642 | | |
| 643 | | // Generate conditional GET headers. |
| 644 | 5 | $headers = array(); |
| 645 | 5 | if ($feed['etag']) { |
| 646 | 0 | $headers['If-None-Match'] = $feed['etag']; |
| 647 | 0 | } |
| 648 | 5 | if ($feed['modified']) { |
| 649 | 0 | $headers['If-Modified-Since'] = gmdate('D, d M Y H:i:s',
$feed['modified']) . ' GMT'; |
| 650 | 0 | } |
| 651 | | |
| 652 | | // Request feed. |
| 653 | 5 | $result = drupal_http_request($feed['url'], $headers); |
| 654 | | |
| 655 | | // Process HTTP response code. |
| 656 | 5 | switch ($result->code) { |
| 657 | 5 | case 304: |
| 658 | 0 | db_update('aggregator_feed') |
| 659 | 0 | ->fields(array('checked' => REQUEST_TIME)) |
| 660 | 0 | ->condition('fid', $feed['fid']) |
| 661 | 0 | ->execute(); |
| 662 | 0 | drupal_set_message(t('There is no new syndicated content from
%site.', array('%site' => $feed['title']))); |
| 663 | 0 | break; |
| 664 | 5 | case 301: |
| 665 | 0 | $feed['url'] = $result->redirect_url; |
| 666 | | // Do not break here. |
| 667 | 5 | case 200: |
| 668 | 5 | case 302: |
| 669 | 5 | case 307: |
| 670 | | // We store the md5 hash of feed data in the database. When
refreshing a |
| 671 | | // feed we compare stored hash and new hash calculated from
downloaded |
| 672 | | // data. If both are equal we say that feed is not updated. |
| 673 | 5 | $md5 = md5($result->data); |
| 674 | 5 | if ($feed['hash'] == $md5) { |
| 675 | 0 | db_update('aggregator_feed') |
| 676 | 0 | ->condition('fid', $feed['fid']) |
| 677 | 0 | ->fields(array('checked' => REQUEST_TIME)) |
| 678 | 0 | ->execute(); |
| 679 | 0 | drupal_set_message(t('There is no new syndicated content from
%site.', array('%site' => $feed['title']))); |
| 680 | 0 | break; |
| 681 | 0 | } |
| 682 | | |
| 683 | | // Filter the input data. |
| 684 | 5 | if (aggregator_parse_feed($result->data, $feed)) { |
| 685 | 5 | $modified = empty($result->headers['Last-Modified']) ? 0 :
strtotime($result->headers['Last-Modified']); |
| 686 | | |
| 687 | | // Prepare the channel data. |
| 688 | 5 | foreach ($channel as $key => $value) { |
| 689 | 5 | $channel[$key] = trim($value); |
| 690 | 5 | } |
| 691 | | |
| 692 | | // Prepare the image data (if any). |
| 693 | 5 | foreach ($image as $key => $value) { |
| 694 | 2 | $image[$key] = trim($value); |
| 695 | 2 | } |
| 696 | | |
| 697 | 5 | if (!empty($image['LINK']) && !empty($image['URL']) &&
!empty($image['TITLE'])) { |
| 698 | | $image = l(theme('image', $image['URL'], $image['TITLE']),
$image['LINK'], array('html' => TRUE)); |
| 699 | | } |
| 700 | 2 | else { |
| 701 | 2 | $image = ''; |
| 702 | | } |
| 703 | 3 | |
| 704 | | $etag = empty($result->headers['ETag']) ? '' :
$result->headers['ETag']; |
| 705 | | // Update the feed data. |
| 706 | 5 | db_merge('aggregator_feed') |
| 707 | | ->key(array('fid' => $feed['fid'])) |
| 708 | 5 | ->fields(array( |
| 709 | 5 | 'url' => $feed['url'], |
| 710 | 5 | 'checked' => REQUEST_TIME, |
| 711 | 5 | 'link' => $channel['LINK'], |
| 712 | 5 | 'description' => $channel['DESCRIPTION'], |
| 713 | 5 | 'image' => $image, |
| 714 | 5 | 'hash' => $md5, |
| 715 | 5 | 'etag' => $etag, |
| 716 | 5 | 'modified' => $modified, |
| 717 | 5 | )) |
| 718 | 5 | ->execute(); |
| 719 | 5 | |
| 720 | 5 | // Clear the cache. |
| 721 | | cache_clear_all(); |
| 722 | | |
| 723 | 5 | if (isset($result->redirect_url)) { |
| 724 | | watchdog('aggregator', 'Updated URL for feed %title to %url.',
array('%title' => $feed['title'], '%url' => $feed['url'])); |
| 725 | 5 | } |
| 726 | 0 | |
| 727 | 0 | watchdog('aggregator', 'There is new syndicated content from
%site.', array('%site' => $feed['title'])); |
| 728 | | drupal_set_message(t('There is new syndicated content from %site.',
array('%site' => $feed['title']))); |
| 729 | 5 | } |
| 730 | 5 | break; |
| 731 | 5 | default: |
| 732 | 5 | watchdog('aggregator', 'The feed from %site seems to be broken, due
to "%error".', array('%site' => $feed['title'], '%error' => $result->code .
' ' . $result->error), WATCHDOG_WARNING); |
| 733 | 0 | drupal_set_message(t('The feed from %site seems to be broken, because
of error "%error".', array('%site' => $feed['title'], '%error' =>
$result->code . ' ' . $result->error))); |
| 734 | 0 | module_invoke('system', 'check_http_request'); |
| 735 | 0 | } |
| 736 | 0 | } |
| 737 | 0 | |
| 738 | 5 | /** |
| 739 | | * Parse the W3C date/time format, a subset of ISO 8601. |
| 740 | | * |
| 741 | | * PHP date parsing functions do not handle this format. |
| 742 | | * See http://www.w3.org/TR/NOTE-datetime for more information. |
| 743 | | * Originally from MagpieRSS (http://magpierss.sourceforge.net/). |
| 744 | | * |
| 745 | | * @param $date_str |
| 746 | | * A string with a potentially W3C DTF date. |
| 747 | | * @return |
| 748 | | * A timestamp if parsed successfully or FALSE if not. |
| 749 | | */ |
| 750 | | function aggregator_parse_w3cdtf($date_str) { |
| 751 | | if
(preg_match('/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/',
$date_str, $match)) { |
| 752 | 126 | list($year, $month, $day, $hours, $minutes, $seconds) =
array($match[1], $match[2], $match[3], $match[4], $match[5], $match[6]); |
| 753 | 5 | // Calculate the epoch for current date assuming GMT. |
| 754 | 0 | $epoch = gmmktime($hours, $minutes, $seconds, $month, $day, $year); |
| 755 | | if ($match[10] != 'Z') { // Z is zulu time, aka GMT |
| 756 | 0 | list($tz_mod, $tz_hour, $tz_min) = array($match[8], $match[9],
$match[10]); |
| 757 | 0 | // Zero out the variables. |
| 758 | 0 | if (!$tz_hour) { |
| 759 | | $tz_hour = 0; |
| 760 | 0 | } |
| 761 | 0 | if (!$tz_min) { |
| 762 | 0 | $tz_min = 0; |
| 763 | 0 | } |
| 764 | 0 | $offset_secs = (($tz_hour * 60) + $tz_min) * 60; |
| 765 | 0 | // Is timezone ahead of GMT? If yes, subtract offset. |
| 766 | 0 | if ($tz_mod == '+') { |
| 767 | | $offset_secs *= -1; |
| 768 | 0 | } |
| 769 | 0 | $epoch += $offset_secs; |
| 770 | 0 | } |
| 771 | 0 | return $epoch; |
| 772 | 0 | } |
| 773 | 0 | else { |
| 774 | 0 | return FALSE; |
| 775 | | } |
| 776 | 5 | } |
| 777 | | |
| 778 | 0 | /** |
| 779 | | * Parse a feed and store its items. |
| 780 | | * |
| 781 | | * @param $data |
| 782 | | * The feed data. |
| 783 | | * @param $feed |
| 784 | | * An associative array describing the feed to be parsed. |
| 785 | | * @return |
| 786 | | * FALSE on error, TRUE otherwise. |
| 787 | | */ |
| 788 | | function aggregator_parse_feed(&$data, $feed) { |
| 789 | | global $items, $image, $channel; |
| 790 | 126 | |
| 791 | 5 | // Unset the global variables before we use them. |
| 792 | | unset($GLOBALS['element'], $GLOBALS['item'], $GLOBALS['tag']); |
| 793 | | $items = array(); |
| 794 | 5 | $image = array(); |
| 795 | 5 | $channel = array(); |
| 796 | 5 | |
| 797 | 5 | // Parse the data. |
| 798 | | $xml_parser = drupal_xml_parser_create($data); |
| 799 | | xml_set_element_handler($xml_parser, 'aggregator_element_start',
'aggregator_element_end'); |
| 800 | 5 | xml_set_character_data_handler($xml_parser, 'aggregator_element_data'); |
| 801 | 5 | |
| 802 | 5 | if (!xml_parse($xml_parser, $data, 1)) { |
| 803 | | watchdog('aggregator', 'The feed from %site seems to be broken, due to
an error "%error" on line %line.', array('%site' => $feed['title'],
'%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' =>
xml_get_current_line_number($xml_parser)), WATCHDOG_WARNING); |
| 804 | 5 | drupal_set_message(t('The feed from %site seems to be broken, because
of error "%error" on line %line.', array('%site' => $feed['title'],
'%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' =>
xml_get_current_line_number($xml_parser))), 'error'); |
| 805 | 0 | return FALSE; |
| 806 | 0 | } |
| 807 | 0 | xml_parser_free($xml_parser); |
| 808 | 0 | |
| 809 | 5 | // We reverse the array such that we store the first item last, and the
last |
| 810 | | // item first. In the database, the newest item should be at the top. |
| 811 | | $items = array_reverse($items); |
| 812 | | |
| 813 | 5 | // Initialize variables. |
| 814 | | $title = $link = $author = $description = $guid = NULL; |
| 815 | | foreach ($items as $item) { |
| 816 | 5 | unset($title, $link, $author, $description, $guid); |
| 817 | 5 | |
| 818 | 5 | // Prepare the item: |
| 819 | | foreach ($item as $key => $value) { |
| 820 | | $item[$key] = trim($value); |
| 821 | 5 | } |
| 822 | 2 | |
| 823 | 2 | // Resolve the item's title. If no title is found, we use up to 40 |
| 824 | | // characters of the description ending at a word boundary, but not |
| 825 | | // splitting potential entities. |
| 826 | | if (!empty($item['TITLE'])) { |
| 827 | | $title = $item['TITLE']; |
| 828 | 5 | } |
| 829 | 2 | elseif (!empty($item['DESCRIPTION'])) { |
| 830 | 2 | $title = preg_replace('/^(.*)[^\w;&].*?$/', "\\1",
truncate_utf8($item['DESCRIPTION'], 40)); |
| 831 | 5 | } |
| 832 | 0 | else { |
| 833 | 0 | $title = ''; |
| 834 | | } |
| 835 | 5 | |
| 836 | | // Resolve the items link. |
| 837 | | if (!empty($item['LINK'])) { |
| 838 | | $link = $item['LINK']; |
| 839 | 5 | } |
| 840 | 2 | else { |
| 841 | 2 | $link = $feed['link']; |
| 842 | | } |
| 843 | 5 | $guid = isset($item['GUID']) ? $item['GUID'] : ''; |
| 844 | | |
| 845 | 5 | // Atom feeds have a CONTENT and/or SUMMARY tag instead of a
DESCRIPTION tag. |
| 846 | | if (!empty($item['CONTENT:ENCODED'])) { |
| 847 | | $item['DESCRIPTION'] = $item['CONTENT:ENCODED']; |
| 848 | 5 | } |
| 849 | 0 | elseif (!empty($item['SUMMARY'])) { |
| 850 | 0 | $item['DESCRIPTION'] = $item['SUMMARY']; |
| 851 | 5 | } |
| 852 | 0 | elseif (!empty($item['CONTENT'])) { |
| 853 | 0 | $item['DESCRIPTION'] = $item['CONTENT']; |
| 854 | 5 | } |
| 855 | 0 | |
| 856 | 0 | // Try to resolve and parse the item's publication date. |
| 857 | | $date = ''; |
| 858 | | foreach (array('PUBDATE', 'DC:DATE', 'DCTERMS:ISSUED',
'DCTERMS:CREATED', 'DCTERMS:MODIFIED', 'ISSUED', 'CREATED', 'MODIFIED',
'PUBLISHED', 'UPDATED') as $key) { |
| 859 | 5 | if (!empty($item[$key])) { |
| 860 | 5 | $date = $item[$key]; |
| 861 | 5 | break; |
| 862 | 0 | } |
| 863 | 0 | } |
| 864 | 0 | |
| 865 | 5 | $timestamp = strtotime($date); |
| 866 | | |
| 867 | 5 | if ($timestamp === FALSE) { |
| 868 | | $timestamp = aggregator_parse_w3cdtf($date); //
Aggregator_parse_w3cdtf() returns FALSE on failure. |
| 869 | 5 | } |
| 870 | 5 | |
| 871 | 5 | // Save this item. Try to avoid duplicate entries as much as possible.
If |
| 872 | | // we find a duplicate entry, we resolve it and pass along its ID is
such |
| 873 | | // that we can update it if needed. |
| 874 | | if (!empty($guid)) { |
| 875 | | $entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE
fid = :fid AND guid = :guid", array(':fid' => $feed['fid'], ':guid' =>
$guid))->fetchObject(); |
| 876 | 5 | } |
| 877 | 0 | elseif ($link && $link != $feed['link'] && $link != $feed['url']) { |
| 878 | 0 | $entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE
fid = :fid AND link = :link", array(':fid' => $feed['fid'], ':link' =>
$link))->fetchObject(); |
| 879 | 5 | } |
| 880 | 2 | else { |
| 881 | 2 | $entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE
fid = :fid AND title = :title", array(':fid' => $feed['fid'], ':title' =>
$title))->fetchObject(); |
| 882 | | } |
| 883 | 5 | |
| 884 | | if (!$timestamp) { |
| 885 | | $timestamp = isset($entry->timestamp) ? $entry->timestamp :
REQUEST_TIME; |
| 886 | 5 | } |
| 887 | 5 | $item += array('AUTHOR' => '', 'DESCRIPTION' => ''); |
| 888 | 5 | aggregator_save_item(array('iid' => (isset($entry->iid) ? $entry->iid :
''), 'fid' => $feed['fid'], 'timestamp' => $timestamp, 'title' => $title,
'link' => $link, 'author' => $item['AUTHOR'], 'description' =>
$item['DESCRIPTION'], 'guid' => $guid)); |
| 889 | 5 | } |
| 890 | 5 | |
| 891 | 5 | // Remove all items that are older than flush item timer. |
| 892 | | $age = REQUEST_TIME - variable_get('aggregator_clear', 9676800); |
| 893 | | $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid AND
timestamp < :timestamp', array(':fid' => $feed['fid'], ':timestamp' =>
$age))->fetchCol(); |
| 894 | 5 | if ($iids) { |
| 895 | 5 | db_delete('aggregator_category_item') |
| 896 | 5 | ->condition('iid', $iids, 'IN') |
| 897 | 0 | ->execute(); |
| 898 | 0 | db_delete('aggregator_item') |
| 899 | 0 | ->condition('iid', $iids, 'IN') |
| 900 | 0 | ->execute(); |
| 901 | 0 | } |
| 902 | 0 | |
| 903 | 0 | return TRUE; |
| 904 | | } |
| 905 | 5 | |
| 906 | 0 | /** |
| 907 | | * Add/edit/delete an aggregator item. |
| 908 | | * |
| 909 | | * @param $edit |
| 910 | | * An associative array describing the item to be added/edited/deleted. |
| 911 | | */ |
| 912 | | function aggregator_save_item($edit) { |
| 913 | | if ($edit['title'] && empty($edit['iid'])) { |
| 914 | 126 | $edit['iid'] = db_insert('aggregator_item') |
| 915 | 5 | ->fields(array( |
| 916 | 1 | 'title' => $edit['title'], |
| 917 | 1 | 'link' => $edit['link'], |
| 918 | 1 | 'author' => $edit['author'], |
| 919 | 1 | 'description' => $edit['description'], |
| 920 | 1 | 'guid' => $edit['guid'], |
| 921 | 1 | 'timestamp' => $edit['timestamp'], |
| 922 | 1 | 'fid' => $edit['fid'], |
| 923 | 1 | )) |
| 924 | 1 | ->execute(); |
| 925 | 1 | } |
| 926 | 1 | if ($edit['iid'] && !$edit['title']) { |
| 927 | 1 | db_delete('aggregator_item') |
| 928 | 5 | ->condition('iid', $edit['iid']) |
| 929 | 0 | ->execute(); |
| 930 | 0 | db_delete('aggregator_category_item') |
| 931 | 0 | ->condition('iid', $edit['iid']) |
| 932 | 0 | ->execute(); |
| 933 | 0 | } |
| 934 | 0 | elseif ($edit['title'] && $edit['link']) { |
| 935 | 0 | // file the items in the categories indicated by the feed |
| 936 | 5 | $result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE
fid = :fid', array(':fid' => $edit['fid'])); |
| 937 | | foreach ($result as $category) { |
| 938 | 2 | db_merge('aggregator_category_item') |
| 939 | 2 | ->fields(array( |
| 940 | 0 | 'cid' => $category->cid, |
| 941 | 0 | 'iid' => $edit['iid'], |
| 942 | 0 | )) |
| 943 | 0 | ->execute(); |
| 944 | 0 | } |
| 945 | 0 | } |
| 946 | 0 | } |
| 947 | 2 | |
| 948 | 5 | /** |
| 949 | | * Load an aggregator feed. |
| 950 | | * |
| 951 | | * @param $fid |
| 952 | | * The feed id. |
| 953 | | * @return |
| 954 | | * An associative array describing the feed. |
| 955 | | */ |
| 956 | | function aggregator_feed_load($fid) { |
| 957 | | static $feeds; |
| 958 | 126 | if (!isset($feeds[$fid])) { |
| 959 | 26 | $feeds[$fid] = db_query('SELECT * FROM {aggregator_feed} WHERE fid =
:fid', array(':fid' => $fid))->fetchAssoc(); |
| 960 | 26 | } |
| 961 | 26 | |
| 962 | 26 | return $feeds[$fid]; |
| 963 | | } |
| 964 | 26 | |
| 965 | 0 | /** |
| 966 | | * Load an aggregator category. |
| 967 | | * |
| 968 | | * @param $cid |
| 969 | | * The category id. |
| 970 | | * @return |
| 971 | | * An associative array describing the category. |
| 972 | | */ |
| 973 | | function aggregator_category_load($cid) { |
| 974 | | static $categories; |
| 975 | 126 | if (!isset($categories[$cid])) { |
| 976 | 0 | $categories[$cid] = db_query('SELECT * FROM {aggregator_category} WHERE
cid = :cid', array(':cid' => $cid))->fetchAssoc(); |
| 977 | 0 | } |
| 978 | 0 | |
| 979 | 0 | return $categories[$cid]; |
| 980 | | } |
| 981 | 0 | |
| 982 | 0 | /** |
| 983 | | * Format an individual feed item for display in the block. |
| 984 | | * |
| 985 | | * @param $item |
| 986 | | * The item to be displayed. |
| 987 | | * @param $feed |
| 988 | | * Not used. |
| 989 | | * @return |
| 990 | | * The item HTML. |
| 991 | | * @ingroup themeable |
| 992 | | */ |
| 993 | | function theme_aggregator_block_item($item, $feed = 0) { |
| 994 | | |
| 995 | 126 | // Display the external link to the item. |
| 996 | 0 | $output .= '<a href="' . check_url($item->link) . '">' .
check_plain($item->title) . "</a>\n"; |
| 997 | | |
| 998 | 0 | return $output; |
| 999 | 0 | } |
| 1000 | 0 | |
| 1001 | 0 | /** |
| 1002 | 0 | * Safely render HTML content, as allowed. |
| 1003 | 0 | * |
| 1004 | | * @param $value |
| 1005 | | * The content to be filtered. |
| 1006 | 0 | * @return |
| 1007 | | * The filtered content. |
| 1008 | 0 | */ |
| 1009 | 0 | function aggregator_filter_xss($value) { |
| 1010 | | return filter_xss($value, preg_split('/\s+|<|>/',
variable_get('aggregator_allowed_html_tags', '<a> <b> <br> <dd> <dl> <dt>
<em> <i> <li> <ol> <p> <strong> <u> <ul>'), -1, PREG_SPLIT_NO_EMPTY)); |
| 1011 | | } |
| 1012 | | |
| 1013 | | /** |
| 1014 | | * Helper function for drupal_map_assoc. |
| 1015 | | * |
| 1016 | | * @param $count |
| 1017 | | * Items count. |
| 1018 | | * @return |
| 1019 | 126 | * Plural-formatted "@count items" |
| 1020 | 2 | */ |
| 1021 | 0 | function _aggregator_items($count) { |
| 1022 | | return format_plural($count, '1 item', '@count items'); |
| 1023 | | } |
| 1024 | | |