From 857a75cbef139e7e10358f13b8701235f8d8421f 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 --- C4/ImportBatch.pm | 24 +++++-- Koha/Plugins.pm | 2 +- Koha/Plugins/Handler.pm | 3 +- .../prog/en/modules/admin/admin-home.tt | 7 ++ .../prog/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 08304d7..9852922 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); @@ -317,11 +318,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 @@ -330,6 +335,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; @@ -361,6 +367,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 3b31fc5..a66cf5a 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 41c242b..ba7591f 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 02bc60a..c85aa8e 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 b9b17f5..50d7517 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 2e15466..3003084 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 39d2373..6a8ca78 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 bb7a836..3271b1a 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 500e6cd..0cb4982 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; @@ -186,15 +192,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; -- 1.7.10.4