/[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 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 109  my $db = $config->{databases}->{$databas Line 110  my $db = $config->{databases}->{$databas
110  sub read_config_txt {  sub read_config_txt {
111          my ( $file ) = @_;          my ( $file ) = @_;
112          my $input;          my $input;
113          foreach ( split(/[\n\r]+/, read_file( "$dir/$path/$path-$file.txt" ) ) ) {          my $path ="$dir/$path/$path-$file.txt";
114            if ( ! -e $path ) {
115                    warn "missing $path";
116                    return;
117            }
118            foreach ( split(/[\n\r]+/, read_file( $path ) ) ) {
119                  my ( $val,$label ) = split(/\s*\t\s*/,$_,2);                  my ( $val,$label ) = split(/\s*\t\s*/,$_,2);
120                  push @{ $input->{ '-values' } }, $val;                  push @{ $input->{ '-values' } }, $val;
121                                  $input->{ '-labels' }->{$val} = $label;                                  $input->{ '-labels' }->{$val} = $label;
# Line 120  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 142  my @style = ( '../../style.css' ); Line 151  my @style = ( '../../style.css' );
151  push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";  push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";
152  dump_yaml( 'style', \@style );  dump_yaml( 'style', \@style );
153    
154  print  sub search_form {
155          start_html(          qq|<a name="form"></a>|,
                 -title => $db->{name},  
                 -style => [ @style ],  
         ),  
         h1( $db->{name} ),  
         qq|<div id=description>|, $db->{description}, qq|</div>|,  
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,
180            ;
181    }
182    
 ;  
183    
184  print   end_form;  print
185            start_html(
186                    -title => $db->{name},
187                    -style => [ @style ],
188            ),
189            h1( $db->{name} ),
190            qq|<div id=description>|, $db->{description}, qq|</div>|,
191    ;
192    
193  if ( my $search = param('search') ) {  if ( my $search = param('search') ) {
194    
195          print qq|<div id="results">|;          $search = unac_string( $search );
196    
197            print qq|
198                    <a href="#form" class="skip" title="skip to search form">#</a>
199                    <div id="results">
200            |;
201    
202          my $swish = SWISH::API->new( "$dir/../var/swish/$database" );          my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
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                  $v =~ s{(\s*)(\S+)}{rewrite($1,$2)}ge;                                          $attr . '="' . $template . '"';
229                                            ;
230                  push @search, $v;                          };
231                                    if ( $op =~ m{\s} ) {
232                  my @only_input = param('only_input');                                  my $template = $op;
233                  push @search, '(' . join(') OR (', map { "input=$_" } @only_input) . ')' if @only_input;                                     $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    
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 249  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;
299                          print qq|<li$li_class>|;                          print qq|<li$li_class>|;
300                          foreach my $attr ( @attr ) {                          foreach my $attr ( @attr ) {
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} = $@;
308                                          }                                          }
309                                    } else {
310                                            $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
311                                  }                                  }
312                                  my $label = $attr_labels->{'-labels'}->{$attr} || $attr;                                  my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
313                                  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 326  if ( my $search = param('search') ) {
326    
327  }  }
328    
329    print search_form;
330    
331  dump_yaml( "config databases $database", $db );  dump_yaml( "config databases $database", $db );
332  dump_yaml( 'html_markup_skip', $html_markup_skip );  dump_yaml( 'html_markup_skip', $html_markup_skip );
333    

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

  ViewVC Help
Powered by ViewVC 1.1.26