Newer
Older
Digital_Repository / OARiNZ / DIY / deb_package / eprints-3.0 / cgi / openurl
nstanger on 7 Jun 2007 5 KB - Added debian package source.
  1. ######################################################################
  2. #
  3. # EPrints OpenURL Internal Resolver
  4. #
  5. ######################################################################
  6. #
  7. # This file is part of GNU EPrints 2.
  8. #
  9. # Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
  10. #
  11. # EPrints 2 is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # EPrints 2 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with EPrints 2; if not, write to the Free Software
  23. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. #
  25. ######################################################################
  26.  
  27. use EPrints;
  28.  
  29. eval "use URI::OpenURL";
  30. if( $@ )
  31. {
  32. EPrints::abort( "To enable the OpenURL resolver please install URI::OpenURL: cpan install URI::OpenURL" );
  33. }
  34.  
  35. use strict;
  36. my $session = new EPrints::Session;
  37. exit( 0 ) unless( defined $session );
  38. # $session->get_database->set_debug( 1 );
  39.  
  40. my $repository = $session->get_repository();
  41.  
  42. # Create an OpenURL object
  43. my $uri = URI::OpenURL->new();
  44.  
  45. # Fill the OpenURL object with the current CGI parameters
  46. my @query;
  47. foreach my $name ($session->param)
  48. {
  49. for ($session->param( $name ))
  50. {
  51. push @query, $name => $_;
  52. }
  53. }
  54. $uri->query_form( @query );
  55.  
  56. # warn $uri->dump;
  57.  
  58. # Get the referent (the referred to object)
  59. my $rft = $uri->referent;
  60.  
  61. # Get the metadata given for the referent
  62. my %md = $rft->metadata;
  63.  
  64. # Set up a new search expression
  65. # NB I'm not worrying about filtering because this script will only
  66. # redirect?
  67. my $ds = $session->get_repository->get_dataset( "archive" );
  68. my $searchexp = new EPrints::Search(
  69. keep_cache => 1,
  70. session => $session,
  71. dataset => $ds,
  72. satisfy_all => 1,
  73. );
  74.  
  75. # Fill in the search expression, mapping OpenURL terms to eprint fields
  76. if( $md{ "aulast" } )
  77. {
  78. $searchexp->add_field(
  79. $ds->get_field( "creators_name" ),
  80. $md{ "aulast" }
  81. );
  82. }
  83. if( defined($rft->id) )
  84. {
  85. my $oai_id = $session->get_repository->get_conf( "oai" )->{ "v2" }->{ "archive_id" };
  86. if( defined($rft->id) and $rft->id =~ /^(?:info:ofi\/nam:info:)?oai:$oai_id:(\d+)$/ )
  87. {
  88. $searchexp->add_field(
  89. $ds->get_field( "eprintid" ),
  90. $1
  91. );
  92. }
  93. else
  94. {
  95. $searchexp->add_field(
  96. $ds->get_field( "official_url" ),
  97. scalar($rft->id)
  98. );
  99. }
  100. }
  101. my %FREE_TEXT = qw(
  102. title atitle
  103. publication title
  104. );
  105. while(my( $epf, $ouf ) = each %FREE_TEXT)
  106. {
  107. next unless defined($md{ $ouf }) and length($md{ $ouf });
  108. $searchexp->add_field(
  109. $ds->get_field( $epf ),
  110. $md{ $ouf },
  111. "IN"
  112. );
  113. }
  114. my %EXACT_TERMS = qw(
  115. volume volume
  116. number number
  117. series series
  118. date date
  119. pagerange pages
  120. );
  121. while(my( $epf, $ouf ) = each %EXACT_TERMS)
  122. {
  123. next unless defined($md{ $ouf }) and length($md{ $ouf });
  124. $searchexp->add_field(
  125. $ds->get_field( $epf ),
  126. $md{ $ouf }
  127. );
  128. }
  129.  
  130. # Urg, either the query was empty or we couldn't use the query given
  131. if( $searchexp->is_blank )
  132. {
  133. not_found( $session, $uri, "empty_query" );
  134. }
  135.  
  136. # Perform the search, but we're only interested in the first match
  137. # that comes back (perhaps we should error on more than one match?)
  138. my $list = $searchexp->perform_search();
  139. my( $eprint ) = $list->get_records( 0, 1 );
  140.  
  141. # No match found for the given query
  142. unless( $eprint ) {
  143. not_found( $session, $uri, "no_match" );
  144. }
  145.  
  146. my $svc = $uri->serviceType;
  147.  
  148. my %svc_md = $svc->metadata;
  149.  
  150. # OAI-PMH
  151. if( $svc->val_fmt and $svc->val_fmt eq 'info:ofi/fmt:kev:mtx:oai_pmh' )
  152. {
  153. # Location of the OAI-PMH interface
  154. my $url = $repository->get_conf( "base_url" );
  155. $url .= "/cgi/oai2";
  156. $url = URI->new( $url );
  157.  
  158. # Get the OAI identifier for this eprint
  159. my $oai_id = $session->get_repository->get_conf(
  160. "oai",
  161. "v2",
  162. "archive_id" );
  163. $oai_id = EPrints::OpenArchives::to_oai_identifier( $oai_id, $eprint->get_id );
  164.  
  165. # Add the OAI-PMH arguments
  166. my %args = %svc_md;
  167. my $verb = $args{verb} || '';
  168. if( $verb eq 'GetRecord' || $verb eq 'ListMetadataFormats' )
  169. {
  170. $args{identifier} = $oai_id;
  171. }
  172. $url->query_form( %args );
  173.  
  174. $session->redirect( $url );
  175. }
  176. # Scholarly Service
  177. elsif( $svc_md{ "fulltext" } and
  178. $svc_md{ "fulltext" } eq 'yes' )
  179. {
  180. # Redirect to the main document (if possible)
  181. my @docs = $eprint->get_all_documents();
  182. if( @docs )
  183. {
  184. my $url = $docs[0]->get_url();
  185. $session->redirect( $url );
  186. }
  187. }
  188. # Default to redirecting to the abstract page
  189. else
  190. {
  191. # Redirect to the abstract page
  192. my $url = $eprint->get_url;
  193. $session->redirect( $url );
  194. }
  195.  
  196. $session->terminate;
  197.  
  198. sub not_found
  199. {
  200. my( $session, $uri, $message ) = @_;
  201.  
  202. $session->not_found( undef ); # Don't print the default 404!
  203.  
  204. my %pins = $uri->query_form;
  205. $_ = $session->make_text($_) for values %pins;
  206.  
  207. $session->build_page(
  208. $session->html_phrase( "cgi/openurl:${message}_title" ),
  209. $session->html_phrase( "cgi/openurl:${message}_blurb", %pins ),
  210. "plain_404" );
  211.  
  212. $session->send_page();
  213.  
  214. exit(0);
  215. }