22 |
|
|
23 |
sub dump_yaml { |
sub dump_yaml { |
24 |
my $name = shift; |
my $name = shift; |
25 |
print qq|<pre># $name\n|, YAML::Dump( @_ ), qq|</pre>| if $debug; |
print qq|<div class=dump><tt>$name</tt><pre>|, YAML::Dump( @_ ), qq|</pre></div>| if $debug; |
26 |
} |
} |
27 |
|
|
28 |
sub show_pager { |
sub show_pager { |
76 |
} |
} |
77 |
|
|
78 |
my $path = $ENV{PATH_INFO} || 'ecas'; |
my $path = $ENV{PATH_INFO} || 'ecas'; |
79 |
|
$path =~ s{^/+}{}; |
80 |
|
$path =~ s{/+$}{}; |
81 |
my $dir = $0; |
my $dir = $0; |
82 |
$dir =~ s{/[^/]+.cgi}{}; |
$dir =~ s{/[^/]+.cgi}{}; |
83 |
|
|
84 |
|
dump_yaml( 'dir', $dir ); |
85 |
|
|
86 |
my $config = YAML::LoadFile( "$dir/$path/config.yml" ); |
my $config = YAML::LoadFile( "$dir/$path/config.yml" ); |
87 |
|
|
88 |
my $database = (keys %{ $config->{databases} })[0]; |
my $database = (keys %{ $config->{databases} })[0]; |
97 |
undef $html_markup; |
undef $html_markup; |
98 |
} |
} |
99 |
|
|
100 |
my $estraier = YAML::LoadFile( "$dir/../var/estraier/$database.yaml" ); |
my $stats; |
101 |
|
{ |
102 |
|
my $path = "$dir/../var/swish/$database.yaml"; |
103 |
|
$stats = YAML::LoadFile( $path ); |
104 |
|
dump_yaml( "stats $path", $stats ); |
105 |
|
} |
106 |
|
|
107 |
my $db = $config->{databases}->{$database}; |
my $db = $config->{databases}->{$database}; |
108 |
|
|
109 |
my @attr = keys %{ $estraier->{attr} }; # FIXME replace with real gnerated lookup |
sub read_config_txt { |
110 |
|
my ( $file ) = @_; |
111 |
|
my $input; |
112 |
|
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); |
119 |
|
push @{ $input->{ '-values' } }, $val; |
120 |
|
$input->{ '-labels' }->{$val} = $label; |
121 |
|
} |
122 |
|
return $input; |
123 |
|
} |
124 |
|
|
125 |
print |
my $attr_labels = read_config_txt 'labels'; |
126 |
start_html( |
my $attr_operators = read_config_txt 'operators'; |
127 |
-title => $db->{name}, |
|
128 |
-style => '../../style.css', |
my @attr = @{ $attr_labels->{'-values'} }; |
129 |
), |
@attr = keys %{ $stats->{attr} } unless @attr; |
130 |
h1( $db->{name} ), |
|
131 |
qq|<div id=description>|, $db->{description}, qq|</div>|, |
|
132 |
|
warn dump( $attr_labels, $attr_operators ); |
133 |
|
|
134 |
|
my $only_input; |
135 |
|
my $inputs_available = 0; |
136 |
|
|
137 |
|
foreach ( @{ $db->{input} } ) { |
138 |
|
my $input = $_->{name} || die "no name in ",dump( $_ ); |
139 |
|
if ( ! $only_input->{'-labels'}->{$input} ) { |
140 |
|
push @{ $only_input->{'-values'} }, $input; |
141 |
|
$only_input->{'-labels'}->{$input} = $_->{description} || $input; |
142 |
|
$inputs_available++; |
143 |
|
} |
144 |
|
} |
145 |
|
|
146 |
|
warn "## only_input = ", dump( $only_input ); |
147 |
|
|
148 |
|
my @style = ( '../../style.css' ); |
149 |
|
push @style, "../../$path/$path.css" if -e "$dir/$path/$path.css"; |
150 |
|
dump_yaml( 'style', \@style ); |
151 |
|
|
152 |
|
sub search_form { |
153 |
|
qq|<a name="form"></a>|, |
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 |
-values => [ @attr ], |
%$attr_labels, |
158 |
# -linebreak => 0, |
# -linebreak => 0, |
159 |
), |
), |
160 |
textfield( -name => 'search' ), |
textfield( -name => 'search' ), |
161 |
popup_menu( -name => 'attr_operator', -values => [ '', 'STRBW', 'STREQ' ], |
$attr_operators ? popup_menu( -name => 'attr_operator', %$attr_operators ) : '', |
|
-labels => { |
|
|
'' => 'Bilo koja riječ', |
|
|
'STRBW' => 'Početak', |
|
|
'STREQ' => 'Točan oblik', |
|
|
}, |
|
|
), |
|
162 |
submit, |
submit, |
163 |
hidden( -name => 'entries_per_page', -default => $entries_per_page ), |
hidden( -name => 'entries_per_page', -default => $entries_per_page ), |
164 |
hidden( -name => 'current_page', -default => 1 ), |
# we need current_page fixed at 1 so that every submit through form will reset it |
165 |
|
qq|<input type=hidden name=current_page value=1 >|, |
166 |
checkbox( -name => 'debug', -default => 0 ), # FIXME hidden? |
checkbox( -name => 'debug', -default => 0 ), # FIXME hidden? |
167 |
qq|<div id=inputs>|, |
qq|<div id=inputs>|, |
168 |
h2( 'Select input' ), |
$inputs_available > 1 ? |
169 |
|
h2( 'Select input' ) . |
170 |
checkbox_group( |
checkbox_group( |
171 |
-name => 'only_input', |
-name => 'only_input', |
172 |
-values => [ map { $_->{name} } @{ $db->{input} } ], |
%$only_input, |
173 |
-linebreak=> 'true', |
-linebreak=> 'true', |
174 |
), |
) : '', |
175 |
qq|</div>|, |
qq|</div>|, |
176 |
|
end_form, |
177 |
|
; |
178 |
|
} |
179 |
|
|
|
; |
|
|
|
|
|
dump_yaml( 'inputs', $db->{input} ); |
|
|
dump_yaml( 'input names', map { $_->{name} } @{ $db->{input} } ); |
|
180 |
|
|
181 |
print end_form; |
print |
182 |
|
start_html( |
183 |
|
-title => $db->{name}, |
184 |
|
-style => [ @style ], |
185 |
|
), |
186 |
|
h1( $db->{name} ), |
187 |
|
qq|<div id=description>|, $db->{description}, qq|</div>|, |
188 |
|
; |
189 |
|
|
190 |
if ( my $search = param('search') ) { |
if ( my $search = param('search') ) { |
191 |
|
|
192 |
print qq|<div id="results">|; |
print qq| |
193 |
|
<a href="#form" class="skip" title="skip to search form">#</a> |
194 |
|
<div id="results"> |
195 |
|
|; |
196 |
|
|
197 |
my $swish = SWISH::API->new( "$dir/../var/swish/$database" ); |
my $swish = SWISH::API->new( "$dir/../var/swish/$database" ); |
198 |
$swish->abort_last_error if $swish->Error; |
$swish->abort_last_error if $swish->Error; |
199 |
|
|
200 |
param( 'entries_per_page', $entries_per_page ) unless param('entries_per_page'); # FIXME not needed? |
my @search = (); |
201 |
my $pager = Data::Page->new; |
my @attrs = param('attr'); |
202 |
$pager->total_entries( param('current_page') * param('entries_per_page') ); |
my $op = param('attr_operator'); |
203 |
$pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ ); |
|
204 |
|
if ( $search =~ m{(=|"|AND|OR)} ) { |
205 |
|
push @search, $search; |
206 |
|
} elsif ( @attrs ) { |
207 |
|
|
208 |
|
$op ||= 'Q*'; |
209 |
|
my @or; |
210 |
|
foreach my $attr ( @attrs ) { |
211 |
|
my $v = $search; |
212 |
|
$v =~ s/^\s+//; |
213 |
|
warn "-- v: $v\n"; |
214 |
|
sub rewrite { |
215 |
|
my ( $attr, $whitespace, $v ) = @_; |
216 |
|
warn "## filter $op $whitespace $v\n"; |
217 |
|
my $template = $op; |
218 |
|
$template =~ s{Q}{$v}; |
219 |
|
$whitespace = " AND " if $whitespace; |
220 |
|
|
221 |
|
return |
222 |
|
$whitespace . |
223 |
|
$attr . '="' . $template . '"'; |
224 |
|
; |
225 |
|
}; |
226 |
|
if ( $op =~ m{\s} ) { |
227 |
|
my $template = $op; |
228 |
|
$template =~ s{Q}{$v}; |
229 |
|
$v = $attr . '="' . $template . '"'; |
230 |
|
} else { |
231 |
|
$v =~ s{(\s*)(\S+)}{rewrite($attr,$1,$2)}ge; |
232 |
|
} |
233 |
|
|
234 |
dump_yaml( 'pager', $pager ); |
push @or, $v; |
235 |
|
|
236 |
|
} |
237 |
|
push @search, '(' . join(') OR (', @or) . ')'; |
238 |
|
|
239 |
my @search = ( "xml=$search" ); |
} else { |
240 |
|
push @search, "all=\"$search\""; |
241 |
|
} |
242 |
|
|
243 |
# if ( my $op = param('attr_operator') ) { |
my $q = '(' . join(') AND (', @search) . ')'; |
|
# $cond->add_attr( param('attr') . " $op " . param('search') ); |
|
|
# } |
|
244 |
|
|
245 |
my @only_input = param('only_input'); |
my @only_input = param('only_input'); |
246 |
|
$q .= ' AND ((' . join(') OR (', map { "input=\"$_\"" } @only_input) . '))' if @only_input; |
247 |
|
|
|
push @search, '(' . join(') or (', @only_input) . ')' if @only_input; |
|
|
|
|
|
my $q = '(' . join(') and (', @search) . ')'; |
|
248 |
warn "# query: $q\n"; |
warn "# query: $q\n"; |
249 |
my $swish_results = $swish->query( $q ); |
my $swish_results = $swish->query( $q ); |
|
$swish->abort_last_error if $swish->Error; |
|
250 |
|
|
251 |
dump_yaml( 'swish_results', $swish_results ); |
dump_yaml( 'swish_results', $swish_results ); |
252 |
|
|
253 |
|
my $pager = Data::Page->new; |
254 |
|
$pager->$_( param($_) ) foreach ( qw/entries_per_page current_page/ ); |
255 |
$pager->total_entries( $swish_results->hits ); |
$pager->total_entries( $swish_results->hits ); |
256 |
|
|
257 |
|
dump_yaml( 'pager', $pager ); |
258 |
|
|
259 |
|
$swish_results->seek_result( $pager->first - 1 ); |
260 |
|
|
261 |
if ( ! $pager->total_entries ) { |
if ( ! $pager->total_entries ) { |
262 |
my $no_results = "No results for search '%s'"; |
my $no_results = 'No results for search <b>%s</b>'; |
263 |
printf qq|<div class="error">$no_results</div>\n\n|, $search; |
$no_results = $swish->error_string . '<br><b>%s</b>' if $swish->error; |
264 |
|
printf qq|<div class="error">$no_results</div>\n\n|, $q; |
265 |
} else { |
} else { |
266 |
|
|
267 |
my $results = "<b>%d</b> results for search <b>%s</b> showing results %d - %d"; |
my $results = "<b>%d</b> results for search <b>%s</b> showing results %d - %d"; |
268 |
printf qq|<div class="message">$results</div>\n\n|, $pager->total_entries, $search, $pager->first, $pager->last; |
printf qq|<div class="message">$results</div>\n\n|, $pager->total_entries, $q, $pager->first, $pager->last; |
269 |
|
|
270 |
my $pager_html = join("\n", show_pager( $pager )); |
my $pager_html = join("\n", show_pager( $pager )); |
271 |
|
|
272 |
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; |
273 |
|
|
274 |
my $start = $pager->first; |
my $nr = $pager->first; |
275 |
print qq|<ol start=$start>\n|; |
print qq|<ol start=$nr>\n|; |
276 |
|
|
277 |
|
my $limit = $pager->entries_on_this_page; |
278 |
|
|
279 |
|
my $nr = 1; |
280 |
|
|
281 |
while ( my $result = $swish_results->next_result ) { |
while ( my $result = $swish_results->next_result ) { |
282 |
|
|
283 |
my $data = from_json $result->property('data'); |
my $data = from_json $result->property('data'); |
284 |
|
|
285 |
print qq|<li>|; |
dump_yaml( 'data', $data ); |
286 |
|
|
287 |
|
my $li_class = ''; |
288 |
|
$li_class = qq| class="z"| if $nr % 2 == 0; |
289 |
|
print qq|<li$li_class>|; |
290 |
foreach my $attr ( @attr ) { |
foreach my $attr ( @attr ) { |
291 |
next unless defined $data->{$attr}; |
next unless defined $data->{$attr}; |
292 |
my $v = $data->{$attr}; |
my $v = $data->{$attr}; |
296 |
warn "disable html markup for $attr: $@"; |
warn "disable html markup for $attr: $@"; |
297 |
$html_markup_skip->{$attr} = $@; |
$html_markup_skip->{$attr} = $@; |
298 |
} |
} |
299 |
|
} else { |
300 |
|
$v =~ s{(http://\S+)}{<a href="$1">$1</a>}; |
301 |
} |
} |
302 |
print qq|<div><label>$attr</label><span class=$attr>$v</span></div>\n|; |
my $label = $attr_labels->{'-labels'}->{$attr} || $attr; |
303 |
|
print qq|<div><label>$label</label><span class=$attr>$v</span></div>\n|; |
304 |
} |
} |
305 |
print qq|</li>\n|; |
print qq|</li>\n|; |
306 |
|
|
307 |
|
last if $nr++ == $pager->last; |
308 |
} |
} |
309 |
print qq|</ol>\n\n|; |
print qq|</ol>\n\n|; |
310 |
|
|
316 |
|
|
317 |
} |
} |
318 |
|
|
319 |
dump_yaml( 'db', $db ); |
print search_form; |
320 |
|
|
321 |
|
dump_yaml( "config databases $database", $db ); |
322 |
dump_yaml( 'html_markup_skip', $html_markup_skip ); |
dump_yaml( 'html_markup_skip', $html_markup_skip ); |
323 |
|
|
324 |
print end_html; |
print end_html; |