1 package Koha::REST::Plugin::Pagination;
3 # This file is part of Koha.
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Plugin';
24 Koha::REST::Plugin::Pagination
28 =head2 Mojolicious::Plugin methods
35 my ( $self, $app ) = @_;
39 =head3 add_pagination_headers
41 my $patrons = Koha::Patrons->search( ... );
42 $c->add_pagination_headers({
43 total => $patrons->count,
51 Adds a Link header to the response message $c carries, following RFC5988, including
52 the following relation types: 'prev', 'next', 'first' and 'last'.
53 It also adds X-Total-Count containing the total results count, and X-Base-Total-Count containing the total of the non-filtered results count.
55 If page size is omitted, it defaults to the value of the RESTdefaultPageSize syspref.
60 'add_pagination_headers' => sub {
61 my ( $c, $args ) = @_;
63 my $total = $args->{total};
64 my $base_total = $args->{base_total};
65 my $req_page = $args->{params}->{_page} // 1;
66 my $per_page = $args->{params}->{_per_page} //
67 C4::Context->preference('RESTdefaultPageSize') // 20;
70 if ( $per_page == -1 ) {
75 $pages = int $total / $per_page;
77 if $total % $per_page > 0;
82 if ( $per_page != -1 and $pages > 1 and $req_page > 1 ) { # Previous exists?
86 { page => $req_page - 1,
87 per_page => $per_page,
89 params => $args->{params}
94 if ( $per_page != -1 and $pages > 1 and $req_page < $pages ) { # Next exists?
98 { page => $req_page + 1,
99 per_page => $per_page,
101 params => $args->{params}
108 { page => 1, per_page => $per_page, rel => 'first', params => $args->{params} } );
111 { page => $pages, per_page => $per_page, rel => 'last', params => $args->{params} } );
114 foreach my $link (@links) {
115 $c->res->headers->add( 'Link' => $link );
118 # Add X-Total-Count header
119 $c->res->headers->add( 'X-Total-Count' => $total );
120 $c->res->headers->add( 'X-Base-Total-Count' => $base_total );
125 =head3 dbic_merge_pagination
127 $filter = $c->dbic_merge_pagination({
130 page => $params->{_page},
131 per_page => $params->{_per_page}
135 Adds I<page> and I<rows> elements to the filter parameter.
140 'dbic_merge_pagination' => sub {
141 my ( $c, $args ) = @_;
142 my $filter = $args->{filter};
144 $filter->{page} = $args->{params}->{_page};
145 $filter->{rows} = $args->{params}->{_per_page};
152 =head2 Internal methods
156 my $link = _build_link( $c, { page => 1, per_page => 5, rel => 'prev' });
158 Returns a string, suitable for using in Link headers following RFC5988.
163 my ( $c, $args ) = @_;
165 my $params = $args->{params};
167 $params->{_page} = $args->{page};
168 $params->{_per_page} = $args->{per_page};
171 . $c->req->url->clone->query(
175 . $args->{rel} . '"';
177 # TODO: Find a better solution for this horrible (but needed) fix
178 $link =~ s|api/v1/app\.pl/||;