blib/lib/PMLTQ/CGI.pm

filter: Statementstmt 8.9% 99 / 1112 Branchbran 0.0% 0 / 506 Conditioncond 0.0% 0 / 271 MC/DCmcdc 0.0% 0 / 148 Subroutinesub 26.6% 24 / 90 Podpod 8.8% 4 / 45 totaltotal 5.8% 127 / 2172 CC 388 SCAR 61.9
CC
388
Cov
5%
resp_query
2970.0
Configure
552.0
resp_relation_target_types
552.0
CRAP 486.7
↑ Summary
1 package PMLTQ::CGI;
2 1 our $AUTHORITY = 'cpan:MATY';
3 1 $PMLTQ::CGI::VERSION = '2.0.1';
4 # ABSTRACT: [DEPRECATED] This is html fronted for SQLEvaluator and is currently being replaced by PMLTQ::Server
5  
6 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Treex::PML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Treex::PML::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Treex::PML::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use PMLTQ::Common ':tredmacro';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use PMLTQ::SQLEvaluator;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Benchmark;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use File::Glob qw(:glob);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use UNIVERSAL::DOES;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use MIME::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use YAML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 my $ua = LWP::UserAgent->new;
31 1 $ua->agent("Tree_Query_CGI/1.0 ");
32  
33 1 my $conf;
34 1 my $URL_BASE = '';
35 1 my $APP_PREFIX = 'app';
36 1 my $DEBUG=0;
37 1 our $HELP_URI = q{http://ufal.mff.cuni.cz/pmltq/doc/pmltq_doc.html};
38 1 our $PMLTQ_PROJECT_URI = q{http://ufal.mff.cuni.cz/pmltq/index.html};
39 1 my $log_dir='.pmltq_cgi_log';
40 1 my $tmp_dir;
41  
42 1 my $static_dir = undef;
43  
44 # print STDERR "XHTML: $CGI::XHTML\n";
45 # $CGI::XHTML=0;
46  
47 1 my $tree_print_service;
48 my $nodes_to_query_service;
49 1 my $pid_file;
50 1 my $pid_dir;
51 1 my $title = "PML Tree-Query Engine";
52 1 my $desc=Treex::PML::Factory->createStructure;
53 1 my $auth_file;
54 my $service_id;
55 1 my $use_google_translate;
56 1 my $use_ms_translate;
57  
58 1 my $ga_tracking_code;
59 1 my $ga_tracking_domain;
60  
61 1 ▶use vars qw($PAST_QUERIES_SCRIPT);
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63  
64  
65 0 ▶sub Configure {
Subroutine Configure: not called CC=23 SCAR=63.1
Pod undocumented
66 0 my ($opts)=@_;
67 0 ▶ croak("Usage: Configure({...})\n") unless ref($opts) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts eq "HASH" 0 0 0
68  
69 0 ▶ if ($opts->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"debug"}) 0 0 0
70 0 print STDERR "Reading configuration from $opts->{'config-file'}\n";
71 }
72  
73 0 ▶ if ($opts->{'method-prefix'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"method-prefix"}) 0 0 0
74 0 $URL_BASE = $opts->{'method-prefix'};
75 0 ▶ $URL_BASE .= '/' unless $URL_BASE=~m{/$};
Branch: 1 branch
BranchTrueFalseTotal
unless $URL_BASE =~ m[/$] 0 0 0
76 }
77 0 ▶ if ($opts->{'app-prefix'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"app-prefix"}) 0 0 0
78 0 $APP_PREFIX = $opts->{'app-prefix'};
79 0 $APP_PREFIX=~s{/$}{};
80 }
81  
82 # directory for static content
83 0 $static_dir = $opts->{'static-dir'};
84 0 ▶ if (! $static_dir) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $static_dir) { } 0 0 0
elsif (not -d $static_dir) { } 0 0 0
85 0 die "Static content directory not given!\nUse $0 --static-dir <dir-name>\n"
86 } elsif (! -d $static_dir) {
87 0 die "Static content directory '$static_dir' does not exist!\n";
88 }
89  
90 # configuration
91 0 my $cfg_root = Treex::PML::Instance->load({ filename=>$opts->{'config-file'} })->get_root;
92 0 my $configs = $cfg_root->{configurations};
93 0 $tree_print_service = $cfg_root->{tree_print_service};
94 0 $nodes_to_query_service = $cfg_root->{nodes_to_query_service};
95  
96 # configuration id
97 0 ▶ my $id = $service_id = $opts->{'server'} || 'cgi-default';
Condition0%0/2
$opts->{'server'} || 'cgi-default'
l!l
00
MC/DC0%0/1
$opts->{'server'} || 'cgi-default'
$opts->{'server'}
Truth table0%0/2
A
A= $opts->{'server'}
Aresult
11
00
98 0 ▶ $conf = first { $_->{id} eq $id } map $_->value, grep $_->name eq 'dbi', SeqV($configs);
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 ▶ die "Didn't find server configuration named '$id' in $opts->{'config-file'}!\nUse $0 --list-servers and then $0 --server <name>\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $conf 0 0 0
100 unless $conf;
101 0 ▶ $DEBUG=1 if $opts->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"debug"} 0 0 0
102  
103 0 ▶ if (defined($conf->{resources})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"resources"}) 0 0 0
104 0 Treex::PML::AddResourcePath($conf->{resources}->values);
105 }
106  
107 # query log dir
108 0 ▶ if ($opts->{'query-log-dir'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"query-log-dir"}) 0 0 0
109 0 $log_dir=$opts->{'query-log-dir'};
110 }
111  
112 0 ▶ if ($opts->{'tmp-dir'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'tmp-dir'}) { } 0 0 0
113 0 $tmp_dir=$opts->{'tmp-dir'};
114 } else {
115 0 $tmp_dir=File::Temp::tempdir()
116 }
117  
118 0 $pid_dir = $opts->{'pid-dir'};
119 0 ▶ $desc = $conf->{'description'} || Treex::PML::Factory->createStructure();
Condition0%0/3
$conf->{'description'} || 'Treex::PML::Factory'->createStructure
l!l&&r!l&&!r
000
MC/DC0%0/2
$conf->{'description'} || 'Treex::PML::Factory'->createStructure
$conf->{'description'} 'Treex::PML::Factory'->createStructure
Truth table0%0/3
A || B
A= $conf->{'description'}B= 'Treex::PML::Factory'->createStructure
ABresult
1X1
011
000
120 0 ▶ if ($pid_dir and -d $pid_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid_dir and -d $pid_dir) 0 0 0
Condition0%0/3
$pid_dir and -d $pid_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$pid_dir and -d $pid_dir
$pid_dir -d $pid_dir
Truth table0%0/3
A and B
A= $pid_dirB= -d $pid_dir
ABresult
0X0
100
111
121 0 $pid_file = File::Spec->catfile($pid_dir, "pmltq_cgi_$$.$id");
122 0 my $fh;
123 0 ▶ if (open ($fh, '>:utf8', $pid_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, ">:utf8", $pid_file) 0 0 0
124 0 ▶ if ($conf->{public}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"public"}) 0 0 0
125 0 print $fh ($opts->{'port'}."\n");
126 0 for my $k (qw(title abstract moreinfo)) {
127 0 ▶ my $v = $desc->{$k} || '';
Condition0%0/2
$desc->{$k} || ''
l!l
00
MC/DC0%0/1
$desc->{$k} || ''
$desc->{$k}
Truth table0%0/2
A
A= $desc->{$k}
Aresult
11
00
128 0 $v=~tr{\n}{ };
129 0 print $fh "$v\n";
130 }
131 0 print $fh ($conf->{'featured'}."\n");
132 0 ▶ print $fh ($conf->{'anonymous_access'}||''."\n");
Condition0%0/2
$conf->{'anonymous_access'} || "\n"
l!l
00
MC/DC0%0/1
$conf->{'anonymous_access'} || "\n"
$conf->{'anonymous_access'}
Truth table0%0/2
A
A= $conf->{'anonymous_access'}
Aresult
11
00
133 0 close($fh);
134 }
135 }
136 }
137  
138 0 $auth_file = $opts->{'auth-file'};
139 0 ▶ update_auth_info() if $auth_file;
Branch: 1 branch
BranchTrueFalseTotal
if $auth_file 0 0 0
140  
141 0 $use_google_translate = $opts->{'google-translate'};
142 0 $use_ms_translate = $opts->{'ms-translate'};
143  
144 0 $ga_tracking_code = $opts->{'ga-tracking-code'};
145 0 $ga_tracking_domain = $opts->{'ga-tracking-domain'};
146  
147 }
148  
149 0 ▶sub configuration {
Subroutine configuration: not called CC=2 SCAR=17.9
Pod undocumented
150 0 ▶ $conf || {};
Branch: 1 branch
BranchTrueFalseTotal
$conf or {} 0 0 0
151 }
152  
153  
154 # read authorization data from the auth-file
155  
156 1 my %auth_data;
157 1 my $last_auth_file_mtime;
158 0 ▶sub update_auth_info {
Subroutine update_auth_info: not called CC=15 SCAR=54.8
Pod undocumented
159 0 my $mtime = [stat $auth_file]->[9];
160 0 ▶ if (!defined($last_auth_file_mtime) or
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $last_auth_file_mtime or $mtime != $last_auth_file_mtime) 0 0 0
Condition0%0/3
not defined $last_auth_file_mtime or $mtime != $last_auth_file_mtime
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $last_auth_file_mtime or $mtime != $last_auth_file_mtime
not defined $last_auth_file_mtime $mtime != $last_auth_file_mtime
Truth table0%0/3
not(A) or B
A= not defined $last_auth_file_mtimeB= $mtime != $last_auth_file_mtime
ABresult
1X1
011
000
161 $mtime != $last_auth_file_mtime) {
162 0 %auth_data=();
163 0 print STDERR scalar(localtime).": (Re)loading AUTH information\n";
164 0 $last_auth_file_mtime=$mtime;
165 0 ▶ if (open my $af, '<', $auth_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $af, '<', $auth_file) { } 0 0 0
166 0 local $_;
167 0 ▶ while (<$af>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $af)) 0 0 0
168 0 ▶ chomp if defined($_);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 0 0 0
169 0 s{^\s*|\s*$|#.*}{}g;
170 0 ▶ next unless length;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 0 0 0
171 0 my ($user,$passwd,$selection)=split m{\s*:\s+},$_,3;
172 0 ▶ next unless length($user) and length($passwd);
Branch: 1 branch
BranchTrueFalseTotal
unless length $user and length $passwd 0 0 0
Condition0%0/3
length $user and length $passwd
!ll&&!rl&&r
000
MC/DC0%0/2
length $user and length $passwd
length $user length $passwd
Truth table0%0/3
A and B
A= length $userB= length $passwd
ABresult
0X0
100
111
173 0 my $data = $auth_data{$user}={};
174 0 ▶ if ($selection) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selection) { } 0 0 0
175 0 ▶ my %s; my $bool = $1 if $selection=~s{^\s*([-+])}{};
Branch: 1 branch
BranchTrueFalseTotal
if $selection =~ s/^\s*([-+])// 0 0 0
176 0 @s{ split m{\s*,\s*}, $selection } = ();
177 0 ▶ if (defined($bool) and $bool eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bool and $bool eq '-') { } 0 0 0
Condition0%0/3
defined $bool and $bool eq "-"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bool and $bool eq "-"
defined $bool $bool eq "-"
Truth table0%0/3
A and B
A= defined $boolB= $bool eq "-"
ABresult
0X0
100
111
178 0 $data->{deny}=\%s;
179 0 ▶ next if exists $s{ $service_id }
Branch: 1 branch
BranchTrueFalseTotal
if exists $s{$service_id} 0 0 0
180 } else {
181 0 $data->{allow}=\%s;
182 0 ▶ next if !exists $s{ $service_id }
Branch: 1 branch
BranchTrueFalseTotal
unless exists $s{$service_id} 0 0 0
183 }
184 } else {
185 # complete access
186 0 $data->{deny}={};
187 }
188 0 $data->{passwd}=$passwd;
189 }
190 0 close $af;
191 } else {
192 0 print STDERR "Failed to read AUTH information!\n";
193 }
194 }
195 }
196  
197  
198 # this method returns (401,"") for unknown user
199 # and (200,$password) for a known user.
200 0 ▶sub auth {
Subroutine auth: not called CC=3 SCAR=24.8
Pod documented
201 0 my ($url,$user)=@_;
202 0 update_auth_info();
203 0 my $data=$auth_data{$user};
204 0 ▶ my $passwd = $data && $data->{passwd};
Condition0%0/3
$data && $data->{'passwd'}
!ll&&!rl&&r
000
MC/DC0%0/2
$data && $data->{'passwd'}
$data $data->{'passwd'}
Truth table0%0/3
A && B
A= $dataB= $data->{'passwd'}
ABresult
0X0
100
111
205 0 ▶ unless (defined $passwd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $passwd) 0 0 0
206 0 print STDERR scalar(localtime).": denied AUTH to $user for $url: code 401.\n";
207 0 return ("401", "");
208 }
209 0 print STDERR scalar(localtime).": accepted AUTH from $user for $url\n";
210 0 return ("200",$passwd);
211 }
212  
213 # check if the current HTTP user is authorized for a given service
214 0 ▶sub user_authorized {
Subroutine user_authorized: not called CC=7 SCAR=40.3
Pod undocumented
215 0 my ($cgi,$id)=@_;
216 0 ▶ return 1 unless $auth_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth_file 0 0 0
217 0 my $user = _user_name($cgi);
218 0 my $data = $auth_data{$user};
219 0 ▶ return 0 unless ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 0 0 0
220 0 ▶ if (ref($data->{allow})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->{"allow"}) 0 0 0
221 0 ▶ return exists($data->{allow}{$id}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $data->{'allow'}{$id} ? : 0 0 0
222 }
223 0 ▶ if (ref $data->{deny}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->{"deny"}) 0 0 0
224 0 ▶ return exists($data->{deny}{$id}) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $data->{'deny'}{$id} ? : 0 0 0
225 }
226 0 return 1;
227 }
228  
229 # auxiliary method used to generate a HTML description of a given list
230 # of services
231 0 ▶sub _print_service_info {
Subroutine _print_service_info: not called CC=14 SCAR=53.5
232 0 my ($cgi,$services)=@_;
233 0 my $format = $cgi->param('format');
234 0 my $current;
235 0 my $uri = URI->new($cgi->url(-base=>1));
236 0 update_auth_info();
237 0 ▶ unless ($services) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($services) 0 0 0
238 $services=[{
239 id => $service_id,
240 access => user_authorized($cgi,$service_id)||$conf->{anonymous_access}||0,
241 service => $uri,
242 title => $desc->{title},
243 abstract => $desc->{abstract},
244 moreinfo => $desc->{moreinfo},
245 featured => $conf->{featured},
246 0 ▶ anonymous_access => $conf->{anonymous_access}||0,
Condition0%0/7
user_authorized($cgi, $service_id) || $conf->{'anonymous_access'} || 0
l!l
00
user_authorized($cgi, $service_id) or $conf->{"anonymous_access"}
l!l&&r!l&&!r
000
$conf->{'anonymous_access'} || 0
l!l
00
MC/DC0%0/2
user_authorized($cgi, $service_id) || $conf->{'anonymous_access'} || 0
user_authorized($cgi, $service_id) $conf->{"anonymous_access"}
MC/DC0%0/1
$conf->{'anonymous_access'} || 0
$conf->{'anonymous_access'}
Truth table0%0/3
A or B
A= user_authorized($cgi, $service_id)B= $conf->{"anonymous_access"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $conf->{'anonymous_access'}
Aresult
11
00
247 }];
248 0 $current = 1;
249 }
250  
251 # use current host, but change the port number
252 0 for (@$services) {
253 0 ▶ next if $_->{service};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"service"} 0 0 0
254 0 my $u = URI->new($uri);
255 0 $u->port(delete $_->{port});
256 0 $_->{service} = $u;
257 }
258 0 ▶ if ($format eq 'html') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'html') { } 0 0 0
elsif ($format eq 'json') { } 0 0 0
259 0 my $i=0;
260 print(
261 $cgi->header(-charset => 'UTF-8'),
262 $cgi->start_html({-title=>($current ? 'About This PML-TQ Service' : 'Available PML-TQ Services'),
263 -encoding => 'UTF-8',
264 -style => {
265 -type => 'text/css',
266 -src => $URL_BASE.'static/css/query.css'
267 },
268  
269 }),
270 _title($cgi,$current ? 'about' : 'other'),
271 $cgi->div({-class => 'content'},
272 (map {
273 0 ▶ $i++;
Branch: 2 branches
BranchTrueFalseTotal
$current ? : 0 0 0
$current ? : 0 0 0
274 $cgi->div({-class => 'service'},
275 $cgi->div({ -class => 'hh', id=>"hh$i" },
276 $cgi->h3(
277 (($current or !$_->{access}) ? () :
278 $cgi->a({href=>"javascript:document.qf.action='$_->{service}';document.qf.submit()"},
279 'Select')),
280 $_->{title},
281 $cgi->a({class=>"show",
282 onClick=>"document.getElementById('hh$i').setAttribute('class','hs'); return false;"
283 },"&raquo;"),
284 $cgi->a({class=>"hide",
285 onClick=>"document.getElementById('hh$i').setAttribute('class','hh'); return false;"
286 },"&laquo;")),
287 $cgi->div({ -class => 'hbody' },
288 $cgi->div({ -class => 'abstract' },
289 $cgi->p($_->{abstract}),
290 ($_->{moreinfo}
291 0 ▶ ? $cgi->p($cgi->a({href=>"$_->{moreinfo}", target=>"_blank"},
Branch: 2 branches
BranchTrueFalseTotal
$current || !$_->{'access'} ? : 0 0 0
$_->{'moreinfo'} ? : 0 0 0
Condition0%0/3
$current or not $_->{"access"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$current or not $_->{"access"}
$current not $_->{"access"}
Truth table0%0/3
A or not(B)
A= $currentB= not $_->{"access"}
ABresult
1X1
011
000
292 'More info ...'))
293 : ()),
294 )
295 ),
296 )
297 )
298 } @$services)),
299 $cgi->end_html()
300 );
301 } elsif ($format eq 'json') {
302 0 print $cgi->header(-type => 'text/x-json',
303 -charset=>'UTF-8'
304 );
305 print(qq{[},
306 join(",", map {
307 0 my $s=$_;
308 qq[\n {]
309 .join(",", map {
310 0 qq{\n \"}
311 ._js_escape_string($_)
312 .q{" : "}
313 0 ._js_escape_string($s->{$_})
314 .q{"}
315 } sort keys %$s)
316 .qq[\n }]
317 } @$services),
318 qq{\n]\n});
319 } else {
320 0 print($cgi->header(-type=>'text/plain',
321 -charset=>'UTF-8'));
322 0 my ($v,$s);
323 print(map {
324 0 $s=$_;
325 join("\t", map {
326 0 $v=$s->{$_};
327 0 $v=~s{\s+}{ }g;
328 0 qq{$_:$v}
329 } sort keys %$s)
330 .qq[\n]
331 } @$services);
332 }
333 }
334  
335 0 ▶sub _dump_all_info {
Subroutine _dump_all_info: not called CC=12 SCAR=50.5
336 0 my ($cgi)=@_;
337 0 ▶ my $format = $cgi->param('format')||'text';
Condition0%0/2
$cgi->param('format') || 'text'
l!l
00
MC/DC0%0/1
$cgi->param('format') || 'text'
$cgi->param('format')
Truth table0%0/2
A
A= $cgi->param('format')
Aresult
11
00
338 0 ▶ $format = 'text' if $format eq 'html';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "html" 0 0 0
339 0 my $ev = eval { init_evaluator() };
340 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
341 0 print STDERR "INIT: $@\n";
342 0 return $@;
343 }
344 0 my %dump;
345 0 my $schema_names = $ev->get_schema_names();
346 0 my $node_types = $ev->get_node_types();
347 0 $dump{schema_names} = $schema_names;
348 0 my %node_types = map { $_ => $ev->get_node_types($_) } @$schema_names;
349 0 $dump{node_types} = \%node_types;
350  
351 $dump{relations} = {
352 0 standard => \@{PMLTQ::Common::standard_relations()},
353 pml => { },
354 user => { },
355 };
356 0 foreach my $type (@$node_types) {
357 0 my $types = $ev->get_pmlrf_relations($type);
358 0 ▶ if (@$types) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$types) 0 0 0
359 0 $dump{relations}->{pml}->{$type} = $types;
360 }
361 }
362 0 foreach my $type (@$node_types) {
363 0 my $types = $ev->get_user_defined_relations($type);
364 0 ▶ if (@$types) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$types) 0 0 0
365 0 $dump{relations}->{user}->{$type} = $types;
366 }
367 }
368  
369 my %attributes = map {
370 0 my @res;
371 0 my $type = $_;
372 0 my $decl = $ev->get_decl_for($_);
373 0 ▶ if ($decl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decl) 0 0 0
374 0 @res = map { my $t = $_; $t=~s{#content}{content()}g; $t } $decl->get_paths_to_atoms({ no_childnodes => 1});
375 0 ▶ if (@{PMLTQ::Common::GetElementNamesForDecl($decl)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{PMLTQ::Common::GetElementNamesForDecl($decl);}) 0 0 0
376 0 unshift @res, 'name()';
377 }
378 }
379 0 ▶ @res?($type => \@res):()
Branch: 1 branch
BranchTrueFalseTotal
@res ? : 0 0 0
380 } @$node_types;
381 0 $dump{attributes} = \%attributes;
382  
383 0 $dump{doc} = generate_doc($ev);
384  
385 0 ▶ if ($format eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'json') { } 0 0 0
386 0 print $cgi->header(-type => 'text/x-json',
387 -charset=>'UTF-8'
388 );
389 0 my $json = JSON->new->allow_nonref;
390 0 print $json->pretty->encode(\%dump);
391 } else { ## dump text for everything else
392 0 print($cgi->header(-type=>'text/plain',
393 -charset=>'UTF-8'));
394 0 print YAML::Dump(\%dump);
395 }
396 }
397  
398 # logout a given user
399 0 ▶sub logout_user {
Subroutine logout_user: not called CC=3 SCAR=24.8
Pod undocumented
400 0 my ($user)=@_;
401 0 print STDERR "logging out $user\n";
402 0 ▶ if (-d $pid_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $pid_dir) 0 0 0
403 0 for my $session_file (glob(File::Spec->catfile($pid_dir,'session_'.$user.'-*'))) {
404 0 unlink $session_file
405 }
406 }
407 }
408  
409 # generate a new session ID for a given user
410 0 ▶sub generate_session_id {
Subroutine generate_session_id: not called CC=2 SCAR=17.9
Pod undocumented
411 0 my $session_id = '';
412 0 $session_id.=sprintf("%02x",int(rand(256))) for 0..15;
413 0 return $session_id;
414 }
415  
416 # generate a HTML redirect response to a given URL with URL params
417 0 ▶sub redirect {
Subroutine redirect: not called CC=3 SCAR=24.8
Pod undocumented
418 0 my ($cgi,$to,$params)=@_;
419 0 my $url = URI->new($to);
420 $params = {
421 (map {
422 0 my $val = $cgi->param($_);
423 0 ▶ defined($val) ? ($_ => $val) : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
424 } $cgi->param()),
425 0 ▶ %{$params||{}}
Condition0%0/2
$params || {}
l!l
00
MC/DC0%0/1
$params || {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
426 };
427 0 $url->query_form($params);
428 0 print STDERR "Redirecting to $url\n";
429 0 print $cgi->header(-Location => $url->as_string);
430 0 return 303;
431 }
432  
433  
434 0 ▶sub app {
Subroutine app: not called CC=4 SCAR=30.0
Pod documented
435 0 my $callback=shift;
436 0 my $cgi=shift;
437 0 ▶ if (!defined $callback) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $callback) 0 0 0
438 0 return 404;
439 }
440 0 ▶ if (session_ok($cgi) or $conf->{anonymous_access}) {
Branch: 1 branch
BranchTrueFalseTotal
if (session_ok($cgi) or $conf->{'anonymous_access'}) { } 0 0 0
Condition0%0/3
session_ok($cgi) or $conf->{"anonymous_access"}
l!l&&r!l&&!r
000
MC/DC0%0/2
session_ok($cgi) or $conf->{"anonymous_access"}
session_ok($cgi) $conf->{"anonymous_access"}
Truth table0%0/3
A or B
A= session_ok($cgi)B= $conf->{"anonymous_access"}
ABresult
1X1
011
000
441 0 return $callback->($cgi);
442 } else {
443 0 print STDERR "No or invalid session\n";
444 0 return redirect($cgi,qq{login});
445 }
446 }
447  
448  
449 0 ▶sub resp_login {
Subroutine resp_login: not called CC=7 SCAR=40.3
Pod documented
450 0 my ($cgi)=@_;
451 0 my $user = _user_name($cgi);
452 0 ▶ return 403 unless user_name_string_ok($user);
Branch: 1 branch
BranchTrueFalseTotal
unless user_name_string_ok($user) 0 0 0
453 0 ▶ return 403 unless -d $pid_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $pid_dir 0 0 0
454  
455 0 my $session_file_base = File::Spec->catfile($pid_dir,'session_'.$user.'-');
456 0 print STDERR "logging in $user\n";
457 0 print STDERR $session_file_base,"\n";
458  
459 0 my @session_files = glob($session_file_base.'*');
460 0 my $session_id;
461 0 ▶ if (@session_files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@session_files) 0 0 0
462 0 $session_id=$session_files[0];
463 0 $session_id=~s{^.*-}{}g;
464 0 ▶ unless (session_id_string_ok($session_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (session_id_string_ok($session_id)) 0 0 0
465 0 logout_user($user);
466 0 undef $session_id;
467 }
468 }
469 0 ▶ unless ($session_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session_id) 0 0 0
470 0 $session_id = generate_session_id();
471 0 my $session_file = $session_file_base.$session_id;
472 0 print STDERR "Creating session file $session_file\n";
473 0 ▶ open(my $fh, '>', $session_file) || return 403;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $session_file or return 403 0 0 0
474 0 print $fh ("$$\t$user\t".localtime()."\n");
475 0 close $fh;
476 }
477 0 return redirect($cgi,qq{form},{u=>$user,s => $session_id});
478 }
479  
480 0 ▶sub session_id_string_ok {
Subroutine session_id_string_ok: not called CC=2 SCAR=17.9
Pod undocumented
481 0 my ($id)=@_;
482 0 ▶ return (($id =~/^[a-z0-9]+$/) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$id =~ /^[a-z0-9]+$/ ? : 0 0 0
483 }
484 0 ▶sub user_name_string_ok {
Subroutine user_name_string_ok: not called CC=3 SCAR=24.8
Pod undocumented
485 0 my ($user)=@_;
486 0 ▶ return 0 if $user eq 'unknown';
Branch: 1 branch
BranchTrueFalseTotal
if $user eq "unknown" 0 0 0
487 0 ▶ return (($user=~/^[a-zA-Z0-9_.]+$/) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$user =~ /^[a-zA-Z0-9_.]+$/ ? : 0 0 0
488 }
489  
490 # check if the session ID provided in the s and u arguments
491 # is vallid (backed by a session file in the pid-dir and not expired)
492 0 ▶sub session_ok {
Subroutine session_ok: not called CC=9 SCAR=45.0
Pod undocumented
493 0 my ($cgi)=@_;
494 # return 1 unless $auth_file;
495 0 my ($session_id,$user)=($cgi->param('s'), $cgi->param('u'));
496 0 ▶ if ($session_id and $user and session_id_string_ok($session_id)
Branch: 1 branch
BranchTrueFalseTotal
if ($session_id and $user and session_id_string_ok($session_id) and user_name_string_ok($user) and -d $pid_dir) 0 0 0
Condition0%0/12
$session_id and $user and session_id_string_ok($session_id) and user_name_string_ok($user) and -d $pid_dir
!ll&&!rl&&r
000
$session_id and $user and session_id_string_ok($session_id) and user_name_string_ok($user)
!ll&&!rl&&r
000
$session_id and $user and session_id_string_ok($session_id)
!ll&&!rl&&r
000
$session_id and $user
!ll&&!rl&&r
000
MC/DC0%0/5
$session_id and $user and session_id_string_ok($session_id) and user_name_string_ok($user) and -d $pid_dir
$session_id $user session_id_string_ok($session_id) user_name_string_ok($user) -d $pid_dir
Truth table0%0/6
A and B and C and D and E
A= $session_idB= $userC= session_id_string_ok($session_id)D= user_name_string_ok($user)E= -d $pid_dir
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
497 and user_name_string_ok($user) and (-d $pid_dir)) {
498 0 my $session_file = File::Spec->catfile($pid_dir,'session_'.$user.'-'.$session_id);
499 0 ▶ if (-f $session_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $session_file) 0 0 0
500 0 update_auth_info();
501 0 ▶ if (user_authorized($cgi,$service_id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (user_authorized($cgi, $service_id)) 0 0 0
502 0 my $now = time();
503 0 ▶ if (($now-(stat($session_file))[9])<259_200) { # two days
Branch: 1 branch
BranchTrueFalseTotal
if ($now - (stat $session_file)[9] < 259200) { } 0 0 0
504 0 print STDERR "Session ok.\n";
505 0 utime $now, $now, $session_file;
506 0 return 1;
507 } else {
508 0 print STDERR "Session expired.\n";
509 0 logout_user($user);
510 }
511 }
512 }
513 }
514  
515 0 return 0;
516 }
517  
518 # faster form of session_ok... just doesn't check session expiration
519 0 ▶sub logged_in {
Subroutine logged_in: not called CC=6 SCAR=37.4
Pod undocumented
520 0 my ($cgi)=@_;
521 0 my ($session_id,$user)=($cgi->param('s'), $cgi->param('u'));
522 0 ▶ if ($session_id and $user and session_id_string_ok($session_id)
Branch: 1 branch
BranchTrueFalseTotal
if ($session_id and $user and session_id_string_ok($session_id) and user_name_string_ok($user) and -d $pid_dir) 0 0 0
Condition0%0/12
$session_id and $user and session_id_string_ok($session_id) and user_name_string_ok($user) and -d $pid_dir
!ll&&!rl&&r
000
$session_id and $user and session_id_string_ok($session_id) and user_name_string_ok($user)
!ll&&!rl&&r
000
$session_id and $user and session_id_string_ok($session_id)
!ll&&!rl&&r
000
$session_id and $user
!ll&&!rl&&r
000
MC/DC0%0/5
$session_id and $user and session_id_string_ok($session_id) and user_name_string_ok($user) and -d $pid_dir
$session_id $user session_id_string_ok($session_id) user_name_string_ok($user) -d $pid_dir
Truth table0%0/6
A and B and C and D and E
A= $session_idB= $userC= session_id_string_ok($session_id)D= user_name_string_ok($user)E= -d $pid_dir
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
523 and user_name_string_ok($user) and (-d $pid_dir)) {
524 0 return 1;
525 }
526 0 return 0;
527 }
528  
529 0 ▶sub is_anonymous {
Subroutine is_anonymous: not called CC=2 SCAR=17.9
Pod undocumented
530 0 my ($cgi)=@_;
531 0 ▶ return !logged_in($cgi) && $conf->{anonymous_access};
Condition0%0/3
!logged_in($cgi) && $conf->{'anonymous_access'}
!ll&&!rl&&r
000
MC/DC0%0/2
!logged_in($cgi) && $conf->{'anonymous_access'}
!logged_in($cgi) $conf->{'anonymous_access'}
Truth table0%0/3
not(A) && B
A= !logged_in($cgi)B= $conf->{'anonymous_access'}
ABresult
0X0
100
111
532 }
533  
534 0 ▶sub resp_root {
Subroutine resp_root: not called CC=3 SCAR=24.8
Pod documented
535 0 my ($cgi)=@_;
536 0 ▶ if (session_ok($cgi) or $conf->{anonymous_access}) {
Branch: 1 branch
BranchTrueFalseTotal
if (session_ok($cgi) or $conf->{'anonymous_access'}) { } 0 0 0
Condition0%0/3
session_ok($cgi) or $conf->{"anonymous_access"}
l!l&&r!l&&!r
000
MC/DC0%0/2
session_ok($cgi) or $conf->{"anonymous_access"}
session_ok($cgi) $conf->{"anonymous_access"}
Truth table0%0/3
A or B
A= session_ok($cgi)B= $conf->{"anonymous_access"}
ABresult
1X1
011
000
537 0 return redirect($cgi,qq{$APP_PREFIX/${URL_BASE}form});
538 } else {
539 0 return redirect($cgi,qq{$APP_PREFIX/${URL_BASE}login});
540 }
541 }
542  
543 0 ▶sub resp_about {
Subroutine resp_about: not called CC=4 SCAR=30.0
Pod undocumented
544 0 my ($cgi)=@_;
545 0 ▶ my $ext = $cgi->param('extended')?1:0;
Branch: 1 branch
BranchTrueFalseTotal
$cgi->param('extended') ? : 0 0 0
546 0 ▶ _print_service_info($cgi) unless $ext;
Branch: 1 branch
BranchTrueFalseTotal
unless $ext 0 0 0
547 0 ▶ _dump_all_info($cgi) if $ext;
Branch: 1 branch
BranchTrueFalseTotal
if $ext 0 0 0
548 0 return 200;
549 }
550  
551 0 ▶sub resp_other_services {
Subroutine resp_other_services: not called CC=17 SCAR=57.2
Pod undocumented
552 0 my ($cgi)=@_;
553 0 my @services;
554  
555 # read all pid filesa
556 0 update_auth_info();
557 0 ▶ if (-d $pid_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $pid_dir) 0 0 0
558 0 for my $file (glob(File::Spec->catfile($pid_dir,"pmltq_cgi_*.*"))) {
559 0 ▶ open(my $fh, '<', $file) or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or next 0 0 0
560 0 my $port = <$fh>;
561 0 ▶ next unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 0 0 0
562 0 chomp $port;
563 0 ▶ next unless $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $port 0 0 0
564 0 my $title = <$fh>;
565 0 $title=~s{\s+}{ }g;
566 0 $title=~s{^ | $}{}g;
567 0 my $abstract = <$fh>;
568 0 $abstract=~s{\s+}{ }g;
569 0 $abstract=~s{^ | $}{}g;
570 0 my $moreinfo = <$fh>; chomp $moreinfo;
571 0 my $featured = <$fh>; chomp $featured;
572 0 my $anonymous_access = <$fh>; chomp $anonymous_access;
573 0 ▶ if ($port) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port) 0 0 0
574 0 my (undef, $id) = split m{\.}, $file,2;
575 0 ▶ push @services, {
Condition0%0/7
user_authorized($cgi, $id) || $anonymous_access || 0
l!l
00
user_authorized($cgi, $id) or $anonymous_access
l!l&&r!l&&!r
000
$anonymous_access || 0
l!l
00
MC/DC0%0/2
user_authorized($cgi, $id) || $anonymous_access || 0
user_authorized($cgi, $id) $anonymous_access
MC/DC0%0/1
$anonymous_access || 0
$anonymous_access
Truth table0%0/3
A or B
A= user_authorized($cgi, $id)B= $anonymous_access
ABresult
1X1
011
000
Truth table0%0/2
A
A= $anonymous_access
Aresult
11
00
576 id => $id,
577 port => $port,
578 title => $title,
579 abstract => $abstract,
580 moreinfo => $moreinfo,
581 featured => $featured,
582 access => user_authorized($cgi,$id)||$anonymous_access||0,
583 anonymous_access => $anonymous_access||0,
584 service => undef,
585 };
586 }
587 }
588 }
589 @services = sort {
590 0 $b->{access} <=> $a->{access} or
591 ($a->{featured}||10000) <=> ($b->{featured}||10000) or
592 ($a->{title}||'') cmp ($b->{title}||'') or
593 0 ▶ $a->{port} <=> $b->{port} } @services;
Condition0%0/17
$PMLTQ::CGI::b->{'access'} <=> $PMLTQ::CGI::a->{'access'} || ($PMLTQ::CGI::a->{'featured'} || 10000) <=> ($PMLTQ::CGI::b->{'featured'} || 10000) || ($PMLTQ::CGI::a->{'title'} || '') cmp ($PMLTQ::CGI::b->{'title'} || '') || $PMLTQ::CGI::a->{'port'} <=> $PMLTQ::CGI::b->{'port'}
l!l&&r!l&&!r
000
$PMLTQ::CGI::b->{"access"} <=> $PMLTQ::CGI::a->{"access"} or ($PMLTQ::CGI::a->{"featured"} || 10000) <=> ($PMLTQ::CGI::b->{"featured"} || 10000) or ($PMLTQ::CGI::a->{"title"} || "") cmp ($PMLTQ::CGI::b->{"title"} || "")
l!l&&r!l&&!r
000
$PMLTQ::CGI::b->{"access"} <=> $PMLTQ::CGI::a->{"access"} or ($PMLTQ::CGI::a->{"featured"} || 10000) <=> ($PMLTQ::CGI::b->{"featured"} || 10000)
l!l&&r!l&&!r
000
$PMLTQ::CGI::a->{'featured'} || 10000
l!l
00
$PMLTQ::CGI::b->{'featured'} || 10000
l!l
00
$PMLTQ::CGI::a->{'title'} || ''
l!l
00
$PMLTQ::CGI::b->{'title'} || ''
l!l
00
MC/DC0%0/4
$PMLTQ::CGI::b->{'access'} <=> $PMLTQ::CGI::a->{'access'} || ($PMLTQ::CGI::a->{'featured'} || 10000) <=> ($PMLTQ::CGI::b->{'featured'} || 10000) || ($PMLTQ::CGI::a->{'title'} || '') cmp ($PMLTQ::CGI::b->{'title'} || '') || $PMLTQ::CGI::a->{'port'} <=> $PMLTQ::CGI::b->{'port'}
$PMLTQ::CGI::b->{"access"} <=> $PMLTQ::CGI::a->{"access"} $PMLTQ::CGI::a->{'featured'} $PMLTQ::CGI::a->{'title'} $PMLTQ::CGI::a->{'port'} <=> $PMLTQ::CGI::b->{'port'}
MC/DC0%0/1
$PMLTQ::CGI::b->{'featured'} || 10000
$PMLTQ::CGI::b->{'featured'}
MC/DC0%0/1
$PMLTQ::CGI::b->{'title'} || ''
$PMLTQ::CGI::b->{'title'}
Truth table0%0/5
A or B or C || D
A= $PMLTQ::CGI::b->{"access"} <=> $PMLTQ::CGI::a->{"access"}B= $PMLTQ::CGI::a->{'featured'}C= $PMLTQ::CGI::a->{'title'}D= $PMLTQ::CGI::a->{'port'} <=> $PMLTQ::CGI::b->{'port'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
Truth table0%0/2
A
A= $PMLTQ::CGI::b->{'featured'}
Aresult
11
00
Truth table0%0/2
A
A= $PMLTQ::CGI::b->{'title'}
Aresult
11
00
594 0 _print_service_info($cgi,\@services);
595 0 return 200;
596 }
597  
598 0 ▶sub _user_name {
Subroutine _user_name: not called CC=3 SCAR=24.8
599 0 my ($cgi)=@_;
600 0 ▶ my $user = $cgi->remote_user || $cgi->param('u') || 'unknown';
Condition0%0/5
$cgi->remote_user || $cgi->param('u') || 'unknown'
l!l
00
$cgi->remote_user or $cgi->param("u")
l!l&&r!l&&!r
000
MC/DC0%0/2
$cgi->remote_user || $cgi->param('u') || 'unknown'
$cgi->remote_user $cgi->param("u")
Truth table0%0/3
A or B
A= $cgi->remote_userB= $cgi->param("u")
ABresult
1X1
011
000
601 0 $user=~y{/!#'"*~$^&()[]\{\}\.\+\|}{_}; # sanity
602 0 return $user;
603 }
604  
605 0 ▶sub log_query {
Subroutine log_query: not called CC=10 SCAR=47.0
Pod undocumented
606 0 my ($cgi,$query)=@_;
607 0 $query=~s{^\s+|\s+$}{}g;
608 0 my $md5 = md5_hex($query);
609 0 my $user = _user_name($cgi);
610 0 ▶ my $log_string = sprintf("query: time='%s' remote_user='%s' auth='%s' remote_host='%s' port='%s' srv_version='%s'\n",
Condition0%0/2
$cgi->auth_type || ''
l!l
00
MC/DC0%0/1
$cgi->auth_type || ''
$cgi->auth_type
Truth table0%0/2
A
A= $cgi->auth_type
Aresult
11
00
611 scalar(localtime()), $user, $cgi->auth_type||'',
612 $cgi->remote_host, $cgi->server_port, $PMLTQ::SQLEvaluator::VERSION );
613 0 print STDERR $log_string;
614 0 ▶ return unless $log_dir and logged_in($cgi);
Branch: 1 branch
BranchTrueFalseTotal
unless $log_dir and logged_in($cgi) 0 0 0
Condition0%0/3
$log_dir and logged_in($cgi)
!ll&&!rl&&r
000
MC/DC0%0/2
$log_dir and logged_in($cgi)
$log_dir logged_in($cgi)
Truth table0%0/3
A and B
A= $log_dirB= logged_in($cgi)
ABresult
0X0
100
111
615 0 ▶ if (!(-d $log_dir) and !(mkdir($log_dir))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $log_dir or mkdir $log_dir) 0 0 0
Condition0%0/3
-d $log_dir or mkdir $log_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $log_dir or mkdir $log_dir
-d $log_dir mkdir $log_dir
Truth table0%0/3
A or B
A= -d $log_dirB= mkdir $log_dir
ABresult
1X1
011
000
616 0 warn "Failed to create query log dir: $log_dir: $!\n";
617 0 return;
618 }
619 0 my $user_log_dir = File::Spec->catdir($log_dir,$user);
620 0 ▶ if (!(-d $user_log_dir) and !(mkdir($user_log_dir))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $user_log_dir or mkdir $user_log_dir) 0 0 0
Condition0%0/3
-d $user_log_dir or mkdir $user_log_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $user_log_dir or mkdir $user_log_dir
-d $user_log_dir mkdir $user_log_dir
Truth table0%0/3
A or B
A= -d $user_log_dirB= mkdir $user_log_dir
ABresult
1X1
011
000
621 0 warn "Failed to create query user log dir: $user_log_dir: $!\n";
622 0 return;
623 }
624 0 my $log_file = File::Spec->catfile($user_log_dir,$md5.".txt");
625 0 ▶ if (!-f $log_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $log_file) { } 0 0 0
626 0 ▶ if (open my $log, '>', $log_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $log, '>', $log_file) { } 0 0 0
627 0 print STDERR "query_log_file:", $log_file,"\n";
628 0 print $log "# log: ".$log_string;
629 0 print $log $query;
630 0 close $log;
631 } else {
632 0 warn "Failed to create query log file: $log_file: $!\n";
633 }
634 } else {
635 0 my $now = time;
636 0 utime $now, $now, $log_file;
637 }
638 }
639  
640 0 ▶sub _js_escape_string {
Subroutine _js_escape_string: not called CC=1 SCAR=6.9
641 0 my $str=join '',@_;
642 0 $str=~s{([\\'"])}{\\$1}g;
643 0 $str=~s{\n}{\\n}g;
644 0 return $str;
645 }
646  
647  
648 0 ▶sub resp_past_queries {
Subroutine resp_past_queries: not called CC=20 SCAR=60.4
Pod undocumented
649 0 my ($cgi)=@_;
650 0 my $format = $cgi->param('format');
651 0 my $callback = $cgi->param('cb');
652 0 my $max = $cgi->param('max');
653 0 my $first = $cgi->param('first');
654 0 ▶ if ($format eq 'html') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'html') { } 0 0 0
elsif ($format eq 'json') { } 0 0 0
655 0 print
656 $cgi->header(-charset => 'UTF-8'),
657 $cgi->start_html(-title => $title,
658 -encoding => 'UTF-8',
659 -style => {
660 -type => 'text/css',
661 -src => $URL_BASE.'static/css/query.css'
662 },
663 -script => [
664 {
665 -type => 'text/javascript',
666 -src => $URL_BASE.'static/js/common.js',
667 },
668 {
669 -type => 'text/javascript',
670 -code => $PAST_QUERIES_SCRIPT,
671 },
672 ],
673 ),
674 _title($cgi,'past_queries');
675 } elsif ($format eq 'json') {
676 0 ▶ print $cgi->header(
Branch: 1 branch
BranchTrueFalseTotal
$callback ? : 0 0 0
677 -type => ($callback ? 'text/javascript' : 'text/x-json'),
678 -charset => 'UTF-8',
679 );
680 0 ▶ print $callback ? $callback.'([' : '[';
Branch: 1 branch
BranchTrueFalseTotal
$callback ? : 0 0 0
681 } else {
682 0 print $cgi->header(
683 -type => 'text/plain',
684 -charset => 'UTF-8'
685 );
686 }
687 0 ▶ return 200 unless logged_in($cgi); # ignore past queries for anonymous users
Branch: 1 branch
BranchTrueFalseTotal
unless logged_in($cgi) 0 0 0
688  
689 0 my $user = _user_name($cgi);
690 0 my $user_log_dir = File::Spec->catdir($log_dir,$user);
691 0 ▶ if ($log_dir and (-d $user_log_dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log_dir and -d $user_log_dir) 0 0 0
Condition0%0/3
$log_dir and -d $user_log_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$log_dir and -d $user_log_dir
$log_dir -d $user_log_dir
Truth table0%0/3
A and B
A= $log_dirB= -d $user_log_dir
ABresult
0X0
100
111
692 0 my $id = 0;
693 0 print $cgi->start_div({-class => 'content'});
694 0 my @queries = map { $_->[0] } sort { $b->[1]<=>$a->[1] } map { [$_, ((stat($_))[9])] } glob(File::Spec->catfile($user_log_dir,'*.txt'));
695 0 my $count = scalar(@queries);
696 0 ▶ $max ||= ($format eq 'html' ? 50 : $count);
Branch: 1 branch
BranchTrueFalseTotal
$format eq 'html' ? : 0 0 0
Condition0%0/3
$max ||= $format eq "html" ? 50 : $count
l!l&&r!l&&!r
000
MC/DC0%0/2
$max ||= $format eq "html" ? 50 : $count
$max $format eq "html" ? 50 : $count
Truth table0%0/3
A ||= B
A= $maxB= $format eq "html" ? 50 : $count
ABresult
1X1
011
000
697 0 ▶ $first||=1;
Condition0%0/2
$first ||= 1
l!l
00
MC/DC0%0/1
$first ||= 1
$first
Truth table0%0/2
A
A= $first
Aresult
11
00
698 0 ▶ my $last = ($first+$max-1>$count ? $count-1 : $first+$max-2);
Branch: 1 branch
BranchTrueFalseTotal
$first + $max - 1 > $count ? : 0 0 0
699 0 for my $f (@queries[($first-1)..$last]) {
700 0 $id++;
701 0 open my $fh, '<', $f;
702 0 scalar( <$fh> ); # skip 1st line
703 0 local $/;
704 0 ▶ if ($format eq 'html') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'html') { } 0 0 0
elsif ($format eq 'json') { } 0 0 0
705 0 print $cgi->div(
706 {-class => 'past_queries'},
707 $cgi->div({-class => 'past_queries_head'},scalar localtime(((stat($fh))[9])),
708 $cgi->a({href=>"javascript:edit('q$id')"},'Edit'),
709 $cgi->a({href=>"javascript:run('q$id')"},'Run'),
710 ),
711 $cgi->pre({id=>"q$id"},$cgi->escapeHTML(<$fh>)));
712 } elsif ($format eq 'json') {
713 0 print '[',((stat($fh))[9]),",'",_js_escape_string(<$fh>),"'],\n";
714 } else {
715 0 print <$fh>,"\n\n";
716 }
717 0 close $fh;
718 }
719 0 ▶ if ($first>1 or @queries>$max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first > 1 or @queries > $max) 0 0 0
Condition0%0/3
$first > 1 or @queries > $max
l!l&&r!l&&!r
000
MC/DC0%0/2
$first > 1 or @queries > $max
$first > 1 @queries > $max
Truth table0%0/3
A or B
A= $first > 1B= @queries > $max
ABresult
1X1
011
000
720 0 print $cgi->div('Past querires from ', ($first-1),' to ',$last);
721 0 print $cgi->div(_generate_range_index($cgi,scalar(@queries),$first,$max,"javascript:goto(%d)",{}));
722 }
723 0 print $cgi->end_div();
724 }
725 0 ▶ if ($format eq 'html') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'html') { } 0 0 0
elsif ($format eq 'json') { } 0 0 0
726 0 print $cgi->end_html;
727 } elsif ($format eq 'json') {
728 0 ▶ print ($callback ? ']);' : ']');
Branch: 1 branch
BranchTrueFalseTotal
$callback ? : 0 0 0
729 }
730 0 return 200;
731 }
732  
733 0 ▶sub _generate_range_index {
Subroutine _generate_range_index: not called CC=10 SCAR=47.0
734 0 my ($cgi, $count, $first, $max, $href_format,$a_opts)=@_;
735 0 ▶ $a_opts||={};
Condition0%0/2
$a_opts ||= {}
l!l
00
MC/DC0%0/1
$a_opts ||= {}
$a_opts
Truth table0%0/2
A
A= $a_opts
Aresult
11
00
736 0 my $i=1;
737 0 my @ret;
738 0 push @ret, $cgi->a({href=>sprintf($href_format,1),%$a_opts},'<<'),' ';
739 0 ▶ push @ret, $cgi->a({href=>sprintf($href_format,$first-1),%$a_opts},'<'),' ' if $first>1;
Branch: 1 branch
BranchTrueFalseTotal
if $first > 1 0 0 0
740 0 ▶ if ($first > 5*$max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first > 5 * $max) 0 0 0
741 0 $i=$first-5*$max;
742 0 push @ret,' ... ';
743 }
744 0 for (1..10) {
745 0 ▶ last if $i>$count;
Branch: 1 branch
BranchTrueFalseTotal
if $i > $count 0 0 0
746 0 ▶ if ($i <= $first and $first < $i+$max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $first and $first < $i + $max) { } 0 0 0
Condition0%0/3
$i <= $first and $first < $i + $max
!ll&&!rl&&r
000
MC/DC0%0/2
$i <= $first and $first < $i + $max
$i <= $first $first < $i + $max
Truth table0%0/3
A and B
A= $i <= $firstB= $first < $i + $max
ABresult
0X0
100
111
747 0 push @ret, $cgi->b( $i ),' ';
748 } else {
749 0 push @ret, $cgi->a({href=>sprintf($href_format,$i),%$a_opts},$i),' ';
750 }
751 0 $i+=$max;
752 }
753 0 ▶ if ($i<$count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 0 0 0
754 0 push @ret,' ... ';
755 }
756 0 ▶ push @ret, $cgi->a({href=>sprintf($href_format,$first+$max),%$a_opts},'>'),' ' if $first+$max<=$count;
Branch: 1 branch
BranchTrueFalseTotal
if $first + $max <= $count 0 0 0
757 0 push @ret, $cgi->a({href=>sprintf($href_format,$count-$max+1),%$a_opts},'>>'),' ';
758 0 return @ret;
759 }
760  
761 0 ▶sub _param_form {
Subroutine _param_form: not called CC=2 SCAR=17.9
762 0 my ($cgi,$type)=@_;
763 return (
764 $cgi->start_form(
765 { -method => 'POST',
766 -name => 'qf',
767 -action => $URL_BASE.'form'}),
768 $cgi->input({name=>'format',value=>'html',type=>'hidden'}),
769 $cgi->input({name=>'back_to',value=>$type,type=>'hidden'}),
770 ($type eq 'past_queries'
771 ? $cgi->input({name=>'first',value=>scalar($cgi->param('first')),type=>'hidden'})
772 : ()
773 ),
774 (map {
775 0 $cgi->input({
776 name => $_,
777 value => scalar($cgi->param($_)),
778 type => 'hidden',
779 })
780 } grep {
781 0 ▶ !/^(query_submit|back_to|_.*|first|format)$/
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'past_queries' ? : 0 0 0
782 } $cgi->param()),
783 $cgi->end_form,
784 );
785 }
786  
787 0 ▶sub _start_query_form {
Subroutine _start_query_form: not called CC=1 SCAR=6.9
788 0 my ($cgi)=@_;
789 return (
790 $cgi->start_form(-method => 'POST',
791 -name => 'qf',
792 -onSubmit=>"show_loading(); return true",
793 -action => $URL_BASE.'query'),
794 $cgi->input({name=>'format',value=>'html',type=>'hidden'}),
795 $cgi->input({name=>'row_limit',value=>'limit',type=>'hidden'}),
796 $cgi->input({name=>'back_to',value=>'form',type=>'hidden'}),
797 (map {
798 0 $cgi->input({
799 name => $_,
800 value => scalar($cgi->param($_)),
801 type => 'hidden',
802 })
803 0 } grep {!/^(query|back_to|first|format|limit|timeout|_.*|query_submit)$/} $cgi->param()),
804 $cgi->start_div({-class => 'query'}),
805 );
806 }
807  
808 0 ▶sub _query_textarea {
Subroutine _query_textarea: not called CC=3 SCAR=24.8
809 0 my ($cgi)=@_;
810 return
811 0 ▶ $cgi->div({-style=>'width:99%', -class=>'qmenubar'},
Condition0%0/4
scalar $cgi->param('limit') || 100
l!l
00
scalar $cgi->param('timeout') || 30
l!l
00
MC/DC0%0/1
scalar $cgi->param('limit') || 100
scalar $cgi->param('limit')
MC/DC0%0/1
scalar $cgi->param('timeout') || 30
scalar $cgi->param('timeout')
Truth table0%0/2
A
A= scalar $cgi->param('limit')
Aresult
11
00
Truth table0%0/2
A
A= scalar $cgi->param('timeout')
Aresult
11
00
812 $cgi->div(
813 {-style=>'float:right'},
814 'Limit: '.$cgi->popup_menu(-name=>"limit",
815 -default=>(scalar($cgi->param('limit'))||100),
816 -values => [1,10,100,1000,10000]),
817 'Timeout: '.$cgi->popup_menu(-name=>"timeout",
818 -default=>(scalar($cgi->param('timeout'))||30),
819 -values => [20,30,45,60,90,120,200,300])
820 ).
821 $cgi->start_div({-id => 'qm'}).$cgi->end_div()
822 ).
823 # $cgi->pre({-id => 'qp', -style=>'width:99%'},'foo').
824 $cgi->fieldset({style=>'clear:both'},
825 $cgi->div({-class => 'query-area'},
826 $cgi->textarea(
827 -id => 'queryTA',
828 -name => 'query',
829 -default => "# Enter your query here\n",
830 -style => 'font-size: 7pt',
831 -rows => 2,
832 -accesskey => 'e',
833 -onFocus => 'this.rows=12;this.style.fontSize="10pt"; return true;',
834 -onBlur => <<'EOJS',
835 var t=this;
836 setTimeout(
837 function(){
838 if (t) {
839 if (isMenuButtonActive()) {
840 setOnDeactivate( function(inserting){ if (t && !inserting) t.rows=2; t.style.fontSize="7pt"; } )
841 } else { t.rows=2; t.style.fontSize="7pt"; }
842 }
843 } ,200);
844 return true;
845 EOJS
846 # -columns => 80,
847 )));
848 }
849  
850 0 ▶sub _end_query_form {
Subroutine _end_query_form: not called CC=1 SCAR=6.9
851 0 my ($cgi)=@_;
852 return (
853 # $cgi->end_div(),
854 0 $cgi->endform(),
855 );
856 }
857  
858 0 ▶sub _query_form {
Subroutine _query_form: not called CC=1 SCAR=6.9
859 0 my ($cgi,$text)=@_;
860 return (
861 0 _start_query_form($cgi),
862 _toolbar_text($cgi,$text),
863 _end_query_form($cgi),
864 )
865 }
866 {
867 1 my $evaluator;
868 0 ▶ sub init_evaluator {
Subroutine init_evaluator: not called CC=2 SCAR=17.9
Pod undocumented
869 0 ▶ unless ($evaluator) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($evaluator) 0 0 0
870 0 $evaluator = PMLTQ::SQLEvaluator->new(undef,{
871 connect => $conf,
872 debug=>$DEBUG,
873 });
874 0 $evaluator->connect();
875 }
876 0 return $evaluator;
877 }
878 }
879  
880 1 ▶sub search {
Subroutine search: not called CC=8 SCAR=42.2
Pod undocumented
881 0 my ($query,%opts)=@_;
882 0 my $evaluator;
883 my $sth;
884 0 ▶ my $no_distinct = ($opts{node_limit} and abs($opts{node_limit})<=10_000) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'node_limit'} && abs $opts{'node_limit'} <= 10000 ? : 0 0 0
Condition0%0/3
$opts{"node_limit"} and abs $opts{"node_limit"} <= 10000
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"node_limit"} and abs $opts{"node_limit"} <= 10000
$opts{"node_limit"} abs $opts{"node_limit"} <= 10000
Truth table0%0/3
A and B
A= $opts{"node_limit"}B= abs $opts{"node_limit"} <= 10000
ABresult
0X0
100
111
885 0 eval {
886 0 $evaluator = init_evaluator();
887 my $qt = $evaluator->prepare_query($query,{
888 node_limit => $opts{node_limit},
889 row_limit => $opts{row_limit},
890 select_first => $opts{select_first},
891 node_IDs => ($tree_print_service ? 1 : 0),
892 no_filters => $opts{no_filters},
893 use_cursor => $opts{use_cursor},
894 no_distinct => $no_distinct,
895 timeout => $opts{timeout},
896 debug_sql => $opts{debug},
897 0 ▶ }); # count=>1
Branch: 1 branch
BranchTrueFalseTotal
$tree_print_service ? : 0 0 0
898 # if ($opts{display_tree}) {
899 # undef $qt;
900 # } else {
901 0 undef $qt;
902 # }
903 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
904 0 print STDERR "[BEGIN_SQL]=================================================\n";
905 0 my $sql = $evaluator->get_sql;
906 0 Encode::_utf8_off($sql);
907 0 print STDERR $sql,"\n";
908 0 print STDERR "[END_SQL]=================================================\n";
909 }
910 $sth = $evaluator->run({
911 node_limit => $opts{node_limit},
912 row_limit => $opts{row_limit},
913 timeout => $opts{timeout},
914 use_cursor => $opts{use_cursor},
915 0 return_sth=>1,
916 });
917 };
918 0 my $err = $@;
919 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 0 0 0
920 0 print STDERR "[BEGIN_SQL]=================================================\n";
921 0 my $sql = $evaluator->get_sql;
922 0 Encode::_utf8_off($sql);
923 0 print STDERR $sql,"\n";
924 0 print STDERR "[END_SQL]=================================================\n";
925  
926 0 ▶ if ($opts{use_cursor}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_cursor"}) 0 0 0
927 0 $evaluator->close_cursor();
928 }
929 0 print STDERR "ERROR [ $err ]\n";
930 0 ▶ if ($err =~ /\tTIMEOUT\t/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /\tTIMEOUT\t/) 0 0 0
931 0 return "Evaluation of query exceeded specified maximum time limit of $opts{timeout} seconds\n";
932 }
933 0 $err =~ s{\bat \S+ line \d+.*}{}s;
934 0 return "$err";
935 } else {
936 0 return ($sth,$evaluator->{returns_nodes},$evaluator->get_query_nodes,$evaluator);
937 }
938 }
939  
940 0 ▶sub _html_editor_menu {
Subroutine _html_editor_menu: not called CC=13 SCAR=52.0
941 0 my $ret = "
942 var menu = [\n";
943 0 my $evaluator = eval { init_evaluator() };
944 0 ▶ if ($evaluator) {
Branch: 1 branch
BranchTrueFalseTotal
if ($evaluator) 0 0 0
945 0 my $node_types = $evaluator->get_node_types();
946 0 $ret .=
947 "['Relations',0,[\n";
948 $ret .= " ['Standard',0,[\n"
949 0 . join(',', map { my $v=$_; $v=~s/'/\\'/g; " ['$v',1,'javascript:ins(\"$v \");']" }
950 0 @{PMLTQ::Common::standard_relations()})
951 . " ]],\n";
952  
953 0 $ret .= " ['PML Reference',0,[\n";
954 0 foreach my $type (@$node_types) {
955 0 my $types = $evaluator->get_pmlrf_relations($type);
956 0 ▶ if (@$types) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$types) 0 0 0
957 $ret .= " ['$type',0,[\n".
958 0 join (',', map { my $v=$_; $v=~s/'/\\'/g; " ['$v',1,'javascript:ins(\"$v \");']" } @$types)
959 . " ]],\n";
960 }
961 }
962 0 $ret .= " ]],\n";
963  
964 0 $ret .= " ['Other',0,[\n";
965 0 foreach my $type (@$node_types) {
966 0 my $types = $evaluator->get_user_defined_relations($type);
967 0 ▶ if (@$types) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$types) 0 0 0
968 $ret .= " ['$type',0,[\n".
969 0 join (',',map { my $v=$_; $v=~s/'/\\'/g; " ['$v',1,'javascript:ins(\"$v \");']" } @$types)
970 . " ]],\n";
971 }
972 }
973 0 $ret .= " ]],\n";
974 0 $ret .= "]],\n";
975  
976 0 my $schema_names = $evaluator->get_schema_names;
977 $ret .=
978 "['Node Types',0,[\n"
979 . join(',',
980 map {
981 0 my $nt = $evaluator->get_node_types($_);
982 0 my $l = $_; $l=~s/'/\\'/g;
983 (
984 qq{['',2,'<small>Layer $l</small>']},
985 0 ▶ (map { my $v=$_; $v=~s/'/\\'/g; " ['$v',1,'javascript:ins(\"$v [ ]\");']" }
Branch: 1 branch
BranchTrueFalseTotal
@$schema_names == 1 ? : 0 0 0
986 @$nt, (@$schema_names == 1 ? '*' : $l.':*')),
987 )
988 } @$schema_names
989 )
990 . "]],\n";
991  
992 0 $ret .=
993 "['Attributes',0,[\n";
994 0 foreach my $type (@$node_types) {
995 0 my @res;
996 0 my $decl = $evaluator->get_decl_for($type);
997 0 ▶ if ($decl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decl) 0 0 0
998 0 @res = map { my $t = $_; $t=~s{#content}{content()}g; $t } $decl->get_paths_to_atoms({ no_childnodes => 1 });
999 0 ▶ if (@{PMLTQ::Common::GetElementNamesForDecl($decl)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{PMLTQ::Common::GetElementNamesForDecl($decl);}) 0 0 0
1000 0 unshift @res, 'name()';
1001 }
1002 }
1003 0 ▶ if (@res) {
Branch: 1 branch
BranchTrueFalseTotal
if (@res) 0 0 0
1004 $ret .= " ['of $type',0,[\n".
1005 0 join (',',map { my $v=$_; $v=~s/'/\\'/g; " ['$v',1,'javascript:ins(\"$v \");']" } @res)
1006 . " ]],\n";
1007 }
1008 }
1009 0 $ret .= "]],\n";
1010  
1011 0 my %operators = (
1012 Logical => [qw{and or !}, ','],
1013 Comparison => [qw{= ~ ~* < > >= <= != !~ !~* },'in {..., ...}'],
1014 Arithmetical => [qw{+ - * div mod}],
1015 String => [qw{&}],
1016 Misc => [qw{() [] := >> . '...' "..."}],
1017 );
1018 0 $ret .=
1019 "['Operators',0,[\n";
1020 0 for my $type (sort keys %operators) {
1021 $ret .= " ['$type',0,[\n".
1022 0 join (',',map { my $v=$_; $v=~s/'/\\'/g; " ['$v',1,'javascript:ins(\" $v \");']" } @{$operators{$type}})
1023 . " ]],\n";
1024 }
1025 0 $ret .= "]],\n";
1026  
1027  
1028 0 my %functions = (
1029 Strings => [
1030 'lower(STR)',
1031 'upper(STR)',
1032 'length(STR)',
1033 'substr(STR,OFFSET,LEN?),',
1034 'tr(STR,CHARS_TO_REPLACE,REPLACEMENT_CHARS)',
1035 'replace(STR,SUBSTR,REPLACEMENT)',
1036 'substitute(STR,REGEXP,REPLACEMENT,FLAGS?)',
1037 'match(STR,REGEXP,FLAGS?)',
1038 ],
1039 Numeric => [
1040 'ciel(NUM)',
1041 'floor(NUM)',
1042 'round(NUM,PLACES?)',
1043 'trunc(NUM,PLACES?)',
1044 'percnt(NUM)',
1045 'abs(NUM)',
1046 'sqrt(NUM)',
1047 'exp(NUM)',
1048 'ln(NUM)',
1049 'power(BASE?,NUM)',
1050 'log(BASE?,NUM)',
1051 ],
1052 'Node Properties' => [
1053 'descendants(NODE?)',
1054 'lbrothers(NODE?)',
1055 'rbrothers(NODE?)',
1056 'sons(NODE?)',
1057 'depth_first_order(NODE?)',
1058 'depth(NODE?)',
1059 'name(NODE?)',
1060 'type_of(NODE?)',
1061 'file(NODE?)',
1062 'tree_no(NODE?)',
1063 'address(NODE?)',
1064 ],
1065 'Conditional' => [
1066 'first_defined(VALUE2,VALUE2,...)',
1067 'if(CONDITION,VALUE_IF_TRUE,VALUE_IF_FALSE)',
1068 ],
1069 Group => [
1070 'avg(EXP? [over COLUMNS...])',
1071 'count([over COLUMNS...])',
1072 'min(EXP? [over COLUMNS...])',
1073 'max(EXP? [over COLUMNS...])',
1074 'sum(EXP? [over COLUMNS...])',
1075 'concat(EXP, SEPARATOR [over COLUMNS... sort by COLUMNS...])',
1076 'ratio(EXP? [over COLUMNS...])',
1077 'rank([over COLUMNS... sort by COLUMNS...])',
1078 'dense_rank([over COLUMNS... sort by COLUMNS...])',
1079 'row_number([over COLUMNS... sort by COLUMNS...])',
1080 ],
1081 );
1082  
1083 0 $ret .=
1084 "['Functions',0,[\n";
1085 0 for my $type (sort keys %functions) {
1086 $ret .= " ['$type',0,[\n".
1087 0 join (',',map { my $v=$_; $v=~s/'/\\'/g; " ['$v',1,'javascript:ins(\"$v\");']" } @{$functions{$type}})
1088 . " ]],\n";
1089 }
1090 0 $ret .= "]],\n";
1091  
1092 }
1093 0 $ret .= "];\n";
1094 0 $ret .= <<'EOF';
1095 function m_init () {
1096 document.getElementById('qm').innerHTML = build_menu(menu, 'menu', 'menuBar');
1097 }
1098 EOF
1099  
1100 0 return $ret;
1101 }
1102  
1103  
1104 0 ▶sub resp_form {
Subroutine resp_form: not called CC=3 SCAR=24.8
Pod undocumented
1105 0 my $cgi = shift; # CGI.pm object
1106 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1107 print(
1108 $cgi->header(-charset=>'UTF-8'),
1109 $cgi->start_html(-title => $title,
1110 -encoding => 'UTF-8',
1111 -style => [{
1112 -type => 'text/css',
1113 -src => $URL_BASE.'static/css/query.css'
1114 },{
1115 -type => 'text/css',
1116 -src => $URL_BASE.'static/menubar/menu.css'
1117 }],
1118 -script => [{
1119 -type => 'text/javascript',
1120 -src => $URL_BASE.'static/js/common.js',
1121 },{
1122 -type => 'text/javascript',
1123 -src => $URL_BASE.'static/menubar/menu.js',
1124 },{
1125 -type => 'text/javascript',
1126 0 ▶ -code => eval { _html_editor_menu() } || '',
Condition0%0/2
eval { do { _html_editor_menu() } } || ''
l!l
00
MC/DC0%0/1
eval { do { _html_editor_menu() } } || ''
eval { do { _html_editor_menu() } }
Truth table0%0/2
A
A= eval { do { _html_editor_menu() } }
Aresult
11
00
1127 }],
1128 -onLoad => "m_init();hide_loading();"
1129 ),
1130 _title($cgi,'form'),
1131 _query_form($cgi),
1132 intro($cgi),
1133 $cgi->end_html
1134 );
1135 0 return 200;
1136 }
1137  
1138 0 ▶sub _error_form {
Subroutine _error_form: not called CC=1 SCAR=6.9
1139 0 my ($cgi)=@_;
1140 0 return (_query_form($cgi),_error_html(@_));
1141 }
1142 0 ▶sub _error_html {
Subroutine _error_html: not called CC=1 SCAR=6.9
1143 0 my ($cgi,$head,$message)=@_;
1144 0 warn($message);
1145 0 $message=~s/\(DBD ERROR:.*//s;
1146 0 $message = $cgi->escapeHTML($message);
1147 0 $message=~s/\n/<br>\n/g;
1148 0 $cgi->div({-class=>'error', id=>'error'},
1149 $cgi->h2($head), $cgi->font({-size => -1}, $message));
1150 }
1151  
1152  
1153 0 ▶sub resp_query {
Subroutine resp_query: not called CC=54 SCAR=80.0
Pod undocumented
1154 0 my $cgi = shift; # CGI.pm object
1155 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1156 0 my $query = decode_utf8($cgi->param('query'));
1157 0 my ($format,$limit,$row_limit,$timeout, $query_submit) =
1158 map scalar($cgi->param($_)), qw(format limit row_limit timeout query_submit);
1159 0 ▶ my $no_filters = $query_submit =~ m{w/o} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$query_submit =~ m[w/o] ? : 0 0 0
1160 0 ▶ $format = 'html' unless (defined($format) and length($format));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format and length $format 0 0 0
Condition0%0/3
defined $format and length $format
!ll&&!rl&&r
000
MC/DC0%0/2
defined $format and length $format
defined $format length $format
Truth table0%0/3
A and B
A= defined $formatB= length $format
ABresult
0X0
100
111
1161 0 ▶ unless (defined($query) and length($query)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $query and length $query) 0 0 0
Condition0%0/3
defined $query and length $query
!ll&&!rl&&r
000
MC/DC0%0/2
defined $query and length $query
defined $query length $query
Truth table0%0/3
A and B
A= defined $queryB= length $query
ABresult
0X0
100
111
1162 0 ▶ if ($format eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'html') { } 0 0 0
1163 0 return resp_form($cgi);
1164 } else {
1165 0 print $cgi->header(-type=>'text/plain',
1166 -charset=>'UTF-8',
1167 );
1168 0 print "ERROR\n";
1169 0 print "Query was empty!\n";
1170 0 return 400;
1171 }
1172 }
1173  
1174 0 log_query($cgi,$cgi->param('query')); # DON'T USE $query here, we need non-UTF8-decoded form
1175 0 ▶ print STDERR "CGI: TIMEOUT ".(defined($timeout) ? $timeout : '')
Branch: 4 branches
BranchTrueFalseTotal
if $DEBUG 0 0 0
defined $timeout ? : 0 0 0
defined $limit ? : 0 0 0
defined $row_limit ? : 0 0 0
1176 .", NODE LIMIT ".(defined($limit) ? $limit : '')
1177 ." ROW LIMIT ".(defined($row_limit) ? $row_limit : '')."\n"
1178 if $DEBUG;
1179 0 ▶ $limit = 100 unless (defined($limit) and length($limit));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $limit and length $limit 0 0 0
Condition0%0/3
defined $limit and length $limit
!ll&&!rl&&r
000
MC/DC0%0/2
defined $limit and length $limit
defined $limit length $limit
Truth table0%0/3
A and B
A= defined $limitB= length $limit
ABresult
0X0
100
111
1180 0 $limit=int($limit);
1181 0 ▶ if ($row_limit eq 'limit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_limit eq "limit") 0 0 0
1182 0 $row_limit = $limit
1183 }
1184 0 ▶ $row_limit = 1000 unless (defined($row_limit) and length($row_limit));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row_limit and length $row_limit 0 0 0
Condition0%0/3
defined $row_limit and length $row_limit
!ll&&!rl&&r
000
MC/DC0%0/2
defined $row_limit and length $row_limit
defined $row_limit length $row_limit
Truth table0%0/3
A and B
A= defined $row_limitB= length $row_limit
ABresult
0X0
100
111
1185 0 $row_limit=int($row_limit);
1186 0 ▶ $timeout = 30 unless (defined($timeout) and length($timeout));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout and length $timeout 0 0 0
Condition0%0/3
defined $timeout and length $timeout
!ll&&!rl&&r
000
MC/DC0%0/2
defined $timeout and length $timeout
defined $timeout length $timeout
Truth table0%0/3
A and B
A= defined $timeoutB= length $timeout
ABresult
0X0
100
111
1187 0 $timeout=int($timeout);
1188 0 ▶ $timeout = 300 if $timeout>300;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout > 300 0 0 0
1189 0 ▶ print STDERR "USING: TIMEOUT $timeout, NODE LIMIT $limit, ROW LIMIT $row_limit\n"
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
1190 if $DEBUG;
1191 0 ▶ unless ($format=~/^(html|text)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($format =~ /^(html|text)$/) 0 0 0
1192 0 resp_error($cgi,"Wrong format requested!\n");
1193 0 return 400;
1194 }
1195 0 ▶ my ($sth,$returns_nodes,$query_nodes,$evaluator) = search(
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG && $query =~ /^#\s*DEBUG=1\s/ ? : 0 0 0
Condition0%0/3
$DEBUG and $query =~ /^#\s*DEBUG=1\s/
!ll&&!rl&&r
000
MC/DC0%0/2
$DEBUG and $query =~ /^#\s*DEBUG=1\s/
$DEBUG $query =~ /^#\s*DEBUG=1\s/
Truth table0%0/3
A and B
A= $DEBUGB= $query =~ /^#\s*DEBUG=1\s/
ABresult
0X0
100
111
1196 $query,
1197 node_limit => $limit,
1198 row_limit => $row_limit,
1199 timeout => $timeout,
1200 no_filters => $no_filters,
1201 use_cursor => 1,
1202 debug_sql => ($DEBUG && ($query =~ /^#\s*DEBUG=1\s/) ? 1 : 0),
1203 );
1204  
1205 0 $limit=abs($limit);
1206 0 $row_limit=abs($row_limit);
1207 # $format='text';
1208 0 ▶ if ($format eq 'html') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'html') { } 0 0 0
elsif ($format eq 'text') { } 0 0 0
1209 0 ▶ my $have_trees = ($returns_nodes and
Branch: 1 branch
BranchTrueFalseTotal
$returns_nodes && $tree_print_service && ref $sth && !$sth->err ? : 0 0 0
Condition0%0/9
$returns_nodes and $tree_print_service and ref $sth and not $sth->err
!ll&&!rl&&r
000
$returns_nodes and $tree_print_service and ref $sth
!ll&&!rl&&r
000
$returns_nodes and $tree_print_service
!ll&&!rl&&r
000
MC/DC0%0/4
$returns_nodes and $tree_print_service and ref $sth and not $sth->err
$returns_nodes $tree_print_service ref $sth not $sth->err
Truth table0%0/5
A and B and C and not(D)
A= $returns_nodesB= $tree_print_serviceC= ref $sthD= not $sth->err
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1210 $tree_print_service
1211 and
1212 ref($sth) and !$sth->err) ? 1 : 0;
1213 0 my $js_code = eval {
1214 0 _html_editor_menu()
1215 };
1216 0 my $err = $@;
1217 0 ▶ if ($returns_nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returns_nodes) 0 0 0
1218 0 $js_code .= "\n result_nodes=".(eval{node_ids_js($cgi,$evaluator)})."\n";
1219 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1220 0 ▶ $err.="\n" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
1221 0 $err.=$@;
1222 }
1223 }
1224 # warn $err if $err;
1225 print(
1226 $cgi->header(-charset=>'UTF-8'),
1227 $cgi->start_html(-title => $title,
1228 -encoding => 'UTF-8',
1229 -style => [{
1230 -type => 'text/css',
1231 -src => $URL_BASE.'static/css/query.css'
1232 },{
1233 -type => 'text/css',
1234 -src => $URL_BASE.'static/menubar/menu.css'
1235 }],
1236 -script => [{
1237 -type => 'text/javascript',
1238 -src => $URL_BASE.'static/js/common.js',
1239 },{
1240 -type => 'text/javascript',
1241 -src => $URL_BASE.'static/js/results.js',
1242 },
1243 {
1244 -type => 'text/javascript',
1245 -src => $URL_BASE.'static/menubar/menu.js',
1246 },
1247 ($use_google_translate && $have_trees ?
1248 ({
1249 -type => 'text/javascript',
1250 -src => 'http://www.google.com/jsapi',
1251 },
1252 {
1253 -type => 'text/javascript',
1254 -code => 'google.load("language", "1");',
1255 }
1256 ) : ()),
1257 ($use_ms_translate && $have_trees ?
1258 ({
1259 -type => 'text/javascript',
1260 -src => "http://api.microsofttranslator.com/v1/Ajax.svc/Embed?appId=$use_ms_translate",
1261 },
1262 ) : ()),
1263 {
1264 -type => 'text/javascript',
1265 -code => $js_code,
1266 },],
1267 (($have_trees) ?
1268 (
1269 -class=>"tree-results",
1270 -onLoad => "m_init();init('".$URL_BASE."', result_nodes,"
1271 0 ▶ .'['.join(',',map { $_->{name} ? qq{'$_->{name}'} : qq{''} } @$query_nodes).'])'
Branch: 4 branches
BranchTrueFalseTotal
$use_google_translate && $have_trees ? : 0 0 0
$use_ms_translate && $have_trees ? : 0 0 0
$have_trees ? : 0 0 0
$_->{'name'} ? : 0 0 0
Condition0%0/6
$use_google_translate and $have_trees
!ll&&!rl&&r
000
$use_ms_translate and $have_trees
!ll&&!rl&&r
000
MC/DC0%0/2
$use_google_translate and $have_trees
$use_google_translate $have_trees
MC/DC0%0/2
$use_ms_translate and $have_trees
$use_ms_translate $have_trees
Truth table0%0/3
A and B
A= $use_google_translateB= $have_trees
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $use_ms_translateB= $have_trees
ABresult
0X0
100
111
1272 )
1273 : (
1274 -class=>"tabular-results",
1275 -onLoad => "m_init(); hide_loading();"
1276 ))
1277 ),
1278 _title($cgi,'query')
1279 );
1280 0 ▶ if (!ref $sth) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $sth) { } 0 0 0
elsif ($err) { } 0 0 0
elsif ($sth->err) { } 0 0 0
1281 0 print (_error_form($cgi,"ERROR",$sth));
1282 } elsif ($err) {
1283 0 print (_error_form($cgi,"INTERNAL SERVER ERROR",$err));
1284 } elsif ($sth->err) {
1285 0 print (_error_form($cgi,"INTERNAL SERVER ERROR", $sth->errstr));
1286 } else {
1287 0 my $count=0;
1288 0 ▶ if ($returns_nodes and $tree_print_service) {
Branch: 1 branch
BranchTrueFalseTotal
if ($returns_nodes and $tree_print_service) { } 0 0 0
Condition0%0/3
$returns_nodes and $tree_print_service
!ll&&!rl&&r
000
MC/DC0%0/2
$returns_nodes and $tree_print_service
$returns_nodes $tree_print_service
Truth table0%0/3
A and B
A= $returns_nodesB= $tree_print_service
ABresult
0X0
100
111
1289 0 print(_start_query_form($cgi),
1290 _toolbar_trees($cgi),
1291 _result_tree_div($cgi),
1292 _end_query_form($cgi));
1293  
1294 } else {
1295 0 print _query_form($cgi,"RESULTS (first $row_limit rows)"),
1296 $cgi->start_center(),
1297 $cgi->start_div({-class => 'results'}),
1298 $cgi->start_table({-border => '1px',
1299 -rules=>"all",
1300 -cellpadding=>"4pt",
1301 -class=> 'result-table',
1302 });
1303 0 my $row;
1304 0 ▶ while ($row = $evaluator->cursor_next()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $evaluator->cursor_next) 0 0 0
1305 0 Encode::_utf8_off($_) for @$row;
1306 0 ▶ print $cgi->Tr({-align => 'LEFT', -valign => 'TOP', ($count%2==0 ? (-class=>'odd-row') : ())},
Branch: 2 branches
BranchTrueFalseTotal
$count % 2 == 0 ? : 0 0 0
$returns_nodes ? : 0 0 0
1307 $returns_nodes
1308 ? $cgi->td([map $cgi->a({href=>$URL_BASE."node?idx=$_"}, $_), @$row])
1309 : $cgi->td($row)
1310 );
1311 0 $count++;
1312 }
1313  
1314 0 ▶ print $cgi->end_table(),
Branch: 1 branch
BranchTrueFalseTotal
$returns_nodes ? : 0 0 0
1315 $cgi->p("$count ".($returns_nodes ? "OCCURRENCE(S)" : "ROW(S)")),
1316 $cgi->end_div(),
1317 $cgi->end_center();
1318 }
1319 0 ▶ if ($sth->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->err) 0 0 0
1320 0 print _error_html($cgi,"ERROR", $sth->errstr);
1321 }
1322 }
1323 0 print($cgi->end_html);
1324 } elsif ($format eq 'text') {
1325 0 ▶ if (!ref $sth) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $sth) { } 0 0 0
elsif ($sth->err) { } 0 0 0
1326 0 print $cgi->header(-type=>'text/plain',
1327 -charset=>'UTF-8',
1328 );
1329 0 print($sth);
1330 0 return 500;
1331 } elsif ($sth->err) {
1332 0 print $cgi->header(-type=>'text/plain',
1333 -charset=>'UTF-8',
1334 );
1335 0 print $sth->errstr;
1336 0 return 400;
1337 } else {
1338 0 print $cgi->header(-type=>'text/plain',
1339 -charset=>'UTF-8',
1340 -pmltq_returns_nodes => $returns_nodes,
1341 );
1342 0 my $t0 = new Benchmark;
1343 0 my $count=0;
1344 0 my $row;
1345 0 ▶ while ($row = $evaluator->cursor_next()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $evaluator->cursor_next) 0 0 0
1346 0 $_ = join("\t",@$row)."\n";
1347 0 Encode::_utf8_off($_);
1348 0 print;
1349 0 $count++;
1350 }
1351 0 my $t1 = new Benchmark;
1352 0 print STDERR "Sending $count results took ",timestr(timediff($t1,$t0)),"\n";
1353 0 ▶ if ($sth->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->err) 0 0 0
1354 0 print "ERROR\n";
1355 0 print $sth->errstr;
1356 0 return 400;
1357 }
1358 }
1359 }
1360 0 return 200;
1361 }
1362  
1363 0 ▶sub node_ids_js {
Subroutine node_ids_js: not called CC=3 SCAR=24.8
Pod undocumented
1364 0 my ($cgi,$evaluator)=@_;
1365 0 my $row;
1366 my @ret;
1367 0 eval {
1368 0 ▶ while ($row = $evaluator->cursor_next()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $evaluator->cursor_next) 0 0 0
1369 0 Encode::_utf8_off($_) for @$row;
1370 0 push @ret,('['.join(',',map qq{'$_'}, @$row).']');
1371 }
1372 };
1373 0 return '['.join(',',@ret).']';
1374 }
1375  
1376 0 ▶sub _report_error {
Subroutine _report_error: not called CC=1 SCAR=6.9
1377 0 my ($err) = @_;
1378 0 print STDERR $err;
1379 0 $err =~ s{\bat \S+ line \d+.*}{}s;
1380 0 print "$err";
1381 0 return 500;
1382 }
1383  
1384  
1385 0 ▶sub resp_query_svg {
Subroutine resp_query_svg: not called CC=3 SCAR=24.8
Pod undocumented
1386 0 my ($cgi)=@_;
1387 0 my $evaluator;
1388 0 my $query = decode_utf8($cgi->param('query'));
1389 0 my $tree;
1390 0 eval {
1391 0 $evaluator = init_evaluator();
1392 0 $tree = PMLTQ::Common::parse_query($query,{
1393 pmlrf_relations => $evaluator->get_pmlrf_relations,
1394 user_defined_relations => $evaluator->get_user_defined_relations,
1395 });
1396 0 Treex::PML::Document->determine_node_type($_) for ($tree->descendants);
1397 0 PMLTQ::Common::CompleteMissingNodeTypes($evaluator,$tree);
1398 };
1399 0 ▶ print STDERR $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1400 0 my $fh = File::Temp->new(UNLINK=>0, SUFFIX=>'.query.pml', DIR => $tmp_dir);
1401 0 my $path = $fh->filename;
1402 0 my $pml = PMLTQ::Common::NewQueryFileInstance($path);
1403 0 $pml->{'_trees'} = Treex::PML::Factory->createList;
1404 0 $pml->get_trees->append($tree);
1405 0 $pml->save({ fh => $fh, filename => $path });
1406 0 $fh->flush;
1407  
1408 0 my $url = URI->new($tree_print_service);
1409 0 $url->query_form(file => $path, tree_no=>1, sentence=>0, fileinfo=>0, dt=>0, no_cache=>1);
1410 0 my $res = $ua->request( HTTP::Request->new(GET => $url) );
1411 0 print $cgi->header(
1412 -type => 'image/svg+xml',# $res->header('Content-Type'),
1413 -Content_length => $res->header('Content-Length'),
1414 );
1415 0 print $res->content;
1416 0 unlink $path;
1417 0 return $res->code;
1418 }
1419  
1420  
1421 0 ▶sub resp_svg {
Subroutine resp_svg: not called CC=9 SCAR=45.0
Pod undocumented
1422 0 my ($cgi)=@_;
1423 0 my @nodes = split(/\|/,$cgi->param('nodes'));
1424 0 ▶ unless (@nodes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@nodes) 0 0 0
1425 0 resp_error($cgi,"Error: no nodes requested!\n");
1426 0 return 500;
1427 }
1428 0 my $evaluator;
1429 0 my ($f) = eval {
1430 0 $evaluator = init_evaluator();
1431 0 $evaluator->idx_to_pos([$nodes[0]])
1432 };
1433 0 ▶ return _report_error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1434 0 my $path;
1435 0 my $tree_no=$cgi->param('tree_no');
1436 0 print STDERR "Treeno: $tree_no\n";
1437 0 ▶ if ($f) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f) 0 0 0
1438 0 Encode::_utf8_off($f);
1439 0 ▶ $tree_no=$1 if ($f=~s{##(\d+)(?:\.\d+)?}{} and $tree_no<=0);
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ s/##(\d+)(?:\.\d+)?// and $tree_no <= 0 0 0 0
Condition0%0/3
$f =~ s/##(\d+)(?:\.\d+)?// and $tree_no <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$f =~ s/##(\d+)(?:\.\d+)?// and $tree_no <= 0
$f =~ s/##(\d+)(?:\.\d+)?// $tree_no <= 0
Truth table0%0/3
A and B
A= $f =~ s/##(\d+)(?:\.\d+)?//B= $tree_no <= 0
ABresult
0X0
100
111
1440 0 $path = resolve_data_path($f);
1441 }
1442 0 print STDERR "Treeno: ($f / $path) $tree_no\n";
1443 0 ▶ if (!defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 0 0
1444 0 resp_error($cgi,"Error: $f not found!\n");
1445 0 return 404;
1446 }
1447 0 my $url = URI->new($tree_print_service);
1448 0 $url->query_form(file => $path,tree_no=>$tree_no,sentence=>1,fileinfo=>1,dt=>1);
1449 # print STDERR "$url\n";
1450 0 my $res = $ua->request( HTTP::Request->new(GET => $url) );
1451 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
1452 0 my $length = $res->header('Content-Length');
1453 0 ▶ print $cgi->header(
Branch: 1 branch
BranchTrueFalseTotal
$length ? : 0 0 0
1454 -type => 'image/svg+xml',# $res->header('Content-Type'),
1455 -charset => 'UTF-8',
1456 ($length ? (-Content_length => $length ) : ()),
1457 );
1458 } else {
1459 0 print $cgi->header(
1460 -type => $res->header('Content-Type'),
1461 -charset => 'UTF-8',
1462 );
1463 }
1464 0 print $res->content;
1465 0 return $res->code;
1466 }
1467  
1468  
1469  
1470 0 ▶sub resp_n2q {
Subroutine resp_n2q: not called CC=13 SCAR=52.0
Pod undocumented
1471 0 my ($cgi)=@_;
1472 0 my $format = $cgi->param('format');
1473 0 my $callback = $cgi->param('cb');
1474 0 my @ids = split(/\|/,$cgi->param('ids'));
1475 0 my $vars = $cgi->param('vars');
1476 0 ▶ unless ($nodes_to_query_service) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nodes_to_query_service) 0 0 0
1477 0 resp_error($cgi,"n2q service not configured!\n");
1478 0 return 500;
1479 }
1480 0 ▶ unless (@ids) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ids) 0 0 0
1481 0 resp_error($cgi,"Error: no IDs requested!\n");
1482 0 return 500;
1483 }
1484 0 my $evaluator;
1485 0 my @f = eval {
1486 0 $evaluator = init_evaluator();
1487 0 $evaluator->ids_to_pos(\@ids,1);
1488 };
1489 0 ▶ return _report_error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1490 0 foreach my $f (@f) {
1491 0 my $path;
1492 0 ▶ my $goto=$1 if $f=~s{(#.*$)}{};
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ s/(#.*$)// 0 0 0
1493 0 ▶ if ($f) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f) 0 0 0
1494 0 Encode::_utf8_off($f);
1495 0 $path = resolve_data_path($f);
1496 }
1497 0 ▶ if (!defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 0 0
1498 0 resp_error($cgi,"Error: $f not found!\n");
1499 0 return 404;
1500 }
1501 0 $f = $path.$goto;
1502 0 print STDERR "Node: $f\n";
1503 }
1504 0 my $url = URI->new($nodes_to_query_service);
1505 0 ▶ $url->query_form(p=>join('|', @f), ($vars ? (r => $vars) : ()) );
Branch: 1 branch
BranchTrueFalseTotal
$vars ? : 0 0 0
1506 0 my $res = $ua->request( HTTP::Request->new(GET => $url) );
1507 0 ▶ if (!$res->is_success) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $res->is_success) { } 0 0 0
elsif ($format eq 'json') { } 0 0 0
1508 0 print $cgi->header(
1509 -type => $res->header('Content-Type')
1510 );
1511 0 my $content = $res->content;
1512 0 print STDERR "n2p: ",$res->code,"\n";
1513 0 print STDERR $content,"\n";
1514 0 print $content;
1515 } elsif ($format eq 'json') {
1516 0 ▶ print $cgi->header(
Branch: 1 branch
BranchTrueFalseTotal
$callback ? : 0 0 0
1517 -type => ($callback ? 'text/javascript' : 'text/x-json'),
1518 -charset => 'UTF-8',
1519 );
1520 0 my $js = q{'}._js_escape_string($res->content).q{'};
1521 0 ▶ $js = $callback.'('.$js.');' if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
1522 0 print STDERR $js;
1523 0 print $js;
1524 } else {
1525 0 print $cgi->header(
1526 -type => 'text/plain',
1527 -charset => 'UTF-8',
1528 );
1529 0 print $res->content;
1530 }
1531 0 return $res->code;
1532 }
1533  
1534  
1535 0 ▶sub resp_error {
Subroutine resp_error: not called CC=2 SCAR=17.9
Pod undocumented
1536 0 my ($cgi,$msg)=@_;
1537 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1538 0 print STDERR $msg,"\n";
1539 0 print(
1540 $cgi->header(-charset=>'UTF-8'),
1541 $msg
1542 );
1543 0 return 200;
1544 }
1545  
1546 0 ▶sub locate_file {
Subroutine locate_file: not called CC=11 SCAR=48.8
Pod undocumented
1547 0 my ($f)=@_;
1548 0 my $evaluator = init_evaluator();
1549 0 my $schemas = $evaluator->run_sql_query(qq{SELECT "root","data_dir","schema_file" FROM "#PML"},{ RaiseError=>1 });
1550 0 for my $schema (@$schemas) {
1551 0 for my $what ('__#files','__#references') {
1552 0 my $n = $schema->[0].$what;
1553 0 ▶ next if $n=~/"/; # just for sure
Branch: 1 branch
BranchTrueFalseTotal
if $n =~ /"/ 0 0 0
1554 0 print STDERR "testing: $what $n $f\n";
1555 0 my $count = $evaluator->run_sql_query(qq{SELECT count(1) FROM "$n" WHERE "file"=?},
1556 {
1557 RaiseError=>1,
1558 Bind=>[ $f ],
1559 }
1560 );
1561 0 ▶ if ($count->[0][0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count->[0][0]) 0 0 0
1562 0 return ($schema->[0], $schema->[1]);
1563 }
1564 }
1565 }
1566 0 my $basename = $f; $basename =~ s{.*/}{};
1567 0 for my $schema (@$schemas) {
1568 0 my $schema_filename = $schema->[2];
1569 0 ▶ if ($schema_filename eq $f or
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_filename eq $f or $schema_filename eq $basename or $schema_filename =~ s[.*/][] and $schema_filename eq $basename || $schema_filename eq $f) 0 0 0
Condition0%0/12
$schema_filename eq $f or $schema_filename eq $basename or $schema_filename =~ s[.*/][] and $schema_filename eq $basename || $schema_filename eq $f
l!l&&r!l&&!r
000
$schema_filename eq $f or $schema_filename eq $basename
l!l&&r!l&&!r
000
$schema_filename =~ s[.*/][] and $schema_filename eq $basename || $schema_filename eq $f
!ll&&!rl&&r
000
$schema_filename eq $basename or $schema_filename eq $f
l!l&&r!l&&!r
000
MC/DC0%0/5
$schema_filename eq $f or $schema_filename eq $basename or $schema_filename =~ s[.*/][] and $schema_filename eq $basename || $schema_filename eq $f
$schema_filename eq $f $schema_filename eq $basename $schema_filename =~ s[.*/][] $schema_filename eq $basename $schema_filename eq $f
Truth table0%0/6
A or B or C and D or E
A= $schema_filename eq $fB= $schema_filename eq $basenameC= $schema_filename =~ s[.*/][]D= $schema_filename eq $basenameE= $schema_filename eq $f
ABCDEresult
1XXXX1
01XXX1
0011X1
001011
000XX0
001000
1570 $schema_filename eq $basename or
1571 ($schema_filename =~ s{.*/}{} and ($schema_filename eq $basename or $schema_filename eq $f))) {
1572 # assume it is a schema file and return it:
1573 0 return ($schema->[0],$schema->[1],$schema->[2]);
1574 }
1575 }
1576 0 return;
1577 }
1578  
1579 0 ▶sub resolve_data_path {
Subroutine resolve_data_path: not called CC=13 SCAR=52.0
Pod undocumented
1580 0 my ($f)=@_;
1581 0 my ($schema_name,$data_dir,$new_filename) = locate_file($f);
1582 0 my $path;
1583 0 ▶ if (defined($schema_name) and defined($data_dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema_name and defined $data_dir) { } 0 0 0
Condition0%0/3
defined $schema_name and defined $data_dir
!ll&&!rl&&r
000
MC/DC0%0/2
defined $schema_name and defined $data_dir
defined $schema_name defined $data_dir
Truth table0%0/3
A and B
A= defined $schema_nameB= defined $data_dir
ABresult
0X0
100
111
1584 0 ▶ $f = $new_filename if defined $new_filename;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_filename 0 0 0
1585 # print STDERR "SOURCES: $conf->{sources}\n";
1586 my ($sources) =
1587 map $_->{'#content'},
1588 0 ▶ grep { $_->{schema} eq $schema_name }
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::DOES::does($conf->{'sources'}, 'Treex::PML::Alt') ? : 0 0 0
1589 0 grep ref, (UNIVERSAL::DOES::does($conf->{sources},'Treex::PML::Alt') ? @{$conf->{sources}} : $conf->{sources});
1590 0 ▶ if ($sources) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sources) { } 0 0 0
1591 0 $path = URI::file->new($f)->abs(URI::file->new($sources.'/'))->file;
1592 0 print STDERR "F: schema '$schema_name', file: $f, located: $path in configured sources\n";
1593 } else {
1594 0 $path = URI::file->new($f)->abs(URI::file->new($data_dir.'/'))->file;
1595 0 print STDERR "F: schema '$schema_name', file: $f, located: $path in data-dir\n";
1596 }
1597 } else {
1598 0 print STDERR "did not find $f in the database\n";
1599 0 my $uri = URI->new($f);
1600 0 ▶ if (!$uri->scheme) { # it must be a relative URI
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->scheme) 0 0 0
1601 0 $uri->scheme('file'); # convert it to file URI
1602 0 my $file = $uri->file;
1603 0 ▶ unless (File::Spec->file_name_is_absolute($file) and $file ne 'treebase.conf') { # must be a relative path
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($file) and $file ne "treebase.conf") 0 0 0
Condition0%0/3
"File::Spec"->file_name_is_absolute($file) and $file ne "treebase.conf"
!ll&&!rl&&r
000
MC/DC0%0/2
"File::Spec"->file_name_is_absolute($file) and $file ne "treebase.conf"
"File::Spec"->file_name_is_absolute($file) $file ne "treebase.conf"
Truth table0%0/3
A and B
A= "File::Spec"->file_name_is_absolute($file)B= $file ne "treebase.conf"
ABresult
0X0
100
111
1604 0 ($path) = Treex::PML::FindInResources($file, {strict=>1});
1605 0 ▶ if (!defined($path) or $path eq $file) { # must be in resource dir
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $path or $path eq $file) 0 0 0
Condition0%0/3
not defined $path or $path eq $file
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $path or $path eq $file
not defined $path $path eq $file
Truth table0%0/3
not(A) or B
A= not defined $pathB= $path eq $file
ABresult
1X1
011
000
1606 0 (undef,undef,$file)=File::Spec->splitpath($file);
1607 0 ($path) = Treex::PML::FindInResources($file, {strict=>1});
1608 0 ▶ undef $path if $path and $path eq $file; # must be in resource dir
Branch: 1 branch
BranchTrueFalseTotal
if $path and $path eq $file 0 0 0
Condition0%0/3
$path and $path eq $file
!ll&&!rl&&r
000
MC/DC0%0/2
$path and $path eq $file
$path $path eq $file
Truth table0%0/3
A and B
A= $pathB= $path eq $file
ABresult
0X0
100
111
1609 }
1610 }
1611 }
1612 }
1613 0 return $path;
1614 }
1615  
1616  
1617  
1618 0 ▶sub resp_data {
Subroutine resp_data: not called CC=5 SCAR=34.0
Pod undocumented
1619 0 my $cgi = shift; # CGI.pm object
1620 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1621 0 my $f = $cgi->url(-absolute=>1,-path_info=>1);
1622 0 print STDERR "URL: ",$cgi->url(-path_info=>1),"\n";
1623 0 $f=~s{^/*(\Q$APP_PREFIX\E/+)?\Q${URL_BASE}\Edata/+}{};
1624 0 my $path = resolve_data_path($f);
1625 # my $path;
1626 # if ($conf->{sources}) {
1627 # $f = File::Spec->abs2rel($f,$conf->{sources});
1628 # }
1629 # $path = $conf->{sources} ? File::Spec->rel2abs($f,$conf->{sources}) : $f;
1630 # print STDERR "$path [$f]\t",_have_file($f),"\n";
1631 # my $have = (_have_file($f) or _have_file($path));
1632 0 ▶ if (!defined $path) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $path) { } 0 0 0
elsif (not -r $path) { } 0 0 0
elsif (serve_static_data($cgi, $path, {'-type', 'application/octet-stream'})) { } 0 0 0
1633 0 print STDERR "ERROR: $f not found in the database!\n";
1634 0 resp_error($cgi,"Object $f not found!");
1635 0 return 404;
1636 } elsif (! (-r $path) ) {
1637 0 print STDERR "ERROR: '$path' not readable!\n";
1638 0 resp_error($cgi,"Error: object $f not readable!\nPlease notify PML-TQ Engine administrator!");
1639 0 return 404;
1640 } elsif (serve_static_data($cgi,$path,{-type => 'application/octet-stream'})) {
1641 0 print STDERR "OK\n";
1642 0 return 200;
1643 } else {
1644 0 print STDERR "FAIL: error opening $path: $!\n";
1645 0 resp_error($cgi,"Error: could not open $f for reading!\nPlease notify PML-TQ Engine administrator!");
1646 0 return 404;
1647 }
1648 }
1649  
1650 0 ▶sub serve_static_data {
Subroutine serve_static_data: not called CC=4 SCAR=30.0
Pod undocumented
1651 0 my ($cgi,$path,$header)=@_;
1652 0 ▶ $header||={};
Condition0%0/2
$header ||= {}
l!l
00
MC/DC0%0/1
$header ||= {}
$header
Truth table0%0/2
A
A= $header
Aresult
11
00
1653 0 ▶ open(my $fh, '<:bytes',$path) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:bytes", $path or return 0 0 0 0
1654 0 my $content_length = (stat($fh))[7];
1655 # print "HTTP/1.1 200 OK\015\012";
1656 0 print $cgi->header(
1657 # -charset => 'UTF-8',
1658 -Content_length => $content_length,
1659 %$header,
1660 );
1661 0 my $buffer;
1662 0 ▶ print $buffer while read $fh, $buffer, 16*1024;
Branch: 1 branch
BranchTrueFalseTotal
read $fh, $buffer, 16384 and do { print $buffer } 0 0 0
1663 0 return 1;
1664 }
1665  
1666 0 ▶sub resp_favicon {
Subroutine resp_favicon: not called CC=3 SCAR=24.8
Pod undocumented
1667 0 my $cgi = shift; # CGI.pm object
1668 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1669 0 my $path = URI::file->new('favicon.ico')->abs(URI::file->new($static_dir.'/icons/'))->file;
1670 0 ▶ if (serve_static_data($cgi,$path,{ -type => 'image/vnd.microsoft.icon' })) {
Branch: 1 branch
BranchTrueFalseTotal
if (serve_static_data($cgi, $path, {'-type', 'image/vnd.microsoft.icon'})) { } 0 0 0
1671 0 return 200;
1672 } else {
1673 0 return 404;
1674 }
1675 }
1676  
1677  
1678 1 my $mt = MIME::Types->new;
1679 0 ▶sub resp_static {
Subroutine resp_static: not called CC=3 SCAR=24.8
Pod undocumented
1680 0 my $cgi = shift; # CGI.pm object
1681 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1682 0 my $f = $cgi->url(-absolute=>1,-path_info=>1);
1683 0 print STDERR "STATIC URL: ",$f,"\n";
1684 0 $f=~s{^/*(\Q$APP_PREFIX\E/+)?\Q${URL_BASE}\Estatic/}{};
1685 0 my $path = URI::file->new($f)->abs(URI::file->new($static_dir.'/'))->file;
1686 0 my ($mimetype) = $mt->mimeTypeOf($path);
1687 0 ▶ if (serve_static_data($cgi,$path,{ -type => $mimetype })) {
Branch: 1 branch
BranchTrueFalseTotal
if (serve_static_data($cgi, $path, {'-type', $mimetype})) { } 0 0 0
1688 0 print STDERR "SERVED STATIC: $path as $mimetype\n";
1689 0 return 200;
1690 } else {
1691 0 resp_error($cgi,"Static object $f not found!");
1692 0 return 404;
1693 }
1694 }
1695  
1696  
1697 0 ▶sub resp_node {
Subroutine resp_node: not called CC=7 SCAR=40.3
Pod undocumented
1698 0 my $cgi = shift; # CGI.pm object
1699 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1700 0 my ($idx,$format) = map $cgi->param($_), qw(idx format);
1701 0 my $evaluator;
1702 0 my ($f) = eval {
1703 0 $evaluator = init_evaluator();
1704 0 $evaluator->idx_to_pos([$idx])
1705 };
1706 0 ▶ return _report_error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1707 0 Encode::_utf8_off($f);
1708 0 ▶ $format = 'text' unless (defined($format) and length($format));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format and length $format 0 0 0
Condition0%0/3
defined $format and length $format
!ll&&!rl&&r
000
MC/DC0%0/2
defined $format and length $format
defined $format length $format
Truth table0%0/3
A and B
A= defined $formatB= length $format
ABresult
0X0
100
111
1709 0 ▶ if ($f) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f) { } 0 0 0
1710 0 ▶ if ($format eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'html') { } 0 0 0
1711 0 print $cgi->header(-charset=>'UTF-8'),
1712 $cgi->start_html("Type"),
1713 $cgi->a({href=>$URL_BASE.'data/'.$f},"$idx"),
1714 $cgi->end_html();
1715 } else {
1716 0 print($cgi->header(-type=>'text/plain',
1717 -charset=>'UTF-8'),
1718 $f."\r\n");
1719 }
1720 0 return 200;
1721 } else {
1722 # resp_error($cgi,"Error resolving $type $idx: $@");
1723 0 return 404;
1724 }
1725 }
1726  
1727  
1728 0 ▶sub resp_schema {
Subroutine resp_schema: not called CC=6 SCAR=37.4
Pod undocumented
1729 0 my $cgi = shift; # CGI.pm object
1730 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1731 0 my $name = $cgi->param('name');
1732 0 my ($evaluator,$results);
1733 0 eval {
1734 0 $evaluator = init_evaluator();
1735 0 $results = $evaluator->run_sql_query(
1736 qq(SELECT "schema" FROM "#PML" WHERE "root" = ? ),
1737 {
1738 MaxRows => 1,
1739 RaiseError => 1,
1740 LongReadLen => 512*1024,
1741 Bind => [$name]
1742 });
1743 };
1744 0 ▶ return _report_error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1745 0 my $mimetype='application/octet-stream';
1746 0 ▶ if (ref($results) and ref($results->[0]) and $results->[0][0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $results and ref $results->[0] and $results->[0][0]) { } 0 0 0
Condition0%0/6
ref $results and ref $results->[0] and $results->[0][0]
!ll&&!rl&&r
000
ref $results and ref $results->[0]
!ll&&!rl&&r
000
MC/DC0%0/3
ref $results and ref $results->[0] and $results->[0][0]
ref $results ref $results->[0] $results->[0][0]
Truth table0%0/4
A and B and C
A= ref $resultsB= ref $results->[0]C= $results->[0][0]
ABCresult
0XX0
10X0
1100
1111
1747 0 Encode::_utf8_off($results->[0][0]);
1748 0 my $content_length = length($results->[0][0]);
1749 0 print $cgi->header(
1750 -type => $mimetype,
1751 -Content_length => $content_length
1752 );
1753 0 print $results->[0][0];
1754 0 return 200;
1755 } else {
1756 0 print $cgi->header(
1757 -type => $mimetype,
1758 -Content_length => 0,
1759 );
1760 0 return 404;
1761 }
1762 }
1763  
1764  
1765 0 ▶sub resp_type {
Subroutine resp_type: not called CC=5 SCAR=34.0
Pod undocumented
1766 0 my $cgi = shift; # CGI.pm object
1767 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1768 0 my $type = $cgi->param('type');
1769 0 my $format = $cgi->param('format');
1770 0 my $evaluator;
1771 0 my $name = eval {
1772 0 $evaluator = init_evaluator();
1773 0 $evaluator->get_schema_name_for($type)
1774 };
1775 0 ▶ return _report_error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1776 0 ▶ $name ||= '';
Condition0%0/2
$name ||= ""
l!l
00
MC/DC0%0/1
$name ||= ""
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
1777 0 Encode::_utf8_off($name);
1778 0 ▶ if ($format eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'html') { } 0 0 0
1779 0 print
1780 $cgi->header(-charset=>'UTF-8'),
1781 $cgi->start_html("Type"),
1782 $name,
1783 $cgi->end_html();
1784 } else {
1785 0 print($cgi->header(-type=>'text/plain',
1786 -charset=>'UTF-8'),
1787 $name."\r\n");
1788 }
1789 0 return 200;
1790 }
1791  
1792  
1793 0 ▶sub resp_nodetypes {
Subroutine resp_nodetypes: not called CC=7 SCAR=40.3
Pod undocumented
1794 0 my $cgi = shift; # CGI.pm object
1795 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1796 0 ▶ my $format = $cgi->param('format') || 'text';
Condition0%0/2
$cgi->param('format') || 'text'
l!l
00
MC/DC0%0/1
$cgi->param('format') || 'text'
$cgi->param('format')
Truth table0%0/2
A
A= $cgi->param('format')
Aresult
11
00
1797 0 my $layer = $cgi->param('layer');
1798 0 my $evaluator;
1799 0 my $types = eval{
1800 0 $evaluator = init_evaluator();
1801 0 ▶ $evaluator->get_node_types($layer || ());
Condition0%0/3
$layer || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$layer || ()
$layer ()
Truth table0%0/3
A || B
A= $layerB= ()
ABresult
1X1
011
000
1802 };
1803 0 ▶ return _report_error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1804 0 Encode::_utf8_off($_) for @$types;
1805 0 ▶ if ($format eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'text') { } 0 0 0
1806 0 print($cgi->header(-type=>'text/plain',
1807 -charset=>'UTF-8'),
1808 map "$_\r\n", @$types);
1809 } else {
1810 0 print
1811 $cgi->header(-charset=>'UTF-8'),
1812 $cgi->start_html("Node Types"),
1813 $cgi->table({-border => undef},
1814 $cgi->Tr({-align => 'LEFT', -valign => 'TOP'},
1815 [ map $cgi->td($_), @$types ])),
1816 $cgi->end_html();
1817 }
1818 0 return 200;
1819 }
1820  
1821  
1822 0 ▶sub resp_relations {
Subroutine resp_relations: not called CC=8 SCAR=42.8
Pod undocumented
1823 0 my $cgi = shift; # CGI.pm object
1824 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1825 0 ▶ my $format = $cgi->param('format') || 'text';
Condition0%0/2
$cgi->param('format') || 'text'
l!l
00
MC/DC0%0/1
$cgi->param('format') || 'text'
$cgi->param('format')
Truth table0%0/2
A
A= $cgi->param('format')
Aresult
11
00
1826 0 my $type = $cgi->param('type');
1827 0 my $rel_cat = $cgi->param('category');
1828 0 my $evaluator = eval { init_evaluator() };
1829 0 ▶ return _report_error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1830 0 my $relations;
1831 0 ▶ if ($rel_cat eq 'implementation') {
Branch: 2 branches
BranchTrueFalseTotal
if ($rel_cat eq 'implementation') { } 0 0 0
elsif ($rel_cat eq 'pmlrf') { } 0 0 0
1832 0 $relations = $evaluator->get_user_defined_relations($type);
1833 } elsif ($rel_cat eq 'pmlrf') {
1834 0 $relations = $evaluator->get_pmlrf_relations($type);
1835 } else {
1836 0 $relations = $evaluator->get_specific_relations($type);
1837 }
1838 0 Encode::_utf8_off($_) for @$relations;
1839 0 ▶ if ($format eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'html') { } 0 0 0
1840 0 print
1841 $cgi->header(-charset=>'UTF-8'),
1842 $cgi->start_html("Specific relations"),
1843 $cgi->table({-border => undef},
1844 $cgi->Tr({-align => 'LEFT', -valign => 'TOP'},
1845 [ map $cgi->td($_), @$relations ])),
1846 $cgi->end_html();
1847 } else {
1848 0 print($cgi->header(-type=>'text/plain',
1849 -charset=>'UTF-8'),
1850 map "$_\r\n", @$relations);
1851 }
1852 0 return 200;
1853 }
1854  
1855  
1856 0 ▶sub resp_relation_target_types {
Subroutine resp_relation_target_types: not called CC=23 SCAR=63.1
Pod undocumented
1857 0 my $cgi = shift; # CGI.pm object
1858 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1859 0 ▶ my $format = $cgi->param('format') || 'text';
Condition0%0/2
$cgi->param('format') || 'text'
l!l
00
MC/DC0%0/1
$cgi->param('format') || 'text'
$cgi->param('format')
Truth table0%0/2
A
A= $cgi->param('format')
Aresult
11
00
1860 0 my $type = $cgi->param('type');
1861 0 my $category = $cgi->param('category');
1862 0 my $evaluator = eval{ init_evaluator() };
1863 0 ▶ return _report_error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1864 0 my @map;
1865 my %map;
1866 0 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) { } 0 0 0
1867 0 my @maps;
1868 0 ▶ if (!$category or $category eq 'pmlrf') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $category or $category eq "pmlrf") 0 0 0
Condition0%0/3
not $category or $category eq "pmlrf"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $category or $category eq "pmlrf"
not $category $category eq "pmlrf"
Truth table0%0/3
not(A) or B
A= not $categoryB= $category eq "pmlrf"
ABresult
1X1
011
000
1869 0 push @maps, $evaluator->get_pmlrf_relation_map_for_type($type);
1870 }
1871 0 ▶ if (!$category or $category eq 'implementation') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $category or $category eq "implementation") 0 0 0
Condition0%0/3
not $category or $category eq "implementation"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $category or $category eq "implementation"
not $category $category eq "implementation"
Truth table0%0/3
not(A) or B
A= not $categoryB= $category eq "implementation"
ABresult
1X1
011
000
1872 0 push @maps, $evaluator->get_user_defined_relation_map_for_type($type);
1873 }
1874 0 for my $map (@maps) {
1875 0 for my $rel (sort keys %$map) {
1876 0 my $target = $map->{$rel};
1877 0 ▶ if (defined $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 0 0 0
1878 0 push @map,[$type,$rel,$target->[2]];
1879 }
1880 }
1881 }
1882 } else {
1883 0 my @maps;
1884 0 ▶ if (!$category or $category eq 'pmlrf') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $category or $category eq "pmlrf") 0 0 0
Condition0%0/3
not $category or $category eq "pmlrf"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $category or $category eq "pmlrf"
not $category $category eq "pmlrf"
Truth table0%0/3
not(A) or B
A= not $categoryB= $category eq "pmlrf"
ABresult
1X1
011
000
1885 0 push @maps, $evaluator->get_pmlrf_relation_map();
1886 }
1887 0 ▶ if (!$category or $category eq 'implementation') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $category or $category eq "implementation") 0 0 0
Condition0%0/3
not $category or $category eq "implementation"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $category or $category eq "implementation"
not $category $category eq "implementation"
Truth table0%0/3
not(A) or B
A= not $categoryB= $category eq "implementation"
ABresult
1X1
011
000
1888 0 push @maps, $evaluator->get_user_defined_relation_map();
1889 }
1890 0 for my $map (@maps) {
1891 0 for my $node_type (sort keys %$map) {
1892 0 my $map2 = $map->{$node_type};
1893 0 ▶ if ($map2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map2) 0 0 0
1894 0 for my $rel (sort keys %$map2) {
1895 0 my $target = $map2->{$rel};
1896 0 ▶ if (defined $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 0 0 0
1897 0 push @map,[$node_type,$rel,$target->[2]];
1898 }
1899 }
1900 }
1901 }
1902 }
1903 }
1904 0 Encode::_utf8_off($_) for @map;
1905 0 ▶ if ($format eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'html') { } 0 0 0
1906 0 print
1907 $cgi->header(-charset=>'UTF-8'),
1908 $cgi->start_html("Target-node types for specific relations"),
1909 $cgi->table({-border => undef},
1910 $cgi->Tr({-align => 'LEFT', -valign => 'TOP'},
1911 $cgi->th([qw(node-type relation target-type)])),
1912 $cgi->Tr({-align => 'LEFT', -valign => 'TOP'},
1913 [ map $cgi->td($_), @map ])),
1914 $cgi->end_html();
1915 } else {
1916 print($cgi->header(-type=>'text/plain',
1917 -charset=>'UTF-8'),
1918 0 map {join(':',@$_)."\r\n" } @map);
1919 }
1920 0 return 200;
1921 }
1922  
1923  
1924  
1925 0 ▶sub resp_version {
Subroutine resp_version: not called CC=7 SCAR=40.3
Pod undocumented
1926 0 my $cgi = shift; # CGI.pm object
1927 0 ▶ return 500 if !ref $cgi;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cgi 0 0 0
1928 0 ▶ my $format = $cgi->param('format') || 'html';
Condition0%0/2
$cgi->param('format') || 'html'
l!l
00
MC/DC0%0/1
$cgi->param('format') || 'html'
$cgi->param('format')
Truth table0%0/2
A
A= $cgi->param('format')
Aresult
11
00
1929 0 ▶ my $version = $cgi->param('client_version') || '';
Condition0%0/2
$cgi->param('client_version') || ''
l!l
00
MC/DC0%0/1
$cgi->param('client_version') || ''
$cgi->param('client_version')
Truth table0%0/2
A
A= $cgi->param('client_version')
Aresult
11
00
1930 0 my $evaluator = eval { init_evaluator() };
1931 0 ▶ return _report_error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1932 0 ▶ my $ok = $evaluator->check_client_version($version) ? 'COMPATIBLE' : 'INCOMPATIBLE';
Branch: 1 branch
BranchTrueFalseTotal
$evaluator->check_client_version($version) ? : 0 0 0
1933 0 ▶ if ($format eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'text') { } 0 0 0
1934 0 print($cgi->header(-type=>'text/plain',
1935 -charset=>'UTF-8'),
1936 $ok."\r\n".$PMLTQ::SQLEvaluator::VERSION
1937 );
1938 } else {
1939 0 print
1940 $cgi->header(-charset=>'UTF-8'),
1941 $cgi->start_html("VERSION"),
1942 $cgi->p("CLIENT VERSION $version IS $ok"),
1943 $cgi->p($PMLTQ::SQLEvaluator::VERSION),
1944 $cgi->end_html();
1945 }
1946 0 return 200;
1947 }
1948  
1949 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
1950  
1951 1 $PAST_QUERIES_SCRIPT = <<"EOF";
1952 function goto (first) {
1953 document.qf.first.value = first;
1954 document.qf.action = 'past_queries';
1955 document.qf.submit();
1956 }
1957 function edit (id) {
1958 var el = document.getElementById(id);
1959 if (el == null) return;
1960 var query = el.textContent;
1961 if (query==null) query=el.innerText;
1962 document.qf.query.value = query;
1963 document.qf.action = 'form';
1964 document.qf.submit();
1965 }
1966 function run (id) {
1967 var el = document.getElementById(id);
1968 if (el == null) return;
1969 show_loading();
1970 var query = el.textContent;
1971 if (query==null) query = el.innerText;
1972 document.qf.query.value = query;
1973 document.qf.action = 'query';
1974 document.qf.submit();
1975 }
1976 EOF
1977  
1978 }
1979  
1980 0 ▶sub _query_tree_div{
Subroutine _query_tree_div: not called CC=1 SCAR=6.9
1981 return <<EOF
1982 <div id="query_tree" style="visibility:hidden">
1983 <div class="handle">
1984 <a href="javascript:document.getElementById('query_tree').setAttribute('style','visibility:hidden');">x</a>
1985 </div>
1986 <object width="100%" type="image/svg+xml"
1987 id="query_tree_img" title="Visualization of the Query" alt="query image"></object>
1988 </div>
1989 </div>
1990 EOF
1991 0 }
1992  
1993 0 ▶sub _toolbar_text {
Subroutine _toolbar_text: not called CC=3 SCAR=24.8
1994 0 my ($cgi,$title)=@_;
1995 0 ▶ $title||='';
Condition0%0/2
$title ||= ""
l!l
00
MC/DC0%0/1
$title ||= ""
$title
Truth table0%0/2
A
A= $title
Aresult
11
00
1996 0 ▶ my $query_field = _query_textarea($cgi) || '';
Condition0%0/2
_query_textarea($cgi) || ''
l!l
00
MC/DC0%0/1
_query_textarea($cgi) || ''
_query_textarea($cgi)
Truth table0%0/2
A
A= _query_textarea($cgi)
Aresult
11
00
1997 0 return <<"EOF" . _query_tree_div();
1998 <center>
1999 <div class="toolbar">
2000 $query_field
2001 <table rules="none" border="0pt">
2002 <tr>
2003 <td align="left" width="40%">
2004 <input type="submit" accesskey="q" value="&#x21e9; Query" name="query_submit" title="Execute the query (including output filters)" />
2005 <input type="submit" accesskey="w" value="&#x21e9; w/o Filters" name="query_submit" title="Execute the query, excluding possible output filters" />
2006 <input type="button" accesskey="v" onclick="show_query_svg()" value="&#x263C; Visualize" name="view_submit" title="Display graphical visualization of the query" />
2007 <input type="button" accesskey="c" onclick="document.qf.query.value=''; return false;" title="Empty the query form" value="&#x2715; Clear" name="clear" />
2008 </td>
2009 <td align="center"><span id="title">$title</span></td>
2010 <td align="right" width="40%"></td>
2011 </tr>
2012 </table>
2013 </div>
2014 </center>
2015 EOF
2016 }
2017 0 ▶sub _toolbar_trees {
Subroutine _toolbar_trees: not called CC=3 SCAR=24.8
2018 0 my ($cgi)=@_;
2019 0 my $query_field = _query_textarea($cgi);
2020 0 ▶ my $size = length(int($cgi->param('limit') || 100)) || 4;
Condition0%0/4
length int($cgi->param('limit') || 100) || 4
l!l
00
$cgi->param('limit') || 100
l!l
00
MC/DC0%0/1
length int($cgi->param('limit') || 100) || 4
$cgi->param('limit')
Truth table0%0/2
A
A= $cgi->param('limit')
Aresult
11
00
2021 0 return <<"EOF" . _query_tree_div();
2022 <center>
2023 <div class="toolbar">
2024 $query_field
2025 <div class="toolbar-controls">
2026 <span style="float:left">
2027 <input type="submit" value="&#x21e9; Query" name="query_submit" title="Execute the query (including output filters)" accesskey="q" />
2028 <input type="submit" value="&#x21e9; w/o Filters " name="query_submit" title="Execute the query, excluding possible output filters" accesskey="w" />
2029 <input type="button" onclick="node2pmltq()" value="&#x21e7; Suggest" title="Suggest query based on nodes marked by user" name="extract" disabled="disabled" accesskey="s" />
2030 <input type="button" onclick="show_query_svg()" value="&#x263C; Visualize" name="view_submit" title="Display graphical visualization of the query" accesskey="v" />
2031 <input type="button" onclick="document.qf.query.value=''; return false;" title="Empty the query form" value="&#x2715; Clear" name="clear" accesskey="c"/>
2032 </span>
2033 <span style="float:right; margin-right: 10px;">Result:
2034 <button type="button" accesskey="p" title="Previous match" onclick="next_tree(-1)">&lt;</button>
2035 <input type="text" size="$size" maxlength="$size" id="cur_tree" name="_ct" onkeypress="return tree_no_keypress(event, this.value)" value="0" /> of <span id="tree_count">0</span>
2036 <button type="button" accesskey="n" title="Next match" onclick="next_tree(1)">&gt;</button>
2037 </span>
2038 <span class="qnodes" id="q_nodes"></span>
2039 </div>
2040 <div style="clear: both; height: 0pt;" ></div>
2041 <div id="n2p-dlg">
2042 <div id="n2p-hint">
2043 <p>This is a PML-TQ query generated from the marked nodes. Use the checkboxes
2044 to include/exclude parts of the query.</p>
2045 <p>
2046 Then insert the result to the current query or replace it.
2047 </p>
2048 </div>
2049 <div id="n2p-buttons">
2050 <button type="button" accesskey="i" title="Insert into query at cursor position" onclick="n2p_insert()">Insert</button>
2051 <button type="button" accesskey="r" title="Replace current query" onclick="n2p_replace()">Replace</button>
2052 <button type="button" accesskey="u" title="Clean up disabled parts of the query" onclick="n2p_cleanup()">Clean Up</button>
2053 <button type="button" accesskey="m" title="Clear marks" onclick="n2p_clear()">Clear Marks</button>
2054 <button type="button" accesskey="l" title="Close" onclick="n2p_cancel()">Close</button>
2055 </div>
2056 <div id="n2p-query"><table id="n2p-body"></table></div>
2057 </div>
2058 </div>
2059 </center>
2060 EOF
2061 # return <<"EOF";
2062 # <center>
2063 # <div class="toolbar">
2064 # $query_field
2065 # <table rules="none" border="0pt">
2066 # <tr>
2067 # <td align="left" width="40%">
2068 # <input type="submit" value="Submit New Query &#x21e9;" name="query_submit" />
2069 # <small><input type="checkbox" value="1" $no_filters_checked name="no_filters" /> Without filters</small>
2070 # </td>
2071 # <td align="center">
2072 # <span class="qnodes" id="q_nodes"></span>
2073 # </td>
2074 # <td align="right" width="40%">
2075 # Result: <input type="button" accesskey="p" title="Previous match" value="&lt;" onclick="next_tree(-1)" />
2076 # <span id="cur_tree">0</span> of <span id="tree_count">0</span>
2077 # <input type="button" accesskey="n" title="Next match" value="&gt;" onclick="next_tree(1)" />
2078 # </td>
2079 # </tr>
2080 # </table>
2081 # </div>
2082 # </center>
2083 # EOF
2084 }
2085  
2086  
2087 0 ▶sub _result_tree_div {
Subroutine _result_tree_div: not called CC=1 SCAR=6.9
2088 0 my ($cgi)=@_;
2089 0 my $tools="";
2090 0 return <<"EOF";
2091 <div class="result-info">
2092 <span class="zoom-buttons" style="float: right">
2093 <span id="tools-menu"></span>
2094 <button class="zoom-button" accesskey="+" title="Zoom in" type="button" onclick="zoom_inc(0.1)">+</button>
2095 <button class="zoom-button" accesskey="-" title="Zoom out" type="button" onclick="zoom_inc(-0.1)">-</button>
2096 </span>
2097 <div id="result-text">
2098 <span id="context">
2099 <a class="svg-title" accesskey="9" id="context-tree-before" title="Context (Tree Before)" type="button" href="javascript:next_context_tree(-1)">&lt;</a>
2100 <span id="tree_offset" class="svg-title">-</span>
2101 <a class="svg-title" accesskey="0" id="context-tree-after" title="Context (Tree After)" type="button" href="javascript:next_context_tree(1)">&gt;</a>
2102 </span>
2103 <span id="title" class="svg-title">Searching...</span>
2104 <span id="desc" class="desc"></span>
2105 </div>
2106 </div>
2107 <div><div id="tooltip"></div><div id="tree" class="tree" style="width:100%; height:60%; overflow:auto;">
2108 <object id="svg-tree" width="100%" height="100%" type="image/svg+xml"></object>
2109 </div>
2110 </div>
2111 EOF
2112 }
2113  
2114 0 ▶sub _ga_track_code {
Subroutine _ga_track_code: not called CC=1 SCAR=6.9
2115 0 my ($cgi) = @_;
2116 0 return <<"EOF";
2117 <script>
2118 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
2119 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
2120 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
2121 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
2122  
2123 ga('create', '$ga_tracking_code', '$ga_tracking_domain');
2124 ga('send', 'pageview');
2125 </script>
2126 EOF
2127 }
2128  
2129 0 ▶sub _title {
Subroutine _title: not called CC=17 SCAR=57.2
2130 0 my ($cgi,$type)=@_;
2131 0 ▶ $type ||= 'default';
Condition0%0/2
$type ||= "default"
l!l
00
MC/DC0%0/1
$type ||= "default"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
2132  
2133 return (
2134 ($ga_tracking_code ? _ga_track_code($cgi) : ()),
2135 ($cgi->user_agent =~ /MSIE/ ? ($cgi->p({-style=>'color:red; font-weight:bold; font-size:9pt'},"Warning: Internet Explorer detected; your browser may not work correctly, try Mozilla FireFox, Google Chrome, Opera, or Safari")) : ()),
2136 $cgi->div($cgi->a({href=>'javascript:document.qf.action="form";document.qf.submit()'},
2137 $cgi->img({-src=>$URL_BASE."static/icons/pmltq_small.png",
2138 -alt=>"PML Tree-Query Engine",
2139 -style=>"float: left; margin: -5px 5px 0px 5px;",
2140 })),
2141 $cgi->img({-src=>$URL_BASE."static/icons/loading_bar.gif", -id => "loading", -class=>"loading-bar",
2142 -style => "visibility: ".($type =~ /^(query|form)$/ ? 'visible' : 'hidden'),
2143 })),
2144 $cgi->div({-class=>'top'},
2145 $cgi->div(
2146 {-class => 'subtitle', -style=>'text-align: right; padding: 5pt;'},
2147 $type eq 'past_queries' ?
2148 "Previous Queries"
2149 : $type eq 'other' ?
2150 "Available Treebanks"
2151 : $type eq 'about' ?
2152 "About This Treebank"
2153 0 ▶ : $cgi->a({href=>'javascript:document.qf.action="about";document.qf.submit()'}, ($desc->{title}||''))),
Branch: 10 branches
BranchTrueFalseTotal
$ga_tracking_code ? : 0 0 0
$cgi->user_agent =~ /MSIE/ ? : 0 0 0
$type =~ /^(query|form)$/ ? : 0 0 0
$type eq 'past_queries' ? : 0 0 0
$type eq 'other' ? : 0 0 0
$type eq 'about' ? : 0 0 0
$type eq 'past_queries' || $type eq 'other' || $type eq 'about' ? : 0 0 0
!is_anonymous($cgi) && $type ne 'past_queries' ? : 0 0 0
$type ne 'other' ? : 0 0 0
!session_ok($cgi) ? : 0 0 0
Condition0%0/13
$desc->{'title'} || ''
l!l
00
$type eq "past_queries" or $type eq "other" or $type eq "about"
l!l&&r!l&&!r
000
$type eq "past_queries" or $type eq "other"
l!l&&r!l&&!r
000
$cgi->param('back_to') || 'form'
l!l
00
not is_anonymous($cgi) and $type ne "past_queries"
!ll&&!rl&&r
000
MC/DC0%0/1
$desc->{'title'} || ''
$desc->{'title'}
MC/DC0%0/3
$type eq "past_queries" or $type eq "other" or $type eq "about"
$type eq "past_queries" $type eq "other" $type eq "about"
MC/DC0%0/1
$cgi->param('back_to') || 'form'
$cgi->param('back_to')
MC/DC0%0/2
not is_anonymous($cgi) and $type ne "past_queries"
not is_anonymous($cgi) $type ne "past_queries"
Truth table0%0/2
A
A= $desc->{'title'}
Aresult
11
00
Truth table0%0/4
A or B or C
A= $type eq "past_queries"B= $type eq "other"C= $type eq "about"
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/2
A
A= $cgi->param('back_to')
Aresult
11
00
Truth table0%0/3
not(A) and B
A= not is_anonymous($cgi)B= $type ne "past_queries"
ABresult
0X0
100
111
2154 $cgi->span({-style=>'float: right'},
2155 (($type eq 'past_queries' or $type eq 'other' or $type eq 'about')
2156 ? (
2157 _param_form($cgi,$type),
2158 $cgi->a({href=>'javascript:document.qf.action="'.($cgi->param('back_to')||'form').'";document.qf.submit()'},"Back")," ")
2159 : ()
2160 ),
2161 ((!is_anonymous($cgi) and $type ne 'past_queries') ?
2162 ($cgi->a({href=>'javascript:document.qf.action="past_queries";document.qf.submit()'},"Previous Queries"), " ") : ()),
2163 (($type ne 'other') ?
2164 ($cgi->a({href=>'javascript:document.qf.action="other";document.qf.submit()'},"Select Treebank")," ") : ()),
2165 $cgi->a({href=>$HELP_URI, target=>"_blank"},"Documentation"),
2166 " ",
2167 $cgi->a({href=>$PMLTQ_PROJECT_URI, target=>"_blank"},"Project Page"),
2168 (!session_ok($cgi) ? $cgi->a({href=>qq{/$APP_PREFIX/${URL_BASE}login}},"Login") : ())
2169 ),
2170 $cgi->div({-style=>'clear:both;'},''),
2171 ),
2172 );
2173 }
2174  
2175 1 my $INTRO;
2176 0 ▶sub intro {
Subroutine intro: not called CC=5 SCAR=34.0
Pod undocumented
2177 0 my ($cgi)=@_;
2178 0 ▶ unless ($INTRO) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INTRO) 0 0 0
2179 0 $INTRO = INTRO();
2180  
2181 0 my $ev = eval { init_evaluator() };
2182 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
2183 0 print STDERR "INIT: $@\n";
2184 0 return $@;
2185 }
2186 0 my $schema_names = $ev->get_schema_names;
2187 0 my $layers = '<ul>'.join('',map { '<li style="padding-bottom: 6pt;">Layer <b><tt>'.$_.'</tt></b> consists of node types:<br />'.
2188 0 ▶ join(', ',map qq{<b><tt>$_</tt></b>},@{$ev->get_node_types($_)})
Branch: 1 branch
BranchTrueFalseTotal
@$schema_names == 1 ? : 0 0 0
2189 .' (wildcard <b><tt>'.(@$schema_names == 1 ? '*' : $_.':*' ).'</b></tt>)'
2190 .'</li>' } @$schema_names).'</ul>';
2191 0 $INTRO =~ s/\%layers\%/$layers/g;
2192 0 my $title = $desc->{title};
2193 0 $title=~s{\(.*?\)}{}g;
2194 0 $title=~s{,?\s*(?:Oracle|PostgreSQL|Postgres|DB2)\s*$}{};
2195 0 $INTRO =~ s/\%title\%/$title/g;
2196  
2197 0 my $doc = generate_doc($ev);
2198  
2199 0 $INTRO =~ s/\%node\%/$doc->{type}/g;
2200 0 $INTRO =~ s/\%layer\%/$doc->{layer}/g;
2201 0 $INTRO =~ s/\%attr\%/$doc->{attr}/g;
2202 0 $INTRO =~ s/\%value\%/$doc->{value}/g;
2203  
2204 0 $INTRO =~ s{\%try\%}{<button style="float:right" class="try-button" title="Try" type="button" onclick="try_example(this.parentNode)">Try</button>}g;
2205 }
2206  
2207 0 my $copy = $INTRO;
2208 0 my $user = _user_name($cgi);
2209  
2210 0 ▶ if ($user ne 'unknown') {
Branch: 1 branch
BranchTrueFalseTotal
if ($user ne 'unknown') { } 0 0 0
2211 0 $user=~s{([[:lower:]])([[:upper:]])}{$1 $2}g;
2212 0 $copy =~ s/\%user\%/ $user/g;
2213 } else {
2214 0 $copy =~ s/\%user\%//g;
2215 }
2216 0 return $copy;
2217 }
2218  
2219 0 ▶sub generate_doc {
Subroutine generate_doc: not called CC=12 SCAR=50.5
Pod undocumented
2220 0 my ($ev) = @_;
2221 0 my @aux;
2222 my %doc;
2223 SCHEMA:
2224 0 for my $layer (@{$ev->get_schema_names}) {
2225 0 for my $type (@{$ev->get_node_types($layer)}) {
2226 0 ▶ my $decl = $ev->get_decl_for($type) || next;
Condition0%0/2
$ev->get_decl_for($type) || (next)
l!l
00
MC/DC0%0/1
$ev->get_decl_for($type) || (next)
$ev->get_decl_for($type)
Truth table0%0/2
A
A= $ev->get_decl_for($type)
Aresult
11
00
2227 0 for my $attr (map { my $t = $_; $t=~s{#content}{content()}g; $t }
2228 map $_->[0],
2229 0 ▶ sort { $a->[1]<=>$b->[1] or $a->[0] cmp $b->[0] }
Condition0%0/3
$PMLTQ::CGI::a->[1] <=> $PMLTQ::CGI::b->[1] || $PMLTQ::CGI::a->[0] cmp $PMLTQ::CGI::b->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$PMLTQ::CGI::a->[1] <=> $PMLTQ::CGI::b->[1] || $PMLTQ::CGI::a->[0] cmp $PMLTQ::CGI::b->[0]
$PMLTQ::CGI::a->[1] <=> $PMLTQ::CGI::b->[1] $PMLTQ::CGI::a->[0] cmp $PMLTQ::CGI::b->[0]
Truth table0%0/3
A || B
A= $PMLTQ::CGI::a->[1] <=> $PMLTQ::CGI::b->[1]B= $PMLTQ::CGI::a->[0] cmp $PMLTQ::CGI::b->[0]
ABresult
1X1
011
000
2230 map [$_,scalar(@aux = m{/}g) ],
2231 $decl->get_paths_to_atoms({ no_childnodes => 1 })) {
2232 0 my $mdecl = $decl->find($attr,1);
2233 0 ▶ next if $mdecl->get_role();
Branch: 1 branch
BranchTrueFalseTotal
if $mdecl->get_role 0 0 0
2234 0 ▶ $mdecl=$mdecl->get_knit_content_decl unless $mdecl->is_atomic;
Branch: 1 branch
BranchTrueFalseTotal
unless $mdecl->is_atomic 0 0 0
2235 0 ▶ next if ($mdecl->get_decl_type == PML_CDATA_DECL and $mdecl->get_format eq 'PMLREF');
Branch: 1 branch
BranchTrueFalseTotal
if $mdecl->get_decl_type == 8 and $mdecl->get_format eq "PMLREF" 0 0 0
Condition0%0/3
$mdecl->get_decl_type == 8 and $mdecl->get_format eq "PMLREF"
!ll&&!rl&&r
000
MC/DC0%0/2
$mdecl->get_decl_type == 8 and $mdecl->get_format eq "PMLREF"
$mdecl->get_decl_type == 8 $mdecl->get_format eq "PMLREF"
Truth table0%0/3
A and B
A= $mdecl->get_decl_type == 8B= $mdecl->get_format eq "PMLREF"
ABresult
0X0
100
111
2236  
2237 0 $doc{layer} = $layer;
2238 0 $doc{type} = $type;
2239 0 $doc{attr} = $attr;
2240 0 $doc{value} = '...some value...';
2241  
2242 0 my ($sth) =
2243 search(qq{$type \$n:=[ $attr=$attr ]>>\$n.$attr}, select_first => 1);
2244 0 ▶ if (ref($sth) and !$sth->err) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sth and not $sth->err) 0 0 0
Condition0%0/3
ref $sth and not $sth->err
!ll&&!rl&&r
000
MC/DC0%0/2
ref $sth and not $sth->err
ref $sth not $sth->err
Truth table0%0/3
A and not(B)
A= ref $sthB= not $sth->err
ABresult
0X0
100
111
2245 0 my $row = $sth->fetch;
2246 0 Encode::_utf8_off($row->[0]);
2247 0 $doc{value} = $row->[0];
2248 }
2249 0 last SCHEMA;
2250 }
2251 }
2252 }
2253  
2254 0 return \%doc;
2255 }
2256  
2257  
2258 0 ▶sub INTRO {
Subroutine INTRO: not called CC=1 SCAR=6.9
Pod undocumented
2259 0 q@
2260 <div id="quick_intro">
2261 <h2>Welcome%user%!</h2>
2262 <div class="note">This quick introduction to PML-TQ is semi-automatically generated
2263 and covers the most basic topics only. See the <a target="_blank" href="http://ufal.mff.cuni.cz/~pajas/pmltq/doc/pmltq_doc.html">Documentation</a> for full reference. You can always return to this help
2264 by clicking the logo.
2265 </div>
2266 <h3>Using This Interface</h3>
2267 <p>Type your query into the text box above. The menus allow you to insert
2268 special strings into the query, such as node types, attribute names,
2269 node relations, operators, and functions.
2270 <span class="guibutton">&#x21e9; Query</span> sends the query to the server and displays the results.
2271 Complex or low-selective queries may take a few seconds to evaluate; the <b>Timeout</b> option
2272 specifies maximum time the server should spend trying to evaluate the query.
2273 The <span class="guibutton">&#x21e9; w/o Filters</span> button submits the query without
2274 any <a href="#output-filters">output filters</a> (see below).
2275 The <span class="guibutton">&#x263C; Visualize</span> button
2276 shows visualization of the query.
2277 </p>
2278 <p>It is also possible to add result nodes to the query:
2279 run some simple query to display a tree from the treebank. Then
2280 mark (by clicking) one or more related nodes in the result tree
2281 and press <span class="guibutton">&#x21e7; Suggest</span>; a PML-TQ query based
2282 on the marked nodes will be suggested and displayed in
2283 a dialog where you can exclude/include its parts and then
2284 paste it directly to the query text box.</p>
2285 <p>The button <span class="guibutton">&#x2715; Clear</span> empties the query form.</p>
2286 <h3>Annotation Schema of %title%</h3>
2287 <p><b>This treebank</b>, consists of the following <b>annotation layer(s)</b> and <b>node type(s)</b>:
2288 </p>
2289 %layers%
2290 <h3>Query Language</h3>
2291 <p>A basic <b>query</b> for nodes of the type, say <tt>%node%</tt>, looks like this:</p>
2292 <pre class="programlisting" id="example-1">%try%%node% [ ]</pre>
2293 <p>Similarly for other node types and node-type wildcards.</p>
2294 <p><b>Contstraints</b> on the node go between the square brackets [...].
2295 Basic constraints on <b>attribute</b> values have the following forms:</p>
2296 <table>
2297 <tr><td><tt><i>attribute</i> = '<i>value</i>'</tt></td>
2298 <td>string comparison</td>
2299 </tr>
2300 <tr><td><tt><i>attribute</i> ~ '<i>reg-exp</i>'</tt></td>
2301 <td>regular expression match</td>
2302 <tr><td><tt><i>attribute</i> &lt; number</tt></td>
2303 <td>number comparison, same for >, &lt;=, >=, =</td>
2304 <tr><td><tt><i>attribute</i> in {'<i>value1</i>', '<i>value2</i>', ...}</tt></td>
2305 <td>membership in an enumeration</td>
2306 </table>
2307 <p>For example:</p>
2308 <pre class="programlisting" id="example-2">%try%%node% [ %attr%='%value%' ]</pre>
2309 <p>
2310 See the <span class="guimenu">Attributes</span> menu for the list of attributes for each node type.
2311 </p>
2312 <p>Other <b>node properties</b>, such as number of sons, descendants, siblings, etc.
2313 can be obtained using functions (see <span class="guimenu">Funcions</span> &raquo; <span class="guimenuitem">Node Properties</span>).
2314 In fact, <b>complex expressions</b> can be used on left and right hand side of
2315 the comparison operators in constraints; see the menus
2316 <span class="guimenu">Operators</span> and
2317 <span class="guimenu">Funcions</span> for available arithmetic and string operators
2318 and functions.</p>
2319 <p>The constraints can be combined using commas (<tt>and</tt>)
2320 or the logical operators
2321 <tt>!</tt> (not), <tt>and</tt>, <tt>or</tt> (see also the <span class="guimenu">Operators</span> menu). For example:
2322 </p>
2323 <pre class="programlisting" id="example-3">%try%%node% [ %attr%='%value%', sons()=0 or lbrothers()+rbrothers()=0 ]</pre>
2324 <p>searches for any %node% with %attr%='%value%' that is either
2325 a leaf node (has no sons) or an only child (has no left nor right siblings).</p>
2326 <p>To introduce another node into the query, include it among the constrains and
2327 specify its <b>relation</b> to the existing node.
2328 The <span class="guimenu">Relation</span> menu contains a list list of available relations.
2329 Here a is an example using the relation 'child':
2330 </p>
2331 <pre class="programlisting" id="example-4">%try%%node% [
2332 %attr%='%value%',
2333 child %node% [ ]
2334 ]</pre>
2335 <p><b>Additional relations</b> between a pair of nodes can be specified by assigning <b>symbolic names</b>
2336 to the query nodes. The names can be used also to refer to
2337 other node's attributes, e.g.</p>
2338 <pre class="programlisting" id="example-5">%try%%node% $a := [
2339 sibling %node% [
2340 depth-first-follows $a,
2341 %attr%=$a.%attr%,
2342 ]
2343 ]</pre>
2344 <p>searches for any %node% $a with a sibling %node% having the same value of %attr%
2345 and following the node $a. We may also search for any %node% with no such sibling, using a so called <b>subquery</b>
2346 by quantifying the number of occurrences:</p>
2347 <pre class="programlisting" id="example-6">%try%%node% $a := [
2348 0x sibling %node% [
2349 depth-first-follows $a,
2350 %attr%=$a.%attr%,
2351 ]
2352 ]</pre>
2353 <p>The symbol <literal>0x</literal>, stands for `zero times'.
2354 Similarly, <literal>1x</literal> stands for `exactly one', <literal>3+x</literal> for `three and more',
2355 <literal>2-x</literal> for `at most two', or <literal>1..10x</literal> for `one to ten'.</p>
2356 <h3>Output Filters</h3>
2357 <a name="output-filters" ></a>
2358 <p>To count number of all matches of a query, append an output filter, e.g.</p>
2359 <pre class="programlisting" id="example-7">%try%%node% [ ]
2360 >> count()</pre>
2361 <p>Be careful when counting matches for queries with more than one node,
2362 To count only distinct occurrences of a particular node, say <tt>$a</tt>, regardless of the other nodes
2363 in the query, use these output filters instead:</p>
2364 <pre class="programlisting">
2365 >> distinct $a
2366 >> count()</pre>
2367 <p>Output filters are used to extract data and generate a tabular output from a query.
2368 For example, the following query lists all values of the attribute %attr%
2369 and counts their occurrences:
2370 <pre class="programlisting" id="example-7">%try%%node% $a:= [ ]
2371 >> for $a.%attr% give $1, count()</pre>
2372 </p>
2373  
2374 See the <a target="_blank" href="http://ufal.mff.cuni.cz/~pajas/pmltq/doc/pmltq_doc.html">Documentation</a> for more on output-filters.</p>
2375 <div>
2376 @
2377 }
2378  
2379 1 1; # End of PMLTQ::CGI
2380  
2381 __END__
↑ Summary