/[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 1176 by dpavlin, Wed Apr 29 18:06:15 2009 UTC revision 1250 by dpavlin, Fri Jul 24 19:34:26 2009 UTC
# Line 11  use Data::Page; Line 11  use Data::Page;
11  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
12  use SWISH::API;  use SWISH::API;
13  use JSON;  use JSON;
14    use Text::Unaccent::PurePerl qw/unac_string/;
15    use HTML::FillInForm::Lite;
16    
17  my $range_around = 5;  my $range_around = 5;
18  my $entries_per_page = 30;  my @entries_per_page = ( 30, 50, 100, 500 );
19  my $debug = param('debug');  my $debug = param('debug');
20    
21  print header(  print header(
# Line 109  my $db = $config->{databases}->{$databas Line 111  my $db = $config->{databases}->{$databas
111  sub read_config_txt {  sub read_config_txt {
112          my ( $file ) = @_;          my ( $file ) = @_;
113          my $input;          my $input;
114          foreach ( split(/[\n\r]+/, read_file( "$dir/$path/$path-$file.txt" ) ) ) {          my $path ="$dir/$path/$path-$file.txt";
115            if ( ! -e $path ) {
116                    warn "missing $path";
117                    return;
118            }
119            foreach ( split(/[\n\r]+/, read_file( $path ) ) ) {
120                  my ( $val,$label ) = split(/\s*\t\s*/,$_,2);                  my ( $val,$label ) = split(/\s*\t\s*/,$_,2);
121                  push @{ $input->{ '-values' } }, $val;                  push @{ $input->{ '-values' } }, $val;
122                                  $input->{ '-labels' }->{$val} = $label;                                  $input->{ '-labels' }->{$val} = $label;
# Line 120  sub read_config_txt { Line 127  sub read_config_txt {
127  my $attr_labels    = read_config_txt 'labels';  my $attr_labels    = read_config_txt 'labels';
128  my $attr_operators = read_config_txt 'operators';  my $attr_operators = read_config_txt 'operators';
129    
130  my @attr = @{ $attr_labels->{'-values'} };  my @attr = @{ $attr_labels->{'-values'} } if $attr_labels;
131  @attr = keys %{ $stats->{attr} } unless @attr;  @attr = keys %{ $stats->{attr} } unless @attr;
132    
133    
134  warn dump( $attr_labels, $attr_operators );  warn dump( $attr_labels, $attr_operators );
135    
136  my $only_input;  my $only_input;
137    my $inputs_available = 0;
138    
139  foreach ( @{ $db->{input} } ) {  foreach ( @{ $db->{input} } ) {
140          my $input = $_->{name} || die "no name in ",dump( $_ );          my $input = $_->{name} || die "no name in ",dump( $_ );
141            next unless defined $stats->{input}->{$input}; # skip inputs without data
142          if ( ! $only_input->{'-labels'}->{$input} ) {          if ( ! $only_input->{'-labels'}->{$input} ) {
143                  push @{ $only_input->{'-values'} }, $input;                  push @{ $only_input->{'-values'} }, $input;
144                          $only_input->{'-labels'}->{$input} = $_->{description} || $input;                          $only_input->{'-labels'}->{$input} = $_->{description} || $input;
145                    $inputs_available++;
146          }          }
147  }  }
148    
# Line 142  my @style = ( '../../style.css' ); Line 152  my @style = ( '../../style.css' );
152  push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";  push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";
153  dump_yaml( 'style', \@style );  dump_yaml( 'style', \@style );
154    
155  print  sub search_form {
156          start_html(  
157                  -title => $db->{name},          my $form_html = "$dir/$path/$path-search.html";
158                  -style => [ @style ],          if ( -e $form_html ) {
159          ),                  my $html = read_file( $form_html );
160          h1( $db->{name} ),                  my $q = CGI->new();
161          qq|<div id=description>|, $db->{description}, qq|</div>|,                  my $h = HTML::FillInForm::Lite->new();
162                    return $h->fill(\$html, $q);
163            }
164    
165            qq|<a name="form"></a>|,
166          start_form( -action => self_url( query => 0 ) ),          start_form( -action => self_url( query => 0 ) ),
167                  radio_group(                  checkbox_group(
168                          -name => 'attr',                          -name => 'attr',
169                          %$attr_labels,                          %$attr_labels,
170  #                       -linebreak => 0,  #                       -linebreak => 0,
171                  ),                  ),
172                  textfield( -name => 'search' ),                  textfield( -name => 'search' ),
173                  popup_menu( -name => 'attr_operator', %$attr_operators ),                  $attr_operators ? popup_menu( -name => 'attr_operator', %$attr_operators ) : '',
174                  submit,                  submit( -value => 'Search' ),
175                  hidden( -name => 'entries_per_page', -default => $entries_per_page ),  #               hidden( -name => 'entries_per_page', -default => $entries_per_page ),
176                    popup_menu( -name => 'entries_per_page', -values => [ @entries_per_page ], -title => 'entries per page' ),
177                  # we need current_page fixed at 1 so that every submit through form will reset it                  # we need current_page fixed at 1 so that every submit through form will reset it
178                  qq|<input type=hidden name=current_page value=1 >|,                  qq|<input type=hidden name=current_page value=1 >|,
179                  checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?                  checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?
180                  qq|<div id=inputs>|,                  qq|<div id=inputs>|,
181                  h2( 'Select input' ),                  $inputs_available > 1 ?
182                    h2( 'Select input' ) .
183                  checkbox_group(                  checkbox_group(
184                          -name => 'only_input',                          -name => 'only_input',
185                          %$only_input,                          %$only_input,
186                          -linebreak=> 'true',                          -linebreak=> 'true',
187                  ),                  ) : '',
188                  qq|</div>|,                  qq|</div>|,
189                    end_form,
190            ;
191    }
192    
 ;  
193    
194  print   end_form;  print
195            start_html(
196                    -title => $db->{name},
197                    -style => [ @style ],
198            ),
199            h1( $db->{name} ),
200            qq|<div id=description>|, $db->{description}, qq|</div>|,
201    ;
202    
203  if ( my $search = param('search') ) {  if ( my $search = param('search') ) {
204    
205          print qq|<div id="results">|;          $search = unac_string( $search );
206    
207            print qq|
208                    <a href="#form" class="skip" title="skip to search form">#</a>
209                    <div id="results">
210            |;
211    
212          my $swish = SWISH::API->new( "$dir/../var/swish/$database" );          my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
213          $swish->abort_last_error if $swish->Error;          $swish->abort_last_error if $swish->Error;
214    
215          my @search = ();          my @search = ();
216            my @attrs = param('attr');
217            my $op = param('attr_operator');
218    
219          if ( $search =~ m{(=|"|AND|OR)} ) {          if ( $search =~ m{(=|"|AND|OR)} ) {
220                  push @search, $search;                  push @search, $search;
221          } elsif ( my $op = param('attr_operator') ) {          } elsif ( @attrs ) {
222                  my $attr = param('attr');  
223                  my $v = $search;                  $op ||= 'Q*';
224                  $v =~ s/^\s+//;                  my @or;
225                  warn "-- v: $v\n";                  foreach my $attr ( @attrs ) {
226                  sub rewrite {                          my $v = $search;
227                          my ( $whitespace, $v ) = @_;                          $v =~ s/^\s+//;
228                          warn "## filter $op $whitespace $v\n";                          warn "-- v: $v\n";
229                          my $template = $op;                          sub rewrite {
230                             $template =~ s{Q}{$v};                                  my ( $attr, $whitespace, $v ) = @_;
231                          $whitespace = " AND " if $whitespace;                                  warn "## filter $op $whitespace $v\n";
232                                    my $template = $op;
233                          return                                     $template =~ s{Q}{$v};
234                                  $whitespace .                                  $whitespace = " AND " if $whitespace;
235                                  $attr . '="' . $template . '"';  
236                                  ;                                  return
237                  };                                          $whitespace .
238                  $v =~ s{(\s*)(\S+)}{rewrite($1,$2)}ge;                                          $attr . '="' . $template . '"';
239                                            ;
240                  push @search, $v;                          };
241                                    if ( $op =~ m{\s} ) {
242                  my @only_input = param('only_input');                                  my $template = $op;
243                  push @search, '(' . join(') OR (', map { "input=$_" } @only_input) . ')' if @only_input;                                     $template =~ s{Q}{$v};
244                                    $v = $attr . '="' . $template . '"';
245                            } else {
246                                    $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
247                            }
248    
249                            push @or, $v;
250                    
251                    }
252                    push @search, '(' . join(') OR (', @or) . ')';
253    
254          } else {          } else {
255                  push @search, "all=\"$search\"";                  push @search, "all=\"$search\"";
256          }          }
257    
258          my $q = '(' . join(') AND (', @search) . ')';          my $q = '(' . join(') AND (', @search) . ')';
259          $q =~ s{\(\((.+)\)\)}{($1)};  
260            my @only_input = param('only_input');
261            $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
262    
263          warn "# query: $q\n";          warn "# query: $q\n";
264          my $swish_results = $swish->query( $q );          my $swish_results = $swish->query( $q );
265    
# Line 249  if ( my $search = param('search') ) { Line 295  if ( my $search = param('search') ) {
295    
296                  while ( my $result = $swish_results->next_result ) {                  while ( my $result = $swish_results->next_result ) {
297    
298                          my $data = from_json $result->property('data');                          my $data = $result->property('data');
   
299                          dump_yaml( 'data', $data );                          dump_yaml( 'data', $data );
300                            # FIXME if we produce valid json we shouldn't need eval here!
301                            eval { $data = from_json( $data, {utf8 => 1} ); };
302                            if ( $@ ) {
303                                    warn "ERROR: $@ from ",dump( $data );
304                                    next;
305                            }
306    
307                          my $li_class = '';                          my $li_class = '';
308                          $li_class = qq| class="z"| if $nr++ % 2 == 0;                          $li_class = qq| class="z"| if $nr % 2 == 0;
309                          print qq|<li$li_class>|;                          print qq|<li$li_class>|;
310                          foreach my $attr ( @attr ) {                          foreach my $attr ( @attr ) {
311                                  next unless defined $data->{$attr};                                  next unless defined $data->{$attr};
312                                  my $v = $data->{$attr};                                  my $v = $data->{$attr};
313                                  if ( $html_markup && ! $html_markup_skip->{$attr} ) {                                  if ( $html_markup && ! $html_markup_skip->{$attr} ) {
314                                          eval "\$v = $html_markup->$attr( \$v );";                                          eval "\$v = $html_markup->$attr( \$v, \$data );";
315                                          if ( $@ ) {                                          if ( $@ ) {
316                                                  warn "disable html markup for $attr: $@";                                                  warn "disable html markup for $attr: $@";
317                                                  $html_markup_skip->{$attr} = $@;                                                  $html_markup_skip->{$attr} = $@;
318                                          }                                          }
319                                    } else {
320                                            $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
321                                  }                                  }
322                                  my $label = $attr_labels->{'-labels'}->{$attr} || $attr;                                  my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
323                                  print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|;                                  print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|;
# Line 283  if ( my $search = param('search') ) { Line 336  if ( my $search = param('search') ) {
336    
337  }  }
338    
339    print search_form;
340    
341  dump_yaml( "config databases $database", $db );  dump_yaml( "config databases $database", $db );
342  dump_yaml( 'html_markup_skip', $html_markup_skip );  dump_yaml( 'html_markup_skip', $html_markup_skip );
343    

Legend:
Removed from v.1176  
changed lines
  Added in v.1250

  ViewVC Help
Powered by ViewVC 1.1.26