#[BOFN]###############################################################################
#
#
#Pagenews - a free script to publish news on websites
#Copyright (C) 2004,2005,2006,2007,2008 Philipp Kindt
#
#This file is part of Pagenews.
#
# 	 This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#[EOFN]###############################################################################


#this package provides, together with the dnfeed_whatever packages,
#functions for the built-in search engine. 

package dnsearch;
require dnfeeds;

#########################################################################################################
#looks up the query $_[0]
#
#$_[1] may specify the first index, $_[2] the number postings to return.
#you get an interval I[$_[1], $_[1] + $_[2]] of postings in return.
#however, it is not very effective as, with every search request, the whole search has to
#be repeated COMPLETELY, that means all results have to be created every single time again.
#only do this with small databases.
   
#$_[3] can specify a special feed to search in. if emty, all feeds are used
#$_[4] may specify a colour for highligting matches' background
#
#returned is a hash reference that contains the postings (@{$rh->{'postings'}};
#and the number of results ($rh->{'nr_of_results'});
sub search{
	if ($_[0] eq ""){
		return "";
	};
	$_[0] =~ s/[^a-zA-Z1-99]//g;
	$_[0] = doncgitools::txt_to_html($_[0]);
	my @feednames;
	my @postings;
 	my $rh = {};
	if($_[3] ne ""){
		push @feednames, $_[3];
	}else{
		@feednames = dnfeeds->get_list_of_feeds();
	};
	#do the searches - hand over to feeds
	foreach my $feedname (@feednames){

		my $feed = dnfeeds->new($feedname);
		push @postings, $feed->search_feed($_[0],$_[4]);
	};  

	if($_[2] eq ""){
		my $nrofpostings = @postings;
		$rh->{'nr_of_results'} = $nrofpostings;
		@{$rh->{'postings'}} = @postings;
		return $rh;

	}else{
		my $nrofpostings = @postings;
		$rh->{'nr_of_results'} = $nrofpostings;
		@{$rh->{'postings'}} = splice @postings, $_[1], $_[2];	
		return $rh;

	};
};

