CGI-Application-Search
view release on metacpan or search on metacpan
lib/CGI/Application/Search.pm view on Meta::CPAN
return $self->show_search();
}
$self->param('elapsed_time' => format_number(Time::HiRes::time - $start_time, 3, 1));
# create my pager and then go to the start page
$self->_get_paging_vars($results);
my @words = $self->_get_search_terms($swish, $search, $results, $keywords);
$self->param(
'hits' => $self->_process_results($swish, $search, $results, $search_query));
} else {
return $self->show_search();
}
}
# if there are any extra properties used in the search, make them available to
# the templates with the value in the query object
my @extra_props;
push(@extra_props, @{$self->param('EXTRA_PROPERTIES')}) if $self->param('EXTRA_PROPERTIES');
push(@extra_props, @{$self->param('EXTRA_RANGE_PROPERTIES')})
if $self->param('EXTRA_RANGE_PROPERTIES');
foreach my $prop (@extra_props) {
$self->param($prop => $q->param($prop));
}
$self->param('keywords' => $keywords);
return $self->show_search();
}
=head2 highlight_remote_page
This run mode will fetch a remote page (with either a relative, or
absolute URL using the C<url> Query param) and highlight the keywords
used in the search on that page (the C<keywords> Query param) using the
B<HIGHLIGHT_TAG>, B<HIGHLIGHT_CLASS> or B<HIGHLIGHT_COLORS> options. This
run mode is best used in the links of the search results listing.
<a href="?rm=highlight_remote_page;url=http%3A%2F%2Fexample.com%2Fabout_us%2Findex.html;keywords=Us">about us</a>
=cut
sub highlight_remote_page {
my $self = shift;
my $q = $self->query();
my $url = $q->param('url');
# if it's relative, get the hostname and make it absolute
if ($url !~ /^https?:\/\//) {
$url = $q->url(-base => 1) . $url;
}
return $self->_hilight_page($url);
}
sub _hilight_page {
my ($self, $page) = @_;
my $content;
# Search::Tools::HiLiter doesn't like blank searches so handle those on our own
if( -e $page ) {
$content = decode_utf8(read_file($page));
} else {
require HTTP::Request;
require LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $request = HTTP::Request->new( GET => $page);
my $response = $ua->request($request);
if ( $response->is_error ) {
warn "Error: Couldn't get '$page': response code " . $response->code . "\n";
return;
}
$content = $response->content;
}
my $search_query = $self->query->param('keywords');
if( $search_query && $search_query ne $BLANK_SEARCH ) {
eval { require Search::Tools::HiLiter };
if ($@) {
warn "Could not load Search::Tools::HiLiter so no hilighting will be done: $@";
} else {
$content = Search::Tools::HiLiter->new(
tag => $self->param('HIGHLIGHT_TAG'),
class => $self->param('HIGHLIGHT_CLASS'),
colors => $self->param('HIGHLIGHT_COLORS'),
query => $search_query,
)->hilite($content);
}
}
return $content;
}
=head2 highlight_local_page
This run mode will fetch a local page (only allowing relative files based
in the B<DOCUMENT_ROOT> config var and the path using the C<path> Query
param) and highlight the keywords used in the search on that page (the
C<keywords> Query param) using the B<HIGHLIGHT_TAG>, B<HIGHLIGHT_CLASS>
or B<HIGHLIGHT_COLORS> options. This run mode is best used in the links
of the search results listing.
<a href="?rm=highlight_local_page;path=%2Fabout_us%2Findex.html;keywords=Us">about us</a>
=cut
sub highlight_local_page {
my $self = shift;
my $q = $self->query();
my $doc_root = $self->param('DOCUMENT_ROOT');
my $path = $q->param('path');
if (!$doc_root) {
croak "You must define your DOCUMENT_ROOT to use this run mode!";
}
# make sure $path doesn't have any '/..' tricks in it
$path =~ s/\/\.\.//g;
my $file = catfile($doc_root, $path);
return $self->_hilight_page($file);
}
=head2 suggestions
This run mode will return an AJAX listing of words that should be
suggested to the user for the words that they have typed so far. It
( run in 0.787 second using v1.01-cache-2.11-cpan-acf6aa7dc9e )