From 29c52d8c6e857fa4086f130ab0d436b045835863 Mon Sep 17 00:00:00 2001 From: Andreas Roussos Date: Mon, 7 Jul 2025 03:01:04 +0300 Subject: [PATCH] Bug 40309: add new Authority Framework plugin for MARC21 082$a This Authority Framework plugin should be attached to MARC21 authority subfield 082$a, which will then allow the cataloguer to lookup a single DDC in exchange for a Dewey Linked Data URL that will be copied to subfield 082$1. Test plan: 1) Go to Administration > Authority types, and for each authtype associate subfield 082$a with the marc21_field_082a_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 082$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. 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. --- .../marc21_field_082a_authorities.pl | 161 ++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100755 cataloguing/value_builder/marc21_field_082a_authorities.pl diff --git a/cataloguing/value_builder/marc21_field_082a_authorities.pl b/cataloguing/value_builder/marc21_field_082a_authorities.pl new file mode 100755 index 0000000000..cd07e9f7a0 --- /dev/null +++ b/cataloguing/value_builder/marc21_field_082a_authorities.pl @@ -0,0 +1,161 @@ +#!/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 ); +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 $enabled = C4::Context->preference('OCLCDeweyLinkedDataAPI') ? 1 : 0; + if ( not $enabled ) { + $ret = 'Please enable the OCLCDeweyLinkedDataAPI System Preference in order to use this Framework Plugin'; + } + + my $access_token; + if ( not $ret ) { + my $scope = 'deweyLinkedData'; + + ( $access_token, $msg ) = Koha::ExternalContent::OCLC->get_access_token( + { + scope => $scope, + } + ); + if ( not $access_token ) { + $ret = "Cannot obtain API token ($msg)"; + } + } + + if ( not $ret ) { + + # Trim leading 'T' (uppercase 't') from DDC number before looking it up + my $ddc_lookup = $ddc_number =~ s/^T//r; + + my $ddc_url; + ( $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' ) { + $ret = 'The API access token has expired'; + } elsif ( $ddc_url eq '' ) { + $ret = "No Dewey Linked Data URL found for DDC '$ddc_number'"; + } else { + $ret = $ddc_url; + } + } + + $template->param( return => $ret ); + output_html_with_http_headers $input, $cookie, $template->output; + +}; + +return { builder => $builder, launcher => $launcher }; -- 2.39.5