From c831b7fda79ee3922aa9d75fec639f5c49388f7b Mon Sep 17 00:00:00 2001 From: Paul Pound Date: Thu, 11 Apr 2013 13:05:41 -0300 Subject: [PATCH] first crack at eds results --- targets/eds/includes/rest/Config.xml | 24 + targets/eds/includes/rest/EBSCOAPI.php | 391 ++++++++ targets/eds/includes/rest/EBSCOConnector.php | 379 +++++++ targets/eds/includes/rest/EBSCOResponse.php | 982 +++++++++++++++++++ targets/eds/js/eds_results.js | 26 +- targets/eds/roblib_search_eds.info | 5 +- targets/eds/roblib_search_eds.module | 73 +- 7 files changed, 1843 insertions(+), 37 deletions(-) create mode 100644 targets/eds/includes/rest/Config.xml create mode 100755 targets/eds/includes/rest/EBSCOAPI.php create mode 100755 targets/eds/includes/rest/EBSCOConnector.php create mode 100755 targets/eds/includes/rest/EBSCOResponse.php diff --git a/targets/eds/includes/rest/Config.xml b/targets/eds/includes/rest/Config.xml new file mode 100644 index 0000000..507f5f7 --- /dev/null +++ b/targets/eds/includes/rest/Config.xml @@ -0,0 +1,24 @@ + + + EDS API-PHP Demo 1.4 + + http://eds-api.ebscohost.com/edsapi/rest + https://eds-api.ebscohost.com/Authservice/rest + + 300 + + + insert client side profile + insert client side profile + + + + + + guest + uprince + robertson1 + edsapi + + + diff --git a/targets/eds/includes/rest/EBSCOAPI.php b/targets/eds/includes/rest/EBSCOAPI.php new file mode 100755 index 0000000..2d19fe1 --- /dev/null +++ b/targets/eds/includes/rest/EBSCOAPI.php @@ -0,0 +1,391 @@ +config = $config; + $this->connector = new EBSCOConnector($config); + } + + /** + * Create a new EBSCOConnector object or reuse an existing one + * + * @param none + * + * @return EBSCOConnector object + * @access public + */ + public function connector($config) + { + if (empty($this->connector)) { + $this->connector = new EBSCOConnector($config); + } + + return $this->connector; + } + + + /** + * Create a new EBSCOResponse object + * + * @param object $response + * + * @return EBSCOResponse object + * @access public + */ + public function response($response) + { + $responseObj = new EBSCOResponse($response); + return $responseObj; + } + + + /** + * Request authentication and session tokens, then send the API request. + * Retry the request if authentication errors occur + * + * @param string $action The EBSCOConnector method name + * @param array $params The parameters for the HTTP request + * @param integer $attempts The number of retries. The default number is 3 but can be increased. + * 3 retries can handle a situation when both autentication and session tokens need to be refreshed + the current API call + * + * @return array An associative array with results. + * @access protected + */ + protected function request($action, $params = null, $attempts = 3) + { + try { + + $authenticationToken = $this->getAuthToken(); + $sessionToken = $this->getSessionToken($authenticationToken); + + if(empty($authenticationToken)){ + $authenticationToken = $this -> getAuthToken(); + } + + if(empty($sessionToken)){ + $sessionToken = $this -> getSessionToken($authenticationToken,'y'); + } + + $headers = array( + 'x-authenticationToken: ' . $authenticationToken, + 'x-sessionToken: ' . $sessionToken + ); + + $response = call_user_func_array(array($this->connector($this->config), "request{$action}"), array($params, $headers)); + $result = $this->response($response)->result(); + $results = $result; + return $results; + } catch(EBSCOException $e) { + try { + // Retry the request if there were authentication errors + $code = $e->getCode(); + switch ($code) { + case EBSCOConnector::EDS_AUTH_TOKEN_INVALID: + $authenticationToken = $this->getAuthToken(); + $sessionToken = $this ->getSessionToken($authenticationToken); + $headers = array( + 'x-authenticationToken: ' . $authenticationToken, + 'x-sessionToken: ' . $sessionToken + ); + if ($attempts > 0) { + return $this->request($action, $params, $headers, --$attempts); + } + break; + case EBSCOConnector::EDS_SESSION_TOKEN_INVALID: + $sessionToken = $this ->getSessionToken($authenticationToken,'y'); + $headers = array( + 'x-authenticationToken: ' . $authenticationToken, + 'x-sessionToken: ' . $sessionToken + ); + if ($attempts > 0) { + return $this->request($action, $params, $headers, --$attempts); + } + break; + default: + $result = array( + 'error' => $e->getMessage() + ); + return $result; + break; + } + } catch(Exception $e) { + $result = array( + 'error' => $e->getMessage() + ); + return $result; + } + } catch(Exception $e) { + $result = array( + 'error' => $e->getMessage() + ); + return $result; + } + } + + private function writeLockFile(){ + $tokenFile = fopen("edstoken.txt","w+"); + $result = $this->apiAuthenticationToken(); + fwrite($tokenFile, $result['authenticationToken']."\n"); + fwrite($tokenFile, $result['authenticationTimeout']."\n"); + fwrite($tokenFile, $result['authenticationTimeStamp']); + fclose($tokenFile); + return $result['authenticationToken']; + } + + /* + * Get authentication token from appication scop + * Check authToen's expiration + * if expired get a new authToken and re-new the time stamp + * + * @param none + * + * @access public + */ + public function getAuthToken(){ + $lockFile = fopen("edslock.txt","r"); + $tokenFile =fopen("edstoken.txt","r"); + if(empty($lockFile) || empty($tokenFile)){ + return $this->writeLockFile(); + } + while(!feof($tokenFile)){ + $authToken = rtrim(fgets($tokenFile),"\n"); + $timeout = fgets($tokenFile)-600; + $timestamp = fgets($tokenFile); + } + fclose($tokenFile); + if(time()-$timestamp>=$timeout){ + // Lock check. + if(flock($lockFile, LOCK_EX)){ + $tokenFile = fopen("edstoken.txt","w+"); + $result = $this->apiAuthenticationToken(); + fwrite($tokenFile, $result['authenticationToken']."\n"); + fwrite($tokenFile, $result['authenticationTimeout']."\n"); + fwrite($tokenFile, $result['authenticationTimeStamp']); + fclose($tokenFile); + return $result['authenticationToken']; + }else{ + return $authToken; + } + }else{ + return $authToken; + } + fclose($lockFile); + } + + /** + * Wrapper for authentication API call + * + * @param none + * + * @access public + */ + public function apiAuthenticationToken() + { + $response = $this->connector->requestAuthenticationToken(); + $result = $this->response($response)->result(); + return $result; + } + + /** + * Get session token for a profile + * If session token is not available + * a new session token will be generated + * + * @param Authentication token, Profile + * @access public + */ + public function getSessionToken($authenToken, $invalid='n'){ + $token = ''; + + // Check user's login status + if(isset($_COOKIE['login'])){ + if($invalid=='y'){ + $profile = $_SESSION['sessionToken']['profile']; + $sessionToken = $this->apiSessionToken($authenToken, $profile,'n'); + $_SESSION['sessionToken']=$sessionToken; + } + $token = $_SESSION['sessionToken']['sessionToken']; + } + else if(isset($_COOKIE['Guest'])){ + if($invalid=='y'){ + $profile = $_SESSION['sessionToken']['profile']; + $sessionToken = $this->apiSessionToken($authenToken, $profile,'y'); + $_SESSION['sessionToken']=$sessionToken; + } + $token = $_SESSION['sessionToken']['sessionToken']; + }else{ + //$xml ="Config.xml"; + //$dom = new DOMDocument(); + //$dom->load($xml); + //$EDSCredentials = $dom ->getElementsByTagName('EDSCredentials')->item(0); + //$users = $EDSCredentials -> getElementsByTagName('User'); + $profileId = $this->config['profile']; + //foreach($users as $user){ + // $userType = $user->getElementsByTagName('ClientUser')->item(0)->nodeValue; + // if($userType == 'guest'){ + // $profileId = $user -> getElementsByTagName('EDSProfile')->item(0)->nodeValue; + // break; + // } + //} + $sessionToken = $this->apiSessionToken($authenToken, $profileId,'y'); + $_SESSION['profile'] = $profileId; + $_SESSION['sessionToken']=$sessionToken; + setcookie("Guest", $profileId, 0); + $token = $sessionToken['sessionToken']; + } + return $token; + } + + /** + * Wrapper for session API call + * + * @param Authentication token + * + * @access public + */ + public function apiSessionToken($authenToken, $profile, $guest) + { + // Add authentication tokens to headers + $headers = array( + 'x-authenticationToken: ' . $authenToken + ); + + $response = $this->connector($this->config)->requestSessionToken($headers, $profile,$guest); + $result = $this->response($response)->result(); + $token = array( + 'sessionToken'=>$result, + 'profile' => $profile + ); + return $token; + } + + /** + * Wrapper for end session API call + * + * @param Authentication token + * + * @access public + */ + public function apiEndSessionToken($authenToken, $sessionToken){ + + // Add authentication tokens to headers + $headers = array( + 'x-authenticationToken: '.$authenToken + ); + + $this -> connector($this->config)->requestEndSessionToken($headers, $sessionToken); + } + + /** + * Wrapper for search API call + * + * @param + * + * @throws object PEAR Error + * @return array An array of query results + * @access public + */ + public function apiSearch($params) { + + $results = $this->request('Search', $params); + return $results; + } + + + /** + * Wrapper for retrieve API call + * + * @param array $an The accession number + * @param string $start The short database name + * + * @throws object PEAR Error + * @return array An associative array of data + * @access public + */ + public function apiRetrieve($an, $db, $term) + { + // Add the HTTP query params + $params = array( + 'an' => $an, + 'dbid' => $db, + 'highlightterms' => $term // Get currect param name + ); + $params = http_build_query($params); + $result = $this->request('Retrieve', $params); + return $result; + } + + + /** + * Wrapper for info API call + * + * @return array An associative array of data + * @access public + */ + public function getInfo() + { + if(isset($_SESSION['info'])){ + $InfoArray = $_SESSION['info']; + $timestamp = $InfoArray['timestamp']; + if(time()-$timestamp>=3600){ + // Get new Info for the profile + $InfoArray = $this->apiInfo(); + $_SESSION['info'] = $InfoArray; + $info = $InfoArray['Info']; + }else{ + $info = $InfoArray['Info']; + } + }else{ + // Get new Info for the profile + $InfoArray = $this->apiInfo(); + $_SESSION['info'] = $InfoArray; + $info = $InfoArray['Info']; + } + return $info; + } + + public function apiInfo(){ + + $response = $this->request('Info',''); + $Info = array( + 'Info' => $response, + 'timestamp'=>time() + ); + return $Info; + } +} +?> \ No newline at end of file diff --git a/targets/eds/includes/rest/EBSCOConnector.php b/targets/eds/includes/rest/EBSCOConnector.php new file mode 100755 index 0000000..f51e7c4 --- /dev/null +++ b/targets/eds/includes/rest/EBSCOConnector.php @@ -0,0 +1,379 @@ +end_point = $config['rest_url']; + $this->authentication_end_point = $config['auth_url']; + + $this->userId = $config['user']; + $this->password = $config['pass']; + $this->profiled = $config['profile']; + $this->interfaceId = ''; + $this->orgId = ''; + } + + /** + * Request the authentication token + * + * @param none + * + * @return string .The authentication token + * @access public + */ + public function requestAuthenticationToken() { + $url = $this->authentication_end_point . '/UIDAuth'; +// Add the body of the request. Important. + $params = << + {$this->userId} + {$this->password} + {$this->interfaceId} + +BODY; + +// Set the content type to 'application/xml'. Important, otherwise cURL will use the usual POST content type. + $headers = array( + 'Content-Type: application/xml', + 'Conent-Length: ' . strlen($params) + ); + + $response = $this->request($url, $params, $headers, 'POST'); + return $response; + } + + /** + * Request the session token + * + * @param array $headers Authentication token + * + * @return string .The session token + * @access public + */ + public function requestSessionToken($headers, $profile, $guest) { + $url = $this->end_point . '/CreateSession'; + +// Add the HTTP query parameters + $params = array( + 'profile' => $profile, + 'org' => $this->orgId, + 'guest' => $guest + ); + $params = http_build_query($params); + $response = $this->request($url, $params, $headers); + return $response; + } + + /** + * End the session token + * + * @param array $headers Session token + * @access public + */ + public function requestEndSessionToken($headers, $sessionToken) { + $url = $this->end_point . '/endsession'; + +// Add the HTTP query parameters + $params = array( + 'sessiontoken' => $sessionToken + ); + $params = http_build_query($params); + $this->request($url, $params, $headers); + } + + public function getProfiled(){ + return $profiled; + } + + /** + * Request the search records + * + * @param array $params Search specific parameters + * @param array $headers Authentication and session tokens + * + * @return array An associative array of data + * @access public + */ + public function requestSearch($params, $headers) { + $url = $this->end_point . '/Search'; + + $response = $this->request($url, $params, $headers); + return $response; + } + + /** + * Request a specific record + * + * @param array $params Retrieve specific parameters + * @param array $headers Authentication and session tokens + * + * @return array An associative array of data + * @access public + */ + public function requestRetrieve($params, $headers) { + $url = $this->end_point . '/Retrieve'; + + $response = $this->request($url, $params, $headers); + return $response; + } + + /** + * Request the info data + * + * @param null $params Not used + * @param array $headers Authentication and session tokens + * + * @return array An associative array of data + * @access public + */ + public function requestInfo($params, $headers) { + $url = $this->end_point . '/Info'; + + $response = $this->request($url, $params, $headers); + return $response; + } + + /** + * Send an HTTP request and inspect the response + * + * @param string $url The url of the HTTP request + * @param array $params The parameters of the HTTP request + * @param array $headers The headers of the HTTP request + * @param array $body The body of the HTTP request + * @param string $method The HTTP method, default is 'GET' + * + * @return object SimpleXml + * @access protected + */ + protected function request($url, $params = null, $headers = null, $method = 'GET') { + $log = fopen('curl.log', 'w'); // for debugging cURL + $xml = false; +//$return = false; +// Create a cURL instance + $ch = curl_init(); + +// Set the cURL options + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); + curl_setopt($ch, CURLOPT_VERBOSE, true); + curl_setopt($ch, CURLOPT_STDERR, $log); // for debugging cURL + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Termporary +// Set the query parameters and the url + if (empty($params)) { +// Only Info request has empty parameters + curl_setopt($ch, CURLOPT_URL, $url); + } + else { +// GET method + if ($method == 'GET') { + $url .= '?' . $params; + curl_setopt($ch, CURLOPT_URL, $url); +// POST method + } + else { + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POSTFIELDS, $params); + } + } + +// Set the header + if (!empty($headers)) { + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + } + +// Send the request + $response = curl_exec($ch); + +//Save XML file for debug mode + if (strstr($url, 'Search')) { + $_SESSION['resultxml'] = $response; + } + if (strstr($url, 'Retrieve')) { + $_SESSION['recordxml'] = $response; + } +// Parse the response +// In case of errors, throw 2 type of exceptions +// EBSCOException if the API returned an error message +// Exception in all other cases. Should be improved for better handling + if ($response === false) { + fclose($log); // for debugging cURL + throw new Exception(curl_error($ch)); + curl_close($ch); + } + else { + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + fclose($log); // for debugging cURL + curl_close($ch); + switch ($code) { + case self::HTTP_OK: + $xml = simplexml_load_string($response); + if ($xml === false) { + throw new Exception('Error while parsing the response.'); + } + else { + return $xml; + } + break; + case self::HTTP_BAD_REQUEST: + $xml = @simplexml_load_string($response); + if ($xml === false) { + throw new Exception('Error while parsing the response.'); + } + else { +// If the response is an API error + $error = ''; + $code = 0; + $isError = isset($xml->ErrorNumber) || isset($xml->ErrorCode); + if ($isError) { + if (isset($xml->DetailedErrorDescription) && !empty($xml->DetailedErrorDescription)) { + $error = (string) $xml->DetailedErrorDescription; + } + else if (isset($xml->ErrorDescription)) { + $error = (string) $xml->ErrorDescription; + } + else if (isset($xml->Reason)) { + $error = (string) $xml->Reason; + } + if (isset($xml->ErrorNumber)) { + $code = (integer) $xml->ErrorNumber; + } + else if (isset($xml->ErrorCode)) { + $code = (integer) $xml->ErrorCode; + } + throw new EBSCOException($error, $code); + } + else { + throw new Exception('The request could not be understood by the server + due to malformed syntax. Modify your search before retrying.'); + } + } + break; + case self::HTTP_NOT_FOUND: + throw new Exception('The resource you are looking for might have been removed, + had its name changed, or is temporarily unavailable.'); + break; + case self::HTTP_INTERNAL_SERVER_ERROR: + throw new Exception('The server encountered an unexpected condition which prevented + it from fulfilling the request.'); + break; +// Other HTTP status codes + default: + throw new Exception('Unexpected HTTP error.'); + break; + } + } + } + +} + +?> \ No newline at end of file diff --git a/targets/eds/includes/rest/EBSCOResponse.php b/targets/eds/includes/rest/EBSCOResponse.php new file mode 100755 index 0000000..22debc1 --- /dev/null +++ b/targets/eds/includes/rest/EBSCOResponse.php @@ -0,0 +1,982 @@ +response = $response; + } + + + /** + * A proxy method which decides which subsequent method should be * called, based on the SimpleXml object structure + * + * @param none + * + * @return array An associative array of data or the SimpleXml object itself in case of API error messages + * @access public + */ + public function result() + { + // If there is an ErrorNumber tag then return the object itself. + // Should not happen, this method is called after parsing the SimpleXml for API errors + if(!empty($this->response->ErrorNumber)) { + return $this->response; + } else { + if (!empty($this->response->AuthToken)) { + return $this->buildAuthenticationToken(); + } else if (!empty($this->response->SessionToken)) { + return (string)$this->buildSessionToken(); + } else if (!empty($this->response->SearchResult)) { + return $this->buildSearch(); + } else if(!empty($this->response->Record)) { + return $this->buildRetrieve(); + } else if(!empty($this->response->AvailableSearchCriteria)) { + return $this->buildInfo(); + } + } + } + + + /** + * Parse the SimpleXml object when an AuthenticationToken API call was executed + * + * @param none + * + * @return array An associative array of data + * @access private + */ + private function buildAuthenticationToken() + { + $token = (string) $this->response->AuthToken; + $timeout = (integer) $this->response->AuthTimeout; + + $result = array( + 'authenticationToken' => $token, + 'authenticationTimeout' => $timeout, + 'authenticationTimeStamp'=> time() + ); + return $result; + } + /** + * Parse the SimpleXml object when an SessionToken API call was executed + * + * @param none + * + * @return sessionToken + * @access private + */ + private function buildSessionToken() + { + $sessionToken = (string) $this->response->SessionToken; + return $sessionToken; + } + + /** + * Parse the SimpleXml object when a Search API call was executed + * + * @param none + * + * @return array An associative array of data + * @access private + */ + private function buildSearch() + { + $hits = (integer) $this->response->SearchResult->Statistics->TotalHits; + $queryString = (string)$this->response->SearchRequestGet->QueryString; + $records = array(); + $facets = array(); + $queries = array(); + $appliedFacets = array(); + $appliedLimiters = array(); + $appliedExpanders = array(); + + if($this->response->SearchRequestGet->SearchCriteriaWithActions->QueriesWithAction){ + $queriesWithAction = $this->response->SearchRequestGet->SearchCriteriaWithActions->QueriesWithAction->QueryWithAction; + foreach($queriesWithAction as $queryWithAction){ + $queries[]=array( + 'query' => (string)$queryWithAction->Query->Term, + 'removeAction'=> (string) $queryWithAction->RemoveAction + ); + } + } + + if($this->response->SearchRequestGet->SearchCriteriaWithActions->FacetFiltersWithAction){ + $facetFiltersWithAction = $this->response->SearchRequestGet->SearchCriteriaWithActions->FacetFiltersWithAction->FacetFilterWithAction; + foreach($facetFiltersWithAction as $facetFilterWithAction){ + $facetValue = array(); + foreach($facetFilterWithAction->FacetValuesWithAction->FacetValueWithAction as $facetValueWithAction){ + $facetValue[] = array( + 'Id' => (string)$facetValueWithAction->FacetValue->Id, + 'value'=>(string)$facetValueWithAction->FacetValue->Value, + 'removeAction'=>(string)$facetValueWithAction->RemoveAction + ); + } + + $appliedFacets[] = array( + 'filterId' => (string)$facetFilterWithAction->FilterId, + 'facetValue'=> $facetValue, + 'removeAction'=> (string)$facetFilterWithAction->RemoveAction + ); + } + } + + if($this->response->SearchRequestGet->SearchCriteriaWithActions->LimitersWithAction){ + $limitersWithAction = $this->response->SearchRequestGet->SearchCriteriaWithActions->LimitersWithAction->LimiterWithAction; + foreach($limitersWithAction as $limiterWithAction){ + $limiterValue = array( + 'value' => (string) $limiterWithAction->LimiterValuesWithAction->LimiterValueWithAction->Value, + 'removeAction'=> (string) $limiterWithAction->LimiterValuesWithAction->LimiterValueWithAction->RemoveAction + ); + $appliedLimiters[] = array( + 'Id' => (string)$limiterWithAction->Id, + 'limiterValue'=>$limiterValue, + 'removeAction'=> (string) $limiterWithAction->RemoveAction + ); + } + } + + if($this->response->SearchRequestGet->SearchCriteriaWithActions->ExpandersWithAction){ + $expandersWithAction = $this->response->SearchRequestGet->SearchCriteriaWithActions->ExpandersWithAction->ExpanderWithAction; + foreach($expandersWithAction as $expanderWithAction){ + $appliedExpanders[] = array( + 'Id' => (string)$expanderWithAction->Id, + 'removeAction'=>(string)$expanderWithAction->RemoveAction + ); + } + } + + if ($hits > 0) { + $records = $this->buildRecords(); + $facets = $this->buildFacets(); + } + + $results = array( + 'recordCount' => $hits, + 'queryString' => $queryString, + 'queries' => $queries, + 'appliedFacets'=>$appliedFacets, + 'appliedLimiters'=>$appliedLimiters, + 'appliedExpanders'=>$appliedExpanders, + 'records' => $records, + 'facets' => $facets + ); + + return $results; + } + + + /** + * Parse the SimpleXml object when a Search API call was executed + * and find all records + * + * @param none + * + * @return array An associative array of data + * @access private + */ +private function buildRecords() + { + $results = array(); + + $records = $this->response->SearchResult->Data->Records->Record; + foreach ($records as $record) { + $result = array(); + $result['AccessLevel'] = $record->Header -> AccessLevel?(string)$record->Header -> AccessLevel:''; + $result['pubType'] = $record -> Header-> PubType? (string)$record -> Header-> PubType:''; + $result['PubTypeId']=$record->Header->PubTypeId? (string) $record->Header->PubTypeId:''; + $result['ResultId'] = $record->ResultId ? (integer) $record->ResultId : ''; + $result['DbId'] = $record->Header->DbId ? (string) $record->Header->DbId : ''; + $result['DbLabel'] = $record->Header->DbLabel ? (string) $record->Header->DbLabel:''; + $result['An'] = $record->Header->An ? (string) $record->Header->An : ''; + $result['PLink'] = $record->PLink ? (string) $record->PLink : ''; + $result['PDF'] = $record->FullText->Links ? (string) $record->FullText->Links->Link->Type : ''; + $result['HTML'] = $record->FullText->Text->Availability? (string) $record->FullText->Text->Availability : ''; + if (!empty($record->ImageInfo->CoverArt)) { + foreach ($record->ImageInfo->CoverArt as $image) { + $size = (string) $image->Size; + $target = (string) $image->Target; + $result['ImageInfo'][$size] = $target; + } + } else { + $result['ImageInfo'] = ''; + } + + $result['FullText'] = $record->FullText ? (string) $record->FullText : ''; + + if ($record->CustomLinks) { + $result['CustomLinks'] = array(); + foreach ($record->CustomLinks->CustomLink as $customLink) { + $category = $customLink->Category ? (string) $customLink->Category : ''; + $icon = $customLink->Icon ? (string) $customLink->Icon : ''; + $mouseOverText = $customLink->MouseOverText ? (string) $customLink->MouseOverText : ''; + $name = $customLink->Name ? (string) $customLink->Name : ''; + $text = $customLink->Text ? (string) $customLink->Text : ''; + $url = $customLink->Url ? (string) $customLink->Url : ''; + $result['CustomLinks'][] = array( + 'Category' => $category, + 'Icon' => $icon, + 'MouseOverText' => $mouseOverText, + 'Name' => $name, + 'Text' => $text, + 'Url' => $url + ); + } + } + + if ($record->FullText->CustomLinks) { + $result['FullTextCustomLinks'] = array(); + foreach ($record->FullText->CustomLinks->CustomLink as $customLink) { + $category = $customLink->Category ? (string) $customLink->Category : ''; + $icon = $customLink->Icon ? (string) $customLink->Icon : ''; + $mouseOverText = $customLink->MouseOverText ? (string) $customLink->MouseOverText : ''; + $name = $customLink->Name ? (string) $customLink->Name : ''; + $text = $customLink->Text ? (string) $customLink->Text : ''; + $url = $customLink->Url ? (string) $customLink->Url : ''; + $result['CustomLinks'][] = array( + 'Category' => $category, + 'Icon' => $icon, + 'MouseOverText' => $mouseOverText, + 'Name' => $name, + 'Text' => $text, + 'Url' => $url + ); + } + } + + if($record->Items) { + $result['Items'] = array(); + foreach ($record->Items->Item as $item) { + $label = $item->Label ? (string) $item->Label : ''; + $group = $item->Group ? (string) $item->Group : ''; + $data = $item->Data ? (string) $item->Data : ''; + $result['Items'][$group][] = array( + 'Label' => $label, + 'Group' => $group, + 'Data' => $this->toHTML($data, $group) + ); + } + } + + if($record->RecordInfo){ + $result['RecordInfo'] = array(); + $result['RecordInfo']['BibEntity']=array( + 'Identifiers'=>array(), + 'Languages'=>array(), + 'PhysicalDescription'=>array(), + 'Subjects'=>array(), + 'Titles'=>array() + ); + + if($record->RecordInfo->BibRecord->BibEntity->Identifiers){ + foreach($record->RecordInfo->BibRecord->BibEntity->Identifiers->Identifier as $identifier){ + $type = $identifier->Type? (string) $identifier->Type:''; + $value = $identifier->Value? (string) $identifier->Value:''; + $result['RecordInfo']['BibEntity']['Identifiers'][]= array( + 'Type'=>$type, + 'Value'=>$value + ); + } + } + + if($record->RecordInfo->BibRecord->BibEntity->Languages){ + foreach($record->RecordInfo->BibRecord->BibEntity->Languages->Language as $language){ + $code = $language->Code? (string)$language->Code:''; + $text = $language->Text? (string)$language->Text:''; + $result['RecordInfo']['BibEntity']['Languages'][]= array( + 'Code'=>$code, + 'Text'=>$text + ); + } + } + + if($record->RecordInfo->BibRecord->BibEntity->PhysicalDescription){ + $pageCount = $record->RecordInfo->BibRecord->BibEntity->PhysicalDescription->Pagination->PageCount? (string) $record->RecordInfo->BibRecord->BibEntity->PhysicalDescription->Pagination->PageCount:''; + $startPage = $record->RecordInfo->BibRecord->BibEntity->PhysicalDescription->Pagination->StartPage? (string) $record->RecordInfo->BibRecord->BibEntity->PhysicalDescription->Pagination->StartPage:''; + $result['RecordInfo']['BibEntity']['PhysicalDescription']['Pagination'] = $pageCount; + $result['RecordInfo']['BibEntity']['PhysicalDescription']['StartPage'] = $startPage; + } + + if($record->RecordInfo->BibRecord->BibEntity->Subjects){ + foreach($record->RecordInfo->BibRecord->BibEntity->Subjects->Subject as $subject){ + $subjectFull = $subject->SubjectFull? (string)$subject->SubjectFull:''; + $type = $subject->Type? (string)$subject->Type:''; + $result['RecordInfo']['BibEntity']['Subjects'][]=array( + 'SubjectFull'=>$subjectFull, + 'Type'=>$type + ); + } + } + + if($record->RecordInfo->BibRecord->BibEntity->Titles){ + foreach($record->RecordInfo->BibRecord->BibEntity->Titles->Title as $title){ + $titleFull = $title->TitleFull? (string)$title->TitleFull:''; + $type = $title->Type? (string)$title->Type:''; + $result['RecordInfo']['BibEntity']['Titles'][]=array( + 'TitleFull'=>$titleFull, + 'Type'=>$type + ); + } + } + + $result['RecordInfo']['BibRelationships']=array( + 'HasContributorRelationships'=>array(), + 'IsPartOfRelationships'=>array() + ); + + if($record->RecordInfo->BibRecord->BibRelationships->HasContributorRelationships){ + foreach($record->RecordInfo->BibRecord->BibRelationships->HasContributorRelationships->HasContributor as $contributor){ + $nameFull = $contributor->PersonEntity->Name->NameFull? (string)$contributor->PersonEntity->Name->NameFull:''; + $result['RecordInfo']['BibRelationships']['HasContributorRelationships'][]=array( + 'NameFull'=>$nameFull + ); + } + } + + if($record->RecordInfo->BibRecord->BibRelationships){ + foreach($record->RecordInfo->BibRecord->BibRelationships->IsPartOfRelationships->IsPartOf as $relationship){ + if($relationship->BibEntity->Dates){ + foreach($relationship->BibEntity->Dates->Date as $date){ + $d = $date->D? (string)$date->D:''; + $m = $date->M? (string)$date->M:''; + $type = $date->Type? (string)$date->Type:''; + $y = $date->Y? (string)$date->Y:''; + $result['RecordInfo']['BibRelationships']['IsPartOfRelationships']['date'][] = array( + 'D'=> $d, + 'M'=>$m, + 'Type'=>$type, + 'Y'=>$y + ); + } + } + + if($relationship->BibEntity->Identifiers){ + foreach($relationship->BibEntity->Identifiers->Identifier as $identifier){ + $type = $identifier->Type? (string) $identifier->Type :''; + $value = $identifier->Value? (string) $identifier->Value:''; + $result['RecordInfo']['BibRelationships']['IsPartOfRelationships']['Identifiers'][]=array( + 'Type'=>$type, + 'Value'=>$value + ); + } + } + + if($relationship->BibEntity->Titles){ + foreach($relationship->BibEntity->Titles->Title as $title){ + $titleFull = $title->TitleFull? (string)$title->TitleFull:''; + $type = $title->Type? (string)$title->Type:''; + $result['RecordInfo']['BibRelationships']['IsPartOfRelationships']['Titles'][]=array( + 'TitleFull' => $titleFull, + 'Type'=>$type + ); + } + } + + if($relationship->BibEntity->Numbering){ + foreach($relationship->BibEntity->Numbering->Number as $number){ + $type = (string)$number->Type; + $value= (string)$number->Value; + $result['RecordInfo']['BibRelationships']['IsPartOfRelationships']['numbering'][] = array( + 'Type'=> $type, + 'Value'=>$value + ); + } + } + } + } + } + + $results[] = $result; + } + + return $results; + } + + + /** + * Parse the SimpleXml object when a Search API call was executed + * and find all facets + * + * @param none + * + * @return array An associative array of data + * @access private + */ + private function buildFacets() + { + $results = array(); + + if($this->response->SearchResult->AvailableFacets){ + $facets = $this->response->SearchResult->AvailableFacets->AvailableFacet; + foreach ($facets as $facet) { + $values = array(); + foreach ($facet->AvailableFacetValues->AvailableFacetValue as $value) { + + $values[] = array( + 'Value' => (string) $value->Value, + 'Action' => (string) $value->AddAction, + 'Count' => (string) $value->Count + ); + } + $id = (string) $facet->Id; + $label = (string) $facet->Label; + $results[] = array( + 'Id' => $id, + 'Label' => $label, + 'Values' => $values + ); + } + } + + return $results; + } + + + /** + * Parse the SimpleXml object when an Info API call was executed + * + * @param none + * + * @return array An associative array of data + * @access private + */ + private function buildInfo() + { + // Sort options + $sort = array(); + foreach ($this->response->AvailableSearchCriteria->AvailableSorts->AvailableSort as $element) { + $sort[] = array( + 'Id' => (string) $element->Id, + 'Label' => (string) $element->Label, + 'Action' => (string) $element->AddAction + ); + } + + // Search fields + $search = array(); + foreach ($this->response->AvailableSearchCriteria->AvailableSearchFields->AvailableSearchField as $element) { + $search[] = array( + 'Label' => (string) $element->Label, + 'Code' => (string) $element->FieldCode + ); + } + + // Expanders + $expanders = array(); + foreach ($this->response->AvailableSearchCriteria->AvailableExpanders->AvailableExpander as $element) { + $expanders[] = array( + 'Id' => (string) $element->Id, + 'Label' => (string) $element->Label, + 'Action' => (string) $element->AddAction + ); + } + + // Limiters + $limiters = array(); + foreach ($this->response->AvailableSearchCriteria->AvailableLimiters->AvailableLimiter as $element) { + $values = array(); + if ($element->LimiterValues) { + $items = $element->LimiterValues->LimiterValue; + foreach($items as $item) { + $values[] = array( + 'Value' => (string) $item->Value, + 'Action' => (string) $item->AddAction + ); + } + } + $limiters[] = array( + 'Id' => (string) $element->Id, + 'Label' => (string) $element->Label, + 'Action' => (string) $element->AddAction, + 'Type' => (string) $element->Type, + 'values' => $values + ); + } + + $result = array( + 'sort' => $sort, + 'search' => $search, + 'expanders' => $expanders, + 'limiters' => $limiters + ); + + return $result; + } + + + /** + * Parse a SimpleXml object when a Retrieve API call was executed + * + * @param none + * + * @return array An associative array of data + * @access private + */ + private function buildRetrieve() + { + $record = $this->response->Record; + + if ($record) { + $record = $record[0]; // there is only one record + } + + $result = array(); + $result['AccessLevel'] = $record->Header -> AccessLevel?(string)$record->Header -> AccessLevel:''; + $result['pubType'] = $record -> Header-> PubType? (string)$record -> Header-> PubType:''; + $result['PubTypeId']=$record->Header->PubTypeId? (string) $record->Header->PubTypeId:''; + $result['DbId'] = $record->Header->DbId ? (string) $record->Header->DbId : ''; + $result['DbLabel'] = $record->Header->DbLabel ? (string) $record->Header->DbLabel:''; + $result['An'] = $record->Header->An ? (string) $record->Header->An : ''; + $result['PLink'] = $record->PLink ? (string) $record->PLink : ''; + $result['pdflink'] = $record->FullText->Links ? (string) $record->FullText->Links->Link->Url : ''; + $result['PDF'] = $record->FullText->Links ? (string) $record->FullText->Links->Link->Type : ''; + $value = $record->FullText->Text->Value ? (string) $record->FullText->Text->Value : ''; + $result['htmllink'] = $this->toHTML($value,$group = ''); + $result['HTML'] = $record->FullText->Text->Availability? (string) $record->FullText->Text->Availability : ''; + if (!empty($record->ImageInfo->CoverArt)) { + foreach ($record->ImageInfo->CoverArt as $image) { + $size = (string) $image->Size; + $target = (string) $image->Target; + $result['ImageInfo'][$size] = $target; + } + } else { + $result['ImageInfo'] = ''; + } + $result['FullText'] = $record->FullText ? (string) $record->FullText : ''; + + if ($record->CustomLinks) { + $result['CustomLinks'] = array(); + foreach ($record->CustomLinks->CustomLink as $customLink) { + $category = $customLink->Category ? (string) $customLink->Category : ''; + $icon = $customLink->Icon ? (string) $customLink->Icon : ''; + $mouseOverText = $customLink->MouseOverText ? (string) $customLink->MouseOverText : ''; + $name = $customLink->Name ? (string) $customLink->Name : ''; + $text = $customLink->Text ? (string) $customLink->Text : ''; + $url = $customLink->Url ? (string) $customLink->Url : ''; + $result['CustomLinks'][] = array( + 'Category' => $category, + 'Icon' => $icon, + 'MouseOverText' => $mouseOverText, + 'Name' => $name, + 'Text' => $text, + 'Url' => $url + ); + } + } + + if ($record->FullText->CustomLinks) { + $result['FullTextCustomLinks'] = array(); + foreach ($record->FullText->CustomLinks->CustomLink as $customLink) { + $category = $customLink->Category ? (string) $customLink->Category : ''; + $icon = $customLink->Icon ? (string) $customLink->Icon : ''; + $mouseOverText = $customLink->MouseOverText ? (string) $customLink->MouseOverText : ''; + $name = $customLink->Name ? (string) $customLink->Name : ''; + $text = $customLink->Text ? (string) $customLink->Text : ''; + $url = $customLink->Url ? (string) $customLink->Url : ''; + $result['CustomLinks'][] = array( + 'Category' => $category, + 'Icon' => $icon, + 'MouseOverText' => $mouseOverText, + 'Name' => $name, + 'Text' => $text, + 'Url' => $url + ); + } + } + + if($record->Items) { + $result['Items'] = array(); + foreach ($record->Items->Item as $item) { + $label = $item->Label ? (string) $item->Label : ''; + $group = $item->Group ? (string) $item->Group : ''; + $data = $item->Data ? (string) $item->Data : ''; + $result['Items'][] = array( + 'Label' => $label, + 'Group' => $group, + 'Data' => $this->retrieveHTML($data, $group) + ); + } + } + + if($record->RecordInfo){ + $result['RecordInfo'] = array(); + $result['RecordInfo']['BibEntity']=array( + 'Identifiers'=>array(), + 'Languages'=>array(), + 'PhysicalDescription'=>array(), + 'Subjects'=>array(), + 'Titles'=>array() + ); + + if($record->RecordInfo->BibRecord->BibEntity->Identifiers){ + foreach($record->RecordInfo->BibRecord->BibEntity->Identifiers->Identfier as $identifier){ + $type = $identifier->Type? (string) $identifier->Type:''; + $value = $identifier->Value? (string) $identifier->Value:''; + $result['RecordInfo']['BibEntity']['Identifiers'][]= array( + 'Type'=>$type, + 'Value'=>$value + ); + } + } + + if($record->RecordInfo->BibRecord->BibEntity->Languages){ + foreach($record->RecordInfo->BibRecord->BibEntity->Languages->Language as $language){ + $code = $language->Code? (string)$language->Code:''; + $text = $language->Text? (string)$language->Text:''; + $result['RecordInfo']['BibEntity']['Languages'][]= array( + 'Code'=>$code, + 'Text'=>$text + ); + } + } + + if($record->RecordInfo->BibRecord->BibEntity->PhysicalDescription){ + $pageCount = $record->RecordInfo->BibRecord->BibEntity->PhysicalDescription->Pagination->PageCount? (string) $record->RecordInfo->BibRecord->BibEntity->PhysicalDescription->Pagination->PageCount:''; + $startPage = $record->RecordInfo->BibRecord->BibEntity->PhysicalDescription->Pagination->StartPage? (string) $record->RecordInfo->BibRecord->BibEntity->PhysicalDescription->Pagination->StartPage:''; + $result['RecordInfo']['BibEntity']['PhysicalDescription']['Pagination'] = $pageCount; + $result['RecordInfo']['BibEntity']['PhysicalDescription']['StartPage'] = $startPage; + } + + if($record->RecordInfo->BibRecord->BibEntity->Subjects){ + foreach($record->RecordInfo->BibRecord->BibEntity->Subjects->Subject as $subject){ + $subjectFull = $subject->SubjectFull? (string)$subject->SubjectFull:''; + $type = $subject->Type? (string)$subject->Type:''; + $result['RecordInfo']['BibEntity']['Subjects'][]=array( + 'SubjectFull'=>$subjectFull, + 'Type'=>$type + ); + } + } + + if($record->RecordInfo->BibRecord->BibEntity->Titles){ + foreach($record->RecordInfo->BibRecord->BibEntity->Titles->Title as $title){ + $titleFull = $title->TitleFull? (string)$title->TitleFull:''; + $type = $title->Type? (string)$title->Type:''; + $result['RecordInfo']['BibEntity']['Titles'][]=array( + 'TitleFull'=>$titleFull, + 'Type'=>$type + ); + } + } + + $result['RecordInfo']['BibRelationships']=array( + 'HasContributorRelationships'=>array(), + 'IsPartOfRelationships'=>array() + ); + + if($record->RecordInfo->BibRecord->BibRelationships->HasContributorRelationships){ + foreach($record->RecordInfo->BibRecord->BibRelationships->HasContributorRelationships->HasContributor as $contributor){ + $nameFull = $contributor->PersonEntity->Name->NameFull? (string)$contributor->PersonEntity->Name->NameFull:''; + $result['RecordInfo']['BibRelationships']['HasContributorRelationships'][]=array( + 'NameFull'=>$nameFull + ); + } + } + + if($record->RecordInfo->BibRecord->BibRelationships){ + foreach($record->RecordInfo->BibRecord->BibRelationships->IsPartOfRelationships->IsPartOf as $relationship){ + if($relationship->BibEntity->Dates){ + foreach($relationship->BibEntity->Dates->Date as $date){ + $d = $date->D? (string)$date->D:''; + $m = $date->M? (string)$date->M:''; + $type = $date->Type? (string)$date->Type:''; + $y = $date->Y? (string)$date->Y:''; + $result['RecordInfo']['BibRelationships']['IsPartOfRelationships']['date'][] = array( + 'D'=> $d, + 'M'=>$m, + 'Type'=>$type, + 'Y'=>$y + ); + } + } + + if($relationship->BibEntity->Identifiers){ + foreach($relationship->BibEntity->Identifiers->Identfier as $identifier){ + $type = $identifier->Type? (string) $identifier->Type :''; + $value = $identifier->Value? (string) $identifier->Value:''; + $result['RecordInfo']['BibRelationships']['IsPartOfRelationships']['Identifiers'][]=array( + 'Type'=>$type, + 'Value'=>$value + ); + } + } + + if($relationship->BibEntity->Numbering){ + foreach($relationship->BibEntity->Numbering->Number as $number){ + $type = (string)$number->Type; + $value= (string)$number->Value; + $result['RecordInfo']['BibRelationships']['IsPartOfRelationships']['numbering'][] = array( + 'Type'=> $type, + 'Value'=>$value + ); + } + } + + if($relationship->BibEntity->Titles){ + foreach($relationship->BibEntity->Titles->Title as $title){ + $titleFull = $title->TitleFull? (string)$title->TitleFull:''; + $type = $title->Type? (string)$title->Type:''; + $result['RecordInfo']['BibRelationships']['IsPartOfRelationships']['Titles'][]=array( + 'TitleFull' => $titleFull, + 'Type'=>$type + ); + } + } + } + } + } + + return $result; + } + + + /** + * Parse the "inner XML" of a SimpleXml element and + * return it as an HTML string + * + * @param SimpleXml $element A SimpleXml DOM + * + * @return string The HTML string + * @access protected + */ + private function retrieveHTML($data, $group = '') + { + //global $path; + // Any group can be added here, but we only use Au (Author) + // Other groups, not present here, won't be transformed to HTML links + $allowed_searchlink_groups = array('Au','Su'); + $allowed_link_groups = array('URL'); + // Map xml tags to the HTML tags + // This is just a small list, the total number of xml tags is far more greater + $xml_to_html_tags = array( + ' ' ' ' '' => '', // Temporary bug fix + ' '', + ' ' ' ' ' ' ' ' ' ' ' ' '', + ' ' ' ' ' '

