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