r1786@llin: dpavlin | 2009-04-22 13:47:56 +0200
[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 Search::Estraier;
11 use Data::Page;
12 use Data::Dump qw/dump/;
13
14 my $range_around = 5;
15 my $entries_per_page = 30;
16
17 print header;
18
19 sub dump_yaml {
20         my $name = shift;
21         print qq|<pre># $name\n|, YAML::Dump( @_ ), qq|</pre>|;
22 }
23
24 sub show_pager {
25         my ($pager,$coderef) = @_;
26
27         my @show_pages;
28         my $after_current = 0;
29
30         if ( $pager->current_page <= $range_around + 2 ) {
31                 @show_pages = ( $pager->first_page .. $pager->current_page );
32                 $after_current = $range_around - $pager->current_page;
33         } else {
34                 @show_pages = ( $pager->first_page, '', $pager->current_page - $range_around .. $pager->current_page );
35         }
36
37         if ( $pager->current_page + $after_current + $range_around + 1 >= $pager->last_page ) {
38                 push @show_pages, ( $pager->current_page + 1 .. $pager->last_page );
39         } else {
40                 push @show_pages, ( $pager->current_page + 1 .. $pager->current_page + $after_current + $range_around, '', $pager->last_page );
41         }
42
43         dump_yaml( 'show_pages', \@show_pages );
44
45         return '' unless $#show_pages;
46
47         my ( $prev, $next ) = ( '&lt;&lt;', '&gt;&gt;' );
48
49         return
50                   $pager->previous_page ? $coderef->( $pager->previous_page, $prev ) : $prev
51                 , join( ' ', map {
52                         if ( $_ == $pager->current_page ) {
53                                 qq|<span class=current_page>$_</span>|;
54                         } elsif ( $_ eq '' ) {
55                                 qq|<span class=skip>...</span>|;
56                         } else {
57                                 $coderef->( $_ );
58                         }
59                 } @show_pages )
60                 , $pager->next_page ? $coderef->( $pager->next_page, $next ) : $next
61                 ;
62                 
63 }
64
65 my $path = $ENV{PATH_INFO} || 'ecas';
66 my $dir = $0;
67 $dir =~ s{/[^/]+.cgi}{};
68
69 my $config = YAML::LoadFile( "$dir/$path/config.yml" );
70
71 my $database = (keys %{ $config->{databases} })[0];
72 die "$database not in $path" unless $path =~ m{\Q$database\E};
73
74 my $html_markup = "$dir/$path/html.pm";
75 my $html_markup_skip;
76 if ( -e $html_markup ) {
77         require $html_markup;
78         $html_markup = $database . '::html';
79 } else {
80         undef $html_markup;
81 }
82
83 my $estraier = YAML::LoadFile( "$dir/../var/estraier/$database.yaml" );
84
85 my $db = $config->{databases}->{$database};
86
87 my @attr = keys %{ $estraier->{attr} }; # FIXME replace with real gnerated lookup
88
89 print
90         start_html(
91                 -title => $db->{name},
92                 -style => '../../style.css',
93         ),
94         h1( $db->{name} ),
95         qq|<div id=description>|, $db->{description}, qq|</div>|,
96         start_form( -action => self_url( query => 0 ) ),
97                 radio_group(
98                         -name => 'attr',
99                         -values => [ @attr ],
100 #                       -linebreak => 0,
101                 ),
102                 textfield( -name => 'search' ),
103                 submit,
104                 textfield( -name => 'entries_per_page', -default => $entries_per_page ),
105                 textfield( -name => 'current_page', -default => 1 ),
106 ;
107
108 print   end_form;
109
110 if ( my $search = param('search') ) {
111
112         print qq|<div id="results">|;
113
114         my $node = Search::Estraier::Node->new(
115                 url => $config->{hyperestraier}->{masterurl} . '/node/' . $database,
116                 croak_on_error => 1,
117         );
118
119         param( 'entries_per_page', $entries_per_page ) unless param('entries_per_page'); # FIXME not needed?
120         my $pager = Data::Page->new;
121         $pager->total_entries( param('current_page') * param('entries_per_page') );
122         $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
123
124         dump_yaml( 'pager', $pager );
125
126         my $cond = Search::Estraier::Condition->new;
127         $cond->set_phrase( $search );
128         $cond->set_skip( $pager->skipped );
129         $cond->set_max(  $pager->entries_per_page );
130         my $nres = $node->search( $cond, 0 );
131         $pager->total_entries( $nres->hits );
132
133         dump_yaml( 'cond', $cond );
134
135         if ( ! $nres ) {
136                 my $no_results = "No results for search '%s'";
137                 printf qq|<div class="error">$no_results</div>|, $search;
138         } else {
139
140                 my $results = "Got %d results for search '%s'";
141                 printf qq|<div class="message">$results</div>|, $nres->hits, $search;
142
143                 print
144                         qq|<div class=pager>|,
145                         join(' ', show_pager( $pager,
146                                 sub {
147                                         my ($page,$label) = @_;
148                                         param( 'current_page', $page );
149                                         my $url = self_url( -query => 1 );
150                                         $label = $page unless defined $label;
151                                         qq|<a href="$url">$label</a>|;
152                                 }
153                         )),
154                         qq|</div>|
155                 ;
156
157                 my $start = $pager->first;
158                 print qq|<ol start=$start>|;
159
160                 foreach my $i ( 1 .. $nres->doc_num ) {
161                         my $rdoc = $nres->get_doc( $i - 1 );
162                         print qq|<li>|;
163                         foreach my $attr ( @attr ) {
164                                 my $v = $rdoc->attr( $attr );
165                                 if ( defined $v && $html_markup && ! $html_markup_skip->{$attr} ) {
166                                         eval "\$v = $html_markup->$attr( \$v );";
167                                         if ( $@ ) {
168                                                 warn "disable html markup for $attr: $@";
169                                                 $html_markup_skip->{$attr} = $@;
170                                         }
171                                 }
172                                 next unless defined $v;
173                                 print qq|<div><label>$attr</label><span class=$attr>$v</span></div>\n|;
174                         }
175                         print qq|</li>\n|;
176                 }
177                 print qq|</ol>|;
178         }
179         print qq|</div>|;
180
181         dump_yaml( 'pager', $pager );
182
183 }
184
185 dump_yaml( 'estraier', $estraier );
186 dump_yaml( 'db', $db );
187 dump_yaml( 'html_markup_skip', $html_markup_skip );
188
189 print   end_html;