1 package Koha::REST::Plugin::Query;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Mojo::Base 'Mojolicious::Plugin';
21 use List::MoreUtils qw(any);
22 use Scalar::Util qw(reftype);
28 Koha::REST::Plugin::Query
32 =head2 Mojolicious::Plugin methods
39 my ( $self, $app ) = @_;
43 =head3 extract_reserved_params
45 my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($params);
47 Generates the DBIC query from the query parameters.
52 'extract_reserved_params' => sub {
53 my ( $c, $params ) = @_;
59 my $reserved_words = _reserved_words();
60 my @query_param_names = keys %{$c->req->params->to_hash};
62 foreach my $param ( keys %{$params} ) {
63 if ( grep { $param eq $_ } @{$reserved_words} ) {
64 $reserved_params->{$param} = $params->{$param};
66 elsif ( grep { $param eq $_ } @query_param_names ) {
67 $filtered_params->{$param} = $params->{$param};
70 $path_params->{$param} = $params->{$param};
74 return ( $filtered_params, $reserved_params, $path_params );
78 =head3 dbic_merge_sorting
80 $attributes = $c->dbic_merge_sorting({ attributes => $attributes, params => $params });
82 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$attributes>.
87 'dbic_merge_sorting' => sub {
88 my ( $c, $args ) = @_;
89 my $attributes = $args->{attributes};
90 my $result_set = $args->{result_set};
92 if ( defined $args->{params}->{_order_by} ) {
93 my $order_by = $args->{params}->{_order_by};
94 if ( reftype($order_by) and reftype($order_by) eq 'ARRAY' ) {
95 my @order_by = map { _build_order_atom({ string => $_, result_set => $result_set }) }
96 @{ $args->{params}->{_order_by} };
97 $attributes->{order_by} = \@order_by;
100 $attributes->{order_by} = _build_order_atom({ string => $order_by, result_set => $result_set });
108 =head3 dbic_merge_prefetch
110 $attributes = $c->dbic_merge_prefetch({ attributes => $attributes, result_set => $result_set });
112 Generates the DBIC prefetch attribute based on embedded relations, and merges into I<$attributes>.
117 'dbic_merge_prefetch' => sub {
118 my ( $c, $args ) = @_;
119 my $attributes = $args->{attributes};
120 my $result_set = $args->{result_set};
121 my $embed = $c->stash('koha.embed');
123 return unless defined $embed;
126 foreach my $key (keys %{$embed}) {
127 my $parsed = _parse_prefetch($key, $embed, $result_set);
128 push @prefetches, $parsed if defined $parsed;
131 if(scalar(@prefetches)) {
132 $attributes->{prefetch} = \@prefetches;
137 =head3 _build_query_params_from_api
139 my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
141 Builds the params for searching on DBIC based on the selected matching algorithm.
142 Valid options are I<contains>, I<starts_with>, I<ends_with> and I<exact>. Default is
143 I<contains>. If other value is passed, a Koha::Exceptions::WrongParameter exception
149 'build_query_params' => sub {
151 my ( $c, $filtered_params, $reserved_params ) = @_;
154 my $match = $reserved_params->{_match} // 'contains';
156 foreach my $param ( keys %{$filtered_params} ) {
157 if ( $match eq 'contains' ) {
159 { like => '%' . $filtered_params->{$param} . '%' };
161 elsif ( $match eq 'starts_with' ) {
162 $params->{$param} = { like => $filtered_params->{$param} . '%' };
164 elsif ( $match eq 'ends_with' ) {
165 $params->{$param} = { like => '%' . $filtered_params->{$param} };
167 elsif ( $match eq 'exact' ) {
168 $params->{$param} = $filtered_params->{$param};
171 # We should never reach here, because the OpenAPI plugin should
172 # prevent invalid params to be passed
173 Koha::Exceptions::WrongParameter->throw(
174 "Invalid value for _match param ($match)");
184 $c->stash_embed( $c->match->endpoint->pattern->defaults->{'openapi.op_spec'} );
189 'stash_embed' => sub {
191 my ( $c, $args ) = @_;
193 my $spec = $args->{spec} // {};
195 my $embed_spec = $spec->{'x-koha-embed'};
196 my $embed_header = $c->req->headers->header('x-koha-embed');
198 Koha::Exceptions::BadParameter->throw("Embedding objects is not allowed on this endpoint.")
199 if $embed_header and !defined $embed_spec;
201 if ( $embed_header ) {
203 foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
204 my $matches = grep {lc $_ eq lc $embed_req} @{ $embed_spec };
206 Koha::Exceptions::BadParameter->throw(
207 error => 'Embeding '.$embed_req. ' is not authorised. Check your x-koha-embed headers or remove it.'
210 _merge_embed( _parse_embed($embed_req), $THE_embed);
213 $c->stash( 'koha.embed' => $THE_embed )
222 =head2 Internal methods
224 =head3 _reserved_words
226 my $reserved_words = _reserved_words();
230 sub _reserved_words {
232 my @reserved_words = qw( _match _order_by _page _per_page );
233 return \@reserved_words;
236 =head3 _build_order_atom
238 my $order_atom = _build_order_atom( $string );
240 Parses I<$string> and outputs data valid for using in SQL::Abstract order_by attribute
241 according to the following rules:
244 +string -> I<{ -asc => string }>
245 -string -> I<{ -desc => string }>
249 sub _build_order_atom {
251 my $string = $args->{string};
252 my $result_set = $args->{result_set};
255 $param =~ s/^(\+|\-|\s)//;
257 my $model_param = $result_set->from_api_mapping->{$param};
258 $param = $model_param if defined $model_param;
261 if ( $string =~ m/^\+/ or
262 $string =~ m/^\s/ ) {
263 # asc order operator present
264 return { -asc => $param };
266 elsif ( $string =~ m/^\-/ ) {
267 # desc order operator present
268 return { -desc => $param };
271 # no order operator present
278 my $embed = _parse_embed( $string );
280 Parses I<$string> and outputs data valid for passing to the Kohaa::Object(s)->to_api
289 my ( $curr, $next ) = split /\s*\.\s*/, $string, 2;
292 $result->{$curr} = { children => _parse_embed( $next ) };
295 if ( $curr =~ m/^(?<relation>.*)\+count/ ) {
296 my $key = $+{relation} . "_count";
297 $result->{$key} = { is_count => 1 };
300 $result->{$curr} = {};
309 _merge_embed( $parsed_embed, $global_embed );
311 Merges the hash referenced by I<$parsed_embed> into I<$global_embed>.
316 my ( $structure, $embed ) = @_;
318 my ($root) = keys %{ $structure };
320 if ( any { $root eq $_ } keys %{ $embed } ) {
322 _merge_embed( $structure->{$root}, $embed->{$root} );
326 $embed->{$root} = $structure->{$root};
330 sub _parse_prefetch {
331 my ( $key, $embed, $result_set) = @_;
333 return unless exists $result_set->prefetch_whitelist->{$key};
335 my $ko_class = $result_set->prefetch_whitelist->{$key};
336 return $key unless defined $embed->{$key}->{children} && defined $ko_class;
339 foreach my $child (keys %{$embed->{$key}->{children}}) {
340 my $parsed = _parse_prefetch($child, $embed->{$key}->{children}, $ko_class->new);
341 $prefetch->{$key} = $parsed if defined $parsed;
344 return unless scalar(keys %{$prefetch});