File Coverage

blib/lib/BBDB/Export/LDIF.pm
Criterion Covered Total %
statement 68 80 85.0
branch 21 28 75.0
condition 4 12 33.3
subroutine 7 7 100.0
pod 3 3 100.0
total 103 130 79.2


line stmt bran cond sub pod time code
1             package BBDB::Export::LDIF;
2 1     1   844 use strict;
  1         3  
  1         40  
3 1     1   6 use warnings;
  1         2  
  1         57  
4              
5             our $VERSION = '0.015';
6              
7              
8             our @ISA = qw(BBDB::Export);
9              
10 1     1   5 use Data::Dumper;
  1         3  
  1         979  
11              
12             #
13             #_* get_record_hash
14             #
15             sub get_record_hash
16             {
17 7     7 1 13 my ( $self, $record ) = @_;
18              
19             # call get_record_hash from BBDB::Export
20 7         27 $record = $self->SUPER::get_record_hash( $record );
21              
22 7 50 33     26 unless ( $record->{'first'} || $record->{'last'} )
23             {
24 0         0 $self->error( "No first or last name for record" );
25 0         0 return;
26             }
27              
28             # add some custom fields to the hash
29 7         39 $record->{'dn'} = lc( "cn=$record->{'full'}, ou=addressbook, $self->{'data'}->{'dc'}" );
30              
31 7         22 my @objectClass = qw( top person organizationalPerson inetOrgPerson );
32 7         14 $record->{'objectClass'} = \@objectClass;
33              
34 7         14 $record->{'cn'} = $record->{'full'};
35 7         16 $record->{'givenName'} = $record->{'first'};
36 7   33     21 $record->{'sn'} = $record->{'last'} || $record->{'first'};
37 7         15 $record->{'ou'} = "addressbook";
38              
39 7 100       18 $record->{'mail'} = ( @{ $record->{'net'} } )[0] if $record->{'net'};
  2         6  
40 7         13 $record->{'street'} = $record->{'street'};
41 7         16 $record->{'l'} = $record->{'city'};
42 7         15 $record->{'st'} = $record->{'state'};
43 7         14 $record->{'postalCode'} = $record->{'zip'};
44              
45             # Phone
46 7 100       17 if ( $record->{'phone'} )
47             {
48 2         4 $record->{'telephoneNumber'} = $record->{'phone'}->{'work'};
49 2         5 $record->{'homePhone'} = $record->{'phone'}->{'home'};
50 2         4 $record->{'mobile'} = $record->{'phone'}->{'mobile'};
51 2         6 $record->{'pager'} = $record->{'phone'}->{'pager'};
52 2         4 $record->{'facsimileTelephoneNumber'} = $record->{'phone'}->{'fax'};
53             }
54              
55              
56             # title
57 7         7 my @title;
58 7 100       17 if ( $record->{'title'} )
59             {
60 1         2 push @title, $record->{'title'};
61             }
62 7 100       15 if ( $record->{'group'} )
63             {
64 1         2 push @title, $record->{'group'};
65             }
66 7 100       17 if ( $record->{'company'} )
67             {
68 3         6 push @title, $record->{'company'};
69             }
70 7 100       16 if ( $title[0] )
71             {
72 3         7 $record->{'title'} = join ( " - ", @title );
73             }
74              
75             # description
76 7         17 $record->{'description'} = $record->{'notes'};
77              
78 7         20 return $record;
79              
80             }
81              
82             #
83             #_* process_record
84             #
85              
86             sub process_record
87             {
88 7     7 1 11 my ( $self, $record ) = @_;
89 7         9 my ( $data, $return );
90              
91             # start of record
92 7         18 for my $field ( qw(
93             dn objectClass cn givenName sn ou mail street l st postalCode
94             telephoneNumber homePhone mobile pager facsimileTelephoneNumber
95             title description
96             ) )
97             {
98 126 100       249 next unless $record->{ $field };
99 54 100       95 if ( ref $record->{ $field } eq "ARRAY" )
100             {
101 7         9 for my $index ( 0 .. $#{ $record->{ $field } } )
  7         20  
102             {
103 28         61 $return .= $self->_format_field( $field, $record->{ $field }->[$index] );
104             }
105             }
106             else
107             {
108 47         93 $return .= $self->_format_field( $field, $record->{ $field } );
109             }
110             }
111              
112             # jpegPhoto
113 7 50       21 if ( $record->{'face'} )
114             {
115 0         0 $return .= "jpegPhoto:: ";
116 0         0 $return .= $record->{'face'};
117 0         0 $return .= "\n";
118             }
119              
120 7         8 $return .= "\n\n";
121              
122 7         22 return ( $return, $data );
123              
124             }
125              
126              
127             #
128             #_* _format_field
129             #
130             sub _format_field
131             {
132 75     75   96 my ( $self, $name, $value ) = @_;
133              
134 75 50 33     280 return unless ( $name && $value );
135              
136 75         113 $name =~ s|^\s+||g;
137 75         86 $name =~ s|\s+$||g;
138 75         86 $value =~ s|^\s+||g;
139 75         135 $value =~ s|[\s\,]+$||g;
140              
141 75         229 return "$name: $value\n";
142              
143             }
144              
145             #
146             #_* post_processing
147             #
148             sub post_processing
149             {
150 7     7 1 10 my ( $self, $output ) = @_;
151              
152 7         25 $self->info("Exporting to LDIF" );
153              
154 7 50       17 unless ( $output )
155             {
156 0         0 $self->error( "No text to export to LDIF" );
157 0         0 return "";
158             }
159              
160 7         14 my $outfile = $self->{'data'}->{'output_file'};
161 7 50 33     28 unless ( $outfile && ! $self->{'data'}->{'quiet'} )
162             {
163 7         26 $self->error( "No output_file defined" );
164 7         16 return "";
165             }
166              
167 0 0         open ( my $out_fh, ">", $outfile ) or die "Unable to create $outfile";
168 0           print $out_fh $output;
169 0           close $out_fh;
170              
171 0           $self->info( "Exported LDIF data to $outfile" );
172              
173 0           return $output;
174             }
175              
176             1;
177              
178