7 use CGI::Carp qw/fatalsToBrowser/;
11 use Data::Dump qw/dump/;
14 #use Text::Unaccent::PurePerl qw/unac_string/;
15 use Text::Unaccent qw/unac_string/;
16 use HTML::FillInForm::Lite;
20 my @entries_per_page = ( 30, 50, 100, 500 );
21 my $debug = param('debug');
29 print qq|<div class=dump><tt>$name</tt><pre>|, YAML::Dump( @_ ), qq|</pre></div>| if $debug;
36 my $after_current = 0;
38 if ( $pager->current_page <= $range_around + 2 ) {
39 @show_pages = ( $pager->first_page .. $pager->current_page );
40 $after_current = $range_around - $pager->current_page;
42 @show_pages = ( $pager->first_page, '', $pager->current_page - $range_around .. $pager->current_page );
45 if ( $pager->current_page + $after_current + $range_around + 1 >= $pager->last_page ) {
46 push @show_pages, ( $pager->current_page + 1 .. $pager->last_page );
48 push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $range_around, '', $pager->last_page );
51 # dump_yaml( 'show_pages', \@show_pages );
53 return '' unless $#show_pages;
55 my ( $prev, $next ) = ( '<<', '>>' );
58 my ( $page, $label, $attr ) = @_;
59 param( 'current_page', $page );
60 my $url = self_url( -query => 1 );
63 qq|<li$attr><a href="$url" title="$page">$label</a></li>|;
67 $pager->previous_page ? li_a_href( $pager->previous_page, $prev ) : qq|<li class=skip>$prev</li>|
69 if ( $_ eq $pager->current_page ) {
70 qq|<li class=current_page>$_</li>|;
71 } elsif ( $_ eq '' ) {
72 qq|<li class=skip>...</li>|;
77 , $pager->next_page ? li_a_href( $pager->next_page, $next ) : qq|<li class=skip>$next</li>|
82 my $path = $ENV{PATH_INFO} || 'ecas';
86 $dir =~ s{/[^/]+.cgi}{};
88 dump_yaml( 'dir', $dir );
90 my $config = YAML::LoadFile( "$dir/$path/config.yml" );
92 my $database = (keys %{ $config->{databases} })[0];
93 die "$database not in $path" unless $path =~ m{\Q$database\E};
95 my $html_markup = "$dir/$path/html.pm";
97 if ( -e $html_markup ) {
99 $html_markup = $database . '::html';
106 my $path = "$dir/../var/swish/$database.yaml";
107 $stats = YAML::LoadFile( $path );
108 dump_yaml( "stats $path", $stats );
111 my $db = $config->{databases}->{$database};
113 sub read_config_txt {
116 my $path ="$dir/$path/$path-$file.txt";
118 warn "missing $path";
121 foreach ( split(/[\n\r]+/, read_file( $path ) ) ) {
122 my ( $val,$label ) = split(/\s*\t\s*/,$_,2);
123 push @{ $input->{ '-values' } }, $val;
124 $input->{ '-labels' }->{$val} = $label;
129 my $attr_labels = read_config_txt 'labels';
130 my $attr_operators = read_config_txt 'operators';
132 my @attr = @{ $attr_labels->{'-values'} } if $attr_labels;
133 @attr = keys %{ $stats->{attr} } unless @attr;
136 warn dump( $attr_labels, $attr_operators );
139 my $inputs_available = 0;
141 foreach ( @{ $db->{input} } ) {
142 my $input = $_->{name} || die "no name in ",dump( $_ );
143 next unless defined $stats->{input}->{$input}; # skip inputs without data
144 if ( ! $only_input->{'-labels'}->{$input} ) {
145 push @{ $only_input->{'-values'} }, $input;
146 $only_input->{'-labels'}->{$input} = $_->{description} || $input;
151 warn "## only_input = ", dump( $only_input );
153 my @style = ( '../../style.css' );
154 push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";
155 dump_yaml( 'style', \@style );
159 my $form_html = "$dir/$path/$path-search.html";
160 if ( -e $form_html ) {
161 my $html = read_file( $form_html );
163 my $h = HTML::FillInForm::Lite->new();
164 return $h->fill(\$html, $q);
167 qq|<a name="form"></a>|,
168 start_form( -action => self_url( query => 0 ) ),
174 textfield( -name => 'search' ),
175 $attr_operators ? popup_menu( -name => 'attr_operator', %$attr_operators ) : '',
176 submit( -value => 'Search' ),
177 # hidden( -name => 'entries_per_page', -default => $entries_per_page ),
178 popup_menu( -name => 'entries_per_page', -values => [ @entries_per_page ], -title => 'entries per page' ),
179 # we need current_page fixed at 1 so that every submit through form will reset it
180 qq|<input type=hidden name=current_page value=1 >|,
181 checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?
183 $inputs_available > 1 ?
184 h2( 'Select input' ) .
186 -name => 'only_input',
198 -title => $db->{name},
199 -style => [ @style ],
202 qq|<div id=description>|, $db->{description}, qq|</div>|,
205 if ( my $search = param('search') ) {
207 $search = unac_string( 'utf-8', Encode::decode('utf-8',$search) );
210 <a href="#form" class="skip" title="skip to search form">#</a>
214 my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
215 $swish->abort_last_error if $swish->Error;
218 my @attrs = param('attr');
219 my $op = param('attr_operator');
221 if ( $search =~ m{(=|"|\bAND\b|\bOR\b)} ) {
222 push @search, $search;
227 foreach my $attr ( @attrs ) {
232 my ( $attr, $whitespace, $v ) = @_;
233 warn "## filter $op $whitespace $v\n";
235 $template =~ s{Q}{$v};
236 $whitespace = " AND " if $whitespace;
238 # don't return -* &* and other non-word characters
239 return '' if $template =~ m/^\W\*$/ || $template =~ m/\band\b/i;
243 $attr . '="' . $template . '"';
246 if ( $op =~ m{\s} ) {
248 $template =~ s{Q}{$v};
249 $v = $attr . '="' . $template . '"';
251 $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
257 push @search, '(' . join(') OR (', @or) . ')';
260 push @search, "all=\"$search\"";
263 my $q = '(' . join(') AND (', @search) . ')';
265 my @only_input = param('only_input');
266 $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
268 warn "# query: $q\n";
269 my $search_obj = $swish->new_search_object;
270 if ( my $sort = param('sort') ) {
271 $search_obj->set_sort( $sort );
273 my $swish_results = $search_obj->execute( $q );
275 dump_yaml( 'swish_results', $swish_results );
277 my $pager = Data::Page->new;
278 $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
279 $pager->total_entries( $swish_results->hits );
281 dump_yaml( 'pager', $pager );
283 $swish_results->seek_result( $pager->first - 1 );
285 if ( ! $pager->total_entries ) {
286 my $no_results = 'No results for search <b>%s</b>';
287 $no_results = $swish->error_string . '<br><b>%s</b>' if $swish->error;
288 printf qq|<div class="error">$no_results</div>\n\n|, $q;
291 my $results = "<b>%d</b> results for search <b>%s</b> showing results %d - %d";
292 printf qq|<div class="message">$results</div>\n\n|, $pager->total_entries, $q, $pager->first, $pager->last;
294 my $pager_html = join("\n", show_pager( $pager ));
296 print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html;
298 my $nr = $pager->first;
299 print qq|<ol start=$nr>\n|;
301 my $limit = $pager->entries_on_this_page;
305 while ( my $result = $swish_results->next_result ) {
307 my $data = $result->property('data');
308 dump_yaml( 'data', $data );
309 # FIXME if we produce valid json we shouldn't need eval here!
310 eval { $data = from_json( $data, {utf8 => 1} ); };
312 warn "ERROR: $@ from ",dump( $data );
317 $li_class = qq| class="z"| if $nr % 2 == 0;
318 print qq|<li$li_class>|;
319 foreach my $attr ( @attr ) {
320 next unless defined $data->{$attr};
321 my $v = $data->{$attr};
322 if ( $html_markup && ! $html_markup_skip->{$attr} ) {
323 eval "\$v = $html_markup->$attr( \$v, \$data );";
325 warn "disable html markup for $attr: $@";
326 $html_markup_skip->{$attr} = $@;
329 $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
331 my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
332 print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|;
336 last if $nr++ == $pager->last;
340 print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html;
344 dump_yaml( 'pager', $pager );
350 dump_yaml( "config databases $database", $db );
351 dump_yaml( 'html_markup_skip', $html_markup_skip );