lib/Serge/Engine/Plugin/parse_json.pm

filter: Statementstmt 17.8% 22 / 123 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 46.6% 7 / 15 Podpod 0.0% 0 / 7 totaltotal 13.4% 29 / 216 SCAR 68.9
CC
37
Cov
12%
process_node
272.0
report_errors
156.0
parse
42.0
CRAP 981.4
« lib/Serge/Engine/Plugin/completeness.pm ↑ Summary lib/Serge/Engine/Plugin/control_commands.pm »
1 package Serge::Engine::Plugin::parse_json;
2 1 ▶use parent Serge::Engine::Plugin::Base::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use parent Serge::Interface::PluginHost;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON -support_by_pp; # -support_by_pp is used to make Perl on Mac happy
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Serge::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Serge::Util qw(xml_escape_strref);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub name {
13 0 ▶ return 'Generic JSON tree parser plugin';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
14 }
15  
16 sub init {
17 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
18  
19 0 $self->SUPER::init(@_);
20  
21 0 $self->{errors} = {};
22  
23 0 $self->merge_schema({
24 path_matches => 'ARRAY',
25 path_doesnt_match => 'ARRAY',
26 path_html => 'ARRAY',
27  
28 email_from => 'STRING',
29 email_to => 'ARRAY',
30 email_subject => 'STRING',
31  
32 html_parser => {
33 plugin => 'STRING',
34  
35 data => {
36 '*' => 'DATA',
37 }
38 },
39 });
40  
41 0 $self->add('after_job', \&report_errors);
42 }
43  
44 sub report_errors {
45 0 ▶ my ($self, $phase) = @_;
Subroutine report_errors: not called CC=12 SCAR=50.5
Pod undocumented
46  
47 # copy over errors from the child parser, if any
48 0 ▶ if ($self->{html_parser}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"html_parser"}) 0 0 0
49 0 my @keys = keys %{$self->{html_parser}->{errors}};
50 0 ▶ if (scalar @keys > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys > 0) 0 0 0
51 map {
52 0 $self->{errors}->{$_} = $self->{html_parser}->{errors}->{$_};
53 } @keys;
54 0 $self->{html_parser}->{errors} = {};
55 }
56 }
57  
58 0 ▶ return if !scalar keys %{$self->{errors}};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$$self{"errors"};} 0 0 0
59  
60 0 my $email_from = $self->{data}->{email_from};
61 0 my $email_to = $self->{data}->{email_to};
62  
63 0 ▶ if (!$email_from || !$email_to) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email_from and $email_to) 0 0 0
Condition0%0/3
$email_from and $email_to
!ll&&!rl&&r
000
MC/DC0%0/2
$email_from and $email_to
$email_from $email_to
Truth table0%0/3
A and B
A= $email_fromB= $email_to
ABresult
0X0
100
111
64 0 my @a;
65 0 ▶ push @a, "'email_from'" unless $email_from;
Branch: 1 branch
BranchTrueFalseTotal
unless $email_from 0 0 0
66 0 ▶ push @a, "'email_to'" unless $email_to;
Branch: 1 branch
BranchTrueFalseTotal
unless $email_to 0 0 0
67 0 my $fields = join(' and ', @a);
68 0 ▶ my $are = scalar @a > 1 ? 'are' : 'is';
Branch: 1 branch
BranchTrueFalseTotal
scalar @a > 1 ? : 0 0 0
69 0 print "WARNING: there are some parsing errors, but $fields $are not defined, so can't send an email.\n";
70 0 $self->{errors} = {};
71 0 return;
72 }
73  
74 0 ▶ my $email_subject = $self->{data}->{email_subject} || ("[".$self->{parent}->{id}.']: JSON Parse Errors');
Condition0%0/2
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: JSON Parse Errors'
l!l
00
MC/DC0%0/1
$self->{'data'}{'email_subject'} || '[' . $self->{'parent'}{'id'} . ']: JSON Parse Errors'
$self->{'data'}{'email_subject'}
Truth table0%0/2
A
A= $self->{'data'}{'email_subject'}
Aresult
11
00
75  
76 0 my $text;
77 0 foreach my $key (sort keys %{$self->{errors}}) {
78 0 my $pre_contents = $self->{errors}->{$key};
79 0 xml_escape_strref(\$pre_contents);
80 0 $text .= "<hr />\n<p><b style='color: red'>$key</b> <pre>".$pre_contents."</pre></p>\n";
81 }
82  
83 0 $self->{errors} = {};
84  
85 0 ▶ if ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 0 0 0
86 0 $text = qq|
87 <html>
88 <head>
89 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
90 </head>
91 <body style="font-family: sans-serif; font-size: 120%">
92  
93 <p>
94 # This is an automatically generated message.
95  
96 The following parsing errors were found when attempting to localize resource files.
97 </p>
98  
99 $text
100  
101 </body>
102 </html>
103 |;
104  
105 0 Serge::Mail::send_html_message(
106 $email_from, # from
107 $email_to, # to (list)
108 $email_subject, # subject
109 $text # message body
110 );
111 }
112  
113 }
114  
115 sub parse {
116 0 ▶ my ($self, $textref, $callbackref, $lang) = @_;
Subroutine parse: not called CC=6 SCAR=37.4
Pod undocumented
117  
118 0 ▶ die 'callbackref not specified' unless $callbackref;
Branch: 1 branch
BranchTrueFalseTotal
unless $callbackref 0 0 0
119  
120 # Make a copy of the string as we will change it
121  
122 0 my $text = $$textref;
123  
124 # Parse JSON
125  
126 0 my $tree;
127 0 eval {
128 0 ($tree) = from_json($text, {relaxed => 1});
129 };
130 0 ▶ if ($@ || !$tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $tree) 0 0 0
Condition0%0/3
$@ or not $tree
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $tree
$@ not $tree
Truth table0%0/3
A or not(B)
A= $@B= not $tree
ABresult
1X1
011
000
131 0 my $error_text = $@;
132 0 ▶ if ($error_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_text) { } 0 0 0
133 0 $error_text =~ s/\t/ /g;
134 0 $error_text =~ s/^\s+//s;
135 } else {
136 0 $error_text = "from_json() returned empty data structure";
137 }
138  
139 0 $self->{errors}->{$self->{parent}->{engine}->{current_file_rel}} = $error_text;
140  
141 0 die $error_text;
142 }
143  
144 # Process tree recursively
145  
146 0 $self->process_node('', $tree, $callbackref, $lang);
147  
148 # Reconstruct JSON
149  
150 # need to force indent_length, otherwise it will be zero when PP extension 'escape_slash' is used
151 # (looks like an issue in some newer version of JSON:PP)
152 # also, force 'canonical' to sort keys alphabetically to ensure the structure won't be changed on subsequent script runs
153 0 ▶ return $lang ? to_json($tree, {pretty => 1, indent_length => 3, canonical => 1, escape_slash => 1}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$lang ? : 0 0 0
154 }
155  
156 sub process_node {
157 0 ▶ my ($self, $path, $subtree, $callbackref, $lang, $parent, $key, $index) = @_;
Subroutine process_node: not called CC=16 SCAR=56.1
Pod undocumented
158  
159 0 ▶ if (ref($subtree) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $subtree eq 'HASH') { } 0 0 0
elsif (ref $subtree eq 'ARRAY') { } 0 0 0
160 # hash
161  
162 0 foreach my $key (sort keys %$subtree) {
163 0 $self->process_node($path.'/'.$key, $subtree->{$key}, $callbackref, $lang, $subtree, $key);
164 }
165 } elsif (ref($subtree) eq 'ARRAY') {
166 # array
167  
168 0 my $i = 0;
169 0 foreach my $item (@$subtree) {
170 # translate only non-empty strings
171 0 $self->process_node($path.'['.$i.']', $item, $callbackref, $lang, $subtree, undef, $i);
172 0 $i++;
173 }
174  
175 } else {
176 # text
177 0 ▶ return unless $self->check_path($path);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_path($path) 0 0 0
178  
179 0 ▶ print "Text node '$path' matches the rules\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
180  
181 0 my $string = $subtree;
182  
183 # trim the string
184 0 my $trimmed = $string;
185 0 $trimmed =~ s/^\s+//sg;
186 0 $trimmed =~ s/\s+$//sg;
187  
188 # translate only non-empty (and non-whitespace) strings
189 0 ▶ if ($trimmed eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($trimmed eq "") 0 0 0
190 0 return;
191 }
192  
193 0 ▶ if ($self->is_html($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_html($path)) { } 0 0 0
194 # if node is html, pass its text to html parser for string extraction;
195 # if html_parser fails to parse the XML due to errors,
196 # it will die(), and this will be catched in main application
197  
198 # lazy-load html parser plugin
199 # (parse_php_xhtml or the one specified in html_parser config node)
200 0 ▶ if (!$self->{html_parser}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"html_parser"}) 0 0 0
201 0 ▶ if (exists $self->{data}->{html_parser}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'data'}{'html_parser'}) { } 0 0 0
202 $self->{html_parser} = $self->load_plugin_from_node(
203 'Serge::Engine::Plugin', $self->{data}->{html_parser}
204 0 );
205 } else {
206 # fallback to loading parse_php_xhtml with default parameters
207 0 eval('use Serge::Engine::Plugin::parse_php_xhtml; $self->{html_parser} = Serge::Engine::Plugin::parse_php_xhtml->new($self->{parent});');
208 0 ▶ ($@) && die "Can't load parser plugin 'parse_php_xhtml': $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "Can't load parser plugin 'parse_php_xhtml': $@" 0 0 0
209 0 ▶ print "Loaded HTML parser plugin for HTML nodes\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
210 }
211 }
212  
213 0 $self->{html_parser}->{current_file_rel} = $self->{parent}->{engine}->{current_file_rel}.":$path";
214 0 ▶ if ($lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang) { } 0 0 0
215 0 $string = $self->{html_parser}->parse(\$string, $callbackref, $lang);
216 } else {
217 0 $self->{html_parser}->parse(\$string, $callbackref);
218 return
219 0 }
220 } else {
221 # plain-text content
222 0 ▶ if ($lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang) { } 0 0 0
223 0 $string = &$callbackref($string, undef, $path, undef, $lang, $path);
224 } else {
225 0 &$callbackref($string, undef, $path, undef, undef, $path);
226 return
227 0 }
228 }
229  
230 0 ▶ if (defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index) { } 0 0 0
231 0 $parent->[$index] = $string;
232 } else {
233 0 $parent->{$key} = $string;
234 }
235 }
236 }
237  
238 sub _check_ruleset {
239 0 ▶ my ($ruleset, $positive, $value, $default) = @_;
Subroutine _check_ruleset: not called CC=4 SCAR=30.0
240  
241 0 ▶ return $default unless defined $ruleset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ruleset 0 0 0
242  
243 0 foreach my $rule (@$ruleset) {
244 0 ▶ if ($value =~ m/$rule/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$rule/s) 0 0 0
245 0 return $positive;
246 }
247 }
248 0 return !$positive;
249 }
250  
251 sub check_path {
252 0 ▶ my ($self, $path) = @_;
Subroutine check_path: not called CC=3 SCAR=24.8
Pod undocumented
253 0 ▶ return 0 unless _check_ruleset($self->{data}->{path_matches}, 1, $path, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_ruleset($self->{"data"}{"path_matches"}, 1, $path, 1) 0 0 0
254 0 ▶ return 0 unless _check_ruleset($self->{data}->{path_doesnt_match}, undef, $path, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_ruleset($self->{"data"}{"path_doesnt_match"}, undef, $path, 1) 0 0 0
255 0 return 1;
256 }
257  
258 sub is_html {
259 0 ▶ my ($self, $path) = @_;
Subroutine is_html: not called CC=1 SCAR=6.9
Pod undocumented
260 0 return _check_ruleset($self->{data}->{path_html}, 1, $path, undef);
261 }
262  
263 1 1;
« lib/Serge/Engine/Plugin/completeness.pm ↑ Summary lib/Serge/Engine/Plugin/control_commands.pm »