# Blosxom Plugin: swish # Modified Lucene Search # Modified by: Julian Fong, http://www.levork.org/ # Modified by: Matthew Gregg # by : Rael Dornfest # and Sam Ruby # Version: 0.3 # Documentation: See the bottom of this file or type: perldoc swish package swish; # --- Configurable variables ----- # Turn on the Swish++ engine (set to 1 only once the other bits are in place)? my $swish_on = 1; # What's my index? my $index = '/home/jfong/html/swish++.index'; # Path to search++ my $swish_bin = '/usr/local/bin'; # Number of search results to display per page my $results_per_page = 10; # --- Export variables ----- # Search result summary text ("Results XX-YY of ZZ for query") $search; # Total number of matched entries $search_count = 0; # Used in story template, contains swish rank of search result # (i.e. "Relevance" to the query) $rank; # Navigation aid if applicable ("Result page: Previous Next") $navigation; # Hash of filename to ranks my $allranks; my $ignored = 0; use CGI qw/:standard/; use File::stat; use URI::Escape; use POSIX qw(strftime); use Env qw(@PATH @CLASSPATH); sub start { $swish_on or return 0; $blosxom::static_or_dynamic eq 'dynamic' or return 0; param('q') or return 0; $blosxom::num_entries = $results_per_page; 1; } sub entries { return sub { my(%files, %indexes); # Escape the query my $query = uri_escape(param('q')); # We do want to preserve spaces and parentheses in the query $query =~ s/%20/ /g; $query =~ s/%28/(/g; $query =~ s/%29/)/g; my $qoff = param('qoff') + 0; # Open a pipe from the 'search' command. my $pid = open SEARCH, "-|"; die "Cannot fork $!" unless defined $pid; unless ( $pid ) { exec "$swish_bin/search -i $index '$query'" } while ( ) { # See if the results were ignored if (/^# ignored/) { $ignored = 1; next; } # Ignore all other comments next if /^#/; # Skip over offset if ($qoff > 0) { $qoff--; $search_count++; next; } my( $rank, $file, $size, $title ) = split( / /, $_, 4 ); $file =~ /\.txt$/ and $files{"$file"} = stat("$file")->mtime; $allranks{"$file"} = $rank; $search_count++; } close(SEARCH); return (\%files, \%indexes); }; } sub head { my($pkg, $path, $head_ref) = @_; if ($ignored) { $search = "Ignored " . param('q'); } elsif ($search_count == 0) { $search = "No results found for " . param('q'); } else { my $query = uri_escape(param('q')); my $search_begin = param('qoff') + 1; my $search_end = $search_begin + $blosxom::num_entries - 1; my $next, $previous; if ($search_end >= $search_count) { $search_end = $search_count; } else { $next = "Next"; } if ($search_begin > 1) { my $poff = $search_begin - 1 - $blosxom::num_entries; if ($poff > 0) { $previous = "Previous"; } else { $previous = "Previous"; } } if ($previous ne "" || $next ne "") { $navigation = "Result Page: $previous $next"; } $search = "Results $search_begin - $search_end of $search_count for " . param('q'); } } sub sort { return sub { my ($files_ref) = @_; return sort { $allranks{$b} <=> $allranks{$a} } keys %$files_ref; } } sub story { my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_; my $qualfile = "$blosxom::datadir/$filename.$blosxom::file_extension"; $rank = $allranks{$qualfile}; 1; } 1; __END__ =head1 NAME Blosxom Plug-in: swish =head1 SYNOPSIS Based upon: http://www.blosxom.com/plugins/search/lucene.htm Purpose: Swish++ [http://homepage.mac.com/pauljlucas/software/swish/] is a C++ based text search engine. This plug-in hooks in the results of a Swish search, displaying only the stories matching the search query (as specified by ?q=keywords). The search results are paginated. Populates several variables for use in flavour templates: - $swish::search gets "Results XX-YY of ZZ for keywords" - $swish::navigation contains navigation aids to navigate search pages if applicable (e.g. "Result page: Previous Next") - $swish::search_count contains the number of search results - $swish::rank contains the swish ranking of the current story (available in story templates) Replaces the default $blosxom::entries subroutine. You'd best put it before any other plug-ins that override the default $blosxom::entries subroutine. When ?q=keywords turns on the swish plug-in, it'll then be used instead of whatever other entries overrides you have down the chain. E.g. My setup is as follows: 01swish 02entries_index other plugins follow This plug-in is based on the lucene search template =head1 VERSION 0.1 Initial port of Blosxom Lucene search plugin. 0.2 Added $swish::search_count. Makes the number of search results available to flavour templates. 0.3 Changes by Julian Fong: - Results now sorted by swish rank - Added pagination and navigation features - Preserve spaces and parentheses in query =head1 AUTHOR Rael Dornfest , http://www.raelity.org/ Modified by: Matthew Gregg , http://www.itlab.musc.edu/ Modified by: Julian Fong, http://www.levork.org/ =head1 SEE ALSO Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/ Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml =head1 SWISH Blosxom and this Blosxom Plug-in Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.