/[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 1240 - (show annotations)
Sun Jul 12 17:23:47 2009 UTC (14 years, 9 months ago) by dpavlin
File size: 8220 byte(s)
entries per page

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 if ( ! $only_input->{'-labels'}->{$input} ) {
140 push @{ $only_input->{'-values'} }, $input;
141 $only_input->{'-labels'}->{$input} = $_->{description} || $input;
142 $inputs_available++;
143 }
144 }
145
146 warn "## only_input = ", dump( $only_input );
147
148 my @style = ( '../../style.css' );
149 push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";
150 dump_yaml( 'style', \@style );
151
152 sub search_form {
153 qq|<a name="form"></a>|,
154 start_form( -action => self_url( query => 0 ) ),
155 checkbox_group(
156 -name => 'attr',
157 %$attr_labels,
158 # -linebreak => 0,
159 ),
160 textfield( -name => 'search' ),
161 $attr_operators ? popup_menu( -name => 'attr_operator', %$attr_operators ) : '',
162 submit( -value => 'Search' ),
163 # hidden( -name => 'entries_per_page', -default => $entries_per_page ),
164 popup_menu( -name => 'entries_per_page', -values => [ @entries_per_page ], -title => 'entries per page' ),
165 # we need current_page fixed at 1 so that every submit through form will reset it
166 qq|<input type=hidden name=current_page value=1 >|,
167 checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?
168 qq|<div id=inputs>|,
169 $inputs_available > 1 ?
170 h2( 'Select input' ) .
171 checkbox_group(
172 -name => 'only_input',
173 %$only_input,
174 -linebreak=> 'true',
175 ) : '',
176 qq|</div>|,
177 end_form,
178 ;
179 }
180
181
182 print
183 start_html(
184 -title => $db->{name},
185 -style => [ @style ],
186 ),
187 h1( $db->{name} ),
188 qq|<div id=description>|, $db->{description}, qq|</div>|,
189 ;
190
191 if ( my $search = param('search') ) {
192
193 print qq|
194 <a href="#form" class="skip" title="skip to search form">#</a>
195 <div id="results">
196 |;
197
198 my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
199 $swish->abort_last_error if $swish->Error;
200
201 my @search = ();
202 my @attrs = param('attr');
203 my $op = param('attr_operator');
204
205 if ( $search =~ m{(=|"|AND|OR)} ) {
206 push @search, $search;
207 } elsif ( @attrs ) {
208
209 $op ||= 'Q*';
210 my @or;
211 foreach my $attr ( @attrs ) {
212 my $v = $search;
213 $v =~ s/^\s+//;
214 warn "-- v: $v\n";
215 sub rewrite {
216 my ( $attr, $whitespace, $v ) = @_;
217 warn "## filter $op $whitespace $v\n";
218 my $template = $op;
219 $template =~ s{Q}{$v};
220 $whitespace = " AND " if $whitespace;
221
222 return
223 $whitespace .
224 $attr . '="' . $template . '"';
225 ;
226 };
227 if ( $op =~ m{\s} ) {
228 my $template = $op;
229 $template =~ s{Q}{$v};
230 $v = $attr . '="' . $template . '"';
231 } else {
232 $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
233 }
234
235 push @or, $v;
236
237 }
238 push @search, '(' . join(') OR (', @or) . ')';
239
240 } else {
241 push @search, "all=\"$search\"";
242 }
243
244 my $q = '(' . join(') AND (', @search) . ')';
245
246 my @only_input = param('only_input');
247 $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
248
249 warn "# query: $q\n";
250 my $swish_results = $swish->query( $q );
251
252 dump_yaml( 'swish_results', $swish_results );
253
254 my $pager = Data::Page->new;
255 $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
256 $pager->total_entries( $swish_results->hits );
257
258 dump_yaml( 'pager', $pager );
259
260 $swish_results->seek_result( $pager->first - 1 );
261
262 if ( ! $pager->total_entries ) {
263 my $no_results = 'No results for search <b>%s</b>';
264 $no_results = $swish->error_string . '<br><b>%s</b>' if $swish->error;
265 printf qq|<div class="error">$no_results</div>\n\n|, $q;
266 } else {
267
268 my $results = "<b>%d</b> results for search <b>%s</b> showing results %d - %d";
269 printf qq|<div class="message">$results</div>\n\n|, $pager->total_entries, $q, $pager->first, $pager->last;
270
271 my $pager_html = join("\n", show_pager( $pager ));
272
273 print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html;
274
275 my $nr = $pager->first;
276 print qq|<ol start=$nr>\n|;
277
278 my $limit = $pager->entries_on_this_page;
279
280 my $nr = 1;
281
282 while ( my $result = $swish_results->next_result ) {
283
284 my $data = from_json $result->property('data');
285
286 dump_yaml( 'data', $data );
287
288 my $li_class = '';
289 $li_class = qq| class="z"| if $nr % 2 == 0;
290 print qq|<li$li_class>|;
291 foreach my $attr ( @attr ) {
292 next unless defined $data->{$attr};
293 my $v = $data->{$attr};
294 if ( $html_markup && ! $html_markup_skip->{$attr} ) {
295 eval "\$v = $html_markup->$attr( \$v, \$data );";
296 if ( $@ ) {
297 warn "disable html markup for $attr: $@";
298 $html_markup_skip->{$attr} = $@;
299 }
300 } else {
301 $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
302 }
303 my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
304 print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|;
305 }
306 print qq|</li>\n|;
307
308 last if $nr++ == $pager->last;
309 }
310 print qq|</ol>\n\n|;
311
312 print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html;
313 }
314 print qq|</div>|;
315
316 dump_yaml( 'pager', $pager );
317
318 }
319
320 print search_form;
321
322 dump_yaml( "config databases $database", $db );
323 dump_yaml( 'html_markup_skip', $html_markup_skip );
324
325 print end_html;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26