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

Legend:
Removed from v.1175  
changed lines
  Added in v.1328

  ViewVC Help
Powered by ViewVC 1.1.26