Koha/cataloguing/merge.pl
Zeno Tajoli c60a6d8cd9
Bug 33036: REST API: Merge biblio records implements merging of records
+ attached items, subscriptions etc via the API as an alternative to the web interface: cgi-bin/koha/cataloguing/merge.pl

This is a slightly improved version of Zenos patch: I (domm) have converted the code in Koha::Biblio to a more DBICy style and packed it into a transaction (as requested in Comment 23)

Even the QA script is happy now!

To test:
    1) you need an API user with the permissions "editcatalogue"
    2) two records: one to be merged into (with biblio_id, eg 262) and another one from
       which to merge (with biblio_id_to_merge, eg 9) which will be deleted!
       both records may/should have items, subscription, subscriptionhistory, serial, suggestions
       orders and holds
    3) check both records via the web
    4) Apply patch
    5) Write a JSON file with inside the field 'biblio_id_to_merge' and the biblionumber from wihich to merge.
       As example:
       {
         "biblio_id_to_merge" : 9
       }
     6) Execute an API call with correct headers and location. For example:
        curl -s -u koha:koha --header "Content-Type: application/json" --header "Accept: application/marc-in-json"
                  --request POST "http://127.0.0.1:8080/api/v1/biblios/262/merge" -d @file.json
        You must to setup the headers and to use a json file with parameters
     7) The record with the id 9 is deleted now, the record with 262 has all items, etc attached,
        the return is: return code 200 and the changed record 262 in marc-in-json format
     8) It is possible to override biblio data with an external bib record. You need to put external bib record
        into the json file in marc-in-json format. To write use the json file uploaded as example
        You need to fill the fields 'rules' and 'datarecord'. The field 'rules' must contains 'override_ext'
        To do the call:
         curl -s -u koha:koha --header "Content-Type: application/json" --header "Accept: application/marc-in-json"
                  --request POST "http://127.0.0.1:8080/api/v1/biblios/XXX/merge" -d @file_with_recod.json
      9) The record in 'biblio_id_to_merge' is deleted now, in biblio XXX now there are the bibliographic data
         of field 'datarecord' of json file, the return is: return code 200 and the changed record XXX in marc-in-json format
      10) Go into intranet and do a search. Select two or (better) more record.
      11) Merge them; merge must be a success.
      12) Test with prove -v t/db_dependent/Koha/Biblio.t
      13) Test with prove -v t/db_dependent/api/v1/biblios.t

To test with curl the step 8 you can customize the json file attached in bugzilla.
The marc-in-json record inside follows the MAR21 standard

Sponsored-by: Technische Hochschule Wildau
Co-authored-by: Zeno Tajoli <ztajoli@gmail.com>
Co-authored-by: Thomas Klausner <domm@plix.at>
Co-authored-by: Mark Hofstetter <<mark@hofstetter.at>>
Signed-off-by: Jan Kissig <jkissig@th-wildau.de>

Bug 33036: Update of test number.

File ../biblios.t was update with a new subutest.
So we need this update to have a 'OK' after test running.

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-03-18 11:03:39 +01:00

222 lines
7.4 KiB
Perl
Executable file

