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 ) = @_;
58 my $reserved_words = _reserved_words();
60 foreach my $param ( keys %{$params} ) {
61 if ( grep { $param eq $_ } @{$reserved_words} ) {
62 $reserved_params->{$param} = $params->{$param};
65 $filtered_params->{$param} = $params->{$param};
69 return ( $filtered_params, $reserved_params );
73 =head3 dbic_merge_sorting
75 $attributes = $c->dbic_merge_sorting({ attributes => $attributes, params => $params });
77 Generates the DBIC order_by attributes based on I<$params>, and merges into I<$attributes>.
82 'dbic_merge_sorting' => sub {
83 my ( $c, $args ) = @_;
84 my $attributes = $args->{attributes};
85 my $result_set = $args->{result_set};
87 if ( defined $args->{params}->{_order_by} ) {
88 my $order_by = $args->{params}->{_order_by};
89 if ( reftype($order_by) and reftype($order_by) eq 'ARRAY' ) {
90 my @order_by = map { _build_order_atom({ string => $_, result_set => $result_set }) }
91 @{ $args->{params}->{_order_by} };
92 $attributes->{order_by} = \@order_by;
95 $attributes->{order_by} = _build_order_atom({ string => $order_by, result_set => $result_set });
103 =head3 _build_query_params_from_api
105 my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
107 Builds the params for searching on DBIC based on the selected matching algorithm.
108 Valid options are I<contains>, I<starts_with>, I<ends_with> and I<exact>. Default is
109 I<contains>. If other value is passed, a Koha::Exceptions::WrongParameter exception
115 'build_query_params' => sub {
117 my ( $c, $filtered_params, $reserved_params ) = @_;
120 my $match = $reserved_params->{_match} // 'contains';
122 foreach my $param ( keys %{$filtered_params} ) {
123 if ( $match eq 'contains' ) {
125 { like => '%' . $filtered_params->{$param} . '%' };
127 elsif ( $match eq 'starts_with' ) {
128 $params->{$param} = { like => $filtered_params->{$param} . '%' };
130 elsif ( $match eq 'ends_with' ) {
131 $params->{$param} = { like => '%' . $filtered_params->{$param} };
133 elsif ( $match eq 'exact' ) {
134 $params->{$param} = $filtered_params->{$param};
137 # We should never reach here, because the OpenAPI plugin should
138 # prevent invalid params to be passed
139 Koha::Exceptions::WrongParameter->throw(
140 "Invalid value for _match param ($match)");
150 $c->stash_embed( $c->match->endpoint->pattern->defaults->{'openapi.op_spec'} );
155 'stash_embed' => sub {
157 my ( $c, $args ) = @_;
159 my $spec = $args->{spec} // {};
161 my $embed_spec = $spec->{'x-koha-embed'};
162 my $embed_header = $c->req->headers->header('x-koha-embed');
164 Koha::Exceptions::BadParameter->throw("Embedding objects is not allowed on this endpoint.")
165 if $embed_header and !defined $embed_spec;
167 if ( $embed_header ) {
169 foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
170 my $matches = grep {lc $_ eq lc $embed_req} @{ $embed_spec };
172 Koha::Exceptions::BadParameter->throw(
173 error => 'Embeding '.$embed_req. ' is not authorised. Check your x-koha-embed headers or remove it.'
176 _merge_embed( _parse_embed($embed_req), $THE_embed);
179 $c->stash( 'koha.embed' => $THE_embed )
188 =head2 Internal methods
190 =head3 _reserved_words
192 my $reserved_words = _reserved_words();
196 sub _reserved_words {
198 my @reserved_words = qw( _match _order_by _page _per_page );
199 return \@reserved_words;
202 =head3 _build_order_atom
204 my $order_atom = _build_order_atom( $string );
206 Parses I<$string> and outputs data valid for using in SQL::Abstract order_by attribute
207 according to the following rules:
210 +string -> I<{ -asc => string }>
211 -string -> I<{ -desc => string }>
215 sub _build_order_atom {
217 my $string = $args->{string};
218 my $result_set = $args->{result_set};
221 $param =~ s/^(\+|\-|\s)//;
223 $param = (keys %{$result_set->attributes_from_api({ $param => 1 })})[0];
226 if ( $string =~ m/^\+/ or
227 $string =~ m/^\s/ ) {
228 # asc order operator present
229 return { -asc => $param };
231 elsif ( $string =~ m/^\-/ ) {
232 # desc order operator present
233 return { -desc => $param };
236 # no order operator present
243 my $embed = _parse_embed( $string );
245 Parses I<$string> and outputs data valid for passing to the Kohaa::Object(s)->to_api
254 my ( $curr, $next ) = split /\s*\.\s*/, $string, 2;
257 $result->{$curr} = { children => _parse_embed( $next ) };
260 $result->{$curr} = {};
268 _merge_embed( $parsed_embed, $global_embed );
270 Merges the hash referenced by I<$parsed_embed> into I<$global_embed>.
275 my ( $structure, $embed ) = @_;
277 my ($root) = keys %{ $structure };
279 if ( any { $root eq $_ } keys %{ $embed } ) {
281 _merge_embed( $structure->{$root}, $embed->{$root} );
285 $embed->{$root} = $structure->{$root};