Listman.pm

filter: Statementstmt 10.6% 23 / 217 Branchbran 0.0% 0 / 116 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 24.1% 7 / 29 Podpod 100% 17 / 17 totaltotal 10.8% 47 / 433 CC 80 SCAR 40.3
CC
80
Cov
6%
commit
210.0
_dbi_setup
110.0
check_params
56.0
CRAP 56.1
« Listman/dictionary.pm ↑ Summary Listman/dictionary/term.pm »
1 # Listman.pm - this file is part of the CGI::Listman distribution
2 #
3 # CGI::Listman is Copyright (C) 2002 iScream multimédia <info@iScream.ca>
4 #
5 # This package is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7 #
8 # Author: Wolfgang Sourdeau <Wolfgang@Contre.COM>
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package CGI::Listman;
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use CGI::Listman::dictionary;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use CGI::Listman::line;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use CGI::Listman::selection;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 $VERSION = '0.05';
23  
24 =pod
25  
26 =head1 NAME
27  
28 CGI::Listman - Easily managing web subscribtion lists
29  
30 =head1 SYNOPSIS
31  
32 use CGI::Listman;
33  
34 =head1 DESCRIPTION
35  
36 CGI::Listman provides an object-oriented interface to easily manage
37 web-based subscribtion lists. It implements concepts such as
38 "dictionaries", "selections", "exporters", provides some checking
39 facilities (field duplication or requirements) and uses the DBI interface
40 so as to provide a backend-independent storage area (PostgreSQL, ...).
41  
42 The I<CGI::Listman> class manages the listmanagers of your project. This
43 is the very first class you want to instantiate. It is the logical
44 central point of all others objects. Except for I<CGI::Listman::line>,
45 I<CGI::Listman::exporter> and I<CGI::Listman::selection>, you should not
46 call any other class's "new" method since I<CGI::Listman> will handle its
47 own instances for you.
48  
49 =head1 API
50  
51 =head2 new
52  
53 As for any perl class, new acts as the constructor for an instance of this
54 class. It has three optional arguments that, if not specified, can be
55 replaced with calls to the respective methods: I<set_backend>,
56 I<set_list_name>, I<set_list_directory>.
57  
58 =over
59  
60 =item Parameters
61  
62 All the parameters are optional with this method.
63  
64 =over
65  
66 =item backend
67  
68 A string representing the I<DBI> backend to be used. (Warning: only "CSV"
69 and "mysql" are supported at this time.)
70  
71 =item list filename
72  
73 A string representing the base filename for the dictionary and the
74 storage file (for the CVS backend).
75  
76 =item list directory
77  
78 A string representing the directory where the list data will be stored.
79  
80 =back
81  
82 =item Return values
83  
84 A reference to a blessed instance of I<CGI::Listman>.
85  
86 =item Examples
87  
88 =over
89  
90 =item 1
91  
92 my $list_manager = CGI::Listman->new; # creates a simple list
93 # manager without any
94 # arguments
95  
96 =item 2
97  
98 # creates a list manager by specifying the backend, the filename
99 # and the storage directory
100 my $list_manager = CGI::Listman->new ('CSV', 'userlist', '/var/lib/weblists');
101  
102 =back
103  
104 =back
105  
106 =cut
107  
108 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
109 0 my $class = shift;
110  
111 0 my $self = {};
112 0 $self->{'dbi_backend'} = shift;
113 0 $self->{'list_name'} = shift;
114 0 $self->{'list_dir'} = shift;
115 0 $self->{'table_name'} = $self->{'list_name'};
116 0 $self->{'db_name'} = undef;
117 0 $self->{'db_uname'} = undef;
118 0 $self->{'db_passwd'} = undef;
119 0 $self->{'db_host'} = undef;
120 0 $self->{'db_port'} = undef;
121  
122 0 $self->{'list'} = undef;
123 0 $self->{'_dbi_params'} = undef;
124 0 $self->{'_dictionary'} = undef;
125 0 $self->{'_last_line_number'} = 0;
126 0 $self->{'_loading_list'} = undef;
127  
128 0 bless $self, $class;
129 }
130  
131 =pod
132  
133 =head2 set_backend
134  
135 Defines the DBI backend used to store the list data.
136  
137 =over
138  
139 =item Parameters
140  
141 =over
142  
143 =item backend
144  
145 A string representing the I<DBI> backend. As noted before only 'CSV' and
146 'mysql' are currently supported. More will be supported in the future.
147  
148 =back
149  
150 =item Return values
151  
152 This method returns nothing.
153  
154 =back
155  
156 =cut
157  
158 0 ▶sub set_backend {
Subroutine set_backend: not called CC=3 SCAR=24.8
Pod documented
159 0 my ($self, $backend) = @_;
160  
161 0 ▶ if (defined $self->{'dbi_backend'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'dbi_backend'}) { } 0 0 0
162 croak "A backend is already defined ("
163 0 .$self->{'dbi_backend'}.") for this CGI::Listman instance.\n"
164 } else {
165 0 eval "use DBD::".$backend.";";
166 0 ▶ croak "This backend is not available:\n".$@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
167 0 $self->{'dbi_backend'} = $backend;
168 }
169 }
170  
171 =pod
172  
173 =head2 set_db_name
174  
175 Defines the database where the list data has to be stored.
176  
177 =over
178  
179 =item Parameters
180  
181 =over
182  
183 =item db_name
184  
185 A string representing the database name. This information is required for
186 non-file-based storage databases.
187  
188 =back
189  
190 =item Return values
191  
192 This method returns nothing.
193  
194 =back
195  
196 =cut
197  
198 0 ▶sub set_db_name {
Subroutine set_db_name: not called CC=3 SCAR=24.8
Pod documented
199 0 my ($self, $db_name) = @_;
200  
201 croak "A database is already defined (".$self->{'db_name'}
202 .") for this CGI::Listman instance.\n"
203 0 ▶ if (defined $self->{'db_name'} && $self->{'db_name'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"db_name"} and $self->{"db_name"} ne "" 0 0 0
Condition0%0/3
defined $self->{"db_name"} and $self->{"db_name"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"db_name"} and $self->{"db_name"} ne ""
defined $self->{"db_name"} $self->{"db_name"} ne ""
Truth table0%0/3
A and B
A= defined $self->{"db_name"}B= $self->{"db_name"} ne ""
ABresult
0X0
100
111
204  
205 0 $self->{'db_name'} = $db_name;
206 }
207  
208 =pod
209  
210 =head2 set_user_infos
211  
212 Defines the username and password needed to connect to the database.
213  
214 =over
215  
216 =item Parameters
217  
218 =over
219  
220 =item username
221  
222 A string representing the username.
223  
224 =item password
225  
226 A string representing the password.
227  
228 =back
229  
230 =item Return values
231  
232 This method returns nothing.
233  
234 =back
235  
236 =cut
237  
238 0 ▶sub set_user_infos {
Subroutine set_user_infos: not called CC=5 SCAR=34.0
Pod documented
239 0 my ($self, $db_uname, $db_passwd) = @_;
240  
241 croak "A password is already defined for this CGI::Listman instance.\n"
242 0 ▶ if (defined $self->{'db_passwd'} && $self->{'db_passwd'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"db_passwd"} and $self->{"db_passwd"} ne "" 0 0 0
Condition0%0/3
defined $self->{"db_passwd"} and $self->{"db_passwd"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"db_passwd"} and $self->{"db_passwd"} ne ""
defined $self->{"db_passwd"} $self->{"db_passwd"} ne ""
Truth table0%0/3
A and B
A= defined $self->{"db_passwd"}B= $self->{"db_passwd"} ne ""
ABresult
0X0
100
111
243 croak "A username is already defined (".$self->{'db_uname'}
244 .") for this CGI::Listman instance.\n"
245 0 ▶ if (defined $self->{'db_uname'} && $self->{'db_uname'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"db_uname"} and $self->{"db_uname"} ne "" 0 0 0
Condition0%0/3
defined $self->{"db_uname"} and $self->{"db_uname"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"db_uname"} and $self->{"db_uname"} ne ""
defined $self->{"db_uname"} $self->{"db_uname"} ne ""
Truth table0%0/3
A and B
A= defined $self->{"db_uname"}B= $self->{"db_uname"} ne ""
ABresult
0X0
100
111
246  
247 0 $self->{'db_uname'} = $db_uname;
248 0 $self->{'db_passwd'} = $db_passwd;
249 }
250  
251 =pod
252  
253 =head2 set_host_infos
254  
255 Defines the hostname and port where the database resides. The use of this
256 function might not absolutely be needed. For example, the "mysql" backend
257 default's host is "localhost". So if your database is stored on the same
258 machine as your webserver, you will not need to use this function.
259  
260 =over
261  
262 =item Parameters
263  
264 =over
265  
266 =item hostname
267  
268 A string representing the hostname of the machine your database engine is
269 running on.
270  
271 =item port
272  
273 An integer representing the TCP/IP port your database daemon is listening on.
274  
275 =back
276  
277 =item Return values
278  
279 This method returns nothing.
280  
281 =back
282  
283 =cut
284  
285 0 ▶sub set_host_infos {
Subroutine set_host_infos: not called CC=5 SCAR=34.0
Pod documented
286 0 my ($self, $db_host, $db_port) = @_;
287  
288 croak "A hostname/address is already defined (".$self->{'db_host'}
289 .") for this CGI::Listman instance.\n"
290 0 ▶ if (defined $self->{'db_host'} && $self->{'db_host'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"db_host"} and $self->{"db_host"} ne "" 0 0 0
Condition0%0/3
defined $self->{"db_host"} and $self->{"db_host"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"db_host"} and $self->{"db_host"} ne ""
defined $self->{"db_host"} $self->{"db_host"} ne ""
Truth table0%0/3
A and B
A= defined $self->{"db_host"}B= $self->{"db_host"} ne ""
ABresult
0X0
100
111
291 croak "A port is already defined (".$self->{'db_port'}
292 .") for this CGI::Listman instance.\n"
293 0 ▶ if (defined $self->{'db_port'} && $self->{'db_port'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"db_port"} and $self->{"db_port"} ne "" 0 0 0
Condition0%0/3
defined $self->{"db_port"} and $self->{"db_port"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"db_port"} and $self->{"db_port"} ne ""
defined $self->{"db_port"} $self->{"db_port"} ne ""
Truth table0%0/3
A and B
A= defined $self->{"db_port"}B= $self->{"db_port"} ne ""
ABresult
0X0
100
111
294  
295 0 $self->{'db_host'} = $db_host;
296 0 $self->{'db_port'} = $db_port;
297 }
298  
299 =pod
300  
301 =head2 set_list_name
302  
303 Gives a name to your list.
304  
305 =over
306  
307 =item Parameters
308  
309 =over
310  
311 =item name
312  
313 A string representing the name of your list, which it turns define the
314 base name for various storage files. The name of the list's dictionary
315 (see L<CGI::Listman::dictionary>) will be deduced from it as well as its
316 CSV "database" file if ever.
317  
318 =back
319  
320 =item Return values
321  
322 This method returns nothing.
323  
324 =back
325  
326 =cut
327  
328 0 ▶sub set_list_name {
Subroutine set_list_name: not called CC=3 SCAR=24.8
Pod documented
329 0 my ($self, $list_name) = @_;
330  
331 croak "A list name is already defined (".$self->{'list_name'}
332 .") for this instance of CGI::Listman.\n"
333 0 ▶ if (defined $self->{'list_name'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"list_name"} 0 0 0
334  
335 0 $self->{'list_name'} = $list_name;
336 $self->{'table_name'} = $list_name
337 0 ▶ unless (defined $self->{'table_name'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"table_name"} 0 0 0
338 }
339  
340 =pod
341  
342 =head2 set_list_directory
343  
344 Defines where the list's dictionary and data files are stored.
345  
346 =over
347  
348 =item Parameters
349  
350 =over
351  
352 =item directory
353  
354 A string representing the directory where this instance of
355 I<CGI::Listman> will have its data files stored.
356  
357 =back
358  
359 =item Return values
360  
361 This method returns nothing.
362  
363 =back
364  
365 =cut
366  
367 0 ▶sub set_list_directory {
Subroutine set_list_directory: not called CC=2 SCAR=17.9
Pod documented
368 0 my ($self, $new_directory) = @_;
369  
370 croak "A list directory is already defined (".$self->{'list_name'}
371 .") for this instance of CGI::Listman.\n"
372 0 ▶ if (defined $self->{'list_dir'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"list_dir"} 0 0 0
373 0 $self->{'list_dir'} = $new_directory;
374 }
375  
376 =pod
377  
378 =head2 set_table_name
379  
380 For "real" (i.e. everything except "CSV") database backends, gives the
381 name of the table the list is stored into. If not called, the list name
382 will be used.
383  
384 =over
385  
386 =item Parameters
387  
388 =over
389  
390 =item table name
391  
392 A string representing the table name of your list for use with databases.
393  
394 =back
395  
396 =item Return values
397  
398 This method returns nothing.
399  
400 =back
401  
402 =cut
403  
404 0 ▶sub set_table_name {
Subroutine set_table_name: not called CC=2 SCAR=17.9
Pod documented
405 0 my ($self, $table_name) = @_;
406  
407 croak "Please defined a list_name before a table_name.\n"
408 0 ▶ unless (defined $self->{'table_name'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"table_name"} 0 0 0
409  
410 0 croak "The table_name cannot be empty.\n"
411 if ('table_name' eq '');
412 0 $self->{'table_name'} = $table_name;
413 }
414  
415 =pod
416  
417 =head2 dictionary
418  
419 Obtain the dictionary of this instance (there is only one dictionary for
420 each instance). This method will automatically create and read the list's
421 dictionary for you if needed.
422  
423 =over
424  
425 =item Parameters
426  
427 This method takes no parameter.
428  
429 =item Return values
430  
431 A reference to an instance of I<CGI::Listman::dictionary>.
432  
433 =back
434  
435 =cut
436  
437 0 ▶sub dictionary {
Subroutine dictionary: not called CC=5 SCAR=34.0
Pod documented
438 0 my $self = shift;
439  
440 0 ▶ unless (defined $self->{'_dictionary'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_dictionary"}) 0 0 0
441 croak "List directory not defined for this instance of CGI::Listman.\n"
442 0 ▶ unless (defined $self->{'list_dir'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"list_dir"} 0 0 0
443 croak "List filename not defined for this instance of CGI::Listman.\n"
444 0 ▶ unless (defined $self->{'list_name'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"list_name"} 0 0 0
445  
446 0 my $path = $self->{'list_dir'}.'/'.$self->{'list_name'}.'.dict';
447 0 ▶ croak "No dictionary ('".$self->{'list_name'}.".dict')\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 0 0 0
448 unless (-f $path);
449  
450 0 my $dictionary = CGI::Listman::dictionary->new ($path);
451  
452 0 $self->{'_dictionary'} = $dictionary;
453 }
454  
455 0 return $self->{'_dictionary'};
456 }
457  
458 =pod
459  
460 =head2 seek_line_by_num
461  
462 Returns the n'th I<CGI::Listman::line> of this instance.
463  
464 =over
465  
466 =item Parameters
467  
468 =over
469  
470 =item number
471  
472 An integer representing the requested I<CGI::Listman::line>.
473  
474 =back
475  
476 =item Return values
477  
478 A reference to an instance of I<CGI::Listman::line>.
479  
480 =back
481  
482 =cut
483  
484 0 ▶sub seek_line_by_num {
Subroutine seek_line_by_num: not called CC=4 SCAR=30.0
Pod documented
485 0 my ($self, $number) = @_;
486  
487 0 ▶ $self->load_lines () unless (defined $self->{'list'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"list"} 0 0 0
488  
489 0 my $ret_line = undef;
490 0 my $list_ref = $self->{'list'};
491  
492 0 foreach my $line (@$list_ref) {
493 0 ▶ if ($line->number () == $number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->number == $number) 0 0 0
494 0 $ret_line = $line;
495 0 last;
496 }
497 }
498  
499 0 return $ret_line;
500 }
501  
502 =pod
503  
504 =head2 add_line
505  
506 Add a I<CGI::Listman::line> (see L<CGI::Listman::line> to this instance's
507 list of lines.
508  
509 =over
510  
511 =item Parameters
512  
513 =over
514  
515 =item line
516  
517 An instance of I<CGI::Listman::line> to be added to this list manager.
518  
519 =back
520  
521 =item Return values
522  
523 This method returns nothing.
524  
525 =back
526  
527 =cut
528  
529 0 ▶sub add_line {
Subroutine add_line: not called CC=6 SCAR=37.4
Pod documented
530 0 my ($self, $line) = @_;
531  
532 $self->load_lines ()
533 unless (defined $self->{'list'}
534 0 ▶ || defined $self->{'_loading_list'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"list"} or defined $self->{"_loading_list"} 0 0 0
Condition0%0/3
defined $self->{"list"} or defined $self->{"_loading_list"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $self->{"list"} or defined $self->{"_loading_list"}
defined $self->{"list"} defined $self->{"_loading_list"}
Truth table0%0/3
A or B
A= defined $self->{"list"}B= defined $self->{"_loading_list"}
ABresult
1X1
011
000
535  
536 $line->{'number'} = $self->{'_last_line_number'} + 1
537 0 ▶ unless ($line->{'number'});
Branch: 1 branch
BranchTrueFalseTotal
unless $line->{"number"} 0 0 0
538  
539 0 my @numbers = $self->_get_line_numbers ();
540 croak "This instance's list of lines already contains a line with"
541 0 ▶ ." this number (".$line->{'number'}.").\n"
Branch: 1 branch
BranchTrueFalseTotal
if grep /$line->{"number"}/, @numbers 0 0 0
542 if (grep (m/$line->{'number'}/, @numbers));
543  
544 0 $self->{'_last_line_number'} = $line->{'number'};
545  
546 0 ▶ unless (defined $self->{'list'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"list"}) 0 0 0
547 0 my @new_list;
548 0 $self->{'list'} = \@new_list;
549 }
550  
551 0 my $list_ref = $self->{'list'};
552 0 push @$list_ref, $line;
553 }
554  
555 =pod
556  
557 =head2 load_lines
558  
559 Loads the line from the list database or storage file. This function is
560 deprecated and will probably be removed or made private in a later
561 release.
562  
563 =over
564  
565 =item Parameters
566  
567 This method takes no argument.
568  
569 =item Return values
570  
571 This method returns nothing.
572  
573 =back
574  
575 =cut
576  
577 0 ▶sub load_lines {
Subroutine load_lines: not called CC=5 SCAR=34.0
Pod documented
578 0 my $self = shift;
579  
580 0 $self->{'_loading_list'} = 1;
581 0 $self->_db_connect ();
582  
583 0 my $dbh = $self->{'_db_connection'};
584  
585 my $row_list =
586 0 ▶ $dbh->selectall_arrayref ("SELECT * FROM ".$self->{'table_name'})
Branch: 1 branch
BranchTrueFalseTotal
my $row_list = $dbh->selectall_arrayref('SELECT * FROM ' . $self->{"table_name"}) or croak($dbh->errstr) 0 0 0
587 or croak $dbh->errstr;
588  
589 # croak $row_list->[0];
590 0 ▶ delete $self->{'list'} if (defined $self->{'list'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"list"} 0 0 0
591  
592 0 ▶ if (defined $row_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row_list) 0 0 0
593 0 foreach my $row (@$row_list) {
594 0 my $line = CGI::Listman::line->new ();
595 0 $line->_build_from_listman_data ($row);
596 0 $self->add_line ($line);
597 }
598 }
599  
600 0 $self->{'_loading_list'} = undef;
601 }
602  
603 =pod
604  
605 =head2 list_contents
606  
607 Returns a reference to an ARRAY of the list's lines. This method takes
608 care of preloading the list from the database if needed.
609  
610 =over
611  
612 =item Parameters
613  
614 This method takes no argument.
615  
616 =item Return values
617  
618 A reference to the ARRAY of I<CGI::Listman::line> of this list manager
619 object.
620  
621 =back
622  
623 =cut
624  
625 0 ▶sub list_contents {
Subroutine list_contents: not called CC=4 SCAR=30.0
Pod documented
626 0 my $self = shift;
627  
628 0 my $contents_ref = undef;
629 0 ▶ if (defined $self->{'list'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'list'}) { } 0 0 0
630 0 my @filt_contents;
631 0 my $old_cref = $self->{'list'};
632 0 foreach my $line (@$old_cref) {
633 push @filt_contents, $line
634 0 ▶ if (!$line->{'_deleted'});
Branch: 1 branch
BranchTrueFalseTotal
unless $line->{"_deleted"} 0 0 0
635 }
636 0 $contents_ref = \@filt_contents;
637 } else {
638 0 $self->load_lines ();
639 0 $contents_ref = $self->{'list'};
640 }
641  
642 0 return $contents_ref;
643 }
644  
645 =pod
646  
647 =head2 check_params
648  
649 This method checks the presence in the hash ref of keys that are marked
650 as mandatory in the instance's dictionary. It returns two ARRAY
651 references, the first of which lists the missing mandatory fields, the
652 second being a list of the fields that are not present in the dictionary.
653  
654 =over
655  
656 =item Parameters
657  
658 =over
659  
660 =item fields_hashref
661  
662 A reference to a HASH whereof the keys are the names CGI fields.
663  
664 =back
665  
666 =item Return values
667  
668 =over
669  
670 =item missing_arrayref
671  
672 A reference to an array of mandatory fields (see
673 L<CGI::Listman::dictionary::term> that were missing from
674 I<parameters_hashref>.
675  
676 =item unknown_arrayref
677  
678 A reference to an array of "unknown fields". That is, fields that were
679 part of I<parameters_hashref> but that were not found in the dictionary.
680  
681 =back
682  
683 =back
684  
685 =cut
686  
687 # Check the validity of received parameters and returns two refs against
688 # the missing mandatory values and the unknown fields.
689 0 ▶sub check_params {
Subroutine check_params: not called CC=7 SCAR=40.3
Pod documented
690 0 my ($self, $param_hash_ref) = @_;
691  
692 0 my $dictionary = $self->dictionary ();
693  
694 0 my @missing;
695 my @unknown;
696  
697 0 foreach my $key (keys %$param_hash_ref) {
698 0 my $term = $dictionary->get_term ($key);
699 0 ▶ push @unknown, $key
Branch: 1 branch
BranchTrueFalseTotal
unless defined $term 0 0 0
700 unless (defined $term);
701 }
702  
703 0 my $dict_terms = $dictionary->terms ();
704  
705 0 foreach my $term (@$dict_terms) {
706 0 my $key = $term->{'key'};
707 push @missing, $term->definition_or_key ()
708 if ($term->{'mandatory'}
709 && (!defined $param_hash_ref->{$key}
710 0 ▶ || $param_hash_ref->{$key} eq ''));
Branch: 1 branch
BranchTrueFalseTotal
if $term->{"mandatory"} and !defined($param_hash_ref->{$key}) || $param_hash_ref->{$key} eq "" 0 0 0
Condition0%0/6
$term->{"mandatory"} and !defined($param_hash_ref->{$key}) || $param_hash_ref->{$key} eq ""
!ll&&!rl&&r
000
not defined $param_hash_ref->{$key} or $param_hash_ref->{$key} eq ""
l!l&&r!l&&!r
000
MC/DC0%0/3
$term->{"mandatory"} and !defined($param_hash_ref->{$key}) || $param_hash_ref->{$key} eq ""
$term->{"mandatory"} not defined $param_hash_ref->{$key} $param_hash_ref->{$key} eq ""
Truth table0%0/4
A and not(B) or C
A= $term->{"mandatory"}B= not defined $param_hash_ref->{$key}C= $param_hash_ref->{$key} eq ""
ABCresult
0XX0
1000
11X1
1011
711 }
712  
713 0 return (\@missing, \@unknown);
714 }
715  
716 =pod
717  
718 =head2 commit
719  
720 This method commits any changes made to your instance, after which, that
721 instance will be invalidated. As long as it is not called, you can of
722 course apply any modifications to your instance. This limitation will
723 probably be got rid of in a next release.
724  
725 =over
726  
727 =item Parameters
728  
729 This method takes no argument.
730  
731 =item Return values
732  
733 This method returns nothing.
734  
735 =back
736  
737 =cut
738  
739 0 ▶sub commit {
Subroutine commit: not called CC=14 SCAR=53.5
Pod documented
740 0 my $self = shift;
741  
742 croak "Commit again?\n"
743 0 ▶ if (defined $self->{'_commit'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_commit"} 0 0 0
744  
745 0 ▶ if (defined $self->{'list'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"list"}) 0 0 0
746 0 $self->_db_connect ();
747 0 my $dbh = $self->{'_db_connection'};
748 0 my $list_ref = $self->{'list'};
749 0 foreach my $line (@$list_ref) {
750 0 ▶ if ($line->{'_updated'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"_updated"}) 0 0 0
751 0 ▶ next if ($line->{'_deleted'} && $line->{'_new_line'});
Branch: 1 branch
BranchTrueFalseTotal
if $line->{"_deleted"} and $line->{"_new_line"} 0 0 0
Condition0%0/3
$line->{"_deleted"} and $line->{"_new_line"}
!ll&&!rl&&r
000
MC/DC0%0/2
$line->{"_deleted"} and $line->{"_new_line"}
$line->{"_deleted"} $line->{"_new_line"}
Truth table0%0/3
A and B
A= $line->{"_deleted"}B= $line->{"_new_line"}
ABresult
0X0
100
111
752 0 ▶ if ($line->{'_deleted'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line->{'_deleted'}) { } 0 0 0
elsif ($line->{'_new_line'}) { } 0 0 0
753 $dbh->do ("DELETE FROM ".$self->{'table_name'}.
754 " WHERE number = ".$line->{'number'})
755 or croak "A DBI error occured while deleting line "
756 0 ▶ .$line->{'number'}." from ".$self->{'table_name'}
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do('DELETE FROM ' . $self->{"table_name"} . ' WHERE number = ' . $line->{"number"}) or croak('A DBI error occured while deleting line ' . $line->{"number"} . ' from ' . $self->{"table_name"} . ":\n" . $dbh->errstr) 0 0 0
757 .":\n".$dbh->errstr;
758 } elsif ($line->{'_new_line'}) {
759 $line->{'timestamp'} = time ()
760 0 ▶ unless ($line->{'timestamp'});
Branch: 1 branch
BranchTrueFalseTotal
unless $line->{"timestamp"} 0 0 0
761 0 my $record = $self->_prepare_record ($line);
762 my $sth = $dbh->do ("INSERT INTO ".$self->{'table_name'}.
763 " VALUES (".$record.")")
764 or croak "A DBI error occured while inserting...\n".$record.
765 0 ▶ "... into ".$self->{'table_name'}.":\n".$dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->do('INSERT INTO ' . $self->{"table_name"} . ' VALUES (' . $record . ')') or croak("A DBI error occured while inserting...\n" . $record . '... into ' . $self->{"table_name"} . ":\n" . $dbh->errstr) 0 0 0
766 } else {
767 $dbh->do ("DELETE FROM ".$self->{'table_name'}.
768 " WHERE number = ".$line->{'number'})
769 or croak "A DBI error occured while deleting line "
770 0 ▶ .$line->{'number'}." from ".$self->{'table_name'}
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do('DELETE FROM ' . $self->{"table_name"} . ' WHERE number = ' . $line->{"number"}) or croak('A DBI error occured while deleting line ' . $line->{"number"} . ' from ' . $self->{"table_name"} . ":\n" . $dbh->errstr) 0 0 0
771 .":\n".$dbh->errstr;
772 0 my $record = $self->_prepare_record ($line);
773 my $sth = $dbh->do ("INSERT INTO ".$self->{'table_name'}.
774 " VALUES (".$record.")")
775 or croak "A DBI error occured while inserting...\n".$record.
776 0 ▶ "... into ".$self->{'table_name'}.":\n".$dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->do('INSERT INTO ' . $self->{"table_name"} . ' VALUES (' . $record . ')') or croak("A DBI error occured while inserting...\n" . $record . '... into ' . $self->{"table_name"} . ":\n" . $dbh->errstr) 0 0 0
777 }
778 }
779 }
780 0 $dbh->disconnect ();
781 }
782  
783 0 $self->{'_commit'} = 1;
784 }
785  
786 =pod
787  
788 =head2 delete_line
789  
790 Delete a I<CGI::Listman::line> from this instance's list of lines.
791  
792 =over
793  
794 =item Parameters
795  
796 An instance of I<CGI::Listman::line> to be removed from this list manager.
797  
798 =item Return values
799  
800 This method returns nothing.
801  
802 =back
803  
804 =cut
805  
806 0 ▶sub delete_line {
Subroutine delete_line: not called CC=6 SCAR=37.4
Pod documented
807 0 my ($self, $line) = @_;
808  
809 croak "Cannot delete a line with number equal to 0.\n"
810 0 ▶ unless ($line->{'number'});
Branch: 1 branch
BranchTrueFalseTotal
unless $line->{"number"} 0 0 0
811  
812 0 my $list_ref = $self->{'list'};
813 0 ▶ croak "List empty.\n" unless (defined $list_ref);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list_ref 0 0 0
814  
815 # delete the line from the list in memory...
816 0 my $count;
817 0 ▶ for ($count = 0; $count < @$list_ref; $count++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < @$list_ref) 0 0 0
818 0 ▶ if ($list_ref->[$count] == $line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_ref->[$count] == $line) 0 0 0
819 0 $line->{'_updated'} = 1;
820 0 $line->{'_deleted'} = 1;
821 0 last;
822 }
823 }
824  
825 0 ▶ croak "Line not found in list."
Branch: 1 branch
BranchTrueFalseTotal
if $count == @$list_ref 0 0 0
826 if ($count == @$list_ref);
827 }
828  
829 =pod
830  
831 =head2 delete_selection
832  
833 Delete many lines at the same time through the use of a
834 I<CGI::Listman::selection> (see L<CGI::Listman::selection>).
835  
836 =over
837  
838 =item Parameters
839  
840 An instance of I<CGI::Listman::selection> made of lines to be removed
841 from this list manager.
842  
843 =item Return values
844  
845 This method returns nothing.
846  
847 =back
848  
849 =cut
850  
851 0 ▶sub delete_selection {
Subroutine delete_selection: not called CC=3 SCAR=24.8
Pod documented
852 0 my ($self, $selection) = @_;
853  
854 0 my $list_ref = $selection->{'list'};
855 0 ▶ croak "Selection is empty.\n" unless ($list_ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $list_ref 0 0 0
856 0 foreach my $line (@$list_ref) {
857 0 $self->delete_line ($line);
858 }
859 }
860  
861 # the private methods begin here
862  
863 0 ▶sub _prepare_record {
Subroutine _prepare_record: not called CC=3 SCAR=24.8
864 0 my ($self, $line) = @_;
865  
866 0 my $fields_ref = $line->line_fields ();
867 0 my @records;
868 0 push @records, ($line->{'timestamp'}, $line->{'seen'}, $line->{'exported'});
869 0 push @records, @$fields_ref;
870  
871 0 my $record_line = "'".$line->{'number'}."'";
872 0 foreach my $record (@records) {
873 0 $record =~ s/\'/\\\'/g;
874 0 ▶ $record = '' unless (defined $record);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record 0 0 0
875 0 $record_line .= ", '".$record."'";
876 }
877  
878 # if we don't untaint $record_line, we get a stange error regarding
879 # DBD::SQL::Statement::HASH_ref...
880 0 $record_line =~ m/(.*)/;
881 0 $record_line = $1;
882  
883 0 return $record_line;
884 }
885  
886 0 ▶sub _dbi_setup {
Subroutine _dbi_setup: not called CC=10 SCAR=47.0
887 0 my $self = shift;
888  
889 0 ▶ unless (defined $self->{'_dbi_params'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_dbi_params"}) 0 0 0
890 croak "No backend specified for this instance of CGI::Listman.\n"
891 0 ▶ unless (defined $self->{'dbi_backend'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"dbi_backend"} 0 0 0
892 0 ▶ if ($self->{'dbi_backend'} eq 'CSV') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'dbi_backend'} eq 'CSV') { } 0 0 0
893 0 $self->{'_dbi_params'} = ":f_dir=".$self->{'list_dir'};
894 0 ▶ unless (-f $self->{'list_dir'}.'/'.$self->{'table_name'}.'.csv') {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $self->{"list_dir"} . '/' . $self->{"table_name"} . '.csv') 0 0 0
895 open my $list_file, '>'
896 0 .$self->{'list_dir'}.'/'.$self->{'table_name'}.'.csv';
897 0 close $list_file;
898 }
899 } else {
900 croak "Sorry, the DBI backend \"".$self->{'dbi_backend'}
901 ."\" is not handled at this time.\n"
902 0 ▶ unless ($self->{'dbi_backend'} eq 'mysql');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbi_backend"} eq "mysql" 0 0 0
903 0 my $dbi_params = ":database=".$self->{'db_name'};
904 $dbi_params .= ":host=".$self->{'db_host'}
905 0 ▶ if (defined $self->{'db_host'} && $self->{'db_host'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"db_host"} and $self->{"db_host"} ne "" 0 0 0
Condition0%0/3
defined $self->{"db_host"} and $self->{"db_host"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"db_host"} and $self->{"db_host"} ne ""
defined $self->{"db_host"} $self->{"db_host"} ne ""
Truth table0%0/3
A and B
A= defined $self->{"db_host"}B= $self->{"db_host"} ne ""
ABresult
0X0
100
111
906 $dbi_params .= ":port=".$self->{'db_port'}
907 0 ▶ if (defined $self->{'db_port'} && $self->{'db_port'} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"db_port"} and $self->{"db_port"} ne "" 0 0 0
Condition0%0/3
defined $self->{"db_port"} and $self->{"db_port"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"db_port"} and $self->{"db_port"} ne ""
defined $self->{"db_port"} $self->{"db_port"} ne ""
Truth table0%0/3
A and B
A= defined $self->{"db_port"}B= $self->{"db_port"} ne ""
ABresult
0X0
100
111
908 0 $self->{'_dbi_params'} = $dbi_params;
909 }
910 }
911 }
912  
913 0 ▶sub _db_fields_setup {
Subroutine _db_fields_setup: not called CC=3 SCAR=24.8
914 0 my $self = shift;
915  
916 0 ▶ unless (defined $self->{'_db_fields'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_db_fields"}) 0 0 0
917 0 my @fields = ('number', 'timestamp', 'seen', 'exported');
918 0 my $dictionary = $self->dictionary ();
919 0 my $dict_terms = $dictionary->terms ();
920  
921 0 foreach my $term (@$dict_terms) {
922 0 push @fields, $term->{'key'};
923 }
924 0 $self->{'_db_fields'} = \@fields;
925 }
926 }
927  
928 0 ▶sub _db_connect {
Subroutine _db_connect: not called CC=4 SCAR=30.0
929 0 my $self = shift;
930  
931 0 ▶ unless (defined $self->{'_db_connection'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_db_connection"}) 0 0 0
932 0 $self->_dbi_setup ();
933 0 $self->_db_fields_setup ();
934 my $dbh = DBI->connect ("DBI:"
935 .$self->{'dbi_backend'}.$self->{'_dbi_params'},
936 $self->{'db_uname'},
937 0 ▶ $self->{'db_passwd'})
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect('DBI:' . $self->{"dbi_backend"} . $self->{"_dbi_params"}, $self->{"db_uname"}, $self->{"db_passwd"}) or "DBI"->croak->errstr 0 0 0
938 or croak DBI->errstr;
939 0 ▶ if ($self->{'dbi_backend'} eq 'CSV') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbi_backend"} eq "CSV") 0 0 0
940 $dbh->{'csv_tables'}->{$self->{'table_name'}} =
941 {'col_names' => $self->{'_db_fields'},
942 0 'file' => $self->{'table_name'}.".csv"};
943 }
944 0 $self->{'_db_connection'} = $dbh;
945 }
946 }
947  
948 0 ▶sub _get_line_numbers {
Subroutine _get_line_numbers: not called CC=3 SCAR=24.8
949 0 my $self = shift;
950  
951 0 my @numbers;
952  
953 0 ▶ if (defined $self->{'list'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"list"}) 0 0 0
954 0 my $list_ref = $self->{'list'};
955  
956 0 foreach my $line (@$list_ref) {
957 0 push @numbers, $line->number ();
958 }
959 }
960  
961 0 return @numbers;
962 }
963  
964 1 1;
965  
966 __END__
« Listman/dictionary.pm ↑ Summary Listman/dictionary/term.pm »