CGI-Application-Search

 view release on metacpan or  search on metacpan

lib/CGI/Application/Search.pm  view on Meta::CPAN


Please see the swish-e documentation on the exact syntax for the query.

=cut 

sub generate_search_query {
    my $self     = shift;
    my $keywords = shift;
    my $q        = $self->query;

    # create a new swish-e search object
    my $search = $keywords || '';
    $search =~ s/=/\=/g;    # escape '=' just in case

    # add any EXTRA_PROPERTIES to the search
    if ($self->param('EXTRA_PROPERTIES')) {
        foreach my $prop (@{$self->param('EXTRA_PROPERTIES')}) {
            my $value = $q->param($prop);
            $search .= ' and ' if $search;
            $search .= "$prop=($value)" if defined $value && length $value;
        }
    }

    return $search || $BLANK_SEARCH;
}

=head2 suggested_words($word)

This object method is used by the B<AUTO_SUGGEST> flag to return the words
that should be suggested to the user after they have typed a C<$word>.
It returns an array reference of those words.

By default it will just look for words in the B<AUTO_SUGGEST_FILE>
that begin with C<$word>. If you need more performance or flexibility
(eg, storing your words in a database and querying for them) you are
encouraged to override this method.

=cut

sub suggested_words {
    my ($self, $phrase) = @_;

    # just get the last word in this phrase
    my @phrase_words = split(/\s+/, $phrase);
    my $word = pop(@phrase_words);

    my $want_to_cache = $self->param('AUTO_SUGGEST_CACHE');
    my $file          = $self->param('AUTO_SUGGEST_FILE');
    my @suggestions;

    if (!$file) {
        warn "AUTO_SUGGEST_FILE was not specified!";
        return [];
    } elsif (!-r $file) {
        warn "AUTO_SUGGEST_FILE $file is not readable!";
        return [];
    }

    # if we are going to use the cache (meaning we want to use
    # it and there's up-to-date data in there)
    my $file_mod_time = (stat($file))[9];
    if (    $want_to_cache
        and @SUGGEST_CACHE
        and $SUGGEST_CACHE_TIME >= $file_mod_time)
    {
        foreach my $cached (@SUGGEST_CACHE) {

            # if it starts with this $word
            if (index($cached, lc $word) == 0) {
                push(@suggestions, $cached);

                # else if this is the first mis-match
            } elsif (@suggestions) {
                last;
            }

            # if we have a limit and we've reached it
            # don't do any more
            if (    $self->param('AUTO_SUGGEST_LIMIT')
                and @suggestions >= $self->param('AUTO_SUGGEST_LIMIT'))
            {
                last;
            }
        }

        # else we don't have anything cached, so just load from the file
    } else {

        # reset it if we want to cache
        if ($want_to_cache) {
            @SUGGEST_CACHE      = ();
            $SUGGEST_CACHE_TIME = time();
        }

        # read each line from the AUTO_SUGGEST_FILE
        my $IN;
        open($IN, '<', $file)
          or die "Could not open $file for reading! $!";

        # now look at each line
      LINE: while (my $line = <$IN>) {

            # if we want to cache the words in this file
            if ($want_to_cache) {
                chomp($line);
                push(@SUGGEST_CACHE, $line);
            }

            # if it starts with this $word
            if (index($line, lc $word) == 0) {
                chomp($line) unless ($want_to_cache);
                push(@suggestions, $line);

                # else if we aren't caching, and this is the first mis-match
                # then we want to finish and close the file
            } elsif (@suggestions && !$want_to_cache) {
                last LINE;
            }

            # if we have a limit and we've reached it
            # don't do any more



( run in 0.727 second using v1.01-cache-2.11-cpan-39bf76dae61 )