delete file
[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 Text::Unaccent qw/unac_string/;
16 use HTML::FillInForm::Lite;
17 use Encode;
18
19 my $range_around = 5;
20 my @entries_per_page = ( 30, 50, 100, 500 );
21 my $debug = param('debug');
22
23 print header(
24         -charset => 'utf-8',
25 );
26
27 sub dump_yaml {
28         my $name = shift;
29         print qq|<div class=dump><tt>$name</tt><pre>|, YAML::Dump( @_ ), qq|</pre></div>| if $debug;
30 }
31
32 sub show_pager {
33         my ($pager) = @_;
34
35         my @show_pages;
36         my $after_current = 0;
37
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;
41         } else {
42                 @show_pages = ( $pager->first_page, '', $pager->current_page - $range_around .. $pager->current_page );
43         }
44
45         if ( $pager->current_page + $after_current + $range_around + 1 >= $pager->last_page ) {
46                 push @show_pages, ( $pager->current_page + 1 .. $pager->last_page );
47         } else {
48                 push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $range_around, '', $pager->last_page );
49         }
50
51 #       dump_yaml( 'show_pages', \@show_pages );
52
53         return '' unless $#show_pages;
54
55         my ( $prev, $next ) = ( '&lt;&lt;', '&gt;&gt;' );
56
57         sub li_a_href {
58                 my ( $page, $label, $attr ) = @_;
59                 param( 'current_page', $page );
60                 my $url = self_url( -query => 1 );
61                 $attr ||= '';
62                 $label ||= $page;
63                 qq|<li$attr><a href="$url" title="$page">$label</a></li>|;
64         }
65
66         return
67                   $pager->previous_page ? li_a_href( $pager->previous_page, $prev ) : qq|<li class=skip>$prev</li>|
68                 , ( map {
69                         if ( $_ eq $pager->current_page ) {
70                                 qq|<li class=current_page>$_</li>|;
71                         } elsif ( $_ eq '' ) {
72                                 qq|<li class=skip>...</li>|;
73                         } else {
74                                 li_a_href( $_ );
75                         }
76                 } @show_pages )
77                 , $pager->next_page ? li_a_href( $pager->next_page, $next ) : qq|<li class=skip>$next</li>|
78                 ;
79                 
80 }
81
82 my $path = $ENV{PATH_INFO} || 'ecas';
83 $path =~ s{^/+}{};
84 $path =~ s{/+$}{};
85 my $dir = $0;
86 $dir =~ s{/[^/]+.cgi}{};
87
88 dump_yaml( 'dir', $dir );
89
90 my $config = YAML::LoadFile( "$dir/$path/config.yml" );
91
92 my $database = (keys %{ $config->{databases} })[0];
93 die "$database not in $path" unless $path =~ m{\Q$database\E};
94
95 my $html_markup = "$dir/$path/html.pm";
96 my $html_markup_skip;
97 if ( -e $html_markup ) {
98         require $html_markup;
99         $html_markup = $database . '::html';
100 } else {
101         undef $html_markup;
102 }
103
104 my $stats;
105 {
106         my $path = "$dir/../var/swish/$database.yaml";
107         $stats = YAML::LoadFile( $path );
108         dump_yaml( "stats $path", $stats );
109 }
110
111 my $db = $config->{databases}->{$database};
112
113 sub read_config_txt {
114         my ( $file ) = @_;
115         my $input;
116         my $path ="$dir/$path/$path-$file.txt";
117         if ( ! -e $path ) {
118                 warn "missing $path";
119                 return;
120         }
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;
125         }
126         return $input;
127 }
128
129 my $attr_labels    = read_config_txt 'labels';
130 my $attr_operators = read_config_txt 'operators';
131
132 my @attr = @{ $attr_labels->{'-values'} } if $attr_labels;
133 @attr = keys %{ $stats->{attr} } unless @attr;
134
135
136 warn dump( $attr_labels, $attr_operators );
137
138 my $only_input;
139 my $inputs_available = 0;
140
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;
147                 $inputs_available++;
148         }
149 }
150
151 warn "## only_input = ", dump( $only_input );
152
153 my @style = ( '../../style.css' );
154 push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";
155 dump_yaml( 'style', \@style );
156
157 sub search_form {
158
159         my $form_html = "$dir/$path/$path-search.html";
160         if ( -e $form_html ) {
161                 my $html = read_file( $form_html );
162                 my $q = CGI->new();
163                 my $h = HTML::FillInForm::Lite->new();
164                 return $h->fill(\$html, $q);
165         }
166
167         qq|<a name="form"></a>|,
168         start_form( -action => self_url( query => 0 ) ),
169                 checkbox_group(
170                         -name => 'attr',
171                         %$attr_labels,
172 #                       -linebreak => 0,
173                 ),
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?
182                 qq|<div id=inputs>|,
183                 $inputs_available > 1 ?
184                 h2( 'Select input' ) .
185                 checkbox_group(
186                         -name => 'only_input',
187                         %$only_input,
188                         -linebreak=> 'true',
189                 ) : '',
190                 qq|</div>|,
191                 end_form,
192         ;
193 }
194
195
196 print
197         start_html(
198                 -title => $db->{name},
199                 -style => [ @style ],
200         ),
201         h1( $db->{name} ),
202         qq|<div id=description>|, $db->{description}, qq|</div>|,
203 ;
204
205 if ( my $search = param('search') ) {
206
207         $search = unac_string( 'utf-8', Encode::decode('utf-8',$search) );
208
209         print qq|
210                 <a href="#form" class="skip" title="skip to search form">#</a>
211                 <div id="results">
212         |;
213
214         my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
215         $swish->abort_last_error if $swish->Error;
216
217         my @search = (); 
218         my @attrs = param('attr');
219         my $op = param('attr_operator');
220
221         if ( $search =~ m{(=|"|\bAND\b|\bOR\b)} ) {
222                 push @search, $search;
223         } elsif ( @attrs ) {
224
225                 $op ||= 'Q*';
226                 my @or;
227                 foreach my $attr ( @attrs ) {
228                         my $v = $search;
229                         $v =~ s/^\s+//;
230                         warn "-- v: $v\n";
231                         sub rewrite {
232                                 my ( $attr, $whitespace, $v ) = @_;
233                                 warn "## filter $op $whitespace $v\n";
234                                 my $template = $op;
235                                    $template =~ s{Q}{$v};
236                                 $whitespace = " AND " if $whitespace;
237
238                                 # don't return -* &* and other non-word characters
239                                 return '' if $template =~ m/^\W\*$/ || $template =~ m/\band\b/i;
240
241                                 return
242                                         $whitespace .
243                                         $attr . '="' . $template . '"';
244                                         ;
245                         };
246                         if ( $op =~ m{\s} ) {
247                                 my $template = $op;
248                                    $template =~ s{Q}{$v};
249                                 $v = $attr . '="' . $template . '"';
250                         } else {
251                                 $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
252                         }
253
254                         push @or, $v;
255                 
256                 }
257                 push @search, '(' . join(') OR (', @or) . ')';
258
259         } else {
260                 push @search, "all=\"$search\"";
261         }
262
263         my $q = '(' . join(') AND (', @search) . ')';
264
265         my @only_input = param('only_input');
266         $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
267
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 );
272         }
273         my $swish_results = $search_obj->execute( $q );
274
275         dump_yaml( 'swish_results', $swish_results );
276
277         my $pager = Data::Page->new;
278         $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
279         $pager->total_entries( $swish_results->hits );
280
281         dump_yaml( 'pager', $pager );
282
283         $swish_results->seek_result( $pager->first - 1 );
284
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;
289         } else {
290
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;
293
294                 my $pager_html = join("\n", show_pager( $pager ));
295
296                 print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html;
297
298                 my $nr = $pager->first;
299                 print qq|<ol start=$nr>\n|;
300
301                 my $limit = $pager->entries_on_this_page;
302
303                 my $nr = 1;
304
305                 while ( my $result = $swish_results->next_result ) {
306
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} ); };
311                         if ( $@ ) {
312                                 warn "ERROR: $@ from ",dump( $data );
313                                 next;
314                         }
315
316                         my $li_class = '';
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 );";
324                                         if ( $@ ) {
325                                                 warn "disable html markup for $attr: $@";
326                                                 $html_markup_skip->{$attr} = $@;
327                                         }
328                                 } else {
329                                         $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
330                                 }
331                                 my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
332                                 print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|;
333                         }
334                         print qq|</li>\n|;
335
336                         last if $nr++ == $pager->last;
337                 }
338                 print qq|</ol>\n\n|;
339
340                 print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html;
341         }
342         print qq|</div>|;
343
344         dump_yaml( 'pager', $pager );
345
346 }
347
348 print search_form;
349
350 dump_yaml( "config databases $database", $db );
351 dump_yaml( 'html_markup_skip', $html_markup_skip );
352
353 print   end_html;