/usr/share/perl5/Catmandu/Fix/wd_language.pm is in libcatmandu-wikidata-perl 0.06-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 | package Catmandu::Fix::wd_language;
#ABSTRACT: Limit string values in a Wikidata entity record to a selected language
our $VERSION = '0.06'; #VERSION
use Catmandu::Sane;
use Moo;
with 'Catmandu::Fix::Base';
has language => (is => 'ro', required => 1);
has force => (is => 'ro');
around BUILDARGS => sub {
my ($orig, $class, $language) = @_;
$orig->($class, { language => $language });
};
sub emit {
my ($self, $fixer) = @_;
my $language = $self->language;
my $var = $fixer->var;
my $code = $fixer->capture( sub { _fix_code($_[0],$language) } );
return "${code}->(${var})";
}
sub _fix_code {
my ($data, $language) = @_;
foreach my $what (qw(labels descriptions)) {
next unless exists $data->{$what};
my $field = $data->{$what};
if (ref $field) { # keep simple strings as given
my $string = $field->{$language};
if (defined $string) {
$data->{$what} = ref $string ? $string->{value} : $string;
} else {
delete $data->{$what};
}
}
}
if (exists $data->{labels}) {
$data->{label} = delete $data->{labels};
}
if (exists $data->{descriptions}) {
$data->{description} = delete $data->{descriptions};
}
if (ref $data->{aliases} and ref $data->{aliases} eq 'HASH') {
my $aliases = $data->{aliases}->{$language};
if (defined $aliases) {
$data->{aliases} = [
map { ref $_ ? $_->{value} : $_ } @$aliases
];
} else {
$data->{aliases} = [ ];
}
}
# TODO: only delete of string of requested language was found (or force)
$data;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Catmandu::Fix::wd_language - Limit string values in a Wikidata entity record to a selected language
=head1 VERSION
version 0.06
=head1 DESCRIPTION
This L<Catmandu::Fix> modifies a Wikidata entity record, as imported by
L<Catmandu::Importer::Wikidata>, by deleting all language tagged strings (in
C<aliases>, C<labels>, and C<descriptions>) except a selected language. The
strings are also simplified as done with L<Catmandu::Fix::wd_simple_strings>.
=head1 AUTHOR
Jakob Voß
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Jakob Voß.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|