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( |
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; |
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 |
if ( ! $only_input->{'-labels'}->{$input} ) { |
if ( ! $only_input->{'-labels'}->{$input} ) { |
140 |
push @{ $only_input->{'-values'} }, $input; |
push @{ $only_input->{'-values'} }, $input; |
141 |
$only_input->{'-labels'}->{$input} = $_->{description} || $input; |
$only_input->{'-labels'}->{$input} = $_->{description} || $input; |
142 |
|
$inputs_available++; |
143 |
} |
} |
144 |
} |
} |
145 |
|
|
149 |
push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css"; |
push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css"; |
150 |
dump_yaml( 'style', \@style ); |
dump_yaml( 'style', \@style ); |
151 |
|
|
152 |
print |
sub search_form { |
153 |
start_html( |
qq|<a name="form"></a>|, |
|
-title => $db->{name}, |
|
|
-style => [ @style ], |
|
|
), |
|
|
h1( $db->{name} ), |
|
|
qq|<div id=description>|, $db->{description}, qq|</div>|, |
|
154 |
start_form( -action => self_url( query => 0 ) ), |
start_form( -action => self_url( query => 0 ) ), |
155 |
radio_group( |
checkbox_group( |
156 |
-name => 'attr', |
-name => 'attr', |
157 |
%$attr_labels, |
%$attr_labels, |
158 |
# -linebreak => 0, |
# -linebreak => 0, |
159 |
), |
), |
160 |
textfield( -name => 'search' ), |
textfield( -name => 'search' ), |
161 |
popup_menu( -name => 'attr_operator', %$attr_operators ), |
$attr_operators ? popup_menu( -name => 'attr_operator', %$attr_operators ) : '', |
162 |
submit, |
submit( -value => 'Search' ), |
163 |
hidden( -name => 'entries_per_page', -default => $entries_per_page ), |
# hidden( -name => 'entries_per_page', -default => $entries_per_page ), |
164 |
|
popup_menu( -name => 'entries_per_page', -values => [ @entries_per_page ], -title => 'entries per page' ), |
165 |
# 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 |
166 |
qq|<input type=hidden name=current_page value=1 >|, |
qq|<input type=hidden name=current_page value=1 >|, |
167 |
checkbox( -name => 'debug', -default => 0 ), # FIXME hidden? |
checkbox( -name => 'debug', -default => 0 ), # FIXME hidden? |
168 |
qq|<div id=inputs>|, |
qq|<div id=inputs>|, |
169 |
h2( 'Select input' ), |
$inputs_available > 1 ? |
170 |
|
h2( 'Select input' ) . |
171 |
checkbox_group( |
checkbox_group( |
172 |
-name => 'only_input', |
-name => 'only_input', |
173 |
%$only_input, |
%$only_input, |
174 |
-linebreak=> 'true', |
-linebreak=> 'true', |
175 |
), |
) : '', |
176 |
qq|</div>|, |
qq|</div>|, |
177 |
|
end_form, |
178 |
|
; |
179 |
|
} |
180 |
|
|
|
; |
|
181 |
|
|
182 |
print end_form; |
print |
183 |
|
start_html( |
184 |
|
-title => $db->{name}, |
185 |
|
-style => [ @style ], |
186 |
|
), |
187 |
|
h1( $db->{name} ), |
188 |
|
qq|<div id=description>|, $db->{description}, qq|</div>|, |
189 |
|
; |
190 |
|
|
191 |
if ( my $search = param('search') ) { |
if ( my $search = param('search') ) { |
192 |
|
|
193 |
print qq|<div id="results">|; |
print qq| |
194 |
|
<a href="#form" class="skip" title="skip to search form">#</a> |
195 |
|
<div id="results"> |
196 |
|
|; |
197 |
|
|
198 |
my $swish = SWISH::API->new( "$dir/../var/swish/$database" ); |
my $swish = SWISH::API->new( "$dir/../var/swish/$database" ); |
199 |
$swish->abort_last_error if $swish->Error; |
$swish->abort_last_error if $swish->Error; |
200 |
|
|
|
my $pager = Data::Page->new; |
|
|
$pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ ); |
|
|
|
|
|
dump_yaml( 'pager', $pager ); |
|
|
|
|
201 |
my @search = (); |
my @search = (); |
202 |
|
my @attrs = param('attr'); |
203 |
|
my $op = param('attr_operator'); |
204 |
|
|
205 |
if ( $search =~ m{(=|"|AND|OR)} ) { |
if ( $search =~ m{(=|"|AND|OR)} ) { |
206 |
push @search, $search; |
push @search, $search; |
207 |
} elsif ( my $op = param('attr_operator') ) { |
} elsif ( @attrs ) { |
208 |
my $attr = param('attr'); |
|
209 |
my $v = $search; |
$op ||= 'Q*'; |
210 |
$v =~ s/^\s+//; |
my @or; |
211 |
warn "-- v: $v\n"; |
foreach my $attr ( @attrs ) { |
212 |
sub rewrite { |
my $v = $search; |
213 |
my ( $whitespace, $v ) = @_; |
$v =~ s/^\s+//; |
214 |
warn "## filter $op $whitespace $v\n"; |
warn "-- v: $v\n"; |
215 |
my $template = $op; |
sub rewrite { |
216 |
$template =~ s{Q}{$v}; |
my ( $attr, $whitespace, $v ) = @_; |
217 |
$whitespace = " AND " if $whitespace; |
warn "## filter $op $whitespace $v\n"; |
218 |
|
my $template = $op; |
219 |
return |
$template =~ s{Q}{$v}; |
220 |
$whitespace . |
$whitespace = " AND " if $whitespace; |
221 |
$attr . '="' . $template . '"'; |
|
222 |
; |
return |
223 |
}; |
$whitespace . |
224 |
$v =~ s{(\s*)(\S+)}{rewrite($1,$2)}ge; |
$attr . '="' . $template . '"'; |
225 |
|
; |
226 |
push @search, $v; |
}; |
227 |
|
if ( $op =~ m{\s} ) { |
228 |
my @only_input = param('only_input'); |
my $template = $op; |
229 |
push @search, '(' . join(') OR (', map { "input=$_" } @only_input) . ')' if @only_input; |
$template =~ s{Q}{$v}; |
230 |
|
$v = $attr . '="' . $template . '"'; |
231 |
|
} else { |
232 |
|
$v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge; |
233 |
|
} |
234 |
|
|
235 |
|
push @or, $v; |
236 |
|
|
237 |
|
} |
238 |
|
push @search, '(' . join(') OR (', @or) . ')'; |
239 |
|
|
240 |
} else { |
} else { |
241 |
push @search, "all=\"$search\""; |
push @search, "all=\"$search\""; |
242 |
} |
} |
243 |
|
|
244 |
my $q = '(' . join(') AND (', @search) . ')'; |
my $q = '(' . join(') AND (', @search) . ')'; |
245 |
$q =~ s{\(\((.+)\)\)}{($1)}; |
|
246 |
|
my @only_input = param('only_input'); |
247 |
|
$q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input; |
248 |
|
|
249 |
warn "# query: $q\n"; |
warn "# query: $q\n"; |
250 |
my $swish_results = $swish->query( $q ); |
my $swish_results = $swish->query( $q ); |
251 |
|
|
252 |
dump_yaml( 'swish_results', $swish_results ); |
dump_yaml( 'swish_results', $swish_results ); |
253 |
|
|
254 |
|
my $pager = Data::Page->new; |
255 |
|
$pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ ); |
256 |
$pager->total_entries( $swish_results->hits ); |
$pager->total_entries( $swish_results->hits ); |
257 |
|
|
258 |
$swish_results->seek_result( $pager->first ); |
dump_yaml( 'pager', $pager ); |
259 |
|
|
260 |
|
$swish_results->seek_result( $pager->first - 1 ); |
261 |
|
|
262 |
if ( ! $pager->total_entries ) { |
if ( ! $pager->total_entries ) { |
263 |
my $no_results = 'No results for search <b>%s</b>'; |
my $no_results = 'No results for search <b>%s</b>'; |
272 |
|
|
273 |
print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html; |
print qq|<ul class="pager">$pager_html</ul>\n\n| if $pager_html; |
274 |
|
|
275 |
my $start = $pager->first; |
my $nr = $pager->first; |
276 |
print qq|<ol start=$start>\n|; |
print qq|<ol start=$nr>\n|; |
277 |
|
|
278 |
my $limit = $pager->entries_on_this_page; |
my $limit = $pager->entries_on_this_page; |
279 |
|
|
280 |
|
my $nr = 1; |
281 |
|
|
282 |
while ( my $result = $swish_results->next_result ) { |
while ( my $result = $swish_results->next_result ) { |
|
last if $limit-- == 0; |
|
283 |
|
|
284 |
my $data = from_json $result->property('data'); |
my $data = from_json $result->property('data'); |
285 |
|
|
286 |
dump_yaml( 'data', $data ); |
dump_yaml( 'data', $data ); |
287 |
|
|
288 |
print qq|<li>|; |
my $li_class = ''; |
289 |
|
$li_class = qq| class="z"| if $nr % 2 == 0; |
290 |
|
print qq|<li$li_class>|; |
291 |
foreach my $attr ( @attr ) { |
foreach my $attr ( @attr ) { |
292 |
next unless defined $data->{$attr}; |
next unless defined $data->{$attr}; |
293 |
my $v = $data->{$attr}; |
my $v = $data->{$attr}; |
294 |
if ( $html_markup && ! $html_markup_skip->{$attr} ) { |
if ( $html_markup && ! $html_markup_skip->{$attr} ) { |
295 |
eval "\$v = $html_markup->$attr( \$v );"; |
eval "\$v = $html_markup->$attr( \$v, \$data );"; |
296 |
if ( $@ ) { |
if ( $@ ) { |
297 |
warn "disable html markup for $attr: $@"; |
warn "disable html markup for $attr: $@"; |
298 |
$html_markup_skip->{$attr} = $@; |
$html_markup_skip->{$attr} = $@; |
299 |
} |
} |
300 |
|
} else { |
301 |
|
$v =~ s{(http://\S+)}{<a href="$1">$1</a>}; |
302 |
} |
} |
303 |
my $label = $attr_labels->{'-labels'}->{$attr} || $attr; |
my $label = $attr_labels->{'-labels'}->{$attr} || $attr; |
304 |
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|; |
305 |
} |
} |
306 |
print qq|</li>\n|; |
print qq|</li>\n|; |
307 |
|
|
308 |
|
last if $nr++ == $pager->last; |
309 |
} |
} |
310 |
print qq|</ol>\n\n|; |
print qq|</ol>\n\n|; |
311 |
|
|
317 |
|
|
318 |
} |
} |
319 |
|
|
320 |
|
print search_form; |
321 |
|
|
322 |
dump_yaml( "config databases $database", $db ); |
dump_yaml( "config databases $database", $db ); |
323 |
dump_yaml( 'html_markup_skip', $html_markup_skip ); |
dump_yaml( 'html_markup_skip', $html_markup_skip ); |
324 |
|
|