From 1fe5a47629605b0604b7c258176927c271dab1d1 Mon Sep 17 00:00:00 2001 From: Andreas Roussos Date: Mon, 7 Jul 2025 03:06:12 +0300 Subject: [PATCH] Bug 40310: add new Authority Framework plugin for MARC21 083$a This Authority Framework plugin should be attached to MARC21 authority subfield 083$a, which will then allow the cataloguer to lookup a single DDC (or a DDC range if 083$b is also filled in) in exchange for a Dewey Linked Data URL and Description that will be copied to subfields 083$1 and 083$c, respectively. Test plan: 1) Go to Administration > Authority types, and for each authtype associate subfield 083$a with the marc21_field_083a_authorities.pl plugin. 2) Create a new authority, or edit an existing one. Click on the 'Tag editor' button on the far right of the 083$a subfield. Experiment with the behaviour of the plugin when no DDC has been filled in, or when an erroneous DDC has been filled in, or when 083$b has also been filled in. Monitor your browser's JavaScript console for warnings/errors (there shouldn't be any!). 3) Experiment with changing the values of the related System Preferences (found under the 'OCLC Dewey Linked Data API' heading) and seeing how it affects the behaviour of the plugin launcher. --- .../marc21_field_083a_authorities.pl | 221 ++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100755 cataloguing/value_builder/marc21_field_083a_authorities.pl diff --git a/cataloguing/value_builder/marc21_field_083a_authorities.pl b/cataloguing/value_builder/marc21_field_083a_authorities.pl new file mode 100755 index 0000000000..202ee1e25a --- /dev/null +++ b/cataloguing/value_builder/marc21_field_083a_authorities.pl @@ -0,0 +1,221 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Copyright (C) 2025 Dataly Tech +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use C4::Auth qw( check_cookie_auth get_template_and_user ); +use C4::Context qw( preference ); +use C4::Output qw( output_html_with_http_headers ); + +use Koha::Caches; +use Koha::ExternalContent::OCLC; + +use CGI qw( -utf8 ); +use JSON qw( from_json to_json ); +use LWP::UserAgent; + +use YAML; + +my $input = CGI->new; +my ($auth_status) = + check_cookie_auth( $input->cookie('CGISESSID'), { catalogue => 1 } ); +if ( $auth_status ne "ok" ) { + print $input->header( -type => 'text/plain', -status => '403 Forbidden' ); + exit 0; +} + +my $builder = sub { + my ($params) = @_; + + my $function_name = $params->{id}; + + my $ddcregex = '^((T([1-6])--([0-9]+)))$|^([0-9]{3})$|^(([0-9]{3})\.([0-9]+))$'; + + my $res = " + +"; + return $res; +}; + +my $launcher = sub { + my ($params) = @_; + my $input = $params->{cgi}; + my $ddc_number = $input->param('ddc'); + + my ( $template, $loggedinuser, $cookie ) = get_template_and_user( + { + template_name => "cataloguing/value_builder/ajax.tt", + query => $input, + type => "intranet", + flagsrequired => { editauthorities => 1 }, + } + ); + + my @ret; + my $msg; + my $info = undef; + my $enabled = C4::Context->preference('OCLCDeweyLinkedDataAPI') ? 1 : 0; + my $language = C4::Context->preference('OCLCDeweyLinkedDataLanguage'); + + if ( not $enabled ) { + $info = 'Please enable the OCLCDeweyLinkedDataAPI System Preference in order to use this Framework Plugin'; + } + + my $access_token; + if ( not $info ) { + + my $scope = 'deweyLinkedData'; + + ( $access_token, $msg ) = Koha::ExternalContent::OCLC->get_access_token( + { + scope => $scope, + } + ); + if ( not $access_token ) { + $info = "Cannot obtain API token ($msg)"; + } + + } + + # Trim leading 'T' (uppercase 't') from DDC number (or range) before looking it up + my $ddc_lookup = $ddc_number =~ s/^T//r; + + my $ddc_uri; + if ( not $info ) { + + ( $ddc_uri, $msg ) = Koha::ExternalContent::OCLC->get_ddc_uri( + { + access_token => $access_token, + ddc_number => $ddc_lookup + } + ); + if ( not $ddc_uri and $msg eq 'Unauthorized' ) { + $info = 'The API access token has expired'; + } elsif ( $ddc_uri eq '' ) { + $info = "No Dewey Linked Data URI found for DDC '$ddc_number'"; + } + } + + my $ddc_url; + if ( not $info ) { + + ( $ddc_url, $msg ) = Koha::ExternalContent::OCLC->get_ddc_url( + { + access_token => $access_token, + ddc_number => $ddc_lookup + } + ); + if ( not $ddc_url and $msg eq 'Unauthorized' ) { + $info = 'The API access token has expired'; + } elsif ( $ddc_url eq '' ) { + $info = "No Dewey Linked Data URL found for DDC '$ddc_number'"; + } + + } + + my $ddc_descr; + if ( not $info ) { + + ( $ddc_descr, $msg ) = Koha::ExternalContent::OCLC->get_ddc_description( + { + access_token => $access_token, + ddc_uri => $ddc_uri, + language => $language, + } + ); + if ( not $ddc_descr and $msg eq 'Unauthorized' ) { + $info = 'The API access token has expired'; + } elsif ( not defined $ddc_descr ) { + $info = "No Dewey Linked Data Description found for DDC URI '$ddc_uri'"; + } + + } + + push @ret, { + info => $info, + ld_url => $ddc_url, + ld_descr => $ddc_descr, + }; + output_html_with_http_headers $input, $cookie, to_json( \@ret, { utf8 => 1 } ), 'json'; +}; + +return { builder => $builder, launcher => $launcher }; -- 2.39.5