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