/[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 1242 by dpavlin, Sun Jul 12 18:18:57 2009 UTC
# Line 13  use SWISH::API; Line 13  use SWISH::API;
13  use JSON;  use JSON;
14    
15  my $range_around = 5;  my $range_around = 5;
16  my $entries_per_page = 30;  my @entries_per_page = ( 30, 50, 100, 500 );
17  my $debug = param('debug');  my $debug = param('debug');
18    
19  print header(  print header(
# Line 109  my $db = $config->{databases}->{$databas Line 109  my $db = $config->{databases}->{$databas
109  sub read_config_txt {  sub read_config_txt {
110          my ( $file ) = @_;          my ( $file ) = @_;
111          my $input;          my $input;
112          foreach ( split(/[\n\r]+/, read_file( "$dir/$path/$path-$file.txt" ) ) ) {          my $path ="$dir/$path/$path-$file.txt";
113            if ( ! -e $path ) {
114                    warn "missing $path";
115                    return;
116            }
117            foreach ( split(/[\n\r]+/, read_file( $path ) ) ) {
118                  my ( $val,$label ) = split(/\s*\t\s*/,$_,2);                  my ( $val,$label ) = split(/\s*\t\s*/,$_,2);
119                  push @{ $input->{ '-values' } }, $val;                  push @{ $input->{ '-values' } }, $val;
120                                  $input->{ '-labels' }->{$val} = $label;                                  $input->{ '-labels' }->{$val} = $label;
# Line 120  sub read_config_txt { Line 125  sub read_config_txt {
125  my $attr_labels    = read_config_txt 'labels';  my $attr_labels    = read_config_txt 'labels';
126  my $attr_operators = read_config_txt 'operators';  my $attr_operators = read_config_txt 'operators';
127    
128  my @attr = @{ $attr_labels->{'-values'} };  my @attr = @{ $attr_labels->{'-values'} } if $attr_labels;
129  @attr = keys %{ $stats->{attr} } unless @attr;  @attr = keys %{ $stats->{attr} } unless @attr;
130    
131    
132  warn dump( $attr_labels, $attr_operators );  warn dump( $attr_labels, $attr_operators );
133    
134  my $only_input;  my $only_input;
135    my $inputs_available = 0;
136    
137  foreach ( @{ $db->{input} } ) {  foreach ( @{ $db->{input} } ) {
138          my $input = $_->{name} || die "no name in ",dump( $_ );          my $input = $_->{name} || die "no name in ",dump( $_ );
139            next unless defined $stats->{input}->{$input}; # skip inputs without data
140          if ( ! $only_input->{'-labels'}->{$input} ) {          if ( ! $only_input->{'-labels'}->{$input} ) {
141                  push @{ $only_input->{'-values'} }, $input;                  push @{ $only_input->{'-values'} }, $input;
142                          $only_input->{'-labels'}->{$input} = $_->{description} || $input;                          $only_input->{'-labels'}->{$input} = $_->{description} || $input;
143                    $inputs_available++;
144          }          }
145  }  }
146    
# Line 142  my @style = ( '../../style.css' ); Line 150  my @style = ( '../../style.css' );
150  push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";  push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css";
151  dump_yaml( 'style', \@style );  dump_yaml( 'style', \@style );
152    
153  print  sub search_form {
154          start_html(          qq|<a name="form"></a>|,
                 -title => $db->{name},  
                 -style => [ @style ],  
         ),  
         h1( $db->{name} ),  
         qq|<div id=description>|, $db->{description}, qq|</div>|,  
155          start_form( -action => self_url( query => 0 ) ),          start_form( -action => self_url( query => 0 ) ),
156                  radio_group(                  checkbox_group(
157                          -name => 'attr',                          -name => 'attr',
158                          %$attr_labels,                          %$attr_labels,
159  #                       -linebreak => 0,  #                       -linebreak => 0,
160                  ),                  ),
161                  textfield( -name => 'search' ),                  textfield( -name => 'search' ),
162                  popup_menu( -name => 'attr_operator', %$attr_operators ),                  $attr_operators ? popup_menu( -name => 'attr_operator', %$attr_operators ) : '',
163                  submit,                  submit( -value => 'Search' ),
164                  hidden( -name => 'entries_per_page', -default => $entries_per_page ),  #               hidden( -name => 'entries_per_page', -default => $entries_per_page ),
165                    popup_menu( -name => 'entries_per_page', -values => [ @entries_per_page ], -title => 'entries per page' ),
166                  # 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
167                  qq|<input type=hidden name=current_page value=1 >|,                  qq|<input type=hidden name=current_page value=1 >|,
168                  checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?                  checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?
169                  qq|<div id=inputs>|,                  qq|<div id=inputs>|,
170                  h2( 'Select input' ),                  $inputs_available > 1 ?
171                    h2( 'Select input' ) .
172                  checkbox_group(                  checkbox_group(
173                          -name => 'only_input',                          -name => 'only_input',
174                          %$only_input,                          %$only_input,
175                          -linebreak=> 'true',                          -linebreak=> 'true',
176                  ),                  ) : '',
177                  qq|</div>|,                  qq|</div>|,
178                    end_form,
179            ;
180    }
181    
 ;  
182    
183  print   end_form;  print
184            start_html(
185                    -title => $db->{name},
186                    -style => [ @style ],
187            ),
188            h1( $db->{name} ),
189            qq|<div id=description>|, $db->{description}, qq|</div>|,
190    ;
191    
192  if ( my $search = param('search') ) {  if ( my $search = param('search') ) {
193    
194          print qq|<div id="results">|;          print qq|
195                    <a href="#form" class="skip" title="skip to search form">#</a>
196                    <div id="results">
197            |;
198    
199          my $swish = SWISH::API->new( "$dir/../var/swish/$database" );          my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
200          $swish->abort_last_error if $swish->Error;          $swish->abort_last_error if $swish->Error;
201    
202          my @search = ();          my @search = ();
203            my @attrs = param('attr');
204            my $op = param('attr_operator');
205    
206          if ( $search =~ m{(=|"|AND|OR)} ) {          if ( $search =~ m{(=|"|AND|OR)} ) {
207                  push @search, $search;                  push @search, $search;
208          } elsif ( my $op = param('attr_operator') ) {          } elsif ( @attrs ) {
209                  my $attr = param('attr');  
210                  my $v = $search;                  $op ||= 'Q*';
211                  $v =~ s/^\s+//;                  my @or;
212                  warn "-- v: $v\n";                  foreach my $attr ( @attrs ) {
213                  sub rewrite {                          my $v = $search;
214                          my ( $whitespace, $v ) = @_;                          $v =~ s/^\s+//;
215                          warn "## filter $op $whitespace $v\n";                          warn "-- v: $v\n";
216                          my $template = $op;                          sub rewrite {
217                             $template =~ s{Q}{$v};                                  my ( $attr, $whitespace, $v ) = @_;
218                          $whitespace = " AND " if $whitespace;                                  warn "## filter $op $whitespace $v\n";
219                                    my $template = $op;
220                          return                                     $template =~ s{Q}{$v};
221                                  $whitespace .                                  $whitespace = " AND " if $whitespace;
222                                  $attr . '="' . $template . '"';  
223                                  ;                                  return
224                  };                                          $whitespace .
225                  $v =~ s{(\s*)(\S+)}{rewrite($1,$2)}ge;                                          $attr . '="' . $template . '"';
226                                            ;
227                  push @search, $v;                          };
228                                    if ( $op =~ m{\s} ) {
229                  my @only_input = param('only_input');                                  my $template = $op;
230                  push @search, '(' . join(') OR (', map { "input=$_" } @only_input) . ')' if @only_input;                                     $template =~ s{Q}{$v};
231                                    $v = $attr . '="' . $template . '"';
232                            } else {
233                                    $v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
234                            }
235    
236                            push @or, $v;
237                    
238                    }
239                    push @search, '(' . join(') OR (', @or) . ')';
240    
241          } else {          } else {
242                  push @search, "all=\"$search\"";                  push @search, "all=\"$search\"";
243          }          }
244    
245          my $q = '(' . join(') AND (', @search) . ')';          my $q = '(' . join(') AND (', @search) . ')';
246          $q =~ s{\(\((.+)\)\)}{($1)};  
247            my @only_input = param('only_input');
248            $q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
249    
250          warn "# query: $q\n";          warn "# query: $q\n";
251          my $swish_results = $swish->query( $q );          my $swish_results = $swish->query( $q );
252    
# Line 245  if ( my $search = param('search') ) { Line 278  if ( my $search = param('search') ) {
278    
279                  my $limit = $pager->entries_on_this_page;                  my $limit = $pager->entries_on_this_page;
280    
281                    my $nr = 1;
282    
283                  while ( my $result = $swish_results->next_result ) {                  while ( my $result = $swish_results->next_result ) {
284    
285                          my $data = from_json $result->property('data');                          my $data = from_json( $result->property('data'), {utf8 => 1} );
286    
287                          dump_yaml( 'data', $data );                          dump_yaml( 'data', $data );
288    
289                          print qq|<li>|;                          my $li_class = '';
290                            $li_class = qq| class="z"| if $nr % 2 == 0;
291                            print qq|<li$li_class>|;
292                          foreach my $attr ( @attr ) {                          foreach my $attr ( @attr ) {
293                                  next unless defined $data->{$attr};                                  next unless defined $data->{$attr};
294                                  my $v = $data->{$attr};                                  my $v = $data->{$attr};
295                                  if ( $html_markup && ! $html_markup_skip->{$attr} ) {                                  if ( $html_markup && ! $html_markup_skip->{$attr} ) {
296                                          eval "\$v = $html_markup->$attr( \$v );";                                          eval "\$v = $html_markup->$attr( \$v, \$data );";
297                                          if ( $@ ) {                                          if ( $@ ) {
298                                                  warn "disable html markup for $attr: $@";                                                  warn "disable html markup for $attr: $@";
299                                                  $html_markup_skip->{$attr} = $@;                                                  $html_markup_skip->{$attr} = $@;
300                                          }                                          }
301                                    } else {
302                                            $v =~ s{(http://\S+)}{<a href="$1">$1</a>};
303                                  }                                  }
304                                  my $label = $attr_labels->{'-labels'}->{$attr} || $attr;                                  my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
305                                  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 318  if ( my $search = param('search') ) {
318    
319  }  }
320    
321    print search_form;
322    
323  dump_yaml( "config databases $database", $db );  dump_yaml( "config databases $database", $db );
324  dump_yaml( 'html_markup_skip', $html_markup_skip );  dump_yaml( 'html_markup_skip', $html_markup_skip );
325    

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

  ViewVC Help
Powered by ViewVC 1.1.26