r1842@llin: dpavlin | 2009-04-25 23:25:15 +0200
[webpac2] / vhost / webpac2.cgi
index d1047da..b0a919e 100755 (executable)
@@ -7,22 +7,26 @@ use CGI qw/:standard/;
 use CGI::Carp qw/fatalsToBrowser/;
 use File::Slurp;
 use YAML;
-use Search::Estraier;
 use Data::Page;
 use Data::Dump qw/dump/;
+use SWISH::API;
+use JSON;
 
 my $range_around = 5;
 my $entries_per_page = 30;
+my $debug = param('debug');
 
-print header;
+print header(
+       -charset => 'utf-8',
+);
 
 sub dump_yaml {
        my $name = shift;
-       print qq|<pre># $name\n|, YAML::Dump( @_ ), qq|</pre>|;
+       print qq|<pre># $name\n|, YAML::Dump( @_ ), qq|</pre>| if $debug;
 }
 
 sub show_pager {
-       my ($pager,$coderef) = @_;
+       my ($pager) = @_;
 
        my @show_pages;
        my $after_current = 0;
@@ -46,18 +50,27 @@ sub show_pager {
 
        my ( $prev, $next ) = ( '&lt;&lt;', '&gt;&gt;' );
 
+       sub li_a_href {
+               my ( $page, $label, $attr ) = @_;
+               param( 'current_page', $page );
+               my $url = self_url( -query => 1 );
+               $attr ||= '';
+               $label ||= $page;
+               qq|<li$attr><a href="$url" title="$page">$label</a></li>|;
+       }
+
        return
-                 $pager->previous_page ? $coderef->( $pager->previous_page, $prev ) : qq|<span class=skip>$prev</span>|
-               , join( ' ', map {
-                       if ( $_ == $pager->current_page ) {
-                               qq|<span class=current_page>$_</span>|;
+                 $pager->previous_page ? li_a_href( $pager->previous_page, $prev ) : qq|<li class=skip>$prev</li>|
+               , ( map {
+                       if ( $_ eq $pager->current_page ) {
+                               qq|<li class=current_page>$_</li>|;
                        } elsif ( $_ eq '' ) {
-                               qq|<span class=skip>...</span>|;
+                               qq|<li class=skip>...</li>|;
                        } else {
-                               $coderef->( $_ );
+                               li_a_href( $_ );
                        }
                } @show_pages )
-               , $pager->next_page ? $coderef->( $pager->next_page, $next ) : qq|<span class=skip>$next</span>|
+               , $pager->next_page ? li_a_href( $pager->next_page, $next ) : qq|<li class=skip>$next</li>|
                ;
                
 }
@@ -80,11 +93,45 @@ if ( -e $html_markup ) {
        undef $html_markup;
 }
 
-my $estraier = YAML::LoadFile( "$dir/../var/estraier/$database.yaml" );
+my $stats;
+{
+       my $path = "$dir/../var/swish/$database.yaml";
+       $stats = YAML::LoadFile( $path );
+       dump_yaml( "stats $path", $stats );
+}
 
 my $db = $config->{databases}->{$database};
 
-my @attr = keys %{ $estraier->{attr} }; # FIXME replace with real gnerated lookup
+my @attr = keys %{ $stats->{attr} }; # FIXME replace with real gnerated lookup
+
+# XXX pipe delimit list!
+my $select_attr_operators = << '__ATTR_OPERATORS__';
+Q*                     |       Bilo koja riječ
+BW Q           |       Početak
+BW Q EW                |       Točan oblik
+__ATTR_OPERATORS__
+
+my $attr_operator;
+
+foreach ( split(/[\n\r]+/, $select_attr_operators ) ) {
+       my ( $operator,$label ) = split(/\s+\|\s+/,$_,2);
+       push @{ $attr_operator->{ '-values' } }, $operator;
+               $attr_operator->{ '-labels' }->{$operator} = $label;
+}
+
+warn "## attr_operator = ", dump( $attr_operator );
+
+my $only_input;
+
+foreach ( @{ $db->{input} } ) {
+       my $input = $_->{name} || die "no name in ",dump( $_ );
+       if ( ! $only_input->{'-labels'}->{$input} ) {
+               push @{ $only_input->{'-values'} }, $input;
+                       $only_input->{'-labels'}->{$input} = $_->{description} || $input;
+       }
+}
+
+warn "## only_input = ", dump( $only_input );
 
 print
        start_html(
@@ -100,9 +147,20 @@ print
 #                      -linebreak => 0,
                ),
                textfield( -name => 'search' ),
+               popup_menu( -name => 'attr_operator', %$attr_operator ),
                submit,
-               textfield( -name => 'entries_per_page', -default => $entries_per_page ),
-               textfield( -name => 'current_page', -default => 1 ),
+               hidden( -name => 'entries_per_page', -default => $entries_per_page ),
+               hidden( -name => 'current_page', -default => 1 ),
+               checkbox( -name => 'debug', -default => 0 ), # FIXME hidden?
+               qq|<div id=inputs>|,
+               h2( 'Select input' ),
+               checkbox_group(
+                       -name => 'only_input',
+                       %$only_input,
+                       -linebreak=> 'true',
+               ),
+               qq|</div>|,
+
 ;
 
 print  end_form;
@@ -111,10 +169,8 @@ if ( my $search = param('search') ) {
 
        print qq|<div id="results">|;
 
-       my $node = Search::Estraier::Node->new(
-               url => $config->{hyperestraier}->{masterurl} . '/node/' . $database,
-               croak_on_error => 1,
-       );
+       my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
+       $swish->abort_last_error if $swish->Error;
 
        param( 'entries_per_page', $entries_per_page ) unless param('entries_per_page'); # FIXME not needed?
        my $pager = Data::Page->new;
@@ -123,61 +179,90 @@ if ( my $search = param('search') ) {
 
        dump_yaml( 'pager', $pager );
 
-       my $cond = Search::Estraier::Condition->new;
-       $cond->set_phrase( $search );
-       $cond->set_skip( $pager->skipped );
-       $cond->set_max(  $pager->entries_per_page );
-       my $nres = $node->search( $cond, 0 );
-       $pager->total_entries( $nres->hits );
+       my @search = (); 
+       if ( $search =~ m{(=|"|AND|OR)} ) {
+               push @search, $search;
+       } elsif ( my $op = param('attr_operator') ) {
+               my $attr = param('attr');
+               my $v = $search;
+               $v =~ s/^\s+//;
+               warn "-- v: $v\n";
+               sub rewrite {
+                       my ( $whitespace, $v ) = @_;
+                       warn "## filter $op $whitespace $v\n";
+                       my $template = $op;
+                          $template =~ s{Q}{$v};
+                       $whitespace = " AND " if $whitespace;
+
+                       return
+                               $whitespace .
+                               $attr . '="' . $template . '"';
+                               ;
+               };
+               $v =~ s{(\s*)(\S+)}{rewrite($1,$2)}ge;
+
+               push @search, $v;
+       
+               my @only_input = param('only_input');
+               push @search, '(' . join(') OR (', map { "input=$_" } @only_input) . ')' if @only_input;
+       } else {
+               push @search, "all=\"$search\"";
+       }
 
+       my $q = '(' . join(') AND (', @search) . ')';
+       $q =~ s{\(\((.+)\)\)}{($1)};
+       warn "# query: $q\n";
+       my $swish_results = $swish->query( $q );
 
-       dump_yaml( 'cond', $cond );
+       dump_yaml( 'swish_results', $swish_results );
 
-       if ( ! $nres ) {
-               my $no_results = "No results for search '%s'";
-               printf qq|<div class="error">$no_results</div>|, $search;
+       $pager->total_entries( $swish_results->hits );
+
+       $swish_results->seek_result( $pager->first );
+
+       if ( ! $pager->total_entries ) {
+               my $no_results = 'No results for search <b>%s</b>';
+               $no_results = $swish->error_string . '<br><b>%s</b>' if $swish->error;
+               printf qq|<div class="error">$no_results</div>\n\n|, $q;
        } else {
 
-               my $results = "%d results for search '%s' showing results %d - %d on page %d";
-               printf qq|<div class="message">$results</div>|, $nres->hits, $search, $pager->first, $pager->last, $pager->current_page;
-
-               my $pager_html =
-                       join(' ', show_pager( $pager,
-                               sub {
-                                       my ($page,$label) = @_;
-                                       param( 'current_page', $page );
-                                       my $url = self_url( -query => 1 );
-                                       $label = $page unless defined $label;
-                                       qq|<a href="$url">$label</a>|;
-                               }
-                       ))
-               ;
+               my $results = "<b>%d</b> results for search <b>%s</b> showing results %d - %d";
+               printf qq|<div class="message">$results</div>\n\n|, $pager->total_entries, $q, $pager->first, $pager->last;
+
+               my $pager_html = join("\n", show_pager( $pager ));
 
-               print qq|<div class="pager top">$pager_html</div>| if $pager_html;
+               print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html;
 
                my $start = $pager->first;
-               print qq|<ol start=$start>|;
+               print qq|<ol start=$start>\n|;
+
+               my $limit = $pager->entries_on_this_page;
+
+               while ( my $result = $swish_results->next_result ) {
+                       last if $limit-- == 0;
+
+                       my $data = from_json $result->property('data');
+
+                       dump_yaml( 'data', $data );
 
-               foreach my $i ( 1 .. $nres->doc_num ) {
-                       my $rdoc = $nres->get_doc( $i - 1 );
                        print qq|<li>|;
                        foreach my $attr ( @attr ) {
-                               my $v = $rdoc->attr( $attr );
-                               if ( defined $v && $html_markup && ! $html_markup_skip->{$attr} ) {
+                               next unless defined $data->{$attr};
+                               my $v = $data->{$attr};
+                               if ( $html_markup && ! $html_markup_skip->{$attr} ) {
                                        eval "\$v = $html_markup->$attr( \$v );";
                                        if ( $@ ) {
                                                warn "disable html markup for $attr: $@";
                                                $html_markup_skip->{$attr} = $@;
                                        }
                                }
-                               next unless defined $v;
                                print qq|<div><label>$attr</label><span class=$attr>$v</span></div>\n|;
                        }
                        print qq|</li>\n|;
                }
-               print qq|</ol>|;
+               print qq|</ol>\n\n|;
 
-               print qq|<div class="pager bottom">$pager_html</div>| if $pager_html;
+               print qq|<ul class="pager bottom">$pager_html</ul>\n\n| if $pager_html;
        }
        print qq|</div>|;
 
@@ -185,8 +270,7 @@ if ( my $search = param('search') ) {
 
 }
 
-dump_yaml( 'estraier', $estraier );
-dump_yaml( 'db', $db );
+dump_yaml( "config databases $database", $db );
 dump_yaml( 'html_markup_skip', $html_markup_skip );
 
 print  end_html;