line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Wikibase::Cache; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
366543
|
use strict; |
|
3
|
|
|
|
|
30
|
|
|
3
|
|
|
|
|
86
|
|
4
|
3
|
|
|
3
|
|
18
|
use warnings; |
|
3
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
81
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
985
|
use Class::Utils qw(set_params); |
|
3
|
|
|
|
|
25865
|
|
|
3
|
|
|
|
|
87
|
|
7
|
3
|
|
|
3
|
|
130
|
use English; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
31
|
|
8
|
3
|
|
|
3
|
|
1356
|
use Error::Pure qw(err); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
1014
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = 0.03; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
4
|
|
|
4
|
1
|
4069
|
my ($class, @params) = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# Create object. |
16
|
4
|
|
|
|
|
14
|
my $self = bless {}, $class; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Wikibase::Cache::Backend backend. |
19
|
4
|
|
|
|
|
12
|
$self->{'backend'} = 'Basic'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# Process parameters. |
22
|
4
|
|
|
|
|
16
|
set_params($self, @params); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Load backend module. |
25
|
4
|
|
|
|
|
48
|
my $backend_module = 'Wikibase::Cache::Backend::'.$self->{'backend'}; |
26
|
4
|
|
|
|
|
239
|
eval "require $backend_module;"; |
27
|
4
|
100
|
|
|
|
3076
|
if ($EVAL_ERROR) { |
28
|
1
|
|
|
|
|
7
|
err "Cannot load module '$backend_module'.", |
29
|
|
|
|
|
|
|
'Error', $EVAL_ERROR; |
30
|
|
|
|
|
|
|
} |
31
|
3
|
|
|
|
|
21
|
$self->{'_backend'} = $backend_module->new; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Check for inheritance. |
34
|
3
|
100
|
|
|
|
1342
|
if (! $self->{'_backend'}->isa('Wikibase::Cache::Backend')) { |
35
|
1
|
|
|
|
|
4
|
err "Backend must inherit 'Wikibase::Cache::Backend' abstract class."; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
2
|
|
|
|
|
28
|
return $self; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub get { |
42
|
0
|
|
|
0
|
1
|
|
my ($self, $type, $key) = @_; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
return $self->{'_backend'}->get($type, $key); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub save { |
48
|
0
|
|
|
0
|
1
|
|
my ($self, $type, $key, $value) = @_; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
return $self->{'_backend'}->save($type, $key, $value); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
1; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
__END__ |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=pod |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=encoding utf8 |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 NAME |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Wikibase::Cache - Wikibase cache class. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 SYNOPSIS |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
use Wikibase::Cache; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $obj = Wikibase::Cache->new(%params); |
70
|
|
|
|
|
|
|
my $backend_ret = $obj->get($type, $key); |
71
|
|
|
|
|
|
|
my $backend_ret = $obj->save($type, $key, $value); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 METHODS |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 C<new> |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $obj = Wikibase::Cache->new(%params); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Constructor. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=over 8 |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item * C<backend> |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Wikibase::Cache::Backend backend class. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Default value is 'Basic' = L<Wikibase::Cache::Backend::Basic>. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=back |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Returns instance of object. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 C<get> |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $backend_ret = $obj->get($type, $key); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Get cached value for C<$type> and C<$key>. |
98
|
|
|
|
|
|
|
Example C<$type> is 'label' and C<$key> is Wikidata QID like 'Q42'. Returns |
99
|
|
|
|
|
|
|
something like 'Douglas Adams'. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Returns backend return value(s). |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 C<save> |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $backend_ret = $obj->save($type, $key, $value); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Save cached value for C<$type> and C<$key>. |
108
|
|
|
|
|
|
|
Example C<$type> is 'label' and C<$key> is Wikidata QID like 'Q42' (Douglas |
109
|
|
|
|
|
|
|
Adams). |
110
|
|
|
|
|
|
|
Another example C<$type> is 'description' and C<$key> is Wikidata QID like 'Q42' |
111
|
|
|
|
|
|
|
(English science fiction writer and humourist). |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Returns backend return value(s). |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 ERRORS |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
new(): |
118
|
|
|
|
|
|
|
From Class::Utils::set_params(): |
119
|
|
|
|
|
|
|
Unknown parameter '%s'. |
120
|
|
|
|
|
|
|
Backend must inherit 'Wikibase::Cache::Backend' abstract class. |
121
|
|
|
|
|
|
|
Cannot load module '%s'. |
122
|
|
|
|
|
|
|
Error: %s |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 EXAMPLE1 |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=for comment filename=get_cached_value.pl |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
use strict; |
129
|
|
|
|
|
|
|
use warnings; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
use Wikibase::Cache; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
if (@ARGV < 1) { |
134
|
|
|
|
|
|
|
print STDERR "Usage: $0 qid_or_pid\n"; |
135
|
|
|
|
|
|
|
exit 1; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
my $qid_or_pid = $ARGV[0]; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# Object. |
140
|
|
|
|
|
|
|
my $obj = Wikibase::Cache->new; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# Get translated QID. |
143
|
|
|
|
|
|
|
my $translated_qid_or_pid = $obj->get('label', $qid_or_pid) || $qid_or_pid; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# Print out. |
146
|
|
|
|
|
|
|
print $translated_qid_or_pid."\n"; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# Output for nothing: |
149
|
|
|
|
|
|
|
# Usage: ./get_cached_value.pl qid_or_pid |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
# Output for 'P31': |
152
|
|
|
|
|
|
|
# instance of |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# Output for 'Q42': |
155
|
|
|
|
|
|
|
# Q42 |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 EXAMPLE2 |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=for comment filename=save_cached_value.pl |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
use strict; |
162
|
|
|
|
|
|
|
use warnings; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
use Error::Pure qw(err); |
165
|
|
|
|
|
|
|
use Wikibase::Cache; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
$Error::Pure::TYPE = 'Error'; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# Object. |
170
|
|
|
|
|
|
|
my $obj = Wikibase::Cache->new; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Save label for 'Q42'. |
173
|
|
|
|
|
|
|
$obj->save('label', 'Q42', 'Douglas Adams'); |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# Get translated QID. |
176
|
|
|
|
|
|
|
my $translated_qid = $obj->get('label', 'Q42'); |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Print out. |
179
|
|
|
|
|
|
|
print $translated_qid."\n"; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# Output: |
182
|
|
|
|
|
|
|
# #Error [../Wikibase/Cache/Backend/Basic.pm:60] Wikibase::Cache::Backend::Basic doesn't implement save() method. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 DEPENDENCIES |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
L<Class::Utils>, |
187
|
|
|
|
|
|
|
L<English>, |
188
|
|
|
|
|
|
|
L<Error::Pure>. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 SEE ALSO |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=over |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item L<Wikibase::Cache::Backend> |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
TODO |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=back |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 REPOSITORY |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
L<https://github.com/michal-josef-spacek/Wikibase-Cache> |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 AUTHOR |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Michal Josef Špaček L<mailto:skim@cpan.org> |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
L<http://skim.cz> |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
© 2021-2023 Michal Josef Špaček |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
BSD 2-Clause License |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head1 VERSION |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
0.03 |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=cut |