7 |
use CGI::Carp qw/fatalsToBrowser/; |
use CGI::Carp qw/fatalsToBrowser/; |
8 |
use File::Slurp; |
use File::Slurp; |
9 |
use YAML; |
use YAML; |
10 |
use Search::Estraier; |
use Data::Page; |
11 |
|
use Data::Dump qw/dump/; |
12 |
print header; |
use SWISH::API; |
13 |
|
use JSON; |
14 |
|
use Text::Unaccent::PurePerl qw/unac_string/; |
15 |
|
use HTML::FillInForm::Lite; |
16 |
|
|
17 |
|
my $range_around = 5; |
18 |
|
my @entries_per_page = ( 30, 50, 100, 500 ); |
19 |
|
my $debug = param('debug'); |
20 |
|
|
21 |
|
print header( |
22 |
|
-charset => 'utf-8', |
23 |
|
); |
24 |
|
|
25 |
sub dump_yaml { |
sub dump_yaml { |
26 |
print qq|<pre>|, YAML::Dump( @_ ), qq|</pre>|; |
my $name = shift; |
27 |
|
print qq|<div class=dump><tt>$name</tt><pre>|, YAML::Dump( @_ ), qq|</pre></div>| if $debug; |
28 |
} |
} |
29 |
|
|
30 |
my $path = $ENV{PATH_INFO}; |
sub show_pager { |
31 |
|
my ($pager) = @_; |
32 |
|
|
33 |
|
my @show_pages; |
34 |
|
my $after_current = 0; |
35 |
|
|
36 |
|
if ( $pager->current_page <= $range_around + 2 ) { |
37 |
|
@show_pages = ( $pager->first_page .. $pager->current_page ); |
38 |
|
$after_current = $range_around - $pager->current_page; |
39 |
|
} else { |
40 |
|
@show_pages = ( $pager->first_page, '', $pager->current_page - $range_around .. $pager->current_page ); |
41 |
|
} |
42 |
|
|
43 |
|
if ( $pager->current_page + $after_current + $range_around + 1 >= $pager->last_page ) { |
44 |
|
push @show_pages, ( $pager->current_page + 1 .. $pager->last_page ); |
45 |
|
} else { |
46 |
|
push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $range_around, '', $pager->last_page ); |
47 |
|
} |
48 |
|
|
49 |
|
# dump_yaml( 'show_pages', \@show_pages ); |
50 |
|
|
51 |
|
return '' unless $#show_pages; |
52 |
|
|
53 |
|
my ( $prev, $next ) = ( '<<', '>>' ); |
54 |
|
|
55 |
|
sub li_a_href { |
56 |
|
my ( $page, $label, $attr ) = @_; |
57 |
|
param( 'current_page', $page ); |
58 |
|
my $url = self_url( -query => 1 ); |
59 |
|
$attr ||= ''; |
60 |
|
$label ||= $page; |
61 |
|
qq|<li$attr><a href="$url" title="$page">$label</a></li>|; |
62 |
|
} |
63 |
|
|
64 |
|
return |
65 |
|
$pager->previous_page ? li_a_href( $pager->previous_page, $prev ) : qq|<li class=skip>$prev</li>| |
66 |
|
, ( map { |
67 |
|
if ( $_ eq $pager->current_page ) { |
68 |
|
qq|<li class=current_page>$_</li>|; |
69 |
|
} elsif ( $_ eq '' ) { |
70 |
|
qq|<li class=skip>...</li>|; |
71 |
|
} else { |
72 |
|
li_a_href( $_ ); |
73 |
|
} |
74 |
|
} @show_pages ) |
75 |
|
, $pager->next_page ? li_a_href( $pager->next_page, $next ) : qq|<li class=skip>$next</li>| |
76 |
|
; |
77 |
|
|
78 |
|
} |
79 |
|
|
80 |
|
my $path = $ENV{PATH_INFO} || 'ecas'; |
81 |
|
$path =~ s{^/+}{}; |
82 |
|
$path =~ s{/+$}{}; |
83 |
my $dir = $0; |
my $dir = $0; |
84 |
$dir =~ s{/[^/]+.cgi}{}; |
$dir =~ s{/[^/]+.cgi}{}; |
85 |
|
|
86 |
|
dump_yaml( 'dir', $dir ); |
87 |
|
|
88 |
my $config = YAML::LoadFile( "$dir/$path/config.yml" ); |
my $config = YAML::LoadFile( "$dir/$path/config.yml" ); |
89 |
|
|
90 |
my $database = (keys %{ $config->{databases} })[0]; |
my $database = (keys %{ $config->{databases} })[0]; |
91 |
die "$database not in $path" unless $path =~ m{\Q$database\E}; |
die "$database not in $path" unless $path =~ m{\Q$database\E}; |
92 |
|
|
93 |
my $estraier = YAML::LoadFile( "$dir/../var/estraier/$database.yaml" ); |
my $html_markup = "$dir/$path/html.pm"; |
94 |
|
my $html_markup_skip; |
95 |
|
if ( -e $html_markup ) { |
96 |
|
require $html_markup; |
97 |
|
$html_markup = $database . '::html'; |
98 |
|
} else { |
99 |
|
undef $html_markup; |
100 |
|
} |
101 |
|
|
102 |
|
my $stats; |
103 |
|
{ |
104 |
|
my $path = "$dir/../var/swish/$database.yaml"; |
105 |
|
$stats = YAML::LoadFile( $path ); |
106 |
|
dump_yaml( "stats $path", $stats ); |
107 |
|
} |
108 |
|
|
109 |
my $db = $config->{databases}->{$database}; |
my $db = $config->{databases}->{$database}; |
110 |
|
|
111 |
my @attr = keys %{ $estraier->{attr} }; # FIXME replace with real gnerated lookup |
sub read_config_txt { |
112 |
|
my ( $file ) = @_; |
113 |
|
my $input; |
114 |
|
my $path ="$dir/$path/$path-$file.txt"; |
115 |
|
if ( ! -e $path ) { |
116 |
|
warn "missing $path"; |
117 |
|
return; |
118 |
|
} |
119 |
|
foreach ( split(/[\n\r]+/, read_file( $path ) ) ) { |
120 |
|
my ( $val,$label ) = split(/\s*\t\s*/,$_,2); |
121 |
|
push @{ $input->{ '-values' } }, $val; |
122 |
|
$input->{ '-labels' }->{$val} = $label; |
123 |
|
} |
124 |
|
return $input; |
125 |
|
} |
126 |
|
|
127 |
|
my $attr_labels = read_config_txt 'labels'; |
128 |
|
my $attr_operators = read_config_txt 'operators'; |
129 |
|
|
130 |
|
my @attr = @{ $attr_labels->{'-values'} } if $attr_labels; |
131 |
|
@attr = keys %{ $stats->{attr} } unless @attr; |
132 |
|
|
133 |
|
|
134 |
|
warn dump( $attr_labels, $attr_operators ); |
135 |
|
|
136 |
|
my $only_input; |
137 |
|
my $inputs_available = 0; |
138 |
|
|
139 |
|
foreach ( @{ $db->{input} } ) { |
140 |
|
my $input = $_->{name} || die "no name in ",dump( $_ ); |
141 |
|
next unless defined $stats->{input}->{$input}; # skip inputs without data |
142 |
|
if ( ! $only_input->{'-labels'}->{$input} ) { |
143 |
|
push @{ $only_input->{'-values'} }, $input; |
144 |
|
$only_input->{'-labels'}->{$input} = $_->{description} || $input; |
145 |
|
$inputs_available++; |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
|
warn "## only_input = ", dump( $only_input ); |
150 |
|
|
151 |
|
my @style = ( '../../style.css' ); |
152 |
|
push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css"; |
153 |
|
dump_yaml( 'style', \@style ); |
154 |
|
|
155 |
|
sub search_form { |
156 |
|
|
157 |
|
my $form_html = "$dir/$path/$path-search.html"; |
158 |
|
if ( -e $form_html ) { |
159 |
|
my $html = read_file( $form_html ); |
160 |
|
my $q = CGI->new(); |
161 |
|
my $h = HTML::FillInForm::Lite->new(); |
162 |
|
return $h->fill(\$html, $q); |
163 |
|
} |
164 |
|
|
165 |
|
qq|<a name="form"></a>|, |
166 |
|
start_form( -action => self_url( query => 0 ) ), |
167 |
|
checkbox_group( |
168 |
|
-name => 'attr', |
169 |
|
%$attr_labels, |
170 |
|
# -linebreak => 0, |
171 |
|
), |
172 |
|
textfield( -name => 'search' ), |
173 |
|
$attr_operators ? popup_menu( -name => 'attr_operator', %$attr_operators ) : '', |
174 |
|
submit( -value => 'Search' ), |
175 |
|
# hidden( -name => 'entries_per_page', -default => $entries_per_page ), |
176 |
|
popup_menu( -name => 'entries_per_page', -values => [ @entries_per_page ], -title => 'entries per page' ), |
177 |
|
# we need current_page fixed at 1 so that every submit through form will reset it |
178 |
|
qq|<input type=hidden name=current_page value=1 >|, |
179 |
|
checkbox( -name => 'debug', -default => 0 ), # FIXME hidden? |
180 |
|
qq|<div id=inputs>|, |
181 |
|
$inputs_available > 1 ? |
182 |
|
h2( 'Select input' ) . |
183 |
|
checkbox_group( |
184 |
|
-name => 'only_input', |
185 |
|
%$only_input, |
186 |
|
-linebreak=> 'true', |
187 |
|
) : '', |
188 |
|
qq|</div>|, |
189 |
|
end_form, |
190 |
|
; |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
print |
print |
195 |
start_html( |
start_html( |
196 |
-title => $db->{name}, |
-title => $db->{name}, |
197 |
-style => '../../style.css', |
-style => [ @style ], |
198 |
), |
), |
199 |
h1( $db->{name} ), |
h1( $db->{name} ), |
200 |
qq|<div id=description>|, $db->{description}, qq|</div>|, |
qq|<div id=description>|, $db->{description}, qq|</div>|, |
|
start_form, |
|
|
radio_group( |
|
|
-name => 'attr', |
|
|
-values => [ @attr ], |
|
|
# -linebreak => 0, |
|
|
), |
|
|
textfield( -name => 'search' ), |
|
|
submit |
|
201 |
; |
; |
202 |
|
|
|
print end_form; |
|
|
|
|
203 |
if ( my $search = param('search') ) { |
if ( my $search = param('search') ) { |
204 |
|
|
205 |
print qq|<div id="results">search: $search|; |
$search = unac_string( $search ); |
206 |
|
|
207 |
|
print qq| |
208 |
|
<a href="#form" class="skip" title="skip to search form">#</a> |
209 |
|
<div id="results"> |
210 |
|
|; |
211 |
|
|
212 |
|
my $swish = SWISH::API->new( "$dir/../var/swish/$database" ); |
213 |
|
$swish->abort_last_error if $swish->Error; |
214 |
|
|
215 |
|
my @search = (); |
216 |
|
my @attrs = param('attr'); |
217 |
|
my $op = param('attr_operator'); |
218 |
|
|
219 |
|
if ( $search =~ m{(=|"|AND|OR)} ) { |
220 |
|
push @search, $search; |
221 |
|
} elsif ( @attrs ) { |
222 |
|
|
223 |
|
$op ||= 'Q*'; |
224 |
|
my @or; |
225 |
|
foreach my $attr ( @attrs ) { |
226 |
|
my $v = $search; |
227 |
|
$v =~ s/^\s+//; |
228 |
|
warn "-- v: $v\n"; |
229 |
|
sub rewrite { |
230 |
|
my ( $attr, $whitespace, $v ) = @_; |
231 |
|
warn "## filter $op $whitespace $v\n"; |
232 |
|
my $template = $op; |
233 |
|
$template =~ s{Q}{$v}; |
234 |
|
$whitespace = " AND " if $whitespace; |
235 |
|
|
236 |
|
return |
237 |
|
$whitespace . |
238 |
|
$attr . '="' . $template . '"'; |
239 |
|
; |
240 |
|
}; |
241 |
|
if ( $op =~ m{\s} ) { |
242 |
|
my $template = $op; |
243 |
|
$template =~ s{Q}{$v}; |
244 |
|
$v = $attr . '="' . $template . '"'; |
245 |
|
} else { |
246 |
|
$v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge; |
247 |
|
} |
248 |
|
|
249 |
|
push @or, $v; |
250 |
|
|
251 |
|
} |
252 |
|
push @search, '(' . join(') OR (', @or) . ')'; |
253 |
|
|
254 |
|
} else { |
255 |
|
push @search, "all=\"$search\""; |
256 |
|
} |
257 |
|
|
258 |
|
my $q = '(' . join(') AND (', @search) . ')'; |
259 |
|
|
260 |
|
my @only_input = param('only_input'); |
261 |
|
$q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input; |
262 |
|
|
263 |
|
warn "# query: $q\n"; |
264 |
|
my $swish_results = $swish->query( $q ); |
265 |
|
|
266 |
|
dump_yaml( 'swish_results', $swish_results ); |
267 |
|
|
268 |
my $node = Search::Estraier::Node->new( |
my $pager = Data::Page->new; |
269 |
url => $config->{hyperestraier}->{masterurl} . '/node/' . $database, |
$pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ ); |
270 |
croak_on_error => 1, |
$pager->total_entries( $swish_results->hits ); |
271 |
); |
|
272 |
|
dump_yaml( 'pager', $pager ); |
273 |
my $cond = Search::Estraier::Condition->new; |
|
274 |
$cond->set_phrase( $search ); |
$swish_results->seek_result( $pager->first - 1 ); |
275 |
my $nres = $node->search( $cond, 0 ); |
|
276 |
|
if ( ! $pager->total_entries ) { |
277 |
if ( ! $nres ) { |
my $no_results = 'No results for search <b>%s</b>'; |
278 |
my $no_results = "No results for search '%s'"; |
$no_results = $swish->error_string . '<br><b>%s</b>' if $swish->error; |
279 |
printf qq|<div class="error">$no_results</a>|, $search; |
printf qq|<div class="error">$no_results</div>\n\n|, $q; |
280 |
} else { |
} else { |
281 |
print qq|<ul>|; |
|
282 |
foreach my $i ( 1 .. $nres->doc_num ) { |
my $results = "<b>%d</b> results for search <b>%s</b> showing results %d - %d"; |
283 |
my $rdoc = $nres->get_doc( $i - 1 ); |
printf qq|<div class="message">$results</div>\n\n|, $pager->total_entries, $q, $pager->first, $pager->last; |
284 |
print qq|<li>|; |
|
285 |
print qq|<div><label>$_</label><span class=$_>|, $rdoc->attr( $_ ), qq|</span></div>| |
my $pager_html = join("\n", show_pager( $pager )); |
286 |
foreach @attr; |
|
287 |
|
print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html; |
288 |
|
|
289 |
|
my $nr = $pager->first; |
290 |
|
print qq|<ol start=$nr>\n|; |
291 |
|
|
292 |
|
my $limit = $pager->entries_on_this_page; |
293 |
|
|
294 |
|
my $nr = 1; |
295 |
|
|
296 |
|
while ( my $result = $swish_results->next_result ) { |
297 |
|
|
298 |
|
my $data = $result->property('data'); |
299 |
|
dump_yaml( 'data', $data ); |
300 |
|
# FIXME if we produce valid json we shouldn't need eval here! |
301 |
|
eval { $data = from_json( $data, {utf8 => 1} ); }; |
302 |
|
if ( $@ ) { |
303 |
|
warn "ERROR: $@ from ",dump( $data ); |
304 |
|
next; |
305 |
|
} |
306 |
|
|
307 |
|
my $li_class = ''; |
308 |
|
$li_class = qq| class="z"| if $nr % 2 == 0; |
309 |
|
print qq|<li$li_class>|; |
310 |
|
foreach my $attr ( @attr ) { |
311 |
|
next unless defined $data->{$attr}; |
312 |
|
my $v = $data->{$attr}; |
313 |
|
if ( $html_markup && ! $html_markup_skip->{$attr} ) { |
314 |
|
eval "\$v = $html_markup->$attr( \$v, \$data );"; |
315 |
|
if ( $@ ) { |
316 |
|
warn "disable html markup for $attr: $@"; |
317 |
|
$html_markup_skip->{$attr} = $@; |
318 |
|
} |
319 |
|
} else { |
320 |
|
$v =~ s{(http://\S+)}{<a href="$1">$1</a>}; |
321 |
|
} |
322 |
|
my $label = $attr_labels->{'-labels'}->{$attr} || $attr; |
323 |
|
print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|; |
324 |
|
} |
325 |
print qq|</li>\n|; |
print qq|</li>\n|; |
326 |
|
|
327 |
|
last if $nr++ == $pager->last; |
328 |
} |
} |
329 |
print qq|</ul>|; |
print qq|</ol>\n\n|; |
330 |
|
|
331 |
|
print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html; |
332 |
} |
} |
333 |
print qq|</div>|; |
print qq|</div>|; |
334 |
|
|
335 |
|
dump_yaml( 'pager', $pager ); |
336 |
|
|
337 |
} |
} |
338 |
|
|
339 |
dump_yaml( $estraier ); |
print search_form; |
340 |
dump_yaml( $db ); |
|
341 |
|
dump_yaml( "config databases $database", $db ); |
342 |
|
dump_yaml( 'html_markup_skip', $html_markup_skip ); |
343 |
|
|
344 |
print end_html; |
print end_html; |