Bug 30536: Remove validation overhead
[koha.git] / Koha / REST / Plugin / Query.pm
1 package Koha::REST::Plugin::Query;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Plugin';
21 use List::MoreUtils qw( any );
22 use Scalar::Util qw( reftype );
23 use JSON qw( decode_json );
24
25 use Koha::Exceptions;
26
27 =head1 NAME
28
29 Koha::REST::Plugin::Query
30
31 =head1 API
32
33 =head2 Mojolicious::Plugin methods
34
35 =head3 register
36
37 =cut
38
39 sub register {
40     my ( $self, $app ) = @_;
41
42 =head2 Helper methods
43
44 =head3 extract_reserved_params
45
46     my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($params);
47
48 Generates the DBIC query from the query parameters.
49
50 =cut
51
52     $app->helper(
53         'extract_reserved_params' => sub {
54             my ( $c, $params ) = @_;
55
56             my $reserved_params;
57             my $filtered_params;
58             my $path_params;
59
60             my $reserved_words = _reserved_words();
61             my @query_param_names = keys %{$c->req->params->to_hash};
62
63             foreach my $param ( keys %{$params} ) {
64                 if ( grep { $param eq $_ } @{$reserved_words} ) {
65                     $reserved_params->{$param} = $params->{$param};
66                 }
67                 elsif ( grep { $param eq $_ } @query_param_names ) {
68                     $filtered_params->{$param} = $params->{$param};
69                 }
70                 else {
71                     $path_params->{$param} = $params->{$param};
72                 }
73             }
74
75             return ( $filtered_params, $reserved_params, $path_params );
76         }
77     );
78
79 =head3 dbic_merge_sorting
80
81     $attributes = $c->dbic_merge_sorting({ attributes => $attributes, params => $params });
82
83 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$attributes>.
84
85 =cut
86
87     $app->helper(
88         'dbic_merge_sorting' => sub {
89             my ( $c, $args ) = @_;
90             my $attributes = $args->{attributes};
91             my $result_set = $args->{result_set};
92
93             my @order_by_styles = (
94                 '_order_by',
95                 '_order_by[]'
96             );
97             my @order_by_params;
98
99             foreach my $order_by_style ( @order_by_styles ) {
100                 if ( defined $args->{params}->{$order_by_style} and ref($args->{params}->{$order_by_style}) eq 'ARRAY' )  {
101                     push( @order_by_params, @{$args->{params}->{$order_by_style} });
102                 }
103                 else {
104                     push @order_by_params, $args->{params}->{$order_by_style}
105                         if defined $args->{params}->{$order_by_style};
106                 }
107             }
108
109             my @THE_order_by;
110
111             foreach my $order_by_param ( @order_by_params ) {
112                 my $order_by;
113                 $order_by = [ split(/,/, $order_by_param) ]
114                     if ( !reftype($order_by_param) && index(',',$order_by_param) == -1);
115
116                 if ($order_by) {
117                     if ( reftype($order_by) and reftype($order_by) eq 'ARRAY' ) {
118                         my @order_by = map { _build_order_atom({ string => $_, result_set => $result_set }) } @{ $order_by };
119                         push( @THE_order_by, @order_by);
120                     }
121                     else {
122                         push @THE_order_by, _build_order_atom({ string => $order_by, result_set => $result_set });
123                     }
124                 }
125             }
126
127             $attributes->{order_by} = \@THE_order_by
128                 if scalar @THE_order_by > 0;
129
130             return $attributes;
131         }
132     );
133
134 =head3 dbic_merge_prefetch
135
136     $attributes = $c->dbic_merge_prefetch({ attributes => $attributes, result_set => $result_set });
137
138 Generates the DBIC prefetch attribute based on embedded relations, and merges into I<$attributes>.
139
140 =cut
141
142     $app->helper(
143         'dbic_merge_prefetch' => sub {
144             my ( $c, $args ) = @_;
145             my $attributes = $args->{attributes};
146             my $result_set = $args->{result_set};
147             my $embed = $c->stash('koha.embed');
148             return unless defined $embed;
149
150             my @prefetches;
151             foreach my $key (sort keys(%{$embed})) {
152                 my $parsed = _parse_prefetch($key, $embed, $result_set);
153                 push @prefetches, $parsed if defined $parsed;
154             }
155
156             if(scalar(@prefetches)) {
157                 $attributes->{prefetch} = \@prefetches;
158             }
159         }
160     );
161
162 =head3 _build_query_params_from_api
163
164     my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
165
166 Builds the params for searching on DBIC based on the selected matching algorithm.
167 Valid options are I<contains>, I<starts_with>, I<ends_with> and I<exact>. Default is
168 I<contains>. If other value is passed, a Koha::Exceptions::WrongParameter exception
169 is raised.
170
171 =cut
172
173     $app->helper(
174         'build_query_params' => sub {
175
176             my ( $c, $filtered_params, $reserved_params ) = @_;
177
178             my $params;
179             my $match = $reserved_params->{_match} // 'contains';
180
181             foreach my $param ( keys %{$filtered_params} ) {
182                 if ( $match eq 'contains' ) {
183                     $params->{$param} =
184                       { like => '%' . $filtered_params->{$param} . '%' };
185                 }
186                 elsif ( $match eq 'starts_with' ) {
187                     $params->{$param} = { like => $filtered_params->{$param} . '%' };
188                 }
189                 elsif ( $match eq 'ends_with' ) {
190                     $params->{$param} = { like => '%' . $filtered_params->{$param} };
191                 }
192                 elsif ( $match eq 'exact' ) {
193                     $params->{$param} = $filtered_params->{$param};
194                 }
195                 else {
196                     # We should never reach here, because the OpenAPI plugin should
197                     # prevent invalid params to be passed
198                     Koha::Exceptions::WrongParameter->throw(
199                         "Invalid value for _match param ($match)");
200                 }
201             }
202
203             return $params;
204         }
205     );
206
207 =head3 merge_q_params
208
209     $c->merge_q_params( $filtered_params, $q_params, $result_set );
210
211 Merges parameters from $q_params into $filtered_params.
212
213 =cut
214
215     $app->helper(
216         'merge_q_params' => sub {
217
218             my ( $c, $filtered_params, $q_params, $result_set ) = @_;
219
220             $q_params = decode_json($q_params) unless reftype $q_params;
221
222             my $params = _parse_dbic_query($q_params, $result_set);
223
224             return $params unless scalar(keys %{$filtered_params});
225             return {'-and' => [$params, $filtered_params ]};
226         }
227     );
228
229 =head3 stash_embed
230
231     $c->stash_embed( );
232
233 =cut
234
235     $app->helper(
236         'stash_embed' => sub {
237
238             my ( $c ) = @_;
239             my $embed_header = $c->req->headers->header('x-koha-embed');
240             if ($embed_header) {
241                 my $THE_embed = {};
242                 foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
243                     _merge_embed( _parse_embed($embed_req), $THE_embed );
244                 }
245
246                 $c->stash( 'koha.embed' => $THE_embed )
247                   if $THE_embed;
248             }
249
250             return $c;
251         }
252     );
253
254 =head3 stash_overrides
255
256     # Stash the overrides
257     $c->stash_overrides();
258     #Use it
259     my $overrides = $c->stash('koha.overrides');
260     if ( $overrides->{pickup_location} ) { ... }
261
262 This helper method parses 'x-koha-override' headers and stashes the passed overriders
263 in the for of a I<hashref> for easy use in controller methods.
264
265 FIXME: With the currently used JSON::Validator version we use, it is not possible to
266 use the validated and coerced data (it doesn't validate array-type headers) so this
267 implementation relies on manual parsing. Look at the JSON::Validator changelog for
268 reference: https://metacpan.org/changes/distribution/JSON-Validator#L14
269
270 =cut
271
272     $app->helper(
273         'stash_overrides' => sub {
274
275             my ( $c ) = @_;
276
277             my $override_header = $c->req->headers->header('x-koha-override') || q{};
278
279             my $overrides = { map { $_ => 1 } split /\s*,\s*/, $override_header };
280
281             $c->stash( 'koha.overrides' => $overrides );
282
283             return $c;
284         }
285     );
286 }
287
288 =head2 Internal methods
289
290 =head3 _reserved_words
291
292     my $reserved_words = _reserved_words();
293
294 =cut
295
296 sub _reserved_words {
297
298     my @reserved_words = qw( _match _order_by _order_by[] _page _per_page q query x-koha-query x-koha-request-id x-koha-embed);
299     return \@reserved_words;
300 }
301
302 =head3 _build_order_atom
303
304     my $order_atom = _build_order_atom( $string );
305
306 Parses I<$string> and outputs data valid for using in SQL::Abstract order_by attribute
307 according to the following rules:
308
309      string -> I<string>
310     +string -> I<{ -asc => string }>
311     -string -> I<{ -desc => string }>
312
313 =cut
314
315 sub _build_order_atom {
316     my ( $args )   = @_;
317     my $string     = $args->{string};
318     my $result_set = $args->{result_set};
319
320     my $param = $string;
321     $param =~ s/^(\+|\-|\s)//;
322     if ( $result_set ) {
323         my $model_param = _from_api_param($param, $result_set);
324         $param = $model_param if defined $model_param;
325     }
326
327     if ( $string =~ m/^\+/ or
328          $string =~ m/^\s/ ) {
329         # asc order operator present
330         return { -asc => $param };
331     }
332     elsif ( $string =~ m/^\-/ ) {
333         # desc order operator present
334         return { -desc => $param };
335     }
336     else {
337         # no order operator present
338         return $param;
339     }
340 }
341
342 =head3 _parse_embed
343
344     my $embed = _parse_embed( $string );
345
346 Parses I<$string> and outputs data valid for passing to the Kohaa::Object(s)->to_api
347 method.
348
349 =cut
350
351 sub _parse_embed {
352     my $string = shift;
353
354     my $result;
355     my ( $curr, $next ) = split /\s*\.\s*/, $string, 2;
356
357     if ( $next ) {
358         $result->{$curr} = { children => _parse_embed( $next ) };
359     }
360     else {
361         if ( $curr =~ m/^(?<relation>.*)\+count/ ) {
362             my $key = $+{relation} . "_count";
363             $result->{$key} = { is_count => 1 };
364         }
365         else {
366             $result->{$curr} = {};
367         }
368     }
369
370     return $result;
371 }
372
373 =head3 _merge_embed
374
375     _merge_embed( $parsed_embed, $global_embed );
376
377 Merges the hash referenced by I<$parsed_embed> into I<$global_embed>.
378
379 =cut
380
381 sub _merge_embed {
382     my ( $structure, $embed ) = @_;
383
384     my ($root) = keys %{ $structure };
385
386     if ( any { $root eq $_ } keys %{ $embed } ) {
387         # Recurse
388         _merge_embed( $structure->{$root}, $embed->{$root} );
389     }
390     else {
391         # Embed
392         $embed->{$root} = $structure->{$root};
393     }
394 }
395
396 sub _parse_prefetch {
397     my ( $key, $embed, $result_set) = @_;
398
399     my $pref_key = $key;
400     $pref_key =~ s/_count$// if $embed->{$key}->{is_count};
401     return unless exists $result_set->prefetch_whitelist->{$pref_key};
402
403     my $ko_class = $result_set->prefetch_whitelist->{$pref_key};
404     return $pref_key unless defined $embed->{$key}->{children} && defined $ko_class;
405
406     my @prefetches;
407     foreach my $child (sort keys(%{$embed->{$key}->{children}})) {
408         my $parsed = _parse_prefetch($child, $embed->{$key}->{children}, $ko_class->new);
409         push @prefetches, $parsed if defined $parsed;
410     }
411
412     return $pref_key unless scalar(@prefetches);
413
414     return {$pref_key => $prefetches[0]} if scalar(@prefetches) eq 1;
415
416     return {$pref_key => \@prefetches};
417 }
418
419 sub _from_api_param {
420     my ($key, $result_set) = @_;
421
422     if($key =~ /\./) {
423
424         my ($curr, $next) = split /\s*\.\s*/, $key, 2;
425
426         return $curr.'.'._from_api_param($next, $result_set) if $curr eq 'me';
427
428         my $ko_class = $result_set->prefetch_whitelist->{$curr};
429
430         Koha::Exceptions::BadParameter->throw("Cannot find Koha::Object class for $curr")
431             unless defined $ko_class;
432
433         $result_set = $ko_class->new;
434
435         if ($next =~ /\./) {
436             return _from_api_param($next, $result_set);
437         } else {
438             return $curr.'.'.($result_set->from_api_mapping && defined $result_set->from_api_mapping->{$next} ? $result_set->from_api_mapping->{$next}:$next);
439         }
440     } else {
441         return defined $result_set->from_api_mapping->{$key} ? $result_set->from_api_mapping->{$key} : $key;
442     }
443 }
444
445 sub _parse_dbic_query {
446     my ($q_params, $result_set) = @_;
447
448     if(reftype($q_params) && reftype($q_params) eq 'HASH') {
449         my $parsed_hash;
450         foreach my $key (keys %{$q_params}) {
451             if($key =~ /-?(not_?)?bool/i ) {
452                 $parsed_hash->{$key} = _from_api_param($q_params->{$key}, $result_set);
453                 next;
454             }
455             my $k = _from_api_param($key, $result_set);
456             $parsed_hash->{$k} = _parse_dbic_query($q_params->{$key}, $result_set);
457         }
458         return $parsed_hash;
459     } elsif (reftype($q_params) && reftype($q_params) eq 'ARRAY') {
460         my @mapped = map{ _parse_dbic_query($_, $result_set) } @$q_params;
461         return \@mapped;
462     } else {
463         return $q_params;
464     }
465
466 }
467
468 1;