From 12c7e159ef2644531d69340979e91e19b3b68735 Mon Sep 17 00:00:00 2001 From: Paul Pound Date: Thu, 5 Nov 2026 11:19:42 -0400 Subject: [PATCH] fix issues like typos etc --- .../roblib_search_eds.routing.yml | 4 +- .../roblib_search_eds/src/Lib/EBSCOAPI.php | 144 +++++++----------- .../roblib_search_solr_site.routing.yml | 4 +- .../RoblibSearchSolrsiteController.php | 5 +- roblib_search.routing.yml | 2 +- 5 files changed, 63 insertions(+), 96 deletions(-) diff --git a/modules/roblib_search_eds/roblib_search_eds.routing.yml b/modules/roblib_search_eds/roblib_search_eds.routing.yml index 74c90ca..5f9d42d 100644 --- a/modules/roblib_search_eds/roblib_search_eds.routing.yml +++ b/modules/roblib_search_eds/roblib_search_eds.routing.yml @@ -10,7 +10,7 @@ roblib_search_eds.article_search: defaults: _controller: '\Drupal\roblib_search_eds\Controller\RoblibSearchEdsController::articleResults' _title: 'Roblib Search EDS Articles' - query: '[a-zA-Z\s]+' + query: null requirements: _permission: 'access content' @@ -19,7 +19,7 @@ roblib_search_eds.book_search: defaults: _controller: '\Drupal\roblib_search_eds\Controller\RoblibSearchEdsController::bookResults' _title: 'Roblib Search EDS Articles' - query: '[a-zA-Z\s]+' + query: null requirements: _permission: 'access content' diff --git a/modules/roblib_search_eds/src/Lib/EBSCOAPI.php b/modules/roblib_search_eds/src/Lib/EBSCOAPI.php index 9361ee2..b4758aa 100644 --- a/modules/roblib_search_eds/src/Lib/EBSCOAPI.php +++ b/modules/roblib_search_eds/src/Lib/EBSCOAPI.php @@ -119,76 +119,6 @@ class EBSCOAPI { $this->config = $config; } - /** - * Setter / Getter for authentication token. - * - * @param string The authentication token - * - * @return string or none - * - * @access public - */ - - - public function authenticationToken(){ - - $autocompleteTokenTimeOut = time(); - $authenticationTimeout = 0; - //$authenticationTimeout; - // $autocompleteUrl = ''; - // $token = ''; - // $timeout = ''; - // $autoToken = ''; - // //$autocompleteTokenTimeOut = ''; - // //$autocompleteTokenTimeOut; - // $autocompleteTokenTimeOut = 0; - // $autocompleteCustId =''; - - if (isset($_SESSION["authenticationToken"])){ - $token = $_SESSION["authenticationToken"]; - $authenticationTimeout = $_SESSION["authenticationTimeout"]-600; - $timeout = $_SESSION["authenticationTimeout"]; - $autocompleteUrl = $_SESSION["autocompleteUrl"]; - $autoToken = $_SESSION["autocompleteToken"]; - $autocompleteTokenTimeOut = $_SESSION["autocompleteTokenTimeOut"]; - $autocompleteCustId = $_SESSION["autocompleteCustId"]; - }else{ - $result = $this->apiAuthenticationToken(); - $_SESSION["authenticationToken"]= $result['authenticationToken']; - $_SESSION["authenticationTimeout"]= $result['authenticationTimeout']; - $_SESSION['autocompleteUrl'] = $result['autocompleteUrl']; - $_SESSION['autocompleteToken'] = $result['autocompleteToken']; - $_SESSION["autocompleteTokenTimeOut"]= $result['autocompleteTokenTimeOut']; - $_SESSION['autocompleteCustId'] = $result['autocompleteCustId']; - } - - if(time()-$autocompleteTokenTimeOut >= $authenticationTimeout){ - $result = $this->apiAuthenticationToken(); - $_SESSION["authenticationToken"]= $result['authenticationToken']; - $_SESSION["authenticationTimeout"]= $result['authenticationTimeout']; - $_SESSION['autocompleteUrl'] = $result['autocompleteUrl']; - $_SESSION['autocompleteToken'] = $result['autocompleteToken']; - $_SESSION["autocompleteTokenTimeOut"]= $result['autocompleteTokenTimeOut']; - $_SESSION['autocompleteCustId'] = $result['autocompleteCustId']; - - - - - $result = array( - 'authenticationToken' => $token, - 'authenticationTimeout' => $timeout, - 'autocompleteUrl' => $autocompleteUrl, - 'autocompleteToken' => $autoToken, - 'autocompleteTokenTimeOut' => $autocompleteTokenTimeOut, - 'autocompleteCustId' => $autocompleteCustId - ); - - return $result['authenticationToken']; - }else{ - return $this->token; - } - } - /** * Setter / Getter for session token. * @@ -273,8 +203,7 @@ class EBSCOAPI { * @access protected */ protected function request($action, $params = NULL, $attempts = 5) { - //$authenticationToken = $this->authenticationToken(); - $sessionToken = $this->apiSessionToken();//$this->sessionToken(); + $sessionToken = $this->sessionToken(); // If authentication token is missing then the session token is missing too, so get both tokens // If session token is missing then the authentication token may be invalid, so get both tokens. @@ -291,7 +220,8 @@ class EBSCOAPI { // Any change of the isGuest should request a new session // (and don't terminate the current request if there was an error during the session request // since it's not that important) - if ($this->isGuest() != $this->connector()->isGuest()) { + $storedGuest = $this->readSession('isGuest'); + if ($storedGuest !== '' && $storedGuest != $this->connector()->isGuest()) { $this->apiSessionToken(); } @@ -319,8 +249,11 @@ class EBSCOAPI { } break; + case EBSCOConnector::EDS_SESSION_TOKEN_MISSING: case EBSCOConnector::EDS_SESSION_TOKEN_INVALID: - $result = $this->apiAuthenticationAndSessionToken(); + $this->deleteSession('sessionToken'); + $this->sessionToken = NULL; + $result = $this->apiSessionToken(); if ($this->isError($result)) { // Any error should terminate the request immediately // in order to prevent infinite recursion. @@ -381,6 +314,7 @@ class EBSCOAPI { $result = $this->response($response)->result(); if (is_string($result)) { $this->sessionToken($result); + $this->writeSession('isGuest', $this->connector()->isGuest()); return $result; } else { @@ -775,36 +709,68 @@ class EBSCOAPI { } /** - * Store the given object into session. + * How long (seconds) EDS values are cached for. + * + * EDS sessions time out after a period of inactivity, so keep this short. + * An expired token is also detected and replaced by request(). + */ + const CACHE_LIFETIME = 900; + + /** + * Builds the cache ID for a key, scoped to the EDS profile in use. * * @param string $key - * The key used for reading the value. - * @param object $value - * The object stored in session. + * The key used for the value. + * + * @return string + * The cache ID. + */ + protected function cacheId($key) { + $profile = $this->config['profile'] ?? ''; + return 'roblib_search_eds:' . $profile . ':' . $key; + } + + /** + * Store the given value in the Drupal cache. * - * @return none + * EDS tokens are obtained via IP authentication of the web server, so they + * are shared by all visitors rather than stored in each user's PHP session + * ($_SESSION is not persisted by Drupal and would start sessions for + * anonymous users, defeating the page cache). * - * @access protected + * @param string $key + * The key used for reading the value. + * @param mixed $value + * The value to store. */ protected function writeSession($key, $value) { if (!empty($key) && !empty($value)) { - $_SESSION['EBSCO'][$key] = $value; + \Drupal::cache()->set($this->cacheId($key), $value, time() + self::CACHE_LIFETIME); } } /** - * Read from session the object having the given key. + * Read the value having the given key from the Drupal cache. * * @param string $key - * The key used for reading the object. - * - * @return object + * The key used for reading the value. * - * @access protected + * @return mixed + * The cached value, or an empty string if there is none. */ protected function readSession($key) { - $value = isset($_SESSION['EBSCO'][$key]) ? $_SESSION['EBSCO'][$key] : ''; - return $value; + $cache = \Drupal::cache()->get($this->cacheId($key)); + return $cache ? $cache->data : ''; + } + + /** + * Remove the value having the given key from the Drupal cache. + * + * @param string $key + * The key of the value to remove. + */ + protected function deleteSession($key) { + \Drupal::cache()->delete($this->cacheId($key)); } /** @@ -817,7 +783,7 @@ class EBSCOAPI { * @access protected */ protected function isError($object) { - return is_a($object, 'EBSCOException'); + return $object instanceof EBSCOException; } } diff --git a/modules/solr_site/roblib_search_solr_site.routing.yml b/modules/solr_site/roblib_search_solr_site.routing.yml index 535d4f2..3f6b9be 100644 --- a/modules/solr_site/roblib_search_solr_site.routing.yml +++ b/modules/solr_site/roblib_search_solr_site.routing.yml @@ -10,7 +10,7 @@ roblib_search_solr_site.bestbet_search: defaults: _controller: '\Drupal\roblib_search_solr_site\Controller\RoblibSearchSolrsiteController::bestbetResults' _title: 'Roblib Search Bestbet' - query: '[a-zA-Z\s]+' + query: null requirements: _permission: 'access content' @@ -19,6 +19,6 @@ roblib_search_solr_site.general_search: defaults: _controller: '\Drupal\roblib_search_solr_site\Controller\RoblibSearchSolrsiteController::generalResults' _title: 'Roblib Search Solr Site Search' - query: '[a-zA-Z\s]+' + query: null requirements: _permission: 'access content' \ No newline at end of file diff --git a/modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php b/modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php index ed70f49..3fdf3d7 100644 --- a/modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php +++ b/modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php @@ -87,6 +87,7 @@ class RoblibSearchSolrsiteController extends ControllerBase { } $titles = $title->getValues(); $output['results'][$id]['title'] = reset($titles); + $bodyOutput = ''; $body = $result->getField('body')->getValues(); if (!empty($body)) { $bodyOutput = strip_tags($body[0]->getText()); @@ -94,11 +95,11 @@ class RoblibSearchSolrsiteController extends ControllerBase { $bodyOutput = substr($bodyOutput, 0, 499) . '...'; } } - $output['results'][$id]['body'] = empty($bodyOutput) ? '' : $bodyOutput; + $output['results'][$id]['body'] = $bodyOutput; //$rendered_items = $result->getField('rendered_item')->getValues(); // $output['rendered_item'] = $rendered_items[0]->getText(); $summarys = $result->getField('summary')->getValues(); - $output['results'][$id]['summary'] = empty($summmarys) ? '' : reset($summarys); + $output['results'][$id]['summary'] = empty($summarys) ? '' : reset($summarys); $type = $result->getField('type')->getValues(); $output['results'][$id]['type'] = reset($type); $urls = $result->getField('url')->getValues(); diff --git a/roblib_search.routing.yml b/roblib_search.routing.yml index 147da0a..7274b66 100644 --- a/roblib_search.routing.yml +++ b/roblib_search.routing.yml @@ -11,7 +11,7 @@ roblib_search.landing_page: defaults: _controller: '\Drupal\roblib_search\Controller\RoblibSearchController::searchResults' _title: 'Roblib Search Landing Page' - query: '[a-zA-Z\s]+' + query: null options: no_cache: 'TRUE' requirements: