Koha/catalogue/showmarc.pl
Julian Maurice 76e980bb1a Bug 29333: Fix encoding of imported UNIMARC authorities
MARC::Record and MARC::File::* modules sometimes use the position 09 of
the leader to detect encoding. A blank character means 'MARC-8' while an
'a' means 'UTF-8'.

In a UNIMARC authority this position is used to store the authority type
(see https://www.transition-bibliographique.fr/wp-content/uploads/2021/02/AIntroLabel-2004.pdf [FR]).
In this case, 'a' means 'Personal Name'.

The result is that the import will succeed for a Personal Name
authority, but it will fail for all other authority types.

Steps to reproduce:
0. Be sure to have a Koha UNIMARC instance.
1. Download the MARCXML for "Honoré de Balzac"
   curl -o balzac.marcxml https://www.idref.fr/02670305X.xml
2. Verify that it's encoded in UTF-8
   file balzac.marcxml
   (should output "balzac.marcxml: XML 1.0 document, UTF-8 Unicode
   text")
3. Go to Tools » Stage MARC for import and import balzac.marcxml with
   the following settings:
   Record type: Authority
   Character encoding: UTF-8
   Format: MARCXML
   Do not touch the other settings
4. Once imported, go to the staged MARC management tool and find your
   batch. Click on the authority title "Balzac Honoré de 1799-1850" to
   show the MARC inside a modal window. There should be no encoding
   issue.
5. Write down the imported record id (the number in column '#') and go
   to the MARC authority editor. Replace all URL parameters by
   'breedingid=THE_ID_YOU_WROTE_DOWN'
   The URL should look like this:
   /cgi-bin/koha/authorities/authorities.pl?breedingid=198
   You should see no encoding issues. Do not save the record.
6. Import the batch into the catalog. Verify that the authority record
   has no encoding issue.
7. Now download the MARCXML for "Athènes (Grèce)"
   curl -o athènes.marcxml https://www.idref.fr/027290530.xml
8. Repeat steps 2 to 6 using athènes.marcxml file. At steps 4 and 5 you
   should see encoding issues and that the position 9 of the leader was
   rewritten from 'c' to 'a'. Strangely, importing this batch fix the
   encoding issue, but we still lose the information in position 09 of
   the leader

This patch makes use of the MARCXML representation of the record instead
of the ISO2709 representation, because, unlike
MARC::Record::new_from_usmarc, MARC::Record::new_from_xml allows us to
pass directly the encoding and the format, which prevents data to be
double encoded when position 09 of the leader is different that 'a'

Test plan:
- Follow the "steps to reproduce" above and verify that you have no
  encoding issues.

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit 01d78e1ec7)

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
2022-08-23 15:30:11 +00:00

93 lines
2.8 KiB
Perl
Executable file

#!/usr/bin/perl
# Koha library project www.koha-community.org
# Copyright 2007 Liblime
# Parts copyright 2010 BibLibre
#
# This file is part of Koha.
#
# 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 <http://www.gnu.org/licenses>.
use Modern::Perl;
# standard or CPAN modules used
use CGI qw(:standard -utf8);
use Encode;
# Koha modules used
use C4::Context;
use C4::Output qw( output_html_with_http_headers );
use C4::Auth qw( get_template_and_user );
use C4::Biblio qw( GetMarcBiblio GetXmlBiblio );
use C4::XSLT;
use Koha::Import::Records;
my $input= CGI->new;
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{
template_name => "catalogue/showmarc.tt",
query => $input,
type => "intranet",
flagsrequired => { catalogue => 1 },
}
);
my $biblionumber= $input->param('id');
my $importid= $input->param('importid');
my $view= $input->param('viewas')||'';
my $marcflavour = C4::Context->preference('marcflavour');
my $record;
my $record_type = 'biblio';
my $format = $marcflavour eq 'UNIMARC' ? 'UNIMARC' : 'USMARC';
if ($importid) {
my $import_record = Koha::Import::Records->find($importid);
if ($import_record) {
if ($marcflavour eq 'UNIMARC' && $import_record->record_type eq 'auth') {
$format = 'UNIMARCAUTH';
}
$record = $import_record->get_marc_record({ embed_items => 1 });
}
}
else {
$record =GetMarcBiblio({ biblionumber => $biblionumber });
}
if(!ref $record) {
print $input->redirect("/cgi-bin/koha/errors/404.pl");
exit;
}
if($view eq 'card' || $view eq 'html') {
my $xml = $importid ? $record->as_xml($format): GetXmlBiblio($biblionumber);
my $xsl;
if ( $view eq 'card' ){
$xsl = $marcflavour eq 'UNIMARC' ? 'UNIMARC_compact.xsl' : 'compact.xsl';
}
else {
$xsl = 'plainMARC.xsl';
}
my $htdocs = C4::Context->config('intrahtdocs');
my ($theme, $lang) = C4::Templates::themelanguage($htdocs, $xsl, 'intranet', $input);
$xsl = "$htdocs/$theme/$lang/xslt/$xsl";
print $input->header(-charset => 'UTF-8'),
Encode::encode_utf8(C4::XSLT::engine->transform($xml, $xsl));
}
else {
$template->param( MARC_FORMATTED => $record->as_formatted );
output_html_with_http_headers $input, $cookie, $template->output;
}