lib/Serge/Engine.pm

filter: Statementstmt 68.1% 606 / 889 Branchbran 32.8% 159 / 484 Conditioncond 24.4% 43 / 176 MC/DCmcdc 2.0% 2 / 100 Subroutinesub 98.5% 70 / 71 Podpod 0.0% 0 / 40 totaltotal 50.0% 880 / 1760 SCAR 93.8
CC
324
Cov
52%
update_database_from_ts_files_lang_file
559.9
update_database_from_source_files
377.4
generate_localized_files_for_file_lang
304.7
CRAP 11815.9
↑ Summary lib/Serge/Engine/Plugin/limit_languages.pm »
1 package Serge::Engine;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Digest::MD5 qw(md5 md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Encode qw(decode encode_utf8 decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Unicode::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Serge;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Serge::DB::Cached;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Serge::FindFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Serge::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #
23 # Initialize object
24 #
25 sub new {
26 7 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
27  
28 my $self = {
29 7 debug => $ENV{CI} ne '', # use debug mode from under CI environment to ensure better coverage
30 debug_nosave_ts => undef, # disable generation of translation files
31 debug_nosave_loc => undef, # disable generation of localized files
32  
33 optimizations => 1, # set to undef to disable optimizations and force generate all files
34  
35 # job defaults (can be overridden in a job config)
36 active => 1,
37 output_lang_files => 1,
38 output_encoding => 'UTF-8',
39 output_bom => 1,
40 reuse_translations => 1,
41 reuse_orphaned => 1,
42 reuse_fuzzy => 1,
43 reuse_as_fuzzy_default => 1,
44 reuse_as_fuzzy => [],
45 reuse_as_not_fuzzy => [],
46 similar_languages => [], # list of rules of how to copy translations between similar languages
47 # end of job defaults
48  
49 rebuild_ts_files => undef, # disable updates from translation files and force recreate them, in order to push changes from db
50 output_only_mode => undef, # force output_only_mode on all jobs
51  
52 db => Serge::DB::Cached->new(),
53 callback_phases => {},
54 job => undef, # holds current job object
55 current_file_rel => undef, # holds relative path to the currently processed file
56 current_file_id => undef, # holds id of current namespace/path record in [files] table
57 current_file_keys => undef, # holds the hash of currently processed keys (strings/contexts)
58  
59 # if set to a list reference, will limit the set of languages to process
60 limit_destination_languages => undef,
61  
62 # if set to a list reference, will limit the set of languages to process
63 limit_destination_jobs => undef,
64  
65 modified_languages => undef,
66  
67 files => {},
68 force_flags => {},
69 skip_flags => {},
70 file_mappings => {},
71 found_files => {},
72  
73 };
74  
75 7 bless $self, $class;
76 7 return $self;
77 }
78  
79 sub run_callbacks {
80 860 ▶ my ($self, $phase, @params) = @_;
Subroutine run_callbacks: called CC=1 SCAR=0.0
Pod undocumented
81  
82 860 return $self->{job}->run_callbacks($phase, @params);
83 }
84  
85 sub adjust_destination_languages {
86 6 ▶ my ($self, $job) = @_;
Subroutine adjust_destination_languages: called CC=7 SCAR=27.1
Pod undocumented
87  
88 # add source language to the list if `output_default_lang_file` is defined
89 6 ▶ if ($job->{output_default_lang_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{"output_default_lang_file"}) 0 6 6
90 0 push @{$job->{destination_languages}}, $job->{source_language};
91 }
92  
93 # deduplicate the list
94 6 my @j = @{$job->{destination_languages}};
95 6 my %job_langs;
96 6 @job_langs{@j} = @j;
97  
98 6 ▶ if (defined $self->{limit_destination_languages}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"limit_destination_languages"}) 0 6 6
99 0 my @l = @{$self->{limit_destination_languages}};
100 0 ▶ print "List of destination languages limited to: ".join(',', sort @l)."\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
101  
102 0 my %limit_langs;
103 0 @limit_langs{@l} = @l;
104  
105 # delete all languages from job_langs hash if they do not exist in limit_langs
106 0 foreach my $lang (keys %job_langs) {
107 0 ▶ delete $job_langs{$lang} unless exists $limit_langs{$lang};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $limit_langs{$lang} 0 0 0
108 }
109 }
110  
111 # update the list with a deduplicated sorted version of it
112 6 my @a = sort keys %job_langs;
113 6 $job->{destination_languages} = \@a;
114  
115 6 ▶ if (@{$job->{destination_languages}} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$job->{"destination_languages"};} == 0) 0 6 6
116 0 print "List of destination languages is empty\n";
117 }
118 }
119  
120 sub job_can_run {
121 6 ▶ my ($self, $job) = @_;
Subroutine job_can_run: called CC=6 SCAR=26.9
Pod undocumented
122  
123 6 ▶ if (!$self->{job}->{active}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"job"}{"active"}) 0 6 6
124 0 return undef;
125 }
126  
127 6 ▶ if (defined $self->{limit_destination_jobs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"limit_destination_jobs"}) 0 6 6
128 0 my @l = @{$self->{limit_destination_jobs}};
129 0 ▶ print "List of destination jobs limited to: ".join(',', sort @l)."\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
130  
131 0 my %limit_jobs;
132 0 @limit_jobs{@l} = @l;
133  
134 0 ▶ if (!exists $limit_jobs{$self->{job}->{id}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $limit_jobs{$self->{"job"}{"id"}}) 0 0 0
135 0 return undef;
136 }
137 }
138  
139 6 ▶ return undef if @{$job->{destination_languages}} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$job->{"destination_languages"};} == 0 0 6 6
140  
141 6 return 1;
142 }
143  
144 sub open_database {
145 6 ▶ my ($self, $job) = @_;
Subroutine open_database: called CC=5 SCAR=18.7
Pod undocumented
146  
147 # reuse the previous DB connection if its parameters weren't changed
148  
149 return if
150 ($self->{db_source} ne '') &&
151 ($self->{db_source} eq $job->{db_source}) &&
152 ($self->{db_username} eq $job->{db_username}) &&
153 6 ▶ ($self->{db_password} eq $job->{db_password});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"db_source"} ne "" and $self->{"db_source"} eq $job->{"db_source"} and $self->{"db_username"} eq $job->{"db_username"} and $self->{"db_password"} eq $job->{"db_password"} 0 6 6
Condition33%3/9
$self->{"db_source"} ne "" and $self->{"db_source"} eq $job->{"db_source"} and $self->{"db_username"} eq $job->{"db_username"} and $self->{"db_password"} eq $job->{"db_password"}
!ll&&!rl&&r
600
$self->{"db_source"} ne "" and $self->{"db_source"} eq $job->{"db_source"} and $self->{"db_username"} eq $job->{"db_username"}
!ll&&!rl&&r
600
$self->{"db_source"} ne "" and $self->{"db_source"} eq $job->{"db_source"}
!ll&&!rl&&r
600
MC/DC0%0/4
$self->{"db_source"} ne "" and $self->{"db_source"} eq $job->{"db_source"} and $self->{"db_username"} eq $job->{"db_username"} and $self->{"db_password"} eq $job->{"db_password"}
$self->{"db_source"} ne "" $self->{"db_source"} eq $job->{"db_source"} $self->{"db_username"} eq $job->{"db_username"} $self->{"db_password"} eq $job->{"db_password"}
Truth table20%1/5
A and B and C and D
A= $self->{"db_source"} ne ""B= $self->{"db_source"} eq $job->{"db_source"}C= $self->{"db_username"} eq $job->{"db_username"}D= $self->{"db_password"} eq $job->{"db_password"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
154  
155 # open the database (this will also close previous connection and commit the transaction, if any)
156 6 $self->{db}->open($job->{db_source}, $job->{db_username}, $job->{db_password});
157  
158 6 $self->{db_source} = $job->{db_source};
159 6 $self->{db_username} = $job->{db_username};
160 6 $self->{db_password} = $job->{db_password};
161  
162 # preload all properties into cache
163  
164 6 $self->{db}->preload_properties;
165 }
166  
167 sub get_job_hash_key {
168 12 ▶ my ($self, $job) = @_;
Subroutine get_job_hash_key: called CC=1 SCAR=0.0
Pod undocumented
169 12 return "job-hash:".$job->{db_namespace}.":".$job->{id};
170 }
171  
172 sub get_job_plugin_version_key {
173 6 ▶ my ($self, $job) = @_;
Subroutine get_job_plugin_version_key: called CC=1 SCAR=0.0
Pod undocumented
174 6 return "job-plugin:".$job->{db_namespace}.":".$job->{id};
175 }
176  
177 sub get_job_serializer_plugin_version_key {
178 6 ▶ my ($self, $job) = @_;
Subroutine get_job_serializer_plugin_version_key: called CC=1 SCAR=0.0
Pod undocumented
179 6 return "job-serializer-plugin:".$job->{db_namespace}.":".$job->{id};
180 }
181  
182 sub get_job_engine_version_key {
183 6 ▶ my ($self, $job) = @_;
Subroutine get_job_engine_version_key: called CC=1 SCAR=0.0
Pod undocumented
184 6 return "job-engine:".$job->{db_namespace}.":".$job->{id};
185 }
186  
187 sub adjust_job_optimizations {
188 6 ▶ my ($self, $job) = @_;
Subroutine adjust_job_optimizations: called CC=10 SCAR=41.6
Pod undocumented
189  
190 # Check if optimizations can be applied
191  
192 6 my $opt = $self->{optimizations};
193  
194 6 ▶ if ($job->{hash} ne $self->{db}->get_property($self->get_job_hash_key($job))) {
Branch: 7 branches
BranchTrueFalseTotal
if ($job->{'hash'} ne $self->{'db'}->get_property($self->get_job_hash_key($job))) { } 6 0 6
elsif ($Serge::VERSION ne $self->{'db'}->get_property($self->get_job_engine_version_key($job))) { } 0 0 0
elsif ($job->{'plugin_version'} ne $self->{'db'}->get_property($self->get_job_plugin_version_key($job))) { } 0 0 0
elsif ($job->{'serializer_version'} ne $self->{'db'}->get_property($self->get_job_serializer_plugin_version_key($job))) { } 0 0 0
elsif ($ENV{'L10N_FORCE_GENERATE'}) { } 0 0 0
elsif (exists $self->{'optimizations'} and not $self->{'optimizations'}) { } 0 0 0
elsif (exists $job->{'optimizations'} and not $job->{'optimizations'}) { } 0 0 0
195 6 $opt = undef;
196 6 print "*** OPTIMIZATIONS DISABLED: job definition has changed (or job is ran for the first time) ***\n";
197 } elsif ($Serge::VERSION ne $self->{db}->get_property($self->get_job_engine_version_key($job))) {
198 0 $opt = undef;
199 0 print "*** OPTIMIZATIONS DISABLED: engine version has changed ***\n";
200 } elsif ($job->{plugin_version} ne $self->{db}->get_property($self->get_job_plugin_version_key($job))) {
201 0 $opt = undef;
202 0 print "*** OPTIMIZATIONS DISABLED: parser plugin version has changed ***\n";
203 } elsif ($job->{serializer_version} ne $self->{db}->get_property($self->get_job_serializer_plugin_version_key($job))) {
204 0 $opt = undef;
205 0 print "*** OPTIMIZATIONS DISABLED: serializer plugin version has changed ***\n";
206 } elsif ($ENV{L10N_FORCE_GENERATE}) {
207 0 $opt = undef;
208 0 print "*** OPTIMIZATIONS DISABLED: L10N_FORCE_GENERATE is ON ***\n";
209 ▶ } elsif (exists $self->{optimizations} and !$self->{optimizations}) {
Condition0%0/3
exists $self->{"optimizations"} and not $self->{"optimizations"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"optimizations"} and not $self->{"optimizations"}
exists $self->{"optimizations"} not $self->{"optimizations"}
Truth table0%0/3
A and not(B)
A= exists $self->{"optimizations"}B= not $self->{"optimizations"}
ABresult
0X0
100
111
210 0 $opt = undef;
211 0 print "*** OPTIMIZATIONS DISABLED: engine 'optimizations' setting is set to OFF ***\n";
212 ▶ } elsif (exists $job->{optimizations} and !$job->{optimizations}) {
Condition0%0/3
exists $job->{"optimizations"} and not $job->{"optimizations"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $job->{"optimizations"} and not $job->{"optimizations"}
exists $job->{"optimizations"} not $job->{"optimizations"}
Truth table0%0/3
A and not(B)
A= exists $job->{"optimizations"}B= not $job->{"optimizations"}
ABresult
0X0
100
111
213 0 $opt = undef;
214 0 print "*** OPTIMIZATIONS DISABLED: per-job preference ***\n";
215 }
216  
217 6 $job->{optimizations} = $opt;
218 }
219  
220 sub adjust_job_defaults {
221 6 ▶ my ($self, $job) = @_;
Subroutine adjust_job_defaults: called CC=2 SCAR=6.9
Pod undocumented
222  
223 map {
224 6 ▶ $job->{$_} = $self->{$_} unless exists $job->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $job->{$_} 54 12 66
225 } qw(
226 active
227 output_lang_files
228 output_encoding
229 output_bom
230 reuse_translations
231 reuse_orphaned
232 reuse_fuzzy
233 reuse_as_fuzzy_default
234 reuse_as_fuzzy
235 reuse_as_not_fuzzy
236 similar_languages
237 );
238 }
239  
240 sub save_job_fingerprints {
241 6 ▶ my ($self, $job) = @_;
Subroutine save_job_fingerprints: called CC=3 SCAR=11.3
Pod undocumented
242  
243 6 $self->{db}->set_property($self->get_job_hash_key($job), $job->{hash});
244 6 ▶ $self->{db}->set_property($self->get_job_plugin_version_key($job), $job->{plugin_version}) if $job->{plugin_version};
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"plugin_version"} 6 0 6
245 6 ▶ $self->{db}->set_property($self->get_job_serializer_plugin_version_key($job), $job->{serializer_version}) if $job->{serializer_version};
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"serializer_version"} 6 0 6
246 6 $self->{db}->set_property($self->get_job_engine_version_key($job), $Serge::VERSION);
247 }
248  
249 sub adjust_modified_languages {
250 6 ▶ my ($self, $job) = @_;
Subroutine adjust_modified_languages: called CC=7 SCAR=31.1
Pod undocumented
251  
252 6 my @j = @{$job->{destination_languages}};
253  
254 6 ▶ if ($job->{optimizations} and defined $self->{modified_languages}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{'optimizations'} and defined $self->{'modified_languages'}) { } 0 6 6
Condition33%1/3
$job->{"optimizations"} and defined $self->{"modified_languages"}
!ll&&!rl&&r
600
MC/DC0%0/2
$job->{"optimizations"} and defined $self->{"modified_languages"}
$job->{"optimizations"} defined $self->{"modified_languages"}
Truth table33%1/3
A and B
A= $job->{"optimizations"}B= defined $self->{"modified_languages"}
ABresult
0X0
100
111
255 0 my %job_langs;
256 0 @job_langs{@j} = @j;
257  
258 0 my @l = @{$self->{modified_languages}};
259 0 ▶ print "List of modified languages: ".join(',', @l)."\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
260  
261 0 my %mod_langs;
262 0 @mod_langs{@l} = @l;
263  
264 # delete all languages from mod_langs hash if they do not exist in job_langs
265 0 foreach my $lang (keys %mod_langs) {
266 0 ▶ delete $mod_langs{$lang} unless exists $job_langs{$lang};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $job_langs{$lang} 0 0 0
267 }
268  
269 0 my @a = sort keys %mod_langs;
270 0 $job->{modified_languages} = \@a;
271 } else {
272 # if the configuration parameter is not set, all target languages are considered as modified
273 6 ▶ print "*** Will use the full list of destination languages\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 6 6
274 6 $job->{modified_languages} = \@j;
275 }
276 }
277  
278 sub init_job {
279 6 ▶ my ($self, $job) = @_;
Subroutine init_job: called CC=4 SCAR=14.3
Pod undocumented
280  
281 # freeze job hash prior to doing any modifications to job structure
282  
283 6 $job->{hash} = $job->get_hash;
284  
285 # Initialize temporary job-scope hash of the processed files,
286 # to use later when generating localized versions of the files
287  
288 6 $self->{files} = {};
289 6 $self->{src_hash} = {};
290 6 $self->{force_flags} = {};
291 6 $self->{skip_flags} = {};
292  
293 # reset an array of found files
294  
295 6 $self->{file_mappings} = {};
296 6 $self->{found_files} = [];
297  
298 # reset per-po item count mappings
299 # (needed for plugins like `completeness`)
300  
301 6 $self->{ts_items_count} = {};
302  
303 # reset cache
304  
305 6 $self->{full_output_path_cache} = {};
306  
307 # add reference to current job
308  
309 6 $self->{job} = $job;
310  
311 6 ▶ $job->{debug} = $job->{debug} || $self->{debug};
Condition33%1/3
$job->{'debug'} || $self->{'debug'}
l!l&&r!l&&!r
006
MC/DC0%0/2
$job->{'debug'} || $self->{'debug'}
$job->{'debug'} $self->{'debug'}
Truth table33%1/3
A || B
A= $job->{'debug'}B= $self->{'debug'}
ABresult
1X1
011
000
312  
313 6 ▶ if (exists $job->{debug_nosave}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $job->{"debug_nosave"}) 0 6 6
314 0 $self->{debug_nosave_loc} = $self->{debug_nosave_ts} = $job->{debug_nosave};
315 }
316  
317 # if output_only_mode flag is set, force it on the job
318  
319 6 ▶ $job->{output_only_mode} = 1 if $self->{output_only_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"output_only_mode"} 0 6 6
320  
321 # preserve the original list for future reference
322 6 $job->{original_destination_languages} = $job->{destination_languages};
323 }
324  
325 sub process_job {
326 6 ▶ my ($self, $job) = @_;
Subroutine process_job: called CC=9 SCAR=22.7
Pod undocumented
327  
328 6 $self->init_job($job);
329 6 $self->adjust_job_defaults($job);
330 6 $self->adjust_destination_languages($job);
331  
332 # now that we have a final list of destination languages, we can
333 # determine if the job can run or not
334  
335 6 my $heading = '['.$job->{id}.']';
336 6 ▶ $heading .= ' "'.$job->{name}.'"' if exists $job->{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $job->{"name"} 6 0 6
337  
338 6 ▶ if ($self->job_can_run($job)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->job_can_run($job)) { } 6 0 6
339 6 print "\n\n*** $heading ***\n\n";
340 } else {
341 0 print "*** SKIPPING: $heading ***\n";
342 0 return;
343 }
344  
345 6 $self->open_database($job);
346 6 $self->adjust_job_optimizations($job);
347 6 $self->adjust_modified_languages($job);
348  
349 6 ▶ die "source_dir directory [$job->{source_dir}] doesn't exist. Try doing an initial data checkout (`serge pull --initialize`), or reconfigure your job" unless -d $job->{source_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $job->{"source_dir"} 0 6 6
350  
351 6 print "Source dir: [".$job->{source_dir}."]\n";
352 6 print "DB source: [".$job->{db_source}."]\n";
353 6 print "TS file path: [".$job->{ts_file_path}."]\n";
354 6 print "Output path: [".$job->{output_file_path}."]\n";
355 6 print "Destination languages: [".join(',', sort @{$job->{destination_languages}})."]\n";
356 6 print "Modified languages: [".join(',', sort @{$job->{modified_languages}})."]\n";
357  
358 # preload all items/strings/translations into cache
359  
360 6 $self->{db}->preload_cache_for_job($job->{db_namespace}, $job->{id}, $job->{modified_languages});
361  
362 6 $self->run_callbacks('before_job');
363  
364 6 $self->run_callbacks('before_update_database_from_source_files');
365  
366 6 my $start = [gettimeofday];
367 6 $self->update_database_from_source_files;
368 6 print "update_database_from_source_files() took ", tv_interval($start), " seconds\n";
369  
370 # The following callback has been deprecated.
371 # See Engine::Job::load_plugin_and_register_callbacks for the deprecation notice
372 6 $self->run_callbacks('before_update_database_from_ts_file');
373 # new callback name
374 6 $self->run_callbacks('before_update_database_from_ts_files');
375  
376 6 ▶ if (!$self->{rebuild_ts_files} && !$job->{output_only_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'rebuild_ts_files'} || $job->{'output_only_mode'}) { } 6 0 6
Condition33%1/3
$self->{'rebuild_ts_files'} || $job->{'output_only_mode'}
l!l&&r!l&&!r
006
MC/DC0%0/2
$self->{'rebuild_ts_files'} || $job->{'output_only_mode'}
$self->{'rebuild_ts_files'} $job->{'output_only_mode'}
Truth table33%1/3
A || B
A= $self->{'rebuild_ts_files'}B= $job->{'output_only_mode'}
ABresult
1X1
011
000
377 6 $start = [gettimeofday];
378 6 $self->update_database_from_ts_files;
379 6 print "update_database_from_ts_files() took ", tv_interval($start), " seconds\n";
380 } else {
381 0 print "Skipping update_database_from_ts_files() step\n";
382 }
383  
384 6 $self->run_callbacks('before_generate_ts_files');
385  
386 6 ▶ if (!$self->{debug_nosave_ts} && !$job->{output_only_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'debug_nosave_ts'} || $job->{'output_only_mode'}) { } 6 0 6
Condition33%1/3
$self->{'debug_nosave_ts'} || $job->{'output_only_mode'}
l!l&&r!l&&!r
006
MC/DC0%0/2
$self->{'debug_nosave_ts'} || $job->{'output_only_mode'}
$self->{'debug_nosave_ts'} $job->{'output_only_mode'}
Truth table33%1/3
A || B
A= $self->{'debug_nosave_ts'}B= $job->{'output_only_mode'}
ABresult
1X1
011
000
387 6 $start = [gettimeofday];
388 6 $self->generate_ts_files;
389 6 print "generate_ts_files() took ", tv_interval($start), " seconds\n";
390 } else {
391 0 print "Skipping generate_ts_files() step\n";
392 }
393  
394 6 $self->run_callbacks('before_generate_localized_files');
395  
396 6 ▶ if (!$self->{debug_nosave_loc}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"debug_nosave_loc"}) 6 0 6
397 6 $start = [gettimeofday];
398 6 $self->generate_localized_files;
399 6 print "generate_localized_files() took ", tv_interval($start), " seconds\n";
400 }
401  
402 6 $self->save_job_fingerprints($job);
403  
404 6 $self->commit_transaction;
405  
406 # note: do not close the database at this point as it might be reused in another job
407  
408 6 $self->run_callbacks('after_job');
409  
410 6 print "*** [end] ***\n";
411 }
412  
413 sub commit_transaction {
414 6 ▶ my ($self) = @_;
Subroutine commit_transaction: called CC=1 SCAR=0.0
Pod undocumented
415  
416 6 $self->{db}->commit_transaction;
417 }
418  
419 sub cleanup {
420 6 ▶ my ($self) = @_;
Subroutine cleanup: called CC=1 SCAR=0.0
Pod undocumented
421  
422 6 $self->{db}->close;
423 }
424  
425 sub update_database_from_source_files_postcheck_callback {
426 8 ▶ my ($self, $file_rel, $fullpath) = @_;
Subroutine update_database_from_source_files_postcheck_callback: called CC=2 SCAR=7.0
Pod undocumented
427  
428 8 my ($result) = $self->run_callbacks('rewrite_path', $file_rel);
429 8 ▶ $file_rel = $result if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 0 8 8
430  
431 # save relative=>absolute path mapping
432 8 $self->{file_mappings}->{$file_rel} = $fullpath;
433  
434 8 return $file_rel;
435 }
436  
437 sub update_database_from_source_files {
438 6 ▶ my ($self) = @_;
Subroutine update_database_from_source_files: called CC=36 SCAR=59.3
Pod undocumented
439  
440 6 print "\nUpdating database from source files...\n\n";
441  
442 # Walk through the given directory and its subdirectories, recursively
443  
444 6 my $start = [gettimeofday];
445  
446 6 print "Scanning directory structure...\n";
447  
448 my $ff = Serge::FindFiles->new({
449 8 ▶ postcheck_callback => sub { return $self->update_database_from_source_files_postcheck_callback(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
450 6 });
451 6 ▶ $ff->{prefix} = $self->{job}->{source_path_prefix} if exists $self->{job}->{source_path_prefix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"job"}{"source_path_prefix"} 0 6 6
452 6 $ff->{process_subdirs} = $self->{job}->{source_process_subdirs};
453 6 ▶ $ff->{match} = $self->{job}->{source_match} if exists $self->{job}->{source_match};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"job"}{"source_match"} 6 0 6
454 6 ▶ $ff->{exclude} = $self->{job}->{source_exclude} if exists $self->{job}->{source_exclude};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"job"}{"source_exclude"} 0 6 6
455 6 ▶ $ff->{dir_exclude} = $self->{job}->{source_exclude_dirs} if exists $self->{job}->{source_exclude_dirs};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"job"}{"source_exclude_dirs"} 0 6 6
456  
457 6 $ff->find($self->{job}->{source_dir});
458  
459 6 @{$self->{found_files}} = sort keys %{$ff->{found_files}};
460  
461 6 my $end = [gettimeofday];
462 6 my $time = tv_interval($start, $end);
463 6 my $files_found = scalar(@{$self->{found_files}});
464 6 print("Scanned in $time sec, $files_found files match the criteria\n");
465  
466 6 ▶ die "No files match the search criteria. Please reconfigure your job" unless $files_found;
Branch: 1 branch
BranchTrueFalseTotal
unless $files_found 0 6 6
467  
468 # compare the list with the one in the database and see how many files are new,
469 # an how many of them are orphaned (and mark them as such), or remove the orphaned
470 # flag if needed
471  
472 6 my $files = $self->{db}->get_all_files_for_job($self->{job}->{db_namespace}, $self->{job}->{id});
473  
474 6 my $new = {};
475 6 my $orphaned = {};
476 6 my $no_longer_orphaned = {};
477 6 my $rename = {};
478  
479 6 foreach my $file_rel (@{$self->{found_files}}) { # process files in sorted order for better reporting
480 8 ▶ if (exists $self->{file_mappings}->{$file_rel}) { # because it could have been removed in parse_source_file()
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"file_mappings"}{$file_rel}) 8 0 8
481 8 ▶ if (!exists $files->{$file_rel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $files->{$file_rel}) { } 8 0 8
482 8 $new->{$file_rel} = 1;
483 } else {
484 0 ▶ if ($files->{$file_rel}->{orphaned} == 1) { # file is currently marked as orphaned
Branch: 1 branch
BranchTrueFalseTotal
if ($files->{$file_rel}{"orphaned"} == 1) 0 0 0
485 0 $no_longer_orphaned->{$file_rel} = 1;
486 }
487 }
488 }
489 }
490  
491 6 foreach my $file_rel (sort keys %$files) { # process files in sorted order for better reporting
492 0 ▶ if (!exists $self->{file_mappings}->{$file_rel}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"file_mappings"}{$file_rel}) 0 0 0
493 0 ▶ if ($files->{$file_rel}->{orphaned} == 0) { # file is currently not marked as orphaned
Branch: 1 branch
BranchTrueFalseTotal
if ($files->{$file_rel}{"orphaned"} == 0) 0 0 0
494 0 $orphaned->{$file_rel} = 1;
495 }
496 }
497 }
498  
499 # now that we have a list of new and orphaned files, see if some orphaned files are actually
500 # known ones that were renamed
501  
502 6 ▶ if ((scalar keys %$new > 0) && (scalar keys %$orphaned > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$new > 0 and scalar keys %$orphaned > 0) 0 6 6
Condition33%1/3
scalar keys %$new > 0 and scalar keys %$orphaned > 0
!ll&&!rl&&r
060
MC/DC0%0/2
scalar keys %$new > 0 and scalar keys %$orphaned > 0
scalar keys %$new > 0 scalar keys %$orphaned > 0
Truth table33%1/3
A and B
A= scalar keys %$new > 0B= scalar keys %$orphaned > 0
ABresult
0X0
100
111
503 # first leave only new/orphaned files with the same file size
504  
505 # get file sizes of new files
506 0 my $new_fsize = {};
507 0 foreach my $file_rel (sort keys %$new) {
508 0 my $path = $self->get_full_source_path($file_rel);
509 0 my $size = -s $path;
510 0 ▶ $new_fsize->{$size} = [] unless exists $new_fsize->{$size};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $new_fsize->{$size} 0 0 0
511 0 push @{$new_fsize->{$size}}, $file_rel;
512 }
513  
514 # get file sizes of orphaned files
515 0 my $orphaned_fsize = {};
516 0 foreach my $file_rel (sort keys %$orphaned) {
517 0 my $path = $self->get_full_source_path($file_rel);
518 0 my $size = $self->{db}->get_property("size:$files->{$file_rel}->{id}");
519 0 ▶ if (defined $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) 0 0 0
520 0 ▶ $orphaned_fsize->{$size} = [] unless exists $orphaned_fsize->{$size};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $orphaned_fsize->{$size} 0 0 0
521 0 push @{$orphaned_fsize->{$size}}, $file_rel;
522 }
523 }
524  
525 0 foreach my $size (keys %$new_fsize) {
526 0 ▶ if (exists $orphaned_fsize->{$size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $orphaned_fsize->{$size}) 0 0 0
527 0 my $md5 = {};
528 0 foreach my $file_rel (@{$new_fsize->{$size}}) {
529 0 my $path = $self->get_full_source_path($file_rel);
530 # go through new files and calculate their md5 hash;
531 # if there are multiple identical files, the last one will win
532 0 $md5->{md5_hex(encode_utf8(read_and_normalize_file($path)))} = $file_rel;
533 }
534 0 foreach my $file_rel (@{$orphaned_fsize->{$size}}) {
535 0 my $hash = $self->{db}->get_property("hash:$files->{$file_rel}->{id}");
536 0 ▶ if (defined $hash && exists $md5->{$hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash and exists $md5->{$hash}) 0 0 0
Condition0%0/3
defined $hash and exists $md5->{$hash}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hash and exists $md5->{$hash}
defined $hash exists $md5->{$hash}
Truth table0%0/3
A and B
A= defined $hashB= exists $md5->{$hash}
ABresult
0X0
100
111
537 0 my $new_file_rel = $md5->{$hash};
538 0 $rename->{$file_rel} = $new_file_rel; # map old file_rel to a new one
539 0 delete $new->{$new_file_rel};
540 0 delete $md5->{$hash};
541 0 delete $orphaned->{$file_rel};
542 }
543 }
544 }
545 }
546 }
547  
548 # update database for renamed files before we parse files (unless we are in output_only mode)
549 # otherwise new entry in database will be crated for the renamed file
550  
551 map {
552 6 ▶ if (!$self->{job}->{output_only_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"job"}{"output_only_mode"}) 0 0 0
553 0 ▶ print "Changing path property for file [$files->{$_}->{id}] to '$rename->{$_}'\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
554 # change path in the database
555 0 $self->{db}->update_file_props($files->{$_}->{id}, {path => $rename->{$_}});
556 }
557 # change the key in the local files list
558 0 $files->{$rename->{$_}} = $files->{$_};
559 0 delete $files->{$_};
560  
561 } keys %$rename;
562  
563 # now parse files; during this process, plugins may report that some files
564 # should be considered orphaned
565  
566 6 foreach my $file_rel (@{$self->{found_files}}) {
567 8 my $orph = undef;
568 8 $self->parse_source_file($file_rel, \$orph);
569 8 ▶ if ($orph) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orph) 0 8 8
570 # remove the file from $self->{file_mappings} hash
571 0 delete $self->{file_mappings}->{$file_rel};
572  
573 # adjust the list of new/orphaned/non-orphaned files
574 # for the script to set props later at once
575 0 delete $new->{$file_rel};
576 0 delete $no_longer_orphaned->{$file_rel};
577  
578 0 ▶ if (exists $files->{$file_rel} && $files->{$file_rel}->{orphaned} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $files->{$file_rel} and $files->{$file_rel}{"orphaned"} == 0) 0 0 0
Condition0%0/3
exists $files->{$file_rel} and $files->{$file_rel}{"orphaned"} == 0
!ll&&!rl&&r
000
MC/DC0%0/2
exists $files->{$file_rel} and $files->{$file_rel}{"orphaned"} == 0
exists $files->{$file_rel} $files->{$file_rel}{"orphaned"} == 0
Truth table0%0/3
A and B
A= exists $files->{$file_rel}B= $files->{$file_rel}{"orphaned"} == 0
ABresult
0X0
100
111
579 0 $orphaned->{$file_rel} = 1;
580 }
581 }
582 }
583  
584 6 print scalar keys %$new, " files are new, ",
585 scalar keys %$orphaned, " were orphaned and ",
586 scalar keys %$no_longer_orphaned, " are no longer orphaned since last run\n";
587 6 ▶ if (scalar keys %$rename) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$rename) 0 6 6
588 0 print "The following files were renamed:\n";
589 map {
590 0 print "\t$_ => $rename->{$_}\n";
591 } sort keys %$rename;
592 }
593  
594 # now update orphaned flags in the database (unless we are in output_only mode)
595  
596 6 ▶ if (!$self->{job}->{output_only_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"job"}{"output_only_mode"}) 6 0 6
597 map {
598 6 ▶ if ($files->{$_}->{orphaned} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files->{$_}{"orphaned"} == 0) 0 0 0
599 0 print "\tSetting 'orphaned' flag on file $_\n";
600 0 $self->{db}->update_file_props($files->{$_}->{id}, {orphaned => 1}); # mark as orphaned
601 }
602 } keys %$orphaned;
603  
604 map {
605 6 ▶ if ($files->{$_}->{orphaned} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files->{$_}{"orphaned"} == 1) 0 0 0
606 0 print "\tRemoving 'orphaned' flag from file $_\n";
607 0 $self->{db}->update_file_props($files->{$_}->{id}, {orphaned => 0}); # mark as not orphaned
608 }
609 } keys %$no_longer_orphaned;
610 }
611 }
612  
613 sub parse_source_file {
614 8 ▶ my ($self, $file_rel, $orphanedref) = @_;
Subroutine parse_source_file: called CC=20 SCAR=42.5
Pod undocumented
615  
616 # set global CURRENT_FILE_REL variable to current relative file path
617 # as it will later be used in different parts
618 8 $self->{current_file_rel} = $file_rel;
619 8 $self->{current_file_id} = undef;
620  
621 8 print "\t$self->{current_file_rel}\n";
622  
623 8 my $path = $self->get_full_source_path($self->{current_file_rel});
624  
625 8 my ($src, $file_hash) = $self->read_file($path, 1);
626  
627 8 $self->run_callbacks('after_load_source_file_for_processing', $self->{current_file_rel}, \$src);
628  
629 8 my $result = combine_and($self->run_callbacks('is_file_orphaned', $self->{current_file_rel}));
630 8 ▶ if ($result eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 1) 0 8 8
631 0 ▶ print "\tSkip $self->{current_file_rel} because callback said it is orphaned\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
632 0 $$orphanedref = 1;
633 0 return;
634 }
635  
636 8 $result = combine_and(1, $self->run_callbacks('can_process_source_file', $self->{current_file_rel}, undef, \$src));
637 8 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 8 8
638 0 ▶ print "\tSkip $self->{current_file_rel} because at least one callback returned 0\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
639 0 return;
640 }
641  
642 # get the file id
643 # - in regular mode, create the record in [files] table if it doesn't exist yet
644 # - in output_only_mode, don't create a file record if it doesn't exist, but skip the file entirely
645  
646 8 $self->{current_file_id} = $self->{db}->get_file_id($self->{job}->{db_namespace}, $self->{job}->{id}, $self->{current_file_rel}, $self->{job}->{output_only_mode});
647 8 ▶ if ($self->{job}->{output_only_mode} && !$self->{current_file_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"job"}{"output_only_mode"} and not $self->{"current_file_id"}) 0 8 8
Condition33%1/3
$self->{"job"}{"output_only_mode"} and not $self->{"current_file_id"}
!ll&&!rl&&r
800
MC/DC0%0/2
$self->{"job"}{"output_only_mode"} and not $self->{"current_file_id"}
$self->{"job"}{"output_only_mode"} not $self->{"current_file_id"}
Truth table33%1/3
A and not(B)
A= $self->{"job"}{"output_only_mode"}B= not $self->{"current_file_id"}
ABresult
0X0
100
111
648 0 print "\tWARNING: $self->{current_file_rel} will be skipped in 'output_only_mode' because it is not registered in the database\n";
649 0 return;
650 }
651  
652 # in output_only_mode, check that the file was previously properly parsed.
653 # If it wasn't, don't add it to the list of files to process
654  
655 8 ▶ if ($self->{job}->{output_only_mode} && $self->{db}->get_property("source:$self->{current_file_id}") eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"job"}{"output_only_mode"} and $self->{"db"}->get_property(qq[source:$self->{"current_file_id"}]) eq "") 0 8 8
Condition33%1/3
$self->{"job"}{"output_only_mode"} and $self->{"db"}->get_property(qq[source:$self->{"current_file_id"}]) eq ""
!ll&&!rl&&r
800
MC/DC0%0/2
$self->{"job"}{"output_only_mode"} and $self->{"db"}->get_property(qq[source:$self->{"current_file_id"}]) eq ""
$self->{"job"}{"output_only_mode"} $self->{"db"}->get_property(qq[source:$self->{"current_file_id"}]) eq ""
Truth table33%1/3
A and B
A= $self->{"job"}{"output_only_mode"}B= $self->{"db"}->get_property(qq[source:$self->{"current_file_id"}]) eq ""
ABresult
0X0
100
111
656 0 print "\tWARNING: $self->{current_file_rel} will be skipped in 'output_only_mode' because it was not properly parsed previously\n";
657 0 return;
658 }
659  
660 # Create a temporary array for the file to store item_id's
661 # (it will be needed to place strings in translation files in the same order
662 # as they were found in the source files)
663 8 my $aref = [];
664  
665 # register the file in the list of of ones that need to be processed this time
666 8 $self->{files}->{$self->{current_file_rel}} = $aref;
667  
668 8 my $current_hash = generate_hash($src);
669  
670 # store actual src hash in memory so that it can be used in
671 # generate_localized_files_for_file_lang() function in output-only mode
672 # (where we can't read this value from 'source:<file_id>' property)
673 8 $self->{src_hash}->{$self->{current_file_rel}} = $current_hash;
674  
675 8 ▶ if ($self->{job}->{optimizations} and ($current_hash eq $self->{db}->get_property("source:$self->{current_file_id}"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"job"}{"optimizations"} and $current_hash eq $self->{"db"}->get_property(qq[source:$self->{"current_file_id"}])) 0 8 8
Condition33%1/3
$self->{"job"}{"optimizations"} and $current_hash eq $self->{"db"}->get_property(qq[source:$self->{"current_file_id"}])
!ll&&!rl&&r
800
MC/DC0%0/2
$self->{"job"}{"optimizations"} and $current_hash eq $self->{"db"}->get_property(qq[source:$self->{"current_file_id"}])
$self->{"job"}{"optimizations"} $current_hash eq $self->{"db"}->get_property(qq[source:$self->{"current_file_id"}])
Truth table33%1/3
A and B
A= $self->{"job"}{"optimizations"}B= $current_hash eq $self->{"db"}->get_property(qq[source:$self->{"current_file_id"}])
ABresult
0X0
100
111
676 0 my $items_text = $self->{db}->get_property("items:$self->{current_file_id}");
677 # $items_text can be an empty string, which is fine
678 # (this means that the file currently contains no translatable strings)
679 0 ▶ if (defined $items_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $items_text) 0 0 0
680 0 @$aref = split(',', $items_text); # restore items
681 0 ▶ print "\tSkip parsing $self->{current_file_rel} because it is not modified since last run\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
682 # mark file as skipped to optimize po/target file generation
683 0 $self->{skip_flags}->{$self->{current_file_rel}} = 1; # we use filename here, not id
684 0 return;
685 }
686 }
687  
688 # Skip parsing source file if we are in special output_only_mode
689 # the file should have been processed by some previous job.
690 # Also, if job ID differs, the file's job won't be updated in the database
691  
692 8 ▶ return if $self->{job}->{output_only_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"job"}{"output_only_mode"} 0 8 8
693  
694 # Update job for the file if necessary (but not for jobs in output_only_mode)
695  
696 8 my $props = $self->{db}->get_file_props($self->{current_file_id});
697 8 ▶ if ($self->{job}->{id} ne $props->{job}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"job"}{"id"} ne $props->{"job"}) 0 8 8
698 0 print "Setting $self->{current_file_rel} file's job to '".$self->{job}->{id}."'\n";# if $self->{debug};
699 0 $self->{db}->update_file_props($self->{current_file_id}, {job => $self->{job}->{id}});
700 }
701  
702 8 $self->clear_disambiguation_cache;
703  
704 # Parsing the file
705  
706 8 eval {
707 8 ▶ $self->{job}->{parser_object}->parse(\$src, sub { $self->parse_source_file_callback(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
708 };
709  
710 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
711 0 print "\t\tWARNING: File parsing failed; the file will not be processed\n";
712 0 print "\t\tReason: $@\n";
713  
714 # Removing the file from the list of ones that need to be processed this time
715 0 delete $self->{files}->{$self->{current_file_rel}};
716  
717 0 return undef;
718 }
719  
720 # Comparing old item_id's in database with the list of new ones,
721 # to set or remove their 'orphaned' flags if necessary
722  
723 8 my %db_items = %{$self->{db}->get_all_items_for_file($self->{current_file_id})};
724  
725 8 foreach my $item_id (@$aref) {
726 54 ▶ if ($db_items{$item_id} == 1) { # item is currently marked as orphaned
Branch: 1 branch
BranchTrueFalseTotal
if ($db_items{$item_id} == 1) 0 54 54
727 0 $self->{db}->update_item_props($item_id, {orphaned => 0}); # mark as not orphaned
728 }
729 54 delete $db_items{$item_id}; # remove from the hash
730 }
731  
732 # now the hash contains only orphaned items, so mark them as such when needed
733  
734 8 foreach my $item_id (keys %db_items) {
735 0 ▶ if ($db_items{$item_id} == 0) { # item is currently not marked as orphaned
Branch: 1 branch
BranchTrueFalseTotal
if ($db_items{$item_id} == 0) 0 0 0
736 0 $self->{db}->update_item_props($item_id, {orphaned => 1}); # mark as orphaned
737 }
738 }
739  
740 8 $self->{db}->set_property("source:$self->{current_file_id}", $current_hash);
741 8 $self->{db}->set_property("hash:$self->{current_file_id}", $file_hash);
742 8 $self->{db}->set_property("size:$self->{current_file_id}", -s $path);
743 8 $self->{db}->set_property("items:$self->{current_file_id}", join(',', @$aref));
744 }
745  
746 sub read_file {
747 16 ▶ my ($self, $fname, $calc_hash) = @_;
Subroutine read_file: called CC=2 SCAR=6.9
Pod undocumented
748  
749 16 my $data = read_and_normalize_file($fname);
750  
751 16 ▶ my $hash = md5_hex(encode_utf8($data)) if $calc_hash;
Branch: 1 branch
BranchTrueFalseTotal
if $calc_hash 8 8 16
752  
753 16 $self->run_callbacks('after_load_file', $fname, $self->{job}->{source_language}, \$data);
754  
755 16 return ($data, $hash);
756 }
757  
758 sub clear_disambiguation_cache {
759 16 ▶ my ($self) = @_;
Subroutine clear_disambiguation_cache: called CC=1 SCAR=0.0
Pod undocumented
760  
761 # empty hash of items used to detect duplicate strings in currently parsed file
762 16 $self->{current_file_source_keys} = {};
763 16 $self->{current_file_keys} = {};
764 }
765  
766 sub disambiguate_string {
767 108 ▶ my ($self, $string, $context, $source_key, $hint) = @_;
Subroutine disambiguate_string: called CC=12 SCAR=34.0
Pod undocumented
768  
769 108 ▶ if (defined $source_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source_key) 108 0 108
770 108 ▶ if ($self->{current_file_source_keys}->{$source_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"current_file_source_keys"}{$source_key}) 0 108 108
771 0 print "\t\tWARNING: Duplicate key '$source_key' found in the file\n";
772 }
773 108 $self->{current_file_source_keys}->{$source_key} = 1;
774 }
775  
776 # if job's `use_keys_as_context` option is turned on,
777 # and the context has not been provided explicitly,
778 # copy the key into the context before disambiguating
779  
780 108 ▶ if ($context eq '' && $self->{job}->{use_keys_as_context} && $source_key ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($context eq "" and $self->{"job"}{"use_keys_as_context"} and $source_key ne "") 0 108 108
Condition33%2/6
$context eq "" and $self->{"job"}{"use_keys_as_context"} and $source_key ne ""
!ll&&!rl&&r
10800
$context eq "" and $self->{"job"}{"use_keys_as_context"}
!ll&&!rl&&r
01080
MC/DC0%0/3
$context eq "" and $self->{"job"}{"use_keys_as_context"} and $source_key ne ""
$context eq "" $self->{"job"}{"use_keys_as_context"} $source_key ne ""
Truth table25%1/4
A and B and C
A= $context eq ""B= $self->{"job"}{"use_keys_as_context"}C= $source_key ne ""
ABCresult
0XX0
10X0
1100
1111
781 0 $context = $source_key;
782 }
783  
784 # see if the item was already found in this file and
785 # alter context if necessary to disambiguate the string
786  
787 108 my $key = generate_key($string, $context);
788  
789 # check if the potential context already exists, and if yes, use source_key as a context
790  
791 108 ▶ if ((exists $self->{current_file_keys}->{$key}) && $source_key ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"current_file_keys"}{$key} and $source_key ne "") 0 108 108
Condition33%1/3
exists $self->{"current_file_keys"}{$key} and $source_key ne ""
!ll&&!rl&&r
10800
MC/DC0%0/2
exists $self->{"current_file_keys"}{$key} and $source_key ne ""
exists $self->{"current_file_keys"}{$key} $source_key ne ""
Truth table33%1/3
A and B
A= exists $self->{"current_file_keys"}{$key}B= $source_key ne ""
ABresult
0X0
100
111
792 0 $context = $source_key;
793 0 $key = generate_key($string, $context);
794 }
795  
796 # check if the potential context already exists, and if yes, use hint as a context
797  
798 108 ▶ if ((exists $self->{current_file_keys}->{$key}) && $hint ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"current_file_keys"}{$key} and $hint ne "") 0 108 108
Condition33%1/3
exists $self->{"current_file_keys"}{$key} and $hint ne ""
!ll&&!rl&&r
10800
MC/DC0%0/2
exists $self->{"current_file_keys"}{$key} and $hint ne ""
exists $self->{"current_file_keys"}{$key} $hint ne ""
Truth table33%1/3
A and B
A= exists $self->{"current_file_keys"}{$key}B= $hint ne ""
ABresult
0X0
100
111
799 0 $context = $hint;
800 0 $key = generate_key($string, $context);
801 }
802  
803 # check if potential context exists, and if yes, try to auto-generate context
804  
805 108 my $context_counter = 1;
806 108 ▶ my $context_base = $context || 'context';
Condition50%1/2
$context || 'context'
l!l
0108
MC/DC0%0/1
$context || 'context'
$context
Truth table50%1/2
A
A= $context
Aresult
11
00
807 108 ▶ while (exists $self->{current_file_keys}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"current_file_keys"}{$key}) 0 108 108
808 0 $context = "$context_base.$context_counter";
809 0 $key = generate_key($string, $context);
810 0 $context_counter++;
811 }
812  
813 108 $self->{current_file_keys}->{$key} = $source_key;
814  
815 108 return $context;
816 }
817  
818 sub parse_source_file_callback {
819 54 ▶ my ($self, $string, $context, $hint, $flagsref, $lang, $key) = @_;
Subroutine parse_source_file_callback: called CC=15 SCAR=34.6
Pod undocumented
820  
821 # Normalize parameters
822  
823 54 my $norm = $self->{job}->{normalize_strings};
824 54 ▶ $norm = undef if is_flag_set($flagsref, 'dont-normalize');
Branch: 1 branch
BranchTrueFalseTotal
if is_flag_set($flagsref, "dont-normalize") 0 54 54
825 54 ▶ $norm = 1 if is_flag_set($flagsref, 'normalize');
Branch: 1 branch
BranchTrueFalseTotal
if is_flag_set($flagsref, "normalize") 0 54 54
826  
827 54 ▶ if ($norm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($norm) 0 54 54
828 0 normalize_strref(\$string);
829 }
830  
831 54 ▶ if ($string eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq "") 0 54 54
832 0 ▶ print "::skipping empty string\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
833 0 return;
834 }
835  
836 54 ▶ $string = NFC($string) if ($string =~ m/[^\x00-\x7F]/);
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /[^\x00-\x7F]/ 0 54 54
837 54 ▶ $context = NFC($context) if ($context =~ m/[^\x00-\x7F]/);
Branch: 1 branch
BranchTrueFalseTotal
if $context =~ /[^\x00-\x7F]/ 0 54 54
838 54 ▶ $hint = NFC($hint) if ($hint =~ m/[^\x00-\x7F]/);
Branch: 1 branch
BranchTrueFalseTotal
if $hint =~ /[^\x00-\x7F]/ 0 54 54
839  
840 54 $self->run_callbacks('rewrite_source', $self->{current_file_rel}, undef, \$string, \$hint);
841  
842 # normalize once again, in case the string was changed
843 54 ▶ $string = NFC($string) if ($string =~ m/[^\x00-\x7F]/);
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /[^\x00-\x7F]/ 0 54 54
844  
845 54 $context = $self->disambiguate_string($string, $context, $key, $hint);
846  
847 54 my $result = combine_and(1, $self->run_callbacks('can_extract', $self->{current_file_rel}, undef, \$string, \$hint, $context, $key));
848 54 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 54 54
849 0 ▶ print "\t\tSkip extracting string '$string' because at least one callback returned 0\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
850 0 return;
851 }
852  
853 54 my $string_id = $self->{db}->get_string_id($string, $context);
854  
855 # get (and create if necessary) the item record for given file_id/string_id
856  
857 54 my $item_id = $self->{db}->get_item_id($self->{current_file_id}, $string_id, $hint);
858  
859 54 ▶ print "::[$item_id]::[$self->{current_file_rel}]::[$string],[$context],[$hint],[$lang]\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 54 54
860  
861 # push the id to the end of the temporary array
862 # (we need to record items for all strings, even skipped, to correctly
863 # distinguish between orphaned items and existing items pointing to skipped strings)
864  
865 54 push @{$self->{files}->{$self->{current_file_rel}}}, $item_id;
866  
867 # get old item hint
868  
869 54 my $props = $self->{db}->get_item_props($item_id);
870  
871 # update item with the hint if it is changed
872  
873 54 ▶ if ($hint ne $props->{hint}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hint ne $props->{"hint"}) 0 54 54
874 0 ▶ print "Hint changed for item $item_id: was '$props->{hint}', now '$hint'\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
875 0 $self->{db}->update_item_props($item_id, {hint => $hint});
876 }
877  
878 # return registered item id (this is used in Serge::Engine method descendant, Serge::Importer)
879 54 return $item_id;
880 }
881  
882 sub update_database_from_ts_files {
883 6 ▶ my ($self) = @_;
Subroutine update_database_from_ts_files: called CC=3 SCAR=11.1
Pod undocumented
884  
885 # Skip parsing translation files if we are in special output_only_mode
886  
887 6 ▶ return if $self->{job}->{output_only_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"job"}{"output_only_mode"} 0 6 6
888  
889 # Iterating through all modified languages
890 6 foreach my $lang (sort @{$self->{job}->{modified_languages}}) {
891 6 $self->update_database_from_ts_files_lang($lang);
892 }
893 }
894  
895 sub update_database_from_ts_files_lang {
896 6 ▶ my ($self, $lang) = @_;
Subroutine update_database_from_ts_files_lang: called CC=7 SCAR=20.5
Pod undocumented
897  
898 # sanity check: if this is a source language, do not import anything into database
899  
900 6 ▶ return if ($lang eq $self->{job}->{source_language});
Branch: 1 branch
BranchTrueFalseTotal
if $lang eq $self->{"job"}{"source_language"} 0 6 6
901  
902 6 print "Updating database from translation files for [$lang] language...\n";
903  
904 # find all translation files in specified directory
905  
906 6 foreach my $file (sort keys %{$self->{files}}) {
907 8 my $count = scalar @{$self->{files}->{$file}};
908 8 ▶ if ($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 8 0 8
909 8 my $count_prop_key = "ts:$self->{current_file_id}:$lang:count";
910 8 ▶ $count = $self->{db}->get_property($count_prop_key) || $count;
Condition33%1/3
$self->{'db'}->get_property($count_prop_key) || $count
l!l&&r!l&&!r
080
MC/DC0%0/2
$self->{'db'}->get_property($count_prop_key) || $count
$self->{'db'}->get_property($count_prop_key) $count
Truth table33%1/3
A || B
A= $self->{'db'}->get_property($count_prop_key)B= $count
ABresult
1X1
011
000
911 }
912 8 ▶ print "[$file]=>[$count]\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 8 8
913 8 ▶ if ($count > 0) { # we expect strings from translation file
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 8 0 8
914 8 $self->update_database_from_ts_files_lang_file($lang, $file);
915 }
916 }
917 }
918  
919 sub update_database_from_ts_files_lang_file {
920 8 ▶ my ($self, $lang, $relfile) = @_;
Subroutine update_database_from_ts_files_lang_file: called CC=26 SCAR=63.3
Pod undocumented
921  
922 8 $self->{current_file_rel} = $relfile;
923 8 $self->{current_file_id} = undef;
924  
925 8 my $fullpath = $self->get_full_ts_file_path($relfile, $lang);
926  
927 8 ▶ if (!-f $fullpath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $fullpath) 8 0 8
928 8 ▶ print "\tFile does not exist: $fullpath\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 8 8
929 8 return;
930 }
931  
932 0 $self->run_callbacks('before_update_database_from_ts_lang_file', $self->{job}->{db_namespace}, $relfile, $lang);
933  
934 0 my $result = combine_and(1, $self->run_callbacks('can_process_ts_file', $relfile, $lang));
935 0 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 0 0
936 0 ▶ print "\tSkip $fullpath because at least one callback returned 0\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
937 0 return;
938 }
939  
940 # creating the record in [files] table if it doesn't exist yet
941  
942 0 $self->{current_file_id} = $self->{db}->get_file_id($self->{job}->{db_namespace}, $self->{job}->{id}, $self->{current_file_rel});
943  
944 # Reading the entire file
945  
946 0 ▶ if (!open(TS, $fullpath)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open TS, $fullpath) 0 0 0
947 0 print "WARNING: Can't read $fullpath: $!\n";
948 0 return;
949 }
950 0 binmode(TS);
951 0 my $text = decode_utf8(join('', <TS>));
952 0 close(TS);
953  
954 0 $self->run_callbacks('before_deserialize_ts_file', $self->{current_file_rel}, \$text);
955  
956 0 my $current_hash = generate_hash($text);
957  
958 0 ▶ if ($self->{job}->{optimizations} and ($current_hash eq $self->{db}->get_property("ts:$self->{current_file_id}:$lang"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'job'}{'optimizations'} and $current_hash eq $self->{'db'}->get_property("ts:$self->{'current_file_id'}:$lang")) { } 0 0 0
Condition0%0/3
$self->{"job"}{"optimizations"} and $current_hash eq $self->{"db"}->get_property(qq[ts:$self->{"current_file_id"}:$lang])
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"job"}{"optimizations"} and $current_hash eq $self->{"db"}->get_property(qq[ts:$self->{"current_file_id"}:$lang])
$self->{"job"}{"optimizations"} $current_hash eq $self->{"db"}->get_property(qq[ts:$self->{"current_file_id"}:$lang])
Truth table0%0/3
A and B
A= $self->{"job"}{"optimizations"}B= $current_hash eq $self->{"db"}->get_property(qq[ts:$self->{"current_file_id"}:$lang])
ABresult
0X0
100
111
959 0 ▶ print "\tSkip $fullpath because it is not modified since last run\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
960 } else {
961 0 print "\t$fullpath\n";
962  
963 # Parsing the file
964  
965 0 my $units;
966 0 eval {
967 0 $units = $self->{job}->{serializer_object}->deserialize(\$text);
968 };
969  
970 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
971 0 print "\t\tWARNING: File deserializing failed; the file will not be processed\n";
972 0 print "\t\tReason: $@\n";
973 0 return undef;
974 }
975  
976 0 foreach my $unit (@$units) {
977 0 my $string_id = $self->{db}->get_string_id($unit->{source}, $unit->{context}, 1); # do not create the string, just look if there is one
978  
979 # get item_id for current namespace/file and string/context
980  
981 0 my $item_id = $self->{db}->get_item_id($self->{current_file_id}, $string_id, undef, 1); # do not create
982  
983 0 ▶ if (!$item_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item_id) 0 0 0
984 0 print "\t\t? [no item_id for string] $unit->{key}\n";
985 0 next;
986 }
987  
988 # run plugins that might want to modify translation/comment,
989 # or introduce special functionality on top of the default behavior
990 # (for example, remove translations or mark strings as skipped based on the special flag or comment)
991  
992 0 my $item_comment;
993 $self->run_callbacks('rewrite_parsed_ts_file_item', $relfile, $lang, $item_id,
994 0 \$unit->{source}, $unit->{flags}, \$unit->{target}, \$unit->{comment}, \$unit->{fuzzy}, \$item_comment);
995 0 ▶ if (defined $item_comment) { # it can be an empty string
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item_comment) 0 0 0
996 0 ▶ $item_comment = undef if $item_comment eq ''; # normalize the empty value
Branch: 1 branch
BranchTrueFalseTotal
if $item_comment eq "" 0 0 0
997 0 my $item_props = $self->{db}->get_item_props($item_id);
998 0 ▶ if ($item_props->{comment} ne $item_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item_props->{"comment"} ne $item_comment) 0 0 0
999 0 print "\t\t> [$item_id:item_comment] => '$item_comment'\n";
1000 0 $self->{db}->update_item_props($item_id, {comment => $item_comment});
1001 }
1002 }
1003  
1004 # sanity check: if the string is marked as skipped, skip the block
1005  
1006 0 my $props = $self->{db}->get_string_props($string_id);
1007 0 ▶ if ($props->{skip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props->{"skip"}) 0 0 0
1008 0 print "\t\t? [string is marked as skipped] $unit->{key}\n";
1009 0 next;
1010 }
1011  
1012 # sanity check: fuzzy flag with empty translation makes no sense
1013  
1014 0 ▶ if ($unit->{target} eq '' && $unit->{fuzzy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit->{"target"} eq "" and $unit->{"fuzzy"}) 0 0 0
Condition0%0/3
$unit->{"target"} eq "" and $unit->{"fuzzy"}
!ll&&!rl&&r
000
MC/DC0%0/2
$unit->{"target"} eq "" and $unit->{"fuzzy"}
$unit->{"target"} eq "" $unit->{"fuzzy"}
Truth table0%0/3
A and B
A= $unit->{"target"} eq ""B= $unit->{"fuzzy"}
ABresult
0X0
100
111
1015 0 print "\t\t? [empty translation marked as fuzzy] $unit->{key}\n";
1016 0 $unit->{fuzzy} = 0; # clear the fuzzy flag
1017 }
1018  
1019 0 my $translation_id = $self->{db}->get_translation_id($item_id, $lang, undef, undef, undef, undef, 1); # do not create
1020  
1021 0 ▶ if ($translation_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($translation_id) 0 0 0
1022 0 my $props = $self->{db}->get_translation_props($translation_id);
1023  
1024 0 ▶ if ($props->{merge}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props->{"merge"}) 0 0 0
1025 0 $self->{db}->update_translation_props($translation_id, {merge => 0}); # clear merge flag
1026 0 print "\t\t! [ignore:merge] $unit->{key}\n";
1027 0 next;
1028 }
1029  
1030 0 ▶ next if (($unit->{target} eq $props->{string}) && ($unit->{comment} eq $props->{comment}) && ($unit->{fuzzy} == $props->{fuzzy}));
Branch: 1 branch
BranchTrueFalseTotal
if $unit->{"target"} eq $props->{"string"} and $unit->{"comment"} eq $props->{"comment"} and $unit->{"fuzzy"} == $props->{"fuzzy"} 0 0 0
Condition0%0/6
$unit->{"target"} eq $props->{"string"} and $unit->{"comment"} eq $props->{"comment"} and $unit->{"fuzzy"} == $props->{"fuzzy"}
!ll&&!rl&&r
000
$unit->{"target"} eq $props->{"string"} and $unit->{"comment"} eq $props->{"comment"}
!ll&&!rl&&r
000
MC/DC0%0/3
$unit->{"target"} eq $props->{"string"} and $unit->{"comment"} eq $props->{"comment"} and $unit->{"fuzzy"} == $props->{"fuzzy"}
$unit->{"target"} eq $props->{"string"} $unit->{"comment"} eq $props->{"comment"} $unit->{"fuzzy"} == $props->{"fuzzy"}
Truth table0%0/4
A and B and C
A= $unit->{"target"} eq $props->{"string"}B= $unit->{"comment"} eq $props->{"comment"}C= $unit->{"fuzzy"} == $props->{"fuzzy"}
ABCresult
0XX0
10X0
1100
1111
1031 }
1032  
1033 # check again if translation or comment is set here,
1034 # because the values could have been removed in the callback;
1035 # also, if translation already exists (translation_id is defined),
1036 # we must update the record as well
1037 0 ▶ if ($unit->{target} ne '' or $unit->{comment} ne '' or $translation_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit->{"target"} ne "" or $unit->{"comment"} ne "" or $translation_id) 0 0 0
Condition0%0/6
$unit->{"target"} ne "" or $unit->{"comment"} ne "" or $translation_id
l!l&&r!l&&!r
000
$unit->{"target"} ne "" or $unit->{"comment"} ne ""
l!l&&r!l&&!r
000
MC/DC0%0/3
$unit->{"target"} ne "" or $unit->{"comment"} ne "" or $translation_id
$unit->{"target"} ne "" $unit->{"comment"} ne "" $translation_id
Truth table0%0/4
A or B or C
A= $unit->{"target"} ne ""B= $unit->{"comment"} ne ""C= $translation_id
ABCresult
1XX1
01X1
0011
0000
1038 0 print "\t\t> $unit->{key} => [$item_id/$lang]=[$translation_id]\n";
1039 0 $self->{db}->set_translation($item_id, $lang, $unit->{target}, $unit->{fuzzy}, $unit->{comment}, 0);
1040 }
1041 } # foreach
1042  
1043 0 $self->{db}->set_property("ts:$self->{current_file_id}:$lang", $current_hash);
1044 } # end if
1045  
1046 0 $self->run_callbacks('after_update_database_from_ts_lang_file', $self->{job}->{db_namespace}, $relfile, $lang);
1047 }
1048  
1049 sub get_full_source_path {
1050 16 ▶ my ($self, $file) = @_;
Subroutine get_full_source_path: called CC=1 SCAR=0.0
Pod undocumented
1051  
1052 # get absolute path based on provided relative path
1053 16 my $path = $self->{file_mappings}->{$file};
1054  
1055 16 return $path;
1056 }
1057  
1058 sub get_full_output_path {
1059 8 ▶ my ($self, $file, $lang, $base_dir) = @_;
Subroutine get_full_output_path: called CC=5 SCAR=16.6
Pod undocumented
1060  
1061 8 ▶ $base_dir = $self->{job}->{output_file_path} unless $base_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $base_dir 8 0 8
1062  
1063 8 my $key = join("\001", $file, $lang, $base_dir);
1064 8 my $fullpath = $self->{full_output_path_cache}->{$key};
1065  
1066 8 ▶ return $fullpath if $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
if $fullpath 0 8 8
1067  
1068 8 my ($f) = $self->run_callbacks('rewrite_relative_output_file_path', $file, $lang);
1069 8 ▶ $file = $f if $f;
Branch: 1 branch
BranchTrueFalseTotal
if $f 0 8 8
1070  
1071 8 $fullpath = $self->{job}->render_full_output_path($base_dir, $file, $lang);
1072  
1073 8 ($f) = $self->run_callbacks('rewrite_absolute_output_file_path', $fullpath, $lang);
1074 8 ▶ $fullpath = $f if $f;
Branch: 1 branch
BranchTrueFalseTotal
if $f 0 8 8
1075  
1076 8 return $self->{full_output_path_cache}->{$key} = $fullpath;
1077 }
1078  
1079 # this will save the new fullpath to the cache so that next calls to
1080 # get_full_output_path() will return this value
1081 sub set_full_output_path {
1082 0 ▶ my ($self, $file, $lang, $fullpath, $base_dir) = @_;
Subroutine set_full_output_path: not called CC=2 SCAR=17.9
Pod undocumented
1083  
1084 0 ▶ $base_dir = $self->{job}->{output_file_path} unless $base_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $base_dir 0 0 0
1085  
1086 0 my $key = join("\001", $file, $lang, $base_dir);
1087 0 return $self->{full_output_path_cache}->{$key} = $fullpath;
1088 }
1089  
1090 sub generate_ts_files {
1091 6 ▶ my ($self) = @_;
Subroutine generate_ts_files: called CC=3 SCAR=11.0
Pod undocumented
1092  
1093 # Skip generating translation files if we are in special output_only_mode
1094  
1095 6 ▶ return if $self->{job}->{output_only_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"job"}{"output_only_mode"} 0 6 6
1096  
1097 6 print "\nGenerating translation files...\n\n";
1098  
1099 6 foreach my $file (sort keys %{$self->{files}}) {
1100 8 $self->generate_ts_files_for_file($file);
1101 }
1102 }
1103  
1104 sub generate_ts_files_for_file {
1105 8 ▶ my ($self, $file) = @_;
Subroutine generate_ts_files_for_file: called CC=11 SCAR=32.2
Pod undocumented
1106  
1107 # Setting the global variables
1108 8 $self->{current_file_rel} = $file;
1109 8 $self->{current_file_id} = $self->{db}->get_file_id($self->{job}->{db_namespace}, $self->{job}->{id}, $file, 1); # do not create
1110  
1111 8 ▶ my $modified = !exists($self->{skip_flags}->{$file}) || $self->{rebuild_ts_files};
Condition33%1/3
!exists $self->{'skip_flags'}{$file} || $self->{'rebuild_ts_files'}
l!l&&r!l&&!r
800
MC/DC0%0/2
!exists $self->{'skip_flags'}{$file} || $self->{'rebuild_ts_files'}
!exists $self->{'skip_flags'}{$file} $self->{'rebuild_ts_files'}
Truth table33%1/3
not(A) || B
A= !exists $self->{'skip_flags'}{$file}B= $self->{'rebuild_ts_files'}
ABresult
1X1
011
000
1112  
1113 8 ▶ if ($self->{job}->{optimizations} && !$self->{rebuild_ts_files}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'job'}{'optimizations'} and not $self->{'rebuild_ts_files'}) { } 0 8 8
Condition33%1/3
$self->{"job"}{"optimizations"} and not $self->{"rebuild_ts_files"}
!ll&&!rl&&r
800
MC/DC0%0/2
$self->{"job"}{"optimizations"} and not $self->{"rebuild_ts_files"}
$self->{"job"}{"optimizations"} not $self->{"rebuild_ts_files"}
Truth table33%1/3
A and not(B)
A= $self->{"job"}{"optimizations"}B= not $self->{"rebuild_ts_files"}
ABresult
0X0
100
111
1114 0 ▶ my $reason = $self->{job}->{output_only_mode} ? 'differs from the original version' : 'modified';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'job'}{'output_only_mode'} ? : 0 0 0
1115 0 ▶ print "\t$file".($modified ? " ($reason)" : ' (not modified)')."\n";
Branch: 1 branch
BranchTrueFalseTotal
$modified ? : 0 0 0
1116 } else {
1117 8 print "\t$file (forced mode)\n";
1118 }
1119  
1120 # sanity check
1121  
1122 8 ▶ die "ERROR: CURRENT_FILE_ID is not defined\n" unless $self->{current_file_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"current_file_id"} 0 8 8
1123  
1124 8 my $target_langs;
1125 8 ▶ if (!$modified) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $modified) { } 0 8 8
1126 0 ▶ print "\t\t*** File wasn't changed, will walk through modified languages only\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1127 # note that this doesn't cover situations when there are some new translation files for new languages
1128 # that need to be generated; however, this should be taken care of in some higher-level logic
1129 # (i.e. if the job parameters are changed since last run, the full pass with no optimizations
1130 # should be performed)
1131 0 $target_langs = $self->{job}->{modified_languages};
1132 } else {
1133 8 ▶ print "\t\t*** File was changed, will walk through all destination languages\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 8 8
1134 8 $target_langs = $self->{job}->{destination_languages};
1135 }
1136  
1137 8 foreach my $lang (@$target_langs) {
1138 8 $self->generate_ts_files_for_file_lang($file, $lang);
1139 }
1140 }
1141  
1142 sub get_full_ts_file_path {
1143 16 ▶ my ($self, $file, $lang) = @_;
Subroutine get_full_ts_file_path: called CC=3 SCAR=11.1
Pod undocumented
1144  
1145 16 my $ts_file = $file;
1146  
1147 16 my ($f) = $self->run_callbacks('rewrite_relative_ts_file_path', $file, $lang);
1148 16 ▶ $ts_file = $f if $f;
Branch: 1 branch
BranchTrueFalseTotal
if $f 0 16 16
1149  
1150 16 my $fullpath = $self->{job}->get_full_ts_file_path($ts_file, $lang);
1151  
1152 16 ($f) = $self->run_callbacks('rewrite_absolute_ts_file_path', $fullpath, $lang);
1153 16 ▶ $fullpath = $f if $f;
Branch: 1 branch
BranchTrueFalseTotal
if $f 0 16 16
1154  
1155 16 return $fullpath;
1156 }
1157  
1158 sub generate_ts_files_for_file_lang {
1159 8 ▶ my ($self, $file, $lang) = @_;
Subroutine generate_ts_files_for_file_lang: called CC=33 SCAR=46.8
Pod undocumented
1160  
1161 # skip generating TS files for source language (it is added in `output_default_lang_file` mode implicitly)
1162 8 ▶ return if ($lang eq $self->{job}->{source_language});
Branch: 1 branch
BranchTrueFalseTotal
if $lang eq $self->{"job"}{"source_language"} 0 8 8
1163  
1164 8 my $result = combine_and(1, $self->run_callbacks('can_generate_ts_file', $file, $lang));
1165 8 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 8 8
1166 0 ▶ print "\t\tSkip generating TS file for $file:$lang because at least one callback returned 0\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1167 0 return;
1168 }
1169  
1170 8 my $fullpath = $self->get_full_ts_file_path($file, $lang);
1171  
1172 8 my $namespace = $self->{job}->{db_namespace};
1173  
1174 8 my $dir = dirname($fullpath);
1175  
1176 8 my $aref = $self->{files}->{$file};
1177  
1178 8 my @units;
1179 my %processed;
1180 8 my $count = 0;
1181  
1182 8 my $ts_items_count_key = "$self->{current_file_id}:$lang";
1183 8 my $count_prop_key = "ts:$self->{current_file_id}:$lang:count";
1184  
1185 # get the highest USN for the current language and all similar languages
1186 8 my $current_usn = $self->{db}->get_highest_usn_for_file_lang($self->{current_file_id}, $lang);
1187  
1188 map {
1189 0 my $usn = $self->{db}->get_highest_usn_for_file_lang($self->{current_file_id}, $_);
1190 0 ▶ $current_usn = $usn if $usn > $current_usn;
Branch: 1 branch
BranchTrueFalseTotal
if $usn > $current_usn 0 0 0
1191 8 } $self->{job}->gather_similar_languages_for_lang($lang);
1192  
1193 8 my $old_usn = $self->{db}->get_property("usn:$self->{current_file_id}:$lang");
1194  
1195 # regenerate translation file if either:
1196 # a) optimizations are disabled (job in a forced mode)
1197 # b) rebuild_ts_files option is turned on
1198 # c) target file is missing
1199 # d) translations or items for the file have changed (based on translations' and items' highest usn value)
1200 8 ▶ my $need_generate_ts_file = !$self->{job}->{optimizations} || $self->{rebuild_ts_files} || !-f $fullpath || ($current_usn ne $old_usn);
Condition33%3/9
!$self->{'job'}{'optimizations'} || $self->{'rebuild_ts_files'} || !(-f $fullpath) || $current_usn ne $old_usn
l!l&&r!l&&!r
800
not $self->{"job"}{"optimizations"} or $self->{"rebuild_ts_files"} or not -f $fullpath
l!l&&r!l&&!r
800
not $self->{"job"}{"optimizations"} or $self->{"rebuild_ts_files"}
l!l&&r!l&&!r
800
MC/DC0%0/4
!$self->{'job'}{'optimizations'} || $self->{'rebuild_ts_files'} || !(-f $fullpath) || $current_usn ne $old_usn
not $self->{"job"}{"optimizations"} $self->{"rebuild_ts_files"} not -f $fullpath $current_usn ne $old_usn
Truth table20%1/5
not(A) or B or not(C) || D
A= not $self->{"job"}{"optimizations"}B= $self->{"rebuild_ts_files"}C= not -f $fullpathD= $current_usn ne $old_usn
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1201  
1202 # also, if the translations have changed, set additionally a force flag on a specific file:lang combo
1203 8 ▶ if ($current_usn ne $old_usn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_usn ne $old_usn) 8 0 8
1204 8 $self->{force_flags}->{"$self->{current_file_id}.$lang"} = 1;
1205 }
1206  
1207 8 ▶ if (!$need_generate_ts_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($need_generate_ts_file) 0 8 8
1208 0 my $old_ts_items_count = $self->{db}->get_property($count_prop_key);
1209 0 ▶ if (defined $old_ts_items_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_ts_items_count) 0 0 0
1210 0 $self->{ts_items_count}->{$ts_items_count_key} = $old_ts_items_count;
1211 0 ▶ print "\t\tSkip generating $fullpath: translations didn't change\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1212 0 return;
1213 }
1214 }
1215  
1216 8 foreach my $item_id (@$aref) {
1217  
1218 # .po files do not allow duplicate keys,
1219 # so skip the keys which were already processed
1220  
1221 54 ▶ if ($processed{$item_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($processed{$item_id}) 0 54 54
1222 0 print "WARNING: duplicate item id $item_id\n";
1223 0 next;
1224 }
1225  
1226 # Getting the properties of the item and its linked string
1227  
1228 54 my $item_props = $self->{db}->get_item_props($item_id);
1229 54 my $string_id = $item_props->{string_id};
1230 54 my $string_props = $self->{db}->get_string_props($string_id);
1231  
1232 # Skip the item if it is marked as skipped
1233 54 ▶ next if ($string_props->{skip});
Branch: 1 branch
BranchTrueFalseTotal
if $string_props->{"skip"} 0 54 54
1234  
1235 54 my $string = $string_props->{string};
1236 54 my $context = $string_props->{context};
1237 54 my $key = generate_key($string, $context);
1238 54 my $source_key = $self->{current_file_keys}->{$key};
1239  
1240 54 my $hint = $item_props->{hint};
1241 54 my $item_comment = $item_props->{comment};
1242  
1243 54 my $result = combine_and(1, $self->run_callbacks('can_translate', $file, $lang, \$string, \$hint));
1244 54 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 54 54
1245 0 ▶ print "\t\tSkip publishing string '$string' in translation file because at least one callback returned 0\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1246 0 next;
1247 }
1248  
1249 # mark the key as processed
1250  
1251 54 $processed{$item_id} = 1;
1252  
1253 # increment item counter
1254 54 $count++;
1255  
1256 # Get translated string; for default language, the returned string will be equal
1257 # to the original string (i.e. all strings in the default language are already 'translated')
1258  
1259 54 my ($translation, $fuzzy, $comments) = $self->get_translation($string, $context, $namespace,
1260 $file, $lang, undef, $item_id, $source_key);
1261  
1262 54 my @hint_lines;
1263  
1264 54 ▶ push @hint_lines, $hint if ($hint ne '') && ($hint ne $string);
Branch: 1 branch
BranchTrueFalseTotal
if $hint ne "" and $hint ne $string 54 0 54
Condition33%1/3
$hint ne "" and $hint ne $string
!ll&&!rl&&r
0054
MC/DC0%0/2
$hint ne "" and $hint ne $string
$hint ne "" $hint ne $string
Truth table33%1/3
A and B
A= $hint ne ""B= $hint ne $string
ABresult
0X0
100
111
1265  
1266 # run callbacks that might want to modify the hint (developer comment)
1267 54 ▶ if ($self->{job}->has_callbacks('add_dev_comment')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'job'}->has_callbacks('add_dev_comment')) { } 0 54 54
1268 0 print "Deprecation notice: add_dev_comment phase is deprecated. Use add_hint phase instead\n";
1269 0 $self->run_callbacks('add_dev_comment', $file, $lang, \$string, \@hint_lines);
1270 } else {
1271 54 $self->run_callbacks(
1272 'add_hint',
1273 $string, $context, $namespace, $file, $source_key, $lang, \@hint_lines
1274 );
1275 }
1276  
1277 54 ▶ if ($item_comment ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item_comment ne "") 0 54 54
1278 0 ▶ push @hint_lines, '' if @hint_lines > 0; # add extra line break between hint and extra item comment
Branch: 1 branch
BranchTrueFalseTotal
if @hint_lines > 0 0 0 0
1279 0 push @hint_lines, $item_comment;
1280 }
1281  
1282 54 my $hint = join("\n", @hint_lines);
1283  
1284 # push the unit to the array
1285  
1286 54 push @units, {
1287 key => $key,
1288 source => $string,
1289 context => $context,
1290 target => $translation,
1291 comment => $comments, # translator's comments
1292 fuzzy => $fuzzy,
1293 hint => $hint, # developer's comments
1294 };
1295 } # foreach key
1296  
1297 # serialize the file
1298  
1299 8 my $text;
1300 8 eval {
1301 8 $text = $self->{job}->{serializer_object}->serialize(\@units, $file, $lang);
1302 };
1303  
1304 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
1305 0 print "\t\tWARNING: File serializing failed; the file will not be saved\n";
1306 0 print "\t\tReason: $@\n";
1307 0 return undef;
1308 }
1309  
1310 8 $self->run_callbacks('after_serialize_ts_file', $file, \$text);
1311  
1312 # update translation file item counter property
1313  
1314 8 $self->{ts_items_count}->{$ts_items_count_key} = $count;
1315  
1316 # update translation file item counter in database
1317  
1318 8 my $old_ts_items_count = $self->{db}->get_property($count_prop_key);
1319 8 ▶ if ($count ne $old_ts_items_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count ne $old_ts_items_count) 8 0 8
1320 8 $self->{db}->set_property($count_prop_key, $count);
1321 }
1322  
1323 # checking translation file hash
1324 8 my $current_hash = generate_hash($text);
1325 8 my $old_hash = $self->{db}->get_property("ts:$self->{current_file_id}:$lang");
1326  
1327 # save translation file if either:
1328 # a) optimizations are disabled (job in a forced mode)
1329 # b) rebuild_ts_files option is turned on
1330 # c) target file is missing
1331 # d) file hash has changed
1332 8 ▶ my $need_save_ts_file = !$self->{job}->{optimizations} || $self->{rebuild_ts_files} || !-f $fullpath || ($current_hash ne $old_hash);
Condition33%3/9
!$self->{'job'}{'optimizations'} || $self->{'rebuild_ts_files'} || !(-f $fullpath) || $current_hash ne $old_hash
l!l&&r!l&&!r
800
not $self->{"job"}{"optimizations"} or $self->{"rebuild_ts_files"} or not -f $fullpath
l!l&&r!l&&!r
800
not $self->{"job"}{"optimizations"} or $self->{"rebuild_ts_files"}
l!l&&r!l&&!r
800
MC/DC0%0/4
!$self->{'job'}{'optimizations'} || $self->{'rebuild_ts_files'} || !(-f $fullpath) || $current_hash ne $old_hash
not $self->{"job"}{"optimizations"} $self->{"rebuild_ts_files"} not -f $fullpath $current_hash ne $old_hash
Truth table20%1/5
not(A) or B or not(C) || D
A= not $self->{"job"}{"optimizations"}B= $self->{"rebuild_ts_files"}C= not -f $fullpathD= $current_hash ne $old_hash
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1333  
1334 8 ▶ if ($need_save_ts_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_save_ts_file) { } 8 0 8
1335 # we need to save even empty translation files with no translatables
1336 # because otherwise it will lead to stale files with old translatables
1337  
1338 8 eval { mkpath($dir) };
1339 8 ▶ die "ERROR: Couldn't create $dir: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
1340  
1341 8 print "\t\tSaving $fullpath";
1342 8 ▶ open(TS, ">$fullpath") || die "ERROR: Can't write to [$fullpath]: $!";
Branch: 1 branch
BranchTrueFalseTotal
open TS, ">$fullpath" or die "ERROR: Can't write to [$fullpath]: $!" 0 8 8
1343 8 binmode(TS);
1344 8 print TS encode_utf8($text); # encode manually to avoid 'Wide character in print' warnings and force Unix-style endings
1345 8 close(TS);
1346 8 my $size = -s $fullpath;
1347 8 print " ($size bytes)\n";
1348 } else {
1349 0 ▶ print "\t\tSkip saving $fullpath: content didn't change\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1350 }
1351  
1352 # get the highest USN for the current language and all similar languages
1353 #
1354 # (we need to do it again because the usn may have been bumped up
1355 # as some translations may have been saved to the database)
1356 #
1357 # NOTE: this is only supposed to work under assumption that no other process
1358 # writes to the database during TS file generation; otherwise we will be 'forgetting'
1359 # about all these changes and not pushing them to a TS file on next run
1360 8 $current_usn = $self->{db}->get_highest_usn_for_file_lang($self->{current_file_id}, $lang);
1361  
1362 8 ▶ if ($current_usn ne $old_usn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_usn ne $old_usn) 8 0 8
1363 8 $self->{db}->set_property("usn:$self->{current_file_id}:$lang", $current_usn);
1364 }
1365 8 ▶ if ($current_hash ne $old_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_hash ne $old_hash) 8 0 8
1366 8 $self->{db}->set_property("ts:$self->{current_file_id}:$lang", $current_hash);
1367 }
1368 }
1369  
1370 sub generate_localized_files {
1371 6 ▶ my ($self) = @_;
Subroutine generate_localized_files: called CC=3 SCAR=11.0
Pod undocumented
1372  
1373 6 ▶ return unless $self->{job}->{output_lang_files};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"job"}{"output_lang_files"} 0 6 6
1374  
1375 6 print "\nGenerating localized files...\n\n";
1376  
1377 6 foreach my $file (sort keys %{$self->{files}}) {
1378 8 $self->generate_localized_files_for_file($file);
1379 }
1380  
1381 6 print "\n";
1382 }
1383  
1384 sub generate_localized_files_for_file {
1385 8 ▶ my ($self, $file) = @_;
Subroutine generate_localized_files_for_file: called CC=9 SCAR=28.0
Pod undocumented
1386  
1387 # Setting the global variables
1388  
1389 8 $self->{current_file_rel} = $file;
1390 8 $self->{current_file_id} = $self->{db}->get_file_id($self->{job}->{db_namespace}, $self->{job}->{id}, $self->{current_file_rel}, 1); # do not create
1391  
1392 8 my $modified = !exists $self->{skip_flags}->{$file};
1393  
1394 8 ▶ if ($self->{job}->{optimizations}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'job'}{'optimizations'}) { } 0 8 8
1395 0 ▶ my $reason = $self->{job}->{output_only_mode} ? 'differs from the original version' : 'modified';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'job'}{'output_only_mode'} ? : 0 0 0
1396 0 ▶ print "\t$file".($modified ? " ($reason)" : ' (not modified)')."\n";
Branch: 1 branch
BranchTrueFalseTotal
$modified ? : 0 0 0
1397 } else {
1398 8 print "\t$file (forced mode)\n";
1399 }
1400  
1401 # sanity check
1402  
1403 8 ▶ die "ERROR: CURRENT_FILE_ID is not defined\n" unless $self->{current_file_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"current_file_id"} 0 8 8
1404  
1405 8 my $target_langs;
1406 8 ▶ if (!$modified) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $modified) { } 0 8 8
1407 0 ▶ print "\t\t*** File wasn't changed, will walk through modified languages only\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1408 # note that this doesn't cover situations when there are some new localized files for new languages
1409 # have to be generated; however, this should be taken care of in some higher-level logic
1410 # (i.e. if the job parameters are changed since last run, the full pass with no optimizations
1411 # should be performed)
1412 0 $target_langs = $self->{job}->{modified_languages};
1413 } else {
1414 8 ▶ print "\t\t*** File was changed, will walk through all destination languages\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 8 8
1415 8 $target_langs = $self->{job}->{destination_languages};
1416 }
1417  
1418 8 foreach my $lang (@$target_langs) {
1419 8 $self->generate_localized_files_for_file_lang($file, $lang);
1420 }
1421 }
1422  
1423 sub generate_localized_files_for_file_lang {
1424 8 ▶ my ($self, $file, $lang) = @_;
Subroutine generate_localized_files_for_file_lang: called CC=39 SCAR=57.2
Pod undocumented
1425  
1426 8 my $result = combine_and(1, $self->run_callbacks('can_generate_localized_file', $file, $lang));
1427  
1428 # get full output path only *after* the 'can_generate_localized_file' callbacks has finished,
1429 # since the path could have been modified there
1430  
1431 8 my $srcpath = $self->get_full_source_path($file);
1432 8 my $fullpath = $self->get_full_output_path($file, $lang);
1433 8 my $dir = dirname($fullpath);
1434  
1435 8 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 8 8
1436 0 ▶ print "\t\tSkip generating $fullpath because at least one callback returned 0\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1437 0 return;
1438 }
1439  
1440 # Always construct the key with the JOB id to disambiguate the keys from multiple jobs
1441 # working in output_only_mode and sharing the same namespace
1442  
1443 8 my $filekey = "$self->{current_file_id}:$self->{job}->{id}";
1444  
1445 8 my $source_hash = $self->{src_hash}->{$self->{current_file_rel}};
1446 8 ▶ die "source_hash is undefined" unless $source_hash ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless $source_hash ne "" 0 8 8
1447 # for source language we don't parse translation file, so get_property would return an empty string;
1448 # DB has a constraint: property value should not be empty. So for the source language we return a dummy non-empty string
1449 8 ▶ my $source_ts_file_hash = ($lang eq $self->{job}->{source_language}) ? '-' : $self->{db}->get_property("ts:$self->{current_file_id}:$lang");
Branch: 1 branch
BranchTrueFalseTotal
$lang eq $self->{'job'}{'source_language'} ? : 0 8 8
1450  
1451 8 my $file_exists = -f $fullpath;
1452 8 ▶ my $current_mtime = file_mtime($fullpath) if $file_exists;
Branch: 1 branch
BranchTrueFalseTotal
if $file_exists 0 8 8
1453 8 my $old_mtime = $self->{db}->get_property("target:mtime:$filekey:$lang");
1454  
1455 # the force flag will be absent if the source file was skipped,
1456 # so there is no need to check the skip flag here; this optimization will
1457 # work automatically
1458  
1459 8 ▶ if ($self->{force_flags}->{"$self->{current_file_id}.$lang"} == 0) { # if not forced
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"force_flags"}{qq[$self->{"current_file_id"}.$lang]} == 0) 0 8 8
1460 0 ▶ if ($self->{job}->{optimizations}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"job"}{"optimizations"} and $file_exists and $current_mtime eq $old_mtime and $source_hash eq $self->{"db"}->get_property("source:${filekey}:$lang") and $source_ts_file_hash eq $self->{"db"}->get_property("source:ts:${filekey}:$lang")) 0 0 0
Condition0%0/12
$self->{"job"}{"optimizations"} and $file_exists and $current_mtime eq $old_mtime and $source_hash eq $self->{"db"}->get_property("source:${filekey}:$lang") and $source_ts_file_hash eq $self->{"db"}->get_property("source:ts:${filekey}:$lang")
!ll&&!rl&&r
000
$self->{"job"}{"optimizations"} and $file_exists and $current_mtime eq $old_mtime and $source_hash eq $self->{"db"}->get_property("source:${filekey}:$lang")
!ll&&!rl&&r
000
$self->{"job"}{"optimizations"} and $file_exists and $current_mtime eq $old_mtime
!ll&&!rl&&r
000
$self->{"job"}{"optimizations"} and $file_exists
!ll&&!rl&&r
000
MC/DC0%0/5
$self->{"job"}{"optimizations"} and $file_exists and $current_mtime eq $old_mtime and $source_hash eq $self->{"db"}->get_property("source:${filekey}:$lang") and $source_ts_file_hash eq $self->{"db"}->get_property("source:ts:${filekey}:$lang")
$self->{"job"}{"optimizations"} $file_exists $current_mtime eq $old_mtime $source_hash eq $self->{"db"}->get_property("source:${filekey}:$lang") $source_ts_file_hash eq $self->{"db"}->get_property("source:ts:${filekey}:$lang")
Truth table0%0/6
A and B and C and D and E
A= $self->{"job"}{"optimizations"}B= $file_existsC= $current_mtime eq $old_mtimeD= $source_hash eq $self->{"db"}->get_property("source:${filekey}:$lang")E= $source_ts_file_hash eq $self->{"db"}->get_property("source:ts:${filekey}:$lang")
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
1461 and $file_exists
1462 and ($current_mtime eq $old_mtime)
1463 and ($source_hash eq $self->{db}->get_property("source:$filekey:$lang"))
1464 and ($source_ts_file_hash eq $self->{db}->get_property("source:ts:$filekey:$lang"))
1465 ) {
1466 0 ▶ print "\t\tSkip generating $fullpath because source file and translations did not change, target file exists and has the same modification time\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1467 0 return;
1468 }
1469 }
1470  
1471 8 my ($src) = $self->read_file($srcpath);
1472  
1473 8 $result = combine_and(1, $self->run_callbacks('can_generate_localized_file_source', $file, $lang, \$src));
1474 8 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 8 8
1475 0 ▶ print "\t\tSkip processing $srcpath because at least one callback returned 0\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1476 0 return;
1477 }
1478  
1479 8 $self->run_callbacks('after_load_source_file_for_generating', $srcpath, $lang, \$src);
1480  
1481 8 $self->clear_disambiguation_cache;
1482  
1483 8 my $out;
1484 8 eval {
1485 8 ▶ $out = $self->{job}->{parser_object}->parse(\$src, sub { $self->generate_localized_files_for_file_lang_callback(@_) }, $lang);
Subroutine __ANON__: called CC=1 SCAR=0.0
1486 };
1487  
1488 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
1489 0 print "\t\tWARNING: Localized file generation failed; the file will not be saved\n";
1490 0 print "\t\tReason: $@\n";
1491 0 return undef;
1492 }
1493  
1494 8 $result = combine_and(1, $self->run_callbacks('can_save_localized_file', $file, $lang, \$out));
1495 8 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 8 8
1496 0 print "\t\tSkip saving $fullpath because at least one callback returned 0\n";# if $self->{debug};
1497 0 return;
1498 }
1499  
1500 8 $self->run_callbacks('before_save_localized_file', $file, $lang, \$out);
1501  
1502 8 my $enc = $self->{job}->{output_encoding};
1503 8 my $text;
1504  
1505 # print BOM
1506 8 ▶ if ($self->{job}->{output_bom}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"job"}{"output_bom"}) 0 8 8
1507 0 ▶ $text = "\xFF\xFE" if (uc($enc) eq 'UTF-16LE');
Branch: 1 branch
BranchTrueFalseTotal
if uc $enc eq "UTF-16LE" 0 0 0
1508 0 ▶ $text = "\xFE\xFF" if ((uc($enc) eq 'UTF-16BE') || (uc($enc) eq 'UTF-16'));
Branch: 1 branch
BranchTrueFalseTotal
if uc $enc eq "UTF-16BE" or uc $enc eq "UTF-16" 0 0 0
Condition0%0/3
uc $enc eq "UTF-16BE" or uc $enc eq "UTF-16"
l!l&&r!l&&!r
000
MC/DC0%0/2
uc $enc eq "UTF-16BE" or uc $enc eq "UTF-16"
uc $enc eq "UTF-16BE" uc $enc eq "UTF-16"
Truth table0%0/3
A or B
A= uc $enc eq "UTF-16BE"B= uc $enc eq "UTF-16"
ABresult
1X1
011
000
1509 0 ▶ $text = "\xFF\xFE\x00\x00" if (uc($enc) eq 'UTF-32LE');
Branch: 1 branch
BranchTrueFalseTotal
if uc $enc eq "UTF-32LE" 0 0 0
1510 0 ▶ $text = "\x00\x00\xFE\xFF" if ((uc($enc) eq 'UTF-32BE') || (uc($enc) eq 'UTF-32'));
Branch: 1 branch
BranchTrueFalseTotal
if uc $enc eq "UTF-32BE" or uc $enc eq "UTF-32" 0 0 0
Condition0%0/3
uc $enc eq "UTF-32BE" or uc $enc eq "UTF-32"
l!l&&r!l&&!r
000
MC/DC0%0/2
uc $enc eq "UTF-32BE" or uc $enc eq "UTF-32"
uc $enc eq "UTF-32BE" uc $enc eq "UTF-32"
Truth table0%0/3
A or B
A= uc $enc eq "UTF-32BE"B= uc $enc eq "UTF-32"
ABresult
1X1
011
000
1511 0 ▶ $text = "\xEF\xBB\xBF" if (uc($enc) eq 'UTF-8');
Branch: 1 branch
BranchTrueFalseTotal
if uc $enc eq "UTF-8" 0 0 0
1512 }
1513  
1514 # append content
1515 8 $text .= encode($enc, $out);
1516  
1517 8 my $current_hash = generate_hash($text);
1518 8 my $old_hash = $self->{db}->get_property("target:$filekey:$lang");
1519  
1520 8 ▶ if ($self->{job}->{optimizations}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'job'}{'optimizations'} and $file_exists and $current_hash eq $old_hash and $current_mtime eq $old_mtime) { } 0 8 8
Condition33%3/9
$self->{"job"}{"optimizations"} and $file_exists and $current_hash eq $old_hash and $current_mtime eq $old_mtime
!ll&&!rl&&r
800
$self->{"job"}{"optimizations"} and $file_exists and $current_hash eq $old_hash
!ll&&!rl&&r
800
$self->{"job"}{"optimizations"} and $file_exists
!ll&&!rl&&r
800
MC/DC0%0/4
$self->{"job"}{"optimizations"} and $file_exists and $current_hash eq $old_hash and $current_mtime eq $old_mtime
$self->{"job"}{"optimizations"} $file_exists $current_hash eq $old_hash $current_mtime eq $old_mtime
Truth table20%1/5
A and B and C and D
A= $self->{"job"}{"optimizations"}B= $file_existsC= $current_hash eq $old_hashD= $current_mtime eq $old_mtime
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1521 and $file_exists
1522 and ($current_hash eq $old_hash)
1523 and ($current_mtime eq $old_mtime)) {
1524 0 ▶ print "\t\tSkip saving $fullpath: content hash and file modification time are the same\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1525 } else {
1526 8 my @reasons;
1527  
1528 8 ▶ if (!$self->{job}->{optimizations}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'job'}{'optimizations'}) { } 8 0 8
1529 8 push @reasons, 'forced mode is on';
1530 } else {
1531 0 ▶ if (!$file_exists) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $file_exists) { } 0 0 0
elsif (not $old_hash) { } 0 0 0
1532 0 push @reasons, 'file didn\'t exist before';
1533 } elsif (!$old_hash) {
1534 0 push @reasons, 'file was never saved before';
1535 } else {
1536 0 ▶ push @reasons, 'content changed' if $current_hash ne $old_hash;
Branch: 1 branch
BranchTrueFalseTotal
if $current_hash ne $old_hash 0 0 0
1537 0 ▶ push @reasons, 'file was modified on disk' if $current_mtime ne $old_mtime;
Branch: 1 branch
BranchTrueFalseTotal
if $current_mtime ne $old_mtime 0 0 0
1538 }
1539 }
1540  
1541 8 eval { mkpath($dir) };
1542 8 ▶ die "Couldn't create $dir: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
1543  
1544 # Writing the entire file
1545 8 print "\t\tSaving $fullpath, because ".join(', ', @reasons);
1546 8 ▶ open(OUT, ">$fullpath") || die "Can't write to [$fullpath]: $!";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$fullpath" or die "Can't write to [$fullpath]: $!" 0 8 8
1547 8 binmode(OUT);
1548 8 print OUT $text;
1549 8 close(OUT);
1550 8 my $size = -s $fullpath;
1551 8 print " ($size bytes)\n";
1552  
1553 8 $self->run_callbacks('on_localized_file_change', $file, $lang, \$text);
1554  
1555 8 $self->{db}->set_property("target:$filekey:$lang", $current_hash);
1556 8 $self->{db}->set_property("target:mtime:$filekey:$lang", file_mtime($fullpath)); # read mtime again since the file was saved
1557 }
1558  
1559 # $source_hash and $source_ts_file_hash might be undefined if the translation file has not been parsed or saved yet before
1560 # (in e.g. `output_only_mode` where we skip saving any translation files)
1561  
1562 8 ▶ $self->{db}->set_property("source:$filekey:$lang", $source_hash) if $source_hash ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $source_hash ne "" 8 0 8
1563 8 ▶ $self->{db}->set_property("source:ts:$filekey:$lang", $source_ts_file_hash) if $source_ts_file_hash ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $source_ts_file_hash ne "" 8 0 8
1564  
1565 8 $self->run_callbacks('after_save_localized_file', $file, $lang, \$text);
1566 }
1567  
1568 sub generate_localized_files_for_file_lang_callback {
1569 54 ▶ my ($self, $string, $context, $hint, $flagsref, $lang, $key) = @_;
Subroutine generate_localized_files_for_file_lang_callback: called CC=32 SCAR=50.8
Pod undocumented
1570  
1571 # Normalize parameters
1572  
1573 54 my $norm = $self->{job}->{normalize_strings};
1574 54 ▶ $norm = undef if is_flag_set($flagsref, 'dont-normalize');
Branch: 1 branch
BranchTrueFalseTotal
if is_flag_set($flagsref, "dont-normalize") 0 54 54
1575 54 ▶ $norm = 1 if is_flag_set($flagsref, 'normalize');
Branch: 1 branch
BranchTrueFalseTotal
if is_flag_set($flagsref, "normalize") 0 54 54
1576  
1577 54 ▶ if ($norm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($norm) 0 54 54
1578 0 normalize_strref(\$string);
1579 }
1580  
1581 54 ▶ if ($string eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq "") 0 54 54
1582 0 ▶ print "::skipping empty string\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1583 0 return $string;
1584 }
1585  
1586 54 ▶ $string = NFC($string) if ($string =~ m/[^\x00-\x7F]/);
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /[^\x00-\x7F]/ 0 54 54
1587 54 ▶ $context = NFC($context) if ($context =~ m/[^\x00-\x7F]/);
Branch: 1 branch
BranchTrueFalseTotal
if $context =~ /[^\x00-\x7F]/ 0 54 54
1588 54 ▶ $hint = NFC($hint) if ($hint =~ m/[^\x00-\x7F]/);
Branch: 1 branch
BranchTrueFalseTotal
if $hint =~ /[^\x00-\x7F]/ 0 54 54
1589  
1590 54 $self->run_callbacks('rewrite_source', $self->{current_file_rel}, $lang, \$string, \$hint);
1591  
1592 # normalize once again, in case the string was changed
1593 54 ▶ $string = NFC($string) if ($string =~ m/[^\x00-\x7F]/);
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /[^\x00-\x7F]/ 0 54 54
1594  
1595 54 $context = $self->disambiguate_string($string, $context, $key, $hint);
1596  
1597 54 my $result = combine_and(1, $self->run_callbacks('can_extract', $self->{current_file_rel}, $lang, \$string, \$hint, $context, $key));
1598 54 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 54 54
1599 0 ▶ print "\t\tSkip extracting string '$string' because at least one callback returned 0\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1600 0 ▶ return $self->{job}->{leave_untranslated_blank} ? '' : $string;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'job'}{'leave_untranslated_blank'} ? : 0 0 0
1601 }
1602  
1603 54 $result = combine_and(1, $self->run_callbacks('can_translate', $self->{current_file_rel}, $lang, \$string, \$hint));
1604 54 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 54 54
1605 0 ▶ print "\t\tSkip translating string '$string' because at least one callback returned 0\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1606 0 ▶ return $self->{job}->{leave_untranslated_blank} ? '' : $string;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'job'}{'leave_untranslated_blank'} ? : 0 0 0
1607 }
1608  
1609 # get (do not create) the string record for given string/context
1610  
1611 54 my $string_id = $self->{db}->get_string_id($string, $context, 1); # do not create
1612 54 ▶ my $item_id = $self->{db}->get_item_id($self->{current_file_id}, $string_id, undef, 1) if $string_id; # do not create
Branch: 1 branch
BranchTrueFalseTotal
if $string_id 54 0 54
1613  
1614 # sanity checks: the string id and item id should be already defined if we got to this point
1615  
1616 54 ▶ if ((!$string_id || !$item_id) && $self->{job}->{output_only_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $string_id && $item_id and $self->{"job"}{"output_only_mode"}) 0 54 54
Condition33%2/6
not $string_id && $item_id and $self->{"job"}{"output_only_mode"}
!ll&&!rl&&r
5400
$string_id && $item_id
!ll&&!rl&&r
0054
MC/DC0%0/3
not $string_id && $item_id and $self->{"job"}{"output_only_mode"}
$string_id $item_id $self->{"job"}{"output_only_mode"}
Truth table20%1/5
not(A && B) and C
A= $string_idB= $item_idC= $self->{"job"}{"output_only_mode"}
ABCresult
11X0
0X00
1000
0X11
1011
1617 0 print "\t\tWARNING: String '$string' is not registered in the database\n";
1618 }
1619  
1620 54 ▶ if (!$self->{job}->{output_only_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"job"}{"output_only_mode"}) 54 0 54
1621 54 ▶ if (!$string_id) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $string_id) { } 0 54 54
elsif (not $item_id) { } 0 54 54
1622 0 ▶ print "\t\t? [string_id] not defined for string='$string', context='$context' (potential problem with the parser?)\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1623 } elsif (!$item_id) {
1624 0 ▶ print "\t\t? [item_id] not defined for string_id $string_id\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1625 }
1626 }
1627  
1628 54 ▶ print "::[$item_id] >> [$self->{current_file_rel}]::[$string], [$context], [$hint], [$lang], [$key]\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 54 54
1629  
1630 54 my ($translation, $fuzzy) = $self->get_translation($string, $context, $self->{job}->{db_namespace}, $self->{current_file_rel}, $lang, undef, $item_id, $key);
1631  
1632 54 ▶ print "::[$item_id] << [$translation, $fuzzy]\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 54 54
1633 54 ▶ if ($translation eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($translation eq "") 28 26 54
1634 28 ▶ $translation = $self->{job}->{leave_untranslated_blank} ? '' : $string;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'job'}{'leave_untranslated_blank'} ? : 0 28 28
1635 }
1636  
1637 54 ▶ if (combine_or($self->run_callbacks('rewrite_translation', $self->{current_file_rel}, $lang, \$translation))) {
Branch: 1 branch
BranchTrueFalseTotal
if (combine_or($self->run_callbacks("rewrite_translation", $self->{"current_file_rel"}, $lang, \$translation))) 0 54 54
1638 # if any of the rewrite_translation plugins returned a true value, normalize the output
1639 0 ▶ $translation = '' if !defined $translation; # convert to a string for consistency
Branch: 1 branch
BranchTrueFalseTotal
unless defined $translation 0 0 0
1640 0 $translation = NFC($translation);
1641 }
1642  
1643 54 ▶ if (is_flag_set($flagsref, 'pad')) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_flag_set($flagsref, "pad")) 0 54 54
1644 0 my $n = $flagsref->[get_flag_pos($flagsref, 'pad') + 1]; # get the first param
1645 0 $translation = sprintf('%*s', -$n, $translation); # pad with spaces (spaces are appended to the end)
1646 }
1647  
1648 $self->run_callbacks('log_translation',
1649 $string, $context, $hint, $flagsref, $lang, $key, $translation,
1650 $self->{job}->{db_namespace}, $self->{current_file_rel}
1651 54 );
1652  
1653 54 return $translation;
1654 }
1655  
1656 sub get_translation { # either from cache or from database
1657 108 ▶ my ($self, $string, $context, $namespace, $filepath, $lang, $disallow_similar_lang, $item_id, $key) = @_;
Subroutine get_translation: called CC=8 SCAR=26.2
Pod undocumented
1658  
1659 # For source language, return the original string immediately ("exact match")
1660  
1661 108 ▶ if ($lang eq $self->{job}->{source_language}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang eq $self->{"job"}{"source_language"}) 0 108 108
1662 0 return $string;
1663 }
1664  
1665 108 my ($translation, $fuzzy, $comment, $need_save) =
1666 $self->internal_get_translation($string, $context, $namespace, $filepath, $lang, $disallow_similar_lang, $item_id, $key);
1667  
1668 # if need_save flag is set, this means that translation comes from a fuzzy match
1669 # and it needs to be applied back to the database; note that if disallow_similar_lang
1670 # flag set (in other words, when getting the translation from a similar language)
1671 # we do not save the translation (but pass the need_save flag status back to the caller)
1672  
1673 108 ▶ if ($need_save && !$disallow_similar_lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_save and not $disallow_similar_lang) 52 56 108
Condition66%2/3
$need_save and not $disallow_similar_lang
!ll&&!rl&&r
56052
MC/DC50%1/2
$need_save and not $disallow_similar_lang
$need_save not $disallow_similar_lang
Truth table66%2/3
A and not(B)
A= $need_saveB= not $disallow_similar_lang
ABresult
0X0
100
111
1674 # Saving the updated translation
1675 52 ▶ if ($item_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item_id) { } 52 0 52
1676 52 print "\t\t[$item_id/$lang] $translation\n";
1677 52 $self->{db}->set_translation($item_id, $lang, $translation, $fuzzy, $comment);
1678 } else {
1679 0 ▶ if (!$self->{job}->{output_only_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"job"}{"output_only_mode"}) 0 0 0
1680 0 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
1681 0 print "\t\t? [need_save, but item_id not defined]\n";
1682 0 print "::>> string=[$string], context=[$context], namespace=[$namespace], filepath=[$filepath], lang=[$lang], disallow_similar_lang=[$disallow_similar_lang], item_id=[$item_id], key=[$key])\n";
1683 0 print "::<< translation=[$translation], fuzzy=[$fuzzy], comment=[$comment], need_save=[$need_save]\n";
1684 }
1685 }
1686 }
1687 }
1688  
1689 # return translation
1690  
1691 108 ▶ return ($translation, $translation ne '' ? $fuzzy : undef, $comment, $need_save);
Branch: 1 branch
BranchTrueFalseTotal
$translation ne '' ? : 52 56 108
1692 }
1693  
1694 sub internal_get_translation { # from database
1695 108 ▶ my ($self, $string, $context, $namespace, $filepath, $lang, $disallow_similar_lang, $item_id, $key) = @_;
Subroutine internal_get_translation: called CC=29 SCAR=49.5
Pod undocumented
1696  
1697 # try to get translation by calling registered plugin callbacks
1698 # (phase 1, before even looking for existing translations)
1699 108 my ($translation, $fuzzy, $comment, $need_save) = $self->run_callbacks(
1700 'get_translation_pre',
1701 $string, $context, $namespace, $filepath, $lang, $disallow_similar_lang, $item_id, $key
1702 );
1703 108 ▶ $translation = NFC($translation) if $translation ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $translation ne "" 52 56 108
1704 108 ▶ return ($translation, $fuzzy, $comment, $need_save) if ($translation ne '' || $comment ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $translation ne "" or $comment ne "" 52 56 108
Condition66%2/3
$translation ne "" or $comment ne ""
l!l&&r!l&&!r
52056
MC/DC50%1/2
$translation ne "" or $comment ne ""
$translation ne "" $comment ne ""
Truth table66%2/3
A or B
A= $translation ne ""B= $comment ne ""
ABresult
1X1
011
000
1705  
1706 # Find exact match for given namespace, file path and context
1707  
1708 56 ($translation, $fuzzy, $comment, my $merge, my $skip) = $self->{db}->get_translation($item_id, $lang, 1); # allow skip
1709 56 ▶ return if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 0 56 56
1710 56 ▶ return ($translation, $fuzzy, $comment, undef) if ($translation ne '' || $comment ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $translation ne "" or $comment ne "" 0 56 56
Condition33%1/3
$translation ne "" or $comment ne ""
l!l&&r!l&&!r
0056
MC/DC0%0/2
$translation ne "" or $comment ne ""
$translation ne "" $comment ne ""
Truth table33%1/3
A or B
A= $translation ne ""B= $comment ne ""
ABresult
1X1
011
000
1711  
1712 # check what the best translation is and if there are multiple translations
1713 # in the database for this exact string
1714  
1715 # try to search for best translation in current language
1716 56 ▶ if ($self->{job}->{reuse_translations}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"job"}{"reuse_translations"}) 56 0 56
1717 # Find the best match from other files or namespaces
1718 my ($translation, $fuzzy, $comment, $multiple_variants) = $self->{db}->find_best_translation(
1719 $namespace, $filepath, $string, $context, $lang,
1720 $self->{job}->{reuse_orphaned},
1721 $self->{job}->{reuse_fuzzy},
1722 $self->{job}->{reuse_uncertain}
1723 56 );
1724  
1725 56 ▶ if ($multiple_variants && !$self->{job}->{reuse_uncertain}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiple_variants and not $self->{"job"}{"reuse_uncertain"}) 0 56 56
Condition33%1/3
$multiple_variants and not $self->{"job"}{"reuse_uncertain"}
!ll&&!rl&&r
5600
MC/DC0%0/2
$multiple_variants and not $self->{"job"}{"reuse_uncertain"}
$multiple_variants not $self->{"job"}{"reuse_uncertain"}
Truth table33%1/3
A and not(B)
A= $multiple_variantsB= not $self->{"job"}{"reuse_uncertain"}
ABresult
0X0
100
111
1726 0 ▶ print "Multiple translations found, won't reuse any because 'reuse_uncertain' mode is set to NO\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1727 # return now, otherwise the translation might be obtained in e.g. transform plugin
1728 # from a similar string that has just one translation variant
1729 0 return;
1730 }
1731  
1732 56 ▶ if ($fuzzy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fuzzy) { } 0 56 56
1733 # if the fuzzy flag is already set, always leave it as is,
1734 # even if the language is listed under `reuse_as_not_fuzzy` list
1735 } else {
1736 # the fuzzy flag is not set, but we might want to raise it here
1737 56 my $lang_as_fuzzy = is_flag_set($self->{job}->{reuse_as_fuzzy}, $lang);
1738 56 my $lang_as_not_fuzzy = is_flag_set($self->{job}->{reuse_as_not_fuzzy}, $lang);
1739 56 ▶ $fuzzy = 1 if $lang_as_fuzzy || ($self->{job}->{reuse_as_fuzzy_default} && !$lang_as_not_fuzzy);
Branch: 1 branch
BranchTrueFalseTotal
if $lang_as_fuzzy or $self->{"job"}{"reuse_as_fuzzy_default"} and not $lang_as_not_fuzzy 56 0 56
Condition33%2/6
$lang_as_fuzzy or $self->{"job"}{"reuse_as_fuzzy_default"} and not $lang_as_not_fuzzy
l!l&&r!l&&!r
0560
$self->{"job"}{"reuse_as_fuzzy_default"} and not $lang_as_not_fuzzy
!ll&&!rl&&r
0056
MC/DC0%0/3
$lang_as_fuzzy or $self->{"job"}{"reuse_as_fuzzy_default"} and not $lang_as_not_fuzzy
$lang_as_fuzzy $self->{"job"}{"reuse_as_fuzzy_default"} not $lang_as_not_fuzzy
Truth table25%1/4
A or B and not(C)
A= $lang_as_fuzzyB= $self->{"job"}{"reuse_as_fuzzy_default"}C= not $lang_as_not_fuzzy
ABCresult
1XX1
0111
00X0
0100
1740 }
1741 56 ▶ return ($translation, $fuzzy, $comment, 1) if ($translation ne '' || $comment ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $translation ne "" or $comment ne "" 0 56 56
Condition33%1/3
$translation ne "" or $comment ne ""
l!l&&r!l&&!r
0056
MC/DC0%0/2
$translation ne "" or $comment ne ""
$translation ne "" $comment ne ""
Truth table33%1/3
A or B
A= $translation ne ""B= $comment ne ""
ABresult
1X1
011
000
1742 }
1743  
1744 # try to get translation by calling registered plugin callbacks
1745 # (phase 2, after looking up the database)
1746 56 ($translation, $fuzzy, $comment, $need_save) = $self->run_callbacks(
1747 'get_translation',
1748 $string, $context, $namespace, $filepath, $lang, $disallow_similar_lang, $item_id, $key
1749 );
1750 56 ▶ $translation = NFC($translation) if $translation ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $translation ne "" 0 56 56
1751 56 ▶ return ($translation, $fuzzy, $comment, $need_save) if ($translation ne '' || $comment ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $translation ne "" or $comment ne "" 0 56 56
Condition33%1/3
$translation ne "" or $comment ne ""
l!l&&r!l&&!r
0056
MC/DC0%0/2
$translation ne "" or $comment ne ""
$translation ne "" $comment ne ""
Truth table33%1/3
A or B
A= $translation ne ""B= $comment ne ""
ABresult
1X1
011
000
1752  
1753 # Otherwise, try to look for a translation from a similar language
1754  
1755 56 ▶ if ($self->{job}->{reuse_translations} && !$disallow_similar_lang && exists $self->{job}->{similar_languages}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"job"}{"reuse_translations"} and not $disallow_similar_lang and exists $self->{"job"}{"similar_languages"}) 56 0 56
Condition33%2/6
$self->{"job"}{"reuse_translations"} and not $disallow_similar_lang and exists $self->{"job"}{"similar_languages"}
!ll&&!rl&&r
0056
$self->{"job"}{"reuse_translations"} and not $disallow_similar_lang
!ll&&!rl&&r
0056
MC/DC0%0/3
$self->{"job"}{"reuse_translations"} and not $disallow_similar_lang and exists $self->{"job"}{"similar_languages"}
$self->{"job"}{"reuse_translations"} not $disallow_similar_lang exists $self->{"job"}{"similar_languages"}
Truth table25%1/4
A and not(B) and C
A= $self->{"job"}{"reuse_translations"}B= not $disallow_similar_langC= exists $self->{"job"}{"similar_languages"}
ABCresult
0XX0
10X0
1100
1111
1756 56 foreach my $rule (@{$self->{job}->{similar_languages}}) {
1757 0 ▶ if ($rule->{destination} eq $lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"destination"} eq $lang) 0 0 0
1758 0 foreach my $source_lang (sort @{$rule->{source}}) {
1759 # pass disallow_similar_lang = 1 to avoid infinite recursion
1760 0 my ($translation, $fuzzy, $comment, $need_save) =
1761 $self->get_translation($string, $context, $namespace, $filepath, $source_lang, 1, $item_id, $key);
1762 # force fuzzy flag if $rule->{as_fuzzy} is true; otherwise, use the original fuzzy flag value
1763 0 ▶ $fuzzy = $fuzzy || $rule->{as_fuzzy};
Condition0%0/3
$fuzzy || $rule->{'as_fuzzy'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$fuzzy || $rule->{'as_fuzzy'}
$fuzzy $rule->{'as_fuzzy'}
Truth table0%0/3
A || B
A= $fuzzyB= $rule->{'as_fuzzy'}
ABresult
1X1
011
000
1764 0 ▶ return ($translation, $fuzzy, $comment, 1) if ($translation ne '' || $comment ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $translation ne "" or $comment ne "" 0 0 0
Condition0%0/3
$translation ne "" or $comment ne ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$translation ne "" or $comment ne ""
$translation ne "" $comment ne ""
Truth table0%0/3
A or B
A= $translation ne ""B= $comment ne ""
ABresult
1X1
011
000
1765 }
1766 }
1767 }
1768 }
1769  
1770 # Otherwise, return nothing
1771 }
1772  
1773 2 1;
↑ Summary lib/Serge/Engine/Plugin/limit_languages.pm »