Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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
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.
11 #
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.
16 #
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>.
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 Modern::Perl;
61
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
94     my $class = shift;
95     my $param = shift;
96
97
98     my $schema  = $param->{schema}  || 'MARC';
99     my $options = $param->{options} || '';
100
101     my $req_filters = ( ref($param->{filters}) ne 'ARRAY' )
102                         ? [ $param->{filters} ]
103                         :   $param->{filters};
104     my @filters = ( );
105
106     foreach my $filter_name (@{ $req_filters }) {
107         next unless $filter_name;
108         # Fully qualify the module name.
109         my $filter_module = $filter_name =~ m/:/ ? $filter_name : "Koha::Filter::${schema}::${filter_name}";
110         if (can_load( modules => { $filter_module => undef } )) {
111             my $filter = $filter_module->new();
112             $filter->initialize($param);
113             push @filters, $filter;
114         }
115     }
116
117     my $self = $class->SUPER::new( { schema => $schema,
118                                      filters => \@filters,
119                                      options => $options });
120     bless $self, $class;
121     return $self;
122 }
123
124 =head3 options
125
126     $processor->options( $new_options );
127
128 Overloaded accessor, that spreads the new options to the filter objects when set
129
130 =cut
131
132 sub options {
133     my ( $self, $options ) = @_;
134
135     if ( $options ) {  # Set
136         foreach my $filter ( @{$self->filters} ) {
137             $filter->params->{options} = $options;
138         }
139
140         $self->{options} = $options;
141         return $self;
142     }
143
144     return $self->{options};
145 }
146
147 =head2 bind
148
149     $normalizer->bind($record)
150
151 Bind a normalizer to a particular record.
152
153 =cut
154 sub bind {
155     my $self = shift;
156     my $record = shift;
157
158     $self->{record} = $record;
159     return;
160 }
161
162 =head2 process
163
164     my $newrecord = $normalizer->process([$record])
165
166 Run the record(s) through the normalization pipeline. If $record is
167 not specified, process the record the normalizer is bound to.
168 Note that $record may be either a scalar or an arrayref, and the
169 return value will be of the same type.
170
171 =cut
172 sub process {
173     my $self = shift;
174     my $record = shift || $self->record;
175
176     return unless defined $record;
177
178     foreach my $filterobj (@{$self->filters}) {
179         next unless $filterobj;
180         $filterobj->filter($record);
181     }
182
183     return $record;
184 }
185
186 sub DESTROY {
187     my $self = shift;
188
189     foreach my $filterobj (@{$self->filters}) {
190         $filterobj->destroy();
191     }
192 }
193
194 =head2 AvailableFilters
195
196     my @available_filters = Koha::RecordProcessor::AvailableFilters([$schema]);
197
198 Get a list of available filters. Optionally specify the metadata schema.
199 At present only MARC is supported as a schema.
200
201 =cut
202 sub AvailableFilters {
203     my $schema = pop || '';
204     my $path = 'Koha::Filter';
205     $path .= "::$schema" if ($schema eq 'MARC');
206     my $finder = Module::Pluggable::Object->new(search_path => $path);
207     return $finder->plugins;
208 }
209
210 1;