Add a bunch of Rael plugins.
[matthijs/upstream/blosxom-plugins.git] / general / lucene
1 # Blosxom Plugin: lucene
2 # Author(s): Rael Dornfest <rael@oreilly.com> 
3 #            and Sam Ruby <rubys@intertwingly.net>
4 # Version: 2.0b2
5 # Documentation: See the bottom of this file or type: perldoc lucene
6
7 package lucene;
8
9 # --- Configurable variables -----
10
11 # Turn on the Lucene engine (set to 1 only once the other bits are in place)?
12 my $lucene_on = 0;
13
14 # Where's Java?
15 my $JAVA_HOME = '/path/to/j2sdk1.3.1/';
16
17 # Where's lucene?
18 my $lucene = '/path/to/lib/java/*.jar';
19
20 # What's my index?
21 my $index = '/Library/WebServer/Data/lucene';
22
23 # --------------------------------
24
25 $search;
26
27 use CGI qw/:standard/;
28 use File::stat;
29 use URI::Escape;
30 use POSIX qw(strftime);
31 use Env qw(@PATH @CLASSPATH);
32
33 unshift @PATH, "$JAVA_HOME/bin";
34 push @CLASSPATH, "$JAVA_HOME/lib/tools.jar";
35 push @CLASSPATH, "$JAVA_HOME/jre/lib/rt.jar";
36 push @CLASSPATH, "/any/other/appropriate/java/lib";
37 push @CLASSPATH, glob($lucene);
38
39 sub start {
40   $lucene_on or return 0;
41   $blosxom::static_or_dynamic eq 'dynamic' or return 0;
42   param('q') or return 0;
43   $search = "Search results for: " . param('q');
44   1;
45 }
46
47 sub entries {
48   return sub {
49     my(%files, %indexes);
50
51     my $query = uri_escape(param('q'));
52     $query =~ s/;//g;
53   
54     # Take a gander at what Lucene came up with based upon the search criteria
55     foreach (`$JAVA_HOME/bin/java -cp $ENV{CLASSPATH} LuceneSearch $index $query`) {
56       chomp;
57       s!\./!!;
58       $_ =~ /\.txt$/ and $files{"$blosxom::datadir/$_"} = stat("$blosxom::datadir/$_")->mtime;
59     }       
60   
61     return (\%files, \%indexes);
62   };
63 }
64
65 1;
66
67 __END__
68
69 =head1 NAME
70
71 Blosxom Plug-in: lucene
72
73 =head1 SYNOPSIS
74
75 Based upon: http://radio.weblogs.com/0101679/stories/2002/08/13/luceneSearchFromBlosxom.html
76
77 Purpose: Lucene [http://jakarta.apache.org/lucene/] is a fabulous
78 text search engine written in Java.  This plug-in hooks in the results
79 of a Lucene search, displaying only the stories matching the search query
80 (as specified by ?q=keywords).
81
82 Populates $lucene::search with "Search results for: keywords" for use in
83 flavour templates.
84
85 Replaces the default $blosxom::entries subroutine.  You'd best put it before
86 any other plug-ins that override the default $blosxom::entries subroutine.  
87 When ?q=keywords turns on the lucene plug-in, it'll then be used instead of
88 whatever other entries overrides you have down the chain.  
89
90 E.g. My setup is as follows:
91
92 01lucene
93
94 02entries_index
95
96 other
97
98 plugins
99
100 follow
101
102 This plug-in could be used as a template for hooking into just about any
103 files-based text search engine.
104
105 =head1 VERSION
106
107 2.0b1
108
109 Version number coincides with the version of Blosxom with which the 
110 current version was first bundled.
111
112 =head1 AUTHOR
113
114 Rael Dornfest  <rael@oreilly.com>, http://www.raelity.org/
115
116 =head1 SEE ALSO
117
118 Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/
119
120 Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml
121
122 =head1 BUGS
123
124 Address bug reports and comments to the Blosxom mailing list 
125 [http://www.yahoogroups.com/group/blosxom].
126
127 =head1 LICENSE
128
129 Blosxom and this Blosxom Plug-in
130 Copyright 2003, Rael Dornfest 
131
132 Permission is hereby granted, free of charge, to any person obtaining a
133 copy of this software and associated documentation files (the "Software"),
134 to deal in the Software without restriction, including without limitation
135 the rights to use, copy, modify, merge, publish, distribute, sublicense,
136 and/or sell copies of the Software, and to permit persons to whom the
137 Software is furnished to do so, subject to the following conditions:
138
139 The above copyright notice and this permission notice shall be included
140 in all copies or substantial portions of the Software.
141
142 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
143 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
144 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
145 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
146 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
147 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
148 OTHER DEALINGS IN THE SOFTWARE.