--- trunk/vhost/webpac2.cgi 2009/04/22 14:27:25 1141 +++ trunk/vhost/webpac2.cgi 2009/04/25 22:07:59 1171 @@ -7,19 +7,22 @@ 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|
# $name\n|, YAML::Dump( @_ ), qq|
| if $debug; + print qq|
$name
|, YAML::Dump( @_ ), qq|
| if $debug; } sub show_pager { @@ -59,7 +62,7 @@ return $pager->previous_page ? li_a_href( $pager->previous_page, $prev ) : qq|| , ( map { - if ( $_ == $pager->current_page ) { + if ( $_ eq $pager->current_page ) { qq|
  • $_
  • |; } elsif ( $_ eq '' ) { qq||; @@ -90,11 +93,45 @@ 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( @@ -110,9 +147,21 @@ # -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 ), + # we need current_page fixed at 1 so that every submit through form will reset it + qq||, + checkbox( -name => 'debug', -default => 0 ), # FIXME hidden? + qq|
    |, + h2( 'Select input' ), + checkbox_group( + -name => 'only_input', + %$only_input, + -linebreak=> 'true', + ), + qq|
    |, + ; print end_form; @@ -121,37 +170,63 @@ print qq|
    |; - 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; - $pager->total_entries( param('current_page') * param('entries_per_page') ); $pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ ); 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 ); - - if ( ! $nres ) { - my $no_results = "No results for search '%s'"; - printf qq|
    $no_results
    \n\n|, $search; - } else { + dump_yaml( 'swish_results', $swish_results ); + + $pager->total_entries( $swish_results->hits ); + + $swish_results->seek_result( $pager->first ); - my $results = "%d results for search '%s' showing results %d - %d on page %d"; - printf qq|
    $results
    \n\n|, $nres->hits, $search, $pager->first, $pager->last, $pager->current_page; + if ( ! $pager->total_entries ) { + my $no_results = 'No results for search %s'; + $no_results = $swish->error_string . '
    %s' if $swish->error; + printf qq|
    $no_results
    \n\n|, $q; + } else { - dump_yaml( 'pager html', show_pager( $pager )); + my $results = "%d results for search %s showing results %d - %d"; + printf qq|
    $results
    \n\n|, $pager->total_entries, $q, $pager->first, $pager->last; my $pager_html = join("\n", show_pager( $pager )); @@ -160,19 +235,26 @@ my $start = $pager->first; print qq|
      \n|; - foreach my $i ( 1 .. $nres->doc_num ) { - my $rdoc = $nres->get_doc( $i - 1 ); + 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 ); + print qq|
    1. |; 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|
      $v
      \n|; } print qq|
    2. \n|; @@ -187,8 +269,7 @@ } -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;