Commit 3111676c authored by Dries's avatar Dries
Browse files

- Patch #302936 by Aron Novak, Crell et al: convert aggregator module to the...

- Patch #302936 by Aron Novak, Crell et al: convert aggregator module to the new DB abstraction layer.
parent 0e7fbe03
......@@ -26,7 +26,7 @@ function aggregator_view() {
$header = array(t('Title'), t('Items'), t('Last update'), t('Next update'), array('data' => t('Operations'), 'colspan' => '3'));
$rows = array();
while ($feed = db_fetch_object($result)) {
foreach ($result as $feed) {
$rows[] = array(l($feed->title, "aggregator/sources/$feed->fid"), format_plural($feed->items, '1 item', '@count items'), ($feed->checked ? t('@time ago', array('@time' => format_interval(REQUEST_TIME - $feed->checked))) : t('never')), ($feed->checked ? t('%time left', array('%time' => format_interval($feed->checked + $feed->refresh - REQUEST_TIME))) : t('never')), l(t('edit'), "admin/content/aggregator/edit/feed/$feed->fid"), l(t('remove items'), "admin/content/aggregator/remove/$feed->fid"), l(t('update items'), "admin/content/aggregator/update/$feed->fid"));
}
$output .= theme('table', $header, $rows);
......@@ -37,7 +37,7 @@ function aggregator_view() {
$header = array(t('Title'), t('Items'), t('Operations'));
$rows = array();
while ($category = db_fetch_object($result)) {
foreach ($result as $category) {
$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);
......@@ -89,8 +89,8 @@ function aggregator_form_feed(&$form_state, $edit = array('refresh' => 900, 'blo
// 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', $edit['fid']);
while ($category = db_fetch_object($categories)) {
$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 = :fid ORDER BY title', array(':fid' => $edit['fid']));
foreach ($categories as $category) {
$options[$category->cid] = check_plain($category->title);
if ($category->fid) $values[] = $category->cid;
}
......@@ -133,12 +133,12 @@ function aggregator_form_feed_validate($form, &$form_state) {
}
// Check for duplicate titles.
if (isset($form_state['values']['fid'])) {
$result = db_query("SELECT title, url FROM {aggregator_feed} WHERE (title = '%s' OR url = '%s') AND fid <> %d", $form_state['values']['title'], $form_state['values']['url'], $form_state['values']['fid']);
$result = db_query("SELECT title, url FROM {aggregator_feed} WHERE (title = :title OR url = :url) AND fid <> :fid", array(':title' => $form_state['values']['title'], ':url' => $form_state['values']['url'], ':fid' => $form_state['values']['fid']));
}
else {
$result = db_query("SELECT title, url FROM {aggregator_feed} WHERE title = '%s' OR url = '%s'", $form_state['values']['title'], $form_state['values']['url']);
$result = db_query("SELECT title, url FROM {aggregator_feed} WHERE title = :title OR url = :url", array(':title' => $form_state['values']['title'], ':url' => $form_state['values']['url']));
}
while ($feed = db_fetch_object($result)) {
foreach ($result as $feed) {
if (strcasecmp($feed->title, $form_state['values']['title']) == 0) {
form_set_error('title', t('A feed named %feed already exists. Please enter a unique title.', array('%feed' => $form_state['values']['title'])));
}
......@@ -256,11 +256,7 @@ function aggregator_form_opml(&$form_state) {
);
// Handling of categories.
$options = array();
$categories = db_query('SELECT cid, title FROM {aggregator_category} ORDER BY title');
while ($category = db_fetch_object($categories)) {
$options[$category->cid] = check_plain($category->title);
}
$options = array_map('check_plain', db_query("SELECT cid, title FROM {aggregator_category} ORDER BY title")->fetchAllKeyed());
if ($options) {
$form['category'] = array(
'#type' => 'checkboxes',
......@@ -323,8 +319,8 @@ function aggregator_form_opml_submit($form, &$form_state) {
}
// Check for duplicate titles or URLs.
$result = db_query("SELECT title, url FROM {aggregator_feed} WHERE title = '%s' OR url = '%s'", $feed['title'], $feed['url']);
while ($old = db_fetch_object($result)) {
$result = db_query("SELECT title, url FROM {aggregator_feed} WHERE title = :title OR url = :url", array(':title' => $feed['title'], ':url' => $feed['url']));
foreach ($result as $old) {
if (strcasecmp($old->title, $feed['title']) == 0) {
drupal_set_message(t('A feed named %title already exists.', array('%title' => $old->title)), 'warning');
continue 2;
......@@ -458,10 +454,10 @@ function aggregator_form_category_validate($form, &$form_state) {
if ($form_state['values']['op'] == t('Save')) {
// Check for duplicate titles
if (isset($form_state['values']['cid'])) {
$category = db_fetch_object(db_query("SELECT cid FROM {aggregator_category} WHERE title = '%s' AND cid <> %d", $form_state['values']['title'], $form_state['values']['cid']));
$category = db_query("SELECT cid FROM {aggregator_category} WHERE title = :title AND cid <> :cid", array(':title' => $form_state['values']['title'], ':cid' => $form_state['values']['cid']))->fetchObject();
}
else {
$category = db_fetch_object(db_query("SELECT cid FROM {aggregator_category} WHERE title = '%s'", $form_state['values']['title']));
$category = db_query("SELECT cid FROM {aggregator_category} WHERE title = :title", array(':title' => $form_state['values']['title']))->fetchObject();
}
if ($category) {
form_set_error('title', t('A category named %category already exists. Please enter a unique title.', array('%category' => $form_state['values']['title'])));
......
......@@ -262,7 +262,7 @@ function aggregator_init() {
* TRUE if there is at least one category and the user has access to them, FALSE otherwise.
*/
function _aggregator_has_categories() {
return user_access('access news feeds') && db_result(db_query('SELECT COUNT(*) FROM {aggregator_category}'));
return user_access('access news feeds') && db_query('SELECT COUNT(*) FROM {aggregator_category}')->fetchField();
}
/**
......@@ -287,9 +287,9 @@ function aggregator_perm() {
* Checks news feeds for updates once their refresh interval has elapsed.
*/
function aggregator_cron() {
$result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < %d', REQUEST_TIME);
while ($feed = db_fetch_array($result)) {
aggregator_refresh($feed);
$result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time', array(':time' => REQUEST_TIME));
foreach ($result as $feed) {
aggregator_refresh((array)$feed);
}
}
......@@ -302,18 +302,18 @@ function aggregator_block($op = 'list', $delta = '', $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)) {
foreach ($result as $category) {
$block['category-' . $category->cid]['info'] = t('!title category latest items', array('!title' => $category->title));
}
$result = db_query('SELECT fid, title FROM {aggregator_feed} WHERE block <> 0 ORDER BY fid');
while ($feed = db_fetch_object($result)) {
foreach ($result as $feed) {
$block['feed-' . $feed->fid]['info'] = t('!title feed latest items', array('!title' => $feed->title));
}
}
elseif ($op == 'configure') {
list($type, $id) = explode('-', $delta);
if ($type == 'category') {
$value = db_result(db_query('SELECT block FROM {aggregator_category} WHERE cid = %d', $id));
$value = db_query('SELECT block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchField();
$form['block'] = array(
'#type' => 'select',
'#title' => t('Number of news items in block'),
......@@ -326,30 +326,33 @@ function aggregator_block($op = 'list', $delta = '', $edit = array()) {
elseif ($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);
db_merge('aggregator_category')
->key(array('cid' => $id))
->fields(array('block' => $edit['block']))
->execute();
}
}
elseif ($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 block <> 0 AND fid = %d', $id))) {
if ($feed = db_query('SELECT fid, title, block FROM {aggregator_feed} WHERE block <> 0 AND fid = :fid', array(':fid' => $id))->fetchObject()) {
$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);
$result = db_query_range("SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC", array(':fid' => $id), 0, $feed->block);
$read_more = theme('more_link', url('aggregator/sources/' . $feed->fid), t("View this feed's recent news."));
}
break;
case 'category':
if ($category = db_fetch_object(db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = %d', $id))) {
if ($category = db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchObject()) {
$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);
$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);
$read_more = theme('more_link', url('aggregator/categories/' . $category->cid), t("View this category's recent news."));
}
break;
}
$items = array();
while ($item = db_fetch_object($result)) {
foreach ($result as $item) {
$items[] = theme('aggregator_block_item', $item);
}
......@@ -375,21 +378,36 @@ function aggregator_save_category($edit) {
if (!empty($edit['cid'])) {
$link_path .= $edit['cid'];
if (!empty($edit['title'])) {
db_query("UPDATE {aggregator_category} SET title = '%s', description = '%s' WHERE cid = %d", $edit['title'], $edit['description'], $edit['cid']);
db_merge('aggregator_category')
->key(array('cid' => $edit['cid']))
->fields(array(
'title' => $edit['title'],
'description' => $edit['description'],
))
->execute();
$op = 'update';
}
else {
db_query('DELETE FROM {aggregator_category} WHERE cid = %d', $edit['cid']);
db_delete('aggregator_category')
->condition('cid', $edit['cid'])
->execute();
// Make sure there is no active block for this category.
db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s'", 'aggregator', 'category-' . $edit['cid']);
db_delete('blocks')
->condition('module', 'aggregator')
->condition('delta', 'category-' . $edit['cid'])
->execute();
$edit['title'] = '';
$op = 'delete';
}
}
elseif (!empty($edit['title'])) {
// A single unique id for bundles and feeds, to use in blocks.
db_query("INSERT INTO {aggregator_category} (title, description, block) VALUES ('%s', '%s', 5)", $edit['title'], $edit['description']);
$link_path .= db_last_insert_id('aggregator_category', 'cid');
$link_path .= db_insert('aggregator_category')
->fields(array(
'title' => $edit['title'],
'description' => $edit['description'],
))
->execute();
$op = 'insert';
}
if (isset($op)) {
......@@ -406,36 +424,64 @@ function aggregator_save_category($edit) {
function aggregator_save_feed($edit) {
if (!empty($edit['fid'])) {
// An existing feed is being modified, delete the category listings.
db_query('DELETE FROM {aggregator_category_feed} WHERE fid = %d', $edit['fid']);
db_delete('aggregator_category_feed')
->condition('fid', $edit['fid'])
->execute();
}
if (!empty($edit['fid']) && !empty($edit['title'])) {
db_query("UPDATE {aggregator_feed} SET title = '%s', url = '%s', refresh = %d, block = %d WHERE fid = %d", $edit['title'], $edit['url'], $edit['refresh'], $edit['block'], $edit['fid']);
db_update('aggregator_feed')
->condition('fid', $edit['fid'])
->fields(array(
'title' => $edit['title'],
'url' => $edit['url'],
'refresh' => $edit['refresh'],
'block' => $edit['block'],
))
->execute();
}
elseif (!empty($edit['fid'])) {
$items = array();
$result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d', $edit['fid']);
while ($item = db_fetch_object($result)) {
$items[] = "iid = $item->iid";
$iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $edit['fid']))->fetchCol();
if ($iids) {
db_delete('aggregator_category_item')
->condition('iid', $iids, 'IN')
->execute();
}
if (!empty($items)) {
db_query('DELETE FROM {aggregator_category_item} WHERE ' . implode(' OR ', $items));
}
db_query('DELETE FROM {aggregator_feed} WHERE fid = %d', $edit['fid']);
db_query('DELETE FROM {aggregator_item} WHERE fid = %d', $edit['fid']);
db_delete('aggregator_feed')->
condition('fid', $edit['fid'])
->execute();
db_delete('aggregator_item')
->condition('fid', $edit['fid'])
->execute();
// Make sure there is no active block for this feed.
db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s'", 'aggregator', 'feed-' . $edit['fid']);
db_delete('blocks')
->condition('module', 'aggregator')
->condition('delta', 'feed-' . $edit['fid'])
->execute();
}
elseif (!empty($edit['title'])) {
db_query("INSERT INTO {aggregator_feed} (title, url, refresh, block, description, image) VALUES ('%s', '%s', %d, %d, '', '')", $edit['title'], $edit['url'], $edit['refresh'], $edit['block']);
// A single unique ID for bundles and feeds, to use in blocks.
$edit['fid'] = db_last_insert_id('aggregator_feed', 'fid');
$edit['fid'] = db_insert('aggregator_feed')
->fields(array(
'title' => $edit['title'],
'url' => $edit['url'],
'refresh' => $edit['refresh'],
'block' => $edit['block'],
'description' => '',
'image' => '',
))
->execute();
}
if (!empty($edit['title'])) {
// The feed is being saved, save the categories as well.
if (!empty($edit['category'])) {
foreach ($edit['category'] as $cid => $value) {
if ($value) {
db_query('INSERT INTO {aggregator_category_feed} (fid, cid) VALUES (%d, %d)', $edit['fid'], $cid);
db_merge('aggregator_category_feed')
->fields(array(
'fid' => $edit['fid'],
'cid' => $cid,
))
->execute();
}
}
}
......@@ -449,15 +495,26 @@ function aggregator_save_feed($edit) {
* An associative array describing the feed to be cleared.
*/
function aggregator_remove($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 (!empty($items)) {
db_query('DELETE FROM {aggregator_category_item} WHERE ' . implode(' OR ', $items));
$iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed['fid']))->fetchCol();
if ($iids) {
db_delete('aggregator_category_item')
->condition('iid', $iids, 'IN')
->execute();
}
db_query('DELETE FROM {aggregator_item} WHERE fid = %d', $feed['fid']);
db_query("UPDATE {aggregator_feed} SET checked = 0, hash = '', etag = '', modified = 0 WHERE fid = %d", $feed['fid']);
db_delete('aggregator_item')
->condition('fid', $feed['fid'])
->execute();
db_merge('aggregator_feed')
->key(array('fid' => $feed['fid']))
->fields(array(
'checked' => 0,
'hash' => '',
'modified' => 0,
'description' => $feed['description'],
'image' => $feed['image'],
))
->execute();
drupal_set_message(t('The news items from %site have been removed.', array('%site' => $feed['title'])));
}
......@@ -598,7 +655,10 @@ function aggregator_refresh($feed) {
// Process HTTP response code.
switch ($result->code) {
case 304:
db_query('UPDATE {aggregator_feed} SET checked = %d WHERE fid = %d', REQUEST_TIME, $feed['fid']);
db_update('aggregator_feed')
->fields(array('checked' => REQUEST_TIME))
->condition('fid', $feed['fid'])
->execute();
drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed['title'])));
break;
case 301:
......@@ -612,7 +672,10 @@ function aggregator_refresh($feed) {
// data. If both are equal we say that feed is not updated.
$md5 = md5($result->data);
if ($feed['hash'] == $md5) {
db_query('UPDATE {aggregator_feed} SET checked = %d WHERE fid = %d', REQUEST_TIME, $feed['fid']);
db_update('aggregator_feed')
->condition('fid', $feed['fid'])
->fields(array('checked' => REQUEST_TIME))
->execute();
drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed['title'])));
break;
}
......@@ -642,7 +705,19 @@ function aggregator_refresh($feed) {
$etag = empty($result->headers['ETag']) ? '' : $result->headers['ETag'];
// Update the feed data.
db_query("UPDATE {aggregator_feed} SET url = '%s', checked = %d, link = '%s', description = '%s', image = '%s', hash = '%s', etag = '%s', modified = %d WHERE fid = %d", $feed['url'], REQUEST_TIME, $channel['LINK'], $channel['DESCRIPTION'], $image, $md5, $etag, $modified, $feed['fid']);
db_merge('aggregator_feed')
->key(array('fid' => $feed['fid']))
->fields(array(
'url' => $feed['url'],
'checked' => REQUEST_TIME,
'link' => $channel['LINK'],
'description' => $channel['DESCRIPTION'],
'image' => $image,
'hash' => $md5,
'etag' => $etag,
'modified' => $modified,
))
->execute();
// Clear the cache.
cache_clear_all();
......@@ -799,13 +874,13 @@ function aggregator_parse_feed(&$data, $feed) {
// we find a duplicate entry, we resolve it and pass along its ID is such
// that we can update it if needed.
if (!empty($guid)) {
$entry = db_fetch_object(db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = %d AND guid = '%s'", $feed['fid'], $guid));
$entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = :fid AND guid = :guid", array(':fid' => $feed['fid'], ':guid' => $guid))->fetchObject();
}
elseif ($link && $link != $feed['link'] && $link != $feed['url']) {
$entry = db_fetch_object(db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = %d AND link = '%s'", $feed['fid'], $link));
$entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = :fid AND link = :link", array(':fid' => $feed['fid'], ':link' => $link))->fetchObject();
}
else {
$entry = db_fetch_object(db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = %d AND title = '%s'", $feed['fid'], $title));
$entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = :fid AND title = :title", array(':fid' => $feed['fid'], ':title' => $title))->fetchObject();
}
if (!$timestamp) {
......@@ -817,17 +892,14 @@ function aggregator_parse_feed(&$data, $feed) {
// Remove all items that are older than flush item timer.
$age = REQUEST_TIME - variable_get('aggregator_clear', 9676800);
$result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d AND timestamp < %d', $feed['fid'], $age);
$items = array();
$num_rows = FALSE;
while ($item = db_fetch_object($result)) {
$items[] = $item->iid;
$num_rows = TRUE;
}
if ($num_rows) {
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);
$iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid AND timestamp < :timestamp', array(':fid' => $feed['fid'], ':timestamp' => $age))->fetchCol();
if ($iids) {
db_delete('aggregator_category_item')
->condition('iid', $iids, 'IN')
->execute();
db_delete('aggregator_item')
->condition('iid', $iids, 'IN')
->execute();
}
return TRUE;
......@@ -840,20 +912,37 @@ function aggregator_parse_feed(&$data, $feed) {
* An associative array describing the item to be added/edited/deleted.
*/
function aggregator_save_item($edit) {
if ($edit['iid'] && $edit['title']) {
db_query("UPDATE {aggregator_item} SET title = '%s', link = '%s', author = '%s', description = '%s', guid = '%s', timestamp = %d WHERE iid = %d", $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['guid'], $edit['timestamp'], $edit['iid']);
if ($edit['title'] && empty($edit['iid'])) {
$edit['iid'] = db_insert('aggregator_item')
->fields(array(
'title' => $edit['title'],
'link' => $edit['link'],
'author' => $edit['author'],
'description' => $edit['description'],
'guid' => $edit['guid'],
'timestamp' => $edit['timestamp'],
'fid' => $edit['fid'],
))
->execute();
}
elseif ($edit['iid']) {
db_query('DELETE FROM {aggregator_item} WHERE iid = %d', $edit['iid']);
db_query('DELETE FROM {aggregator_category_item} WHERE iid = %d', $edit['iid']);
if ($edit['iid'] && !$edit['title']) {
db_delete('aggregator_item')
->condition('iid', $edit['iid'])
->execute();
db_delete('aggregator_category_item')
->condition('iid', $edit['iid'])
->execute();
}
elseif ($edit['title'] && $edit['link']) {
db_query("INSERT INTO {aggregator_item} (fid, title, link, author, description, timestamp, guid) VALUES (%d, '%s', '%s', '%s', '%s', %d, '%s')", $edit['fid'], $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['timestamp'], $edit['guid']);
$edit['iid'] = db_last_insert_id('aggregator_item', 'iid');
// file the items in the categories indicated by the feed
$categories = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = %d', $edit['fid']);
while ($category = db_fetch_object($categories)) {
db_query('INSERT INTO {aggregator_category_item} (cid, iid) VALUES (%d, %d)', $category->cid, $edit['iid']);
$result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = :fid', array(':fid' => $edit['fid']));
foreach ($result as $category) {
db_merge('aggregator_category_item')
->fields(array(
'cid' => $category->cid,
'iid' => $edit['iid'],
))
->execute();
}
}
}
......@@ -869,7 +958,7 @@ function aggregator_save_item($edit) {
function aggregator_feed_load($fid) {
static $feeds;
if (!isset($feeds[$fid])) {
$feeds[$fid] = db_fetch_array(db_query('SELECT * FROM {aggregator_feed} WHERE fid = %d', $fid));
$feeds[$fid] = db_query('SELECT * FROM {aggregator_feed} WHERE fid = :fid', array(':fid' => $fid))->fetchAssoc();
}
return $feeds[$fid];
......@@ -886,7 +975,7 @@ function aggregator_feed_load($fid) {
function aggregator_category_load($cid) {
static $categories;
if (!isset($categories[$cid])) {
$categories[$cid] = db_fetch_array(db_query('SELECT * FROM {aggregator_category} WHERE cid = %d', $cid));
$categories[$cid] = db_query('SELECT * FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $cid))->fetchAssoc();
}
return $categories[$cid];
......
......@@ -15,7 +15,7 @@
function aggregator_page_last() {
drupal_add_feed(url('aggregator/rss'), variable_get('site_name', 'Drupal') . ' ' . t('aggregator'));
$items = aggregator_feed_items_load('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');
$items = aggregator_feed_items_load('sum');
return _aggregator_page_list($items, arg(1));
}
......@@ -42,7 +42,7 @@ function aggregator_page_source($arg1, $arg2 = NULL) {
// It is safe to include the fid in the query because it's loaded from the
// database by aggregator_feed_load.
$items = aggregator_feed_items_load('SELECT * FROM {aggregator_item} WHERE fid = ' . $feed->fid . ' ORDER BY timestamp DESC, iid DESC');
$items = aggregator_feed_items_load('source', $feed);
return _aggregator_page_list($items, arg(3), $feed_source);
}
......@@ -68,31 +68,39 @@ function aggregator_page_category($arg1, $arg2 = NULL) {
// It is safe to include the cid in the query because it's loaded from the
// database by aggregator_category_load.
$items = aggregator_feed_items_load('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, i.iid DESC');
$items = aggregator_feed_items_load('category', $category);
return _aggregator_page_list($items, arg(3));
}
/**
* Load feed items by passing a SQL query.
* Load feed items
*
* @param $sql
* The query to be executed.
* @param $type
* The filter for the items. Possible values: 'sum', 'source', 'category'
* @param $data
* Feed or category data for filtering
* @return
* An array of the feed items.
*/
function aggregator_feed_items_load($sql) {
function aggregator_feed_items_load($type, $data = NULL) {
$items = array();
if (isset($sql)) {
$result = pager_query($sql, 20);
while ($item = db_fetch_object($result)) {
$result_category = 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);
$item->categories = array();
while ($item_categories = db_fetch_object($result_category)) {
$item->categories[] = $item_categories;
}
$items[$item->iid] = $item;
}
$range_limit = 20;
switch ($type) {
case 'sum':
$result = db_query_range('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', 0, $range_limit);
break;
case 'source':
$result = db_query_range('SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC', array(':fid' => $data->fid), 0, $range_limit);
break;
case 'category':
$result = db_query_range('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 = :cid ORDER BY timestamp DESC, i.iid DESC', array(':cid' => $data['cid']), 0, $range_limit);
break;
}
foreach ($result as $item) {
$item->categories = 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 = :iid ORDER BY c.title', array(':iid' => $item->iid))->fetchAll();
$items[] = $item;
}
return $items;
......@@ -158,9 +166,9 @@ function aggregator_categorize_items($items, $feed_source = '') {
foreach ($items as $item) {
$form['items'][$item->iid] = array('#markup' => theme('aggregator_item', $item));
$form['categories'][$item->iid] = array();
$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);
$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 = :iid', array(':iid' => $item->iid));
$selected = array();
while ($category = db_fetch_object($categories_result)) {
foreach ($categories_result as $category) {
if (!$done) {
$categories[$category->cid] = check_plain($category->title);
}
......@@ -197,12 +205,23 @@ function aggregator_categorize_items_validate($form, &$form_state) {
function aggregator_categorize_items_submit($form, &$form_state) {
if (!empty($form_state['values']['categories'])) {
foreach ($form_state['values']['categories'] as $iid => $selection) {
db_query('DELETE FROM {aggregator_category_item} WHERE iid = %d', $iid);
db_delete('aggregator_category_item')
->condition('iid', $iid)
->execute();
$insert = db_insert('aggregator_category_item')->fields(array('iid', 'cid'));
$has_values = FALSE;
foreach ($selection as $cid) {
if ($cid) {
db_query('INSERT INTO {aggregator_category_item} (cid, iid) VALUES (%d, %d)', $cid, $iid);
if ($cid && $iid) {
$has_values = TRUE;
$insert->values(array(
'iid' => $iid,
'cid' => $cid,
));
}
}
if ($has_values) {
$insert->execute();
}