File Coverage

blib/lib/Log/Report/Extract.pm
Criterion Covered Total %
statement 70 104 67.3
branch 12 42 28.5
condition 4 24 16.6
subroutine 17 21 80.9
pod 11 12 91.6
total 114 203 56.1


line stmt bran cond sub pod time code
1             # Copyrights 2007-2018 by [Mark Overmeer ].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5             # This code is part of distribution Log-Report-Lexicon. Meta-POD processed
6             # with OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package Log::Report::Extract;
10 2     2   93253 use vars '$VERSION';
  2         5  
  2         82  
11             $VERSION = '1.11';
12              
13              
14 2     2   10 use warnings;
  2         5  
  2         51  
15 2     2   8 use strict;
  2         3  
  2         33  
16              
17 2     2   7 use Log::Report 'log-report-lexicon';
  2         4  
  2         7  
18 2     2   1211 use Log::Report::Lexicon::Index ();
  2         6  
  2         53  
19 2     2   377 use Log::Report::Lexicon::POT ();
  2         4  
  2         2690  
20              
21              
22             sub new(@)
23 1     1 1 1380 { my $class = shift;
24 1         7 (bless {}, $class)->init( {@_} );
25             }
26              
27             sub init($)
28 1     1 0 9 { my ($self, $args) = @_;
29             my $lexi = $args->{lexicon}
30 1 50       5 or error __"extractions require an explicit lexicon directory";
31              
32 1 50 33     13 -d $lexi or mkdir $lexi
33             or fault __x"cannot create lexicon directory {dir}", dir => $lexi;
34              
35 1         8 $self->{LRE_index} = Log::Report::Lexicon::Index->new($lexi);
36 1   50     5 $self->{LRE_charset} = $args->{LRE_charset} || 'utf-8';
37 1         3 $self->{LRE_domains} = {};
38 1         2 $self;
39             }
40              
41             #---------------
42              
43 2     2 1 8 sub index() {shift->{LRE_index}}
44 1     1 1 3 sub charset() {shift->{LRE_charset}}
45 2     2 1 3 sub domains() {sort keys %{shift->{LRE_domains}}}
  2         10  
