/[webpac2]/trunk/vhost/webpac2.cgi
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/vhost/webpac2.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1351 - (show annotations)
Fri Oct 22 14:13:16 2010 UTC (13 years, 6 months ago) by dpavlin
File size: 9088 byte(s)
ignore and if written any other way than AND

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{(=|"|\bAND\b|\bOR\b)} ) {
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 # don't return -* &* and other non-word characters
238 return '' if $template =~ m/^\W\*$/ || $template =~ m/\band\b/i;
239
240 return
241 $whitespace .
242 $attr . '="' . $template . '"';
243 ;
244 };
245 if ( $op =~ m{\s} ) {
246 my $template = $op;
247 $template =~ s{Q}{$v};
248 $v = $attr . '="' . $template . '"';
249 } else {
250 $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
251 }
252
253 push @or, $v;
254
255 }
256 push @search, '(' . join(') OR (', @or) . ')';
257
258 } else {
259 push @search, "all=\"$search\"";
260 }
261
262 my $q = '(' . join(') AND (', @search) . ')';
263
264 my @only_input = param('only_input');
265 $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
266
267 warn "# query: $q\n";
268 my $search_obj = $swish->new_search_object;
269 if ( my $sort = param('sort') ) {
270 $search_obj->set_sort( $sort );
271 }
272 my $swish_results = $search_obj->execute( $q );
273
274 dump_yaml( 'swish_results', $swish_results );
275
276 my $pager = Data::Page->new;
277 $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
278 $pager->total_entries( $swish_results->hits );
279
280 dump_yaml( 'pager', $pager );
281
282 $swish_results->seek_result( $pager->first - 1 );
283
284 if ( ! $pager->total_entries ) {
285 my $no_results = 'No results for search <b>%s</b>';
286 $no_results = $swish->error_string . '<br><b>%s</b>' if $swish->error;
287 printf qq|<div class="error">$no_results</div>\n\n|, $q;
288 } else {
289
290 my $results = "<b>%d</b> results for search <b>%s</b> showing results %d - %d";
291 printf qq|<div class="message">$results</div>\n\n|, $pager->total_entries, $q, $pager->first, $pager->last;
292
293 my $pager_html = join("\n", show_pager( $pager ));
294
295 print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html;
296
297 my $nr = $pager->first;
298 print qq|<ol start=$nr>\n|;
299
300 my $limit = $pager->entries_on_this_page;
301
302 my $nr = 1;
303
304 while ( my $result = $swish_results->next_result ) {
305
306 my $data = $result->property('data');
307 dump_yaml( 'data', $data );
308 # FIXME if we produce valid json we shouldn't need eval here!
309 eval { $data = from_json( $data, {utf8 => 1} ); };
310 if ( $@ ) {
311 warn "ERROR: $@ from ",dump( $data );
312 next;
313 }
314
315 my $li_class = '';
316 $li_class = qq| class="z"| if $nr % 2 == 0;
317 print qq|<li$li_class>|;
318 foreach my $attr ( @attr ) {
319 next unless defined $data->{$attr};
320 my $v = $data->{$attr};
321 if ( $html_markup && ! $html_markup_skip->{$attr} ) {
322 eval "\$v = $html_markup->$attr( \$v, \$data );";
323 if ( $@ ) {
324 warn "disable html markup for $attr: $@";
325 $html_markup_skip->{$attr} = $@;
326 }
327 } else {
328 $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
329 }
330 my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
331 print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|;
332 }
333 print qq|</li>\n|;
334
335 last if $nr++ == $pager->last;
336 }
337 print qq|</ol>\n\n|;
338
339 print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html;
340 }
341 print qq|</div>|;
342
343 dump_yaml( 'pager', $pager );
344
345 }
346
347 print search_form;
348
349 dump_yaml( "config databases $database", $db );
350 dump_yaml( 'html_markup_skip', $html_markup_skip );
351
352 print end_html;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26