7 use CGI::Carp qw/fatalsToBrowser/;
11 use Data::Dump qw/dump/;
14 use Text::Unaccent::PurePerl qw/unac_string/;
17 my @entries_per_page = ( 30, 50, 100, 500 );
18 my $debug = param('debug');
26 print qq|<div class=dump><tt>$name</tt><pre>|, YAML::Dump( @_ ), qq|</pre></div>| if $debug;
33 my $after_current = 0;
35 if ( $pager->current_page <= $range_around + 2 ) {
36 @show_pages = ( $pager->first_page .. $pager->current_page );
37 $after_current = $range_around - $pager->current_page;
39 @show_pages = ( $pager->first_page, '', $pager->current_page - $range_around .. $pager->current_page );
42 if ( $pager->current_page + $after_current + $range_around + 1 >= $pager->last_page ) {
43 push @show_pages, ( $pager->current_page + 1 .. $pager->last_page );
45 push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $range_around, '', $pager->last_page );
48 # dump_yaml( 'show_pages', \@show_pages );
50 return '' unless $#show_pages;
52 my ( $prev, $next ) = ( '<<', '>>' );
55 my ( $page, $label, $attr ) = @_;
56 param( 'current_page', $page );
57 my $url = self_url( -query => 1 );
60 qq|<li$attr><a href="$url" title="$page">$label</a></li>|;
64 $pager->previous_page ? li_a_href( $pager->previous_page, $prev ) : qq|<li class=skip>$prev</li>|
66 if ( $_ eq $pager->current_page ) {
67 qq|<li class=current_page>$_</li>|;
68 } elsif ( $_ eq '' ) {
69 qq|<li class=skip>...</li>|;
74 , $pager->next_page ? li_a_href( $pager->next_page, $next ) : qq|<li class=skip>$next</li>|
79 my $path = $ENV{PATH_INFO} || 'ecas';
83 $dir =~ s{/[^/]+.cgi}{};
85 dump_yaml( 'dir', $dir );
87 my $config = YAML::LoadFile( "$dir/$path/config.yml" );
89 my $database = (keys %{ $config->{databases} })[0];
90 die "$database not in $path" unless $path =~ m{\Q$database\E};
92 my $html_markup = "$dir/$path/html.pm";
94 if ( -e $html_markup ) {
96 $html_markup = $database . '::html';
103 my $path = "$dir/../var/swish/$database.yaml";
104 $stats = YAML::LoadFile( $path );
105 dump_yaml( "stats $path", $stats );
108 my $db = $config->{databases}->{$database};
110 sub read_config_txt {
113 my $path ="$dir/$path/$path-$file.txt";
115 warn "missing $path";
118 foreach ( split(/[\n\r]+/, read_file( $path ) ) ) {
119 my ( $val,$label ) = split(/\s*\t\s*/,$_,2);
120 push @{ $input->{ '-values' } }, $val;
121 $input->{ '-labels' }->{$val} = $label;
126 my $attr_labels = read_config_txt 'labels';
127 my $attr_operators = read_config_txt 'operators';
129 my @attr = @{ $attr_labels->{'-values'} } if $attr_labels;
130 @attr = keys %{ $stats->{attr} } unless @attr;
133 warn dump( $attr_labels, $attr_operators );
136 my $inputs_available = 0;
138 foreach ( @{ $db->{input} } ) {
139 my $input = $_->{name} || die "no name in ",dump( $_ );
140 next unless defined $stats->{input}->{$input}; # skip inputs without data
141 if ( ! $only_input->{'-labels'}->{$input} ) {
142 push @{ $only_input->{'-values'} }, $input;
143 $only_input->{'-labels'}->{$input} = $_->{description} || $input;
148 warn "## only_input = ", dump( $only_input );
150 my @style = ( '../../style.css' );
151 push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";
152 dump_yaml( 'style', \@style );
155 qq|<a name="form"></a>|,
156 start_form( -action => self_url( query => 0 ) ),
162 textfield( -name => 'search' ),
163 $attr_operators ? popup_menu( -name => 'attr_operator', %$attr_operators ) : '',
164 submit( -value => 'Search' ),
165 # hidden( -name => 'entries_per_page', -default => $entries_per_page ),
166 popup_menu( -name => 'entries_per_page', -values => [ @entries_per_page ], -title => 'entries per page' ),
167 # we need current_page fixed at 1 so that every submit through form will reset it
168 qq|<input type=hidden name=current_page value=1 >|,
169 checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?
171 $inputs_available > 1 ?
172 h2( 'Select input' ) .
174 -name => 'only_input',
186 -title => $db->{name},
187 -style => [ @style ],
190 qq|<div id=description>|, $db->{description}, qq|</div>|,
193 if ( my $search = param('search') ) {
195 $search = unac_string( $search );
198 <a href="#form" class="skip" title="skip to search form">#</a>
202 my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
203 $swish->abort_last_error if $swish->Error;
206 my @attrs = param('attr');
207 my $op = param('attr_operator');
209 if ( $search =~ m{(=|"|AND|OR)} ) {
210 push @search, $search;
215 foreach my $attr ( @attrs ) {
220 my ( $attr, $whitespace, $v ) = @_;
221 warn "## filter $op $whitespace $v\n";
223 $template =~ s{Q}{$v};
224 $whitespace = " AND " if $whitespace;
228 $attr . '="' . $template . '"';
231 if ( $op =~ m{\s} ) {
233 $template =~ s{Q}{$v};
234 $v = $attr . '="' . $template . '"';
236 $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
242 push @search, '(' . join(') OR (', @or) . ')';
245 push @search, "all=\"$search\"";
248 my $q = '(' . join(') AND (', @search) . ')';
250 my @only_input = param('only_input');
251 $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
253 warn "# query: $q\n";
254 my $swish_results = $swish->query( $q );
256 dump_yaml( 'swish_results', $swish_results );
258 my $pager = Data::Page->new;
259 $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
260 $pager->total_entries( $swish_results->hits );
262 dump_yaml( 'pager', $pager );
264 $swish_results->seek_result( $pager->first - 1 );
266 if ( ! $pager->total_entries ) {
267 my $no_results = 'No results for search <b>%s</b>';
268 $no_results = $swish->error_string . '<br><b>%s</b>' if $swish->error;
269 printf qq|<div class="error">$no_results</div>\n\n|, $q;
272 my $results = "<b>%d</b> results for search <b>%s</b> showing results %d - %d";
273 printf qq|<div class="message">$results</div>\n\n|, $pager->total_entries, $q, $pager->first, $pager->last;
275 my $pager_html = join("\n", show_pager( $pager ));
277 print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html;
279 my $nr = $pager->first;
280 print qq|<ol start=$nr>\n|;
282 my $limit = $pager->entries_on_this_page;
286 while ( my $result = $swish_results->next_result ) {
288 my $data = $result->property('data');
289 dump_yaml( 'data', $data );
290 # FIXME if we produce valid json we shouldn't need eval here!
291 eval { $data = from_json( $data, {utf8 => 1} ); };
293 warn "ERROR: $@ from ",dump( $data );
298 $li_class = qq| class="z"| if $nr % 2 == 0;
299 print qq|<li$li_class>|;
300 foreach my $attr ( @attr ) {
301 next unless defined $data->{$attr};
302 my $v = $data->{$attr};
303 if ( $html_markup && ! $html_markup_skip->{$attr} ) {
304 eval "\$v = $html_markup->$attr( \$v, \$data );";
306 warn "disable html markup for $attr: $@";
307 $html_markup_skip->{$attr} = $@;
310 $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
312 my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
313 print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|;
317 last if $nr++ == $pager->last;
321 print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html;
325 dump_yaml( 'pager', $pager );
331 dump_yaml( "config databases $database", $db );
332 dump_yaml( 'html_markup_skip', $html_markup_skip );