#!/usr/bin/perl
# Copyright 2009 BibLibre
# Parts Copyright Catalyst IT 2011
#
# 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;
use CGI qw ( -utf8 );
use C4::Output qw( output_html_with_http_headers );
use C4::Auth qw( get_template_and_user );
use C4::Biblio qw(
DelBiblio
GetBiblioData
GetFrameworkCode
GetMarcFromKohaField
GetMarcStructure
ModBiblio
TransformHtmlToMarc
);
use C4::Serials qw( CountSubscriptionFromBiblionumber );
use C4::Reserves qw( MergeHolds );
use C4::Acquisition qw( ModOrder GetOrdersByBiblionumber );
use Koha::BiblioFrameworks;
use Koha::Biblios;
use Koha::Items;
use Koha::MetadataRecord;
my $input = CGI->new;
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{
template_name => "cataloguing/merge.tt",
query => $input,
type => "intranet",
flagsrequired => { editcatalogue => 'edit_catalogue' },
}
);
my @biblionumbers = $input->multi_param('biblionumber');
my $op = $input->param('op') || q{};
my @errors;
#------------------------
# Merging
#------------------------
if ($op eq 'cud-merge') {
my $dbh = C4::Context->dbh;
# Creating a new record from the html code
my $record = TransformHtmlToMarc( $input, 1 );
my $ref_biblionumber = $input->param('ref_biblionumber');
@biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers;
# prepare report
my @report_records;
my $report_fields_str = $input->param('report_fields');
$report_fields_str ||= C4::Context->preference('MergeReportFields');
my @report_fields;
foreach my $field_str (split /,/, $report_fields_str) {
if ($field_str =~ /(\d{3})([0-9a-z]*)/) {
my ($field, $subfields) = ($1, $2);
push @report_fields, {
tag => $field,
subfields => [ split //, $subfields ]
}
}
}
# Rewriting the leader
my $biblio = Koha::Biblios->find($ref_biblionumber);
$record->leader($biblio->metadata->record->leader());
#Take new framework code
my $frameworkcode = $input->param('frameworkcode');
# Modifying the reference record
ModBiblio($record, $ref_biblionumber, $frameworkcode);
my $report_header = {};
foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
# build report
my $biblio = Koha::Biblios->find($biblionumber);
my $marcrecord = $biblio->metadata->record;
my %report_record = (
biblionumber => $biblionumber,
fields => {},
);
foreach my $field (@report_fields) {
my @marcfields = $marcrecord->field($field->{tag});
foreach my $marcfield (@marcfields) {
my $tag = $marcfield->tag();
if (scalar @{$field->{subfields}}) {
foreach my $subfield (@{$field->{subfields}}) {
my @values = $marcfield->subfield($subfield);
$report_header->{ $tag . $subfield } = 1;
push @{ $report_record{fields}->{$tag . $subfield} }, @values;
}
} elsif ($field->{tag} gt '009') {
my @marcsubfields = $marcfield->subfields();
foreach my $marcsubfield (@marcsubfields) {
my ($code, $value) = @$marcsubfield;
$report_header->{ $tag . $code } = 1;
push @{ $report_record{fields}->{ $tag . $code } }, $value;
}
} else {
$report_header->{ $tag . '@' } = 1;
push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
}
}
}
push @report_records, \%report_record;
}
my $rmerge;
eval {
my $newbiblio = Koha::Biblios->find($ref_biblionumber);
$rmerge = $newbiblio->merge_with( \@biblionumbers );
};
if ($@) {
push @errors, $@;
}
# Parameters
$template->param(
result => 1,
report_records => \@report_records,
report_header => $report_header,
ref_biblionumber => scalar $input->param('ref_biblionumber')
);
#-------------------------
# Show records to merge
#-------------------------
} else {
my $ref_biblionumber = $input->param('ref_biblionumber');
if ($ref_biblionumber) {
my $framework = $input->param('frameworkcode');
$framework //= GetFrameworkCode($ref_biblionumber);
# Getting MARC Structure
my $tagslib = GetMarcStructure(1, $framework);
my $marcflavour = lc(C4::Context->preference('marcflavour'));
# Creating a loop for display
my @records;
foreach my $biblionumber (@biblionumbers) {
my $biblio = Koha::Biblios->find($biblionumber);
my $marcrecord = $biblio->metadata->record;
my $frameworkcode = GetFrameworkCode($biblionumber);
my $recordObj = Koha::MetadataRecord->new({'record' => $marcrecord, schema => $marcflavour});
my $record = {
recordid => $biblionumber,
record => $marcrecord,
frameworkcode => $frameworkcode,
display => $recordObj->createMergeHash($tagslib),
};
if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
$record->{reference} = 1;
$template->param(ref_record => $record);
unshift @records, $record;
} else {
push @records, $record;
}
}
my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
# Parameters
$template->param(
ref_biblionumber => $ref_biblionumber,
records => \@records,
ref_record => $records[0],
framework => $framework,
biblionumbertag => $biblionumbertag,
MergeReportFields => C4::Context->preference('MergeReportFields'),
);
} else {
my @records;
foreach my $biblionumber (@biblionumbers) {
my $frameworkcode = GetFrameworkCode($biblionumber);
my $record = {
biblionumber => $biblionumber,
data => GetBiblioData($biblionumber),
frameworkcode => $frameworkcode,
};
push @records, $record;
}
# Ask the user to choose which record will be the kept
$template->param(
choosereference => 1,
records => \@records,
);
my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
$template->param( frameworks => $frameworks );
}
}
if (@errors) {
# Errors
$template->param( errors => \@errors );
}
output_html_with_http_headers $input, $cookie, $template->output;
exit;