Browse Source

fixed session issues on for eds and minor changes

2.x-ebsco
Paul Pound 12 years ago
parent
commit
86ef54b274
  1. 2
      targets/cufts/theme/roblib-search-cufts.tpl.php
  2. 172
      targets/eds/includes/rest/EBSCOAPI.php
  3. 7
      targets/eds/roblib_search_eds.module
  4. 2
      targets/eds/theme/roblib-search-eds.tpl.php
  5. 14
      targets/evergreen/js/evergreen_results.js
  6. 19
      targets/evergreen/roblib_search_evergreen.module
  7. 2
      targets/evergreen/theme/roblib-search-evergreen.tpl.php

2
targets/cufts/theme/roblib-search-cufts.tpl.php

@ -18,7 +18,7 @@
?> ?>
<div class ="roblib-search-content cufts" id="roblib-search-content-cufts"> <div class ="roblib-search-content cufts" id="roblib-search-content-cufts">
<img src="<?php print $spinner_path; ?>"/> <img src="<?php print (empty($spinner_path) ? ' ' : $spinner_path); ?>"/>
</div> </div>
<div class ="roblib-search-more">Search all Journals</div> <div class ="roblib-search-more">Search all Journals</div>

172
targets/eds/includes/rest/EBSCOAPI.php

