line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::I18N::DB; |
2
|
2
|
|
|
2
|
|
48218
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
139
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
63
|
|
4
|
2
|
|
|
2
|
|
2018
|
use Any::Moose; |
|
2
|
|
|
|
|
117867
|
|
|
2
|
|
|
|
|
12
|
|
5
|
2
|
|
|
2
|
|
2571
|
use Encode; |
|
2
|
|
|
|
|
19268
|
|
|
2
|
|
|
|
|
293
|
|
6
|
2
|
|
|
2
|
|
22497
|
use DBI; |
|
2
|
|
|
|
|
64484
|
|
|
2
|
|
|
|
|
142
|
|
7
|
2
|
|
|
2
|
|
10893
|
use DBD::SQLite; |
|
2
|
|
|
|
|
33528
|
|
|
2
|
|
|
|
|
2757
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has dbh => |
10
|
|
|
|
|
|
|
( is => 'rw' ); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub BUILD { |
13
|
1
|
|
|
1
|
1
|
589
|
my ($self,$args) = @_; |
14
|
|
|
|
|
|
|
|
15
|
1
|
50
|
|
|
|
10
|
if( $args->{path} ) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
16
|
0
|
|
|
|
|
0
|
$self->connect( $args->{path} ); |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
elsif( $args->{name} ) { |
19
|
0
|
|
|
|
|
0
|
my $dbname = $args->{name}; |
20
|
0
|
|
|
|
|
0
|
my $dbpath = File::Spec->join( $ENV{HOME} , $dbname ); |
21
|
0
|
|
|
|
|
0
|
$self->connect( $dbpath ); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
elsif( ! $args->{dbh} ) { |
24
|
1
|
|
|
|
|
8
|
print "Importing database schema\n"; |
25
|
1
|
|
|
|
|
14
|
my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:","","", |
26
|
|
|
|
|
|
|
{ RaiseError => 1, sqlite_unicode => 1, }); |
27
|
1
|
|
|
|
|
1823
|
$self->dbh( $dbh ); |
28
|
1
|
|
|
|
|
5
|
$self->init_schema(); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub connect { |
33
|
0
|
|
|
0
|
0
|
0
|
my ($self,$dbpath) = @_; |
34
|
0
|
|
|
|
|
0
|
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbpath","",""); |
35
|
0
|
|
|
|
|
0
|
$self->dbh( $dbh ); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub close { |
40
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
41
|
0
|
|
|
|
|
0
|
$self->dbh->disconnect(); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub init_schema { |
45
|
1
|
|
|
1
|
0
|
2
|
my ($self) = shift; |
46
|
1
|
|
|
|
|
11
|
$self->dbh->do( qq| |
47
|
|
|
|
|
|
|
create table po_string ( |
48
|
|
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT, |
49
|
|
|
|
|
|
|
lang TEXT, |
50
|
|
|
|
|
|
|
msgid TEXT, |
51
|
|
|
|
|
|
|
msgstr TEXT, |
52
|
|
|
|
|
|
|
updated_on timestamp, |
53
|
|
|
|
|
|
|
updated_by varchar(120)); |
54
|
|
|
|
|
|
|
|); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# by {id} |
58
|
|
|
|
|
|
|
sub get_entry { |
59
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $id ) = @_; |
60
|
0
|
|
|
|
|
0
|
my $sth = $self->dbh->prepare(qq{ select * from po_string where id = ? }); |
61
|
0
|
|
|
|
|
0
|
$sth->execute($id); |
62
|
0
|
|
|
|
|
0
|
my $data = $sth->fetchrow_hashref(); |
63
|
0
|
|
|
|
|
0
|
$sth->finish; |
64
|
0
|
|
|
|
|
0
|
return $data; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub set_entry { |
68
|
0
|
|
|
0
|
0
|
0
|
my ($self,$id,$msgstr) = @_; |
69
|
0
|
0
|
0
|
|
|
0
|
die unless $id && $msgstr; |
70
|
0
|
|
|
|
|
0
|
my $sth = $self->dbh->prepare(qq{ update po_string set msgstr = ? where id = ? }); |
71
|
0
|
|
|
|
|
0
|
my $ret = $sth->execute( $msgstr, $id ); |
72
|
0
|
|
|
|
|
0
|
$sth->finish; |
73
|
0
|
|
|
|
|
0
|
return $ret; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub last_id { |
77
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub insert { |
83
|
1
|
|
|
1
|
0
|
1005
|
my ( $self , $lang , $msgid, $msgstr ) = @_; |
84
|
1
|
|
|
|
|
7
|
$msgstr = decode_utf8( $msgstr ); |
85
|
0
|
|
|
|
|
|
my $sth = $self->dbh->prepare( |
86
|
|
|
|
|
|
|
qq| INSERT INTO po_string ( lang , msgid , msgstr ) VALUES ( ? , ? , ? ); |); |
87
|
0
|
|
|
|
|
|
$sth->execute( $lang, $msgid, $msgstr ); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub find { |
91
|
0
|
|
|
0
|
0
|
|
my ( $self, $lang , $msgid ) = @_; |
92
|
0
|
|
|
|
|
|
my $sth = $self->dbh->prepare(qq| SELECT * FROM po_string WHERE lang = ? AND msgid = ? LIMIT 1;|); |
93
|
0
|
|
|
|
|
|
$sth->execute( $lang, $msgid ); |
94
|
0
|
|
|
|
|
|
my $data = $sth->fetchrow_hashref(); |
95
|
0
|
|
|
|
|
|
$sth->finish; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
return $data; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub get_unset_entry_list { |
102
|
0
|
|
|
0
|
0
|
|
my ($self, $lang ) = @_; |
103
|
0
|
|
|
|
|
|
my $sth; |
104
|
0
|
0
|
|
|
|
|
if( $lang ) { |
105
|
0
|
|
|
|
|
|
$sth = $self->dbh->prepare(qq| SELECT * FROM po_string where lang = ? and msgstr = '' or msgstr is null; |); |
106
|
0
|
|
|
|
|
|
$sth->execute( $lang ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
else { |
109
|
0
|
|
|
|
|
|
$sth = $self->dbh->prepare(qq| SELECT * FROM po_string where msgstr = '' or msgstr is null; | ); |
110
|
0
|
|
|
|
|
|
$sth->execute(); |
111
|
|
|
|
|
|
|
} |
112
|
0
|
|
|
|
|
|
return $self->_entry_sth_to_list( $sth ); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub get_entry_list { |
116
|
0
|
|
|
0
|
0
|
|
my ( $self, $lang ) = @_; |
117
|
0
|
|
|
|
|
|
my $sth; |
118
|
0
|
0
|
|
|
|
|
if( $lang ) { |
119
|
0
|
|
|
|
|
|
$sth = $self->dbh->prepare(qq| select * from po_string where lang = ? order by id desc; |); |
120
|
0
|
|
|
|
|
|
$sth->execute( $lang ); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
else { |
123
|
0
|
|
|
|
|
|
$sth = $self->dbh->prepare(qq| select * from po_string order by id desc; | ); |
124
|
0
|
|
|
|
|
|
$sth->execute(); |
125
|
|
|
|
|
|
|
} |
126
|
0
|
|
|
|
|
|
return $self->_entry_sth_to_list( $sth ); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub _entry_sth_to_list { |
131
|
0
|
|
|
0
|
|
|
my ($self , $sth) = @_; |
132
|
0
|
|
|
|
|
|
my @result; |
133
|
0
|
|
|
|
|
|
while( my $row = $sth->fetchrow_hashref ) { |
134
|
0
|
|
|
|
|
|
push @result, { |
135
|
|
|
|
|
|
|
id => $row->{id}, |
136
|
|
|
|
|
|
|
lang => $row->{lang}, |
137
|
|
|
|
|
|
|
msgid => $row->{msgid}, |
138
|
|
|
|
|
|
|
msgstr => $row->{msgstr}, |
139
|
|
|
|
|
|
|
}; |
140
|
|
|
|
|
|
|
} |
141
|
0
|
|
|
|
|
|
return \@result; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub get_langlist { |
146
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
147
|
0
|
|
|
|
|
|
my $sth = $self->dbh->prepare("select distinct lang from po_string;"); |
148
|
0
|
|
|
|
|
|
$sth->execute(); |
149
|
0
|
|
|
|
|
|
my $hashref = $sth->fetchall_hashref('lang'); |
150
|
0
|
|
|
|
|
|
$sth->finish; |
151
|
0
|
|
|
|
|
|
return keys %$hashref; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
0
|
0
|
|
sub write_to_pofile { |
155
|
|
|
|
|
|
|
# XXX: |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub import_lexicon { |
160
|
0
|
|
|
0
|
0
|
|
my ( $self , $lang , $lex ) = @_; |
161
|
0
|
|
|
|
|
|
while ( my ( $msgid, $msgstr ) = each %$lex ) { |
162
|
0
|
|
|
|
|
|
$self->insert( $lang , $msgid , $msgstr ); |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub import_po { |
168
|
0
|
|
|
0
|
0
|
|
my ( $self, $lang, $pofile ) = @_; |
169
|
0
|
|
|
|
|
|
my $lme = App::I18N->lm_extract; |
170
|
0
|
0
|
0
|
|
|
|
$lme->read_po($pofile) if -f $pofile && $pofile !~ m/pot$/; |
171
|
0
|
|
|
|
|
|
$self->import_lexicon( $lang , $lme->lexicon ); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub export_lexicon { |
175
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
176
|
0
|
|
|
|
|
|
my $lexicon; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
|
179
|
0
|
|
|
|
|
|
return $lexicon; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub export_po { |
183
|
0
|
|
|
0
|
0
|
|
my ( $self , $lang , $pofile ) = @_; |
184
|
0
|
|
|
|
|
|
my $list = $self->get_entry_list( $lang ); |
185
|
0
|
|
|
|
|
|
my $lexicon = { |
186
|
0
|
|
|
|
|
|
map { $_->{msgid} => encode_utf8 $_->{msgstr}; } @$list |
187
|
|
|
|
|
|
|
}; |
188
|
0
|
|
|
|
|
|
my $lme = App::I18N->lm_extract; |
189
|
0
|
|
|
|
|
|
$lme->set_lexicon( $lexicon ); |
190
|
0
|
|
|
|
|
|
$lme->write_po($pofile); |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=pod |
194
|
|
|
|
|
|
|
package MsgEntry; |
195
|
|
|
|
|
|
|
use Any::Moose; |
196
|
|
|
|
|
|
|
use JSON::XS; |
197
|
|
|
|
|
|
|
use overload |
198
|
|
|
|
|
|
|
'""' => \&to_string, |
199
|
|
|
|
|
|
|
'%{}' => \&to_hash; |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
has id => ( is => 'rw', isa => 'Int' ); |
203
|
|
|
|
|
|
|
has lang => ( is => 'rw' , isa => 'Str' ); |
204
|
|
|
|
|
|
|
has msgid => ( is => 'rw' , isa => 'Str' ); |
205
|
|
|
|
|
|
|
has msgstr => ( is => 'rw' , isa => 'Str' ); |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
sub to_hash { |
208
|
|
|
|
|
|
|
my $self = shift; |
209
|
|
|
|
|
|
|
return ( |
210
|
|
|
|
|
|
|
id => $self->id, |
211
|
|
|
|
|
|
|
lang => $self->lang, |
212
|
|
|
|
|
|
|
msgid => $self->msgid, |
213
|
|
|
|
|
|
|
msgstr => $self->msgstr, |
214
|
|
|
|
|
|
|
); |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub to_string { |
218
|
|
|
|
|
|
|
my $self = shift; |
219
|
|
|
|
|
|
|
return encode_json( { $self->to_hash } ); |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
=cut |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
1; |