Koha/tools/batch_record_modification.pl
David Gustafsson 139e6c30d6 Bug 14957: Merge rules system for merging of MARC records
Add a rule based system for merging MARC records to for example
prevent field data from being overwritten.

To test:
    1. Apply this patch.
    2. Log in to staff client.
    3. Enable new syspref MARCMergeRules.
    4. Click the new link "MARC merge rules" in the "Catalog"
       section of the Koha administration page.
    5. Create a new rule:
       Module: source, Filter: *, Tag: 245, Preset: Protect.
    6. Clicking "Edit" should allow you to edit corresponding rule.
    7. Clicking "Delete" should remove corresponding rule after confirmation.
    8. Selecting one or more rules followed by clicking "Delete
       selected" should remove all selected rules after confirmation.
    9. Try creating a rule with tag set to "**", the other options does
       not matter. Verify that saving this rule produces an error
       message complaining about invalid tag regular expression.
    10. Try creating a rule with tag set to "008" (or other control
        field) and set Appended: Append and Removed: Skip, the other
        options does not matter. Verify that saving this rule produces
        an error message complaining about invalid combination of actions
        for control field.
    11. With the 245 rule in step 5 in place, edit a bibliographic record,
        change 245a for example (which should be Title for MARC21) and save.
    12. Verify that the changes has not been saved.
    13. Create a new rule:
        Module: source, Filter: intranet, Tag: 245, Preset: Overwrite.
    14. Repeat step 12, and verify that the changes has now been saved.
    15. Run tests in t/db_dependent/Biblio/MarcMergeRules.t and very
        that all tests pass.

Sponsored-by: Halland County Library
Sponsored-by: Catalyst IT
Sponsored-by: Gothenburg University Library
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Christian Stelzenmüller <christian.stelzenmueller@bsz-bw.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-10-26 16:46:02 +02:00

191 lines
5.8 KiB
Perl
Executable file

#!/usr/bin/perl
# This file is part of Koha.
#
# Copyright 2013 BibLibre
#
# 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;
use List::MoreUtils qw( uniq );
use Try::Tiny qw( catch try );
use C4::Auth qw( get_template_and_user );
use C4::Output qw( output_html_with_http_headers );
use C4::Auth qw( get_template_and_user );
use C4::MarcModificationTemplates qw(
GetModificationTemplateActions
GetModificationTemplates
);
use Koha::Biblios;
use Koha::BackgroundJob::BatchUpdateBiblio;
use Koha::BackgroundJob::BatchUpdateAuthority;
use Koha::MetadataRecord::Authority;
use Koha::Virtualshelves;
my $input = CGI->new;
our $dbh = C4::Context->dbh;
my $op = $input->param('op') // q|form|;
my $recordtype = $input->param('recordtype') // 'biblio';
my $mmtid = $input->param('marc_modification_template_id');
my ( @messages );
my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
template_name => 'tools/batch_record_modification.tt',
query => $input,
type => "intranet",
flagsrequired => { tools => 'records_batchmod' },
});
my $sessionID = $input->cookie("CGISESSID");
my @templates = GetModificationTemplates( $mmtid );
unless ( @templates ) {
$op = 'error';
$template->param(
view => 'errors',
errors => ['no_template_defined'],
);
output_html_with_http_headers $input, $cookie, $template->output;
exit;
}
if ( $mmtid ) {
my @actions = GetModificationTemplateActions( $mmtid );
unless ( @actions ) {
$op = 'form';
push @messages, {
type => 'error',
code => 'no_action_defined_for_the_template',
mmtid => $mmtid,
};
}
}
if ( $op eq 'form' ) {
# Display the form
$template->param(
view => 'form',
lists => scalar Koha::Virtualshelves->search(
[
{ category => 1, owner => $loggedinuser },
{ category => 2 }
]
)
);
} elsif ( $op eq 'list' ) {
# List all records to process
my ( @records, @record_ids );
if ( my $bib_list = $input->param('bib_list') ) {
# Come from the basket
@record_ids = split /\//, $bib_list;
$recordtype = 'biblio';
} elsif ( my $uploadfile = $input->param('uploadfile') ) {
# A file of id is given
binmode $uploadfile, ':encoding(UTF-8)';
while ( my $content = <$uploadfile> ) {
next unless $content;
$content =~ s/[\r\n]*$//;
push @record_ids, $content if $content;
}
} elsif ( my $shelf_number = $input->param('shelf_number') ) {
my $shelf = Koha::Virtualshelves->find($shelf_number);
my $contents = $shelf->get_contents;
while ( my $content = $contents->next ) {
my $biblionumber = $content->biblionumber;
push @record_ids, $biblionumber;
}
} else {
# The user enters manually the list of id
push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
}
for my $record_id ( uniq @record_ids ) {
if ( $recordtype eq 'biblio' ) {
# Retrieve biblio information
my $biblio = Koha::Biblios->find( $record_id );
unless ( $biblio ) {
push @messages, {
type => 'warning',
code => 'biblio_not_exists',
biblionumber => $record_id,
};
next;
}
push @records, $biblio;
} else {
# Retrieve authority information
my $authority = Koha::MetadataRecord::Authority->get_from_authid( $record_id );
unless ( $authority ) {
push @messages, {
type => 'warning',
code => 'authority_not_exists',
authid => $record_id,
};
next;
}
push @records, {
authid => $record_id,
summary => C4::AuthoritiesMarc::BuildSummary( $authority->record, $record_id ),
};
}
}
$template->param(
records => \@records,
mmtid => $mmtid,
view => 'list',
);
} elsif ( $op eq 'modify' ) {
# We want to modify selected records!
my @record_ids = $input->multi_param('record_id');
try {
my $params = {
mmtid => $mmtid,
record_ids => \@record_ids,
};
my $patron = Koha::Patrons->find( $loggedinuser );
my $job_id =
$recordtype eq 'biblio'
? Koha::BackgroundJob::BatchUpdateBiblio->new->enqueue($params, { source => 'batchmod', categorycode => $patron->categorycode, userid => $patron->userid })
: Koha::BackgroundJob::BatchUpdateAuthority->new->enqueue($params);
$template->param(
view => 'enqueued',
job_id => $job_id,
);
} catch {
push @messages, {
type => 'error',
code => 'cannot_enqueue_job',
error => $_,
};
$template->param( view => 'errors' );
};
}
$template->param(
messages => \@messages,
recordtype => $recordtype,
MarcModificationTemplatesLoop => \@templates,
);
output_html_with_http_headers $input, $cookie, $template->output;