From ca167b32b49979b7883f7874461ba66a1ca74f3e Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 12 Jun 2014 12:18:41 -0400 Subject: [PATCH] Bug 12412: Add ability for plugins to convert arbitrary files to MARC from record staging tool Many libraries would like to be able to import various types of files as MARC records ( citations, csv files, etc ). We can add a new function to the plugins system to allow that kind of behavior at a very custom level. Test Plan: 1) Ensure you have plugins enabled and configured correctly 2) Installed the attached version 2.00 of the Kitchen Sink plugin 3) Download the attached text file 4) Browse to "Stage MARC records for import" 5) Select the downloaded text file for staging 6) After uploading, you should see a new area "Transform file to MARC:", select "Example Kitchen-Sink Plugin" from the pulldown menu 7) Click 'Stage for import" 8) Click 'Manage staged records" 9) You should now see two new MARC records! Signed-off-by: Aleisha Signed-off-by: Katrin Fischer Works as described - interesting new feature. Signed-off-by: Tomas Cohen Arazi --- C4/ImportBatch.pm | 24 +++++-- Koha/Plugins.pm | 2 +- Koha/Plugins/Handler.pm | 3 +- .../prog/en/modules/admin/admin-home.tt | 7 ++ .../en/modules/tools/stage-marc-import.tt | 18 +++++ misc/stage_file.pl | 3 +- t/Koha/Plugin/Test.pm | 5 ++ t/db_dependent/Plugins.t | 3 +- tools/stage-marc-import.pl | 68 +++++++++++-------- 9 files changed, 94 insertions(+), 39 deletions(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index 3293b185ec..29a1f94141 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -27,6 +27,7 @@ use C4::Items; use C4::Charset; use C4::AuthoritiesMarc; use C4::MarcModificationTemplates; +use Koha::Plugins::Handler; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); @@ -347,11 +348,15 @@ sub ModAuthInBatch { =head2 BatchStageMarcRecords - ($batch_id, $num_records, $num_items, @invalid_records) = - BatchStageMarcRecords($encoding, $marc_records, $file_name, $marc_modification_template, - $comments, $branch_code, $parse_items, - $leave_as_staging, - $progress_interval, $progress_callback); +( $batch_id, $num_records, $num_items, @invalid_records ) = + BatchStageMarcRecords( + $encoding, $marc_records, + $file_name, $to_marc_plugin, + $marc_modification_template, $comments, + $branch_code, $parse_items, + $leave_as_staging, $progress_interval, + $progress_callback + ); =cut @@ -360,6 +365,7 @@ sub BatchStageMarcRecords { my $encoding = shift; my $marc_records = shift; my $file_name = shift; + my $to_marc_plugin = shift; my $marc_modification_template = shift; my $comments = shift; my $branch_code = shift; @@ -391,6 +397,14 @@ sub BatchStageMarcRecords { SetImportBatchItemAction($batch_id, 'ignore'); } + $marc_records = Koha::Plugins::Handler->run( + { + class => $to_marc_plugin, + method => 'to_marc', + params => { data => $marc_records } + } + ) if $to_marc_plugin; + my $marc_type = C4::Context->preference('marcflavour'); $marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth'); my @invalid_records = (); diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index 6574d51626..9e2d4eb609 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -51,7 +51,7 @@ This will return a list of all the available plugins of the passed type. Usage: my @plugins = C4::Plugins::GetPlugins( $method ); -At the moment, the available types are 'report' and 'tool'. +At the moment, the available types are 'report', 'tool' and 'to_marc'. =cut sub GetPlugins { diff --git a/Koha/Plugins/Handler.pm b/Koha/Plugins/Handler.pm index 993aa14196..187560d514 100644 --- a/Koha/Plugins/Handler.pm +++ b/Koha/Plugins/Handler.pm @@ -56,11 +56,12 @@ sub run { my $plugin_class = $args->{'class'}; my $plugin_method = $args->{'method'}; my $cgi = $args->{'cgi'}; + my $params = $args->{'params'}; if ( can_load( modules => { $plugin_class => undef } ) ) { my $plugin = $plugin_class->new( { cgi => $cgi, enable_plugins => $args->{'enable_plugins'} } ); if ( $plugin->can($plugin_method) ) { - return $plugin->$plugin_method(); + return $plugin->$plugin_method( $params ); } else { warn "Plugin does not have method $plugin_method"; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt index 02bc60a40e..c85aa8ecf0 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt @@ -57,6 +57,13 @@
Cities and towns
Define cities and towns that your patrons live in.
+[% IF CAN_user_plugins %] +

Plugins

+
+
Manage plugins
+
View, manage, configure and run plugins.
+
+[% END %]

