Bugzilla – Attachment 155966 Details for
Bug 34860
New framework plugin for ISBN/ISSN
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 34860: New framework plugin for ISBN/ISSN
Bug-34860-New-framework-plugin-for-ISBNISSN.patch (text/plain), 10.44 KB, created by
Marcel de Rooy
on 2023-09-21 06:19:16 UTC
(
hide
)
Description:
Bug 34860: New framework plugin for ISBN/ISSN
Filename:
MIME Type:
Creator:
Marcel de Rooy
Created:
2023-09-21 06:19:16 UTC
Size:
10.44 KB
patch
obsolete
>From 663e6053b0bbab6630d8b242d500d5e8b4f77b28 Mon Sep 17 00:00:00 2001 >From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> >Date: Thu, 21 Sep 2023 08:03:19 +0200 >Subject: [PATCH] Bug 34860: New framework plugin for ISBN/ISSN >Content-Type: text/plain; charset=utf-8 > >A Koha patch should still be submitted. The REST API does not >yet support an index search like the one in isbn_count. >--- > cataloguing/value_builder/rma_isbn.pl | 51 +++++++++++++++++++ > cataloguing/value_builder/rma_issn.pl | 50 ++++++++++++++++++ > .../prog/en/modules/cataloguing/addbiblio.tt | 2 + > koha-tmpl/intranet-tmpl/prog/js/isbn.js | 47 +++++++++++++++++ > koha-tmpl/intranet-tmpl/prog/js/issn.js | 35 +++++++++++++ > svc/isbn_count | 48 +++++++++++++++++ > svc/issn_count | 31 +++++++++++ > 7 files changed, 264 insertions(+) > create mode 100755 cataloguing/value_builder/rma_isbn.pl > create mode 100755 cataloguing/value_builder/rma_issn.pl > create mode 100644 koha-tmpl/intranet-tmpl/prog/js/isbn.js > create mode 100644 koha-tmpl/intranet-tmpl/prog/js/issn.js > create mode 100755 svc/isbn_count > create mode 100755 svc/issn_count > >diff --git a/cataloguing/value_builder/rma_isbn.pl b/cataloguing/value_builder/rma_isbn.pl >new file mode 100755 >index 0000000000..0b7163dacd >--- /dev/null >+++ b/cataloguing/value_builder/rma_isbn.pl >@@ -0,0 +1,51 @@ >+#!/usr/bin/perl >+ >+use Modern::Perl; >+use CGI; >+ >+my $builder = sub { >+ my ( $params ) = @_; >+ my $field_number = $params->{id}; >+ >+ my $res=" >+<script type=\"text/javascript\" charset=\"utf-8\"> >+//<![CDATA[ >+ >+function Change$field_number() { >+ var isbn = jQuery('#$field_number').val(); >+ var biblionumber = jQuery('input[name=\"biblionumber\"]').val(); >+ if ( isbn == '' ) change_color_isbn(); >+ else api_isbn_count( isbn, biblionumber, change_color_isbn ); >+ return 1; >+} >+ >+function change_color_isbn (color) { >+ jQuery('#$field_number').removeClass( 'callnumber_green callnumber_red' ); >+ if ( color ) jQuery('#$field_number').addClass( 'callnumber_' + color ); >+} >+ >+function api_isbn_count ( isbn, biblionumber, callback ) { >+ var checked = isbn_check(isbn); >+ if( !checked ) { >+ callback('red'); >+ alert( isbn + ': is not a valid ISBN!' ); >+ return; >+ } >+ jQuery.ajax({ >+ method: 'GET', >+ url: '/cgi-bin/koha/svc/isbn_count?isbn=' + checked + ( biblionumber ? 'skip=' + biblionumber : '' ), >+ }).done(function (data) { >+ jQuery('#$field_number').val(checked); >+ callback( !data || data.count ? 'red' : 'green' ); >+ }).fail(function () { >+ alert('An error occurred when checking isbn'); >+ }); >+} >+ >+//]]> >+</script> >+"; >+return $res; >+}; >+ >+return { builder => $builder }; >diff --git a/cataloguing/value_builder/rma_issn.pl b/cataloguing/value_builder/rma_issn.pl >new file mode 100755 >index 0000000000..dbca24944a >--- /dev/null >+++ b/cataloguing/value_builder/rma_issn.pl >@@ -0,0 +1,50 @@ >+#!/usr/bin/perl >+ >+use Modern::Perl; >+use CGI; >+ >+my $builder = sub { >+ my ( $params ) = @_; >+ my $field_number = $params->{id}; >+ >+ my $res=" >+<script type=\"text/javascript\" charset=\"utf-8\"> >+//<![CDATA[ >+ >+function Change$field_number() { >+ var issn = jQuery('#$field_number').val(); >+ var biblionumber = jQuery('input[name=\"biblionumber\"]').val(); >+ if ( issn == '' ) change_color_issn(); >+ else api_issn_count( issn, biblionumber, change_color_issn ); >+ return 1; >+} >+ >+function change_color_issn (color) { >+ jQuery('#$field_number').removeClass( 'callnumber_green callnumber_red' ); >+ if ( color ) jQuery('#$field_number').addClass( 'callnumber_' + color ); >+} >+ >+function api_issn_count ( issn, biblionumber, callback ) { >+ var checked = issn_check(issn); >+ if( !checked ) { >+ callback('red'); >+ alert( issn + ': is not a valid ISSN!' ); >+ return; >+ } >+ jQuery.ajax({ >+ method: 'GET', >+ url: '/cgi-bin/koha/svc/issn_count?issn=' + checked + ( biblionumber ? 'skip=' + biblionumber : '' ), >+ }).done(function (data) { >+ callback( !data || data.count ? 'red' : 'green' ); >+ }).fail(function () { >+ alert('An error occurred when checking issn'); >+ }); >+} >+ >+//]]> >+</script> >+"; >+return $res; >+}; >+ >+return { builder => $builder }; >diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt >index 787d19cb56..56ba3d7a98 100644 >--- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt >+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt >@@ -27,6 +27,8 @@ > </script> > [% Asset.js("js/cataloging.js") | $raw %] > [% Asset.js("js/browser.js") | $raw %] >+[% Asset.js("js/isbn.js") | $raw %] >+[% Asset.js("js/issn.js") | $raw %] > <script> > var browser = KOHA.browser('[% searchid | html %]', parseInt('[% biblionumber | html %]', 10)); > browser.show(); >diff --git a/koha-tmpl/intranet-tmpl/prog/js/isbn.js b/koha-tmpl/intranet-tmpl/prog/js/isbn.js >new file mode 100644 >index 0000000000..2433e6e121 >--- /dev/null >+++ b/koha-tmpl/intranet-tmpl/prog/js/isbn.js >@@ -0,0 +1,47 @@ >+// RMA >+// Check iSBN from MARC21 020$a field (or UNIMARC counterpart) >+// Note: hyphens are removed during the check ! >+// Check digit algorithm from an example on oreilly.com (regular expression cookbook) >+// TODO: Stripping a suffix should be removed later. >+ >+function isbn_check (isbn_subfield_a) { >+ // Remove hyphens and suffix >+ var isbn = isbn_subfield_a.replace( /[-]/g, '' ); >+ var regex = /^(\d{9}(\d{3})?[0-9X])(?=\s|$)/; >+ var matches; >+ if ( matches = isbn.match(regex) ) { >+ if( valid_isbn_check_digit( matches[1] ) ) return matches[1]; >+ } >+ return ''; >+} >+ >+function valid_isbn_check_digit(isbn) { >+ var chars = isbn.split(""); >+ var last = chars.pop(); >+ var sum = 0; >+ var check, i; >+ if (chars.length == 9) { >+ // Compute the ISBN-10 check digit >+ chars.reverse(); >+ for (i = 0; i < chars.length; i++) { >+ sum += (i + 2) * parseInt(chars[i], 10); >+ } >+ check = 11 - (sum % 11); >+ if (check == 10) { >+ check = "X"; >+ } else if (check == 11) { >+ check = "0"; >+ } >+ } else { >+ // Compute the ISBN-13 check digit >+ for (i = 0; i < chars.length; i++) { >+ sum += (i % 2 * 2 + 1) * parseInt(chars[i], 10); >+ } >+ check = 10 - (sum % 10); >+ if (check == 10) { >+ check = "0"; >+ } >+ } >+ if (check == last) return 1; >+ return 0; >+} >diff --git a/koha-tmpl/intranet-tmpl/prog/js/issn.js b/koha-tmpl/intranet-tmpl/prog/js/issn.js >new file mode 100644 >index 0000000000..0c7f1145bb >--- /dev/null >+++ b/koha-tmpl/intranet-tmpl/prog/js/issn.js >@@ -0,0 +1,35 @@ >+// RMA >+// Check ISSN from MARC21 022$a field (or UNIMARC counterpart) >+// Note: We assume NNNN-NNNC format. >+// Check digit algorithm based on example on oreilly.com (regular expression cookbook) >+// TODO: Stripping a suffix should be removed later. >+ >+function issn_check (issn) { >+ // Remove suffix >+ var regex = /^(\d{4}\-\d{3}[0-9X])(?=\s|$)/; >+ var matches; >+ if ( matches = issn.match(regex) ) { >+ if( valid_issn_check_digit( matches[1] ) ) return matches[1]; >+ } >+ return ''; >+} >+ >+function valid_issn_check_digit(issn) { >+ var chars = issn.replace( /[-]/g, '' ).split(""); >+ var last = chars.pop(); >+ var sum = 0; >+ var check, i; >+ if ( chars.length == 7 ) { >+ // Compute the ISSN check digit >+ chars.reverse(); >+ for (i = 0; i < chars.length; i++) { >+ sum += (i + 2) * parseInt(chars[i], 10); >+ } >+ check = 11 - (sum % 11); >+ if (check == 10) { >+ check = "X"; >+ } >+ } >+ if (check == last) return 1; >+ return 0; >+} >diff --git a/svc/isbn_count b/svc/isbn_count >new file mode 100755 >index 0000000000..b55c063e91 >--- /dev/null >+++ b/svc/isbn_count >@@ -0,0 +1,48 @@ >+#!/usr/bin/perl >+ >+# Copyright Rijksmuseum 2023 >+ >+# We still need this svc script, since the REST API biblio endpoint does not allow index searches. >+ >+use Modern::Perl; >+use CGI qw ( -utf8 ); >+use JSON; >+use C4::Auth qw(); >+use C4::Search qw(SimpleSearch); >+ >+my $query = CGI->new; >+my $isbn = $query->param('isbn'); >+my $skip = $query->param('skip'); # this is a biblionumber >+ >+my ( $status, $cookie, $sessionID ) = C4::Auth::check_api_auth( $query, { catalogue => 1 } ); >+unless ( $status eq "ok" ) { >+ print $query->header(-type => 'text/plain', -status => '403 Forbidden' ); >+ exit 0; >+} >+if( !$isbn ) { >+ print $query->header(-type => 'text/plain', -status => '400 Bad request' ); >+ exit 0; >+} >+ >+my ( undef, undef, $count ) = SimpleSearch( $skip ? "nb=$isbn not cnum=$skip" : "nb=$isbn" ); >+# my $count = dbsearch( $isbn, $skip ); >+ >+print $query->header( -type => 'application/json', -charset => 'UTF-8' ); >+print JSON::encode_json( { count => $count } ); >+ >+__END__ >+ >+sub dbsearch { # slower than zebra with a larger database >+ my ( $isbn, $skip ) = @_; >+ return Koha::Biblioitems->search( >+ { >+ -or => [ >+ { isbn => $isbn }, >+ { isbn => { '-LIKE', "$isbn %" } }, >+ { isbn => { '-LIKE', "% $isbn" } }, >+ { isbn => { '-LIKE', "% $isbn %" } }, >+ ], >+ $skip ? ( biblionumber => { '!=', $skip } ) : (), >+ } >+ )->count; >+} >diff --git a/svc/issn_count b/svc/issn_count >new file mode 100755 >index 0000000000..f79c9bbd06 >--- /dev/null >+++ b/svc/issn_count >@@ -0,0 +1,31 @@ >+#!/usr/bin/perl >+ >+# Copyright Rijksmuseum 2023 >+ >+# We still need this svc script, since the REST API biblio endpoint does not allow index searches. >+ >+use Modern::Perl; >+use CGI qw ( -utf8 ); >+use JSON; >+use C4::Auth qw(); >+use C4::Search qw(SimpleSearch); >+#use Koha::Biblioitems; >+ >+my $query = CGI->new; >+my $issn = $query->param('issn'); >+my $skip = $query->param('skip'); # this is a biblionumber >+ >+my ( $status, $cookie, $sessionID ) = C4::Auth::check_api_auth( $query, { catalogue => 1 } ); >+unless ( $status eq "ok" ) { >+ print $query->header(-type => 'text/plain', -status => '403 Forbidden' ); >+ exit 0; >+} >+if( !$issn ) { >+ print $query->header(-type => 'text/plain', -status => '400 Bad request' ); >+ exit 0; >+} >+ >+my ( undef, undef, $count ) = SimpleSearch( $skip ? "ns=$issn not cnum=$skip" : "ns=$issn" ); >+ >+print $query->header( -type => 'application/json', -charset => 'UTF-8' ); >+print JSON::encode_json( { count => $count } ); >-- >2.30.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 34860
:
155966