#!/usr/bin/perl
use warnings;
use strict;
use CGI qw/:standard/;
use CGI::Carp qw/fatalsToBrowser/;
use File::Slurp;
use YAML;
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(
-charset => 'utf-8',
);
sub dump_yaml {
my $name = shift;
print qq|
|,
$inputs_available > 1 ?
h2( 'Select input' ) .
checkbox_group(
-name => 'only_input',
%$only_input,
-linebreak=> 'true',
) : '',
qq|
|,
end_form,
;
}
print
start_html(
-title => $db->{name},
-style => [ @style ],
),
h1( $db->{name} ),
qq|
|;
my $swish = SWISH::API->new( "$dir/../var/swish/$database" );
$swish->abort_last_error if $swish->Error;
my @search = ();
my @attrs = param('attr');
my $op = param('attr_operator');
if ( $search =~ m{(=|"|AND|OR)} ) {
push @search, $search;
} elsif ( @attrs ) {
$op ||= 'Q*';
my @or;
foreach my $attr ( @attrs ) {
my $v = $search;
$v =~ s/^\s+//;
warn "-- v: $v\n";
sub rewrite {
my ( $attr, $whitespace, $v ) = @_;
warn "## filter $op $whitespace $v\n";
my $template = $op;
$template =~ s{Q}{$v};
$whitespace = " AND " if $whitespace;
return
$whitespace .
$attr . '="' . $template . '"';
;
};
if ( $op =~ m{\s} ) {
my $template = $op;
$template =~ s{Q}{$v};
$v = $attr . '="' . $template . '"';
} else {
$v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge;
}
push @or, $v;
}
push @search, '(' . join(') OR (', @or) . ')';
} else {
push @search, "all=\"$search\"";
}
my $q = '(' . join(') AND (', @search) . ')';
my @only_input = param('only_input');
$q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input;
warn "# query: $q\n";
my $swish_results = $swish->query( $q );
dump_yaml( 'swish_results', $swish_results );
my $pager = Data::Page->new;
$pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ );
$pager->total_entries( $swish_results->hits );
dump_yaml( 'pager', $pager );
$swish_results->seek_result( $pager->first - 1 );
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 {
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 ));
print qq|\n\n| if $pager_html;
my $nr = $pager->first;
print qq|
\n|;
my $limit = $pager->entries_on_this_page;
my $nr = 1;
while ( my $result = $swish_results->next_result ) {
my $data = from_json $result->property('data');
dump_yaml( 'data', $data );
my $li_class = '';
$li_class = qq| class="z"| if $nr % 2 == 0;
print qq||;
foreach my $attr ( @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} = $@;
}
} else {
$v =~ s{(http://\S+)}{$1 };
}
my $label = $attr_labels->{'-labels'}->{$attr} || $attr;
print qq|$label $v
\n|;
}
print qq| \n|;
last if $nr++ == $pager->last;
}
print qq| \n\n|;
print qq|\n\n| if $pager_html;
}
print qq|
|;
dump_yaml( 'pager', $pager );
}
print search_form;
dump_yaml( "config databases $database", $db );
dump_yaml( 'html_markup_skip', $html_markup_skip );
print end_html;