@ -1,6 +1,5 @@
<?php <?php
/** /**
* EBSCO API class * EBSCO API class
* *
@ -8,19 +7,17 @@
* *
*/ */
/** /**
* EBSCO API class * EBSCO API class
*/ */
class EBSCOAPI class EBSCOAPI {
{
/** /**
* The authentication token used for API transactions * The authentication token used for API transactions
* @global string * @global string
*/ */
private $authenticationToken; private $authenticationToken;
/** /**
* The session token for API transactions * The session token for API transactions
* @global string * @global string
@ -32,7 +29,6 @@ class EBSCOAPI
* @global object EBSCOConnector * @global object EBSCOConnector
*/ */
private $connector; private $connector;
private $config; private $config;
public function __construct($config) { public function __construct($config) {
@ -48,8 +44,7 @@ class EBSCOAPI
* @return EBSCOConnector object * @return EBSCOConnector object
* @access public * @access public
*/ */
public function connector($config) public function connector($config) {
{
if (empty($this->connector)) { if (empty($this->connector)) {
$this->connector = new EBSCOConnector($config); $this->connector = new EBSCOConnector($config);
} }
@ -57,7 +52,6 @@ class EBSCOAPI
return $this->connector; return $this->connector;
} }
/** /**
* Create a new EBSCOResponse object * Create a new EBSCOResponse object
* *
@ -66,13 +60,11 @@ class EBSCOAPI
* @return EBSCOResponse object * @return EBSCOResponse object
* @access public * @access public
*/ */
public function response($response) public function response($response) {
{
$responseObj = new EBSCOResponse($response); $responseObj = new EBSCOResponse($response);
return $responseObj; return $responseObj;
} }
/** /**
* Request authentication and session tokens, then send the API request. * Request authentication and session tokens, then send the API request.
* Retry the request if authentication errors occur * Retry the request if authentication errors occur
@ -85,19 +77,18 @@ class EBSCOAPI
* @return array An associative array with results. * @return array An associative array with results.
* @access protected * @access protected
*/ */
protected function request($action, $params = null, $attempts = 3) protected function request($action, $params = null, $attempts = 3) {
{
try { try {
$authenticationToken = $this->getAuthToken(); $authenticationToken = $this->getAuthToken();
$sessionToken = $this->getSessionToken($authenticationToken); $sessionToken = $this->getSessionToken($authenticationToken);
if(empty($authenticationToken)){ if (empty($authenticationToken)) {
$authenticationToken = $this -> getAuthToken(); $authenticationToken = $this->getAuthToken();
} }
if(empty($sessionToken)){ if (empty($sessionToken)) {
$sessionToken = $this -> getSessionToken($authenticationToken,'y'); $sessionToken = $this->getSessionToken($authenticationToken, 'y');
} }
$headers = array( $headers = array(
@ -109,14 +100,14 @@ class EBSCOAPI
$result = $this->response($response)->result(); $result = $this->response($response)->result();
$results = $result; $results = $result;
return $results; return $results;
} catch(EBSCOException $e) { } catch (EBSCOException $e) {
try { try {
// Retry the request if there were authentication errors // Retry the request if there were authentication errors
$code = $e->getCode(); $code = $e->getCode();
switch ($code) { switch ($code) {
case EBSCOConnector::EDS_AUTH_TOKEN_INVALID: case EBSCOConnector::EDS_AUTH_TOKEN_INVALID:
$authenticationToken = $this->getAuthToken(); $authenticationToken = $this->getAuthToken();
$sessionToken = $this ->getSessionToken($authenticationToken); $sessionToken = $this->getSessionToken($authenticationToken);
$headers = array( $headers = array(
'x-authenticationToken: ' . $authenticationToken, 'x-authenticationToken: ' . $authenticationToken,
'x-sessionToken: ' . $sessionToken 'x-sessionToken: ' . $sessionToken
@ -126,7 +117,7 @@ class EBSCOAPI
} }
break; break;
case EBSCOConnector::EDS_SESSION_TOKEN_INVALID: case EBSCOConnector::EDS_SESSION_TOKEN_INVALID:
$sessionToken = $this ->getSessionToken($authenticationToken,'y'); $sessionToken = $this->getSessionToken($authenticationToken, 'y');
$headers = array( $headers = array(
'x-authenticationToken: ' . $authenticationToken, 'x-authenticationToken: ' . $authenticationToken,
'x-sessionToken: ' . $sessionToken 'x-sessionToken: ' . $sessionToken
@ -142,13 +133,13 @@ class EBSCOAPI
return $result; return $result;
break; break;
} }
} catch(Exception $e) { } catch (Exception $e) {
$result = array( $result = array(
'error' => $e->getMessage() 'error' => $e->getMessage()
); );
return $result; return $result;
} }
} catch(Exception $e) { } catch (Exception $e) {
$result = array( $result = array(
'error' => $e->getMessage() 'error' => $e->getMessage()
); );
@ -156,16 +147,22 @@ class EBSCOAPI
} }
} }
private function writeLockFile(){ private function writeTokenFile() {
$tokenFile = fopen("edstoken.txt","w+"); $tokenFile = fopen("edstoken.txt", "w+");
$result = $this->apiAuthenticationToken(); $result = $this->apiAuthenticationToken();
fwrite($tokenFile, $result['authenticationToken']."\n"); fwrite($tokenFile, $result['authenticationToken'] . "\n");
fwrite($tokenFile, $result['authenticationTimeout']."\n"); fwrite($tokenFile, $result['authenticationTimeout'] . "\n");
fwrite($tokenFile, $result['authenticationTimeStamp']); fwrite($tokenFile, $result['authenticationTimeStamp']);
fclose($tokenFile); fclose($tokenFile);
return $result['authenticationToken']; return $result['authenticationToken'];
} }
private function writeLockFile() {
$lockFile = fopen("edslock.txt", "w+");
fwrite($lockFile, 'lock');
fclose($lockFile);
}
/* /*
* Get authentication token from appication scop * Get authentication token from appication scop
* Check authToen's expiration * Check authToen's expiration
@ -175,32 +172,40 @@ class EBSCOAPI
* *
* @access public * @access public
*/ */
public function getAuthToken(){
$lockFile = fopen("edslock.txt","r"); public function getAuthToken() {
$tokenFile =fopen("edstoken.txt","r"); $lockFile = fopen("edslock.txt", "r");
if(empty($lockFile) || empty($tokenFile)){ if (empty($lockFile)) {
return $this->writeLockFile(); $this->writeLockFile();
} $lockFile = fopen("edslock.txt", "r");
while(!feof($tokenFile)){ }
$authToken = rtrim(fgets($tokenFile),"\n"); $tokenFile = fopen("edstoken.txt", "r");
$timeout = fgets($tokenFile)-600; if (empty($tokenFile)) {
$this->writetokenFile();
$tokenFile = fopen("edstoken.txt", "r");
}
while (!feof($tokenFile)) {
$authToken = rtrim(fgets($tokenFile), "\n");
$timeout = fgets($tokenFile) - 600;
$timestamp = fgets($tokenFile); $timestamp = fgets($tokenFile);
} }
fclose($tokenFile); fclose($tokenFile);
if(time()-$timestamp>=$timeout){ if (time() - $timestamp >= $timeout) {
// Lock check. // Lock check.
if(flock($lockFile, LOCK_EX)){ if (flock($lockFile, LOCK_EX)) {
$tokenFile = fopen("edstoken.txt","w+"); $tokenFile = fopen("edstoken.txt", "w+");
$result = $this->apiAuthenticationToken(); $result = $this->apiAuthenticationToken();
fwrite($tokenFile, $result['authenticationToken']."\n"); fwrite($tokenFile, $result['authenticationToken'] . "\n");
fwrite($tokenFile, $result['authenticationTimeout']."\n"); fwrite($tokenFile, $result['authenticationTimeout'] . "\n");
fwrite($tokenFile, $result['authenticationTimeStamp']); fwrite($tokenFile, $result['authenticationTimeStamp']);
fclose($tokenFile); fclose($tokenFile);
return $result['authenticationToken']; return $result['authenticationToken'];
}else{ }
else {
return $authToken; return $authToken;
} }
}else{ }
else {
return $authToken; return $authToken;
} }
fclose($lockFile); fclose($lockFile);
@ -213,8 +218,7 @@ class EBSCOAPI
* *
* @access public * @access public
*/ */
public function apiAuthenticationToken() public function apiAuthenticationToken() {
{
$response = $this->connector->requestAuthenticationToken(); $response = $this->connector->requestAuthenticationToken();
$result = $this->response($response)->result(); $result = $this->response($response)->result();
return $result; return $result;
@ -228,42 +232,37 @@ class EBSCOAPI
* @param Authentication token, Profile * @param Authentication token, Profile
* @access public * @access public
*/ */
public function getSessionToken($authenToken, $invalid='n'){ public function getSessionToken($authenToken, $invalid = 'n') {
$token = ''; $token = '';
// Check user's login status // Check user's login status
if(isset($_COOKIE['login'])){ /* if(isset($_COOKIE['login'])){
if($invalid=='y'){ if($invalid=='y'){
$profile = $_SESSION['sessionToken']['profile']; $profile = $_SESSION['sessionToken']['profile'];
$sessionToken = $this->apiSessionToken($authenToken, $profile,'n'); $sessionToken = $this->apiSessionToken($authenToken, $profile,'n');
$_SESSION['sessionToken']=$sessionToken; $_SESSION['sessionToken']=$sessionToken;
} }
$token = $_SESSION['sessionToken']['sessionToken']; $token = $_SESSION['sessionToken']['sessionToken'];
} } */
else if(isset($_COOKIE['Guest'])){ //else
if($invalid=='y'){ if (isset($_COOKIE['Guest'])) {
if ($invalid == 'y') {
$profile = $_SESSION['sessionToken']['profile']; $profile = $_SESSION['sessionToken']['profile'];
$sessionToken = $this->apiSessionToken($authenToken, $profile,'y'); if (empty($profile)) {
$_SESSION['sessionToken']=$sessionToken; $profile = $_COOKIE['Guest'];
}
$sessionToken = $this->apiSessionToken($authenToken, $profile, 'y');
$_SESSION['sessionToken'] = $sessionToken;
} }
$token = $_SESSION['sessionToken']['sessionToken']; $token = $_SESSION['sessionToken']['sessionToken'];
}else{ }
//$xml ="Config.xml"; else {
//$dom = new DOMDocument();
//$dom->load($xml);
//$EDSCredentials = $dom ->getElementsByTagName('EDSCredentials')->item(0);
//$users = $EDSCredentials -> getElementsByTagName('User');
$profileId = $this->config['profile']; $profileId = $this->config['profile'];
//foreach($users as $user){
// $userType = $user->getElementsByTagName('ClientUser')->item(0)->nodeValue; $sessionToken = $this->apiSessionToken($authenToken, $profileId, 'y');
// if($userType == 'guest'){
// $profileId = $user -> getElementsByTagName('EDSProfile')->item(0)->nodeValue;
// break;
// }
//}
$sessionToken = $this->apiSessionToken($authenToken, $profileId,'y');
$_SESSION['profile'] = $profileId; $_SESSION['profile'] = $profileId;
$_SESSION['sessionToken']=$sessionToken; $_SESSION['sessionToken'] = $sessionToken;
setcookie("Guest", $profileId, 0); setcookie("Guest", $profileId, 0);
$token = $sessionToken['sessionToken']; $token = $sessionToken['sessionToken'];
} }
@ -277,17 +276,16 @@ class EBSCOAPI
* *
* @access public * @access public
*/ */
public function apiSessionToken($authenToken, $profile, $guest) public function apiSessionToken($authenToken, $profile, $guest) {
{
// Add authentication tokens to headers // Add authentication tokens to headers
$headers = array( $headers = array(
'x-authenticationToken: ' . $authenToken 'x-authenticationToken: ' . $authenToken
); );
$response = $this->connector($this->config)->requestSessionToken($headers, $profile,$guest); $response = $this->connector($this->config)->requestSessionToken($headers, $profile, $guest);
$result = $this->response($response)->result(); $result = $this->response($response)->result();
$token = array( $token = array(
'sessionToken'=>$result, 'sessionToken' => $result,
'profile' => $profile 'profile' => $profile
); );
return $token; return $token;
@ -300,14 +298,14 @@ class EBSCOAPI
* *
* @access public * @access public
*/ */
public function apiEndSessionToken($authenToken, $sessionToken){ public function apiEndSessionToken($authenToken, $sessionToken) {
// Add authentication tokens to headers // Add authentication tokens to headers
$headers = array( $headers = array(
'x-authenticationToken: '.$authenToken 'x-authenticationToken: ' . $authenToken
); );
$this -> connector($this->config)->requestEndSessionToken($headers, $sessionToken); $this->connector($this->config)->requestEndSessionToken($headers, $sessionToken);
} }
/** /**
@ -325,7 +323,6 @@ class EBSCOAPI
return $results; return $results;
} }
/** /**
* Wrapper for retrieve API call * Wrapper for retrieve API call
* *
@ -336,8 +333,7 @@ class EBSCOAPI
* @return array An associative array of data * @return array An associative array of data
* @access public * @access public
*/ */
public function apiRetrieve($an, $db, $term) public function apiRetrieve($an, $db, $term) {
{
// Add the HTTP query params // Add the HTTP query params
$params = array( $params = array(
'an' => $an, 'an' => $an,
@ -349,27 +345,27 @@ class EBSCOAPI
return $result; return $result;
} }
/** /**
* Wrapper for info API call * Wrapper for info API call
* *
* @return array An associative array of data * @return array An associative array of data
* @access public * @access public
*/ */
public function getInfo() public function getInfo() {
{ if (isset($_SESSION['info'])) {
if(isset($_SESSION['info'])){
$InfoArray = $_SESSION['info']; $InfoArray = $_SESSION['info'];
$timestamp = $InfoArray['timestamp']; $timestamp = $InfoArray['timestamp'];
if(time()-$timestamp>=3600){ if (time() - $timestamp >= 3600) {
// Get new Info for the profile // Get new Info for the profile
$InfoArray = $this->apiInfo(); $InfoArray = $this->apiInfo();
$_SESSION['info'] = $InfoArray; $_SESSION['info'] = $InfoArray;
$info = $InfoArray['Info']; $info = $InfoArray['Info'];
}else{ }
else {
$info = $InfoArray['Info']; $info = $InfoArray['Info'];
} }
}else{ }
else {
// Get new Info for the profile // Get new Info for the profile
$InfoArray = $this->apiInfo(); $InfoArray = $this->apiInfo();
$_SESSION['info'] = $InfoArray; $_SESSION['info'] = $InfoArray;
@ -378,14 +374,16 @@ class EBSCOAPI
return $info; return $info;
} }
public function apiInfo(){ public function apiInfo() {
$response = $this->request('Info',''); $response = $this->request('Info', '');
$Info = array( $Info = array(
'Info' => $response, 'Info' => $response,
'timestamp'=>time() 'timestamp' => time()
); );
return $Info; return $Info;
} }
} }
?> ?>

7
targets/eds/roblib_search_eds.module

@ -41,7 +41,7 @@ function roblib_search_eds_menu() {
$items['admin/roblib_search/eds_search'] = array( $items['admin/roblib_search/eds_search'] = array(
'title' => 'EDS search Target configuration', 'title' => 'EDS search Target configuration',
'description' => 'Configuration for the Roblib evergreen search target', 'description' => 'Configuration for the Roblib eds search target',
'page callback' => 'drupal_get_form', 'page callback' => 'drupal_get_form',
'page arguments' => array('roblib_search_eds_config_form'), 'page arguments' => array('roblib_search_eds_config_form'),
'access arguments' => array('access administration pages'), 'access arguments' => array('access administration pages'),
@ -216,11 +216,6 @@ function roblib_search_eds_get_results($query = NULL) {
} }
$config = roblib_search_eds_build_config_arr(); $config = roblib_search_eds_build_config_arr();
/*$config['user'] = variable_get('roblib_search_eds_user', 'edsusername');
$config['pass'] = variable_get('roblib_search_eds_pass', 'edspassword');
$config['profile'] = variable_get('roblib_search_eds_profile', 'edsapi');
$config['auth_url'] = variable_get('roblib_search_eds_auth_url', 'https://eds-api.ebscohost.com/Authservice/rest');
$config['rest_url'] = variable_get('roblib_search_eds_rest_url', 'http://eds-api.ebscohost.com/edsapi/rest');*/
$eds_api = new EBSCOAPI($config); $eds_api = new EBSCOAPI($config);
$number_per_page = variable_get('roblib_search_eds_num_results', '5'); $number_per_page = variable_get('roblib_search_eds_num_results', '5');
$query = urlencode($query); $query = urlencode($query);

2
targets/eds/theme/roblib-search-eds.tpl.php

@ -18,7 +18,7 @@
?> ?>
<div class ="roblib-search-content eds" id="roblib-search-content-eds"> <div class ="roblib-search-content eds" id="roblib-search-content-eds">
<img src="<?php print $spinner_path; ?>"/> <img src="<?php print (empty($spinner_path) ? ' ' : $spinner_path); ?>"/>
</div> </div>
<div class ="roblib-search-more" id="roblib-search-eds-more"></div> <div class ="roblib-search-more" id="roblib-search-eds-more"></div>

14
targets/evergreen/js/evergreen_results.js

@ -30,6 +30,13 @@ Drupal.behaviors.roblib_search_evergreen = {
}) })
items.push('</div>') items.push('</div>')
} }
if(typeof val.electronic_holdings !== 'undefined'){
items.push('<div class="evergreen-holdings">');
jQuery.each(val.electronic_holdings, function(key2, val2){
roblibEvergreenAddElectronicHoldings(val2, items);
})
items.push('</div>')
}
items.push('</div>'); items.push('</div>');
}); });
} }
@ -41,9 +48,10 @@ Drupal.behaviors.roblib_search_evergreen = {
} }
function roblibEvergreenAddHoldings(holdings, items){ function roblibEvergreenAddHoldings(holdings, items){
items.push('<div class="evergreen-holdings-item">' + holdings.call_number + ' ' + holdings.availability + '</div>'); items.push('<div class="evergreen-holdings-item">' + holdings.call_number + ' - ' + holdings.location +' (' + holdings.availability + ')</div>');
}
function roblibEvergreenAddElectronicHoldings(holdings, items){
items.push('<div class="evergreen-holdings-item"><a href="' + holdings.url + '">' + holdings.label + '</div>');
} }

19
targets/evergreen/roblib_search_evergreen.module

@ -233,6 +233,7 @@ function roblib_search_evergreen_parse_results($results, $query) {
roblib_search_evergreen_update_array($output, $index, 'date', $record->xpath('marcxml:datafield[@tag="260"]/marcxml:subfield[@code="c"]')); roblib_search_evergreen_update_array($output, $index, 'date', $record->xpath('marcxml:datafield[@tag="260"]/marcxml:subfield[@code="c"]'));
roblib_search_evergreen_update_array($output, $index, 'id', $record->xpath('marcxml:datafield[@tag="901"]/marcxml:subfield[@code="c"]')); roblib_search_evergreen_update_array($output, $index, 'id', $record->xpath('marcxml:datafield[@tag="901"]/marcxml:subfield[@code="c"]'));
roblib_search_evergreen_holdings($output, $index, $record->xpath('marcxml:datafield[@tag="852"]')); roblib_search_evergreen_holdings($output, $index, $record->xpath('marcxml:datafield[@tag="852"]'));
roblib_search_evergreen_electronic_holdings($output, $index, $record->xpath('marcxml:datafield[@tag="856"]'));
$id = $output[$index]['id']; $id = $output[$index]['id'];
$output[$index++]['url'] = variable_get('roblib_search_evergreen_url', 'http://137.149.200.52') $output[$index++]['url'] = variable_get('roblib_search_evergreen_url', 'http://137.149.200.52')
. variable_get('roblib_search_evergreen_detail_suffix', '/opac/en-CA/skin/default/xml/rdetail.xml?r=') . variable_get('roblib_search_evergreen_detail_suffix', '/opac/en-CA/skin/default/xml/rdetail.xml?r=')
@ -249,7 +250,23 @@ function roblib_search_evergreen_holdings(&$arr, $index, $xpath_result){
$call_number = $xml->xpath('marcxml:subfield[@code="c"]'); $call_number = $xml->xpath('marcxml:subfield[@code="c"]');
$arr[$index]['holdings'][$holding_index]['call_number'] = (string)$call_number[0]; $arr[$index]['holdings'][$holding_index]['call_number'] = (string)$call_number[0];
$availability = $xml->xpath('marcxml:subfield[@code="n"]'); $availability = $xml->xpath('marcxml:subfield[@code="n"]');
$arr[$index]['holdings'][$holding_index++]['availability'] = (string)$availability[0]; $arr[$index]['holdings'][$holding_index]['availability'] = (string)$availability[0];
$location = $xml->xpath('marcxml:subfield[@code="a"]');
$arr[$index]['holdings'][$holding_index++]['location'] = (string)$location[0];
}
}
function roblib_search_evergreen_electronic_holdings(&$arr, $index, $xpath_result){
$holding_index = 0;
foreach($xpath_result as $xml){
$xml->registerXPathNamespace('marcxml', 'http://www.loc.gov/MARC21/slim');
$label = $xml->xpath('marcxml:subfield[@code="y"]');
$label = (string)$label[0];
$url = $xml->xpath('marcxml:subfield[@code="u"]');
$url = (string)$url[0];
$arr[$index]['electronic_holdings'][$holding_index]['url'] = $url;
$arr[$index]['electronic_holdings'][$holding_index++]['label'] = $label;
} }
} }

2
targets/evergreen/theme/roblib-search-evergreen.tpl.php

@ -18,7 +18,7 @@
?> ?>
<div class ="roblib-search-content evergreen" id="roblib-search-content-evergreen"> <div class ="roblib-search-content evergreen" id="roblib-search-content-evergreen">
<img src="<?php print $spinner_path; ?>"/> <img src="<?php print (empty($spinner_path) ? ' ' : $spinner_path); ?>"/>
</div> </div>
<div class ="roblib-search-more" id="roblib-search-evergreen-more">Search all Results</div> <div class ="roblib-search-more" id="roblib-search-evergreen-more">Search all Results</div>
Loading…
Cancel
Save