7 use CGI::Carp qw/fatalsToBrowser/;
11 use Data::Dump qw/dump/;
14 use Text::Unaccent::PurePerl qw/unac_string/;
15 use HTML::FillInForm::Lite;
19 my @entries_per_page = ( 30, 50, 100, 500 );
20 my $debug = param('debug');
28 print qq|<div class=dump><tt>$name</tt><pre>|, YAML::Dump( @_ ), qq|</pre></div>| if $debug;
35 my $after_current = 0;
37 if ( $pager->current_page <= $range_around + 2 ) {
38 @show_pages = ( $pager->first_page .. $pager->current_page );
39 $after_current = $range_around - $pager->current_page;
41 @show_pages = ( $pager->first_page, '', $pager->current_page - $range_around .. $pager->current_page );
44 if ( $pager->current_page + $after_current + $range_around + 1 >= $pager->last_page ) {
45 push @show_pages, ( $pager->current_page + 1 .. $pager->last_page );
47 push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $range_around, '', $pager->last_page );
50 # dump_yaml( 'show_pages', \@show_pages );
52 return '' unless $#show_pages;
54 my ( $prev, $next ) = ( '<<', '>>' );
57 my ( $page, $label, $attr ) = @_;
58 param( 'current_page', $page );
59 my $url = self_url( -query => 1 );
62 qq|<li$attr><a href="$url" title="$page">$label</a></li>|;
66 $pager->previous_page ? li_a_href( $pager->previous_page, $prev ) : qq|<li class=skip>$prev</li>|
68 if ( $_ eq $pager->current_page ) {
69 qq|<li class=current_page>$_</li>|;
70 } elsif ( $_ eq '' ) {
71 qq|<li class=skip>...</li>|;
76 , $pager->next_page ? li_a_href( $pager->next_page, $next ) : qq|<li class=skip>$next</li>|
81 my $path = $ENV{PATH_INFO} || 'ecas';
85 $dir =~ s{/[^/]+.cgi}{};
87 dump_yaml( 'dir', $dir );
89 my $config = YAML::LoadFile( "$dir/$path/config.yml" );
91 my $database = (keys %{ $config->{databases} })[0];
92 die "$database not in $path" unless $path =~ m{\Q$database\E};
94 my $html_markup = "$dir/$path/html.pm";
96 if ( -e $html_markup ) {
98 $html_markup = $database . '::html';
105 my $path = "$dir/../var/swish/$database.yaml";
106 $stats = YAML::LoadFile( $path );
107 dump_yaml( "stats $path", $stats );
110 my $db = $config->{databases}->{$database};
112 sub read_config_txt {
115 my $path ="$dir/$path/$path-$file.txt";
117 warn "missing $path";
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;
128 my $attr_labels = read_config_txt 'labels';
129 my $attr_operators = read_config_txt 'operators';
131 my @attr = @{ $attr_labels->{'-values'} } if $attr_labels;
132 @attr = keys %{ $stats->{attr} } unless @attr;
135 warn dump( $attr_labels, $attr_operators );
138 my $inputs_available = 0;
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;
150 warn "## only_input = ", dump( $only_input );
152 my @style = ( '../../style.css' );
153 push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";
154 dump_yaml( 'style', \@style );
158 my $form_html = "$dir/$path/$path-search.html";
159 if ( -e $form_html ) {
160 my $html = read_file( $form_html );
162 my $h = HTML::FillInForm::Lite->new();
163 return $h->fill(\$html, $q);
166 qq|<a name="form"></a>|,
167 start_form( -action => self_url( query => 0 ) ),
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?
182 $inputs_available > 1 ?
183 h2( 'Select input' ) .
185 -name => 'only_input',
197 -title => $db->{name},
198 -style => [ @style ],
201 qq|<div id=description>|, $db->{description}, qq|</div>|,
204 if ( my $search = param('search') ) {
206 $search = unac_string( Encode::decode('utf-8',$search) );
209 <a href="#form" class="skip" title="skip to search form">#</a>
213 my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
214 $swish->abort_last_error if $swish->Error;
217 my @attrs = param('attr');
218 my $op = param('attr_operator');
220 if ( $search =~ m{(=|"|AND|OR)} ) {
221 push @search, $search;
226 foreach my $attr ( @attrs ) {
231 my ( $attr, $whitespace, $v ) = @_;
232 warn "## filter $op $whitespace $v\n";
234 $template =~ s{Q}{$v};
235 $whitespace = " AND " if $whitespace;
239 $attr . '="' . $template . '"';
242 if ( $op =~ m{\s} ) {
244 $template =~ s{Q}{$v};
245 $v = $attr . '="' . $template . '"';
247 $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
253 push @search, '(' . join(') OR (', @or) . ')';
256 push @search, "all=\"$search\"";
259 my $q = '(' . join(') AND (', @search) . ')';
261 my @only_input = param('only_input');
262 $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
264 warn "# query: $q\n";
265 my $swish_results = $swish->query( $q );
267 dump_yaml( 'swish_results', $swish_results );
269 my $pager = Data::Page->new;
270 $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
271 $pager->total_entries( $swish_results->hits );
273 dump_yaml( 'pager', $pager );
275 $swish_results->seek_result( $pager->first - 1 );
277 if ( ! $pager->total_entries ) {
278 my $no_results = 'No results for search <b>%s</b>';
279 $no_results = $swish->error_string . '<br><b>%s</b>' if $swish->error;
280 printf qq|<div class="error">$no_results</div>\n\n|, $q;
283 my $results = "<b>%d</b> results for search <b>%s</b> showing results %d - %d";
284 printf qq|<div class="message">$results</div>\n\n|, $pager->total_entries, $q, $pager->first, $pager->last;
286 my $pager_html = join("\n", show_pager( $pager ));
288 print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html;
290 my $nr = $pager->first;
291 print qq|<ol start=$nr>\n|;
293 my $limit = $pager->entries_on_this_page;
297 while ( my $result = $swish_results->next_result ) {
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} ); };
304 warn "ERROR: $@ from ",dump( $data );
309 $li_class = qq| class="z"| if $nr % 2 == 0;
310 print qq|<li$li_class>|;
311 foreach my $attr ( @attr ) {
312 next unless defined $data->{$attr};
313 my $v = $data->{$attr};
314 if ( $html_markup && ! $html_markup_skip->{$attr} ) {
315 eval "\$v = $html_markup->$attr( \$v, \$data );";
317 warn "disable html markup for $attr: $@";
318 $html_markup_skip->{$attr} = $@;
321 $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
323 my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
324 print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|;
328 last if $nr++ == $pager->last;
332 print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html;
336 dump_yaml( 'pager', $pager );
342 dump_yaml( "config databases $database", $db );
343 dump_yaml( 'html_markup_skip', $html_markup_skip );