bug 2505: enable strict and warnings in ILS-DI code
[koha.git] / opac / ilsdi.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 SARL Biblibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 use C4::ILSDI::Services;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Context;
27 use XML::Simple;
28 use CGI;
29
30 =head1 DLF ILS-DI for Koha
31
32 This script is a basic implementation of ILS-DI protocol for Koha.
33 It acts like a dispatcher, that get the CGI request, check required and 
34 optionals arguments, call a function from C4::ILS-DI::Services, and finaly 
35 outputs the returned hashref as XML.
36
37 =cut
38
39 # Instanciate the CGI request
40 my $cgi = new CGI;
41
42 # List of available services, sorted by level
43 my @services = (
44         'Describe',                          # Not part of ILS-DI, online API doc
45 #       Level 1: Basic Discovery Interfaces
46 #       'HarvestBibliographicRecords',       # OAI-PMH
47 #       'HarvestExpandedRecords',            # OAI-PMH
48         'GetAvailability',                   # FIXME Add bibbliographic level
49 #       'GoToBibliographicRequestPage'       # I don't understant this one
50 #       Level 2: Elementary OPAC supplement
51 #       'HarvestAuthorityRecords',           # OAI-PMH
52 #       'HarvestHoldingsRecords',            # OAI-PMH
53         'GetRecords',                        # Note that we can use OAI-PMH for this too
54 #       'Search',                            # TODO
55 #       'Scan',                              # TODO
56         'GetAuthorityRecords',               
57 #       'OutputRewritablePage',              # I don't understant this one
58 #       'OutputIntermediateFormat',          # I don't understant this one
59 #       Level 3: Elementary OPAC alternative
60         'LookupPatron',
61         'AuthenticatePatron',
62         'GetPatronInfo',
63         'GetPatronStatus',
64         'GetServices',                       # FIXME Loans
65         'RenewLoan', 
66         'HoldTitle',                         # FIXME Add dates support
67         'HoldItem',                          # FIXME Add dates support
68         'CancelHold',
69 #       'RecallItem',                        # Not supported by Koha
70 #       'CancelRecall',                      # Not supported by Koha
71 #       Level 4: Robust/domain specific discovery platforms
72 #       'SearchCourseReserves',              # TODO 
73 #       'Explain'                            # TODO
74 );
75
76 # List of required arguments
77 my %required = (
78         'Describe'            => ['verb'],
79         'GetAvailability'     => ['id', 'id_type'],
80         'GetRecords'          => ['id'],
81         'GetAuthorityRecords' => ['id'],
82         'LookupPatron'        => ['id'],
83         'AuthenticatePatron'  => ['username', 'password'],
84         'GetPatronInfo'       => ['patron_id'],
85         'GetPatronStatus'     => ['patron_id'],
86         'GetServices'         => ['patron_id', 'item_id'],
87         'RenewLoan'           => ['patron_id', 'item_id'],
88         'HoldTitle'           => ['patron_id', 'bib_id', 'request_location'],
89         'HoldItem'            => ['patron_id', 'bib_id', 'item_id'],
90         'CancelHold'          => ['patron_id', 'item_id'],
91 );
92
93 # List of optional arguments
94 my %optional = (
95         'Describe'            => [],
96         'GetAvailability'     => ['return_type', 'return_fmt'],
97         'GetRecords'          => ['schema'],
98         'GetAuthorityRecords' => ['schema'],
99         'LookupPatron'        => ['id_type'],
100         'AuthenticatePatron'  => [],
101         'GetPatronInfo'       => ['show_contact', 'show_fines', 'show_holds', 'show_loans'],
102         'GetPatronStatus'     => [],
103         'GetServices'         => [],
104         'RenewLoan'           => ['desired_due_date'],
105         'HoldTitle'           => ['pickup_location', 'needed_before_date', 'pickup_expiry_date'],
106         'HoldItem'            => ['pickup_location', 'needed_before_date', 'pickup_expiry_date'],
107         'CancelHold'          => [],
108 );
109
110 # If ILS-DI module is disabled in System->Preferences, redirect to 404
111 if (not C4::Context->preference('ILS-DI')) {
112         print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
113 }
114
115 # If no service is requested, display the online documentation
116 if (not $cgi->param('service')) {
117         my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
118             {
119                 template_name   => "ilsdi.tmpl",
120                 query           => $cgi,
121                 type            => "opac",
122                 authnotrequired => 1,
123                 debug           => 1,
124             }
125         );
126         output_html_with_http_headers $cgi, $cookie, $template->output;
127         exit 0;
128 }
129
130 # If user requested a service description, then display it
131 if ($cgi->param('service') eq "Describe" and grep {$cgi->param('verb') eq $_} @services) {
132         my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
133             {
134                 template_name   => "ilsdi.tmpl",
135                 query           => $cgi,
136                 type            => "opac",
137                 authnotrequired => 1,
138                 debug           => 1,
139             }
140         );
141         $template->param( $cgi->param('verb') => 1 );
142         output_html_with_http_headers $cgi, $cookie, $template->output;
143         exit 0;
144 }
145
146 my $service = $cgi->param('service') || "ilsdi";
147
148 my $out;
149
150 # Check if the requested service is in the list
151 if ($service and grep {$service eq $_} @services) {
152
153         my @parmsrequired = @{$required{$service}};
154         my @parmsoptional = @{$optional{$service}};
155         my @parmsall = (@parmsrequired, @parmsoptional);
156         my @names = $cgi->param;
157         my %paramhash = ();
158         foreach my $name (@names) {
159                 $paramhash{$name} = 1;
160         }
161
162         # check for missing parameters
163         foreach my $name (@parmsrequired) {
164                 if ((! exists $paramhash{$name})) {
165                         $out->{'message'} = "missing $name parameter";
166                 }
167         }
168         
169         # check for illegal parameters
170         foreach my $name (@names) {
171                 my $found = 0;
172                 foreach my $name2 (@parmsall) {
173                         if ($name eq $name2) { 
174                                 $found = 1; 
175                         }
176                 }
177                 if (($found == 0) && ($name ne 'service')) {
178                         $out->{'message'} = "$name is an illegal parameter";
179                 }
180         }
181         
182         # check for multiple parameters
183         foreach my $name (@names) {
184                 my @values = $cgi->param($name);
185                 if ($#values != 0) {
186                         $out->{'message'} = "multiple values are not allowed for the $name parameter";
187                 }
188         }
189
190         if (! $out->{'message'}) {
191                 # GetAvailability is a special case, as it cannot use XML::Simple
192                 if ($service eq "GetAvailability") {
193                         print CGI::header('text/xml');
194                         print C4::ILSDI::Services::GetAvailability($cgi);
195                         exit 0;
196                 }
197                 else {
198                         # Variable functions
199                         my $sub = do{
200                                 no strict 'refs';
201                                 my $symbol = 'C4::ILSDI::Services::'.$service;
202                                 \&{"$symbol"};
203                         };
204                         # Call the requested service, and get its return value
205                         $out =  &$sub($cgi);
206                 }
207         }
208
209 else {
210         $out->{'message'} = "NotSupported";
211 }
212
213 # Output XML by passing the hashref to XMLOut
214 print CGI::header('text/xml');
215 print XMLout($out,
216         noattr => 1, 
217         noescape => 1,
218         nosort => 1,
219         xmldecl => '<?xml version="1.0" encoding="ISO-8859-1" ?>', 
220         RootName => $service, 
221         SuppressEmpty => 1);
222