Bug 15072: (followup) We use 'New' instead of 'Create' on the UI
[koha.git] / Koha / RecordProcessor.pm
blobcbbd7262f21792edc56a135cf27144ca706ddfae
1 package Koha::RecordProcessor;
3 # Copyright 2012 C & P Bibliography Services
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 =head1 NAME
22 Koha::RecordProcessor - Dispatcher class for record normalization
24 =head1 SYNOPSIS
26 use Koha::RecordProcessor;
27 my $normalizer = Koha::RecordProcessor(%params);
28 $normalizer->process($record)
30 =head1 DESCRIPTION
32 Dispatcher class for record normalization. RecordProcessors must
33 extend Koha::RecordProcessor::Base, be in the Koha::Filter namespace,
34 and provide the following methods:
36 B<filter ($record)> - apply the filter and return the result. $record
37 may be either a scalar or an arrayref, and the return result will be
38 the same type.
40 These methods may be overriden:
42 B<initialize (%params)> - initialize the filter
44 B<destroy ()> - destroy the filter
46 These methods should not be overridden unless you are very sure of what
47 you are doing:
49 B<new ()> - create a new filter object
51 Note that the RecordProcessor will not clone the record that is
52 passed in. If you do not want to change the original MARC::Record
53 object (or whatever type of object you are passing in), you must
54 clone it I<prior> to passing it off to the RecordProcessor.
56 =head1 FUNCTIONS
58 =cut
60 use Modern::Perl;
62 use Module::Load::Conditional qw(can_load);
63 use Module::Pluggable::Object;
65 use base qw(Class::Accessor);
67 __PACKAGE__->mk_accessors(qw( schema filters options record ));
69 =head2 new
71 my $normalizer = Koha::RecordProcessor->new(%params);
73 Create a new normalizer. Available parameters are:
75 =over 8
77 =item B<schema>
79 Which metadata schema is in use. At the moment the only supported schema
80 is 'MARC'.
82 =item B<filters>
84 What filter(s) to use. This must be an arrayref to a list of filters. Filters
85 can be specified either with a complete class path, or, if they are in the
86 Koha::Filter::${schema} namespace, as only the filter name, and
87 "Koha::Filter::${schema}" will be prepended to it before the filter is loaded.
89 =back
91 =cut
92 sub new {
94 my $class = shift;
95 my $param = shift;
98 my $schema = $param->{schema} || 'MARC';
99 my $options = $param->{options} || '';
101 my $req_filters = ( ref($param->{filters}) ne 'ARRAY' )
102 ? [ $param->{filters} ]
103 : $param->{filters};
104 my @filters = ( );
106 foreach my $filter (@{ $req_filters }) {
107 next unless $filter;
108 my $filter_module = $filter =~ m/:/ ? $filter : "Koha::Filter::${schema}::${filter}";
109 if (can_load( modules => { $filter_module => undef } )) {
110 my $object = $filter_module->new();
111 $filter_module->initialize($param);
112 push @filters, $object;
116 my $self = $class->SUPER::new( { schema => $schema,
117 filters => \@filters,
118 options => $options });
119 bless $self, $class;
120 return $self;
123 =head2 bind
125 $normalizer->bind($record)
127 Bind a normalizer to a particular record.
129 =cut
130 sub bind {
131 my $self = shift;
132 my $record = shift;
134 $self->{record} = $record;
135 return;
138 =head2 process
140 my $newrecord = $normalizer->process([$record])
142 Run the record(s) through the normalization pipeline. If $record is
143 not specified, process the record the normalizer is bound to.
144 Note that $record may be either a scalar or an arrayref, and the
145 return value will be of the same type.
147 =cut
148 sub process {
149 my $self = shift;
150 my $record = shift || $self->record;
152 return unless defined $record;
154 my $newrecord = $record;
156 foreach my $filterobj (@{$self->filters}) {
157 next unless $filterobj;
158 $newrecord = $filterobj->filter($newrecord);
161 return $newrecord;
164 sub DESTROY {
165 my $self = shift;
167 foreach my $filterobj (@{$self->filters}) {
168 $filterobj->destroy();
172 =head2 AvailableFilters
174 my @available_filters = Koha::RecordProcessor::AvailableFilters([$schema]);
176 Get a list of available filters. Optionally specify the metadata schema.
177 At present only MARC is supported as a schema.
179 =cut
180 sub AvailableFilters {
181 my $schema = pop || '';
182 my $path = 'Koha::Filter';
183 $path .= "::$schema" if ($schema eq 'MARC');
184 my $finder = Module::Pluggable::Object->new(search_path => $path);
185 return $finder->plugins;