96cfb859485d37531b830d831a22aa3e3bc55033
[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
15 my $range_around = 5;
16 my @entries_per_page = ( 30, 50, 100, 500 );
17 my $debug = param('debug');
18
19 print header(
20         -charset => 'utf-8',
21 );
22
23 sub dump_yaml {
24         my $name = shift;
25         print qq|<div class=dump><tt>$name</tt><pre>|, YAML::Dump( @_ ), qq|</pre></div>| if $debug;
26 }
27
28 sub show_pager {
29         my ($pager) = @_;
30
31         my @show_pages;
32         my $after_current = 0;
33
34         if ( $pager->current_page <= $range_around + 2 ) {
35                 @show_pages = ( $pager->first_page .. $pager->current_page );
36                 $after_current = $range_around - $pager->current_page;
37         } else {
38                 @show_pages = ( $pager->first_page, '', $pager->current_page - $range_around .. $pager->current_page );
39         }
40
41         if ( $pager->current_page + $after_current + $range_around + 1 >= $pager->last_page ) {
42                 push @show_pages, ( $pager->current_page + 1 .. $pager->last_page );
43         } else {
44                 push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $range_around, '', $pager->last_page );
45         }
46
47 #       dump_yaml( 'show_pages', \@show_pages );
48
49         return '' unless $#show_pages;
50
51         my ( $prev, $next ) = ( '&lt;&lt;', '&gt;&gt;' );
52
53         sub li_a_href {
54                 my ( $page, $label, $attr ) = @_;
55                 param( 'current_page', $page );
56                 my $url = self_url( -query => 1 );
57                 $attr ||= '';
58                 $label ||= $page;
59                 qq|<li$attr><a href="$url" title="$page">$label</a></li>|;
60         }
61
62         return
63                   $pager->previous_page ? li_a_href( $pager->previous_page, $prev ) : qq|<li class=skip>$prev</li>|
64                 , ( map {
65                         if ( $_ eq $pager->current_page ) {
66                                 qq|<li class=current_page>$_</li>|;
67                         } elsif ( $_ eq '' ) {
68                                 qq|<li class=skip>...</li>|;
69                         } else {
70                                 li_a_href( $_ );
71                         }
72                 } @show_pages )
73                 , $pager->next_page ? li_a_href( $pager->next_page, $next ) : qq|<li class=skip>$next</li>|
74                 ;
75                 
76 }
77
78 my $path = $ENV{PATH_INFO} || 'ecas';
79 $path =~ s{^/+}{};
80 $path =~ s{/+$}{};
81 my $dir = $0;
82 $dir =~ s{/[^/]+.cgi}{};
83
84 dump_yaml( 'dir', $dir );
85
86 my $config = YAML::LoadFile( "$dir/$path/config.yml" );
87
88 my $database = (keys %{ $config->{databases} })[0];
89 die "$database not in $path" unless $path =~ m{\Q$database\E};
90
91 my $html_markup = "$dir/$path/html.pm";
92 my $html_markup_skip;
93 if ( -e $html_markup ) {
94         require $html_markup;
95         $html_markup = $database . '::html';
96 } else {
97         undef $html_markup;
98 }
99
100 my $stats;
101 {
102         my $path = "$dir/../var/swish/$database.yaml";
103         $stats = YAML::LoadFile( $path );
104         dump_yaml( "stats $path", $stats );
105 }
106
107 my $db = $config->{databases}->{$database};
108
109 sub read_config_txt {
110         my ( $file ) = @_;
111         my $input;
112         my $path ="$dir/$path/$path-$file.txt";
113         if ( ! -e $path ) {
114                 warn "missing $path";
115                 return;
116         }
117         foreach ( split(/[\n\r]+/, read_file( $path ) ) ) {
118                 my ( $val,$label ) = split(/\s*\t\s*/,$_,2);
119                 push @{ $input->{ '-values' } }, $val;
120                                 $input->{ '-labels' }->{$val} = $label;
121         }
122         return $input;
123 }
124
125 my $attr_labels    = read_config_txt 'labels';
126 my $attr_operators = read_config_txt 'operators';
127
128 my @attr = @{ $attr_labels->{'-values'} } if $attr_labels;
129 @attr = keys %{ $stats->{attr} } unless @attr;
130
131
132 warn dump( $attr_labels, $attr_operators );
133
134 my $only_input;
135 my $inputs_available = 0;
136
137 foreach ( @{ $db->{input} } ) {
138         my $input = $_->{name} || die "no name in ",dump( $_ );
139         next unless defined $stats->{input}->{$input}; # skip inputs without data
140         if ( ! $only_input->{'-labels'}->{$input} ) {
141                 push @{ $only_input->{'-values'} }, $input;
142                         $only_input->{'-labels'}->{$input} = $_->{description} || $input;
143                 $inputs_available++;
144         }
145 }
146
147 warn "## only_input = ", dump( $only_input );
148
149 my @style = ( '../../style.css' );
150 push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";
151 dump_yaml( 'style', \@style );
152
153 sub search_form {
154         qq|<a name="form"></a>|,
155         start_form( -action => self_url( query => 0 ) ),
156                 checkbox_group(
157                         -name => 'attr',
158                         %$attr_labels,
159 #                       -linebreak => 0,
160                 ),
161                 textfield( -name => 'search' ),
162                 $attr_operators ? popup_menu( -name => 'attr_operator', %$attr_operators ) : '',
163                 submit( -value => 'Search' ),
164 #               hidden( -name => 'entries_per_page', -default => $entries_per_page ),
165                 popup_menu( -name => 'entries_per_page', -values => [ @entries_per_page ], -title => 'entries per page' ),
166                 # we need current_page fixed at 1 so that every submit through form will reset it
167                 qq|<input type=hidden name=current_page value=1 >|,
168                 checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?
169                 qq|<div id=inputs>|,
170                 $inputs_available > 1 ?
171                 h2( 'Select input' ) .
172                 checkbox_group(
173                         -name => 'only_input',
174                         %$only_input,
175                         -linebreak=> 'true',
176                 ) : '',
177                 qq|</div>|,
178                 end_form,
179         ;
180 }
181
182
183 print
184         start_html(
185                 -title => $db->{name},
186                 -style => [ @style ],
187         ),
188         h1( $db->{name} ),
189         qq|<div id=description>|, $db->{description}, qq|</div>|,
190 ;
191
192 if ( my $search = param('search') ) {
193
194         print qq|
195                 <a href="#form" class="skip" title="skip to search form">#</a>
196                 <div id="results">
197         |;
198
199         my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
200         $swish->abort_last_error if $swish->Error;
201
202         my @search = (); 
203         my @attrs = param('attr');
204         my $op = param('attr_operator');
205
206         if ( $search =~ m{(=|"|AND|OR)} ) {
207                 push @search, $search;
208         } elsif ( @attrs ) {
209
210                 $op ||= 'Q*';
211                 my @or;
212                 foreach my $attr ( @attrs ) {
213                         my $v = $search;
214                         $v =~ s/^\s+//;
215                         warn "-- v: $v\n";
216                         sub rewrite {
217                                 my ( $attr, $whitespace, $v ) = @_;
218                                 warn "## filter $op $whitespace $v\n";
219                                 my $template = $op;
220                                    $template =~ s{Q}{$v};
221                                 $whitespace = " AND " if $whitespace;
222
223                                 return
224                                         $whitespace .
225                                         $attr . '="' . $template . '"';
226                                         ;
227                         };
228                         if ( $op =~ m{\s} ) {
229                                 my $template = $op;
230                                    $template =~ s{Q}{$v};
231                                 $v = $attr . '="' . $template . '"';
232                         } else {
233                                 $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
234                         }
235
236                         push @or, $v;
237                 
238                 }
239                 push @search, '(' . join(') OR (', @or) . ')';
240
241         } else {
242                 push @search, "all=\"$search\"";
243         }
244
245         my $q = '(' . join(') AND (', @search) . ')';
246
247         my @only_input = param('only_input');
248         $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
249
250         warn "# query: $q\n";
251         my $swish_results = $swish->query( $q );
252
253         dump_yaml( 'swish_results', $swish_results );
254
255         my $pager = Data::Page->new;
256         $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
257         $pager->total_entries( $swish_results->hits );
258
259         dump_yaml( 'pager', $pager );
260
261         $swish_results->seek_result( $pager->first - 1 );
262
263         if ( ! $pager->total_entries ) {
264                 my $no_results = 'No results for search <b>%s</b>';
265                 $no_results = $swish->error_string . '<br><b>%s</b>' if $swish->error;
266                 printf qq|<div class="error">$no_results</div>\n\n|, $q;
267         } else {
268
269                 my $results = "<b>%d</b> results for search <b>%s</b> showing results %d - %d";
270                 printf qq|<div class="message">$results</div>\n\n|, $pager->total_entries, $q, $pager->first, $pager->last;
271
272                 my $pager_html = join("\n", show_pager( $pager ));
273
274                 print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html;
275
276                 my $nr = $pager->first;
277                 print qq|<ol start=$nr>\n|;
278
279                 my $limit = $pager->entries_on_this_page;
280
281                 my $nr = 1;
282
283                 while ( my $result = $swish_results->next_result ) {
284
285                         my $data = from_json( $result->property('data'), {utf8 => 1} );
286
287                         dump_yaml( 'data', $data );
288
289                         my $li_class = '';
290                         $li_class = qq| class="z"| if $nr % 2 == 0;
291                         print qq|<li$li_class>|;
292                         foreach my $attr ( @attr ) {
293                                 next unless defined $data->{$attr};
294                                 my $v = $data->{$attr};
295                                 if ( $html_markup && ! $html_markup_skip->{$attr} ) {
296                                         eval "\$v = $html_markup->$attr( \$v, \$data );";
297                                         if ( $@ ) {
298                                                 warn "disable html markup for $attr: $@";
299                                                 $html_markup_skip->{$attr} = $@;
300                                         }
301                                 } else {
302                                         $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
303                                 }
304                                 my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
305                                 print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|;
306                         }
307                         print qq|</li>\n|;
308
309                         last if $nr++ == $pager->last;
310                 }
311                 print qq|</ol>\n\n|;
312
313                 print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html;
314         }
315         print qq|</div>|;
316
317         dump_yaml( 'pager', $pager );
318
319 }
320
321 print search_form;
322
323 dump_yaml( "config databases $database", $db );
324 dump_yaml( 'html_markup_skip', $html_markup_skip );
325
326 print   end_html;