| 1 | package PMLTQ::Command; | ||||||||||||||||||||||||||||||||||||||
| 2 | 14 | our $AUTHORITY = 'cpan:MATY'; | |||||||||||||||||||||||||||||||||||||
| 3 | 14 | $PMLTQ::Command::VERSION = '2.0.3'; | |||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Command base class | ||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||
| 6 | 14 | ▶ | use PMLTQ::Base -base; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||
| 8 | 14 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use DBI; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 14 | ▶ | use File::Slurp; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | 14 | ▶ | use Pod::Usage 'pod2usage'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | 14 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 15 | 14 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 16 | 14 | ▶ | use HTTP::Cookies; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 17 | 14 | ▶ | use URI::WithBase; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 18 | 14 | ▶ | use URI::Encode qw(uri_encode); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 19 | 14 | ▶ | use Encode; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | has config => sub { die 'Command has no configuration'; }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | has usage => sub {'Usage: '}; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | has term => sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 26 | 0 | require Term::UI; | |||||||||||||||||||||||||||||||||||||
| 27 | 0 | require Term::ReadLine; | |||||||||||||||||||||||||||||||||||||
| 28 | 0 | Term::ReadLine->new('pmltq'); | |||||||||||||||||||||||||||||||||||||
| 29 | 14 | }; | |||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | has term_encoding => sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 32 | 0 | require Term::Encoding; | |||||||||||||||||||||||||||||||||||||
| 33 | 0 | Term::Encoding::get_encoding(); | |||||||||||||||||||||||||||||||||||||
| 34 | 14 | }; | |||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 38 | 0 | die 'Override by parent class'; | |||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub extract_usage { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
extract_usage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||
| 44 | 0 | open my $handle, '>', \my $output; | |||||||||||||||||||||||||||||||||||||
| 45 | 0 | pod2usage( -exitval => 'NOEXIT', -input => (caller)[1], -output => $handle ); | |||||||||||||||||||||||||||||||||||||
| 46 | 0 | $output =~ s/\n$//; | |||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||
| 48 | 0 | return $output; | |||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub help { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
help: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 52 | 0 | print shift->usage; | |||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub _db_connect { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_db_connect: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my ( $database, $host, $port, $user, $password ) = @_; | |||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $dbh = DBI->connect( 'DBI:Pg:dbname=' . $database . ';host=' . $host . ';port=' . $port, | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 58 | $user, $password, { RaiseError => 1, PrintError => 1 } ) | ||||||||||||||||||||||||||||||||||||||
| 59 | or die "Unable to connect to database!\n$DBI::errstr\n"; | ||||||||||||||||||||||||||||||||||||||
| 60 | 0 | return $dbh; | |||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub db { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
db: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $db = $self->config->{db}; | |||||||||||||||||||||||||||||||||||||
| 67 | 0 | return _db_connect( $db->{name}, $db->{host}, $db->{port}, $db->{user}, $db->{password} ); | |||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub sys_db { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
sys_db: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $config = $self->config; | |||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $db = $config->{db}; | |||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $sys_db = $config->{sys_db}; | |||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | unless ( ref $sys_db ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $sys_db = { name => $sys_db }; | |||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $sys_db->{$_} = $db->{$_} for ( grep { !defined $sys_db->{$_} } qw/user password/ ); | |||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||
| 83 | 0 | return _db_connect( $sys_db->{name}, $db->{host}, $db->{port}, $sys_db->{user}, $sys_db->{password} ); | |||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub run_sql_from_file { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
run_sql_from_file: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 87 | 0 | my ( $self, $file, $dir, $dbh ) = @_; | |||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my $sqlfile = File::Spec->catfile( $dir, $file ); | |||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $sql = read_file($sqlfile); | |||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | 0 | print STDERR "RUNNING SQL FROM $sqlfile\n"; | |||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ( $file =~ m/.ctl/ and my $copy = () = $sql =~ m/(COPY .*? FROM *?["'].*?["'])/g ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$file =~ /.ctl/ and my $copy = () = $sql =~ /(COPY .*? FROM *?["'].*?["'])/g
MC/DC0%0/2
$file =~ /.ctl/ and my $copy = () = $sql =~ /(COPY .*? FROM *?["'].*?["'])/g
$file =~ /.ctl/
my $copy = () = $sql =~ /(COPY .*? FROM *?["'].*?["'])/g
Truth table0%0/3
A and B
A= $file =~ /.ctl/B= my $copy = () = $sql =~ /(COPY .*? FROM *?["'].*?["'])/g
| |||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | die "More COPY commands than one in file is not supported.\n\n$sql\n" if $copy > 1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $sql =~ s/(COPY .*? FROM) *?["'](.*?)["']/$1 STDIN/; | |||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $dump_file = File::Spec->catfile( $dir, $2 ); | |||||||||||||||||||||||||||||||||||||
| 97 | 0 | eval { | |||||||||||||||||||||||||||||||||||||
| 98 | 0 | $dbh->do($sql); | |||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | open my $fh, '<', "$dump_file" or die "Can't open $dump_file: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | while ( my $data = <$fh> ) { # Do not load whole file, but process it line by line | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | next unless $data; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $data= Encode::decode("UTF-8", $data, Encode::FB_CROAK); | |||||||||||||||||||||||||||||||||||||
| 103 | 0 | $dbh->pg_putcopydata("$data"); | |||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $dbh->pg_putcopyend(); | |||||||||||||||||||||||||||||||||||||
| 106 | }; | ||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | warn $@ if $@; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||
| 109 | else { | ||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my @statements = split /\n\n/, $sql; | |||||||||||||||||||||||||||||||||||||
| 111 | 0 | for my $s (@statements) { | |||||||||||||||||||||||||||||||||||||
| 112 | 0 | eval { $dbh->do($s); }; | |||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | print STDERR "SQL FAILED:\t$s\n\t$@\n" if $@; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||
| 118 | # Borrowed from https://metacpan.org/release/Dist-Zilla | ||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub prompt_str { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
prompt_str: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my ( $self, $prompt, $arg ) = @_; | |||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | $arg ||= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$arg ||= {}
MC/DC0%0/1
$arg ||= {}
$arg
Truth table0%0/2
A
A= $arg
| |||||||||||||||||||||||||||||||||||||||
| 123 | 0 | my $default = $arg->{default}; | |||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $check = $arg->{check}; | |||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | 0 | require Encode; | |||||||||||||||||||||||||||||||||||||
| 127 | 0 | my $term_encoding = $self->term_encoding; | |||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||
| 129 | my $encode | ||||||||||||||||||||||||||||||||||||||
| 130 | = $term_encoding | ||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | ? sub { Encode::encode( $term_encoding, shift, Encode::FB_CROAK() ) } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | : sub {shift}; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 133 | my $decode | ||||||||||||||||||||||||||||||||||||||
| 134 | = $term_encoding | ||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | ? sub { Encode::decode( $term_encoding, shift, Encode::FB_CROAK() ) } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | : sub {shift}; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | my $input_bytes = $self->term->get_reply( | ||||||||||||||||||||||||||||||||||||||
| 139 | prompt => $encode->($prompt), | ||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | allow => $check || sub { defined $_[0] and length $_[0] }, | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | ( defined $default | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$check || sub {
defined $_[0] and length $_[0];
}
MC/DC0%0/2
$check || sub {
defined $_[0] and length $_[0];
}
$check
sub {
defined $_[0] and length $_[0];
}
Truth table0%0/3
A || B
A= $checkB= sub {
defined $_[0] and length $_[0];
}
| |||||||||||||||||||||||||||||||||||||||
| 142 | ? ( default => $encode->($default) ) | ||||||||||||||||||||||||||||||||||||||
| 143 | : () | ||||||||||||||||||||||||||||||||||||||
| 144 | ), | ||||||||||||||||||||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my $input = $decode->($input_bytes); | |||||||||||||||||||||||||||||||||||||
| 148 | 0 | chomp $input; | |||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||
| 150 | 0 | return $input; | |||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub prompt_yn { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
prompt_yn: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my ( $self, $prompt, $arg ) = @_; | |||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | $arg ||= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$arg ||= {}
MC/DC0%0/1
$arg ||= {}
$arg
Truth table0%0/2
A
A= $arg
| |||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my $default = $arg->{default}; | |||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my $input = $self->term->ask_yn( | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 159 | prompt => $prompt, | ||||||||||||||||||||||||||||||||||||||
| 160 | ( defined $default ? ( default => $default ) : () ), | ||||||||||||||||||||||||||||||||||||||
| 161 | ); | ||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||
| 163 | 0 | return $input; | |||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||
| 166 | # WEB | ||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | sub ua { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
ua: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 169 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | $self->{ua} = LWP::UserAgent->new() unless $self->{ua}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 171 | 0 | return $self->{ua}; | |||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | sub login { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
login: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my ($self,$ua,$auth) = @_; | |||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | my $url = URI::WithBase->new('/',$auth->{baseurl}||$self->config->{web_api}->{url}); | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$auth->{'baseurl'} || $self->config->{'web_api'}{'url'}
MC/DC0%0/2
$auth->{'baseurl'} || $self->config->{'web_api'}{'url'}
$auth->{'baseurl'}
$self->config->{'web_api'}{'url'}
Truth table0%0/3
A || B
A= $auth->{'baseurl'}B= $self->config->{'web_api'}{'url'}
| |||||||||||||||||||||||||||||||||||||||
| 177 | 0 | $url->path_segments('api','auth'); | |||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | my $res = $self->request($ua,'POST',$url->abs->as_string,{auth => {password => $auth->{password} || $self->config->{web_api}->{password}, username => $auth->{username} || $self->config->{web_api}->{user}}}); | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$auth->{'password'} || $self->config->{'web_api'}{'password'}
$auth->{'username'} || $self->config->{'web_api'}{'user'}
MC/DC0%0/2
$auth->{'password'} || $self->config->{'web_api'}{'password'}
$auth->{'password'}
$self->config->{'web_api'}{'password'}
MC/DC0%0/2
$auth->{'username'} || $self->config->{'web_api'}{'user'}
$auth->{'username'}
$self->config->{'web_api'}{'user'}
Truth table0%0/3
A || B
A= $auth->{'password'}B= $self->config->{'web_api'}{'password'}
Truth table0%0/3
A || B
A= $auth->{'username'}B= $self->config->{'web_api'}{'user'}
| |||||||||||||||||||||||||||||||||||||||
| 180 | 0 | my $cookie_jar = HTTP::Cookies->new(); | |||||||||||||||||||||||||||||||||||||
| 181 | 0 | $cookie_jar->extract_cookies($res); | |||||||||||||||||||||||||||||||||||||
| 182 | 0 | $ua->cookie_jar($cookie_jar); | |||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
request: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my ($self,$ua,$method, $url,$data) = @_; | |||||||||||||||||||||||||||||||||||||
| 187 | 0 | my $JSON = JSON->new->utf8; | |||||||||||||||||||||||||||||||||||||
| 188 | 0 | my $req = HTTP::Request->new( $method => $url ); | |||||||||||||||||||||||||||||||||||||
| 189 | 0 | $req->content_type('application/json;charset=UTF-8'); | |||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | if($data) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 191 | 0 | $data = $JSON->encode($data); | |||||||||||||||||||||||||||||||||||||
| 192 | 0 | $data =~ s/"false"/false/g; | |||||||||||||||||||||||||||||||||||||
| 193 | 0 | $data =~ s/"true"/true/g; | |||||||||||||||||||||||||||||||||||||
| 194 | 0 | $req->content($data); | |||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my $res = eval { $ua->request( $req ); }; | |||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | confess($@) if $@; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 198 | 0 | ▶ | unless ( $res->is_success ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if($res->code() == 502) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 200 | 0 | die "Error while executing query.\n"; | |||||||||||||||||||||||||||||||||||||
| 201 | } else { | ||||||||||||||||||||||||||||||||||||||
| 202 | 0 | die "Error reported by PML-TQ server:\n\n" . $res->content . "\n"; | |||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||
| 204 | 0 | return; | |||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | if(wantarray) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | return ($res,$res->decoded_content) unless $res->content_type eq 'application/json'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 208 | 0 | my $json = $res->decoded_content; | |||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | return ($res,$json ? $JSON->decode($json) : undef); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||
| 211 | 0 | return $res; | |||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | sub get_all_treebanks { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_all_treebanks: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 215 | 0 | my ($self,$ua, $apiurl) = @_; | |||||||||||||||||||||||||||||||||||||
| 216 | 0 | my $data; | |||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | my $url = URI::WithBase->new('/',$apiurl || $self->config->{web_api}->{url}); | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$apiurl || $self->config->{'web_api'}{'url'}
MC/DC0%0/2
$apiurl || $self->config->{'web_api'}{'url'}
$apiurl
$self->config->{'web_api'}{'url'}
Truth table0%0/3
A || B
A= $apiurlB= $self->config->{'web_api'}{'url'}
| |||||||||||||||||||||||||||||||||||||||
| 218 | 0 | $url->path_segments('api','admin', 'treebanks'); | |||||||||||||||||||||||||||||||||||||
| 219 | 0 | (undef,$data) = $self->request($ua,'GET',$url->abs->as_string); | |||||||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | return $data // []; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$data // []
MC/DC0%0/1
$data // []
$data
Truth table0%0/2
A
A= $data
| |||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | sub get_treebank { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_treebank: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 224 | 0 | my ($self,$ua) = @_; | |||||||||||||||||||||||||||||||||||||
| 225 | 0 | my ($treebank) = grep {$_->{name} eq $self->config->{treebank_id}} @{ $self->get_all_treebanks($ua)}; | |||||||||||||||||||||||||||||||||||||
| 226 | 0 | return $treebank; | |||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | sub request_treebank { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
request_treebank: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my ($self,$treebank,$ua,$method,$data) = @_; | |||||||||||||||||||||||||||||||||||||
| 231 | 0 | my $url = URI::WithBase->new('/',$self->config->{web_api}->{url}); | |||||||||||||||||||||||||||||||||||||
| 232 | 0 | $url->path_segments('api','admin', 'treebanks',$treebank->{id}); | |||||||||||||||||||||||||||||||||||||
| 233 | 0 | (undef,$data) = $self->request($ua,$method,$url->abs->as_string,$data); | |||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | sub create_treebank_param { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
create_treebank_param: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 237 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 238 | 0 | my (@langs,@tags,@server); | |||||||||||||||||||||||||||||||||||||
| 239 | 0 | my @searches = ( | |||||||||||||||||||||||||||||||||||||
| 240 | { | ||||||||||||||||||||||||||||||||||||||
| 241 | results => \@langs, | ||||||||||||||||||||||||||||||||||||||
| 242 | configpath => ['languages'], | ||||||||||||||||||||||||||||||||||||||
| 243 | api => 'languages', | ||||||||||||||||||||||||||||||||||||||
| 244 | compare => 'code', | ||||||||||||||||||||||||||||||||||||||
| 245 | error => "Unknown language code '\%s'\n", | ||||||||||||||||||||||||||||||||||||||
| 246 | min => 0 | ||||||||||||||||||||||||||||||||||||||
| 247 | }, | ||||||||||||||||||||||||||||||||||||||
| 248 | { | ||||||||||||||||||||||||||||||||||||||
| 249 | results => \@tags, | ||||||||||||||||||||||||||||||||||||||
| 250 | configpath => ['tags'], | ||||||||||||||||||||||||||||||||||||||
| 251 | api => 'tags', | ||||||||||||||||||||||||||||||||||||||
| 252 | compare => 'name', | ||||||||||||||||||||||||||||||||||||||
| 253 | error => "Unknown tag '\%s'\n", | ||||||||||||||||||||||||||||||||||||||
| 254 | min => 0 | ||||||||||||||||||||||||||||||||||||||
| 255 | }, | ||||||||||||||||||||||||||||||||||||||
| 256 | { | ||||||||||||||||||||||||||||||||||||||
| 257 | results => \@server, | ||||||||||||||||||||||||||||||||||||||
| 258 | configpath => ['web_api','dbserver'], | ||||||||||||||||||||||||||||||||||||||
| 259 | api => 'servers', | ||||||||||||||||||||||||||||||||||||||
| 260 | compare => 'name', | ||||||||||||||||||||||||||||||||||||||
| 261 | no_url_filter => 1, | ||||||||||||||||||||||||||||||||||||||
| 262 | error => "Unknown server name '\%s'\n", | ||||||||||||||||||||||||||||||||||||||
| 263 | min => 1 | ||||||||||||||||||||||||||||||||||||||
| 264 | }, | ||||||||||||||||||||||||||||||||||||||
| 265 | ); | ||||||||||||||||||||||||||||||||||||||
| 266 | 0 | for my $search (@searches) { | |||||||||||||||||||||||||||||||||||||
| 267 | 0 | my $config_values = $self->config; | |||||||||||||||||||||||||||||||||||||
| 268 | 0 | for my $path (@{$search->{configpath}}) { | |||||||||||||||||||||||||||||||||||||
| 269 | 0 | $config_values = $config_values->{$path}; | |||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | $config_values = $config_values ? (ref $config_values ? $config_values : [$config_values] ) : []; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 272 | 0 | for my $text (@{$config_values}) { | |||||||||||||||||||||||||||||||||||||
| 273 | 0 | my $res = $self->_search_param($text,$search); | |||||||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | if($res) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 275 | 0 | push @{$search->{results}}, $res->{id}; | |||||||||||||||||||||||||||||||||||||
| 276 | } else { | ||||||||||||||||||||||||||||||||||||||
| 277 | 0 | die "ERROR: " . sprintf($search->{error},$text); | |||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | die "ERROR: " . $search->{min} . " " . join("-",@{$search->{configpath}}) . " is required\n" unless @{$search->{results}} >= $search->{min} ; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||
| 283 | return { | ||||||||||||||||||||||||||||||||||||||
| 284 | title => $self->config->{title}, | ||||||||||||||||||||||||||||||||||||||
| 285 | name => $self->config->{treebank_id}, | ||||||||||||||||||||||||||||||||||||||
| 286 | homepage => $self->config->{homepage}, | ||||||||||||||||||||||||||||||||||||||
| 287 | description => $self->config->{description}, | ||||||||||||||||||||||||||||||||||||||
| 288 | manuals => $self->config->{manuals}, | ||||||||||||||||||||||||||||||||||||||
| 289 | 0 | dataSources => [map { {layer => $_->{name},path => $_->{path} } }@{$self->config->{layers}}], | |||||||||||||||||||||||||||||||||||||
| 290 | tags => \@tags, | ||||||||||||||||||||||||||||||||||||||
| 291 | languages => \@langs, | ||||||||||||||||||||||||||||||||||||||
| 292 | serverId => $server[0], | ||||||||||||||||||||||||||||||||||||||
| 293 | database => $self->config->{db}->{name}, | ||||||||||||||||||||||||||||||||||||||
| 294 | isFree => $self->config->{isFree}, | ||||||||||||||||||||||||||||||||||||||
| 295 | isAllLogged => $self->config->{isAllLogged}, | ||||||||||||||||||||||||||||||||||||||
| 296 | isPublic => $self->config->{isPublic}, | ||||||||||||||||||||||||||||||||||||||
| 297 | isFeatured => $self->config->{isFeatured}, | ||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||
| 299 | 0 | } | |||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | sub _search_param { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_search_param: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 302 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 303 | 0 | my $text = shift; | |||||||||||||||||||||||||||||||||||||
| 304 | 0 | my $opts = shift; | |||||||||||||||||||||||||||||||||||||
| 305 | 0 | my $url = URI::WithBase->new('/',$self->config->{web_api}->{url}); | |||||||||||||||||||||||||||||||||||||
| 306 | 0 | $url->path_segments('api', 'admin', $opts->{api}); | |||||||||||||||||||||||||||||||||||||
| 307 | 0 | my $data; | |||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | (undef,$data) = $self->request($self->{ua}, 'GET', $url->abs->as_string.($opts->{no_url_filter} ? '' : "?filter=".URI::Encode::uri_encode("{\"q\":\"$text\"}")), {}); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 309 | 0 | my ($res) = grep {$_->{$opts->{compare}} eq $text } @$data; | |||||||||||||||||||||||||||||||||||||
| 310 | 0 | return $res; | |||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | sub evaluate_query { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate_query: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 315 | 0 | my ($self,$tb_id,$query) = @_; | |||||||||||||||||||||||||||||||||||||
| 316 | 0 | my $url = URI::WithBase->new('/',$self->config->{web_api}->{url}); | |||||||||||||||||||||||||||||||||||||
| 317 | 0 | $url->path_segments('api', 'treebanks', $tb_id, 'query'); | |||||||||||||||||||||||||||||||||||||
| 318 | 0 | my $data; | |||||||||||||||||||||||||||||||||||||
| 319 | 0 | (undef,$data) = $self->request($self->{ua}, 'POST', $url->abs->as_string, {filter => "true", limit => 100, query => $query, timeout => 30}); | |||||||||||||||||||||||||||||||||||||
| 320 | 0 | my $result = ''; | |||||||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | unless($data) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 322 | 0 | print STDERR "Error while executing query: $query\n"; | |||||||||||||||||||||||||||||||||||||
| 323 | } else { | ||||||||||||||||||||||||||||||||||||||
| 324 | 0 | my $results = $data->{results}; | |||||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | if(@$results) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | if($data->{nodes}) { # tree result | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 327 | 0 | $url = URI::WithBase->new('/',$self->config->{web_api}->{url}); | |||||||||||||||||||||||||||||||||||||
| 328 | 0 | $url->path_segments('api', 'treebanks', $tb_id, 'svg'); | |||||||||||||||||||||||||||||||||||||
| 329 | 0 | (undef,$result) = $self->request($self->{ua}, 'POST', $url->abs->as_string, {nodes => $results->[0], tree => 0}); | |||||||||||||||||||||||||||||||||||||
| 330 | } else { # filter result | ||||||||||||||||||||||||||||||||||||||
| 331 | 0 | $result = join("\n",map {join("\t",@$_)} @$results) . "\n"; | |||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||
| 333 | } else { | ||||||||||||||||||||||||||||||||||||||
| 334 | 0 | print STDERR "Empty result for: $query\n"; | |||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||
| 337 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | sub user2admin_format { # converts getted treebank json to treebank configuration format | ||||||||||||||||||||||||||||||||||||
|
Subroutine
user2admin_format: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 341 | 0 | my ($self, $treebank) = @_; | |||||||||||||||||||||||||||||||||||||
| 342 | return { | ||||||||||||||||||||||||||||||||||||||
| 343 | id => $treebank->{name}, | ||||||||||||||||||||||||||||||||||||||
| 344 | 0 | tags => [map {$_->{name}} @{$treebank->{tags}}], | |||||||||||||||||||||||||||||||||||||
| 345 | language => $treebank->{languages}->[0]->{code}, | ||||||||||||||||||||||||||||||||||||||
| 346 | 0 | map {$_ => $treebank->{$_}} qw/title isFree isAllLogged isFeatured isPublic homepage description documentation dataSources manuals/ | |||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||
| 351 | 0 | ▶ | sub get_nodetypes { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_nodetypes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 352 | 0 | my ($self, $treebank) = @_; | |||||||||||||||||||||||||||||||||||||
| 353 | 0 | my $url = URI::WithBase->new('/',$self->config->{web_api}->{url}); | |||||||||||||||||||||||||||||||||||||
| 354 | 0 | $url->path_segments('api', 'treebanks', $treebank->{name}, 'node-types'); | |||||||||||||||||||||||||||||||||||||
| 355 | 0 | my $data; | |||||||||||||||||||||||||||||||||||||
| 356 | 0 | (undef,$data) = $self->request($self->{ua}, 'GET', $url->abs->as_string); | |||||||||||||||||||||||||||||||||||||
| 357 | return $data->{types} | ||||||||||||||||||||||||||||||||||||||
| 358 | 0 | } | |||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | sub get_test_queries { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_test_queries: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 361 | 0 | my ($self, $treebank) = @_; | |||||||||||||||||||||||||||||||||||||
| 362 | # get node types | ||||||||||||||||||||||||||||||||||||||
| 363 | 0 | my ($type) = @{$self->get_nodetypes($treebank)}; | |||||||||||||||||||||||||||||||||||||
| 364 | 0 | return [{filename=>"$type.svg", query => "$type [];"}, {filename=>"${type}_count.txt", query => "$type []; >> count()"}] | |||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||
| 367 | 14 | 1; | |||||||||||||||||||||||||||||||||||||