| 1 | package RDF::TrineX::Parser::Pretdsl; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | no warnings qw( void once uninitialized numeric ); | ||
| 6 | |||
| 7 | our $AUTHORITY = 'cpan:TOBYINK'; | ||
| 8 | our $VERSION = '0.206'; | ||
| 9 | |||
| 10 | our $PROFILE = <<'PRETDSL_PROFILE'; | ||
| 11 | |||
| 12 | # RDFa 1.1 prefixes | ||
| 13 | @prefix grddl: <http://www.w3.org/2003/g/data-view#> . | ||
| 14 | @prefix ma: <http://www.w3.org/ns/ma-ont#> . | ||
| 15 | @prefix owl: <http://www.w3.org/2002/07/owl#> . | ||
| 16 | @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . | ||
| 17 | @prefix rdfa: <http://www.w3.org/ns/rdfa#> . | ||
| 18 | @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | ||
| 19 | @prefix rif: <http://www.w3.org/2007/rif#> . | ||
| 20 | @prefix skos: <http://www.w3.org/2004/02/skos/core#> . | ||
| 21 | @prefix skosxl: <http://www.w3.org/2008/05/skos-xl#> . | ||
| 22 | @prefix wdr: <http://www.w3.org/2007/05/powder#> . | ||
| 23 | @prefix void: <http://rdfs.org/ns/void#> . | ||
| 24 | @prefix wdrs: <http://www.w3.org/2007/05/powder-s#> . | ||
| 25 | @prefix xhv: <http://www.w3.org/1999/xhtml/vocab#> . | ||
| 26 | @prefix xml: <http://www.w3.org/XML/1998/namespace> . | ||
| 27 | @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | ||
| 28 | @prefix cc: <http://creativecommons.org/ns#> . | ||
| 29 | @prefix ctag: <http://commontag.org/ns#> . | ||
| 30 | @prefix dc: <http://purl.org/dc/terms/> . | ||
| 31 | @prefix dcterms: <http://purl.org/dc/terms/> . | ||
| 32 | @prefix foaf: <http://xmlns.com/foaf/0.1/> . | ||
| 33 | @prefix gr: <http://purl.org/goodrelations/v1#> . | ||
| 34 | @prefix ical: <http://www.w3.org/2002/12/cal/icaltzd#> . | ||
| 35 | @prefix og: <http://ogp.me/ns#> . | ||
| 36 | @prefix rev: <http://purl.org/stuff/rev#> . | ||
| 37 | @prefix sioc: <http://rdfs.org/sioc/ns#> . | ||
| 38 | @prefix v: <http://rdf.data-vocabulary.org/#> . | ||
| 39 | @prefix vcard: <http://www.w3.org/2006/vcard/ns#> . | ||
| 40 | @prefix schema: <http://schema.org/> . | ||
| 41 | |||
| 42 | # Additional useful vocabularies | ||
| 43 | @prefix cpant: <http://purl.org/NET/cpan-uri/terms#>. | ||
| 44 | @prefix dbug: <http://ontologi.es/doap-bugs#> . | ||
| 45 | @prefix dcs: <http://ontologi.es/doap-changeset#> . | ||
| 46 | @prefix deps: <http://ontologi.es/doap-deps#> . | ||
| 47 | @prefix doap: <http://usefulinc.com/ns/doap#> . | ||
| 48 | @prefix earl: <http://www.w3.org/ns/earl#> . | ||
| 49 | @prefix nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#> . | ||
| 50 | @prefix pretdsl: <http://ontologi.es/pretdsl#> . | ||
| 51 | @prefix pretdsl-dt: <http://ontologi.es/pretdsl#dt/> . | ||
| 52 | |||
| 53 | # Useful XSD datatypes | ||
| 54 | @dtpattern | ||
| 55 | "[0-9]{4}-[0-9]{2}-[0-9]{2}" | ||
| 56 | <http://www.w3.org/2001/XMLSchema#date> . | ||
| 57 | @dtpattern | ||
| 58 | "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{4}:[0-9]{2}:[0-9]{2}(\\.[0-9]+)?(Z|[+-][0-9]{2}:[0-9]{2})?" | ||
| 59 | <http://www.w3.org/2001/XMLSchema#dateTime> . | ||
| 60 | |||
| 61 | # Other datatype shorthands | ||
| 62 | @pattern | ||
| 63 | "`(?<x>.+?)`" | ||
| 64 | "$x"^^pretdsl-dt:PerlResourceIdentifier . | ||
| 65 | @pattern | ||
| 66 | "d`(?<x>.+?)`" | ||
| 67 | "$x"^^pretdsl-dt:Distribution . | ||
| 68 | @pattern | ||
| 69 | "r`(?<x>.+?)`" | ||
| 70 | "$x"^^pretdsl-dt:Release . | ||
| 71 | @pattern | ||
| 72 | "p`(?<x>.+?)`" | ||
| 73 | "$x"^^pretdsl-dt:Package . | ||
| 74 | @pattern | ||
| 75 | "m`(?<x>.+?)`" | ||
| 76 | "$x"^^pretdsl-dt:Module . | ||
| 77 | @pattern | ||
| 78 | "f`(?<x>.+?)`" | ||
| 79 | "$x"^^pretdsl-dt:File . | ||
| 80 | @pattern | ||
| 81 | "cpan:(?<x>\\w+)" | ||
| 82 | "$x"^^pretdsl-dt:CpanId . | ||
| 83 | @pattern | ||
| 84 | "RT#(?<x>\\d+)" | ||
| 85 | "$x"^^pretdsl-dt:RtBug . | ||
| 86 | |||
| 87 | # Generally useful predicates | ||
| 88 | @term label rdfs:label . | ||
| 89 | @term comment rdfs:comment . | ||
| 90 | @term seealso rdfs:seeAlso . | ||
| 91 | |||
| 92 | # Makefile predicates | ||
| 93 | @term abstract_from cpant:abstract_from . | ||
| 94 | @term author_from cpant:author_from . | ||
| 95 | @term license_from cpant:license_from . | ||
| 96 | @term requires_from cpant:requires_from . | ||
| 97 | @term perl_version_from cpant:perl_version_from . | ||
| 98 | @term version_from cpant:version_from . | ||
| 99 | @term readme_from cpant:readme_from . | ||
| 100 | @term no_index cpant:no_index . | ||
| 101 | @term install_script cpant:install_script . | ||
| 102 | @term requires cpant:requires . | ||
| 103 | @term requires_external_bin cpant:requires_external_bin . | ||
| 104 | @term recommends cpant:recommends . | ||
| 105 | @term test_requires cpant:test_requires . | ||
| 106 | @term configure_requires cpant:configure_requires . | ||
| 107 | @term build_requires cpant:build_requires . | ||
| 108 | @term provides cpant:provides . | ||
| 109 | |||
| 110 | # Changelog predicates | ||
| 111 | @term issued dc:issued . | ||
| 112 | @term changeset dcs:changeset . | ||
| 113 | @term item dcs:item . | ||
| 114 | @term versus dcs:versus . | ||
| 115 | |||
| 116 | # Changelog datatypes | ||
| 117 | @term Addition pretdsl-dt:Addition . | ||
| 118 | @term BackCompat pretdsl-dt:BackCompat . | ||
| 119 | @term Bugfix pretdsl-dt:Bugfix . | ||
| 120 | @term Change pretdsl-dt:Change . | ||
| 121 | @term Documentation pretdsl-dt:Documentation . | ||
| 122 | @term Packaging pretdsl-dt:Packaging . | ||
| 123 | @term Regression pretdsl-dt:Regression . | ||
| 124 | @term Removal pretdsl-dt:Removal . | ||
| 125 | @term SecurityFix pretdsl-dt:SecurityFix . | ||
| 126 | @term SecurityRegression pretdsl-dt:SecurityRegression . | ||
| 127 | @term Tests pretdsl-dt:Tests . | ||
| 128 | @term Update pretdsl-dt:Update . | ||
| 129 | |||
| 130 | PRETDSL_PROFILE | ||
| 131 | |||
| 132 | our $CALLBACKS = {}; | ||
| 133 | |||
| 134 | use Module::Runtime qw< module_notional_filename > ; | ||
| 135 | use RDF::Trine qw< statement iri blank literal >; | ||
| 136 | use RDF::NS::Trine; | ||
| 137 | |||
| 138 | my $curie = RDF::NS::Trine->new('20120521'); | ||
| 139 | |||
| 140 | sub _CB_ (&$) | ||
| 141 | { | ||
| 142 | my ($coderef, $uri) = @_; | ||
| 143 | $uri = "http://ontologi.es/pretdsl#dt/$uri" unless $uri =~ /\W/; | ||
| 144 | $CALLBACKS->{$uri} = $coderef; | ||
| 145 | } | ||
| 146 | |||
| 147 | _CB_ | ||
| 148 | { | ||
| 149 | my ($lit, $cb) = @_; | ||
| 150 | my ($dist, $version, $author) = split /\s+/, $lit->literal_value; | ||
| 151 | if ($dist =~ m{::}) { | ||
| 152 | goto $CALLBACKS->{'http://ontologi.es/pretdsl#dt/Module'} | ||
| 153 | } | ||
| 154 | if ($dist =~ m{/}) { | ||
| 155 | goto $CALLBACKS->{'http://ontologi.es/pretdsl#dt/File'} | ||
| 156 | } | ||
| 157 | if (length $version) { | ||
| 158 | goto $CALLBACKS->{'http://ontologi.es/pretdsl#dt/Release'} | ||
| 159 | } | ||
| 160 | goto $CALLBACKS->{'http://ontologi.es/pretdsl#dt/Distribution'}; | ||
| 161 | } 'PerlResourceIdentifier'; | ||
| 162 | |||
| 163 | _CB_ | ||
| 164 | { | ||
| 165 | my ($lit, $cb) = @_; | ||
| 166 | my $dist = $lit->literal_value; | ||
| 167 | |||
| 168 | my $node = iri(sprintf( | ||
| 169 | 'http://purl.org/NET/cpan-uri/dist/%s/project', | ||
| 170 | $dist, | ||
| 171 | )); | ||
| 172 | |||
| 173 | my $metacpan = iri(sprintf( | ||
| 174 | 'https://metacpan.org/release/%s', | ||
| 175 | $dist, | ||
| 176 | )); | ||
| 177 | |||
| 178 | $cb->(statement($node, $curie->rdf_type, $curie->doap_Project)); | ||
| 179 | $cb->(statement($node, $curie->doap_name, literal($dist))); | ||
| 180 | $cb->(statement($node, $curie->URI('doap:programming-language'), literal('Perl'))); | ||
| 181 | $cb->(statement($node, $curie->doap_homepage, $metacpan)); | ||
| 182 | $cb->(statement($node, $curie->URI('doap:download-page'), $metacpan)); | ||
| 183 | |||
| 184 | return $node; | ||
| 185 | } 'Distribution'; | ||
| 186 | |||
| 187 | _CB_ | ||
| 188 | { | ||
| 189 | my ($lit, $cb) = @_; | ||
| 190 | my ($dist, $version, $author) = split /\s+/, $lit->literal_value; | ||
| 191 | (my $version_token = $version) =~ s/\./-/g; | ||
| 192 | |||
| 193 | my $dist_node = iri(sprintf( | ||
| 194 | 'http://purl.org/NET/cpan-uri/dist/%s/project', | ||
| 195 | $dist, | ||
| 196 | )); | ||
| 197 | |||
| 198 | my $node = iri(sprintf( | ||
| 199 | 'http://purl.org/NET/cpan-uri/dist/%s/v_%s', | ||
| 200 | $dist, | ||
| 201 | $version_token, | ||
| 202 | )); | ||
| 203 | |||
| 204 | $cb->(statement($dist_node, $curie->doap_release, $node)); | ||
| 205 | $cb->(statement($node, $curie->rdf_type, $curie->doap_Version)); | ||
| 206 | $cb->(statement($node, $curie->doap_revision, literal($version, undef, $curie->xsd_string->uri))); | ||
| 207 | $cb->(statement($node, $curie->dcterms_identifier, literal("$dist-$version", undef, $curie->xsd_string->uri))); | ||
| 208 | |||
| 209 | $cb->(statement($node, $curie->rdf_type, iri('http://purl.org/NET/cpan-uri/terms#DeveloperRelease'))) | ||
| 210 | if $version =~ m{dev|_}i; | ||
| 211 | |||
| 212 | if ($author =~ /^cpan:(\w+)$/) | ||
| 213 | { | ||
| 214 | $author = $1; | ||
| 215 | my $author_node = iri(sprintf( | ||
| 216 | 'http://purl.org/NET/cpan-uri/person/%s', | ||
| 217 | lc $author, | ||
| 218 | )); | ||
| 219 | $cb->(statement($node, iri('http://ontologi.es/doap-changeset#released-by'), $author_node)); | ||
| 220 | $cb->(statement($dist_node, $curie->dcterms_contributor, $author_node)); | ||
| 221 | my $download = iri(sprintf( | ||
| 222 | 'http://backpan.cpan.org/authors/id/%s/%s/%s/%s-%s.tar.gz', | ||
| 223 | substr(uc $author, 0, 1), | ||
| 224 | substr(uc $author, 0, 2), | ||
| 225 | uc($author), | ||
| 226 | $dist, | ||
| 227 | $version, | ||
| 228 | )); | ||
| 229 | $cb->(statement($node, $curie->URI('doap:file-release'), $download)); | ||
| 230 | } | ||
| 231 | |||
| 232 | return $node; | ||
| 233 | } 'Release'; | ||
| 234 | |||
| 235 | _CB_ | ||
| 236 | { | ||
| 237 | my ($lit, $cb) = @_; | ||
| 238 | my ($filename, $dist, $ver, $author) = split /\s+/, $lit->literal_value; | ||
| 239 | $filename =~ s{^[.]/}{}; | ||
| 240 | |||
| 241 | my ($author_cpan) = ($author =~ m{^cpan:(\w+)$}i); | ||
| 242 | |||
| 243 | my $node; | ||
| 244 | if ($filename and $dist and $ver and $author_cpan) | ||
| 245 | { | ||
| 246 | $node = iri(sprintf 'http://api.metacpan.org/source/%s/%s-%s/%s', $author_cpan, $dist, $ver, $filename); | ||
| 247 | |||
| 248 | my $release_download = iri(sprintf( | ||
| 249 | 'http://backpan.cpan.org/authors/id/%s/%s/%s/%s-%s.tar.gz', | ||
| 250 | substr(uc $author_cpan, 0, 1), | ||
| 251 | substr(uc $author_cpan, 0, 2), | ||
| 252 | uc($author_cpan), | ||
| 253 | $dist, | ||
| 254 | $ver, | ||
| 255 | )); | ||
| 256 | |||
| 257 | $cb->(statement($node, $curie->nfo_belongsToContainer, $release_download)); | ||
| 258 | } | ||
| 259 | else | ||
| 260 | { | ||
| 261 | $node = blank(); | ||
| 262 | } | ||
| 263 | |||
| 264 | $cb->(statement($node, $curie->rdf_type, $curie->nfo_FileDataObject)); | ||
| 265 | $cb->(statement($node, $curie->nfo_fileName, literal($filename))); | ||
| 266 | |||
| 267 | if ($filename =~ /\.(PL|pl|pm|t|xs|c)$/) | ||
| 268 | { $cb->(statement($node, $curie->rdf_type, $curie->nfo_SourceCode)) } | ||
| 269 | |||
| 270 | if ($filename =~ /\.(PL|pl|pm)$/) | ||
| 271 | { $cb->(statement($node, $curie->nfo_programmingLanguage, literal('Perl'))) } | ||
| 272 | |||
| 273 | if ($filename =~ /\.(html)$/) | ||
| 274 | { $cb->(statement($node, $curie->rdf_type, $curie->nfo_HtmlDocument)) } | ||
| 275 | |||
| 276 | if ($filename =~ /\.(pod)$/) | ||
| 277 | { $cb->(statement($node, $curie->rdf_type, $curie->nfo_Document)) } | ||
| 278 | |||
| 279 | if ($filename =~ /^(Changes|README|TODO|LICENSE|INSTALL|NEWS|FAQ|.*\.txt)$/) | ||
| 280 | { $cb->(statement($node, $curie->rdf_type, $curie->nfo_TextDocument)) } | ||
| 281 | |||
| 282 | return $node; | ||
| 283 | } 'File'; | ||
| 284 | |||
| 285 | _CB_ | ||
| 286 | { | ||
| 287 | my ($lit, $cb) = @_; | ||
| 288 | my ($filename, $dist, $ver, $author) = split /\s+/, $lit->literal_value; | ||
| 289 | $filename =~ s{::$}{}; | ||
| 290 | my $joined = join q( ), grep defined, | ||
| 291 | sprintf('lib/%s', module_notional_filename($filename)), | ||
| 292 | $dist, | ||
| 293 | $ver, | ||
| 294 | $author, | ||
| 295 | ; | ||
| 296 | my $r = $CALLBACKS->{'http://ontologi.es/pretdsl#dt/File'}->(literal($joined), $cb); | ||
| 297 | $cb->(statement($r, $curie->rdfs_label, literal($filename))); | ||
| 298 | return $r; | ||
| 299 | } 'Module'; | ||
| 300 | |||
| 301 | _CB_ | ||
| 302 | { | ||
| 303 | my ($lit, $cb) = @_; | ||
| 304 | my ($mod, $ver) = split /\s+/, $lit->literal_value; | ||
| 305 | $mod =~ s{::$}{}; | ||
| 306 | |||
| 307 | if (length $ver) | ||
| 308 | { | ||
| 309 | return literal("$mod $ver", undef, "http://purl.org/NET/cpan-uri/terms#dsWithVersion"); | ||
| 310 | } | ||
| 311 | |||
| 312 | return literal("$mod", undef, "http://purl.org/NET/cpan-uri/terms#dsWithoutVersion"); | ||
| 313 | } 'Package'; | ||
| 314 | |||
| 315 | _CB_ | ||
| 316 | { | ||
| 317 | my ($lit, $cb) = @_; | ||
| 318 | my $node = iri(sprintf('http://purl.org/NET/cpan-uri/person/%s', lc $lit->literal_value)); | ||
| 319 | $cb->(statement($node, $curie->rdf_type, $curie->foaf_Person)); | ||
| 320 | $cb->(statement($node, $curie->foaf_nick, literal($lit->literal_value))); | ||
| 321 | $cb->(statement($node, $curie->foaf_page, iri(sprintf 'https://metacpan.org/author/%s', uc $lit->literal_value))); | ||
| 322 | return $node; | ||
| 323 | } 'CpanId'; | ||
| 324 | |||
| 325 | _CB_ | ||
| 326 | { | ||
| 327 | my ($lit, $cb) = @_; | ||
| 328 | my $node = iri(sprintf('http://purl.org/NET/cpan-uri/rt/ticket/%d', $lit->literal_value)); | ||
| 329 | $cb->(statement($node, $curie->rdf_type, iri('http://ontologi.es/doap-bugs#Issue'))); | ||
| 330 | $cb->(statement($node, iri('http://ontologi.es/doap-bugs#page'), iri(sprintf 'https://rt.cpan.org/Ticket/Display.html?id=%d', $lit->literal_value))); | ||
| 331 | $cb->(statement($node, iri('http://ontologi.es/doap-bugs#id'), literal($lit->literal_value, undef, $curie->xsd_string->uri))); | ||
| 332 | return $node; | ||
| 333 | } 'RtBug'; | ||
| 334 | |||
| 335 | foreach my $change_type (qw( | ||
| 336 | Addition BackCompat Bugfix Change Documentation Packaging Regression | ||
| 337 | Removal SecurityFix SecurityRegression Tests Update | ||
| 338 | )) | ||
| 339 | { | ||
| 340 | _CB_ | ||
| 341 | { | ||
| 342 | my ($lit, $cb) = @_; | ||
| 343 | my $node = blank(); | ||
| 344 | $cb->(statement($node, $curie->rdf_type, iri("http://ontologi.es/doap-changeset#$change_type"))); | ||
| 345 | $cb->(statement($node, $curie->rdfs_label, literal($lit->literal_value))); | ||
| 346 | return $node; | ||
| 347 | } $change_type; | ||
| 348 | } | ||
| 349 | |||
| 350 | use namespace::clean; | ||
| 351 | use base 'RDF::Trine::Parser::ShorthandRDF'; | ||
| 352 | |||
| 353 | sub new | ||
| 354 | { | ||
| 355 | my ($class, %args) = @_; | ||
| 356 | $class->SUPER::new( | ||
| 357 | datatype_callback => $CALLBACKS, | ||
| 358 | profile => $PROFILE, | ||
| 359 | %args, | ||
| 360 | ); | ||
| 361 | } | ||
| 362 | |||
| 363 | __PACKAGE__ | ||
| 364 | __END__ |