feedapi_aggregator or * $feed->feedapi_aggregator as an array. This might prove superfluent. */ /** * Implementation of function feedapi_aggregator_help(). */ function feedapi_aggregator_help() { switch ($section) { case 'admin/help#feedapi_aggregator': return t('Emulates the Drupal core aggregator with the FeedAPI module.'); } } /** * Implementation of hook_menu(). */ function feedapi_aggregator_menu($may_cache) { $items = array(); $edit = user_access('administer feedapi'); $view = user_access('access news feeds'); // @todo if ($may_cache) { $items[] = array( 'path' => 'admin/content/feed/category', 'title' => t('Categories'), 'callback' => 'feedapi_aggregator_category_view', 'access' => $edit, 'type' => MENU_LOCAL_TASK, ); $items[] = array( 'path' => 'admin/content/feed/category/list', 'title' => t('List'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -15, ); $items[] = array('path' => 'admin/content/feed/category/add', 'title' => t('Add category'), 'callback' => 'drupal_get_form', 'callback arguments' => array('feedapi_aggregator_form_category'), 'access' => $edit, 'type' => MENU_LOCAL_TASK); $items[] = array('path' => 'aggregator', 'title' => t('News aggregator'), 'callback' => 'feedapi_aggregator_page_last', 'access' => $view, 'weight' => 5); $items[] = array('path' => 'aggregator/sources', 'title' => t('Sources'), 'callback' => 'feedapi_aggregator_page_sources', 'access' => $view); $items[] = array('path' => 'aggregator/categories', 'title' => t('Categories'), 'callback' => 'feedapi_aggregator_page_categories', 'access' => $view, 'type' => MENU_ITEM_GROUPING); $items[] = array('path' => 'aggregator/rss', 'title' => t('RSS feed'), 'callback' => 'feedapi_aggregator_page_rss', 'access' => $view, 'type' => MENU_CALLBACK); $items[] = array('path' => 'aggregator/opml', 'title' => t('OPML feed'), 'callback' => 'feedapi_aggregator_page_opml', 'access' => $view, 'type' => MENU_CALLBACK); $result = db_query('SELECT title, cid FROM {aggregator_category} ORDER BY title'); while ($category = db_fetch_array($result)) { $items[] = array('path' => 'aggregator/categories/'. $category['cid'], 'title' => $category['title'], 'callback' => 'feedapi_aggregator_page_category', 'access' => $view); } } else { // Add the CSS for this module // We put this in !$may_cache so it's only added once per request drupal_add_css(drupal_get_path('module', 'feedapi_aggregator') .'/aggregator.css'); if (arg(0) == 'aggregator' && is_numeric(arg(2))) { if (arg(1) == 'sources') { $feed = feedapi_aggregator_get_feed(arg(2)); if ($feed) { $items[] = array('path' => 'aggregator/sources/'. $feed['fid'], 'title' => $feed['title'], 'callback' => 'feedapi_aggregator_page_source', 'access' => $view, 'type' => MENU_CALLBACK); $items[] = array('path' => 'aggregator/sources/'. $feed['fid'] .'/view', 'title' => t('View'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10); $items[] = array('path' => 'aggregator/sources/'. $feed['fid'] .'/categorize', 'title' => t('Categorize'), 'callback' => 'drupal_get_form', 'callback arguments' => array('feedapi_aggregator_page_source'), 'access' => $edit, 'type' => MENU_LOCAL_TASK); $items[] = array('path' => 'aggregator/sources/'. $feed['fid'] .'/configure', 'title' => t('Configure'), 'callback' => 'drupal_get_form', 'callback arguments' => array('feedapi_aggregator_form_feed', $feed), 'access' => $edit, 'type' => MENU_LOCAL_TASK, 'weight' => 1); } } else if (arg(1) == 'categories') { $category = feedapi_aggregator_get_category(arg(2)); if ($category) { $items[] = array('path' => 'aggregator/categories/'. $category['cid'] .'/view', 'title' => t('View'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10); $items[] = array('path' => 'aggregator/categories/'. $category['cid'] .'/categorize', 'title' => t('Categorize'), 'callback' => 'drupal_get_form', 'callback arguments' => array('feedapi_aggregator_page_category'), 'access' => $edit, 'type' => MENU_LOCAL_TASK); $items[] = array('path' => 'aggregator/categories/'. $category['cid'] .'/configure', 'title' => t('Configure'), 'callback' => 'drupal_get_form', 'callback arguments' => array('feedapi_aggregator_form_category', $category), 'access' => $edit, 'type' => MENU_LOCAL_TASK, 'weight' => 1); } } } else if (arg(2) == 'aggregator' && is_numeric(arg(5))) { if (arg(4) == 'feed') { $feed = feedapi_aggregator_get_feed(arg(5)); if ($feed) { $items[] = array('path' => 'admin/content/aggregator/edit/feed/'. $feed['fid'], 'title' => t('Edit feed'), 'callback' => 'drupal_get_form', 'callback arguments' => array('feedapi_aggregator_form_feed', $feed), 'access' => $edit, 'type' => MENU_CALLBACK); } } else { $category = feedapi_aggregator_get_category(arg(5)); if ($category) { $items[] = array('path' => 'admin/content/aggregator/edit/category/'. $category['cid'], 'title' => t('Edit category'), 'callback' => 'drupal_get_form', 'callback arguments' => array('feedapi_aggregator_form_category', $category), 'access' => $edit, 'type' => MENU_CALLBACK); } } } } return $items; } function feedapi_aggregator_admin_settings() { $items = array(0 => t('none')) + drupal_map_assoc(array(3, 5, 10, 15, 20, 25), '_aggregator_items'); $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'); $form['feedapi_aggregator_summary_items'] = array( '#type' => 'select', '#title' => t('Items shown in sources and categories pages') , '#default_value' => variable_get('aggregator_summary_items', 3), '#options' => $items, '#description' => t('The number of items which will be shown with each feed or category in the feed and category summary pages.') ); $form['feedapi_aggregator_category_selector'] = array( '#type' => 'radios', '#title' => t('Category selection type'), '#default_value' => variable_get('aggregator_category_selector', 'checkboxes'), '#options' => array('checkboxes' => t('checkboxes'), 'select' => t('multiple selector')), '#description' => t('The type of category selection widget which is shown on categorization pages. Checkboxes are easier to use; a multiple selector is good for working with large numbers of categories.') ); return system_settings_form($form); } /** * Implementation of hook_block(). * * Generates blocks for the latest news items in each category and feed. */ function feedapi_aggregator_block($op = 'list', $delta = 0, $edit = array()) { if (user_access('access news feeds')) { if ($op == 'list') { $result = db_query('SELECT cid, title FROM {aggregator_category} ORDER BY title'); while ($category = db_fetch_object($result)) { $block['category-'. $category->cid]['info'] = t('!title category latest items', array('!title' => $category->title)); } $result = db_query('SELECT fid, title FROM {aggregator_feed} ORDER BY fid'); while ($feed = db_fetch_object($result)) { $block['feed-'. $feed->fid]['info'] = t('!title feed latest items', array('!title' => $feed->title)); } } else if ($op == 'configure') { list($type, $id) = explode('-', $delta); if ($type == 'category') { $value = db_result(db_query('SELECT block FROM {aggregator_category} WHERE cid = %d', $id)); } else { $value = db_result(db_query('SELECT block FROM {aggregator_feed} WHERE fid = %d', $id)); } $form['block'] = array('#type' => 'select', '#title' => t('Number of news items in block'), '#default_value' => $value, '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20))); return $form; } else if ($op == 'save') { list($type, $id) = explode('-', $delta); if ($type == 'category') { $value = db_query('UPDATE {aggregator_category} SET block = %d WHERE cid = %d', $edit['block'], $id); } else { $value = db_query('UPDATE {aggregator_feed} SET block = %d WHERE fid = %d', $edit['block'], $id); } } else if ($op == 'view') { list($type, $id) = explode('-', $delta); switch ($type) { case 'feed': if ($feed = db_fetch_object(db_query('SELECT fid, title, block FROM {aggregator_feed} WHERE fid = %d', $id))) { $block['subject'] = check_plain($feed->title); $result = db_query_range('SELECT * FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC, iid DESC', $feed->fid, 0, $feed->block); $read_more = ''; } break; case 'category': if ($category = db_fetch_object(db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = %d', $id))) { $block['subject'] = check_plain($category->title); $result = db_query_range('SELECT i.* FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid = i.iid WHERE ci.cid = %d ORDER BY i.timestamp DESC, i.iid DESC', $category->cid, 0, $category->block); $read_more = ''; } break; } $items = array(); while ($item = db_fetch_object($result)) { $items[] = theme('feedapi_aggregator_block_item', $item); } // Only display the block if there are items to show. if (count($items) > 0) { $block['content'] = theme('item_list', $items) . $read_more; } } return $block; } } /** * Generate a form to add/edit/delete aggregator categories. */ function feedapi_aggregator_form_category($edit = array()) { $form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $edit['title'], '#maxlength' => 64, '#required' => TRUE, ); $form['description'] = array('#type' => 'textarea', '#title' => t('Description'), '#default_value' => $edit['description'], ); $form['submit'] = array('#type' => 'submit', '#value' => t('Submit')); if ($edit['cid']) { $form['delete'] = array('#type' => 'submit', '#value' => t('Delete')); $form['cid'] = array('#type' => 'hidden', '#value' => $edit['cid']); } return $form; } /** * Validate feedapi_aggregator_form_feed form submissions. */ function feedapi_aggregator_form_category_validate($form_id, $form_values) { if ($form_values['op'] == t('Submit')) { // Check for duplicate titles if (isset($form_values['cid'])) { $category = db_fetch_object(db_query("SELECT cid FROM {aggregator_category} WHERE title = '%s' AND cid != %d", $form_values['title'], $form_values['cid'])); } else { $category = db_fetch_object(db_query("SELECT cid FROM {aggregator_category} WHERE title = '%s'", $form_values['title'])); } if ($category) { form_set_error('title', t('A category named %category already exists. Please enter a unique title.', array('%category' => $form_values['title']))); } } } /** * Process feedapi_aggregator_form_category form submissions. * @todo Add delete confirmation dialog. */ function feedapi_aggregator_form_category_submit($form_id, $form_values) { if ($form_values['op'] == t('Delete')) { $title = $form_values['title']; // Unset the title: unset($form_values['title']); } feedapi_aggregator_save_category($form_values); menu_rebuild(); if (isset($form_values['cid'])) { if (isset($form_values['title'])) { drupal_set_message(t('The category %category has been updated.', array('%category' => $form_values['title']))); if (arg(0) == 'admin') { return 'admin/content/feed/category/'; } else { return 'aggregator/categories/'. $form_values['cid']; } } else { watchdog('aggregator', t('Category %category deleted.', array('%category' => $title))); drupal_set_message(t('The category %category has been deleted.', array('%category' => $title))); if (arg(0) == 'admin') { return 'admin/content/feed/category/'; } else { return 'aggregator/categories/'; } } } else { watchdog('aggregator', t('Category %category added.', array('%category' => $form_values['title'])), WATCHDOG_NOTICE, l(t('view'), 'admin/content/aggregator')); drupal_set_message(t('The category %category has been added.', array('%category' => $form_values['title']))); } } /** * Add/edit/delete aggregator categories. */ function feedapi_aggregator_save_category($edit) { if ($edit['cid'] && $edit['title']) { db_query("UPDATE {aggregator_category} SET title = '%s', description = '%s' WHERE cid = %d", $edit['title'], $edit['description'], $edit['cid']); } else if ($edit['cid']) { db_query('DELETE FROM {aggregator_category} WHERE cid = %d', $edit['cid']); } else if ($edit['title']) { // A single unique id for bundles and feeds, to use in blocks $next_id = db_next_id('{aggregator_category}_cid'); db_query("INSERT INTO {aggregator_category} (cid, title, description, block) VALUES (%d, '%s', '%s', 5)", $next_id, $edit['title'], $edit['description']); } } /** * Implementation of hook_form_alter(). */ function feedapi_aggregator_form_alter($form_id, &$form) { if ($form_id == 'feedapi_edit_page') { // Handling of categories: $options = array(); $values = array(); $categories = db_query('SELECT c.cid, c.title, f.fid FROM {aggregator_category} c LEFT JOIN {aggregator_category_feed} f ON c.cid = f.cid AND f.fid = %d ORDER BY title', $form['fid']['#value']); while ($category = db_fetch_object($categories)) { $options[$category->cid] = check_plain($category->title); if ($category->fid) $values[] = $category->cid; } if ($options) { $form['processors_settings']['feedapi_aggregator_category'] = array('#type' => 'checkboxes', '#title' => t('Feed API Aggregator - Categorize news items'), '#default_value' => $values, '#options' => $options, '#description' => t('New items in this feed will be automatically filed in the checked categories as they are received.'), '#weight' => 0, ); } } } /** * Implementation of hook_feed_save(). */ function feedapi_aggregator_feedapi_save($feed) { if ($feed->update) { // An existing feed is being modified, delete the category listings. db_query('DELETE FROM {aggregator_category_feed} WHERE fid = %d', $feed->fid); db_query("UPDATE {aggregator_feed} SET title = '%s', url = '%s', refresh = %d WHERE fid = %d", $feed->title, $feed->url, $feed->refresh, $feed->fid); } else { db_query("INSERT INTO {aggregator_feed} (fid, title, url, refresh, block) VALUES (%d, '%s', '%s', %d, 5)", $feed->fid, $feed->title, $feed->url, $feed->refresh); } // The feed is being saved, save the categories as well. if ($feed->feedapi_aggregator_category) { foreach ($feed->feedapi_aggregator_category as $cid => $value) { if ($value) { db_query('INSERT INTO {aggregator_category_feed} (fid, cid) VALUES (%d, %d)', $feed->fid, $cid); } } } return $feed; } /** * Implementation of hook_feedapi_update(). */ function feedapi_aggregator_feedapi_update($feed) { $feed->update = TRUE; $feed = feedapi_aggregator_feedapi_save($feed); return $feed; } /** * Implementation of hook_feedapi_delete(). */ function feedapi_aggregator_feedapi_delete(&$feed) { $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d', $feed->fid); while ($item = db_fetch_object($result)) { $items[] = "iid = $item->iid"; } if ($items) { db_query('DELETE FROM {aggregator_category_item} WHERE '. implode(' OR ', $items)); } db_query('DELETE FROM {aggregator_item} WHERE fid = %d', $feed->fid); db_query('DELETE FROM {aggregator_feed} WHERE fid = %d', $feed->fid); } /** * Implementation of hook_feedapi_load(). */ function feedapi_aggregator_feedapi_load($feed) { $aggregator_feed = db_fetch_array(db_query('SELECT * FROM {aggregator_feed} WHERE fid = %d', $feed->fid)); $feed->title = $aggregator_feed['title']; $feed->description = $aggregator_feed['description']; $feed->options->original_url = $aggregator_feed['link']; $feed->feedapi_aggregator = $aggregator_feed; return $feed; } /** * Implementation of hook_feed_validate(). * @todo editing feeds does not work (most likely - see isset($form_values['fid']) and further) * - test and implement. * @question Should we move duplicate feed checking to feedapi? */ function feedapi_aggregator_feedapi_validate($form_id, $form_values, $feed) { // Check for duplicate titles if (isset($form_values['fid'])) { $result = db_query("SELECT title, url FROM {aggregator_feed} WHERE (title = '%s' OR url='%s') AND fid != %d", $form_values['title'], $form_values['url'], $form_values['fid']); } else { $result = db_query("SELECT title, url FROM {aggregator_feed} WHERE title = '%s' OR url='%s'", $form_values['title'], $form_values['url']); } while ($feed = db_fetch_object($result)) { if (strcasecmp($feed->title, $form_values['title']) == 0) { form_set_error('title', t('A feed named %feed already exists. Please enter a unique title.', array('%feed' => $form_values['title']))); } if (strcasecmp($feed->url, $form_values['url']) == 0) { form_set_error('url', t('A feed with this URL %url already exists. Please enter a unique URL.', array('%url' => $form_values['url']))); } } if ($form_values['feedapi_aggregator_category']) { $feed->categories = $form_values['feedapi_aggregator_category']; } return $feed; } function feedapi_aggregator_feedapi_type() { return array("XML feed"); } /** * Implementation of hook_feedapi_get(). */ function feedapi_aggregator_feedapi_get($url, $username = NULL, $password = NULL) { $method = 'GET'; $follow = 3; $data = NULL; $timeout = 15; if (!function_exists('curl_init')) { // Conventional way - use drupal internal download mechanism $headers = array(); if ($username != NULL && $password != NULL) { $headers['Authorization'] = 'Basic: '. base64_encode("$username:$password"); } $result = drupal_http_request($url, $headers, $method, $data, $follow); return $result->data; } // Solid way - use cURL $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_USERAGENT, 'Drupal'); if (strlen($username) > 0 && strlen($password) > 0) { curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); } $result = curl_exec($ch); if (curl_errno($ch) != 0) { watchdog("feedapi", $url ." : ". curl_error($ch), WATCHDOG_WARNING); curl_close($ch); unset($ch); return FALSE; } // return $result; } function feedapi_aggregator_feedapi_expire($feed, $force = FALSE) { /* ** Remove all items that are older than flush item timer: */ $age = time() - variable_get('aggregator_clear', 9676800); $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d AND timestamp < %d', $feed->fid, $age); if (db_num_rows($result)) { $items = array(); while ($item = db_fetch_object($result)) { $items[] = $item->iid; } db_query('DELETE FROM {aggregator_category_item} WHERE iid IN ('. implode(', ', $items) .')'); db_query('DELETE FROM {aggregator_item} WHERE fid = %d AND timestamp < %d', $feed->fid, $age); } } function feedapi_aggregator_feedapi_item_save($feed_item, $fid) { if ($feed_item->title && $feed_item->options->original_url) { $feed_item->fiid = db_next_id('{aggregator_item}_iid'); $feed_item->feedapi_aggregator['iid'] = $feed_item->fiid; db_query("INSERT INTO {aggregator_item} (iid, fid, title, link, author, description, timestamp, guid) VALUES (%d, %d, '%s', '%s', '%s', '%s', %d, '%s')", $feed_item->fiid, $fid, $feed_item->title, $feed_item->options->original_url, $feed_item->options->author, $feed_item->description, $feed_item->options->timestamp, $feed_item->options->guid); // file the items in the categories indicated by the feed $categories = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = %d', $fid); while ($category = db_fetch_object($categories)) { db_query('INSERT INTO {aggregator_category_item} (cid, iid) VALUES (%d, %d)', $category->cid, $feed_item->fiid); } } return $feed_item; } function feedapi_aggregator_feedapi_item_update($feed_item, $fid) { if ($feed_item->fiid && $feed_item->title) { db_query("UPDATE {aggregator_item} SET title = '%s', link = '%s', author = '%s', description = '%s', guid = '%s', timestamp = %d WHERE iid = %d", $feed_item->title, $feed_item->options->original_url, $feed_item->options->author, $feed_item->description, $feed_item->options->guid, $feed_item->options->timestamp, $feed_item->fiid); } return $feed_item; } /** * Implementation of hook_feedapi_item_delete(). */ function feedapi_aggregator_feedapi_item_delete($feed_item) { db_query('DELETE FROM {aggregator_category_item} WHERE iid = %d'. $feed_item->fiid); db_query('DELETE FROM {aggregator_item} WHERE iid = %d'. $feed_item->fiid); } /** * Implementation of hook_feedapi_item_load(). */ function feedapi_aggregator_feedapi_item_load(&$feed_item) { $item = db_fetch_array(db_query("SELECT * FROM {aggregator_item} WHERE iid = %d", $feed_item->fiid)); $feed_item->fiid = $item['iid']; $feed_item->fid = $item['fid']; $feed_item->arrived = 0; // no arrived time $feed_item->options->original_url = $item['link']; $feed_item->options->guid = $item['guid']; $feed_item->options->timestamp = $item['timestamp']; $feed_item->description = $item['description']; $feed_item->title = $item['title']; $feed_item->feedapi_aggregator = $item; return $feed_item; } /** * Implementation of hook_feedapi_item_fetch_items(). */ function feedapi_aggregator_feedapi_item_fetch_items($feed) { $result = db_query( "SELECT * FROM {aggregator_item} ai WHERE fid = %d", $feed->fid); $items = array(); while ($item = db_fetch_object($result)) { $items[] = $item; } return $items; } /** * Implementation of hook_feedapi_item_unique(). */ function feedapi_aggregator_feedapi_item_unique($feed_item, $fid) { if ($feed_item->options->guid) { $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND guid = '%s'", $fid, $feed_item->options->guid)); } // We cannot test against $feed URL because it is not available. // else if ($link && $link != $feed['link'] && $link != $feed['url']) { else if ($feed_item->options->original_url) { $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND link = '%s'", $fid, $feed_item->options->original_url)); } else { $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND title = '%s'", $fid, $feed_item->title)); } if ($entry) { return FALSE; } return TRUE; } /** * Implementation of hook_feedapi_parse(). */ function feedapi_aggregator_feedapi_parse($feed) { include_once './'. drupal_get_path('module', 'feedapi_aggregator') .'/feedapi_aggregator_parser.inc'; if (!isset($feed->feedapi_aggregator)) { $feed->feedapi_aggregator = db_fetch_array(db_query('SELECT * FROM {aggregator_feed} WHERE fid = %d', $feed->fid)); if (!is_array($feed->feedapi_aggregator)) { $feed->feedapi_aggregator = array(); $feed->feedapi_aggregator['url'] = $feed->url; } } return feedapi_aggregator_refresh($feed->feedapi_aggregator); } /** * Implementation of hook_feedapi_compatible(). * * @todo: Actually detect wether this parser could process this URL. * @todo: Lots of parsers will be slow to go through compatible() because all of them * will want to download and check a URL. * @todo: Add this hook to parser requirements array in feedapi. */ function feedapi_aggregator_feedapi_compatible($feed) { return array_shift(feedapi_aggregator_feedapi_type()); } function feedapi_aggregator_get_feed($fid) { return db_fetch_array(db_query('SELECT * FROM {aggregator_feed} WHERE fid = %d', $fid)); } function feedapi_aggregator_get_category($cid) { return db_fetch_array(db_query('SELECT * FROM {aggregator_category} WHERE cid = %d', $cid)); } function feedapi_aggregator_category_view() { $result = db_query('SELECT c.cid, c.title, count(ci.iid) as items FROM {aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid = ci.cid GROUP BY c.cid, c.title ORDER BY title'); $output .= '

'. t('Category overview') .'

'; $header = array(t('Title'), t('Items'), t('Operations')); $rows = array(); while ($category = db_fetch_object($result)) { $rows[] = array(l($category->title, "aggregator/categories/$category->cid"), format_plural($category->items, '1 item', '@count items'), l(t('edit'), "admin/content/aggregator/edit/category/$category->cid")); } $output .= theme('table', $header, $rows); return $output; } /** * Menu callback; displays the most recent items gathered from any feed. */ function feedapi_aggregator_page_last() { drupal_add_feed(url('aggregator/rss'), variable_get('site_name', 'Drupal') .' '. t('aggregator')); return _feedapi_aggregator_page_list('SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_item} i INNER JOIN {aggregator_feed} f ON i.fid = f.fid ORDER BY i.timestamp DESC, i.iid DESC', arg(1)); } /** * Menu callback; displays all the items captured from a particular feed. */ function feedapi_aggregator_page_source() { $feed = db_fetch_object(db_query('SELECT * FROM {aggregator_feed} WHERE fid = %d', arg(2))); $info = theme('aggregator_feed', $feed); return _feedapi_aggregator_page_list('SELECT * FROM {aggregator_item} WHERE fid = '. $feed->fid .' ORDER BY timestamp DESC, iid DESC', arg(3), $info); } /** * Menu callback; displays all the items aggregated in a particular category. */ function feedapi_aggregator_page_category() { $category = db_fetch_object(db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = %d', arg(2))); drupal_add_feed(url('aggregator/rss/'. arg(2)), variable_get('site_name', 'Drupal') .' '. t('aggregator - @title', array('@title' => $category->title))); return _feedapi_aggregator_page_list('SELECT i.*, f.title AS ftitle, f.link AS flink FROM {aggregator_category_item} c LEFT JOIN {aggregator_item} i ON c.iid = i.iid LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE cid = '. $category->cid .' ORDER BY timestamp DESC, iid DESC', arg(3)); } /** * FIXME */ function feedapi_aggregator_page_list($sql, $header, $categorize) { $form['#base'] = 'feedapi_aggregator_page_list'; $form['header'] = array('#value' => $header); $result = pager_query($sql, 20); $categories = array(); $done = FALSE; $form['items'] = array(); $form['categories'] = array('#tree' => TRUE); while ($item = db_fetch_object($result)) { $form['items'][$item->iid] = array('#value' => theme('feedapi_aggregator_page_item', $item)); $form['categories'][$item->iid] = array(); if ($categorize) { $categories_result = db_query('SELECT c.cid, c.title, ci.iid FROM {aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid = ci.cid AND ci.iid = %d', $item->iid); $selected = array(); while ($category = db_fetch_object($categories_result)) { if (!$done) { $categories[$category->cid] = check_plain($category->title); } if ($category->iid) { $selected[] = $category->cid; } } $done = TRUE; $form['categories'][$item->iid] = array( '#type' => variable_get('aggregator_category_selector', 'checkboxes'), '#default_value' => $selected, '#options' => $categories, '#size' => 10, '#multiple' => TRUE ); } } $form['submit'] = array('#type' => 'submit', '#value' => t('Save categories')); $form['pager'] = array('#value' => theme('pager', NULL, 20, 0)); return $form; } /** * Prints an aggregator page listing a number of feed items. Various * menu callbacks use this function to print their feeds. */ function _feedapi_aggregator_page_list($sql, $op, $header = '') { $categorize = (user_access('administer news feeds') && ($op == 'categorize')); $form = feedapi_aggregator_page_list($sql, $header, $categorize); if ($categorize) { return $form; } else { $output = '
'; $output .= $header; foreach ($form['items'] as $item) { $output .= $item['#value']; } $output .= '
'; $output .= $form['pager']['#value']; $output .= $form['feed_icon']['#value']; return $output; } } /** * Menu callback; displays all the feeds used by the aggregator. */ function feedapi_aggregator_page_sources() { $result = db_query('SELECT f.fid, f.title, f.description, f.image, MAX(i.timestamp) AS last FROM {aggregator_feed} f LEFT JOIN {aggregator_item} i ON f.fid = i.fid GROUP BY f.fid, f.title, f.description, f.image ORDER BY last DESC, f.title'); $output = "
\n"; while ($feed = db_fetch_object($result)) { $output .= '

'. check_plain($feed->title) ."

\n"; // Most recent items: $list = array(); if (variable_get('aggregator_summary_items', 3)) { $items = db_query_range('SELECT i.title, i.timestamp, i.link FROM {aggregator_item} i WHERE i.fid = %d ORDER BY i.timestamp DESC', $feed->fid, 0, variable_get('aggregator_summary_items', 3)); while ($item = db_fetch_object($items)) { $list[] = theme('feedapi_aggregator_summary_item', $item); } } $output .= theme('item_list', $list); $link['sources'] = array( 'title' => t('More'), 'href' => 'aggregator/sources/'. $feed->fid ); $output .= '\n"; } $output .= theme('xml_icon', url('aggregator/opml')); $output .= '
'; return $output; } /** * Menu callback; displays all the categories used by the aggregator. */ function feedapi_aggregator_page_categories() { $result = db_query('SELECT c.cid, c.title, c.description FROM {aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid = ci.cid LEFT JOIN {aggregator_item} i ON ci.iid = i.iid GROUP BY c.cid, c.title, c.description'); $output = "
\n"; while ($category = db_fetch_object($result)) { $output .= '

'. check_plain($category->title) ."

\n"; if (variable_get('aggregator_summary_items', 3)) { // @todo: setting for this guy $list = array(); $items = db_query_range('SELECT i.title, i.timestamp, i.link, f.title as feed_title, f.link as feed_link FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON i.iid = ci.iid LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE ci.cid = %d ORDER BY i.timestamp DESC', $category->cid, 0, variable_get('aggregator_summary_items', 3)); while ($item = db_fetch_object($items)) { $list[] = theme('feedapi_aggregator_summary_item', $item); } $output .= theme('item_list', $list); } $link['categories'] = array( 'title' => t('More'), 'href' => 'aggregator/categories/'. $category->cid ); $output .= '\n"; } $output .= '
'; return $output; } /** * Return a themed item heading for summary pages located at "aggregator/sources" * and "aggregator/categories". * * @param $item The item object from the aggregator module. * @return A string containing the output. * * @ingroup themeable */ function theme_feedapi_aggregator_summary_item($item) { $output = ''. check_plain($item->title) .' '. t('%age old', array('%age' => format_interval(time() - $item->timestamp))) .''; if ($item->feed_link) { $output .= ', '. check_plain($item->feed_title) .''; } return $output ."\n"; } /** * Format an individual feed item for display on the aggregator page. * * @ingroup themeable */ function theme_feedapi_aggregator_page_item($item) { $source = ''; if ($item->ftitle && $item->fid) { $source = l($item->ftitle, "aggregator/sources/$item->fid", array('class' => 'feed-item-source')) .' -'; } if (date('Ymd', $item->timestamp) == date('Ymd')) { $source_date = t('%ago ago', array('%ago' => format_interval(time() - $item->timestamp))); } else { $source_date = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i')); } $output .= "
\n"; $output .= '

'. check_plain($item->title) ."

\n"; $output .= "
$source $source_date
\n"; if ($item->description) { $output .= '
'. feedapi_aggregator_filter_xss($item->description) ."
\n"; } $result = db_query('SELECT c.title, c.cid FROM {aggregator_category_item} ci LEFT JOIN {aggregator_category} c ON ci.cid = c.cid WHERE ci.iid = %d ORDER BY c.title', $item->iid); $categories = array(); while ($category = db_fetch_object($result)) { $categories[] = l($category->title, 'aggregator/categories/'. $category->cid); } if ($categories) { $output .= '
'. t('Categories') .': '. implode(', ', $categories) ."
\n"; } $output .= "
\n"; return $output; } /** * Safely render HTML content, as allowed. */ function feedapi_aggregator_filter_xss($value) { return filter_xss($value, preg_split('/\s+|<|>/', variable_get("aggregator_allowed_html_tags", '
      • '), -1, PREG_SPLIT_NO_EMPTY)); } /** * Helper function for drupal_map_assoc. */ function _feedapi_aggregator_items($count) { return format_plural($count, '1 item', '@count items'); }