NOTEDB.pm

filter: Statementstmt 9.8% 12 / 122 Branchbran 2.9% 1 / 34 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 18.7% 3 / 16 Podpod 0.0% 0 / 11 totaltotal 8.0% 16 / 198 CC 21 SCAR 29.9
CC
21
Cov
8%
lock
56.0
check_exact
30.0
use_crypt
12.0
CRAP 19.9
↑ Summary NOTEDB/mysql.pm »
1 #
2 # this is a generic module, used by note database
3 # backend modules.
4 #
5 # Copyright (c) 2000-2017 T.v.Dein <tlinden@cpan.org>
6  
7  
8 package NOTEDB;
9  
10 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use vars qw(@ISA @EXPORT $crypt_supported);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 $NOTEDB::VERSION = "1.45";
14  
15 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.4
16 # make sure, it works, otherwise encryption
17 # is not supported on this system!
18 1 eval { require Crypt::CBC; };
19 1 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 0 1
20 1 $NOTEDB::crypt_supported = 0;
21 }
22 else {
23 0 $NOTEDB::crypt_supported = 1;
24 }
25 }
26  
27  
28 0 ▶sub no_crypt {
Subroutine no_crypt: not called CC=1 SCAR=6.9
Pod undocumented
29 0 $NOTEDB::crypt_supported = 0;
30 }
31  
32  
33 0 ▶sub use_crypt {
Subroutine use_crypt: not called CC=3 SCAR=24.8
Pod undocumented
34 0 my($this,$key,$method) = @_;
35 0 my($cipher);
36 0 ▶ if($NOTEDB::crypt_supported == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crypt_supported == 1) { } 0 0 0
37 0 eval {
38 0 $cipher = new Crypt::CBC($key, $method);
39 };
40 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
41 0 print "warning: Crypt::$method not supported by system!\n";
42 0 $NOTEDB::crypt_supported = 0;
43 }
44 else {
45 0 $this->{cipher} = $cipher;
46 }
47 }
48 else{
49 0 print "warning: Crypt::CBC not supported by system!\n";
50 }
51 }
52  
53  
54 0 ▶sub use_cache {
Subroutine use_cache: not called CC=1 SCAR=6.9
Pod undocumented
55 #
56 # this sub turns on cache support
57 #
58 0 my $this = shift;
59 0 $this->{use_cache} = 1;
60 0 $this->{changed} = 1;
61 }
62  
63 0 ▶sub cache {
Subroutine cache: not called CC=2 SCAR=17.9
Pod undocumented
64 #
65 # store the whole db as hash
66 # if use_cache is turned on
67 #
68 0 my $this = shift;
69 0 ▶ if ($this->{use_cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"use_cache"}) 0 0 0
70 0 my %res = @_;
71 0 %{$this->{cache}} = %res;
72 }
73 }
74  
75 0 ▶sub unchanged {
Subroutine unchanged: not called CC=3 SCAR=24.8
Pod undocumented
76 #
77 # return true if $this->{changed} is true, this will
78 # be set to true by writing subs using $this->changed().
79 #
80 0 my $this = shift;
81 0 ▶ return 0 if(!$this->{use_cache});
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"use_cache"} 0 0 0
82 0 ▶ if ($this->{changed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'changed'}) { } 0 0 0
83 0 $this->{changed} = 0;
84 0 return 0;
85 }
86 else {
87 0 print "%\n";
88 0 return 1;
89 }
90 }
91  
92 0 ▶sub changed {
Subroutine changed: not called CC=1 SCAR=6.9
Pod undocumented
93 #
94 # turn on $this->{changed}
95 # this will be used by update or create subs.
96 #
97 0 my $this = shift;
98 0 $this->{changed} = 1;
99 0 return 1;
100 }
101  
102  
103 0 ▶sub generate_search {
Subroutine generate_search: not called CC=3 SCAR=24.8
Pod undocumented
104 #
105 # get user input and create perlcode ready for eval
106 # sample input:
107 # "ann.a OR eg???on AND u*do$"
108 # resulting output:
109 # "$match = 1 if(/ann\.a/i or /eg...on/i and /u.*do\$/i );
110 #
111 0 my($this, $string) = @_;
112  
113 0 my $case = "i";
114  
115 0 ▶ if ($string =~ /^\/.+?\/$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($string =~ m[^/.+?/$]) { } 0 0 0
elsif (not $string) { } 0 0 0
116 0 return $string;
117 }
118 elsif (!$string) {
119 0 return "/^/";
120 }
121  
122 # we will get a / in front of the first word too!
123 0 $string = " " . $string . " ";
124  
125 # check for apostrophs
126 0 $string =~ s/(?<=\s)(\(??)("[^"]+"|\S+)(\)??)(?=\s)/$1 . $this->check_exact($2) . $3/ge;
127  
128 # remove odd spaces infront of and after »and« and »or«
129 0 $string =~ s/\s\s*(AND|OR)\s\s*/ $1 /g;
130  
131 # remove odd spaces infront of »(« and after »)«
132 0 $string =~ s/(\s*\()/\(/g;
133 0 $string =~ s/(\)\s*)/\)/g;
134  
135 # remove first and last space so it will not masked!
136 0 $string =~ s/^\s//;
137 0 $string =~ s/\s$//;
138  
139 # mask spaces if not infront of or after »and« and »or«
140 0 $string =~ s/(?<!AND)(?<!OR)(\s+?)(?!AND|OR)/'\s' x length($1)/ge;
141  
142 # add first space again
143 0 $string = " " . $string;
144  
145 # lowercase AND and OR
146 0 $string =~ s/(\s??OR\s??|\s??AND\s??)/\L$1\E/g;
147  
148 # surround brackets with at least one space
149 0 $string =~ s/(?<!\\)(\)|\()/ $1 /g;
150  
151 # surround strings with slashes
152 0 $string =~ s/(?<=\s)(\S+)/ $this->check_or($1, $case) /ge;
153  
154 # remove slashes on »and« and »or«
155 0 $string =~ s/\/(and|or)\/$case/$1/g;
156  
157 # remove spaces inside /string/ constructs
158 0 $string =~ s/(?<!and)(?<!or)\s*\//\//g;
159  
160 0 $string =~ s/\/\s*(?!and|or)/\//g;
161  
162 #my $res = qq(\$match = 1 if($string););
163 0 return qq(\$match = 1 if($string););
164 #print $res . "\n";
165 #return $res;
166 }
167  
168 0 ▶sub check_or {
Subroutine check_or: not called CC=3 SCAR=24.8
Pod undocumented
169 #
170 # surrounds string with slashes if it is not
171 # »and« or »or«
172 #
173 0 my($this, $str, $case) = @_;
174 0 ▶ if ($str =~ /^\s*(or|and)\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /^\s*(or|and)\s*$/) { } 0 0 0
elsif ($str =~ /(?<!\\)[)(]/) { } 0 0 0
175 0 return " $str ";
176 }
177 elsif ($str =~ /(?<!\\)[)(]/) {
178 0 return $str;
179 }
180 else {
181 0 return " \/$str\/$case ";
182 }
183 }
184  
185  
186 0 ▶sub check_exact {
Subroutine check_exact: not called CC=5 SCAR=34.0
Pod undocumented
187 #
188 # helper for generate_search()
189 # masks special chars if string
190 # not inside ""
191 #
192 0 my($this, $str) = @_;
193  
194 0 my %wildcards = (
195 '*' => '.*',
196 '?' => '.',
197 '[' => '[',
198 ']' => ']',
199 '+' => '\+',
200 '.' => '\.',
201 '$' => '\$',
202 '@' => '\@',
203 '/' => '\/',
204 '|' => '\|',
205 '}' => '\}',
206 '{' => '\{',
207 );
208  
209 0 my %escapes = (
210 '*' => '\*',
211 '?' => '\?',
212 '[' => '[',
213 ']' => ']',
214 '+' => '\+',
215 '.' => '\.',
216 '$' => '\$',
217 '@' => '\@',
218 '(' => '\(',
219 ')' => '\)',
220 '/' => '\/',
221 '|' => '\|',
222 '}' => '\}',
223 '{' => '\{',
224 );
225  
226 # mask backslash
227 0 $str =~ s/\\/\\\\/g;
228  
229  
230 0 ▶ if ($str =~ /^"/ && $str =~ /"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^"/ and $str =~ /"$/) { } 0 0 0
Condition0%0/3
$str =~ /^"/ and $str =~ /"$/
!ll&&!rl&&r
000
MC/DC0%0/2
$str =~ /^"/ and $str =~ /"$/
$str =~ /^"/ $str =~ /"$/
Truth table0%0/3
A and B
A= $str =~ /^"/B= $str =~ /"$/
ABresult
0X0
100
111
231 # mask bracket-constructs
232 0 ▶ $str =~ s/(.)/$escapes{$1} || "$1"/ge;
Condition0%0/3
$escapes{$1} || "$1"
l!l&&r!l&&!r
000
MC/DC0%0/2
$escapes{$1} || "$1"
$escapes{$1} "$1"
Truth table0%0/3
A || B
A= $escapes{$1}B= "$1"
ABresult
1X1
011
000
233 }
234 else {
235 0 ▶ $str =~ s/(.)/$wildcards{$1} || "$1"/ge;
Condition0%0/3
$wildcards{$1} || "$1"
l!l&&r!l&&!r
000
MC/DC0%0/2
$wildcards{$1} || "$1"
$wildcards{$1} "$1"
Truth table0%0/3
A || B
A= $wildcards{$1}B= "$1"
ABresult
1X1
011
000
236 }
237  
238 0 $str =~ s/^"//;
239 0 $str =~ s/"$//;
240  
241 # mask spaces
242 0 $str =~ s/\s/\\s/g;
243 0 return $str;
244 }
245  
246  
247  
248  
249 0 ▶sub lock {
Subroutine lock: not called CC=7 SCAR=40.3
Pod undocumented
250 0 my ($this) = @_;
251  
252 0 ▶ if (-e $this->{LOCKFILE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $this->{"LOCKFILE"}) 0 0 0
253 0 ▶ open LOCK, "<$this->{LOCKFILE}" or die "could not open $this->{LOCKFILE}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open LOCK, qq[<$this->{"LOCKFILE"}] or die qq[could not open $this->{"LOCKFILE"}: $!\n] 0 0 0
254 0 my $data = <LOCK>;
255 0 close LOCK;
256 0 chomp $data;
257 0 print "-- waiting for lock by $data --\n";
258 0 print "-- remove the lockfile if you are sure: \"$this->{LOCKFILE}\" --\n";
259 }
260  
261 0 my $timeout = 60;
262  
263 0 eval {
264 0 ▶ local $SIG{ALRM} = sub { die "timeout" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
265 0 ▶ local $SIG{INT} = sub { die "interrupted" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
266 0 alarm $timeout - 2;
267 0 while (1) {
268 0 ▶ if (! -e $this->{LOCKFILE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $this->{"LOCKFILE"}) 0 0 0
269 0 umask 022;
270 0 ▶ open LOCK, ">$this->{LOCKFILE}" or die "could not open $this->{LOCKFILE}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open LOCK, qq[>$this->{"LOCKFILE"}] or die qq[could not open $this->{"LOCKFILE"}: $!\n] 0 0 0
271 0 flock LOCK, LOCK_EX;
272  
273 0 my $now = scalar localtime();
274 0 print LOCK "$ENV{USER} since $now (PID: $$)\n";
275  
276 0 flock LOCK, LOCK_UN;
277 0 close LOCK;
278 0 alarm 0;
279 0 return 0;
280 }
281 0 printf " %0d\r", $timeout;
282 0 $timeout--;
283 0 sleep 1;
284 }
285 };
286 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
287 0 ▶ if ($@ =~ /^inter/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^inter/) { } 0 0 0
288 0 print " interrupted\n";
289 }
290 else {
291 0 print $@;
292 0 print " timeout\n";
293 }
294 0 return 1;
295 }
296 0 return 0;
297 }
298  
299 0 ▶sub unlock {
Subroutine unlock: not called CC=1 SCAR=6.9
Pod undocumented
300 0 my ($this) = @_;
301 0 unlink $this->{LOCKFILE};
302 }
303  
304  
305  
306 1 1;
↑ Summary NOTEDB/mysql.pm »