/[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 1219 by dpavlin, Tue Jun 9 21:36:22 2009 UTC revision 1249 by dpavlin, Fri Jul 24 09:29:03 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    
16  my $range_around = 5;  my $range_around = 5;
17  my $entries_per_page = 30;  my @entries_per_page = ( 30, 50, 100, 500 );
18  my $debug = param('debug');  my $debug = param('debug');
19    
20  print header(  print header(
# Line 125  sub read_config_txt { Line 126  sub read_config_txt {
126  my $attr_labels    = read_config_txt 'labels';  my $attr_labels    = read_config_txt 'labels';
127  my $attr_operators = read_config_txt 'operators';  my $attr_operators = read_config_txt 'operators';
128    
129  my @attr = @{ $attr_labels->{'-values'} };  my @attr = @{ $attr_labels->{'-values'} } if $attr_labels;
130  @attr = keys %{ $stats->{attr} } unless @attr;  @attr = keys %{ $stats->{attr} } unless @attr;
131    
132    
133  warn dump( $attr_labels, $attr_operators );  warn dump( $attr_labels, $attr_operators );
134    
135  my $only_input;  my $only_input;
136    my $inputs_available = 0;
137    
138  foreach ( @{ $db->{input} } ) {  foreach ( @{ $db->{input} } ) {
139          my $input = $_->{name} || die "no name in ",dump( $_ );          my $input = $_->{name} || die "no name in ",dump( $_ );
140            next unless defined $stats->{input}->{$input}; # skip inputs without data
141          if ( ! $only_input->{'-labels'}->{$input} ) {          if ( ! $only_input->{'-labels'}->{$input} ) {
142                  push @{ $only_input->{'-values'} }, $input;                  push @{ $only_input->{'-values'} }, $input;
143                          $only_input->{'-labels'}->{$input} = $_->{description} || $input;                          $only_input->{'-labels'}->{$input} = $_->{description} || $input;
144                    $inputs_available++;
145          }          }
146  }  }
147    
# Line 150  dump_yaml( 'style', \@style ); Line 154  dump_yaml( 'style', \@style );
154  sub search_form {  sub search_form {
155          qq|<a name="form"></a>|,          qq|<a name="form"></a>|,
156          start_form( -action => self_url( query => 0 ) ),          start_form( -action => self_url( query => 0 ) ),
157                  radio_group(                  checkbox_group(
158                          -name => 'attr',                          -name => 'attr',
159                          %$attr_labels,                          %$attr_labels,
160  #                       -linebreak => 0,  #                       -linebreak => 0,
161                  ),                  ),
162                  textfield( -name => 'search' ),                  textfield( -name => 'search' ),
163                  popup_menu( -name => 'attr_operator', %$attr_operators ),                  $attr_operators ? popup_menu( -name => 'attr_operator', %$attr_operators ) : '',
164                  submit,                  submit( -value => 'Search' ),
165                  hidden( -name => 'entries_per_page', -default => $entries_per_page ),  #               hidden( -name => 'entries_per_page', -default => $entries_per_page ),
166                    popup_menu( -name => 'entries_per_page', -values => [ @entries_per_page ], -title => 'entries per page' ),
167                  # 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
168                  qq|<input type=hidden name=current_page value=1 >|,                  qq|<input type=hidden name=current_page value=1 >|,
169                  checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?                  checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?
170                  qq|<div id=inputs>|,                  qq|<div id=inputs>|,
171                  h2( 'Select input' ),                  $inputs_available > 1 ?
172                    h2( 'Select input' ) .
173                  checkbox_group(                  checkbox_group(
174                          -name => 'only_input',                          -name => 'only_input',
175                          %$only_input,                          %$only_input,
176                          -linebreak=> 'true',                          -linebreak=> 'true',
177                  ),                  ) : '',
178                  qq|</div>|,                  qq|</div>|,
179                  end_form,                  end_form,
180          ;          ;
# Line 186  print Line 192  print
192    
193  if ( my $search = param('search') ) {  if ( my $search = param('search') ) {
194    
195            $search = unac_string( $search );
196    
197          print qq|          print qq|
198                  <a href="#form" class="skip" title="skip to search form">#</a>                  <a href="#form" class="skip" title="skip to search form">#</a>
199                  <div id="results">                  <div id="results">
# Line 195  if ( my $search = param('search') ) { Line 203  if ( my $search = param('search') ) {
203          $swish->abort_last_error if $swish->Error;          $swish->abort_last_error if $swish->Error;
204    
205          my @search = ();          my @search = ();
206            my @attrs = param('attr');
207            my $op = param('attr_operator');
208    
209          if ( $search =~ m{(=|"|AND|OR)} ) {          if ( $search =~ m{(=|"|AND|OR)} ) {
210                  push @search, $search;                  push @search, $search;
211          } elsif ( my $op = param('attr_operator') ) {          } elsif ( @attrs ) {
212                  my $attr = param('attr');  
213                  my $v = $search;                  $op ||= 'Q*';
214                  $v =~ s/^\s+//;                  my @or;
215                  warn "-- v: $v\n";                  foreach my $attr ( @attrs ) {
216                  sub rewrite {                          my $v = $search;
217                          my ( $whitespace, $v ) = @_;                          $v =~ s/^\s+//;
218                          warn "## filter $op $whitespace $v\n";                          warn "-- v: $v\n";
219                          my $template = $op;                          sub rewrite {
220                             $template =~ s{Q}{$v};                                  my ( $attr, $whitespace, $v ) = @_;
221                          $whitespace = " AND " if $whitespace;                                  warn "## filter $op $whitespace $v\n";
222                                    my $template = $op;
223                          return                                     $template =~ s{Q}{$v};
224                                  $whitespace .                                  $whitespace = " AND " if $whitespace;
225                                  $attr . '="' . $template . '"';  
226                                  ;                                  return
227                  };                                          $whitespace .
228                  if ( $op =~ m{\s} ) {                                          $attr . '="' . $template . '"';
229                          my $template = $op;                                          ;
230                             $template =~ s{Q}{$v};                          };
231                          $v = $attr . '="' . $template . '"';                          if ( $op =~ m{\s} ) {
232                  } else {                                  my $template = $op;
233                          $v =~ s{(\s*)(\S+)}{rewrite($1,$2)}ge;                                     $template =~ s{Q}{$v};
234                                    $v = $attr . '="' . $template . '"';
235                            } else {
236                                    $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
237                            }
238    
239                            push @or, $v;
240                    
241                  }                  }
242                    push @search, '(' . join(') OR (', @or) . ')';
243    
                 push @search, $v;  
           
                 my @only_input = param('only_input');  
                 push @search, '((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;  
244          } else {          } else {
245                  push @search, "all=\"$search\"";                  push @search, "all=\"$search\"";
246          }          }
247    
248          my $q = '(' . join(') AND (', @search) . ')';          my $q = '(' . join(') AND (', @search) . ')';
249          $q =~ s{\(\((.+)\)\)}{($1)};  
250            my @only_input = param('only_input');
251            $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
252    
253          warn "# query: $q\n";          warn "# query: $q\n";
254          my $swish_results = $swish->query( $q );          my $swish_results = $swish->query( $q );
255    
# Line 267  if ( my $search = param('search') ) { Line 285  if ( my $search = param('search') ) {
285    
286                  while ( my $result = $swish_results->next_result ) {                  while ( my $result = $swish_results->next_result ) {
287    
288                          my $data = from_json $result->property('data');                          my $data = $result->property('data');
   
289                          dump_yaml( 'data', $data );                          dump_yaml( 'data', $data );
290                            # FIXME if we produce valid json we shouldn't need eval here!
291                            eval { $data = from_json( $data, {utf8 => 1} ); };
292                            if ( $@ ) {
293                                    warn "ERROR: $@ from ",dump( $data );
294                                    next;
295                            }
296    
297                          my $li_class = '';                          my $li_class = '';
298                          $li_class = qq| class="z"| if $nr % 2 == 0;                          $li_class = qq| class="z"| if $nr % 2 == 0;
# Line 278  if ( my $search = param('search') ) { Line 301  if ( my $search = param('search') ) {
301                                  next unless defined $data->{$attr};                                  next unless defined $data->{$attr};
302                                  my $v = $data->{$attr};                                  my $v = $data->{$attr};
303                                  if ( $html_markup && ! $html_markup_skip->{$attr} ) {                                  if ( $html_markup && ! $html_markup_skip->{$attr} ) {
304                                          eval "\$v = $html_markup->$attr( \$v );";                                          eval "\$v = $html_markup->$attr( \$v, \$data );";
305                                          if ( $@ ) {                                          if ( $@ ) {
306                                                  warn "disable html markup for $attr: $@";                                                  warn "disable html markup for $attr: $@";
307                                                  $html_markup_skip->{$attr} = $@;                                                  $html_markup_skip->{$attr} = $@;

Legend:
Removed from v.1219  
changed lines
  Added in v.1249

  ViewVC Help
Powered by ViewVC 1.1.26