Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 230 lines (198 sloc) 7.567 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
#!/usr/bin/env perl

# Copyright 2009 SARL Biblibre
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA

use strict;
use warnings;

use C4::ILSDI::Services;
use C4::Auth;
use C4::Output;
use Koha;
use C4::Context;
use XML::Simple;
use CGI;

=head1 DLF ILS-DI for Koha

This script is a basic implementation of ILS-DI protocol for Koha.
It acts like a dispatcher, that get the CGI request, check required and
optionals arguments, call a function from C4::ILS-DI::Services, and finaly
outputs the returned hashref as XML.

=cut

# Instanciate the CGI request
my $cgi = new CGI;

# List of available services, sorted by level
my @services = (
    'Describe', # Not part of ILS-DI, online API doc

    # Level 1: Basic Discovery Interfaces
    # 'HarvestBibliographicRecords', # OAI-PMH
    # 'HarvestExpandedRecords', # OAI-PMH
    'GetAvailability', # FIXME Add bibbliographic level

    # 'GoToBibliographicRequestPage' # I don't understant this one
    # Level 2: Elementary OPAC supplement
    # 'HarvestAuthorityRecords', # OAI-PMH
    # 'HarvestHoldingsRecords', # OAI-PMH
    'GetRecords', # Note that we can use OAI-PMH for this too

    # 'Search', # TODO
    # 'Scan', # TODO
    'GetAuthorityRecords',

    # 'OutputRewritablePage', # I don't understant this one
    # 'OutputIntermediateFormat', # I don't understant this one
    # Level 3: Elementary OPAC alternative
    'LookupPatron',
    'AuthenticatePatron',
    'GetPatronInfo',
    'GetPatronStatus',
    'GetServices', # FIXME Loans
    'RenewLoan',
    'HoldTitle', # FIXME Add dates support
    'HoldItem', # FIXME Add dates support
    'CancelHold',

    # 'RecallItem', # Not supported by Koha
    # 'CancelRecall', # Not supported by Koha
    # Level 4: Robust/domain specific discovery platforms
    # 'SearchCourseReserves', # TODO
    # 'Explain' # TODO
);

# List of required arguments
my %required = (
    'Describe' => ['verb'],
    'GetAvailability' => [ 'id', 'id_type' ],
    'GetRecords' => ['id'],
    'GetAuthorityRecords' => ['id'],
    'LookupPatron' => ['id'],
    'AuthenticatePatron' => [ 'username', 'password' ],
    'GetPatronInfo' => ['patron_id'],
    'GetPatronStatus' => ['patron_id'],
    'GetServices' => [ 'patron_id', 'item_id' ],
    'RenewLoan' => [ 'patron_id', 'item_id' ],
    'HoldTitle' => [ 'patron_id', 'bib_id', 'request_location' ],
    'HoldItem' => [ 'patron_id', 'bib_id', 'item_id' ],
    'CancelHold' => [ 'patron_id', 'item_id' ],
);

# List of optional arguments
my %optional = (
    'Describe' => [],
    'GetAvailability' => [ 'return_type', 'return_fmt' ],
    'GetRecords' => ['schema'],
    'GetAuthorityRecords' => ['schema'],
    'LookupPatron' => ['id_type'],
    'AuthenticatePatron' => [],
    'GetPatronInfo' => [ 'show_contact', 'show_fines', 'show_holds', 'show_loans' ],
    'GetPatronStatus' => [],
    'GetServices' => [],
    'RenewLoan' => ['desired_due_date'],
    'HoldTitle' => [ 'pickup_location', 'needed_before_date', 'pickup_expiry_date' ],
    'HoldItem' => [ 'pickup_location', 'needed_before_date', 'pickup_expiry_date' ],
    'CancelHold' => [],
);

# If ILS-DI module is disabled in System->Preferences, redirect to 404
if ( not C4::Context->preference('ILS-DI') ) {
    print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
}

# If no service is requested, display the online documentation
if ( not $cgi->param('service') ) {
    my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
        { template_name => "ilsdi.tmpl",
            query => $cgi,
            type => "opac",
            authnotrequired => 1,
            debug => 1,
        }
    );
    output_html_with_http_headers $cgi, $cookie, $template->output;
    exit 0;
}

# If user requested a service description, then display it
if ( $cgi->param('service') eq "Describe" and grep { $cgi->param('verb') eq $_ } @services ) {
    my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
        { template_name => "ilsdi.tmpl",
            query => $cgi,
            type => "opac",
            authnotrequired => 1,
            debug => 1,
        }
    );
    $template->param( $cgi->param('verb') => 1 );
    output_html_with_http_headers $cgi, $cookie, $template->output;
    exit 0;
}

my $service = $cgi->param('service') || "ilsdi";

my $out;

# Check if the requested service is in the list
if ( $service and grep { $service eq $_ } @services ) {

    my @parmsrequired = @{ $required{$service} };
    my @parmsoptional = @{ $optional{$service} };
    my @parmsall = ( @parmsrequired, @parmsoptional );
    my @names = $cgi->param;
    my %paramhash = ();
    foreach my $name (@names) {
        $paramhash{$name} = 1;
    }

    # check for missing parameters
    foreach my $name (@parmsrequired) {
        if ( ( !exists $paramhash{$name} ) ) {
            $out->{'message'} = "missing $name parameter";
        }
    }

    # check for illegal parameters
    foreach my $name (@names) {
        my $found = 0;
        foreach my $name2 (@parmsall) {
            if ( $name eq $name2 ) {
                $found = 1;
            }
        }
        if ( ( $found == 0 ) && ( $name ne 'service' ) ) {
            $out->{'message'} = "$name is an illegal parameter";
        }
    }

    # check for multiple parameters
    foreach my $name (@names) {
        my @values = $cgi->param($name);
        if ( $#values != 0 ) {
            $out->{'message'} = "multiple values are not allowed for the $name parameter";
        }
    }

    if ( !$out->{'message'} ) {

        # GetAvailability is a special case, as it cannot use XML::Simple
        if ( $service eq "GetAvailability" ) {
            print CGI::header('text/xml');
            print C4::ILSDI::Services::GetAvailability($cgi);
            exit 0;
        } else {

            # Variable functions
            my $sub = do {
                no strict 'refs';
                my $symbol = 'C4::ILSDI::Services::' . $service;
                \&{"$symbol"};
            };

            # Call the requested service, and get its return value
            $out = &$sub($cgi);
        }
    }
} else {
    $out->{'message'} = "NotSupported";
}

# Output XML by passing the hashref to XMLOut
print CGI::header('text/xml');
print XMLout(
    $out,
    noattr => 1,
    noescape => 1,
    nosort => 1,
    xmldecl => '<?xml version="1.0" encoding="ISO-8859-1" ?>',
    RootName => $service,
    SuppressEmpty => 1
);

Something went wrong with that request. Please try again.