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 = "&nbsp;|&nbsp;<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

Steven’s picture

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.

kmartino’s picture

Hi Steven,
that's good to know. Can you help me secure it in Drupal fashion?

kmartino’s picture

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.

Woy’s picture

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

kmartino’s picture

Steven and community, can you review?

/*
 * 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);
             
            $params = array();             
			foreach ($words as $word) {
				$word = trim($word);
				$title_filter[] = "lower(i.title) LIKE '%%%s%%'";
                $params[] = $word;
				$feed_filter[] = "lower(f.title) LIKE '%%%s%%'";
                $params[] = $word;                
				$content_filter[] = "lower(i.description) LIKE '%%%s%%'";
                $params[] = $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",$params, 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 = "&nbsp;|&nbsp;<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;
	}
webengr’s picture

maybe a syntax error in that last posting, the code first submitted with vulnerability did not cause php errors, but the redo dit.

Anoop Tech’s picture

I need a solution for this issue for Drupal 7

Bob Balfe’s picture

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.

<?php
function aggregator_search_info(){
  return array(
    'title' => 'Aggregator', 
    'path' => 'aggregator',
  );
	
}
function aggregator_search_access() {
  return user_access('access aggregator');
}

function aggregator_search_execute($keys = NULL, $conditions = NULL) {
	
            $find = array();   

            $keys = addslashes ( $keys );
            $keys = preg_replace('!\*+,!', ' ', $keys);
            $word = strtolower ( trim($keys) );
            
            $words = explode(" ", $keys);
            
            foreach ($words as $word) {
                $word = trim($word);
                $filter[] = " lower(aggregator_item.title) LIKE '%" . $word . "%'";
                $filter[]= " lower(aggregator_item.description) LIKE '%" . $word . "%'";
            }
			
            $filter_query = implode(" OR ", $filter);
                                
            $q = "SELECT *"
                 ." FROM aggregator_item"
                 ." WHERE (".$filter_query.") ORDER BY timestamp DESC";           
            
            $result = db_query($q);
       
            foreach ($result as $posting) {   
            	
                $feed_title = $posting->title;
                $feed_link = $posting->link;
                $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);
 
                $find[] = array(
                    'title' => $post_title,
                    'link' => url("$post_link"),
                    'user' => $post_desc);
            	                
            }
            
            return $find;
}
?>
jvdurme’s picture

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!

kaytan’s picture

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?