Index: modules/aggregator/aggregator.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/aggregator/aggregator.admin.inc,v retrieving revision 1.15 diff -u -p -r1.15 aggregator.admin.inc --- modules/aggregator/aggregator.admin.inc 6 Sep 2008 08:36:19 -0000 1.15 +++ modules/aggregator/aggregator.admin.inc 9 Sep 2008 19:17:33 -0000 @@ -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($_SERVER['REQUEST_TIME'] - $feed->checked))) : t('never')), ($feed->checked ? t('%time left', array('%time' => format_interval($feed->checked + $feed->refresh - $_SERVER['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,13 @@ function aggregator_form_feed(&$form_sta // 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_select('aggregator_category', 'c'); + $f_alias = $categories->leftJoin('aggregator_category_feed', 'f', 'c.cid = f.cid AND f.fid = :fid', array(':fid' => $edit['fid'])); + $categories->addField('c', 'cid', 'cid'); + $title_field = $categories->addField('c', 'title', 'title'); + $categories->addField($f_alias, 'fid', 'fid'); + $result = $categories->orderBy($title_field)->execute(); + foreach ($result as $category) { $options[$category->cid] = check_plain($category->title); if ($category->fid) $values[] = $category->cid; } @@ -132,13 +137,14 @@ function aggregator_form_feed_validate($ form_set_error('url', t('The URL %url is invalid. Please enter a fully-qualified URL, such as http://www.example.com/feed.xml.', array('%url' => $form_state['values']['url']))); } // Check for duplicate titles. + $query = db_select('aggregator_feed', 'f')->condition(db_or()->condition('title', $form_state['values']['title'])->condition('url', $form_state['values']['url'])); + $query->addField('f', 'title', 'title'); + $query->addField('f', 'url', 'url'); 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']); + $query->condition('f.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']); - } - while ($feed = db_fetch_object($result)) { + $result = $query->execute(); + 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']))); } @@ -258,7 +264,7 @@ function aggregator_form_opml(&$form_sta // Handling of categories. $options = array(); $categories = db_query('SELECT cid, title FROM {aggregator_category} ORDER BY title'); - while ($category = db_fetch_object($categories)) { + foreach ($categories as $category) { $options[$category->cid] = check_plain($category->title); } if ($options) { @@ -323,8 +329,8 @@ function aggregator_form_opml_submit($fo } // 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; @@ -457,13 +463,14 @@ function aggregator_form_category(&$form function aggregator_form_category_validate($form, &$form_state) { if ($form_state['values']['op'] == t('Save')) { // Check for duplicate titles + $query = db_select('aggregator_category', 'c')->condition('title', $form_state['values']['title']); 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 = $query->condition('cid', $form_state['values']['cid'], '<>')->countQuery()->execute()->fetchField(); } else { - $category = db_fetch_object(db_query("SELECT cid FROM {aggregator_category} WHERE title = '%s'", $form_state['values']['title'])); + $category = $query->countQuery()->execute()->fetchField(); } - if ($category) { + if ($category != 0) { form_set_error('title', t('A category named %category already exists. Please enter a unique title.', array('%category' => $form_state['values']['title']))); } } Index: modules/aggregator/aggregator.module =================================================================== RCS file: /cvs/drupal/drupal/modules/aggregator/aggregator.module,v retrieving revision 1.391 diff -u -p -r1.391 aggregator.module --- modules/aggregator/aggregator.module 6 Sep 2008 08:36:19 -0000 1.391 +++ modules/aggregator/aggregator.module 9 Sep 2008 19:17:34 -0000 @@ -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_select("aggregator_category")->countQuery()->execute()->fetchField()); } /** @@ -281,8 +281,8 @@ 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', $_SERVER['REQUEST_TIME']); - while ($feed = db_fetch_array($result)) { + $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time', array(':time' => $_SERVER['REQUEST_TIME'])); + foreach ($result as $feed) { aggregator_refresh($feed); } } @@ -295,19 +295,29 @@ function aggregator_cron() { 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)) { + $query = db_select("aggregator_category", "c"); + $query->addField("c", "cid", "cid"); + $title_field = $query->addField("c", "title", "title"); + $query->orderBy($title_field); + $result = $query->execute(); + 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)) { + $query = db_select("aggregator_feed", "f"); + $fid_field = $query->addField("f", "fid", "fid"); + $query->addField("f", "title", "title"); + $query->orderBy($fid_field); + $result = $query->condition("block", 0, "<>")->execute(); + 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)); + $query = db_select("aggregator_category", "f"); + $query->addField("f", "block", "block"); + $value = $query->condition("cid", $id)->execute()->fetchField(); $form['block'] = array( '#type' => 'select', '#title' => t('Number of news items in block'), @@ -320,30 +330,40 @@ function aggregator_block($op = 'list', 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))) { + $query = db_select("aggregator_feed", "f"); + $query->addField("f", "title", "title"); + $query->addField("f", "block", "block"); + $query->addField("f", "fid", "fid"); + $feed = $query->condition(db_and()->condition('block', 0, '<>')->condition('fid', $id))->execute()->fetch(); + if ($feed) { $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))) { + $query = db_select("aggregator_category", "f"); + $query->addField("f", "title", "title"); + $query->addField("f", "block", "block"); + $query->addField("f", "cid", "cid"); + $category = $query->condition(db_and()->condition('block', 0, '<>')->condition('cid', $id))->execute()->fetch(); + if ($category) { $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); } @@ -369,20 +389,20 @@ 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' => isset($id) ? $id : FALSE))->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(db_and()->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']); + db_merge('aggregator_category')->fields(array('title' => $edit['title'], 'description' => $edit['description']))->execute(); $link_path .= db_last_insert_id('aggregator_category', 'cid'); $op = 'insert'; } @@ -400,27 +420,32 @@ 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"; + $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $edit['fid'])); + $delete = db_delete('aggregator_category_item'); + $items = FALSE; + $condition = db_or(); + foreach ($result as $item) { + $items = TRUE; + $condition->condition('iid', $item->iid); } - if (!empty($items)) { - db_query('DELETE FROM {aggregator_category_item} WHERE ' . implode(' OR ', $items)); + if ($items) { + $delete->condition($condition)->execute(); } - 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']); + db_insert('aggregator_feed')->fields(array('title' => $edit['title'], 'url' => $edit['url'], 'refresh' => $edit['refresh'], 'block' => $edit['block'], 'description' => '', 'image' => ''))->execute(); + // A single unique ID for bundles and feeds, to use in blocks. $edit['fid'] = db_last_insert_id('aggregator_feed', 'fid'); } @@ -429,7 +454,7 @@ function aggregator_save_feed($edit) { 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(); } } } @@ -443,15 +468,21 @@ 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"; + $query = db_select("aggregator_item", "i"); + $query->addField("i", "iid", "iid"); + $result = $query->condition('fid', $feed['fid'])->execute(); + $delete = db_delete('aggregator_category_item'); + $condition = db_or(); + $items = FALSE; + foreach ($result as $item) { + $items = TRUE; + $condition->condition('iid', $item->iid); } - if (!empty($items)) { - db_query('DELETE FROM {aggregator_category_item} WHERE ' . implode(' OR ', $items)); + if ($items) { + $delete->condition($condition)->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']))); } @@ -592,7 +623,7 @@ function aggregator_refresh($feed) { // Process HTTP response code. switch ($result->code) { case 304: - db_query('UPDATE {aggregator_feed} SET checked = %d WHERE fid = %d', $_SERVER['REQUEST_TIME'], $feed['fid']); + db_merge('aggregator_feed')->key(array('fid' => $feed['fid']))->fields(array('checked' => $_SERVER['REQUEST_TIME']))->execute(); drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed['title']))); break; case 301: @@ -606,7 +637,7 @@ 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', $_SERVER['REQUEST_TIME'], $feed['fid']); + db_update('aggregator_feed')->condition('fid', $feed['fid'])->fields(array('checked' => $_SERVER['REQUEST_TIME']))->execute(); drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed['title']))); break; } @@ -631,12 +662,12 @@ function aggregator_refresh($feed) { $image = '' . check_plain($image['TITLE']) . ''; } else { - $image = NULL; + $image = ''; } $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'], $_SERVER['REQUEST_TIME'], $channel['LINK'], $channel['DESCRIPTION'], $image, $md5, $etag, $modified, $feed['fid']); + // Update the feed data + db_merge('aggregator_feed')->key(array('fid' => $feed['fid']))->fields(array('checked' => $_SERVER['REQUEST_TIME'], 'link' => $channel['LINK'], 'description' => $channel['DESCRIPTION'], 'image' => $image, 'hash' => $md5, 'etag' => $etag, 'modified' => $modified))->execute(); // Clear the cache. cache_clear_all(); @@ -792,14 +823,16 @@ function aggregator_parse_feed(&$data, $ // Save this item. Try to avoid duplicate entries as much as possible. If // we find a duplicate entry, we resolve it and pass along its ID is such // that we can update it if needed. + $query = db_select('aggregator_item', 'i')->condition('fid', $feed['fid']); + $query->addField('i', 'iid', 'iid'); 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 = $query->condition('guid', $guid)->execute()->fetch(); } else if ($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 = $query->condition('link', $link)->execute()->fetch(); } else { - $entry = db_fetch_object(db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = %d AND title = '%s'", $feed['fid'], $title)); + $entry = $query->condition('title', $title)->execute()->fetch(); } if (!$timestamp) { @@ -811,17 +844,22 @@ function aggregator_parse_feed(&$data, $ // Remove all items that are older than flush item timer. $age = $_SERVER['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; + $query = db_select('aggregator_item', 'i')->condition('fid', $feed['fid'])->condition('timestamp', $age); + $query->addField('i', 'iid', 'iid'); + $result = $query->execute(); + + $delete_c = db_delete('aggregator_category_item'); + $delete_i = db_delete('aggregator_item'); + $condition_c = db_or(); + $condition_i = db_or(); + foreach ($result as $item) { + $condition_c->condition('iid', $item->iid); + $condition_i->condition('iid', $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); + $delete_c->condition($condition_c)->execute(); + $delete_i->condition($condition_i)->execute(); } return TRUE; @@ -834,20 +872,21 @@ function aggregator_parse_feed(&$data, $ * 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'])) { + 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']); + $query = db_select('aggregator_category_feed', 'cf')->condition('fid', $edit['fid']); + $query->addField('cf', 'cid', 'cid'); + $result = $query->execute(); + foreach ($result as $category) { + db_merge('aggregator_category_item')->fields(array('cid' => $category->cid, 'iid' => $edit['iid']))->execute(); } } } @@ -863,7 +902,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]; @@ -880,7 +919,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]; Index: modules/aggregator/aggregator.pages.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/aggregator/aggregator.pages.inc,v retrieving revision 1.17 diff -u -p -r1.17 aggregator.pages.inc --- modules/aggregator/aggregator.pages.inc 6 Sep 2008 08:36:19 -0000 1.17 +++ modules/aggregator/aggregator.pages.inc 9 Sep 2008 19:17:35 -0000 @@ -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, $ // 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,43 @@ function aggregator_page_category($arg1, // 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) { + $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 = :iid ORDER BY c.title', array(':iid' => $item->iid)); + $item->categories = array(); + foreach ($result_category as $item_categories) { + $item->categories[] = $item_categories; } + $items[$item->iid] = $item; } return $items; @@ -158,9 +170,9 @@ function aggregator_categorize_items($it 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,10 +209,10 @@ function aggregator_categorize_items_val 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(); foreach ($selection as $cid) { if ($cid) { - db_query('INSERT INTO {aggregator_category_item} (cid, iid) VALUES (%d, %d)', $cid, $iid); + db_insert('aggregator_category_item')->fields(array('iid' => $iid, 'cid' => $cid))->execute(); } } } @@ -284,12 +296,12 @@ function 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 = ''; - while ($feed = db_fetch_object($result)) { + foreach ($result as $feed) { // Most recent items: $summary_items = 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)) { + $items = db_query_range('SELECT i.title, i.timestamp, i.link FROM {aggregator_item} i WHERE i.fid = :fid ORDER BY i.timestamp DESC', array(':fid' => $feed->fid), 0, variable_get('aggregator_summary_items', 3)); + foreach ($items as $item) { $summary_items[] = theme('aggregator_summary_item', $item); } } @@ -308,11 +320,11 @@ function 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 = ''; - while ($category = db_fetch_object($result)) { + foreach ($result as $category) { if (variable_get('aggregator_summary_items', 3)) { $summary_items = 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)) { + $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 = :cid ORDER BY i.timestamp DESC', array(':cid' => $category->cid), 0, variable_get('aggregator_summary_items', 3)); + foreach ($items as $item) { $summary_items[] = theme('aggregator_summary_item', $item); } } @@ -330,9 +342,9 @@ function aggregator_page_rss() { $result = NULL; // arg(2) is the passed cid, only select for that category. if (arg(2)) { - $category = db_fetch_object(db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = %d', arg(2))); - $sql = '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 = %d ORDER BY timestamp DESC, i.iid DESC'; - $result = db_query_range($sql, $category->cid, 0, variable_get('feed_default_items', 10)); + $category = db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = :cid', array(':cid' => arg(2)))->fetchRow(); + $sql = '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'; + $result = db_query_range($sql, array(':cid' => $category->cid), 0, variable_get('feed_default_items', 10)); } // Or, get the default aggregator items. else { @@ -342,7 +354,7 @@ function aggregator_page_rss() { } $feeds = array(); - while ($item = db_fetch_object($result)) { + foreach ($result as $item) { $feeds[] = $item; } @@ -401,14 +413,14 @@ function theme_aggregator_page_rss($feed */ function aggregator_page_opml($cid = NULL) { if ($cid) { - $result = db_query('SELECT f.title, f.url FROM {aggregator_feed} f LEFT JOIN {aggregator_category_feed} c on f.fid = c.fid WHERE c.cid = %d ORDER BY title', $cid); + $result = db_query('SELECT f.title, f.url FROM {aggregator_feed} f LEFT JOIN {aggregator_category_feed} c on f.fid = c.fid WHERE c.cid = :cid ORDER BY title', array(':cid' => $cid)); } else { $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title'); } $feeds = array(); - while ($item = db_fetch_object($result)) { + foreach ($resut as $item) { $feeds[] = $item; } Index: modules/aggregator/aggregator.test =================================================================== RCS file: /cvs/drupal/drupal/modules/aggregator/aggregator.test,v retrieving revision 1.7 diff -u -p -r1.7 aggregator.test --- modules/aggregator/aggregator.test 3 Sep 2008 19:25:08 -0000 1.7 +++ modules/aggregator/aggregator.test 9 Sep 2008 19:17:35 -0000 @@ -23,7 +23,7 @@ class AggregatorTestCase extends DrupalW $this->drupalPost('admin/content/aggregator/add/feed', $edit, t('Save')); $this->assertRaw(t('The feed %name has been added.', array('%name' => $edit['title'])), t('The feed !name has been added.', array('!name' => $edit['title']))); - $feed = db_fetch_object(db_query("SELECT * FROM {aggregator_feed} WHERE title = '%s' AND url='%s'", $edit['title'], $edit['url'])); + $feed = db_query("SELECT * FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $edit['title'], ':url' => $edit['url']))->fetch(); $this->assertTrue(!empty($feed), t('The feed found in database.')); return $feed; } @@ -65,17 +65,17 @@ class AggregatorTestCase extends DrupalW $this->assertResponse(200, t('rss.xml is reachable.')); // Our tests are based off of rss.xml, so let's find out how many elements should be related. - $feed_count = db_result(db_query_range(db_rewrite_sql('SELECT COUNT(*) FROM {node} n WHERE n.promote = 1 AND n.status = 1'), 0, variable_get('feed_default_items', 10))); + $feed_count = db_select('node')->condition('promote', '1')->condition('status', '1')->countQuery()->range(0, variable_get('feed_default_items', 10))->execute()->fetchField(); $feed_count = $feed_count > 10 ? 10 : $feed_count; // Refresh the feed (simulated link click). $this->drupalGet('admin/content/aggregator/update/' . $feed->fid); // Ensure we have the right number of items. - $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d', $feed->fid); + $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid)); $items = array(); $feed->items = array(); - while ($item = db_fetch_object($result)) { + foreach ($result as $item) { $feed->items[] = $item->iid; } $feed->item_count = count($feed->items); @@ -99,8 +99,8 @@ class AggregatorTestCase extends DrupalW */ function getFeedCategories($feed) { // add the categories to the feed so we can use them - $result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = %d', $feed->fid); - while ($category = db_fetch_object($result)) { + $result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = :fid', array(':fid' => $feed->fid)); + foreach ($result as $category) { $feed->categories[] = $category->cid; } } @@ -113,7 +113,7 @@ class AggregatorTestCase extends DrupalW * @return boolean Feed is unique. */ function uniqueFeed($feed_name, $feed_url) { - $result = db_result(db_query("SELECT count(*) FROM {aggregator_feed} WHERE title = '%s' AND url='%s'", $feed_name, $feed_url)); + $result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $feed_name, ':url' => $feed_url))->fetchField(); return (1 == $result); } @@ -328,7 +328,7 @@ class RemoveFeedTestCase extends Aggrega $this->assertResponse(404, t('Deleted feed source does not exists.')); // Check database for feed. - $result = db_result(db_query("SELECT count(*) FROM {aggregator_feed} WHERE title = '%s' AND url='%s'", $feed->title, $feed->url)); + $result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $feed->title, ':url' => $feed->url))->fetchField(); $this->assertFalse($result, t('Feed not found in database')); } } @@ -373,14 +373,14 @@ class UpdateFeedItemTestCase extends Agg $feed = db_fetch_object(db_query("SELECT * FROM {aggregator_feed} WHERE url = '%s'", $edit['url'])); $this->drupalGet('admin/content/aggregator/update/' . $feed->fid); - $before = db_result(db_query('SELECT timestamp FROM {aggregator_item} WHERE fid = %d', $feed->fid)); + $before = db_query('SELECT timestamp FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(); // Sleep for 3 second. sleep(3); - db_query("UPDATE {aggregator_feed} SET checked = 0, hash = '', etag = '', modified = 0 WHERE fid = %d", $feed->fid); + db_update('aggregator_feed')->condition('fid', $feed->fid)->fields(array('checked' => 0, 'hash' => '', 'etag' => '', 'modified' => 0))->execute(); $this->drupalGet('admin/content/aggregator/update/' . $feed->fid); - $after = db_result(db_query('SELECT timestamp FROM {aggregator_item} WHERE fid = %d', $feed->fid)); + $after = db_query('SELECT timestamp FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(); $this->assertTrue($before === $after, t('Publish timestamp of feed item was not updated (!before === !after)', array('!before' => $before, '!after' => $after))); } @@ -407,7 +407,7 @@ class RemoveFeedItemTestCase extends Agg // Add and remove feed items and ensure that the count is zero. $this->updateFeedItems($feed); $this->removeFeedItems($feed); - $count = db_result(db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = %d', $feed->fid)); + $count = db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchField(); $this->assertTrue($count == 0); // Delete feed. @@ -438,11 +438,11 @@ class CategorizeFeedItemTestCase extends $this->drupalPost('admin/content/aggregator/add/category', $edit, t('Save')); $this->assertRaw(t('The category %title has been added.', array('%title' => $edit['title'])), t('The category %title has been added.', array('%title' => $edit['title']))); - $category = db_fetch_object(db_query("SELECT * FROM {aggregator_category} WHERE title = '%s'", $edit['title'])); + $category = db_query("SELECT * FROM {aggregator_category} WHERE title = :title", array(':title' => $edit['title']))->fetch(); $this->assertTrue(!empty($category), t('The category found in database.')); $link_path = 'aggregator/categories/' . $category->cid; - $menu_link = db_fetch_object(db_query("SELECT * FROM {menu_links} WHERE link_path = '%s'", $link_path)); + $menu_link = db_query("SELECT * FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $link_path))->fetch(); $this->assertTrue(!empty($menu_link), t('The menu link associated with the category found in database.')); // TODO: Need to add categories to the feed on creation. @@ -453,8 +453,11 @@ class CategorizeFeedItemTestCase extends // For each category of a feed, ensure feed items have that category, too. if (!empty($feed->categories) && !empty($feed->items)) { foreach ($feed->categories as $category) { - $items_str = implode(', ', $feed->items); - $categorized_count = db_result(db_query('SELECT COUNT(*) FROM {aggregator_category_item} WHERE iid IN (' . $items_str . ')')); + $count = db_select('aggregator_category_item')->db_or(); + foreach ($feed->items as $iid) { + $count->condition('iid', $iid); + } + $categorized_count = $count->countQuery()->execute()->fetchField(); $this->assertEqual($feed->item_count, $categorized_count, t('Total items in feed equal to the total categorized feed items in database')); } } @@ -482,10 +485,10 @@ class ImportOPMLTestCase extends Aggrega * Open OPML import form. */ function openImportForm() { - db_query('DELETE FROM {aggregator_category}'); + db_delete('aggregator_category')->execute(); $category = $this->randomName(10, self::$prefix); - db_query("INSERT INTO {aggregator_category} (cid, title, description) VALUES (%d, '%s', '%s')", 1, $category, ''); + db_insert('aggregator_category')->fields(array('cid' => 1, 'title' => $category, 'description' => ''))->execute(); $this->drupalGet('admin/content/aggregator/add/opml'); $this->assertText('A single OPML document may contain a collection of many feeds.', t('Looking for help text.')); @@ -499,7 +502,7 @@ class ImportOPMLTestCase extends Aggrega * Submit form filled with invalid fields. */ function validateImportFormFields() { - $before = db_result(db_query('SELECT COUNT(*) FROM {aggregator_feed}')); + $before = db_select('aggregator_feed')->countQuery()->execute()->fetchField(); $form = array(); $this->drupalPost('admin/content/aggregator/add/opml', $form, t('Import')); @@ -517,7 +520,7 @@ class ImportOPMLTestCase extends Aggrega $this->drupalPost('admin/content/aggregator/add/opml', $form, t('Import')); $this->assertText(t('This URL is not valid.'), t('Error if the URL is invalid.')); - $after = db_result(db_query('SELECT COUNT(*) FROM {aggregator_feed}')); + $after = db_select('aggregator_feed')->countQuery()->execute()->fetchField(); $this->assertEqual($before, $after, t('No feeds were added during the three last form submissions.')); } @@ -525,7 +528,7 @@ class ImportOPMLTestCase extends Aggrega * Submit form with invalid, empty and valid OPML files. */ function submitImportForm() { - $before = db_result(db_query('SELECT COUNT(*) FROM {aggregator_feed}')); + $before = db_select('aggregator_feed')->countQuery()->execute()->fetchField(); $form['files[upload]'] = $this->getInvalidOpml(); $this->drupalPost('admin/content/aggregator/add/opml', $form, t('Import')); @@ -535,15 +538,15 @@ class ImportOPMLTestCase extends Aggrega $this->drupalPost('admin/content/aggregator/add/opml', $form, t('Import')); $this->assertText(t('No new feed has been added.'), t('Attempting to load empty OPML from remote URL.')); - $after = db_result(db_query('SELECT COUNT(*) FROM {aggregator_feed}')); + $after = db_select('aggregator_feed')->countQuery()->execute()->fetchField(); $this->assertEqual($before, $after, t('No feeds were added during the two last form submissions.')); - db_query('DELETE FROM {aggregator_feed}'); - db_query('DELETE FROM {aggregator_category}'); - db_query('DELETE FROM {aggregator_category_feed}'); + db_delete('aggregator_feed')->execute(); + db_delete('aggregator_category')->execute(); + db_delete('aggregator_category_feed')->execute(); $category = $this->randomName(10, self::$prefix); - db_query("INSERT INTO {aggregator_category} (cid, title, description) VALUES (%d, '%s', '%s')", 1, $category, ''); + db_insert('aggregator_category')->fields(array('cid' => 1, 'title' => $category, 'description' => ''))->execute(); $feeds[0] = $this->getFeedEditArray(); $feeds[1] = $this->getFeedEditArray(); @@ -557,16 +560,16 @@ class ImportOPMLTestCase extends Aggrega $this->assertRaw(t('A feed with the URL %url already exists.', array('%url' => $feeds[0]['url'])), t('Verifying that a duplicate URL was identified')); $this->assertRaw(t('A feed named %title already exists.', array('%title' => $feeds[1]['title'])), t('Verifying that a duplicate title was identified')); - $after = db_result(db_query('SELECT COUNT(*) FROM {aggregator_feed}')); + $after = db_select('aggregator_feed')->countQuery()->execute()->fetchField(); $this->assertEqual($after, 2, t('Verifying that two distinct feeds were added.')); $feeds_from_db = db_query("SELECT f.title, f.url, f.refresh, cf.cid FROM {aggregator_feed} f LEFT JOIN {aggregator_category_feed} cf ON f.fid = cf.fid"); $refresh = $category = TRUE; - while ($feed = db_fetch_array($feeds_from_db)) { - $title[$feed['url']] = $feed['title']; - $url[$feed['title']] = $feed['url']; - $category = $category && $feed['cid'] == 1; - $refresh = $refresh && $feed['refresh'] == 900; + foreach ($feeds_from_db as $feed) { + $title[$feed->url] = $feed->title; + $url[$feed->title] = $feed->url; + $category = $category && $feed->cid == 1; + $refresh = $refresh && $feed->refresh == 900; } $this->assertEqual($title[$feeds[0]['url']], $feeds[0]['title'], t('First feed was added correctly.'));