File Coverage

blib/lib/Dist/Inkt/Role/DetermineRightsFromRdf.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Dist::Inkt::Role::DetermineRightsFromRdf;
2              
3             our $AUTHORITY = 'cpan:TOBYINK';
4             our $VERSION = '0.021';
5              
6 1     1   1952 use Moose::Role;
  0            
  0            
7             use RDF::Trine qw( iri literal statement variable );
8             use List::MoreUtils qw( uniq );
9             use Path::Tiny qw( path );
10             use Path::Iterator::Rule;
11             use Software::License;
12             use Software::LicenseUtils;
13             use Types::Standard -types;
14             use namespace::autoclean;
15              
16             my %URIS = (
17             'http://www.gnu.org/licenses/agpl-3.0.txt' => 'AGPL_3',
18             'http://www.apache.org/licenses/LICENSE-1.1' => 'Apache_1_1',
19             'http://www.apache.org/licenses/LICENSE-2.0' => 'Apache_2_0',
20             'http://www.apache.org/licenses/LICENSE-2.0.txt' => 'Apache_2_0',
21             'http://www.perlfoundation.org/artistic_license_1_0' => 'Artistic_1_0',
22             'http://opensource.org/licenses/artistic-license.php' => 'Artistic_1_0',
23             'http://www.perlfoundation.org/artistic_license_2_0' => 'Artistic_2_0',
24             'http://opensource.org/licenses/artistic-license-2.0.php' => 'Artistic_2_0',
25             'http://www.opensource.org/licenses/bsd-license.php' => 'BSD',
26             'http://creativecommons.org/publicdomain/zero/1.0/' => 'CC0_1_0',
27             'http://www.freebsd.org/copyright/freebsd-license.html' => 'FreeBSD',
28             'http://www.gnu.org/copyleft/fdl.html' => 'GFDL_1_3',
29             'http://www.opensource.org/licenses/gpl-license.php' => 'GPL_1',
30             'http://www.gnu.org/licenses/old-licenses/gpl-1.0.txt' => 'GPL_1',
31             'http://www.opensource.org/licenses/gpl-2.0.php' => 'GPL_2',
32             'http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt' => 'GPL_2',
33             'http://www.opensource.org/licenses/gpl-3.0.html' => 'GPL_3',
34             'http://www.gnu.org/licenses/gpl-3.0.txt' => 'GPL_3',
35             'http://www.opensource.org/licenses/lgpl-2.1.php' => 'LGPL_2_1',
36             'http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt' => 'LGPL_2_1',
37             'http://www.opensource.org/licenses/lgpl-3.0.html' => 'LGPL_3_0',
38             'http://www.gnu.org/licenses/lgpl-3.0.txt' => 'LGPL_3_0',
39             'http://www.opensource.org/licenses/mit-license.php' => 'MIT',
40             'http://www.mozilla.org/MPL/MPL-1.0.txt' => 'Mozilla_1_0',
41             'http://www.mozilla.org/MPL/MPL-1.1.txt' => 'Mozilla_1_1',
42             'http://opensource.org/licenses/mozilla1.1.php' => 'Mozilla_1_1',
43             'http://www.openssl.org/source/license.html' => 'OpenSSL',
44             'http://dev.perl.org/licenses/' => 'Perl_5',
45             'http://www.opensource.org/licenses/postgresql' => 'PostgreSQL',
46             'http://trolltech.com/products/qt/licenses/licensing/qpl' => 'QPL_1_0',
47             'http://h71000.www7.hp.com/doc/83final/BA554_90007/apcs02.html' => 'SSLeay',
48             'http://www.openoffice.org/licenses/sissl_license.html' => 'Sun',
49             'http://www.zlib.net/zlib_license.html' => 'Zlib',
50             );
51             eval("require Software::License::$_") for uniq values %URIS;
52              
53             use RDF::Trine::Namespace qw[RDF RDFS OWL XSD];
54             my $CPAN = RDF::Trine::Namespace->new('http://purl.org/NET/cpan-uri/terms#');
55             my $DC = RDF::Trine::Namespace->new('http://purl.org/dc/terms/');
56             my $DOAP = RDF::Trine::Namespace->new('http://usefulinc.com/ns/doap#');
57             my $FOAF = RDF::Trine::Namespace->new('http://xmlns.com/foaf/0.1/');
58             my $NFO = RDF::Trine::Namespace->new('http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#');
59             my $SKOS = RDF::Trine::Namespace->new('http://www.w3.org/2004/02/skos/core#');
60              
61             sub _determine_rights_from_rdf
62             {
63             my ($self, $f) = @_;
64             unless ($self->{_rdf_copyright_data})
65             {
66             my $model = $self->model;
67             my $iter = $model->get_pattern(
68             RDF::Trine::Pattern->new(
69             statement(variable('subject'), $NFO->fileName, variable('filename')),
70             statement(variable('subject'), $DC->license, variable('license')),
71             statement(variable('subject'), $DC->rightsHolder, variable('rights_holder')),
72             statement(variable('rights_holder'), $FOAF->name, variable('name')),
73             ),
74             );
75             my %results;
76             while (my $row = $iter->next) {
77             my $l = $row->{license}->uri;
78             $row->{class} = literal("Software::License::$URIS{$l}")
79             if exists $URIS{$l};
80             $results{ $row->{filename}->literal_value } = $row;
81             }
82             $self->{_rdf_copyright_data} = \%results;
83             }
84            
85             if ( my $row = $self->{_rdf_copyright_data}{$f} ) {
86             return (
87             sprintf("Copyright %d %s.", 1900 + (localtime((stat $f)[9]))[5], $row->{name}->literal_value),
88             $row->{class}->literal_value->new({holder => "the copyright holder(s)"}),
89             ) if $row->{class};
90             }
91            
92             return;
93             }
94              
95             1;