Catalog

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt index b9b17f58e4..50d7517cec 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt @@ -117,6 +117,24 @@ function CheckForm(f) { + + [% IF plugins %] +
+ Transform file to MARC: +
    +
  1. + + +
  2. +
+
+ [% END %] + [% IF MarcModificationTemplatesLoop %]
Use MARC Modification Template: diff --git a/misc/stage_file.pl b/misc/stage_file.pl index 2e15466758..3003084989 100755 --- a/misc/stage_file.pl +++ b/misc/stage_file.pl @@ -100,8 +100,9 @@ sub process_batch { close IN; print "... staging MARC records -- please wait\n"; + #FIXME: We should really allow the use of marc modification frameworks and to_marc plugins here if possible my ($batch_id, $num_valid_records, $num_items, @import_errors) = - BatchStageMarcRecords($record_type, $encoding, $marc_records, $input_file, undef, $batch_comment, '', $add_items, 0, + BatchStageMarcRecords($record_type, $encoding, $marc_records, $input_file, undef, undef, $batch_comment, '', $add_items, 0, 100, \&print_progress_and_commit); print "... finished staging MARC records\n"; diff --git a/t/Koha/Plugin/Test.pm b/t/Koha/Plugin/Test.pm index 39d237390c..6a8ca7862f 100644 --- a/t/Koha/Plugin/Test.pm +++ b/t/Koha/Plugin/Test.pm @@ -37,6 +37,11 @@ sub tool { return "Koha::Plugin::Test::tool"; } +sub to_marc { + my ( $self, $args ) = @_; + return "Koha::Plugin::Test::to_marc"; +} + sub configure { my ( $self, $args ) = @_; return "Koha::Plugin::Test::configure";; diff --git a/t/db_dependent/Plugins.t b/t/db_dependent/Plugins.t index bb7a836dd8..3271b1a0e8 100755 --- a/t/db_dependent/Plugins.t +++ b/t/db_dependent/Plugins.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 20; +use Test::More tests => 21; use File::Basename; use FindBin qw($Bin); use Archive::Extract; @@ -29,6 +29,7 @@ isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' ); ok( $plugin->can('report'), 'Test plugin can report' ); ok( $plugin->can('tool'), 'Test plugin can tool' ); +ok( $plugin->can('to_marc'), 'Test plugin can to_marc' ); ok( $plugin->can('configure'), 'Test plugin can configure' ); ok( $plugin->can('install'), 'Test plugin can install' ); ok( $plugin->can('uninstall'), 'Test plugin can install' ); diff --git a/tools/stage-marc-import.pl b/tools/stage-marc-import.pl index af1cdc3073..3e9b0ee633 100755 --- a/tools/stage-marc-import.pl +++ b/tools/stage-marc-import.pl @@ -42,33 +42,39 @@ use C4::Matcher; use C4::UploadedFile; use C4::BackgroundJob; use C4::MarcModificationTemplates; +use Koha::Plugins; my $input = new CGI; -my $fileID=$input->param('uploadedfileid'); -my $runinbackground = $input->param('runinbackground'); -my $completedJobID = $input->param('completedJobID'); -my $matcher_id = $input->param('matcher'); -my $overlay_action = $input->param('overlay_action'); -my $nomatch_action = $input->param('nomatch_action'); -my $parse_items = $input->param('parse_items'); -my $item_action = $input->param('item_action'); -my $comments = $input->param('comments'); -my $record_type = $input->param('record_type'); -my $encoding = $input->param('encoding'); +my $fileID = $input->param('uploadedfileid'); +my $runinbackground = $input->param('runinbackground'); +my $completedJobID = $input->param('completedJobID'); +my $matcher_id = $input->param('matcher'); +my $overlay_action = $input->param('overlay_action'); +my $nomatch_action = $input->param('nomatch_action'); +my $parse_items = $input->param('parse_items'); +my $item_action = $input->param('item_action'); +my $comments = $input->param('comments'); +my $record_type = $input->param('record_type'); +my $encoding = $input->param('encoding'); +my $to_marc_plugin = $input->param('to_marc_plugin'); my $marc_modification_template = $input->param('marc_modification_template_id'); -my ($template, $loggedinuser, $cookie) - = get_template_and_user({template_name => "tools/stage-marc-import.tt", - query => $input, - type => "intranet", - authnotrequired => 0, - flagsrequired => {tools => 'stage_marc_import'}, - debug => 1, - }); +my ( $template, $loggedinuser, $cookie ) = get_template_and_user( + { + template_name => "tools/stage-marc-import.tt", + query => $input, + type => "intranet", + authnotrequired => 0, + flagsrequired => { tools => 'stage_marc_import' }, + debug => 1, + } +); -$template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'}, - uploadmarc => $fileID); +$template->param( + SCRIPT_NAME => $ENV{'SCRIPT_NAME'}, + uploadmarc => $fileID +); my %cookies = parse CGI::Cookie($cookie); my $sessionID = $cookies{'CGISESSID'}->value; @@ -128,12 +134,12 @@ if ($completedJobID) { # FIXME branch code my ( $batch_id, $num_valid, $num_items, @import_errors ) = BatchStageMarcRecords( - $record_type, $encoding, - $marcrecord, $filename, - $marc_modification_template, $comments, - '', $parse_items, - 0, 50, - staging_progress_callback( $job, $dbh ) + $record_type, $encoding, + $marcrecord, $filename, + $to_marc_plugin, $marc_modification_template, + $comments, '', + $parse_items, 0, + 50, staging_progress_callback( $job, $dbh ) ); my $num_with_matches = 0; @@ -188,15 +194,17 @@ if ($completedJobID) { } else { # initial form - if (C4::Context->preference("marcflavour") eq "UNIMARC") { - $template->param("UNIMARC" => 1); + if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) { + $template->param( "UNIMARC" => 1 ); } my @matchers = C4::Matcher::GetMatcherList(); - $template->param(available_matchers => \@matchers); + $template->param( available_matchers => \@matchers ); my @templates = GetModificationTemplates(); $template->param( MarcModificationTemplatesLoop => \@templates ); + my @plugins = Koha::Plugins->new()->GetPlugins('to_marc'); + $template->param( plugins => \@plugins ); } output_html_with_http_headers $input, $cookie, $template->output; -- 2.39.5