Bug 7067 [Follow-up: templates] allow patron self registration via the opac
[koha.git] / Koha / RecordProcessor.pm
1 package Koha::RecordProcessor;
2
3 # Copyright 2012 C & P Bibliography Services
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 NAME
21
22 Koha::RecordProcessor - Dispatcher class for record normalization
23
24 =head1 SYNOPSIS
25
26   use Koha::RecordProcessor;
27   my $normalizer = Koha::RecordProcessor(%params);
28   $normalizer->process($record)
29
30 =head1 DESCRIPTION
31
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:
35
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.
39
40 These methods may be overriden:
41
42 B<initialize (%params)> - initialize the filter
43
44 B<destroy ()> - destroy the filter
45
46 These methods should not be overridden unless you are very sure of what
47 you are doing:
48
49 B<new ()> - create a new filter object
50
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.
55
56 =head1 FUNCTIONS
57
58 =cut
59
60 use strict;
61 use warnings;
62 use Module::Load::Conditional qw(can_load);
63 use Module::Pluggable::Object;
64
65 use base qw(Class::Accessor);
66
67 __PACKAGE__->mk_accessors(qw( schema filters options record ));
68
69 =head2 new
70
71     my $normalizer = Koha::RecordProcessor->new(%params);
72
73 Create a new normalizer. Available parameters are:
74
75 =over 8
76
77 =item B<schema>
78
79 Which metadata schema is in use. At the moment the only supported schema
80 is 'MARC'.
81
82 =item B<filters>
83
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.
88
89 =back
90
91 =cut
92 sub new {
93     my $class = shift;
94     my $param = shift;
95
96
97     my $schema = $param->{schema} || 'MARC';
98     my $options = $param->{options} || '';
99     my @filters = ( );
100
101     foreach my $filter ($param->{filters}) {
102         next unless $filter;
103         my $filter_module = $filter =~ m/:/ ? $filter : "Koha::Filter::${schema}::${filter}";
104         if (can_load( modules => { $filter_module => undef } )) {
105             my $object = $filter_module->new();
106             $filter_module->initialize($param);
107             push @filters, $object;
108         }
109     }
110
111     my $self = $class->SUPER::new( { schema => $schema,
112                                      filters => \@filters,
113                                      options => $options });
114     bless $self, $class;
115     return $self;
116 }
117
118 =head2 bind
119
120     $normalizer->bind($record)
121
122 Bind a normalizer to a particular record.
123
124 =cut
125 sub bind {
126     my $self = shift;
127     my $record = shift;
128
129     $self->{record} = $record;
130     return;
131 }
132
133 =head2 process
134
135     my $newrecord = $normalizer->process([$record])
136
137 Run the record(s) through the normalization pipeline. If $record is
138 not specified, process the record the normalizer is bound to.
139 Note that $record may be either a scalar or an arrayref, and the
140 return value will be of the same type.
141
142 =cut
143 sub process {
144     my $self = shift;
145     my $record = shift || $self->record;
146
147     return unless defined $record;
148
149     my $newrecord = $record;
150
151     foreach my $filterobj (@{$self->filters}) {
152         next unless $filterobj;
153         $newrecord = $filterobj->filter($newrecord);
154     }
155
156     return $newrecord;
157 }
158
159 sub DESTROY {
160     my $self = shift;
161
162     foreach my $filterobj (@{$self->filters}) {
163         $filterobj->destroy();
164     }
165 }
166
167 =head2 AvailableFilters
168
169     my @available_filters = Koha::RecordProcessor::AvailableFilters([$schema]);
170
171 Get a list of available filters. Optionally specify the metadata schema.
172 At present only MARC is supported as a schema.
173
174 =cut
175 sub AvailableFilters {
176     my $schema = pop || '';
177     my $path = 'Koha::Filter';
178     $path .= "::$schema" if ($schema eq 'MARC');
179     my $finder = Module::Pluggable::Object->new(search_path => $path);
180     return $finder->plugins;
181 }
182
183 1;