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'); |
my $debug = param('debug'); |
21 |
|
|
22 |
print header( |
print header( |
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>| if $debug; |
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 { |
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 |
print |
my $attr_labels = read_config_txt 'labels'; |
129 |
start_html( |
my $attr_operators = read_config_txt 'operators'; |
130 |
-title => $db->{name}, |
|
131 |
-style => '../../style.css', |
my @attr = @{ $attr_labels->{'-values'} } if $attr_labels; |
132 |
), |
@attr = keys %{ $stats->{attr} } unless @attr; |
133 |
h1( $db->{name} ), |
|
134 |
qq|<div id=description>|, $db->{description}, qq|</div>|, |
|
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 ) ), |
start_form( -action => self_url( query => 0 ) ), |
168 |
radio_group( |
checkbox_group( |
169 |
-name => 'attr', |
-name => 'attr', |
170 |
-values => [ @attr ], |
%$attr_labels, |
171 |
# -linebreak => 0, |
# -linebreak => 0, |
172 |
), |
), |
173 |
textfield( -name => 'search' ), |
textfield( -name => 'search' ), |
174 |
popup_menu( -name => 'attr_operator', -values => [ '', 'STRBW', 'STREQ' ], |
$attr_operators ? popup_menu( -name => 'attr_operator', %$attr_operators ) : '', |
175 |
-labels => { |
submit( -value => 'Search' ), |
176 |
'' => 'Bilo koja riječ', |
# hidden( -name => 'entries_per_page', -default => $entries_per_page ), |
177 |
'STRBW' => 'Početak', |
popup_menu( -name => 'entries_per_page', -values => [ @entries_per_page ], -title => 'entries per page' ), |
178 |
'STREQ' => 'Točan oblik', |
# 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 >|, |
|
), |
|
|
submit, |
|
|
hidden( -name => 'entries_per_page', -default => $entries_per_page ), |
|
|
hidden( -name => 'current_page', -default => 1 ), |
|
180 |
checkbox( -name => 'debug', -default => 0 ), # FIXME hidden? |
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 |
print end_form; |
|
195 |
|
print |
196 |
|
start_html( |
197 |
|
-title => $db->{name}, |
198 |
|
-style => [ @style ], |
199 |
|
), |
200 |
|
h1( $db->{name} ), |
201 |
|
qq|<div id=description>|, $db->{description}, qq|</div>|, |
202 |
|
; |
203 |
|
|
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 |
my $node = Search::Estraier::Node->new( |
print qq| |
209 |
url => $config->{hyperestraier}->{masterurl} . '/node/' . $database, |
<a href="#form" class="skip" title="skip to search form">#</a> |
210 |
croak_on_error => 1, |
<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 |
|
# don't return -* &* and other non-word characters |
238 |
|
return '' if $template =~ m/^\W\*$/; |
239 |
|
|
240 |
|
return |
241 |
|
$whitespace . |
242 |
|
$attr . '="' . $template . '"'; |
243 |
|
; |
244 |
|
}; |
245 |
|
if ( $op =~ m{\s} ) { |
246 |
|
my $template = $op; |
247 |
|
$template =~ s{Q}{$v}; |
248 |
|
$v = $attr . '="' . $template . '"'; |
249 |
|
} else { |
250 |
|
$v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge; |
251 |
|
} |
252 |
|
|
253 |
param( 'entries_per_page', $entries_per_page ) unless param('entries_per_page'); # FIXME not needed? |
push @or, $v; |
254 |
my $pager = Data::Page->new; |
|
255 |
$pager->total_entries( param('current_page') * param('entries_per_page') ); |
} |
256 |
$pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ ); |
push @search, '(' . join(') OR (', @or) . ')'; |
257 |
|
|
258 |
dump_yaml( 'pager', $pager ); |
} else { |
259 |
|
push @search, "all=\"$search\""; |
260 |
|
} |
261 |
|
|
262 |
my $cond = Search::Estraier::Condition->new( debug => $debug ); |
my $q = '(' . join(') AND (', @search) . ')'; |
|
$cond->set_phrase( $search ); |
|
|
$cond->set_skip( $pager->skipped ); |
|
|
$cond->set_max( $pager->entries_per_page ); |
|
263 |
|
|
264 |
if ( my $op = param('attr_operator') ) { |
my @only_input = param('only_input'); |
265 |
$cond->add_attr( param('attr') . " $op " . param('search') ); |
$q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input; |
266 |
|
|
267 |
|
warn "# query: $q\n"; |
268 |
|
my $search_obj = $swish->new_search_object; |
269 |
|
if ( my $sort = param('sort') ) { |
270 |
|
$search_obj->set_sort( $sort ); |
271 |
} |
} |
272 |
|
my $swish_results = $search_obj->execute( $q ); |
273 |
|
|
274 |
my $nres = $node->search( $cond, 0 ); |
dump_yaml( 'swish_results', $swish_results ); |
|
$pager->total_entries( $nres->hits ); |
|
275 |
|
|
276 |
dump_yaml( 'cond', $cond ); |
my $pager = Data::Page->new; |
277 |
dump_yaml( 'nres', $nres ); |
$pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ ); |
278 |
|
$pager->total_entries( $swish_results->hits ); |
279 |
|
|
280 |
if ( ! $nres ) { |
dump_yaml( 'pager', $pager ); |
281 |
my $no_results = "No results for search '%s'"; |
|
282 |
printf qq|<div class="error">$no_results</div>\n\n|, $search; |
$swish_results->seek_result( $pager->first - 1 ); |
283 |
|
|
284 |
|
if ( ! $pager->total_entries ) { |
285 |
|
my $no_results = 'No results for search <b>%s</b>'; |
286 |
|
$no_results = $swish->error_string . '<br><b>%s</b>' if $swish->error; |
287 |
|
printf qq|<div class="error">$no_results</div>\n\n|, $q; |
288 |
} else { |
} else { |
289 |
|
|
290 |
my $results = "%d results for search '%s' showing results %d - %d on page %d"; |
my $results = "<b>%d</b> results for search <b>%s</b> showing results %d - %d"; |
291 |
printf qq|<div class="message">$results</div>\n\n|, $nres->hits, $search, $pager->first, $pager->last, $pager->current_page; |
printf qq|<div class="message">$results</div>\n\n|, $pager->total_entries, $q, $pager->first, $pager->last; |
292 |
|
|
293 |
my $pager_html = join("\n", show_pager( $pager )); |
my $pager_html = join("\n", show_pager( $pager )); |
294 |
|
|
295 |
print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html; |
print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html; |
296 |
|
|
297 |
my $start = $pager->first; |
my $nr = $pager->first; |
298 |
print qq|<ol start=$start>\n|; |
print qq|<ol start=$nr>\n|; |
299 |
|
|
300 |
|
my $limit = $pager->entries_on_this_page; |
301 |
|
|
302 |
foreach my $i ( 1 .. $nres->doc_num ) { |
my $nr = 1; |
303 |
my $rdoc = $nres->get_doc( $i - 1 ); |
|
304 |
print qq|<li>|; |
while ( my $result = $swish_results->next_result ) { |
305 |
|
|
306 |
|
my $data = $result->property('data'); |
307 |
|
dump_yaml( 'data', $data ); |
308 |
|
# FIXME if we produce valid json we shouldn't need eval here! |
309 |
|
eval { $data = from_json( $data, {utf8 => 1} ); }; |
310 |
|
if ( $@ ) { |
311 |
|
warn "ERROR: $@ from ",dump( $data ); |
312 |
|
next; |
313 |
|
} |
314 |
|
|
315 |
|
my $li_class = ''; |
316 |
|
$li_class = qq| class="z"| if $nr % 2 == 0; |
317 |
|
print qq|<li$li_class>|; |
318 |
foreach my $attr ( @attr ) { |
foreach my $attr ( @attr ) { |
319 |
my $v = $rdoc->attr( $attr ); |
next unless defined $data->{$attr}; |
320 |
if ( defined $v && $html_markup && ! $html_markup_skip->{$attr} ) { |
my $v = $data->{$attr}; |
321 |
eval "\$v = $html_markup->$attr( \$v );"; |
if ( $html_markup && ! $html_markup_skip->{$attr} ) { |
322 |
|
eval "\$v = $html_markup->$attr( \$v, \$data );"; |
323 |
if ( $@ ) { |
if ( $@ ) { |
324 |
warn "disable html markup for $attr: $@"; |
warn "disable html markup for $attr: $@"; |
325 |
$html_markup_skip->{$attr} = $@; |
$html_markup_skip->{$attr} = $@; |
326 |
} |
} |
327 |
|
} else { |
328 |
|
$v =~ s{(http://\S+)}{<a href="$1">$1</a>}; |
329 |
} |
} |
330 |
next unless defined $v; |
my $label = $attr_labels->{'-labels'}->{$attr} || $attr; |
331 |
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|; |
332 |
} |
} |
333 |
print qq|</li>\n|; |
print qq|</li>\n|; |
334 |
|
|
335 |
|
last if $nr++ == $pager->last; |
336 |
} |
} |
337 |
print qq|</ol>\n\n|; |
print qq|</ol>\n\n|; |
338 |
|
|
344 |
|
|
345 |
} |
} |
346 |
|
|
347 |
dump_yaml( 'estraier', $estraier ); |
print search_form; |
348 |
dump_yaml( 'db', $db ); |
|
349 |
|
dump_yaml( "config databases $database", $db ); |
350 |
dump_yaml( 'html_markup_skip', $html_markup_skip ); |
dump_yaml( 'html_markup_skip', $html_markup_skip ); |
351 |
|
|
352 |
print end_html; |
print end_html; |