lib/Serge/Importer.pm

filter: Statementstmt 12.9% 25 / 193 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 42.1% 8 / 19 Podpod 0.0% 0 / 7 totaltotal 10.1% 33 / 324 SCAR 76.2
CC
51
Cov
9%
parse_localized_file_callback
420.0
parse_source_file_callback
156.0
parse_localized_files_for_file_lang
90.0
CRAP 2039.5
« lib/Serge/Engine/Plugin/metaparser.pm ↑ Summary lib/Serge/Util.pm »
1 package Serge::Importer;
2 1 ▶use parent Serge::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Unicode::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Serge::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub process_job {
11 0 ▶ my ($self, $job) = @_;
Subroutine process_job: not called CC=5 SCAR=34.0
Pod undocumented
12  
13 0 ▶ $self->{stats} = {} unless exists $self->{stats};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"stats"} 0 0 0
14  
15 0 $self->init_job($job);
16 0 $self->adjust_job_defaults($job);
17 0 $self->adjust_destination_languages($job);
18  
19 # this hash will hold key => {item => <item_id>, string => <string>} mapping
20 # for the source resource file
21 0 $job->{source_keys} = {};
22 0 $job->{localized_keys} = {};
23 0 $job->{optimizations} = undef;
24  
25 # now that we have a final list of destination languages, we can
26 # determine if the job can run or not
27  
28 # for import purposes, skip jobs having output_only_mode as well
29  
30 0 ▶ if ($self->job_can_run($job) && !$job->{output_only_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->job_can_run($job) and not $job->{'output_only_mode'}) { } 0 0 0
Condition0%0/3
$self->job_can_run($job) and not $job->{"output_only_mode"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->job_can_run($job) and not $job->{"output_only_mode"}
$self->job_can_run($job) not $job->{"output_only_mode"}
Truth table0%0/3
A and not(B)
A= $self->job_can_run($job)B= not $job->{"output_only_mode"}
ABresult
0X0
100
111
31 0 print "\n\n*** [".$job->{id}."] ".$job->{name}." ***\n\n";
32 } else {
33 0 print "*** SKIPPING: [".$job->{id}."] ".$job->{name}." ***\n";
34 0 return;
35 }
36  
37 0 $self->open_database($job);
38 0 $self->adjust_modified_languages($job);
39  
40 0 ▶ die "source_dir [$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 0 0
41  
42 0 print "Path to source resources: [".$job->{source_dir}."]\n";
43 0 print "Path to localized resources: [".$job->{output_file_path}."]\n";
44 0 print "Languages: [".join(',', sort @{$job->{modified_languages}})."]\n";
45 0 print "DB source: [".$job->{db_source}."]\n";
46  
47 # preload all items/strings/translations into cache
48  
49 0 $self->{db}->preload_cache_for_job($job->{db_namespace}, $job->{id}, $job->{modified_languages});
50  
51 0 $self->run_callbacks('before_job');
52  
53 0 $self->run_callbacks('before_update_database_from_source_files');
54  
55 0 $self->{current_lang} = '';
56  
57 0 $self->update_database_from_source_files;
58  
59 0 $self->parse_localized_files;
60  
61 # note: do not close the database at this point as it might be reused in another job
62  
63 0 $self->run_callbacks('after_job');
64  
65 0 print "*** [end] ***\n";
66  
67 }
68  
69 sub parse_localized_files {
70 0 ▶ my ($self) = @_;
Subroutine parse_localized_files: not called CC=2 SCAR=17.9
Pod undocumented
71  
72 0 print "\nParsing localized files...\n\n";
73  
74 0 foreach my $file (sort keys %{$self->{files}}) {
75 0 $self->parse_localized_files_for_file($file);
76 }
77 }
78  
79 sub parse_localized_files_for_file {
80 0 ▶ my ($self, $file) = @_;
Subroutine parse_localized_files_for_file: not called CC=4 SCAR=30.0
Pod undocumented
81  
82 0 ▶ $self->{stats}->{''} = {} unless exists $self->{stats}->{''};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"stats"}{""} 0 0 0
83 0 $self->{stats}->{''}->{files}++;
84  
85 0 print "\t$file\n";
86  
87 # Setting the global variables
88  
89 0 $self->{current_file_rel} = $file;
90 0 $self->{current_file_id} = $self->{db}->get_file_id($self->{job}->{db_namespace}, $self->{job}->{id}, $self->{current_file_rel}, 1); # do not create
91  
92 # sanity check
93  
94 0 ▶ die "current_file_id is not defined\n" unless $self->{current_file_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"current_file_id"} 0 0 0
95  
96 0 foreach my $lang (@{$self->{job}->{modified_languages}}) {
97 0 $self->parse_localized_files_for_file_lang($file, $lang);
98 }
99  
100 }
101  
102 sub parse_localized_files_for_file_lang {
103 0 ▶ my ($self, $file, $lang) = @_;
Subroutine parse_localized_files_for_file_lang: not called CC=9 SCAR=45.0
Pod undocumented
104  
105 0 $self->{current_lang} = $lang;
106  
107 0 my $fullpath = $self->get_full_output_path($file, $lang);
108  
109 0 ▶ $self->{stats}->{$lang} = {} unless exists $self->{stats}->{$lang};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"stats"}{$lang} 0 0 0
110  
111 0 $self->{previous_translation} = {};
112  
113 # prepare the report cache key
114 0 ▶ if ($self->{save_report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_report"}) 0 0 0
115 0 ▶ $self->{report} = {} unless exists $self->{report};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"report"} 0 0 0
116 0 ▶ $self->{report}->{$lang} = {} unless exists $self->{report}->{$lang};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"report"}{$lang} 0 0 0
117 0 $self->{current_report_key} = $self->{report}->{$lang}->{$file} = [];
118 }
119  
120 0 print "\t\t$fullpath\n";
121  
122 0 ▶ if (!-f $fullpath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $fullpath) 0 0 0
123 0 $self->_notice("Missing localized file for '$lang' language", "\t\t\t");
124 0 ▶ if ($self->{save_report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_report"}) 0 0 0
125 0 push @{$self->{current_report_key}}, {
126 severity => 'notice',
127 error_status => 'SERGE_NOTICE_FILE_MISSING'
128 };
129 }
130 0 return;
131 }
132  
133 0 $self->{stats}->{$lang}->{files}++;
134  
135 # Getting plugin object
136  
137 0 my ($src) = $self->read_file($fullpath);
138  
139 # clear disambiguation cache
140 0 $self->clear_disambiguation_cache();
141  
142 # Parsing the file
143 0 eval {
144 0 $self->{job}->{parser_object}->{import_mode} = 1;
145 0 ▶ $self->{job}->{parser_object}->parse(\$src, sub { $self->parse_localized_file_callback(@_) }, $lang);
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 };
147  
148 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
149 0 $self->_error("File parsing failed; the file will not be processed in full; reason: $@", "\t\t\t", 1); # non-fatal
150 0 ▶ if ($self->{save_report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_report"}) 0 0 0
151 0 push @{$self->{current_report_key}}, {
152 severity => 'error',
153 error_status => 'SERGE_ERROR_PARSING_FAILED'
154 };
155 }
156 }
157 }
158  
159 sub disambiguate_key {
160 0 ▶ my ($self, $store, $key) = @_;
Subroutine disambiguate_key: not called CC=4 SCAR=30.0
Pod undocumented
161  
162 0 ▶ if (exists $store->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $store->{$key}) 0 0 0
163 0 ▶ die "Shouldn't disambiguate when disambiguate_keys is not enabled" unless $self->{disambiguate_keys};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"disambiguate_keys"} 0 0 0
164  
165 0 my $n = 1;
166 0 my $base_key = $key;
167 do {
168 0 $n++;
169 0 $key = "$base_key.$n";
170 0 ▶ } while exists $store->{$key};
Branch: 1 branch
BranchTrueFalseTotal
exists $store->{$key} and do { do { ++$n; $key = "$base_key.$n" } } 0 0 0
171 }
172  
173 0 return $key;
174 }
175  
176 sub parse_source_file_callback {
177 0 ▶ my ($self, $string, $context, $hint, $flagsref, $lang, $key) = @_;
Subroutine parse_source_file_callback: not called CC=12 SCAR=50.5
Pod undocumented
178  
179 0 ▶ if ($key eq '' && !$self->{disambiguate_keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "" and not $self->{"disambiguate_keys"}) 0 0 0
Condition0%0/3
$key eq "" and not $self->{"disambiguate_keys"}
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "" and not $self->{"disambiguate_keys"}
$key eq "" not $self->{"disambiguate_keys"}
Truth table0%0/3
A and not(B)
A= $key eq ""B= not $self->{"disambiguate_keys"}
ABresult
0X0
100
111
180 0 $self->_error("Parser plugin didn't provide a key value in a callback. ".
181 "Importing translations with this plugin is not possible, unless you use `--disambiguate-keys` option.", "\t\t\t");
182 }
183  
184 0 ▶ if ($string eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq "") 0 0 0
185 0 $self->_notice("Source string for key '$key' is blank, skipping", "\t\t");
186 0 ▶ if ($self->{save_report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_report"}) 0 0 0
187 0 push @{$self->{current_report_key}}, {
188 key => $key,
189 severity => 'error',
190 error_status => 'SERGE_NOTICE_EMPTY_SOURCE'
191 };
192 }
193 0 return;
194 }
195  
196 0 my $keys = $self->{job}->{source_keys}->{$self->{current_file_rel}};
197 0 ▶ $keys = $self->{job}->{source_keys}->{$self->{current_file_rel}} = {} unless defined $keys;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keys 0 0 0
198  
199 0 ▶ if (exists $keys->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $keys->{$key}) 0 0 0
200 0 ▶ if ($self->{disambiguate_keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'disambiguate_keys'}) { } 0 0 0
201 0 my $orig_key = $key;
202 0 $key = $self->disambiguate_key($keys, $key);
203 0 $self->_notice("Duplicate key '$orig_key' found in source file, changed to '$key'", "\t\t\t");
204 } else {
205 0 $self->_error("Duplicate key '$key' found in source file", "\t\t\t", 1); # non-fatal
206  
207 0 ▶ if ($self->{save_report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_report"}) 0 0 0
208 0 push @{$self->{current_report_key}}, {
209 key => $key,
210 source => $string,
211 severity => 'error',
212 error_status => 'SERGE_ERROR_DUPLICATE_KEY'
213 };
214 }
215 }
216 }
217  
218 0 ▶ print "\t\t\t::source file: '$key' => '$string'\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
219  
220 0 my $item_id;
221 0 ▶ if ($self->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'dry_run'}) { } 0 0 0
222 0 ▶ $string = NFC($string) if ($string =~ m/[^\x00-\x7F]/);
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /[^\x00-\x7F]/ 0 0 0
223 } else {
224 0 $item_id = Serge::Engine::parse_source_file_callback(@_);
225 }
226 0 $keys->{$key} = {item => $item_id, string => $string};
227 }
228  
229 sub parse_localized_file_callback {
230 0 ▶ my ($self, $translation, $context, $hint, $flagsref, $lang, $key) = @_;
Subroutine parse_localized_file_callback: not called CC=20 SCAR=60.4
Pod undocumented
231  
232 # Normalize parameters
233  
234 0 ▶ $translation = NFC($translation) if ($translation =~ m/[^\x00-\x7F]/);
Branch: 1 branch
BranchTrueFalseTotal
if $translation =~ /[^\x00-\x7F]/ 0 0 0
235  
236 0 my $keys = $self->{job}->{localized_keys};
237  
238 0 ▶ $keys->{$self->{current_file_rel}} = {} unless exists $keys->{$self->{current_file_rel}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$keys{$self->{"current_file_rel"}} 0 0 0
239 0 $keys = $keys->{$self->{current_file_rel}};
240 0 ▶ $keys->{$lang} = {} unless exists $keys->{$lang};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $keys->{$lang} 0 0 0
241 0 $keys = $keys->{$lang};
242  
243 0 ▶ if (exists $keys->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $keys->{$key}) 0 0 0
244 0 ▶ if ($self->{disambiguate_keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'disambiguate_keys'}) { } 0 0 0
245 0 my $orig_key = $key;
246 0 $key = $self->disambiguate_key($keys, $key);
247 0 $self->_notice("Duplicate key '$orig_key' found in localized file [$lang], changed to '$key'", "\t\t\t");
248 } else {
249 0 my $data = $self->{job}->{source_keys}->{$self->{current_file_rel}}->{$key};
250 0 my $source = '';
251 0 ▶ $source = $data->{string} if $data;
Branch: 1 branch
BranchTrueFalseTotal
if $data 0 0 0
252  
253 0 ▶ if ($translation eq $self->{previous_translation}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($translation eq $self->{'previous_translation'}{$key}) { } 0 0 0
254 0 $self->_warning("Duplicate key '$key' with the same translation found in localized file [$lang]", "\t\t\t", 1); # non-fatal
255  
256 0 ▶ if ($self->{save_report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_report"}) 0 0 0
257 0 push @{$self->{current_report_key}}, {
258 key => $key,
259 source => $source,
260 translation => $translation,
261 severity => 'warning',
262 error_status => 'SERGE_WARNING_DUPLICATE_KEY'
263 };
264 }
265  
266 } else {
267 0 $self->_error("Duplicate key '$key' with different translations found in localized file [$lang]", "\t\t\t", 1); # non-fatal
268  
269 0 ▶ if ($self->{save_report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_report"}) 0 0 0
270 0 push @{$self->{current_report_key}}, {
271 key => $key,
272 source => $source,
273 translation => $translation,
274 severity => 'error',
275 error_status => 'SERGE_ERROR_MULTIPLE_TRANSLATIONS'
276 };
277 }
278 }
279  
280 0 return $translation;
281 }
282 }
283  
284 # mark the key name as used for disambiguation to work
285 0 $keys->{$key} = 1;
286  
287 # store previous translation to check for multiple translations of the same key
288 0 $self->{previous_translation}->{$key} = $translation;
289  
290 # check the resulting key against the store of source keys
291  
292 0 my $data = $self->{job}->{source_keys}->{$self->{current_file_rel}}->{$key};
293  
294 0 ▶ if (!$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
295 0 $self->_warning("Unknown key '$key'", "\t\t\t");
296  
297 0 ▶ if ($self->{save_report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_report"}) 0 0 0
298 0 push @{$self->{current_report_key}}, {
299 key => $key,
300 lang => $lang,
301 severity => 'warning',
302 error_status => 'SERGE_WARNING_UNKNOWN_KEY',
303 translation => $translation
304 };
305 }
306  
307 0 return $translation;
308 }
309  
310 0 my $item_id = $data->{item};
311  
312 0 ▶ if (($translation eq '') && ($data->{string} ne '')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($translation eq "" and $data->{"string"} ne "") 0 0 0
Condition0%0/3
$translation eq "" and $data->{"string"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$translation eq "" and $data->{"string"} ne ""
$translation eq "" $data->{"string"} ne ""
Truth table0%0/3
A and B
A= $translation eq ""B= $data->{"string"} ne ""
ABresult
0X0
100
111
313 0 $self->_notice("Translation for key '$key' is blank, skipping", "\t\t\t");
314 0 ▶ if ($self->{save_report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_report"}) 0 0 0
315 0 push @{$self->{current_report_key}}, {
316 key => $key,
317 severity => 'notice',
318 error_status => 'SERGE_NOTICE_EMPTY_TRANSLATION'
319 };
320 }
321 0 return;
322 }
323  
324 0 ▶ if ($data->{string} ne $translation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'string'} ne $translation) { } 0 0 0
325 0 ▶ if ($self->{save_report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_report"}) 0 0 0
326 0 push @{$self->{current_report_key}}, {
327 key => $key,
328 lang => $lang,
329 source => $data->{string},
330 0 translation => $translation
331 };
332 }
333 } else {
334 0 $self->_notice("Translation is the same as the source for key '$key'", "\t\t\t");
335 0 ▶ if ($self->{save_report}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_report"}) 0 0 0
336 0 push @{$self->{current_report_key}}, {
337 key => $key,
338 lang => $lang,
339 severity => 'notice',
340 error_status => 'SERGE_NOTICE_SAME_TRANSLATION',
341 source => $data->{string},
342 0 translation => $translation
343 };
344 }
345 }
346  
347 0 ▶ print "\t\t\t::localized file [$lang]: '$key' => '$translation'\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
348  
349 0 ▶ $self->{db}->set_translation($item_id, $lang, $translation, undef, undef, 0) unless $self->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dry_run"} 0 0 0
350  
351 0 return $translation;
352 }
353  
354 sub _notice {
355 0 ▶ my ($self, $s, $indent_prefix) = @_;
Subroutine _notice: not called CC=1 SCAR=6.9
356  
357 0 print $indent_prefix."NOTICE: $s\n";
358 0 $self->{stats}->{$self->{current_lang}}->{notices}++;
359 }
360  
361 sub _warning {
362 0 ▶ my ($self, $s, $indent_prefix) = @_;
Subroutine _warning: not called CC=1 SCAR=6.9
363  
364 0 print $indent_prefix."WARNING: $s\n";
365 0 $self->{stats}->{$self->{current_lang}}->{warnings}++;
366 }
367  
368 sub _error {
369 0 ▶ my ($self, $s, $indent_prefix, $nonfatal) = @_;
Subroutine _error: not called CC=2 SCAR=17.9
370  
371 0 print $indent_prefix."ERROR: $s\n";
372 0 $self->{stats}->{$self->{current_lang}}->{errors}++;
373 0 ▶ exit(1) unless $nonfatal;
Branch: 1 branch
BranchTrueFalseTotal
unless $nonfatal 0 0 0
374 }
375  
376 1 1;
« lib/Serge/Engine/Plugin/metaparser.pm ↑ Summary lib/Serge/Util.pm »