/
SolrPlugin.pm
193 lines (144 loc) · 4.82 KB
/
SolrPlugin.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2009-2010 Michael Daum http://michaeldaumconsulting.com
#
# 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 2
# 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.
package Foswiki::Plugins::SolrPlugin;
use strict;
use Foswiki::Func ();
use Foswiki::Plugins ();
our $VERSION = '$Rev: 20091124 (2009-11-24) $';
our $RELEASE = '1.01';
our $SHORTDESCRIPTION = 'Enterprise Search Engine for Foswiki based on [[http://lucene.apache.org/solr/][Solr]]';
our $NO_PREFS_IN_TOPIC = 1;
our $baseWeb;
our $baseTopic;
our $searcher;
our $indexer;
our @knownIndexTopicHandler;
our @knownIndexAttachmentHandler;
sub initPlugin {
($baseTopic, $baseWeb) = @_;
$searcher = undef;
$indexer = undef;
Foswiki::Func::registerTagHandler('SOLRSEARCH', \&SOLRSEARCH);
Foswiki::Func::registerTagHandler('SOLRFORMAT', \&SOLRFORMAT);
Foswiki::Func::registerTagHandler('SOLRSIMILAR', \&SOLRSIMILAR);
Foswiki::Func::registerTagHandler('SOLRSCRIPTURL', \&SOLRSCRIPTURL);
Foswiki::Func::registerRESTHandler('search', \&restSOLRSEARCH);
Foswiki::Func::registerRESTHandler('terms', \&restSOLRTERMS);
Foswiki::Func::registerRESTHandler('similar', \&restSOLRSIMILAR);
Foswiki::Func::registerRESTHandler('autocomplete', \&restSOLRAUTOCOMPLETE);
Foswiki::Func::registerRESTHandler('optimize', \&restOPTIMIZE);
return 1;
}
sub registerIndexTopicHandler {
push @knownIndexTopicHandler, shift;
}
sub registerIndexAttachmentHandler {
push @knownIndexAttachmentHandler, shift;
}
sub getSearcher {
unless ($searcher) {
require Foswiki::Plugins::SolrPlugin::Search;
$searcher = Foswiki::Plugins::SolrPlugin::Search->new(@_);
}
return $searcher;
}
sub getIndexer {
unless ($indexer) {
require Foswiki::Plugins::SolrPlugin::Index;
$indexer = Foswiki::Plugins::SolrPlugin::Index->new(@_);
}
return $indexer;
}
sub SOLRSEARCH {
my ($session, $params, $theTopic, $theWeb) = @_;
return getSearcher($session)->handleSOLRSEARCH($params, $theWeb, $theTopic);
}
sub SOLRFORMAT {
my ($session, $params, $theTopic, $theWeb) = @_;
return getSearcher($session)->handleSOLRFORMAT($params, $theWeb, $theTopic);
}
sub SOLRSIMILAR {
my ($session, $params, $theTopic, $theWeb) = @_;
return getSearcher($session)->handleSOLRSIMILAR($params, $theWeb, $theTopic);
}
sub SOLRSCRIPTURL {
my ($session, $params, $theTopic, $theWeb) = @_;
return getSearcher($session)->handleSOLRSCRIPTURL($params, $theWeb, $theTopic);
}
sub restOPTIMIZE {
my $session = shift;
getIndexer($session)->optimize();
}
sub restSOLRSEARCH {
my $session = shift;
my $web = $session->{webName};
my $topic = $session->{topicName};
return getSearcher($session)->restSOLRSEARCH($web, $topic);
}
sub restSOLRTERMS {
my $session = shift;
my $web = $session->{webName};
my $topic = $session->{topicName};
return getSearcher($session)->restSOLRTERMS($web, $topic);
}
sub restSOLRSIMILAR {
my $session = shift;
my $web = $session->{webName};
my $topic = $session->{topicName};
return getSearcher($session)->restSOLRSIMILAR($web, $topic);
}
sub restSOLRAUTOCOMPLETE {
my $session = shift;
my $web = $session->{webName};
my $topic = $session->{topicName};
return getSearcher($session)->restSOLRAUTOCOMPLETE($web, $topic);
}
sub indexCgi {
my $session = shift;
return getIndexer($session)->index();
}
sub searchCgi {
my $session = shift;
my $request = $session->{cgiQuery} || $session->{request};
my $template = $Foswiki::cfg{SolrPlugin}{SearchTemplate} || 'System.SolrSearchView';
my $result = Foswiki::Func::readTemplate($template);
$result = Foswiki::Func::expandCommonVariables($result);
$result = Foswiki::Func::renderText($result);
$session->writeCompletePage($result, 'view');
}
sub afterSaveHandler {
return unless $Foswiki::cfg{SolrPlugin}{EnableOnSaveUpdates};
my ($text, $topic, $web, $error, $meta) = @_;
getIndexer()->afterSaveHandler($web, $topic, $meta, $text);
}
# Foswiki >= 1.1
sub afterUploadHandler {
return unless $Foswiki::cfg{SolrPlugin}{EnableOnUploadUpdates};
getIndexer()->afterUploadHandler(@_);
}
sub afterRenameHandler {
return unless $Foswiki::cfg{SolrPlugin}{EnableOnRenameUpdates};
getIndexer()->afterRenameHandler(@_);
}
sub finishPlugin {
$indexer->finish() if $indexer;
@knownIndexTopicHandler = ();
if ($searcher) {
my $url = $searcher->{redirectUrl};
if ($url) {
#print STDERR "found redirect $url\n";
Foswiki::Func::redirectCgiQuery(undef, $url);
}
}
}
1;