/usr/share/perl5/RDF/Helper/Properties.pm is in librdf-helper-properties-perl 0.22-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | package RDF::Helper::Properties;
# Uhm, well, this should probably also be a role at some point...
use 5.008;
use Any::Moose;
use RDF::Trine qw(iri variable statement);
use namespace::autoclean -also => [qw/cached/];
our $VERSION = '0.22';
has model => (
is => 'ro',
isa => 'RDF::Trine::Model',
required => 1,
);
has [qw/ page_properties title_properties /] => (
is => 'ro',
isa => 'ArrayRef[RDF::Trine::Node::Resource]',
lazy_build => 1,
);
use constant {
_build_page_properties => [
iri( 'http://xmlns.com/foaf/0.1/homepage' ),
iri( 'http://xmlns.com/foaf/0.1/page' ),
],
_build_title_properties => [
iri( 'http://xmlns.com/foaf/0.1/name' ),
iri( 'http://purl.org/dc/terms/title' ),
iri( 'http://purl.org/dc/elements/1.1/title' ),
iri( 'http://www.w3.org/2004/02/skos/core#prefLabel' ),
iri( 'http://www.geonames.org/ontology#officialName' ),
iri( 'http://www.geonames.org/ontology#name' ),
iri( 'http://purl.org/vocabularies/getty/vp/labelPreferred' ),
iri( 'http://opengraphprotocol.org/schema/title' ),
iri( 'http://www.w3.org/2000/01/rdf-schema#label' ),
# doap ?
],
};
has cache => (
is => 'rw',
isa => 'HashRef | Object',
lazy_build => 1,
);
sub _build_cache
{
+{
title => {
'<http://www.w3.org/2000/01/rdf-schema#label>' => 'label',
'<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>' => 'type',
},
pred => {
'<http://www.w3.org/2000/01/rdf-schema#label>' => 'label',
'<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>' => 'type',
'<http://purl.org/dc/elements/1.1/type>' => 'Type',
},
};
}
sub _cache_set
{
my ($self, $type, $uri, $value) = @_;
my $cache = $self->cache;
if (blessed $cache)
{ $cache->set("$type$uri" => $value) }
else
{ $cache->{$type}{$uri} = $value }
return $value;
}
sub _cache_get
{
my ($self, $type, $uri) = @_;
my $cache = $self->cache;
if (blessed $cache)
{ return $cache->get("$type$uri") }
else
{ return $cache->{$type}{$uri} }
}
# Helper function, cleaned away by namespace::autoclean
sub cached
{
my ($method_name, $coderef) = @_;
no strict 'refs';
*{$method_name} = sub
{
my ($self, $node) = @_;
my $return = $self->_cache_get($method_name, $node)
|| $self->_cache_set($method_name, $node, $self->$coderef($node));
if (blessed $return and $return->isa('RDF::Trine::Node'))
{
if ($return->is_literal)
{
return wantarray
? ($return->literal_value, $return->literal_value_language, $return->literal_datatype)
: $return->literal_value;
}
elsif ($return->is_resource)
{
return $return->uri_value;
}
else
{
return $return->as_string;
}
}
return $return;
}
}
cached page => sub
{
my ($self, $node) = @_;
confess "Node argument needs to be a RDF::Trine::Node::Resource."
unless $node && $node->isa('RDF::Trine::Node::Resource');
my @props = @{ $self->page_properties };
my ($object) =
grep { blessed $_ and $_->is_resource }
scalar $self->model->objects_for_predicate_list($node, @props);
($object) =
grep { blessed $_ and $_->is_resource }
$self->model->objects_for_predicate_list($node, @props)
unless $object;
return $object if $object;
# Return the common link to ourselves
return iri($node->uri_value . '/page');
};
cached title => sub
{
my ($self, $node) = @_;
my @props = @{ $self->title_properties };
my ($object) =
grep { blessed $_ and $_->is_literal }
scalar $self->model->objects_for_predicate_list($node, @props);
($object) =
grep { blessed $_ and $_->is_literal }
$self->model->objects_for_predicate_list($node, @props)
unless $object;
return $object if $object;
# and finally fall back on just returning the node
return $node;
};
cached description => sub
{
my ($self, $node) = @_;
my $model = $self->model;
my $iter = $model->get_statements( $node );
my @label = @{ $self->title_properties };
my @desc;
while (my $st = $iter->next)
{
my $p = $st->predicate;
my $ps;
if ($ps = $self->_cache_get(pred => $p))
{ 1 }
elsif (my $pname = $model->objects_for_predicate_list($p, @label))
{ $ps = $self->html_node_value( $pname ) }
elsif ($p->is_resource and $p->uri_value =~ m<^http://www.w3.org/1999/02/22-rdf-syntax-ns#_(\d+)$>)
{ $ps = '#' . $1 }
else
{
# try to turn the predicate into a qname and use the local part as the printable name
my $name;
eval {
(undef, $name) = $p->qname;
};
$ps = _escape( $name || $p->uri_value );
}
$self->_cache_set(pred => $p, $ps);
my $obj = $st->object;
my $os = $self->html_node_value( $obj, $p );
push(@desc, [$ps, $os]);
}
return \@desc;
};
sub html_node_value
{
my $self = shift;
my $n = shift;
my $rdfapred = shift;
my $qname = '';
my $xmlns = '';
if ($rdfapred)
{
eval {
my ($ns, $ln) = $rdfapred->qname;
$xmlns = qq[xmlns:ns="${ns}"];
$qname = qq[ns:$ln];
};
}
return '' unless blessed $n;
if ($n->is_literal)
{
my $l = _escape( $n->literal_value );
return $qname
? qq[<span $xmlns property="${qname}">$l</span>]
: $l;
}
elsif ($n->is_resource)
{
my $uri = _escape( $n->uri_value );
my $title = _escape( $self->title($n) );
return $qname
? qq[<a $xmlns rel="${qname}" href="${uri}">$title</a>]
: qq[<a href="${uri}">$title</a>];
}
else
{
return $n->as_string;
}
}
sub _escape
{
my $l = shift;
for ($l)
{
s/&/&/g;
s/</</g;
s/"/"/g;
}
return $l;
}
__PACKAGE__->meta->make_immutable || 1;
__END__
=head1 NAME
RDF::Helper::Properties - Module that provides shortcuts to retrieve certain information
=head1 VERSION
Version 0.22
=head1 SYNOPSIS
my $helper = RDF::Helper::Properties->new($model);
print $helper->title($node);
=head1 DESCRIPTION
=head2 Constructor
=over
=item C<< new(model => $model, %attributes) >>
Moose-style constructor.
=back
=head2 Attributes
=over
=item C<< model >>
The RDF::Trine::Model which data will be extracted from. The only attribute
which the constructor requires.
=item C<< page_properties >>
An arrayref of RDF::Trine::Node::Resource objects, each of which are
taken to mean "something a bit like foaf:homepage". There is a sensible
default.
=item C<< title_properties >>
An arrayref of RDF::Trine::Node::Resource objects, each of which are
taken to mean "something a bit like foaf:name". There is a sensible
default.
=item C<< cache >>
A hashref for caching data into, or a blessed object which supports C<get>
and C<set> methods compatible with L<CHI> and L<Cache::Cache>. If you do not
supply a cache, then a hashref will be used by default.
=back
=head2 Methods
=over
=item C<< page($node) >>
A suitable page to redirect to, based on foaf:page or foaf:homepage.
=item C<< title($node) >>
A suitable title for the document will be returned, based on document contents.
Called in list context, returns a ($value, $lang, $datatype) tuple.
=item C<< description($node) >>
A suitable description for the document will be returned, based on document contents
=item C<< html_node_value($node) >>
Formats the nodes for HTML output.
=back
=begin private
=item C<< cached($subname, $coderef) >>
Install a cached version of a sub.
=end private
=head1 AUTHOR
Most of the code was written by Gregory Todd Williams C<<
<gwilliams@cpan.org> >> for L<RDF::LinkedData::Apache>, but refactored
into this class for use by other modules by Kjetil Kjernsmo, C<<
<kjetilk at cpan.org> >>, then refactored again by Toby Inkster,
C<< <tobyink at cpan.org> >>.
=head1 COPYRIGHT & LICENSE
Copyright 2010 Gregory Todd Williams and ABC Startsiden AS.
Copyright 2012 Toby Inkster.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
|