/[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

Diff of /trunk/vhost/webpac2.cgi

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1132 by dpavlin, Tue Apr 21 21:06:31 2009 UTC revision 1138 by dpavlin, Wed Apr 22 10:14:56 2009 UTC
# Line 8  use CGI::Carp qw/fatalsToBrowser/; Line 8  use CGI::Carp qw/fatalsToBrowser/;
8  use File::Slurp;  use File::Slurp;
9  use YAML;  use YAML;
10  use Search::Estraier;  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;  print header;
18    
19  sub dump_yaml {  sub dump_yaml {
20          print qq|<pre>|, YAML::Dump( @_ ), qq|</pre>|;          my $name = shift;
21            print qq|<pre># $name\n|, YAML::Dump( @_ ), qq|</pre>|;
22  }  }
23    
24  my $path = $ENV{PATH_INFO};  sub show_pages {
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 + $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            warn "## show_pages = ",dump( @show_pages );
44    
45            return join( ' ', map {
46                    if ( $_ == $pager->current_page ) {
47                            qq|<b>$_</b>|;
48                    } elsif ( $_ eq '' ) {
49                            qq|...|;
50                    } else {
51                            $coderef->( $_ );
52                    }
53            } @show_pages );
54    }
55    
56    my $path = $ENV{PATH_INFO} || 'ecas';
57  my $dir = $0;  my $dir = $0;
58  $dir =~ s{/[^/]+.cgi}{};  $dir =~ s{/[^/]+.cgi}{};
59    
# Line 24  my $config = YAML::LoadFile( "$dir/$path Line 62  my $config = YAML::LoadFile( "$dir/$path
62  my $database = (keys %{ $config->{databases} })[0];  my $database = (keys %{ $config->{databases} })[0];
63  die "$database not in $path" unless $path =~ m{\Q$database\E};  die "$database not in $path" unless $path =~ m{\Q$database\E};
64    
65    my $html_markup = "$dir/$path/html.pm";
66    my $html_markup_skip;
67    if ( -e $html_markup ) {
68            require $html_markup;
69            $html_markup = $database . '::html';
70    } else {
71            undef $html_markup;
72    }
73    
74  my $estraier = YAML::LoadFile( "$dir/../var/estraier/$database.yaml" );  my $estraier = YAML::LoadFile( "$dir/../var/estraier/$database.yaml" );
75    
76  my $db = $config->{databases}->{$database};  my $db = $config->{databases}->{$database};
# Line 40  print Line 87  print
87          start_form,          start_form,
88                  radio_group(                  radio_group(
89                          -name => 'attr',                          -name => 'attr',
90                          -values => [ 'issn', 'naslov' ],                          -values => [ @attr ],
91  #                       -linebreak => 0,  #                       -linebreak => 0,
92                  ),                  ),
93                  textfield( -name => 'search' ),                  textfield( -name => 'search' ),
94                  submit                  submit,
95                    textfield( -name => 'entries_per_page', -default => $entries_per_page ),
96                    textfield( -name => 'current_page', -default => 1 ),
97  ;  ;
98    
99  print   end_form;  print   end_form;
100    
101  if ( my $search = param('search') ) {  if ( my $search = param('search') ) {
102    
103          print qq|<div id="results">search: $search|;          print qq|<div id="results">|;
104    
105          my $node = Search::Estraier::Node->new(          my $node = Search::Estraier::Node->new(
106                  url => $config->{hyperestraier}->{masterurl} . '/node/' . $database,                  url => $config->{hyperestraier}->{masterurl} . '/node/' . $database,
107                  croak_on_error => 1,                  croak_on_error => 1,
108          );          );
109    
110            param( 'entries_per_page', $entries_per_page ) unless param('entries_per_page'); # FIXME not needed?
111    
112          my $cond = Search::Estraier::Condition->new;          my $cond = Search::Estraier::Condition->new;
113          $cond->set_phrase( $search );          $cond->set_phrase( $search );
114            $cond->set_skip( param('current_page') );
115            $cond->set_max( param('entries_per_page') );
116          my $nres = $node->search( $cond, 0 );          my $nres = $node->search( $cond, 0 );
117    
118            my $pager = Data::Page->new( $nres->hits, param('entries_per_page'), param('current_page') );
119    
120          if ( ! $nres ) {          if ( ! $nres ) {
121                  my $no_results = "No results for search '%s'";                  my $no_results = "No results for search '%s'";
122                  printf qq|<div class="error">$no_results</a>|, $search;                  printf qq|<div class="error">$no_results</div>|, $search;
123          } else {          } else {
124                  print qq|<ul>|;  
125                    my $results = "Got %d results for search '%s'";
126                    printf qq|<div class="message">$results</div>|, $nres->hits, $search;
127    
128                    print
129                            qq|<div class=pager>|,
130                            show_pages( $pager,
131                                    sub {
132                                            my ($page) = @_;
133                                            param( 'current_page', $page );
134                                            my $url = self_url( -query => 1 );
135                                            qq|<a href="$url">$_</a>|;
136                                    }
137                            ),
138                            qq|</div>|
139                    ;
140    
141                    my $start = $pager->first;
142                    print qq|<ol start=$start>|;
143    
144                  foreach my $i ( 1 .. $nres->doc_num ) {                  foreach my $i ( 1 .. $nres->doc_num ) {
145                          my $rdoc = $nres->get_doc( $i - 1 );                          my $rdoc = $nres->get_doc( $i - 1 );
146                          print qq|<li>|;                          print qq|<li>|;
147                          print qq|<div><label>$_</label><span class=$_>|, $rdoc->attr( $_ ), qq|</span></div>|                          foreach my $attr ( @attr ) {
148                                  foreach @attr;                                  my $v = $rdoc->attr( $attr );
149                                    if ( defined $v && $html_markup && ! $html_markup_skip->{$attr} ) {
150                                            eval "\$v = $html_markup->$attr( \$v );";
151                                            if ( $@ ) {
152                                                    warn "disable html markup for $attr: $@";
153                                                    $html_markup_skip->{$attr} = $@;
154                                            }
155                                    }
156                                    next unless defined $v;
157                                    print qq|<div><label>$attr</label><span class=$attr>$v</span></div>\n|;
158                            }
159                          print qq|</li>\n|;                          print qq|</li>\n|;
160                  }                  }
161                  print qq|</ul>|;                  print qq|</ol>|;
162          }          }
163          print qq|</div>|;          print qq|</div>|;
164    
165            dump_yaml( 'pager', $pager );
166    
167  }  }
168    
169  dump_yaml( $estraier );  dump_yaml( 'estraier', $estraier );
170  dump_yaml( $db );  dump_yaml( 'db', $db );
171    dump_yaml( 'html_markup_skip', $html_markup_skip );
172    
173  print   end_html;  print   end_html;

Legend:
Removed from v.1132  
changed lines
  Added in v.1138

  ViewVC Help
Powered by ViewVC 1.1.26