' ' ' ' ' '

' ' ' ' ' ' ' '', + ' '' + ); + + // Map xml types to Search types used by the UI + $xml_to_search_types = array( + 'Au' => 'Author', + 'Su' => 'Subject' + ); + + // The XML data is XML escaped, let's unescape html entities (e.g. < => <) + $data = html_entity_decode($data); + + // Start parsing the xml data + if (!empty($data)) { + // Replace the XML tags with HTML tags + $search = array_keys($xml_to_html_tags); + $replace = array_values($xml_to_html_tags); + $data = str_replace($search, $replace, $data); + + // Temporary : fix unclosed tags + $data = preg_replace('/<\/highlight/', '', $data); + $data = preg_replace('/<\/span>>/', '', $data); + $data = preg_replace('/<\/searchLink/', '', $data); + $data = preg_replace('/<\/searchLink>>/', '', $data); + + // Parse searchLinks + if (!empty($group) && in_array($group, $allowed_searchlink_groups)) { + $type = $xml_to_search_types[$group]; + $link_xml = '//'; + $link_html = ""; //replaced $path with "result.php" + $data = preg_replace($link_xml, $link_html, $data); + $data = str_replace('', '', $data); + $data = str_replace('*','',$data); + } + // Parse link + if (!empty($group) && in_array($group, $allowed_link_groups)) { + $link_xml = '//'; + $link_html = ""; //replaced $path with "result.php" + $data = preg_replace($link_xml, $link_html, $data); + $data = str_replace('', '', $data); + } + // Replace the rest of searchLinks with simple spans + $link_xml = '//'; + $link_html = ''; + $data = preg_replace($link_xml, $link_html, $data); + $data = str_replace('', '', $data); + // Parse bibliography (anchors and links) + $data = preg_replace('/ ' ' ' '' => '', // Temporary bug fix + ' '', + ' ' ' ' ' ' ' ' ' ' ' ' '', + ' ' ' ' ' '

