| 1 | package XML::FOAFKnows::FromvCard; | ||
| 2 | |||
| 3 | 3 | ▶ | use 5.007; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 3 | ▶ | use Text::vCard::Addressbook; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | ▶ | use Text::vCard; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 3 | ▶ | use Digest::SHA1 qw(sha1_hex); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use IDNA::Punycode; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | use base qw( Text::vCard ); | ||
| 14 | |||
| 15 | our $VERSION = '0.6'; | ||
| 16 | |||
| 17 | sub format { | ||
| 18 | my $that = shift; | ||
| 19 | my $class = ref($that) || $that; | ||
| 20 | my ($text,%config) = @_; | ||
| 21 | my $address_book = Text::vCard::Addressbook->new({ | ||
| 22 | 'source_text' => $text, | ||
| 23 | }); | ||
| 24 | |||
| 25 | my $privstatattrib = $config{attribute} || 'CLASS'; | ||
| 26 | # Parse and build the fragment | ||
| 27 | my $records = ''; | ||
| 28 | my @urls = (); | ||
| 29 | my $counts = 0; | ||
| 30 | foreach my $vcard ($address_book->vcards()) { | ||
| 31 | my $privacystat = 'PRIVATE'; # The default privacy option if nothing else is set | ||
| 32 | if (defined($config{privacy})) { | ||
| 33 | $privacystat = uc($config{privacy}); | ||
| 34 | } elsif (($vcard->get($privstatattrib))[0]) { | ||
| 35 | $privacystat = ($vcard->get($privstatattrib))[0]->value; # Check the status and generate full records only for public records | ||
| 36 | } | ||
| 37 | next if ($privacystat eq 'CONFIDENTIAL'); | ||
| 38 | my @email = ($vcard->get('EMAIL')); | ||
| 39 | my $url = ($vcard->get('URL'))[0]; | ||
| 40 | next unless ($url || $email[0]); # We need at least an URL or an email to continue | ||
| 41 | $counts++; | ||
| 42 | $records .= "<foaf:knows>\n\t<foaf:Person"; | ||
| 43 | # TODO: nodeIDs, but how to generate...? | ||
| 44 | if (($vcard->get('NICKNAME'))[0]) { # a nodeID on the Person record can be useful | ||
| 45 | my $punynick = encode_punycode(($vcard->get('NICKNAME'))[0]->value); # puny-encode any nicks, | ||
| 46 | $punynick =~ s/\s/_/gs; # and replace any whitespace with underscores | ||
| 47 | $records .= ' rdf:nodeID="' . $punynick . '">'. | ||
| 48 | "\n\t\t<foaf:nick>" . ($vcard->get('NICKNAME'))[0]->value . '</foaf:nick>'; | ||
| 49 | } else { | ||
| 50 | $records .= ' rdf:nodeID="person'.$counts.'">'; | ||
| 51 | } | ||
| 52 | |||
| 53 | foreach (@email) { | ||
| 54 | next unless (defined($_)); | ||
| 55 | $records .= "\n\t\t<foaf:mbox_sha1sum>" . sha1_hex('mailto:' . $_->value) . '</foaf:mbox_sha1sum>'; | ||
| 56 | } | ||
| 57 | if ($url) { | ||
| 58 | my $tmp = $url->value; | ||
| 59 | $tmp =~ s/\\:/:/g; # Some files seem to have colons in URLs escaped | ||
| 60 | $records .= "\n\t\t".'<foaf:homepage rdf:resource="'.$tmp.'"/>'; | ||
| 61 | } | ||
| 62 | my $fullname = ''; | ||
| 63 | if ($privacystat eq 'PUBLIC') { | ||
| 64 | my $name = ($vcard->get('N'))[0]; | ||
| 65 | if ($name) { | ||
| 66 | $records .= "\n\t\t<foaf:family_name>".$name->family.'</foaf:family_name>'; | ||
| 67 | $fullname = $name->family; | ||
| 68 | if ($name->given()) { | ||
| 69 | $records .= "\n\t\t<foaf:givenname>".$name->given.'</foaf:givenname>'. | ||
| 70 | "\n\t\t<foaf:name>".$name->given.' '.$name->family.'</foaf:name>'; | ||
| 71 | $fullname = $name->given.' '.$name->family; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | elsif (($vcard->get('FN'))[0]->fullname()) { | ||
| 75 | $records .= "\n\t\t<foaf:name>".($vcard->get('FN'))[0]->fullname.'</foaf:name>'; | ||
| 76 | $fullname = ($vcard->get('FN'))[0]->fullname; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | # Now we build the URL to be returned by the links method | ||
| 80 | if ($vcard->get('URL')) { | ||
| 81 | foreach my $url2 ($vcard->get('URL')) { | ||
| 82 | my $tmp = $url2->value; | ||
| 83 | $tmp =~ s/\\:/:/g; | ||
| 84 | push(@urls, {url => $tmp, title => $fullname}); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | $records .= "\n\t</foaf:Person>\n</foaf:knows>\n"; | ||
| 88 | } | ||
| 89 | |||
| 90 | my $self = { | ||
| 91 | _out => $records, | ||
| 92 | _urls => \@urls, | ||
| 93 | _config => \%config, | ||
| 94 | }; | ||
| 95 | bless($self, $class); | ||
| 96 | return $self; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub title { return undef } | ||
| 101 | |||
| 102 | sub links { return shift->{_urls}; } | ||
| 103 | |||
| 104 | sub fragment { return shift->{_out}; } | ||
| 105 | |||
| 106 | sub document { | ||
| 107 | my ($self,$encoding) = @_; | ||
| 108 | |||
| 109 | my $out = '<?xml version="1.0"'; | ||
| 110 | if ($encoding) { | ||
| 111 | $out .= ' encoding="'.$encoding.'"'; | ||
| 112 | } | ||
| 113 | $out .= '?>'; | ||
| 114 | $out .= "\n<rdf:RDF\n". | ||
| 115 | 'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" '. | ||
| 116 | 'xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" '. | ||
| 117 | 'xmlns:foaf="http://xmlns.com/foaf/0.1/">'. | ||
| 118 | "\n<foaf:Person"; | ||
| 119 | |||
| 120 | if ($self->{_config}->{uri}) { | ||
| 121 | $out .= ' rdf:about="'.$self->{_config}->{uri}.'">'; | ||
| 122 | } else { | ||
| 123 | $out .= ">\n\n"; | ||
| 124 | } | ||
| 125 | if ($self->{_config}->{email}) { | ||
| 126 | $out .= "\n\t<foaf:mbox_sha1sum>" . sha1_hex('mailto:' . $self->{_config}->{email}) . '</foaf:mbox_sha1sum>'; | ||
| 127 | } | ||
| 128 | if ($self->{_config}->{seeAlso}) { | ||
| 129 | $out .= "\n\t".'<rdfs:seeAlso rdf:resource="'.$self->{_config}->{seeAlso}.'"/>'."\n\n"; | ||
| 130 | } | ||
| 131 | return $out . $self->{_out} . "\n</foaf:Person>\n</rdf:RDF>\n"; | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | 1; | ||
| 138 | __END__ | ||