#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
-#
-#
-use strict;
-#use warnings; FIXME - Bug 2505
+
+use Modern::Perl;
# please specify in which methods a given module is used
use MARC::Record; # marc2marcxml, marcxml2marc, changeEncoding
use MARC::File::XML; # marc2marcxml, marcxml2marc, changeEncoding
use Biblio::EndnoteStyle;
-use Unicode::Normalize; # _entity_encode
-use C4::Biblio; #marc2bibtex
+use Unicode::Normalize qw( NFC ); # _entity_encode
+use C4::Biblio qw( GetFrameworkCode );
use C4::Koha; #marc2csv
-use C4::XSLT ();
-use YAML; #marcrecords2csv
+use C4::XSLT;
+use YAML::XS; #marcrecords2csv
+use Encode;
use Template;
use Text::CSV::Encoded; #marc2csv
-use Koha::SimpleMARC qw(read_field);
-use Koha::XSLT_Handler;
+use Koha::Items;
+use Koha::SimpleMARC qw( read_field );
+use Koha::XSLT::Base;
use Koha::CsvProfiles;
-use Carp;
+use Koha::AuthorisedValues;
+use Carp qw( carp croak );
use vars qw(@ISA @EXPORT);
# only export API methods
@EXPORT = qw(
- &marc2endnote
- &marc2marc
- &marc2marcxml
- &marcxml2marc
- &marc2dcxml
- &marc2modsxml
- &marc2madsxml
- &marc2bibtex
- &marc2csv
- &changeEncoding
+ marc2endnote
+ marc2marc
+ marc2marcxml
+ marcxml2marc
+ marc2dcxml
+ marc2modsxml
+ marc2madsxml
+ marc2bibtex
+ marc2csv
+ marcrecord2csv
+ changeEncoding
);
=head1 NAME
sub marc2marc {
my ($marc,$to_flavour,$from_flavour,$encoding) = @_;
my $error;
- if ($to_flavour =~ m/marcstd/) {
+ if ($to_flavour && $to_flavour =~ m/marcstd/) {
my $marc_record_obj;
if ($marc =~ /^MARC::Record/) { # it's already a MARC::Record object
$marc_record_obj = $marc;
my dcxml = marc2dcxml (undef, undef, 1, "oaidc");
Convert MARC or MARCXML to Dublin Core metadata (XSLT Transformation),
-optionally can get an XML directly from database (biblioitems.marcxml)
+optionally can get an XML directly from biblio_metadata
without item information. This method take into consideration the syspref
-'marcflavour' (UNIMARC, MARC21 and NORMARC).
+'marcflavour' (UNIMARC or MARC21).
Return an XML file with the format defined in C<$format>
C<$marc> - an ISO-2709 scalar or MARC::Record object
C<$xml> - a MARCXML file
-C<$biblionumber> - obtain the record directly from database (biblioitems.marcxml)
+C<$biblionumber> - biblionumber for database access
C<$format> - accept three type of DC formats (oaidc, srwdc, and rdfdc )
# no need to catch errors or warnings marc2marcxml do it instead
$marcxml = C4::Record::marc2marcxml( $marc );
} elsif ( not defined $xml and defined $biblionumber ) {
- # get MARCXML biblio directly from biblioitems.marcxml without item information
+ # get MARCXML biblio directly without item information
$marcxml = C4::Biblio::GetXmlBiblio( $biblionumber );
} else {
$marcxml = $xml;
}
- # only proceed if MARC21 or UNIMARC; else clause is executed if marcflavour set it to NORMARC
- # generate MARC::Record object to see if not a marcxml record
- unless ( C4::Context->preference('marcflavour') eq 'NORMARC' ) {
- eval { $record = MARC::Record->new_from_xml(
- $marcxml,
- 'UTF-8',
- C4::Context->preference('marcflavour')
- );
- };
- } else {
- eval { $record = MARC::Record->new_from_xml(
- $marcxml,
- 'UTF-8',
- 'MARC21'
- );
- };
- }
+ eval { $record = MARC::Record->new_from_xml(
+ $marcxml,
+ 'UTF-8',
+ C4::Context->preference('marcflavour')
+ );
+ };
# conversion to MARC::Record object failed
if ( $@ ) {
carp "\t". $warn;
};
} elsif ( $record =~ /^MARC::Record/ ) { # if OK makes xslt transformation
- my $xslt_engine = Koha::XSLT_Handler->new;
- if ( $format =~ /oaidc|srwdc|rdfdc/ ) {
+ my $xslt_engine = Koha::XSLT::Base->new;
+ if ( $format =~ /^(dc|oaidc|srwdc|rdfdc)$/i ) {
$output = $xslt_engine->transform( $marcxml, $xsl );
} else {
croak "The format argument ($format) not accepted.\n" .
"Please pass a valid format (oaidc, srwdc, or rdfdc)\n";
}
- my $err = $xslt_engine->err; # error number
- my $errstr = $xslt_engine->errstr; # error message
+ my $err = $xslt_engine->err; # error code
if ( $err ) {
- croak "Error when processing $errstr Error number: $err\n";
+ croak "Error $err while processing\n";
} else {
return $output;
}
Year => $marc_rec_obj->publication_date,
Abstract => $abstract,
};
- my $endnote;
- my $style = new Biblio::EndnoteStyle();
+ my $style = Biblio::EndnoteStyle->new();
my $template;
$template.= "DB - DB\n" if C4::Context->preference("LibraryName");
$template.="T1 - Title\n" if $marc_rec_obj->title();
my $configfile = "../tools/csv-profiles/$id.yaml";
my ($preprocess, $postprocess, $fieldprocessing);
if (-e $configfile){
- ($preprocess,$postprocess, $fieldprocessing) = YAML::LoadFile($configfile);
+ ($preprocess,$postprocess, $fieldprocessing) = YAML::XS::LoadFile($configfile);
}
# Preprocessing
- eval $preprocess if ($preprocess);
+ eval $preprocess if ($preprocess); ## no critic (StringyEval)
my $firstpass = 1;
if ( @$itemnumbers ) {
for my $itemnumber ( @$itemnumbers) {
- my $biblionumber = GetBiblionumberFromItemnumber $itemnumber;
- $output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber] );
+ my $item = Koha::Items->find( $itemnumber );
+ my $biblionumber = $item->biblio->biblionumber;
+ $output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber] ) // '';
$firstpass = 0;
}
} else {
foreach my $biblio (@$biblios) {
- $output .= marcrecord2csv( $biblio, $id, $firstpass, $csv, $fieldprocessing );
+ $output .= marcrecord2csv( $biblio, $id, $firstpass, $csv, $fieldprocessing ) // '';
$firstpass = 0;
}
}
# Postprocessing
- eval $postprocess if ($postprocess);
+ eval $postprocess if ($postprocess); ## no critic (StringyEval)
return $output;
}
=cut
sub marcrecord2csv {
- my ($biblio, $id, $header, $csv, $fieldprocessing, $itemnumbers) = @_;
+ my ($biblionumber, $id, $header, $csv, $fieldprocessing, $itemnumbers) = @_;
my $output;
# Getting the record
- my $record = GetMarcBiblio($biblio);
+ my $biblio = Koha::Biblios->find($biblionumber);
+ return unless $biblio;
+ my $record = eval {
+ $biblio->metadata->record(
+ { embed_items => 1, itemnumbers => $itemnumbers } );
+ };
return unless $record;
- C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblio, $itemnumbers );
# Getting the framework
- my $frameworkcode = GetFrameworkCode($biblio);
+ my $frameworkcode = $biblio->frameworkcode;
# Getting information about the csv profile
my $profile = Koha::CsvProfiles->find($id);
my @fields;
while ( $content =~ m|(\d{3})\$?(.)?|g ) {
my $fieldtag = $1;
- my $subfieldtag = $2 || undef;
+ my $subfieldtag = $2;
push @fields, { fieldtag => $fieldtag, subfieldtag => $subfieldtag };
}
if ( @result == 2) {
} else {
# If not, we get the matching tag name from koha
my $tag = $tags->[0];
- if ( $tag->{subfieldtag} ) {
+ if (defined $tag->{subfieldtag} ) {
my $query = "SELECT liblibrarian FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield=?";
my @results = $dbh->selectrow_array( $query, {}, $tag->{fieldtag}, $tag->{subfieldtag} );
push @marcfieldsheaders, $results[0];
if ( $content =~ m|\[\%.*\%\]| ) {
my $tt = Template->new();
my $template = $content;
- my $vars;
# Replace 00X and 0XX with X or XX
$content =~ s|fields.00(\d)|fields.$1|g;
$content =~ s|fields.0(\d{2})|fields.$1|g;
my @fields = $record->field( $tag->{fieldtag} );
# If it is a subfield
my @loop_values;
- if ( $tag->{subfieldtag} ) {
+ if (defined $tag->{subfieldtag} ) {
+ my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, tagsubfield => $tag->{subfieldtag}, });
+ $av = $av->count ? $av->unblessed : [];
+ my $av_description_mapping = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av };
# For each field
foreach my $field (@fields) {
my @subfields = $field->subfield( $tag->{subfieldtag} );
foreach my $subfield (@subfields) {
- my $authvalues = GetKohaAuthorisedValuesFromField( $tag->{fieldtag}, $tag->{subfieldtag}, $frameworkcode, undef);
- push @loop_values, (defined $authvalues->{$subfield}) ? $authvalues->{$subfield} : $subfield;
+ push @loop_values, (defined $av_description_mapping->{$subfield}) ? $av_description_mapping->{$subfield} : $subfield;
}
}
# Or a field
} else {
- my $authvalues = GetKohaAuthorisedValuesFromField( $tag->{fieldtag}, undef, $frameworkcode, undef);
+ my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, });
+ $av = $av->count ? $av->unblessed : [];
+ my $authvalues = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av };
foreach my $field ( @fields ) {
my $value;
# Field processing
my $marcfield = $tag->{fieldtag}; # This line fixes a retrocompatibility concern
# The "processing" could be based on the $marcfield variable.
- eval $fieldprocessing if ($fieldprocessing);
+ eval $fieldprocessing if ($fieldprocessing); ## no critic (StringyEval)
push @loop_values, $value;
}
my $additional_fields;
if ($BibtexExportAdditionalFields) {
$BibtexExportAdditionalFields = "$BibtexExportAdditionalFields\n\n";
- $additional_fields = eval { YAML::Load($BibtexExportAdditionalFields); };
+ $additional_fields = eval { YAML::XS::Load(Encode::encode_utf8($BibtexExportAdditionalFields)); };
if ($@) {
warn "Unable to parse BibtexExportAdditionalFields : $@";
$additional_fields = undef;