' ' ' ' ' '

' ' ' ' ' ' ' '', + ' '', + '' => '', + '' => '' + ); + + // Map xml types to Search types used by the UI + $xml_to_search_types = array( + 'Au' => 'Author', + 'Su' => 'Subject' + ); + + // The XML data is XML escaped, let's unescape html entities (e.g. < => <) + $data = html_entity_decode($data); + + // Start parsing the xml data + if (!empty($data)) { + // Replace the XML tags with HTML tags + $search = array_keys($xml_to_html_tags); + $replace = array_values($xml_to_html_tags); + $data = str_replace($search, $replace, $data); + + // Temporary : fix unclosed tags + $data = preg_replace('/<\/highlight/', '', $data); + $data = preg_replace('/<\/span>>/', '', $data); + $data = preg_replace('/<\/searchLink/', '', $data); + $data = preg_replace('/<\/searchLink>>/', '', $data); + + // Parse searchLinks + if (!empty($group) && in_array($group, $allowed_searchlink_groups)) { + $type = $xml_to_search_types[$group]; + $link_xml = '//'; + $link_html = ""; //replaced $path with "result.php" + $data = preg_replace($link_xml, $link_html, $data); + $data = str_replace('', '', $data); + $data = str_replace('
', '; ', $data); + $data = str_replace('*','',$data); + } + // Parse link + if (!empty($group) && in_array($group, $allowed_link_groups)) { + $link_xml = '//'; + $link_html = ""; //replaced $path with "result.php" + $data = preg_replace($link_xml, $link_html, $data); + $data = str_replace('', '', $data); + } + // Replace the rest of searchLinks with simple spans + $link_xml = '//'; + $link_html = ''; + $data = preg_replace($link_xml, $link_html, $data); + $data = str_replace('', '', $data); + // Parse bibliography (anchors and links) + $data = preg_replace('/ \ No newline at end of file diff --git a/targets/eds/js/eds_results.js b/targets/eds/js/eds_results.js index a7d717f..84373ba 100644 --- a/targets/eds/js/eds_results.js +++ b/targets/eds/js/eds_results.js @@ -1,21 +1,23 @@ -Drupal.behaviors.roblib_search_cufts = { +Drupal.behaviors.roblib_search_eds = { attach: function(context, settings) { $url = settings.roblib_search_eds.search_url; jQuery.getJSON($url, function(data) { var items = []; if(data.length < 1){ - jQuery('#' + 'roblib-search-content-eds').empty().append('No Results'); - } else { - jQuery.each(data.journals, function(key, val) { - items.push(''); - }); - } - jQuery('#' + 'roblib-search-content-eds').empty().append(items.join('')); + jQuery('#' + 'roblib-search-content-eds').empty().append('No Results'); + } else { + jQuery.each(data.records, function(key, val) { + items.push('
'); + jQuery.each(val.RecordInfo.BibEntity.Titles, function(key2, val2){ + items.push('
'); + items.push(val2.TitleFull+'
'); + }) + items.push('
'); + }); + } + jQuery('#' + 'roblib-search-content-eds').empty().append(items.join('')); }); -} + } } diff --git a/targets/eds/roblib_search_eds.info b/targets/eds/roblib_search_eds.info index 1aec1e1..4fee8a5 100644 --- a/targets/eds/roblib_search_eds.info +++ b/targets/eds/roblib_search_eds.info @@ -5,4 +5,7 @@ description = implements the Roblib Search modules _roblib_search hook package = Roblib Search version = 7.x-dev core = 7.x -stylesheets[all][] = css/roblib_search_eds.base.css \ No newline at end of file +stylesheets[all][] = css/roblib_search_eds.base.css +files[] = includes/rest/EBSCOAPI.php +files[] = includes/rest/EBSCOConnector.php +files[] = includes/rest/EBSCOResponse.php \ No newline at end of file diff --git a/targets/eds/roblib_search_eds.module b/targets/eds/roblib_search_eds.module index 2e1e34e..010e021 100644 --- a/targets/eds/roblib_search_eds.module +++ b/targets/eds/roblib_search_eds.module @@ -5,6 +5,9 @@ * Implementation of Roblib search for searching several targets. */ +require_once dirname(__FILE__) . '/includes/rest/EBSCOConnector.php'; +require_once dirname(__FILE__) . '/includes/rest/EBSCOResponse.php'; +require_once dirname(__FILE__) . '/includes/rest/EBSCOConnector.php'; /** * Implements hook_boot(). * @global type $conf @@ -58,21 +61,45 @@ function roblib_search_eds_menu() { function roblib_search_eds_config_form($form, &$form_state) { - $form['roblib_search_eds_url'] = array( + $form['roblib_search_eds_rest_url'] = array( '#type' => 'textfield', - '#title' => t('EDS url'), - '#default_value' => variable_get('roblib_search_eds_url', 'http://eds2.lib.sfu.ca/CJDB/PCU/browse/show?'), - '#description' => t('The base EDS URL, for example http://eds2.lib.sfu.ca/CJDB/PCU/browse/show?'), + '#title' => t('EDS Rest endpoint'), + '#default_value' => variable_get('roblib_search_eds_rest_url', 'http://eds-api.ebscohost.com/edsapi/rest'), + '#description' => t('The base EDS URL, for example http://eds-api.ebscohost.com/edsapi/rest'), '#required' => TRUE, ); - $form['roblib_search_eds_search_suffix'] = array( + $form['roblib_search_eds_auth_url'] = array( '#type' => 'textfield', - '#title' => t('EDS search suffix'), - '#default_value' => variable_get('roblib_search_eds_search_suffix', 'browse_field=title&search_type=startswith&format=json&search_terms='), - '#description' => t('The suffix will be appended to the base url for searches, for example browse_field=title&search_type=startswith&format=json&search_terms='), + '#title' => t('EDS Auth endpoint'), + '#default_value' => variable_get('roblib_search_eds_auth_url', 'https://eds-api.ebscohost.com/Authservice/rest'), + '#description' => t('The EDS Auth endpoint, for example https://eds-api.ebscohost.com/Authservice/rest'), '#required' => TRUE, ); - + + $form['roblib_search_eds_user'] = array( + '#type' => 'textfield', + '#title' => t('EDS user'), + '#default_value' => variable_get('roblib_search_eds_user', 'edsusername'), + '#description' => t('EDS user, for example username'), + '#required' => TRUE, + ); + + $form['roblib_search_eds_pass'] = array( + '#type' => 'textfield', + '#title' => t('EDS password'), + '#default_value' => variable_get('roblib_search_eds_pass', 'edspassword'), + '#description' => t('EDS password, for example password'), + '#required' => TRUE, + ); + + $form['roblib_search_eds_profile'] = array( + '#type' => 'textfield', + '#title' => t('EDS profile'), + '#default_value' => variable_get('roblib_search_eds_profile', 'edsapi'), + '#description' => t('EDS profile, for example edsapi'), + '#required' => TRUE, + ); + $form['roblib_search_eds_num_results'] = array( '#type' => 'textfield', '#title' => t('Number of results to return'), @@ -171,21 +198,19 @@ function roblib_search_eds_get_results($query = NULL) { return ''; } } - //http://eds2.lib.sfu.ca/CJDB/PCU/browse/show?browse_field=title&search_type=startswith&format=json&search_terms=dog&submit=Search - $url = variable_get('roblib_search_eds_url', 'http://eds2.lib.sfu.ca/CJDB/PCU/browse/show?'); - $url_suffix = variable_get('roblib_search_eds_search_suffix', 'browse_field=title&search_type=startswith&format=json&search_terms='); - $number_of_records = variable_get('roblib_search_eds_num_results', '5'); - $search_url = $url . $url_suffix . '"' . $query . '"' . '&submit=Search'; - - $results = drupal_http_request($search_url); - - if ($results->code == '200') { - $output = $results->data; - } - else { - $output = $results->status_message; - } - return $output; + //TODO move this to drupal variables + $config = array(); + $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); + $number_per_page = variable_get('roblib_search_eds_num_results', '5'); + $query = urlencode($query); + $params = "query=$query&includefacets=n&resultsperpage=$number_per_page"; + $output = $eds_api->apiSearch($params); + return json_encode($output); } /**