/[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 1138 - (show annotations)
Wed Apr 22 10:14:56 2009 UTC (15 years ago) by dpavlin
File size: 4238 byte(s)
 r1784@llin:  dpavlin | 2009-04-22 12:14:51 +0200
 implement pager using Data::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 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_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;
58 $dir =~ s{/[^/]+.cgi}{};
59
60 my $config = YAML::LoadFile( "$dir/$path/config.yml" );
61
62 my $database = (keys %{ $config->{databases} })[0];
63 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" );
75
76 my $db = $config->{databases}->{$database};
77
78 my @attr = keys %{ $estraier->{attr} }; # FIXME replace with real gnerated lookup
79
80 print
81 start_html(
82 -title => $db->{name},
83 -style => '../../style.css',
84 ),
85 h1( $db->{name} ),
86 qq|<div id=description>|, $db->{description}, qq|</div>|,
87 start_form,
88 radio_group(
89 -name => 'attr',
90 -values => [ @attr ],
91 # -linebreak => 0,
92 ),
93 textfield( -name => 'search' ),
94 submit,
95 textfield( -name => 'entries_per_page', -default => $entries_per_page ),
96 textfield( -name => 'current_page', -default => 1 ),
97 ;
98
99 print end_form;
100
101 if ( my $search = param('search') ) {
102
103 print qq|<div id="results">|;
104
105 my $node = Search::Estraier::Node->new(
106 url => $config->{hyperestraier}->{masterurl} . '/node/' . $database,
107 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;
113 $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 );
117
118 my $pager = Data::Page->new( $nres->hits, param('entries_per_page'), param('current_page') );
119
120 if ( ! $nres ) {
121 my $no_results = "No results for search '%s'";
122 printf qq|<div class="error">$no_results</div>|, $search;
123 } else {
124
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 ) {
145 my $rdoc = $nres->get_doc( $i - 1 );
146 print qq|<li>|;
147 foreach my $attr ( @attr ) {
148 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|;
160 }
161 print qq|</ol>|;
162 }
163 print qq|</div>|;
164
165 dump_yaml( 'pager', $pager );
166
167 }
168
169 dump_yaml( 'estraier', $estraier );
170 dump_yaml( 'db', $db );
171 dump_yaml( 'html_markup_skip', $html_markup_skip );
172
173 print end_html;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26