7f122bb638f202900ef3f2f9fde2c8ff9dd0ea40
[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 use HTML::FillInForm::Lite;
16 use Encode;
17
18 my $range_around = 5;
19 my @entries_per_page = ( 30, 50, 100, 500 );
20 my $debug = param('debug');
21
22 print header(
23         -charset => 'utf-8',
24 );
25
26 sub dump_yaml {
27         my $name = shift;
28         print qq|<div class=dump><tt>$name</tt><pre>|, YAML::Dump( @_ ), qq|</pre></div>| if $debug;
29 }
30
31 sub show_pager {
32         my ($pager) = @_;
33
34         my @show_pages;
35         my $after_current = 0;
36
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;
40         } else {
41                 @show_pages = ( $pager->first_page, '', $pager->current_page - $range_around .. $pager->current_page );
42         }
43
44         if ( $pager->current_page + $after_current + $range_around + 1 >= $pager->last_page ) {
45                 push @show_pages, ( $pager->current_page + 1 .. $pager->last_page );
46         } else {
47                 push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $range_around, '', $pager->last_page );
48         }
49
50 #       dump_yaml( 'show_pages', \@show_pages );
51
52         return '' unless $#show_pages;
53
54         my ( $prev, $next ) = ( '&lt;&lt;', '&gt;&gt;' );
55
56         sub li_a_href {
57                 my ( $page, $label, $attr ) = @_;
58                 param( 'current_page', $page );
59                 my $url = self_url( -query => 1 );
60                 $attr ||= '';
61                 $label ||= $page;
62                 qq|<li$attr><a href="$url" title="$page">$label</a></li>|;
63         }
64
65         return
66                   $pager->previous_page ? li_a_href( $pager->previous_page, $prev ) : qq|<li class=skip>$prev</li>|
67                 , ( map {
68                         if ( $_ eq $pager->current_page ) {
69                                 qq|<li class=current_page>$_</li>|;
70                         } elsif ( $_ eq '' ) {
71                                 qq|<li class=skip>...</li>|;
72                         } else {
73                                 li_a_href( $_ );
74                         }
75                 } @show_pages )
76                 , $pager->next_page ? li_a_href( $pager->next_page, $next ) : qq|<li class=skip>$next</li>|
77                 ;
78                 
79 }
80
81 my $path = $ENV{PATH_INFO} || 'ecas';
82 $path =~ s{^/+}{};
83 $path =~ s{/+$}{};
84 my $dir = $0;
85 $dir =~ s{/[^/]+.cgi}{};
86
87 dump_yaml( 'dir', $dir );
88
89 my $config = YAML::LoadFile( "$dir/$path/config.yml" );
90
91 my $database = (keys %{ $config->{databases} })[0];
92 die "$database not in $path" unless $path =~ m{\Q$database\E};
93
94 my $html_markup = "$dir/$path/html.pm";
95 my $html_markup_skip;
96 if ( -e $html_markup ) {
97         require $html_markup;
98         $html_markup = $database . '::html';
99 } else {
100         undef $html_markup;
101 }
102
103 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};
111
112 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 my $attr_labels    = read_config_txt 'labels';
129 my $attr_operators = read_config_txt 'operators';
130
131 my @attr = @{ $attr_labels->{'-values'} } if $attr_labels;
132 @attr = keys %{ $stats->{attr} } unless @attr;
133
134
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 ) ),
168                 checkbox_group(
169                         -name => 'attr',
170                         %$attr_labels,
171 #                       -linebreak => 0,
172                 ),
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?
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
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') ) {
205
206         $search = unac_string( Encode::decode('utf-8',$search) );
207
208         print qq|
209                 <a href="#form" class="skip" title="skip to search form">#</a>
210                 <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{(=|"|AND|OR)} ) {
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                                 return
238                                         $whitespace .
239                                         $attr . '="' . $template . '"';
240                                         ;
241                         };
242                         if ( $op =~ m{\s} ) {
243                                 my $template = $op;
244                                    $template =~ s{Q}{$v};
245                                 $v = $attr . '="' . $template . '"';
246                         } else {
247                                 $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
248                         }
249
250                         push @or, $v;
251                 
252                 }
253                 push @search, '(' . join(') OR (', @or) . ')';
254
255         } else {
256                 push @search, "all=\"$search\"";
257         }
258
259         my $q = '(' . join(') AND (', @search) . ')';
260
261         my @only_input = param('only_input');
262         $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
263
264         warn "# query: $q\n";
265         my $swish_results = $swish->query( $q );
266
267         dump_yaml( 'swish_results', $swish_results );
268
269         my $pager = Data::Page->new;
270         $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
271         $pager->total_entries( $swish_results->hits );
272
273         dump_yaml( 'pager', $pager );
274
275         $swish_results->seek_result( $pager->first - 1 );
276
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;
281         } else {
282
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;
285
286                 my $pager_html = join("\n", show_pager( $pager ));
287
288                 print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html;
289
290                 my $nr = $pager->first;
291                 print qq|<ol start=$nr>\n|;
292
293                 my $limit = $pager->entries_on_this_page;
294
295                 my $nr = 1;
296
297                 while ( my $result = $swish_results->next_result ) {
298
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} ); };
303                         if ( $@ ) {
304                                 warn "ERROR: $@ from ",dump( $data );
305                                 next;
306                         }
307
308                         my $li_class = '';
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 );";
316                                         if ( $@ ) {
317                                                 warn "disable html markup for $attr: $@";
318                                                 $html_markup_skip->{$attr} = $@;
319                                         }
320                                 } else {
321                                         $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
322                                 }
323                                 my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
324                                 print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|;
325                         }
326                         print qq|</li>\n|;
327
328                         last if $nr++ == $pager->last;
329                 }
330                 print qq|</ol>\n\n|;
331
332                 print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html;
333         }
334         print qq|</div>|;
335
336         dump_yaml( 'pager', $pager );
337
338 }
339
340 print search_form;
341
342 dump_yaml( "config databases $database", $db );
343 dump_yaml( 'html_markup_skip', $html_markup_skip );
344
345 print   end_html;