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 _build_query_params_from_api
110 my $params = _build_query_params_from_api( $filtered_params, $reserved_params );
112 Builds the params for searching on DBIC based on the selected matching algorithm.
113 Valid options are I<contains>, I<starts_with>, I<ends_with> and I<exact>. Default is
114 I<contains>. If other value is passed, a Koha::Exceptions::WrongParameter exception
120 'build_query_params' => sub {
122 my ( $c, $filtered_params, $reserved_params ) = @_;
125 my $match = $reserved_params->{_match} // 'contains';
127 foreach my $param ( keys %{$filtered_params} ) {
128 if ( $match eq 'contains' ) {
130 { like => '%' . $filtered_params->{$param} . '%' };
132 elsif ( $match eq 'starts_with' ) {
133 $params->{$param} = { like => $filtered_params->{$param} . '%' };
135 elsif ( $match eq 'ends_with' ) {
136 $params->{$param} = { like => '%' . $filtered_params->{$param} };
138 elsif ( $match eq 'exact' ) {
139 $params->{$param} = $filtered_params->{$param};
142 # We should never reach here, because the OpenAPI plugin should
143 # prevent invalid params to be passed
144 Koha::Exceptions::WrongParameter->throw(
145 "Invalid value for _match param ($match)");
155 $c->stash_embed( $c->match->endpoint->pattern->defaults->{'openapi.op_spec'} );
160 'stash_embed' => sub {
162 my ( $c, $args ) = @_;
164 my $spec = $args->{spec} // {};
166 my $embed_spec = $spec->{'x-koha-embed'};
167 my $embed_header = $c->req->headers->header('x-koha-embed');
169 Koha::Exceptions::BadParameter->throw("Embedding objects is not allowed on this endpoint.")
170 if $embed_header and !defined $embed_spec;
172 if ( $embed_header ) {
174 foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
175 my $matches = grep {lc $_ eq lc $embed_req} @{ $embed_spec };
177 Koha::Exceptions::BadParameter->throw(
178 error => 'Embeding '.$embed_req. ' is not authorised. Check your x-koha-embed headers or remove it.'
181 _merge_embed( _parse_embed($embed_req), $THE_embed);
184 $c->stash( 'koha.embed' => $THE_embed )
193 =head2 Internal methods
195 =head3 _reserved_words
197 my $reserved_words = _reserved_words();
201 sub _reserved_words {
203 my @reserved_words = qw( _match _order_by _page _per_page );
204 return \@reserved_words;
207 =head3 _build_order_atom
209 my $order_atom = _build_order_atom( $string );
211 Parses I<$string> and outputs data valid for using in SQL::Abstract order_by attribute
212 according to the following rules:
215 +string -> I<{ -asc => string }>
216 -string -> I<{ -desc => string }>
220 sub _build_order_atom {
222 my $string = $args->{string};
223 my $result_set = $args->{result_set};
226 $param =~ s/^(\+|\-|\s)//;
228 my $model_param = $result_set->from_api_mapping->{$param};
229 $param = $model_param if defined $model_param;
232 if ( $string =~ m/^\+/ or
233 $string =~ m/^\s/ ) {
234 # asc order operator present
235 return { -asc => $param };
237 elsif ( $string =~ m/^\-/ ) {
238 # desc order operator present
239 return { -desc => $param };
242 # no order operator present
249 my $embed = _parse_embed( $string );
251 Parses I<$string> and outputs data valid for passing to the Kohaa::Object(s)->to_api
260 my ( $curr, $next ) = split /\s*\.\s*/, $string, 2;
263 $result->{$curr} = { children => _parse_embed( $next ) };
266 if ( $curr =~ m/^(?<relation>.*)\+count/ ) {
267 my $key = $+{relation} . "_count";
268 $result->{$key} = { is_count => 1 };
271 $result->{$curr} = {};
280 _merge_embed( $parsed_embed, $global_embed );
282 Merges the hash referenced by I<$parsed_embed> into I<$global_embed>.
287 my ( $structure, $embed ) = @_;
289 my ($root) = keys %{ $structure };
291 if ( any { $root eq $_ } keys %{ $embed } ) {
293 _merge_embed( $structure->{$root}, $embed->{$root} );
297 $embed->{$root} = $structure->{$root};