Browse Source

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 <aleishaamohia@hotmail.com>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Works as described - interesting new feature.
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
3.20.x
Kyle Hall 10 years ago
committed by Tomas Cohen Arazi
parent
commit
ca167b32b4
  1. 24
      C4/ImportBatch.pm
  2. 2
      Koha/Plugins.pm
  3. 3
      Koha/Plugins/Handler.pm
  4. 7
      koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt
  5. 18
      koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt
  6. 3
      misc/stage_file.pl
  7. 5
      t/Koha/Plugin/Test.pm
  8. 3
      t/db_dependent/Plugins.t
  9. 68
      tools/stage-marc-import.pl

24
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 = ();

2
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 {

3
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";
}

7
koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt

@ -57,6 +57,13 @@
<dt><a href="/cgi-bin/koha/admin/cities.pl">Cities and towns</a></dt>
<dd>Define cities and towns that your patrons live in.</dd>
</dl>
[% IF CAN_user_plugins %]
<h3>Plugins</h3>
<dl>
<dt><a href="/cgi-bin/koha/plugins/plugins-home.pl">Manage plugins</a></dt>
<dd>View, manage, configure and run plugins.</dd>
</dl>
[% END %]
</div>
<div class="yui-u">
<h3>Catalog</h3>

18
koha-tmpl/intranet-tmpl/prog/en/modules/tools/stage-marc-import.tt

@ -117,6 +117,24 @@ function CheckForm(f) {
<select name="encoding" id="encoding"><option value="utf8" selected="selected">UTF-8 (Default)</option><option value="MARC-8">MARC 8</option><option value="ISO_5426">ISO 5426</option><option value="ISO_6937">ISO 6937</option><option value=ISO_8859-1">ISO 8859-1</option><option value="EUC-KR">EUC-KR</option></select>
</li>
</ol></fieldset>
[% IF plugins %]
<fieldset class="rows">
<legend>Transform file to MARC:</legend>
<ol>
<li>
<label for="comments">Convert file to MARC using the following plugin: </label>
<select name="to_marc_plugin" id="to_marc_plugin">
<option value="">Do not use.</option>
[% FOREACH p IN plugins %]
<option value="[% p.metadata.class %]">[% p.metadata.name %]</option>
[% END %]
</select>
</li>
</ol>
</fieldset>
[% END %]
[% IF MarcModificationTemplatesLoop %]
<fieldset class="rows">
<legend>Use MARC Modification Template:</legend>

3
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";

5
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";;

3
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' );

68
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;

Loading…
Cancel
Save