d68d2416db7791f665ed87ca5aea562846bccc16
[webpac2] / vhost / webpac2.cgi
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use CGI qw/:standard/;
7 use CGI::Carp qw/fatalsToBrowser/;
8 use File::Slurp;
9 use YAML;
10 use Data::Page;
11 use Data::Dump qw/dump/;
12 use SWISH::API;
13 use JSON;
14 use Text::Unaccent::PurePerl qw/unac_string/;
15
16 my $range_around = 5;
17 my @entries_per_page = ( 30, 50, 100, 500 );
18 my $debug = param('debug');
19
20 print header(
21         -charset => 'utf-8',
22 );
23
24 sub dump_yaml {
25         my $name = shift;
26         print qq|<div class=dump><tt>$name</tt><pre>|, YAML::Dump( @_ ), qq|</pre></div>| if $debug;
27 }
28
29 sub show_pager {
30         my ($pager) = @_;
31
32         my @show_pages;
33         my $after_current = 0;
34
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;
38         } else {
39                 @show_pages = ( $pager->first_page, '', $pager->current_page - $range_around .. $pager->current_page );
40         }
41
42         if ( $pager->current_page + $after_current + $range_around + 1 >= $pager->last_page ) {
43                 push @show_pages, ( $pager->current_page + 1 .. $pager->last_page );
44         } else {
45                 push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $range_around, '', $pager->last_page );
46         }
47
48 #       dump_yaml( 'show_pages', \@show_pages );
49
50         return '' unless $#show_pages;
51
52         my ( $prev, $next ) = ( '&lt;&lt;', '&gt;&gt;' );
53
54         sub li_a_href {
55                 my ( $page, $label, $attr ) = @_;
56                 param( 'current_page', $page );
57                 my $url = self_url( -query => 1 );
58                 $attr ||= '';
59                 $label ||= $page;
60                 qq|<li$attr><a href="$url" title="$page">$label</a></li>|;
61         }
62
63         return
64                   $pager->previous_page ? li_a_href( $pager->previous_page, $prev ) : qq|<li class=skip>$prev</li>|
65                 , ( map {
66                         if ( $_ eq $pager->current_page ) {
67                                 qq|<li class=current_page>$_</li>|;
68                         } elsif ( $_ eq '' ) {
69                                 qq|<li class=skip>...</li>|;
70                         } else {
71                                 li_a_href( $_ );
72                         }
73                 } @show_pages )
74                 , $pager->next_page ? li_a_href( $pager->next_page, $next ) : qq|<li class=skip>$next</li>|
75                 ;
76                 
77 }
78
79 my $path = $ENV{PATH_INFO} || 'ecas';
80 $path =~ s{^/+}{};
81 $path =~ s{/+$}{};
82 my $dir = $0;
83 $dir =~ s{/[^/]+.cgi}{};
84
85 dump_yaml( 'dir', $dir );
86
87 my $config = YAML::LoadFile( "$dir/$path/config.yml" );
88
89 my $database = (keys %{ $config->{databases} })[0];
90 die "$database not in $path" unless $path =~ m{\Q$database\E};
91
92 my $html_markup = "$dir/$path/html.pm";
93 my $html_markup_skip;
94 if ( -e $html_markup ) {
95         require $html_markup;
96         $html_markup = $database . '::html';
97 } else {
98         undef $html_markup;
99 }
100
101 my $stats;
102 {
103         my $path = "$dir/../var/swish/$database.yaml";
104         $stats = YAML::LoadFile( $path );
105         dump_yaml( "stats $path", $stats );
106 }
107
108 my $db = $config->{databases}->{$database};
109
110 sub read_config_txt {
111         my ( $file ) = @_;
112         my $input;
113         my $path ="$dir/$path/$path-$file.txt";
114         if ( ! -e $path ) {
115                 warn "missing $path";
116                 return;
117         }
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;
122         }
123         return $input;
124 }
125
126 my $attr_labels    = read_config_txt 'labels';
127 my $attr_operators = read_config_txt 'operators';
128
129 my @attr = @{ $attr_labels->{'-values'} } if $attr_labels;
130 @attr = keys %{ $stats->{attr} } unless @attr;
131
132
133 warn dump( $attr_labels, $attr_operators );
134
135 my $only_input;
136 my $inputs_available = 0;
137
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;
144                 $inputs_available++;
145         }
146 }
147
148 warn "## only_input = ", dump( $only_input );
149
150 my @style = ( '../../style.css' );
151 push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";
152 dump_yaml( 'style', \@style );
153
154 sub search_form {
155         qq|<a name="form"></a>|,
156         start_form( -action => self_url( query => 0 ) ),
157                 checkbox_group(
158                         -name => 'attr',
159                         %$attr_labels,
160 #                       -linebreak => 0,
161                 ),
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?
170                 qq|<div id=inputs>|,
171                 $inputs_available > 1 ?
172                 h2( 'Select input' ) .
173                 checkbox_group(
174                         -name => 'only_input',
175                         %$only_input,
176                         -linebreak=> 'true',
177                 ) : '',
178                 qq|</div>|,
179                 end_form,
180         ;
181 }
182
183
184 print
185         start_html(
186                 -title => $db->{name},
187                 -style => [ @style ],
188         ),
189         h1( $db->{name} ),
190         qq|<div id=description>|, $db->{description}, qq|</div>|,
191 ;
192
193 if ( my $search = param('search') ) {
194
195         $search = unac_string( $search );
196
197         print qq|
198                 <a href="#form" class="skip" title="skip to search form">#</a>
199                 <div id="results">
200         |;
201
202         my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
203         $swish->abort_last_error if $swish->Error;
204
205         my @search = (); 
206         my @attrs = param('attr');
207         my $op = param('attr_operator');
208
209         if ( $search =~ m{(=|"|AND|OR)} ) {
210                 push @search, $search;
211         } elsif ( @attrs ) {
212
213                 $op ||= 'Q*';
214                 my @or;
215                 foreach my $attr ( @attrs ) {
216                         my $v = $search;
217                         $v =~ s/^\s+//;
218                         warn "-- v: $v\n";
219                         sub rewrite {
220                                 my ( $attr, $whitespace, $v ) = @_;
221                                 warn "## filter $op $whitespace $v\n";
222                                 my $template = $op;
223                                    $template =~ s{Q}{$v};
224                                 $whitespace = " AND " if $whitespace;
225
226                                 return
227                                         $whitespace .
228                                         $attr . '="' . $template . '"';
229                                         ;
230                         };
231                         if ( $op =~ m{\s} ) {
232                                 my $template = $op;
233                                    $template =~ s{Q}{$v};
234                                 $v = $attr . '="' . $template . '"';
235                         } else {
236                                 $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
237                         }
238
239                         push @or, $v;
240                 
241                 }
242                 push @search, '(' . join(') OR (', @or) . ')';
243
244         } else {
245                 push @search, "all=\"$search\"";
246         }
247
248         my $q = '(' . join(') AND (', @search) . ')';
249
250         my @only_input = param('only_input');
251         $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
252
253         warn "# query: $q\n";
254         my $swish_results = $swish->query( $q );
255
256         dump_yaml( 'swish_results', $swish_results );
257
258         my $pager = Data::Page->new;
259         $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
260         $pager->total_entries( $swish_results->hits );
261
262         dump_yaml( 'pager', $pager );
263
264         $swish_results->seek_result( $pager->first - 1 );
265
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;
270         } else {
271
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;
274
275                 my $pager_html = join("\n", show_pager( $pager ));
276
277                 print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html;
278
279                 my $nr = $pager->first;
280                 print qq|<ol start=$nr>\n|;
281
282                 my $limit = $pager->entries_on_this_page;
283
284                 my $nr = 1;
285
286                 while ( my $result = $swish_results->next_result ) {
287
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} ); };
292                         if ( $@ ) {
293                                 warn "ERROR: $@ from ",dump( $data );
294                                 next;
295                         }
296
297                         my $li_class = '';
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 );";
305                                         if ( $@ ) {
306                                                 warn "disable html markup for $attr: $@";
307                                                 $html_markup_skip->{$attr} = $@;
308                                         }
309                                 } else {
310                                         $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
311                                 }
312                                 my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
313                                 print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|;
314                         }
315                         print qq|</li>\n|;
316
317                         last if $nr++ == $pager->last;
318                 }
319                 print qq|</ol>\n\n|;
320
321                 print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html;
322         }
323         print qq|</div>|;
324
325         dump_yaml( 'pager', $pager );
326
327 }
328
329 print search_form;
330
331 dump_yaml( "config databases $database", $db );
332 dump_yaml( 'html_markup_skip', $html_markup_skip );
333
334 print   end_html;