46              
47              
48             sub pots($)
49 12     12 1 15 { my ($self, $domain) = @_;
50 12         21 my $r = $self->{LRE_domains}{$domain};
51 12 50       25 $r ? @$r : ();
52             }
53              
54              
55             sub addPot($$%)
56 0     0 1 0 { my ($self, $domain, $pot) = @_;
57 0 0       0 push @{$self->{LRE_domains}{$domain}}, ref $pot eq 'ARRAY' ? @$pot : $pot
  0 0       0  
58             if $pot;
59             }
60              
61             #---------------
62              
63             sub process($@)
64 0     0 1 0 { my ($self, $fn, %opts) = @_;
65 0         0 panic "not implemented";
66             }
67              
68              
69             sub cleanup(%)
70 0     0 1 0 { my ($self, %args) = @_;
71 0   0     0 my $keep = $args{keep} || {};
72 0 0       0 $keep = +{ map +($_ => 1), @$keep }
73             if ref $keep eq 'ARRAY';
74              
75 0         0 foreach my $domain ($self->domains)
76 0         0 { $_->keepReferencesTo($keep) for $self->pots($domain);
77             }
78             }
79              
80              
81             sub showStats(;$)
82 0     0 1 0 { my $self = shift;
83 0 0       0 my @domains = @_ ? @_ : $self->domains;
84              
85 0 0       0 dispatcher needs => 'INFO'
86             or return;
87              
88 0         0 foreach my $domain (@domains)
89 0 0       0 { my $pots = $self->{LRE_domains}{$domain} or next;
90 0         0 my ($msgids, $fuzzy, $inactive) = (0, 0, 0);
91              
92 0         0 foreach my $pot (@$pots)
93 0         0 { my $stats = $pot->stats;
94 0 0 0     0 next unless $stats->{fuzzy} || $stats->{inactive};
95              
96 0         0 $msgids = $stats->{msgids};
97 0 0       0 next if $msgids == $stats->{fuzzy}; # ignore the template
98              
99             notice __x
100             "{domain}: {fuzzy%3d} fuzzy, {inact%3d} inactive in {filename}"
101             , domain => $domain, fuzzy => $stats->{fuzzy}
102 0         0 , inact => $stats->{inactive}, filename => $pot->filename;
103              
104 0         0 $fuzzy += $stats->{fuzzy};
105 0         0 $inactive += $stats->{inactive};
106             }
107              
108 0 0 0     0 if($fuzzy || $inactive)
109 0         0 { info __xn
110             "{domain}: one file with {ids} msgids, {f} fuzzy and {i} inactive translations"
111             , "{domain}: {_count} files each {ids} msgids, {f} fuzzy and {i} inactive translations in total"
112             , scalar(@$pots), domain => $domain
113             , f => $fuzzy, ids => $msgids, i => $inactive
114             }
115             else
116 0         0 { info __xn
117             "{domain}: one file with {ids} msgids"
118             , "{domain}: {_count} files with each {ids} msgids"
119             , scalar(@$pots), domain => $domain, ids => $msgids;
120             }
121             }
122             }
123              
124              
125             sub write(;$)
126 3     3 1 415 { my ($self, $domain) = @_;
127 3 100       8 unless(defined $domain) # write all
128 2         12 { $self->write($_) for $self->domains;
129 2         141 return;
130             }
131              
132 1 50       3 my $pots = delete $self->{LRE_domains}{$domain}
133             or return; # nothing found
134              
135 1         3 for my $pot (@$pots)
136 1         4 { $pot->updated;
137 1         4 $pot->write;
138             }
139              
140 1         28 $self;
141             }
142              
143 1     1   1044 sub DESTROY() {shift->write}
144              
145             sub _reset($$)
146 1     1   3 { my ($self, $domain, $fn) = @_;
147              
148 1   33     8 my $pots = $self->{LRE_domains}{$domain}
149             ||= $self->_read_pots($domain);
150              
151 1         5 $_->removeReferencesTo($fn) for @$pots;
152             }
153              
154             sub _read_pots($)
155 1     1   3 { my ($self, $domain) = @_;
156              
157 1         4 my $index = $self->index;
158 1         4 my $charset = $self->charset;
159              
160 1         3 my @pots = map Log::Report::Lexicon::POT->read($_, charset=> $charset),
161             $index->list($domain);
162              
163 1         5 trace __xn "found one pot file for domain {domain}"
164             , "found {_count} pot files for domain {domain}"
165             , @pots, domain => $domain;
166              
167             return \@pots
168 1 50       64 if @pots;
169              
170             # new text-domain found, start template
171 1         6 my $fn = $index->addFile("$domain.$charset.po");
172 1         4 info __x"starting new textdomain {domain}, template in {filename}"
173             , domain => $domain, filename => $fn;
174              
175 1         52 my $pot = Log::Report::Lexicon::POT->new
176             ( textdomain => $domain
177             , filename => $fn
178             , charset => $charset
179             , version => 0.01
180             );
181              
182 1         5 [ $pot ];
183             }
184              
185              
186             sub store($$$$;$)
187 12     12 1 22 { my ($self, $domain, $fn, $linenr, $msgid, $plural) = @_;
188              
189 12         26 my $textdomain = textdomain $domain;
190 12         172 my $context = $textdomain->contextRules;
191              
192 12         50 foreach my $pot ($self->pots($domain))
193 12         17 { my ($stripped, $msgctxts);
194 12 50       17 if($context)
195 0   0     0 { my $lang = $pot->language || 'en';
196 0         0 ($stripped, $msgctxts) = $context->expand($msgid, $lang);
197              
198 0 0 0     0 if($plural && $plural =~ m/\{[^}]*\<\w+/)
199 0         0 { error __x"no context tags allowed in plural `{msgid}'"
200             , msgid => $plural;
201             }
202             }
203             else
204 12         17 { $stripped = $msgid;
205             }
206              
207 12 50 33     27 $msgctxts && @$msgctxts
208             or $msgctxts = [undef];
209              
210             MSGCTXT:
211 12         19 foreach my $msgctxt (@$msgctxts)
212             {
213             #warn "($stripped, $msgctxt)";
214 12 50       51 if(my $po = $pot->msgid($stripped, $msgctxt))
215 0         0 { $po->addReferences( ["$fn:$linenr"]);
216 0 0       0 $po->plural($plural) if $plural;
217 0         0 next MSGCTXT;
218             }
219              
220 12 100       34 my $format = $stripped =~ m/\{/ ? 'perl-brace' : 'perl';
221 12         41 my $po = Log::Report::Lexicon::PO->new
222             ( msgid => $stripped
223             , msgid_plural => $plural
224             , msgctxt => $msgctxt
225             , fuzzy => 1
226             , format => $format
227             , references => [ "$fn:$linenr" ]
228             );
229              
230 12         32 $pot->add($po);
231             }
232             }
233             }
234              
235             1;