blib/lib/PMLTQ/Command/query.pm

filter: Statementstmt 15.0% 48 / 320 Branchbran 0.0% 0 / 194 Conditioncond 0.0% 0 / 104 MC/DCmcdc 0.0% 0 / 62 Subroutinesub 40.0% 14 / 35 Podpod 0.0% 0 / 12 totaltotal 8.5% 62 / 727 CC 146 SCAR 67.8
CC
146
Cov
8%
pmltq_http_search
2535.6
jtred_search
600.0
ntred_search
306.0
CRAP 877.3
« blib/lib/PMLTQ/Common.pm ↑ Summary blib/lib/PMLTQ/CGI.pm »
1 package PMLTQ::Command::query;
2 1 our $AUTHORITY = 'cpan:MATY';
3 1 $PMLTQ::Command::query::VERSION = '1.2.2';
4 # ABSTRACT: WIP: Executes query on treebank
5  
6 1 ▶use PMLTQ::Base 'PMLTQ::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Treex::PML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Treex::PML::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Treex::PML::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use PMLTQ::Common ':tredmacro';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 my $extension_dir;
21 my %opts;
22  
23 0 ▶has usage => sub { shift->extract_usage };
Subroutine __ANON__: not called CC=1 SCAR=1.2
24  
25 sub run {
26 0 ▶ my $self = shift;
Subroutine run: not called CC=9 SCAR=45.0
Pod undocumented
27 0 my @args = @_;
28 0 ▶ GetOptionsFromArray(\@args, \%opts,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@args, \%opts, "debug|D", "server|s=s", "command|c=s", "ntred|N", "jtred|J", "btred|B", "sql|S", "shared-dir|d=s", "keep-tmp-files", "filelist|l=s", "username=s", "password=s", "auth-id=s", "pmltq-extension-dir|X=s", "stdin", "query|Q=s", "query-id|i=s", "query-file|f=s", "query-pml-file|p=s", "filters|F=s", "no-filters", "netgraph-query|G=s", "print-servers|P", "config-file|c=s", "node-types|n", "relations|r", "limit|L=i", "timeout|t=i", "quiet|q", "help|h=s\@", "usage|u", "version|V", "man") or die "invalid options" 0 0 0
29 'debug|D',
30 'server|s=s',
31 'command|c=s',
32  
33 'ntred|N',
34 'jtred|J',
35 'btred|B',
36 'sql|S',
37 'shared-dir|d=s',
38 'keep-tmp-files',
39 'filelist|l=s',
40  
41 'username=s',
42 'password=s',
43 'auth-id=s',
44  
45 'pmltq-extension-dir|X=s',
46  
47 'stdin',
48 'query|Q=s',
49 'query-id|i=s',
50 'query-file|f=s',
51 'query-pml-file|p=s',
52 'filters|F=s',
53 'no-filters',
54  
55 'netgraph-query|G=s',
56  
57 'print-servers|P',
58 'config-file|c=s',
59  
60 'node-types|n',
61 'relations|r',
62  
63 'limit|L=i',
64 'timeout|t=i',
65  
66 'quiet|q',
67 'help|h=s@',
68 'usage|u',
69 'version|V',
70 'man' ) || die "invalid options";
71 Treex::PML::AddResourcePath(
72 PMLTQ->resources_dir,
73 File::Spec->catfile(${FindBin::RealBin},'config'),
74 0 $ENV{HOME}.'/.tred.d'
75 );
76 0 Treex::PML::AddBackends(qw(Storable PMLBackend PMLTransformBackend));
77  
78 0 ▶ if ($opts{stdin}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"stdin"}) 0 0 0
79 0 local $/;
80 0 $opts{query} = <STDIN>;
81 }
82  
83 0 ▶ $opts{$1}=1 if defined($opts{server}) and $opts{server}=~s{^[nbj]tred://}{};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"server"} and $opts{"server"} =~ s[^[nbj]tred://][] 0 0 0
Condition0%0/3
defined $opts{"server"} and $opts{"server"} =~ s[^[nbj]tred://][]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"server"} and $opts{"server"} =~ s[^[nbj]tred://][]
defined $opts{"server"} $opts{"server"} =~ s[^[nbj]tred://][]
Truth table0%0/3
A and B
A= defined $opts{"server"}B= $opts{"server"} =~ s[^[nbj]tred://][]
ABresult
0X0
100
111
84 $extension_dir =
85 $opts{'pmltq-extension-dir'} ||
86 0 ▶ File::Spec->catfile($ENV{HOME},'.tred.d','extensions', 'pmltq');
Condition0%0/3
$opts{'pmltq-extension-dir'} || 'File::Spec'->catfile($ENV{'HOME'}, '.tred.d', 'extensions', 'pmltq')
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'pmltq-extension-dir'} || 'File::Spec'->catfile($ENV{'HOME'}, '.tred.d', 'extensions', 'pmltq')
$opts{'pmltq-extension-dir'} 'File::Spec'->catfile($ENV{'HOME'}, '.tred.d', 'extensions', 'pmltq')
Truth table0%0/3
A || B
A= $opts{'pmltq-extension-dir'}B= 'File::Spec'->catfile($ENV{'HOME'}, '.tred.d', 'extensions', 'pmltq')
ABresult
1X1
011
000
87 0 Treex::PML::AddResourcePath(File::Spec->catfile($extension_dir,'resources'));
88  
89 0 ▶ if ($opts{ntred}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opts{'ntred'}) { } 0 0 0
elsif ($opts{'jtred'}) { } 0 0 0
elsif ($opts{'btred'}) { } 0 0 0
90 0 ntred_search();
91 } elsif ($opts{jtred}) {
92 0 jtred_search();
93 } elsif ($opts{btred}) {
94 0 btred_search(@args);
95 } else {
96 0 pmltq_http_search();
97 }
98 }
99  
100 1 my %auth;
101 sub pmltq_http_search {
102 0 ▶ my @args = @_;
Subroutine pmltq_http_search: not called CC=51 SCAR=78.4
Pod undocumented
103 0 my $query;
104 0 ▶ if ($opts{query} and !@args) {
Branch: 5 branches
BranchTrueFalseTotal
if ($opts{'query'} and not @args) { } 0 0 0
elsif (not $query and @args) { } 0 0 0
elsif ($opts{'query-pml-file'}) { } 0 0 0
elsif ($opts{'query-file'}) { } 0 0 0
elsif (not $opts{'node-types'} || $opts{'relations'} || $opts{'print-servers'}) { } 0 0 0
Condition0%0/3
$opts{"query"} and not @args
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"query"} and not @args
$opts{"query"} not @args
Truth table0%0/3
A and not(B)
A= $opts{"query"}B= not @args
ABresult
0X0
100
111
105 0 $query = $opts{query};
106 ▶ } elsif (!$query and @args) {
Condition0%0/3
not $query and @args
!ll&&!rl&&r
000
MC/DC0%0/2
not $query and @args
not $query @args
Truth table0%0/3
not(A) and B
A= not $queryB= @args
ABresult
0X0
100
111
107 0 $query=join ' ',@args;
108 } elsif ($opts{'query-pml-file'}) {
109 0 my $query_file = Treex::PML::Factory->createDocumentFromFile($opts{'query-pml-file'});
110 0 ▶ die "Failed to open PML query file $opts{'query-pml-file'}: $Treex::PML::FSError\n" if $Treex::PML::FSError or !$query_file or !$query_file->trees;
Branch: 1 branch
BranchTrueFalseTotal
if $Treex::PML::FSError or not $query_file or not $query_file->trees 0 0 0
Condition0%0/6
$Treex::PML::FSError or not $query_file or not $query_file->trees
l!l&&r!l&&!r
000
$Treex::PML::FSError or not $query_file
l!l&&r!l&&!r
000
MC/DC0%0/3
$Treex::PML::FSError or not $query_file or not $query_file->trees
$Treex::PML::FSError not $query_file not $query_file->trees
Truth table0%0/4
A or not(B) or not(C)
A= $Treex::PML::FSErrorB= not $query_fileC= not $query_file->trees
ABCresult
1XX1
01X1
0011
0000
111 $query = first {
112 0 ▶ !$opts{'query-id'} or $_->{id} and $_->{id} eq $opts{'query-id'}
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
not $opts{"query-id"} or $_->{"id"} and $_->{"id"} eq $opts{"query-id"} 0 0 0
Condition0%0/6
!$opts{'query-id'} || $_->{'id'} && $_->{'id'} eq $opts{'query-id'}
l!l&&r!l&&!r
000
$_->{"id"} and $_->{"id"} eq $opts{"query-id"}
!ll&&!rl&&r
000
MC/DC0%0/3
!$opts{'query-id'} || $_->{'id'} && $_->{'id'} eq $opts{'query-id'}
!$opts{'query-id'} $_->{"id"} $_->{"id"} eq $opts{"query-id"}
Truth table0%0/4
not(A) || B and C
A= !$opts{'query-id'}B= $_->{"id"}C= $_->{"id"} eq $opts{"query-id"}
ABCresult
1XX1
0111
00X0
0100
113 0 } $query_file->trees;
114 0 ▶ die "Didn't find query $opts{'query-id'} in query file $opts{'query-pml-file'}!" unless $query;
Branch: 1 branch
BranchTrueFalseTotal
unless $query 0 0 0
115 0 $query = encode('UTF-8',PMLTQ::Common::as_text($query));
116 } elsif ($opts{'query-file'}) {
117 0 local $/;
118 0 ▶ open my $fh, $opts{'query-file'}
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $opts{"query-file"} or die qq[Cannot open query file '$opts{"query-file"}': $!] 0 0 0
119 or die "Cannot open query file '$opts{'query-file'}': $!";
120 0 $query = <$fh>;
121 0 ▶ if ($opts{'query-id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"query-id"}) 0 0 0
122 0 $query=~s/#\s*==\s*query:\s*\Q$opts{'query-id'}\E\s* ==(.*?)(?:#\s*==\s*query:\s*\w+\s*==.*|$)/$1/s;
123 }
124 ▶ } elsif (!$opts{'node-types'} and !$opts{'relations'} and !$opts{'print-servers'}) {
Condition0%0/6
$opts{'node-types'} || $opts{'relations'} || $opts{'print-servers'}
l!l&&r!l&&!r
000
$opts{"node-types"} or $opts{"relations"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts{'node-types'} || $opts{'relations'} || $opts{'print-servers'}
$opts{"node-types"} $opts{"relations"} $opts{'print-servers'}
Truth table0%0/4
A or B || C
A= $opts{"node-types"}B= $opts{"relations"}C= $opts{'print-servers'}
ABCresult
1XX1
01X1
0011
0000
125 0 pod2usage(-msg => 'pmltq');
126 0 exit 1;
127 }
128 0 ▶ if (!$opts{'query-pml-file'} and $opts{'netgraph-query'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts{"query-pml-file"} and $opts{"netgraph-query"}) 0 0 0
Condition0%0/3
not $opts{"query-pml-file"} and $opts{"netgraph-query"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $opts{"query-pml-file"} and $opts{"netgraph-query"}
not $opts{"query-pml-file"} $opts{"netgraph-query"}
Truth table0%0/3
not(A) and B
A= not $opts{"query-pml-file"}B= $opts{"netgraph-query"}
ABresult
0X0
100
111
129 0 require PMLTQ::NG2PMLTQ;
130 0 $query = PMLTQ::NG2PMLTQ::ng2pmltq($query,{type=>$opts{'netgraph-query'}});
131 }
132  
133 0 ▶ if (!$opts{'node-types'} and !$opts{'relations'} and !$opts{'print-servers'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"node-types"} or $opts{"relations"} or $opts{"print-servers"}) 0 0 0
Condition0%0/6
$opts{"node-types"} or $opts{"relations"} or $opts{"print-servers"}
l!l&&r!l&&!r
000
$opts{"node-types"} or $opts{"relations"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts{"node-types"} or $opts{"relations"} or $opts{"print-servers"}
$opts{"node-types"} $opts{"relations"} $opts{"print-servers"}
Truth table0%0/4
A or B or C
A= $opts{"node-types"}B= $opts{"relations"}C= $opts{"print-servers"}
ABCresult
1XX1
01X1
0011
0000
134 0 ▶ die "Query is empty!" unless $query;
Branch: 1 branch
BranchTrueFalseTotal
unless $query 0 0 0
135  
136 0 my $filters = $opts{'filters'};
137 0 ▶ if ($filters and $filters=~/\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filters and $filters =~ /\S/) 0 0 0
Condition0%0/3
$filters and $filters =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
$filters and $filters =~ /\S/
$filters $filters =~ /\S/
Truth table0%0/3
A and B
A= $filtersB= $filters =~ /\S/
ABresult
0X0
100
111
138 0 ▶ $filters='>> '.$filters unless $filters =~ /^\s*>>/;
Branch: 1 branch
BranchTrueFalseTotal
unless $filters =~ /^\s*>>/ 0 0 0
139 0 $query .= $filters;
140 }
141 }
142  
143 0 ▶ $opts{'config-file'} ||= Treex::PML::FindInResources('treebase.conf');
Condition0%0/3
$opts{"config-file"} ||= Treex::PML::FindInResources("treebase.conf")
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"config-file"} ||= Treex::PML::FindInResources("treebase.conf")
$opts{"config-file"} Treex::PML::FindInResources("treebase.conf")
Truth table0%0/3
A ||= B
A= $opts{"config-file"}B= Treex::PML::FindInResources("treebase.conf")
ABresult
1X1
011
000
144 0 ▶ if ($opts{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"debug"}) 0 0 0
145 0 print STDERR "Reading configuration from $opts{'config-file'}\n";
146 }
147 my $configs = (-f $opts{'config-file'}) ?
148 Treex::PML::Factory->createPMLInstance({ filename=>$opts{'config-file'} })->get_root->{configurations}
149 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $opts{'config-file'} ? : 0 0 0
150  
151 0 my $id = $opts{'server'};
152 0 ▶ $id ||= 'default' unless $opts{'print-servers'};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"print-servers"} 0 0 0
Condition0%0/2
$id ||= "default"
l!l
00
MC/DC0%0/1
$id ||= "default"
$id
Truth table0%0/2
A
A= $id
Aresult
11
00
153 0 ▶ my ($conf,$type) = $id ? get_server_conf($configs,$id) : ();
Branch: 1 branch
BranchTrueFalseTotal
$id ? : 0 0 0
154 %auth = (
155 username => $opts{username},
156 password => $opts{password},
157 0 );
158 0 ▶ if ($opts{'auth-id'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"auth-id"}) 0 0 0
159 0 my ($auth) = get_server_conf($configs,$opts{'auth-id'});
160 0 ▶ if ($auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth) { } 0 0 0
161 0 ▶ $auth{$_} ||= $auth->{$_} for qw(username password);
Condition0%0/3
$auth{$_} ||= $auth->{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$auth{$_} ||= $auth->{$_}
$auth{$_} $auth->{$_}
Truth table0%0/3
A ||= B
A= $auth{$_}B= $auth->{$_}
ABresult
1X1
011
000
162 } else {
163 0 die "Didn't find auth-id configuration: $opts{'auth-id'}\n";
164 }
165 }
166 0 ▶ if ($conf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf) 0 0 0
167 0 ▶ $auth{$_} ||= $conf->{$_} for qw(username password);
Condition0%0/3
$auth{$_} ||= $conf->{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$auth{$_} ||= $conf->{$_}
$auth{$_} $conf->{$_}
Truth table0%0/3
A ||= B
A= $auth{$_}B= $conf->{$_}
ABresult
1X1
011
000
168 }
169  
170  
171 0 ▶ if ($opts{'print-servers'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"print-servers"}) 0 0 0
172 0 ▶ if ($opts{'server'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"server"}) 0 0 0
173 0 ▶ unless ($type eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type eq "http") 0 0 0
174 0 die "Cannot query available services on a $type server";
175 }
176 0 my $result='';
177 http_search($conf->{url},$query,{ other=>1,
178 0 ▶ callback => sub { $result.=$_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
179 debug=>$opts{debug},
180 0 %auth,
181 });
182 0 my @services = split /\n/,$result;
183 0 for my $srv (@services) {
184 0 my %srv = map { split(':',$_,2) } split /\t/, $srv;
185 0 print $srv{id},"\t",$srv{service},"\t",$srv{title},"\n";
186 }
187 0 exit;
188 }
189 0 my @types = qw(dbi http);
190 0 my %columns = (
191 dbi => [qw(driver host port database username sources)],
192 http => [qw(url username cached_description/title)],
193 );
194 my %configs = (
195 0 map { my $type = $_; ($_ =>[map $_->value, grep { $_->name eq $type } SeqV($configs)]) }
196 @types
197 );
198 0 for my $type (@types) {
199 0 my $confs = $configs{$type};
200 0 ▶ if (@$confs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$confs) 0 0 0
201 0 print uc($type)." configurations:\n";
202 0 print (("-"x60)."\n");
203 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
204 0 for my $c (@$confs) {
205 print $c->{id}.": ".(join(", ", map "$_->[0]=$_->[1]",
206 grep length($_->[1]),
207 0 ▶ map [m{/(.*)} ? $1 : $_,Treex::PML::Instance::get_data($c,$_)], @{$columns{$type}})."\n");
Branch: 1 branch
BranchTrueFalseTotal
m[/(.*)] ? : 0 0 0
208 }
209 }
210 0 print "\n";
211 }
212 0 exit;
213 }
214  
215  
216  
217 0 ▶ print STDERR $query,"\n" if $opts{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} 0 0 0
218  
219  
220  
221 0 ▶ if ($type eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'http') { } 0 0 0
222 http_search($conf->{url},$query,{ 'node-types'=>$opts{'node-types'},
223 'relations'=>$opts{'relations'},
224 debug=>$opts{debug},
225 0 %auth
226 });
227 } else {
228 0 require PMLTQ::SQLEvaluator;
229 my $evaluator = PMLTQ::SQLEvaluator->new(undef,{connect => $conf, debug=>$opts{debug},
230 0 %auth
231 });
232 0 $evaluator->connect();
233 0 ▶ if ($opts{'node-types'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'node-types'}) { } 0 0 0
elsif ($opts{'relations'}) { } 0 0 0
234 0 print join "\n", @{$evaluator->get_node_types};
235 } elsif ($opts{'relations'}) {
236 0 print join "\n", @{$evaluator->get_specific_relations};
237 } else {
238 0 search($evaluator,$query);
239 }
240 0 ▶ $evaluator->{dbi}->disconnect() if $evaluator->{dbi};
Branch: 1 branch
BranchTrueFalseTotal
if $evaluator->{"dbi"} 0 0 0
241 }
242 }
243  
244 sub get_server_conf {
245 0 ▶ my ($configs,$id)=@_;
Subroutine get_server_conf: not called CC=3 SCAR=24.8
Pod undocumented
246 0 my ($conf,$type);
247 0 ▶ if ($id =~ /^http:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^http:/) { } 0 0 0
248 0 $type = 'http';
249 0 $conf = {url => $id};
250 } else {
251 0 ▶ my $conf_el = first { $_->value->{id} eq $id } SeqV($configs);
Subroutine __ANON__: not called CC=1 SCAR=6.9
252 0 ▶ die "Didn't find server configuration named '$id'!\nUse $0 --print-servers and then $0 --server <config-id|URL>\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $conf_el 0 0 0
253 unless $conf_el;
254 0 $conf = $conf_el->value;
255 0 $type = $conf_el->name;
256 }
257 0 return ($conf,$type);
258 }
259  
260 sub http_search {
261 0 ▶ my ($url,$query,$opts)=@_;
Subroutine http_search: not called CC=10 SCAR=47.0
Pod undocumented
262 0 ▶ $opts||={};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
263 0 my $tmp = File::Temp->new( TEMPLATE => 'pmltq_XXXXX',
264 TMPDIR => 1,
265 UNLINK => 1,
266 SUFFIX => '.txt' );
267 0 my $ua = LWP::UserAgent->new;
268 $ua->credentials(URI->new($url)->host_port,'PMLTQ',
269 $auth{username}, $auth{password})
270 0 ▶ if $opts->{username};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"username"} 0 0 0
271 0 $ua->agent("PMLTQ/1.0 ");
272 0 ▶ $url.='/' unless $url=~m{^https?://.+/};
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^https?://.+/] 0 0 0
273 0 ▶ if ($opts->{'node-types'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opts->{'node-types'}) { } 0 0 0
elsif ($opts->{'relations'}) { } 0 0 0
elsif ($opts->{'other'}) { } 0 0 0
274 0 $url = qq{${url}nodetypes};
275 0 $query = '';
276 } elsif ($opts->{'relations'}) {
277 0 $url = qq{${url}relations};
278 0 $query = '';
279 } elsif ($opts->{'other'}) {
280 0 $url = qq{${url}other};
281 0 $query = '';
282 } else {
283 0 $url = qq{${url}query};
284 }
285 0 ▶ $ua->timeout($opts{timeout}+2) if $opts{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"timeout"} 0 0 0
286 0 my $q = $query; Encode::_utf8_off($q);
287 0 binmode STDOUT;
288 0 ▶ my $sub = $opts->{callback} || sub { print $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$opts->{'callback'} || sub { print $_[0]; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'callback'} || sub { print $_[0]; }
$opts->{'callback'} sub { print $_[0]; }
Truth table0%0/3
A || B
A= $opts->{'callback'}B= sub { print $_[0]; }
ABresult
1X1
011
000
289 my $res = $ua->request(POST($url, [
290 query => $q,
291 format => 'text',
292 limit => $opts{limit},
293 row_limit => $opts{limit},
294 timeout => $opts{timeout},
295 0 ]),$sub ,1024*8 );
296 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
297 0 die $res->status_line."\n".$res->content."\n";
298 }
299 }
300  
301 sub search {
302 0 ▶ my ($evaluator,$query)=@_;
Subroutine search: not called CC=5 SCAR=34.0
Pod undocumented
303 0 my $results;
304 0 eval {
305 0 $evaluator->prepare_query($query); # count=>1
306 $results = $evaluator->run({
307 node_limit => $opts{limit},
308 row_limit => $opts{limit},
309 timeout => $opts{timeout},
310 timeout_callback => sub {
311 0 ▶ print STDERR "Evaluation of query timed out\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
312 0 exit 2;
313 },
314 0 });
315 };
316 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
317 0 ▶ if ($results) {
Branch: 1 branch
BranchTrueFalseTotal
if ($results) 0 0 0
318 0 for my $r (@$results) {
319 0 print join("\t",@$r)."\n";
320 }
321 0 ▶ print STDERR $#$results+1," result(s)\n" unless $opts{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"quiet"} 0 0 0
322 }
323 }
324  
325 sub quote_cmdline {
326 0 ▶ my $quoted;
Subroutine quote_cmdline: not called CC=1 SCAR=6.9
Pod undocumented
327 join ' ', map {
328 0 my $arg = $_;
329 0 $arg =~ s{'}{'\\''}g;
330 0 qq{'$arg'}
331 } @_;
332 }
333  
334 sub ntred_search {
335 0 ▶ my @args = @_;
Subroutine ntred_search: not called CC=17 SCAR=57.2
Pod undocumented
336 0 ▶ my ($host,$port)= $opts{server} ? split(/:/,$opts{server}) : ();
Branch: 1 branch
BranchTrueFalseTotal
$opts{'server'} ? : 0 0 0
337 0 ▶ my $command = $opts{command} || 'ntred';
Condition0%0/2
$opts{'command'} || 'ntred'
l!l
00
MC/DC0%0/1
$opts{'command'} || 'ntred'
$opts{'command'}
Truth table0%0/2
A
A= $opts{'command'}
Aresult
11
00
338  
339 0 ▶ my $shared_dir=File::Spec->rel2abs($opts{'shared-dir'} || '.');
Condition0%0/2
$opts{'shared-dir'} || '.'
l!l
00
MC/DC0%0/1
$opts{'shared-dir'} || '.'
$opts{'shared-dir'}
Truth table0%0/2
A
A= $opts{'shared-dir'}
Aresult
11
00
340 0 my $filter_file="$shared_dir/pmltq_ntred_filter.$$.pl";
341  
342 0 my @script_flags=('--filter-code-out', $filter_file);
343 0 foreach (qw(query query-id query-file query-pml-file filters netgraph-query)) {
344 push @script_flags, '--'.$_, (/file/ ? File::Spec->rel2abs($opts{$_}) : $opts{$_})
345 0 ▶ if defined($opts{$_}) and length($opts{$_});
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{$_} and length $opts{$_} 0 0 0
/file/ ? : 0 0 0
Condition0%0/3
defined $opts{$_} and length $opts{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{$_} and length $opts{$_}
defined $opts{$_} length $opts{$_}
Truth table0%0/3
A and B
A= defined $opts{$_}B= length $opts{$_}
ABresult
0X0
100
111
346 }
347  
348 $command .= ' '.quote_cmdline(
349 ((defined($host) and length($host)) ? ('--hub',$host) : ()),
350 ((defined($port) and length($port)) ? ('--port',$port) : ()),
351 '-q',
352 '-I', File::Spec->catfile($extension_dir,qw(contrib pmltq pmltq.ntred)),
353 0 ▶ ($opts{filelist} ? ('-l', File::Spec->rel2abs($opts{filelist})) : (@args ? ('-L', '--', @args) : ())),
Branch: 4 branches
BranchTrueFalseTotal
defined $host && length $host ? : 0 0 0
defined $port && length $port ? : 0 0 0
$opts{'filelist'} ? : 0 0 0
@args ? : 0 0 0
Condition0%0/6
defined $host and length $host
!ll&&!rl&&r
000
defined $port and length $port
!ll&&!rl&&r
000
MC/DC0%0/2
defined $host and length $host
defined $host length $host
MC/DC0%0/2
defined $port and length $port
defined $port length $port
Truth table0%0/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $portB= length $port
ABresult
0X0
100
111
354 '--', @script_flags
355 );
356 0 ▶ open(my $pipe, $command.' | ') || die "Failed to start ntred client: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $pipe, $command . ' | ' or die "Failed to start ntred client: $!" 0 0 0
357 0 apply_filter($pipe, $filter_file);
358 0 close($pipe);
359 0 ▶ unlink $filter_file if -f $filter_file and !$opts{'keep-tmp-files'};
Branch: 1 branch
BranchTrueFalseTotal
if -f $filter_file and not $opts{"keep-tmp-files"} 0 0 0
Condition0%0/3
-f $filter_file and not $opts{"keep-tmp-files"}
!ll&&!rl&&r
000
MC/DC0%0/2
-f $filter_file and not $opts{"keep-tmp-files"}
-f $filter_file not $opts{"keep-tmp-files"}
Truth table0%0/3
A and not(B)
A= -f $filter_fileB= not $opts{"keep-tmp-files"}
ABresult
0X0
100
111
360 }
361  
362 sub jtred_search {
363 0 ▶ my @args = @_;
Subroutine jtred_search: not called CC=24 SCAR=64.0
Pod undocumented
364 0 ▶ my $command = $opts{command} || 'jtred';
Condition0%0/2
$opts{'command'} || 'jtred'
l!l
00
MC/DC0%0/1
$opts{'command'} || 'jtred'
$opts{'command'}
Truth table0%0/2
A
A= $opts{'command'}
Aresult
11
00
365  
366 0 my $jobname="pmltq_jtred_$$";
367 0 ▶ if ($opts{server}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"server"}) 0 0 0
368 0 $jobname.="-".$ENV{HOSTNAME};
369 }
370  
371 0 ▶ my $shared_dir=File::Spec->rel2abs($opts{'shared-dir'} || '.');
Condition0%0/2
$opts{'shared-dir'} || '.'
l!l
00
MC/DC0%0/1
$opts{'shared-dir'} || '.'
$opts{'shared-dir'}
Truth table0%0/2
A
A= $opts{'shared-dir'}
Aresult
11
00
372 0 my $filter_file="$shared_dir/$jobname.pl";
373 0 my $filelist;
374 0 ▶ if ($opts{filelist}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"filelist"}) 0 0 0
375 0 my ($vol,$dir) = File::Spec->splitpath($opts{filelist});
376 0 my $base = File::Spec->catpath($vol,$dir);
377 0 ▶ open my $fh, '<', $opts{filelist} or die "Cannot open filelist $opts{filelist}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $opts{"filelist"} or die qq[Cannot open filelist $opts{"filelist"}: $!] 0 0 0
378 0 $filelist = "$shared_dir/$jobname.fl";
379 0 ▶ open my $out_fh, '>', $filelist or die "Cannot create temporary filelist $filelist: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">", $filelist or die "Cannot create temporary filelist ${filelist}: $!" 0 0 0
380 0 ▶ print STDERR "Resolving filelist files to $base...\n" unless $opts{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"quiet"} 0 0 0
381 0 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
382 0 chomp;
383 0 print $out_fh File::Spec->rel2abs($_,$base),"\n";
384 }
385 0 ▶ print STDERR "done.\n" unless $opts{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"quiet"} 0 0 0
386 0 close $fh;
387 0 close $out_fh;
388 }
389 0 my @script_flags=('--filter-code-out', $filter_file);
390 0 foreach (qw(query query-id query-file query-pml-file filters netgraph-query)) {
391 push @script_flags, '--'.$_, (/file/ ? File::Spec->rel2abs($opts{$_}) : $opts{$_})
392 0 ▶ if defined($opts{$_}) and length($opts{$_});
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{$_} and length $opts{$_} 0 0 0
/file/ ? : 0 0 0
Condition0%0/3
defined $opts{$_} and length $opts{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{$_} and length $opts{$_}
defined $opts{$_} length $opts{$_}
Truth table0%0/3
A and B
A= defined $opts{$_}B= length $opts{$_}
ABresult
0X0
100
111
393 }
394 my @command = (
395 $command,
396 ($opts{'shared-dir'} ? ('-jw', $shared_dir) : ()),
397 '-jn', $jobname,
398 0 ▶ ($opts{quiet} ? '-jq' : ()),
Branch: 3 branches
BranchTrueFalseTotal
$opts{'shared-dir'} ? : 0 0 0
$opts{'quiet'} ? : 0 0 0
$filelist ? : 0 0 0
399 ($filelist ? ('-l', $filelist) : @args),
400 '-jb',
401 '-q',
402 '-I', File::Spec->catfile($extension_dir,qw(contrib pmltq pmltq.ntred)),
403 '-o', @script_flags, '--'
404 );
405 0 my $pipe;
406 0 ▶ if ($opts{server}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'server'}) { } 0 0 0
407 0 my $cwd = quote_cmdline(getcwd());
408 0 ▶ open($pipe, '-|', 'ssh', $opts{server}, <<"SCRIPT".quote_cmdline(@command))
Branch: 1 branch
BranchTrueFalseTotal
open $pipe, "-|", "ssh", $opts{"server"}, "if [ -f ~/.bash_profile ]; then\n . ~/.bash_profile 2>/dev/null 1>&2\nelif [ -f ~/.profile ]; then\n . ~/.profile 2>/dev/null 1>&2\nfi\ncd $cwd;\n" . quote_cmdline(@command) or die qq[Failed to start jtred on host $opts{"server"} over ssh: $!] 0 0 0
409 if [ -f ~/.bash_profile ]; then
410 . ~/.bash_profile 2>/dev/null 1>&2
411 elif [ -f ~/.profile ]; then
412 . ~/.profile 2>/dev/null 1>&2
413 fi
414 cd $cwd;
415 SCRIPT
416 || die "Failed to start jtred on host $opts{server} over ssh: $!"
417 } else {
418 0 ▶ open($pipe, '-|',@command)
Branch: 1 branch
BranchTrueFalseTotal
open $pipe, "-|", @command or die "Failed to start jtred: $!" 0 0 0
419 || die "Failed to start jtred: $!";
420 }
421 0 apply_filter($pipe, $filter_file);
422 0 close($pipe);
423 0 ▶ unlink $filter_file if -f $filter_file and !$opts{'keep-tmp-files'};
Branch: 1 branch
BranchTrueFalseTotal
if -f $filter_file and not $opts{"keep-tmp-files"} 0 0 0
Condition0%0/3
-f $filter_file and not $opts{"keep-tmp-files"}
!ll&&!rl&&r
000
MC/DC0%0/2
-f $filter_file and not $opts{"keep-tmp-files"}
-f $filter_file not $opts{"keep-tmp-files"}
Truth table0%0/3
A and not(B)
A= -f $filter_fileB= not $opts{"keep-tmp-files"}
ABresult
0X0
100
111
424 0 ▶ unlink $filelist if $filelist and !$opts{'keep-tmp-files'};
Branch: 1 branch
BranchTrueFalseTotal
if $filelist and not $opts{"keep-tmp-files"} 0 0 0
Condition0%0/3
$filelist and not $opts{"keep-tmp-files"}
!ll&&!rl&&r
000
MC/DC0%0/2
$filelist and not $opts{"keep-tmp-files"}
$filelist not $opts{"keep-tmp-files"}
Truth table0%0/3
A and not(B)
A= $filelistB= not $opts{"keep-tmp-files"}
ABresult
0X0
100
111
425 }
426  
427 sub btred_search {
428 0 ▶ my @args = @_;
Subroutine btred_search: not called CC=15 SCAR=54.8
Pod undocumented
429 0 ▶ my $command = $opts{command} || 'btred';
Condition0%0/2
$opts{'command'} || 'btred'
l!l
00
MC/DC0%0/1
$opts{'command'} || 'btred'
$opts{'command'}
Truth table0%0/2
A
A= $opts{'command'}
Aresult
11
00
430  
431 0 my $jobname="pmltq_btred_$$";
432 0 ▶ if ($opts{server}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"server"}) 0 0 0
433 0 $jobname.="-".$ENV{HOSTNAME};
434 }
435  
436 0 ▶ my $shared_dir=File::Spec->rel2abs($opts{'shared-dir'} || '.');
Condition0%0/2
$opts{'shared-dir'} || '.'
l!l
00
MC/DC0%0/1
$opts{'shared-dir'} || '.'
$opts{'shared-dir'}
Truth table0%0/2
A
A= $opts{'shared-dir'}
Aresult
11
00
437 0 my $filter_file="$shared_dir/$jobname.pl";
438  
439 0 my @script_flags=('--filter-code-out', $filter_file);
440 0 foreach (qw(query query-id query-file query-pml-file filters netgraph-query)) {
441 push @script_flags, '--'.$_, (/file/ ? File::Spec->rel2abs($opts{$_}) : $opts{$_})
442 0 ▶ if defined($opts{$_}) and length($opts{$_});
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{$_} and length $opts{$_} 0 0 0
/file/ ? : 0 0 0
Condition0%0/3
defined $opts{$_} and length $opts{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{$_} and length $opts{$_}
defined $opts{$_} length $opts{$_}
Truth table0%0/3
A and B
A= defined $opts{$_}B= length $opts{$_}
ABresult
0X0
100
111
443 }
444 0 for (qw(node-types relations)) {
445 0 ▶ if ($opts{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{$_}) 0 0 0
446 0 push @script_flags, '--info', $_;
447 0 last;
448 }
449 }
450  
451 $command .= ' '.quote_cmdline(
452 ($opts{quiet} ? '-Q' : '-q'),
453 '-I', File::Spec->catfile($extension_dir,qw(contrib pmltq pmltq.ntred)),
454 '-o', '--apply-filters', @script_flags, '--',
455 0 ▶ ($opts{filelist} ? ('-l', $opts{filelist}) : @args),
Branch: 2 branches
BranchTrueFalseTotal
$opts{'quiet'} ? : 0 0 0
$opts{'filelist'} ? : 0 0 0
456 );
457 0 ▶ if ($opts{server}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'server'}) { } 0 0 0
458 0 my $cwd = quote_cmdline(getcwd());
459 0 system('ssh', $opts{server}, <<"SCRIPT");
460 if [ -f ~/.bash_profile ]; then
461 . ~/.bash_profile
462 elif [ -f ~/.profile ]; then
463 . ~/.profile;
464 fi
465 cd $cwd
466 $command
467 SCRIPT
468 } else {
469 0 print STDERR "$command\n";
470 0 system($command);
471 }
472 0 ▶ unlink $filter_file if -f $filter_file and !$opts{'keep-tmp-files'};
Branch: 1 branch
BranchTrueFalseTotal
if -f $filter_file and not $opts{"keep-tmp-files"} 0 0 0
Condition0%0/3
-f $filter_file and not $opts{"keep-tmp-files"}
!ll&&!rl&&r
000
MC/DC0%0/2
-f $filter_file and not $opts{"keep-tmp-files"}
-f $filter_file not $opts{"keep-tmp-files"}
Truth table0%0/3
A and not(B)
A= -f $filter_fileB= not $opts{"keep-tmp-files"}
ABresult
0X0
100
111
473 }
474  
475  
476 sub round {
477 0 ▶ my ($value, $precision) = @_;
Subroutine round: not called CC=3 SCAR=24.8
Pod undocumented
478 0 ▶ my $rounding = ($value >= 0 ? 0.5 : -0.5);
Branch: 1 branch
BranchTrueFalseTotal
$value >= 0 ? : 0 0 0
479 0 ▶ my $decimalscale = 10**int($precision || 0);
Condition0%0/2
$precision || 0
l!l
00
MC/DC0%0/1
$precision || 0
$precision
Truth table0%0/2
A
A= $precision
Aresult
11
00
480 0 my $scaledvalue = int($value * $decimalscale + $rounding);
481 0 return $scaledvalue / $decimalscale;
482 }
483  
484 sub trunc {
485 0 ▶ my ($self, $num, $digits) = @_;
Subroutine trunc: not called CC=2 SCAR=17.9
Pod undocumented
486 0 $digits = int $digits;
487 0 my $decimalscale = 10**abs($digits);
488 0 ▶ if ($digits >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digits >= 0) { } 0 0 0
489 0 return int($num * $decimalscale) / $decimalscale;
490 } else {
491 0 return int($num / $decimalscale) * $decimalscale;
492 }
493 }
494  
495 sub apply_filter {
496 0 ▶ my ($input, $filter_file)=@_;
Subroutine apply_filter: not called CC=15 SCAR=53.6
Pod undocumented
497 0 my $filters;
498 my $filter;
499 0 my $first = 1;
500 1 ▶ use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
501  
502 0 ▶ if ($opts{'no-filters'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"no-filters"}) 0 0 0
503 0 ▶ print while (<$input>);
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $input) and do { print $_ } 0 0 0
504 0 return;
505 }
506  
507 my $output_filter = {
508 ▶ init => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
509 process_row => sub {
510 0 ▶ my ($self,$row)=@_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
511 0 print(join("\t",@$row)."\n");
512 },
513 ▶ finish => sub { }
Subroutine __ANON__: not called CC=1 SCAR=0.0
514 0 };
515  
516 0 ▶ while (<$input>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $input)) 0 0 0
517 0 chomp;
518 0 ▶ unless ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filter) 0 0 0
519 0 ▶ if (-f $filter_file and -s $filter_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filter_file and -s $filter_file) { } 0 0 0
Condition0%0/3
-f $filter_file and -s $filter_file
!ll&&!rl&&r
000
MC/DC0%0/2
-f $filter_file and -s $filter_file
-f $filter_file -s $filter_file
Truth table0%0/3
A and B
A= -f $filter_fileB= -s $filter_file
ABresult
0X0
100
111
520 0 ▶ open my $fh, "<", $filter_file or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filter_file or die "Cannot open ${filter_file}: $!" 0 0 0
521 die "Cannot open $filter_file: $!";
522 0 my $filter_code;
523 {
524 0 local $/;
525 0 $filter_code = <$fh>;
526 }
527 0 eval "use utf8;\n".$filter_code;
528 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
529 0 print STDERR $filter_code;
530 0 print STDERR "\n";
531 0 die "Running filter $filter_file failed!";
532 }
533 my @filters = map {
534 0 my @local_filters = map eval, @{$_->{local_filters_code}};
535 0 my $sub = eval($_->{code});
536 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
537 0 $sub
538 } @$filters;
539  
540 # connect filters
541 0 my $prev;
542 0 for my $filter (@filters) {
543 0 ▶ $prev->{output}=$filter if $prev;
Branch: 1 branch
BranchTrueFalseTotal
if $prev 0 0 0
544 0 $prev = $filter;
545 }
546 0 ▶ if ($prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev) 0 0 0
547 0 $prev->{output} = $output_filter;
548 }
549 0 ▶ $filter = $filters[0] || die "First filter is empty!";
Condition0%0/2
$filters[0] || die('First filter is empty!')
l!l
00
MC/DC0%0/1
$filters[0] || die('First filter is empty!')
$filters[0]
Truth table0%0/2
A
A= $filters[0]
Aresult
11
00
550 0 $filter->{init}->($filter);
551 } else {
552 0 $filter = $output_filter;
553 }
554 }
555 0 $filter->{process_row}->($filter,[split /\t/,$_]);
556 }
557 0 ▶ $filter->{finish}->($filter) if $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $filter 0 0 0
558 }
559  
560  
561 1 1;
562  
563 __END__
« blib/lib/PMLTQ/Common.pm ↑ Summary blib/lib/PMLTQ/CGI.pm »