Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By kmartino on
In answer to a thread in general discussion, I shared code that enables search for the aggregator, but it's flawed in numerous ways:
With a version of 4.6 it was possible to add search to the aggregator by implementing a search hook in aggregator.module:
/*
* Implementation of hook_search().
*/
function aggregator_search($op = 'search', $keys = null) {
switch ($op) {
case 'name':
return t('Philadelphia Blogs');
case 'search':
$find = array();
// Replace wildcards with MySQL/PostgreSQL wildcards.
$keys = preg_replace('!\*+!', '%', $keys);
$words = explode(",", $keys);
foreach ($words as $word) {
$word = trim($word);
$title_filter[] = "lower(i.title) LIKE '%" . $word . "%'";
$feed_filter[] = "lower(f.title) LIKE '%" . $word . "%'";
$content_filter[] = "lower(i.description) LIKE '%" . $word . "%'";
}
$filter_query = implode(" OR ", $feed_filter) . ' OR ' .implode(" OR ", $title_filter) . ' OR ' . implode(" OR ", $content_filter);
$result = db_query_range("
SELECT i.*, f.link AS feed_link, f.title AS feed_title, f.url AS feed_rss
FROM {aggregator_item} i
LEFT JOIN {aggregator_feed} f
ON i.fid = f.fid
WHERE (". $filter_query . ")
ORDER BY timestamp DESC", 0, 200);
while ($posting = db_fetch_object($result)) {
$feed_title = $posting->feed_title;
$feed_link = $posting->feed_link;
$feed_rss = $posting->feed_rss;
$post_title = $posting->title;
$post_link = $posting->link;
$post_desc = strip_tags($posting->description);
if (strlen($post_desc) > 300) {
$post_desc = substr($post_desc, 0, 300) . "<em>...<a href=\"$post_link\">more</a></em>";
}
$post_id = $posting->iid;
$post_date = date('m/d/y h:m', $posting->timestamp);
$blogIt = "";
if (user_access('edit own blog')) {
$blogIt = " | <a href=\"/node/add/blog?iid=".$post_id."\">blog it</a>";
}
$find[] = array('title' => $feed_title.": ".$post_title,
'link' => url("$post_link"),
'user' => "$post_desc<div class=\"links\">$post_date <a href=\"$feed_link\">$feed_title</a>$blogIt</div>");
}
return $find;
}
}
There are a number of number of disadvantages to such an approach. No paging. Each search is a live query against the database. It forces you to patch your aggregator.module. It is possibly not compatible with 4.7.
If someone has a set of suggestions to overcome those disadvantages, it would be great to help us.
Comments
Insecure code
The code you've posted contains SQL injection vulnerabilities and should not be used on a production site.
--
If you have a problem, please search before posting a question.
can you help me secure it
Hi Steven,
that's good to know. Can you help me secure it in Drupal fashion?
turn this into a patch?
It would be great, once refined, to contribute this as a patch. It's a starting point and works, but as Steven said, is not safe and as I mentioned, is not ideal.
Appeal for assistance
Hi Steven,
Your warning is appreciated, and per your warning this code will not be used by us (Pittsburgh Bloggers).
I've been a loyal supporter of Drupal since the 2.x days, being a user for many years. In 2004 we launched Pittsburgh Bloggers, which has become a valuable resource for the Pittsburgh blogging community and beyond. One of the features that has been lacking (that made us move to aggregator2, which now we are moving off of since it has been abandoned and not user friendly for us) is the ability for the native search functionality to search aggregated posts in Drupal. Having this feature would allow our users to more easily find blogs that they would be interested in.
I'm not a developer, nor do we have easy access to other developers / developer time. I'm hoping that someone will be able to assist us with this. It would be greatly appreciated by the blogging communities of Pittsburgh and Philadelphia.
-Mike
a possibly safer version of aggregator search
Steven and community, can you review?
maybe a syntax error in that
maybe a syntax error in that last posting, the code first submitted with vulnerability did not cause php errors, but the redo dit.
Need a solution for adding Search to the Aggregator for Drupal 7
I need a solution for this issue for Drupal 7
I did this for a quick implementation in Drupal 7
Taking the version 6 code in this thread, I added this to aggregator.module, after this, you can select "aggregator" as the search type int he Drupal 7 admin. I did not link up the items and feeds views to search a feed title so it just searches items.
Thanks Bob! Works fine here
Thanks Bob! Works fine here in D7.
It is however necessary to also set Aggregator as default (next to active) in the admin settings page for Search to give aggregator search results.
Wakken!
Access denied
Ok, thank you so much.It works pretty fine but there's sill a problem. none-authenticated users (Anonymous users) get a "Access Denied" error when they try to use it.
Here is my settings:
1 - Aggregator search is set as default search module in config section.
2 - Anonymous users are permitted to use both of simple and advanced search options.
Briefly , authenticated users can use it with no problem but it doesn't work for Anonymous users.
Any idea?