Compare commits

..

3 Commits

  1. 4
      modules/roblib_search_eds/js/eds_results_article.js
  2. 4
      modules/roblib_search_eds/js/eds_results_book.js
  3. 6
      modules/roblib_search_eds/roblib_search_eds.info.yml
  4. 6
      modules/roblib_search_eds/roblib_search_eds.libraries.yml
  5. 17
      modules/roblib_search_eds/roblib_search_eds.module
  6. 4
      modules/roblib_search_eds/roblib_search_eds.routing.yml
  7. 44
      modules/roblib_search_eds/src/Controller/RoblibSearchEdsController.php
  8. 50
      modules/roblib_search_eds/src/Form/EdsSettingsForm.php
  9. 145
      modules/roblib_search_eds/src/Lib/EBSCOAPI.php
  10. 7
      modules/roblib_search_eds/src/Lib/EBSCODocument.php
  11. 3
      modules/roblib_search_eds/src/Lib/EBSCOResponse.php
  12. 1
      modules/roblib_search_eds/src/Lib/sanitizer.class.php
  13. 22
      modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsArticles.php
  14. 22
      modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsBooks.php
  15. 4
      modules/solr_site/js/solr_site_bestbet.js
  16. 4
      modules/solr_site/js/solr_site_results.js
  17. 7
      modules/solr_site/roblib_search_solr_site.info.yml
  18. 6
      modules/solr_site/roblib_search_solr_site.libraries.yml
  19. 82
      modules/solr_site/roblib_search_solr_site.module
  20. 4
      modules/solr_site/roblib_search_solr_site.routing.yml
  21. 42
      modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php
  22. 14
      modules/solr_site/src/Form/SolrsiteSettingsForm.php
  23. 13
      modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteBestbet.php
  24. 13
      modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteGeneral.php
  25. 8
      roblib_search.info.yml
  26. 19
      roblib_search.module
  27. 2
      roblib_search.routing.yml
  28. 36
      src/Controller/RoblibSearchController.php
  29. 10
      src/Form/RoblibSearchForm.php
  30. 10
      src/Form/RoblibSearchSettingsForm.php
  31. 5
      src/Plugin/Block/RoblibSearchBoxBlock.php
  32. 10
      src/Plugin/Block/RoblibSearchOtherSourcesBlock.php
  33. 20
      theme/theme.inc

4
modules/roblib_search_eds/js/eds_results_article.js

@ -1,6 +1,8 @@
Drupal.behaviors.roblib_search_eds_articles = {
attach: function (context, settings) {
if (context == document) {
// Run once, when the results container appears. The block may be
// rendered later by BigPipe, so don't rely on context being document.
if (once('roblib-search-eds-articles', '#roblib-search-content-eds-articles', context).length) {
$query = drupalSettings.path.currentPath.replace('roblib_search/', '');
$url = settings.eds_article_search_url + $query;
article_profile = settings.eds_article_profile;

4
modules/roblib_search_eds/js/eds_results_book.js

@ -1,6 +1,8 @@
Drupal.behaviors.roblib_search_eds = {
attach: function (context, settings) {
if (context == document) {
// Run once, when the results container appears. The block may be
// rendered later by BigPipe, so don't rely on context being document.
if (once('roblib-search-eds-books', '#roblib-search-content-eds', context).length) {
$query = drupalSettings.path.currentPath.replace('roblib_search/', '');
$url = settings.eds_book_search_url + $query;
book_profile = settings.eds_book_profile;

6
modules/roblib_search_eds/roblib_search_eds.info.yml

@ -3,6 +3,6 @@ type: module
configure: roblib_search_eds.settings
description: Implements the Roblib Search EDS book and article results
package: Roblib Search
core_version_requirement: ^9 || ^10
dependancies:
- roblib_search
core_version_requirement: ^10 || ^11
dependencies:
- roblib_search:roblib_search

6
modules/roblib_search_eds/roblib_search_eds.libraries.yml

@ -6,7 +6,10 @@ eds-book:
js:
js/eds_results_book.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
- core/once
eds-article:
version: 1.x
css:
@ -15,5 +18,8 @@ eds-article:
js:
js/eds_results_article.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
- core/once

17
modules/roblib_search_eds/roblib_search_eds.module

@ -5,13 +5,14 @@
* Implementation of Roblib search for searching EDSAPI (EBSCO) targets.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_theme().
*/
function roblib_search_eds_theme($existing, $type, $theme, $path) {
$modulePath = \Drupal::service('module_handler')->getModule('roblib_search')
->getPath();
$spinner_path = '/' . $modulePath . "/img/spinner.gif";
$modulePath = \Drupal::service('extension.list.module')->getPath('roblib_search');
$spinner_path = '/' . $modulePath . '/img/spinner.gif';
return [
'roblib_search_eds_books' => [
'variables' => ['query' => NULL, 'spinner_path' => $spinner_path],
@ -24,14 +25,10 @@ function roblib_search_eds_theme($existing, $type, $theme, $path) {
/**
* Implements hook_help().
*
* @param type $path
* @param type $arg
* @return type
*/
function roblib_search_eds_help($path, $arg) {
switch ($path) {
case 'admin/help#roblib_search_eds':
function roblib_search_eds_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.roblib_search_eds':
return t(
'<p>
provides a EDSAPI target for the Roblib search module. This target uses javascript

4
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'

44
modules/roblib_search_eds/src/Controller/RoblibSearchEdsController.php

@ -4,28 +4,25 @@ namespace Drupal\roblib_search_eds\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\roblib_search_eds\Lib\EBSCOAPI;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Controller for d3 graphs.
* Controller returning EDS search results as JSON.
*/
class RoblibSearchEdsController extends ControllerBase {
/**
* Print JSON.
*
* Returns EDS article results as JSON.
*/
public function articleResults($query = NULL) {
print $this->eds_get_results($query, 'Articles');
exit();
return JsonResponse::fromJsonString($this->eds_get_results($query, 'Articles'));
}
/**
* Print JSON.
*
* Returns EDS book results as JSON.
*/
public function bookResults($query = NULL) {
print $this->eds_get_results($query, 'Books');
exit();
return JsonResponse::fromJsonString($this->eds_get_results($query, 'Books'));
}
/**
@ -37,16 +34,14 @@ class RoblibSearchEdsController extends ControllerBase {
* @return string
* A JSON string
*/
function eds_get_results($query = NULL, $type = 'Articles') {
//drupal_add_css(drupal_get_path('module', 'roblib_search_eds') . '/css/roblib_search_eds.theme.css');
$config = \Drupal::config('roblib_search_eds.settings');
public function eds_get_results($query = NULL, $type = 'Articles') {
$config = $this->config('roblib_search_eds.settings');
$request = \Drupal::request();
// Url parameters.
if (!isset($query)) {
if (isset($_GET['roblib_query'])) {
$query = $_GET['roblib_query'];
}
else {
return '';
$query = $request->query->get('roblib_query');
if (!isset($query)) {
return '{}';
}
}
$limiters = '';
@ -62,7 +57,7 @@ class RoblibSearchEdsController extends ControllerBase {
$query = '(' . $query . ') ' . $limiters;
$search = array('lookfor' => $query, 'type' => 'ALLFields', 'index' => 'AllFields', 'q' => 'ebsco/results', 'op' => 'Search');
$eds_api = new EBSCOAPI($eds_config);
$is_local_ip = $eds_api->connector()->isGuestIPAddress($_SERVER["REMOTE_ADDR"]);
$is_local_ip = $eds_api->connector()->isGuestIPAddress($request->getClientIp());
$eds_api->isGuest(FALSE);
$output = $eds_api->apiSearch($search, $filters, 1, $number_per_page);
@ -79,8 +74,8 @@ class RoblibSearchEdsController extends ControllerBase {
* @return string
* The limiters to append to the query.
*/
function get_eds_query_limiters($type) {
$config = \Drupal::config('roblib_search_eds.settings');
public function get_eds_query_limiters($type) {
$config = $this->config('roblib_search_eds.settings');
$limiters = 'n';
switch ($type) {
case 'Articles':
@ -97,9 +92,9 @@ class RoblibSearchEdsController extends ControllerBase {
return $limiters;
}
function build_eds_config_arr($type) {
$ebsco_config = array();
$roblib_search_config = \Drupal::config('roblib_search_eds.settings');
public function build_eds_config_arr($type) {
$ebsco_config = [];
$roblib_search_config = $this->config('roblib_search_eds.settings');
$ebsco_config['user'] = $roblib_search_config->get('eds_user');
$ebsco_config['password'] = $roblib_search_config->get('eds_pass');
//get the local ips allowed from ebsco
@ -111,6 +106,9 @@ class RoblibSearchEdsController extends ControllerBase {
$ebsco_config['rest_url'] = $roblib_search_config->get('eds_rest_url');
$ebsco_config['guest'] = $roblib_search_config->get('eds_guest');
$ebsco_config['autocomplete'] = '';
$ebsco_config['interface'] = '';
$ebsco_config['organization'] = '';
$ebsco_config['log'] = 0;
return $ebsco_config;
}

50
modules/roblib_search_eds/src/Form/EdsSettingsForm.php

@ -23,96 +23,96 @@ class EdsSettingsForm extends FormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = \Drupal::config('roblib_search_eds.settings');
$config = $this->config('roblib_search_eds.settings');
/* $form['eds_rest_url'] = array(
'#type' => 'textfield',
'#title' => t('EDS Rest endpoint'),
'#title' => $this->t('EDS Rest endpoint'),
'#default_value' => $config->get('eds_rest_url'),
'#description' => t('The base EDS URL, for example https://eds-api.ebscohost.com/edsapi/rest'),
'#description' => $this->t('The base EDS URL, for example https://eds-api.ebscohost.com/edsapi/rest'),
'#required' => TRUE,
);
$form['eds_auth_url'] = array(
'#type' => 'textfield',
'#title' => t('EDS Auth endpoint'),
'#title' => $this->t('EDS Auth endpoint'),
'#default_value' => $config->get('eds_auth_url'),
'#description' => t('The EDS Auth endpoint, for example https://eds-api.ebscohost.com/Authservice/rest'),
'#description' => $this->t('The EDS Auth endpoint, for example https://eds-api.ebscohost.com/Authservice/rest'),
'#required' => TRUE,
);
$form['eds_user'] = array(
'#type' => 'textfield',
'#title' => t('EDS user'),
'#title' => $this->t('EDS user'),
'#default_value' => $config->get('eds_user'),
'#description' => t('EDS user, for example username'),
'#description' => $this->t('EDS user, for example username'),
'#required' => TRUE,
);
$form['eds_pass'] = array(
'#type' => 'textfield',
'#title' => t('EDS password'),
'#title' => $this->t('EDS password'),
'#default_value' => $config->get('eds_pass'),
'#description' => t('EDS password, for example password'),
'#description' => $this->t('EDS password, for example password'),
'#required' => TRUE,
);
$form['eds_guest'] = array(
'#type' => 'textfield',
'#title' => t('EDS Guest access'),
'#title' => $this->t('EDS Guest access'),
'#default_value' => $config->get('eds_guest'),
'#description' => t('EDS Guest access, y = guest access, n = not a guest'),
'#description' => $this->t('EDS Guest access, y = guest access, n = not a guest'),
'#required' => TRUE,
);*/
$form['eds_local_ip'] = array(
'#type' => 'textfield',
'#title' => t('EDS Local IP'),
'#title' => $this->t('EDS Local IP'),
'#default_value' => $config->get('eds_local_ip'),
'#description' => t('Local IP address prefix, i. 137.149.'),
'#description' => $this->t('Local IP address prefix, i. 137.149.'),
'#required' => TRUE,
);
$form['eds_article_profile'] = array(
'#type' => 'textfield',
'#title' => t('EDS Article profile'),
'#title' => $this->t('EDS Article profile'),
'#default_value' => $config->get('eds_article_profile'),
'#description' => t('EDS profile, for example edsapi'),
'#description' => $this->t('EDS profile, for example edsapi'),
'#required' => TRUE,
);
$form['eds_book_profile'] = array(
'#type' => 'textfield',
'#title' => t('EDS Book profile'),
'#title' => $this->t('EDS Book profile'),
'#default_value' => $config->get('eds_book_profile'),
'#description' => t('EDS Book profile, for example apilite'),
'#description' => $this->t('EDS Book profile, for example apilite'),
'#required' => TRUE,
);
$form['eds_num_results'] = array(
'#type' => 'textfield',
'#title' => t('Number of results to return'),
'#title' => $this->t('Number of results to return'),
'#default_value' => $config->get('eds_num_results'),
'#description' => t('The number of results to display in the Bento box, usurally 5'),
'#description' => $this->t('The number of results to display in the Bento box, usurally 5'),
'#required' => TRUE,
);
$form['eds_article_limiters'] = array(
'#type' => 'textarea',
'#title' => t('The limiters for Article searches'),
'#title' => $this->t('The limiters for Article searches'),
'#default_value' => $config->get('eds_article_limiters'),
'#description' => t('Specify the Publication Types to use as limiters for Article searches. Formatted similar to
'#description' => $this->t('Specify the Publication Types to use as limiters for Article searches. Formatted similar to
AND (PT Article OR PT Magazines OR PT News OR PT Academic Journals OR PT Conference Materials)'),
'#required' => TRUE,
);
$form['eds_book_limiters'] = array(
'#type' => 'textarea',
'#title' => t('The limiters for Books and Media searches'),
'#title' => $this->t('The limiters for Books and Media searches'),
'#default_value' => $config->get('eds_book_limiters'),
'#description' => t('Specify the Publication Types to use as limiters for Article searches. Formatted similar to
'#description' => $this->t('Specify the Publication Types to use as limiters for Article searches. Formatted similar to
AND (PT Book OR PT Video OR PT Audio'),
'#required' => TRUE,
);
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
'#value' => $this->t('Save'),
'#button_type' => 'primary',
];
return $form;
@ -129,7 +129,7 @@ class EdsSettingsForm extends FormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = \Drupal::configFactory()->getEditable('roblib_search_eds.settings');
$config = $this->configFactory()->getEditable('roblib_search_eds.settings');
//$config->set('eds_rest_url', $form_state->getValue('eds_rest_url'))->save();
//$config->set('eds_auth_url', $form_state->getValue('eds_auth_url'))->save();
//$config->set('eds_user', $form_state->getValue('eds_user'))->save();

145
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.
@ -370,6 +303,7 @@ class EBSCOAPI {
//$headers = array(
// 'x-authenticationToken: '.$this->authenticationToken(),
//);
$headers = [];
$response = $this->connector()->requestSessionToken($headers);
// Raise the exception so that any code running this method should exit immediately.
@ -380,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 {
@ -774,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));
}
/**
@ -816,7 +783,7 @@ class EBSCOAPI {
* @access protected
*/
protected function isError($object) {
return is_a($object, 'EBSCOException');
return $object instanceof EBSCOException;
}
}

7
modules/roblib_search_eds/src/Lib/EBSCODocument.php

@ -54,6 +54,13 @@ class EBSCODocument {
*/
private $params = array();
/**
* The citation style terms (declared to avoid PHP 8.2+ dynamic properties).
*
* @var array|null
*/
private $citationStylesTerms = NULL;
/**
* The associative array of EBSCO results returned by a Search API call
* #global array.

3
modules/roblib_search_eds/src/Lib/EBSCOResponse.php

@ -358,8 +358,7 @@ class EBSCOResponse
if (isset($record->RecordInfo->BibRecord->BibRelationships->HasContributorRelationships)) {
foreach ($record->RecordInfo->BibRecord->BibRelationships->HasContributorRelationships->HasContributor as $contributor) {
$nameFull = $contributor->PersonEntity->Name->NameFull ?
(string) $contributor->PersonEntity->Name->NameFull : '';
$nameFull = (string) ($contributor->PersonEntity?->Name?->NameFull ?? '');
$result['RecordInfo']['BibRelationships']['HasContributorRelationships'][] = array(
'NameFull' => $nameFull,

1
modules/roblib_search_eds/src/Lib/sanitizer.class.php

@ -43,6 +43,7 @@ class HTML_Sanitizer {
*/
private $_allowedTags;
private $_allowJavascriptEvents;
private $_allowDOMEvents;
private $_allowJavascriptInUrls;
private $_allowObjects;
private $_allowScript;

22
modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsArticles.php

@ -3,6 +3,7 @@
namespace Drupal\roblib_search_eds\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
/**
* Provides EDS Article search results.
@ -29,7 +30,6 @@ class RoblibSearchEdsArticles extends BlockBase {
];
return [
'#theme' => 'roblib_search_eds_articles',
'query' => $query,
'#attached' => [
'library' => [
'roblib_search_eds/eds-article'
@ -41,13 +41,21 @@ class RoblibSearchEdsArticles extends BlockBase {
/**
* {@inheritdoc}
*
* The block only renders a placeholder and settings; results are fetched
* client side from uncached JSON endpoints, so the markup can be cached.
* Setting max-age 0 here would make BigPipe stream the block in later.
*/
public function getCacheMaxAge() {
// Reading dynamic content from external source so no caching.
if (\Drupal::currentUser()->isAnonymous()) {
// Anonymous user...
return 0;
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), ['config:roblib_search_eds.settings']);
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
// The search URL in drupalSettings includes the scheme and host.
return Cache::mergeContexts(parent::getCacheContexts(), ['url.site']);
}
}

22
modules/roblib_search_eds/src/Plugin/Block/RoblibSearchEdsBooks.php

@ -3,6 +3,7 @@
namespace Drupal\roblib_search_eds\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
/**
* Provides EDS book search results.
@ -29,7 +30,6 @@ class RoblibSearchEdsBooks extends BlockBase {
];
return [
'#theme' => 'roblib_search_eds_books',
//'query' => $query,
'#attached' => [
'library' => [
'roblib_search_eds/eds-book'
@ -41,13 +41,21 @@ class RoblibSearchEdsBooks extends BlockBase {
/**
* {@inheritdoc}
*
* The block only renders a placeholder and settings; results are fetched
* client side from uncached JSON endpoints, so the markup can be cached.
* Setting max-age 0 here would make BigPipe stream the block in later.
*/
public function getCacheMaxAge() {
// Reading dynamic content from external source so no caching.
if (\Drupal::currentUser()->isAnonymous()) {
// Anonymous user...
return 0;
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), ['config:roblib_search_eds.settings']);
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
// The search URL in drupalSettings includes the scheme and host.
return Cache::mergeContexts(parent::getCacheContexts(), ['url.site']);
}

4
modules/solr_site/js/solr_site_bestbet.js

@ -1,6 +1,8 @@
Drupal.behaviors.roblib_search_solr_bestbet_results = {
attach: function (context, settings) {
if (context == document) {
// Run once, when the results container appears. The block may be
// rendered later by BigPipe, so don't rely on context being document.
if (once('roblib-search-solr-bestbet', '#roblib-search-content-solr-site-bestbet', context).length) {
query = drupalSettings.path.currentPath.replace('roblib_search/', '');
solrUrl = settings.solr_site_search_url + query;
baseUrl = settings.solr_bestbet_base_url + query;

4
modules/solr_site/js/solr_site_results.js

@ -1,6 +1,8 @@
Drupal.behaviors.roblib_search_solr_site_results = {
attach: function (context, settings) {
if (context == document) {
// Run once, when the results container appears. The block may be
// rendered later by BigPipe, so don't rely on context being document.
if (once('roblib-search-solr-results', '#roblib-search-content-solr-site-results', context).length) {
unencoded_query = drupalSettings.path.currentPath.replace('roblib_search/', '');
query = encodeURIComponent(unencoded_query);
solrUrl = settings.solr_site_search_url + query;

7
modules/solr_site/roblib_search_solr_site.info.yml

@ -3,6 +3,7 @@ type: module
configure: roblib_search_solr_site.settings
description: Implements the Roblib Search for local Solr site bento results
package: Roblib Search
core_version_requirement: ^9 || ^10
dependancies:
- roblib_search
core_version_requirement: ^10 || ^11
dependencies:
- roblib_search:roblib_search
- search_api:search_api

6
modules/solr_site/roblib_search_solr_site.libraries.yml

@ -6,10 +6,16 @@ solr-site:
js:
js/solr_site_results.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
- core/once
solr-bestbet:
version: 1.x
js:
js/solr_site_bestbet.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
- core/once

82
modules/solr_site/roblib_search_solr_site.module

@ -2,18 +2,17 @@
/**
* @file
* Implementation of Roblib search for searching several targets.
* Implementation of Roblib search for the local Solr (Search API) site index.
*/
/**
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_theme().
*/
function roblib_search_solr_site_theme($existing, $type, $theme, $path) {
$modulePath = \Drupal::service('module_handler')->getModule('roblib_search')
->getPath();
$spinner_path = '/' . $modulePath . "/img/spinner.gif";
$modulePath = \Drupal::service('extension.list.module')->getPath('roblib_search');
$spinner_path = '/' . $modulePath . '/img/spinner.gif';
return [
'roblib_search_solr_site_results' => [
'variables' => ['solr_type' => 'results', 'spinner_path' => $spinner_path],
@ -24,77 +23,12 @@ function roblib_search_solr_site_theme($existing, $type, $theme, $path) {
];
}
/**
* Send the query to solr but limit the results so it doesn't include certain content.
*
* Print json and exit so the javascript can consume the json.
*
* @param string $query
* The solr search string.
*/
/*function roblib_search_solr_site_results($query) {
print roblib_search_solr_site_get_results($query, '-bundle:bestbet');
exit();
}*/
/**
* Send the query to solr but limit the results so it only includes content of type bestbet.
*
* Print json and exit so the javascript can consume the json.
*
* @param string $query
* The solr search string.
*/
/*function roblib_search_solr_site_bestbet($query) {
$query = str_replace('"', '', $query);
$json = roblib_search_solr_site_get_results("sort_label:$query", 'bundle:bestbet');
$json = roblib_search_solr_site_add_url($json);
print $json;
exit();
}*/
/**
*
* Updates the json passed in with a url directly to the target of the link field.
* This information is not in solr as it comes from a custom field.
*
*
* @param string $json
* The json string returned from the solr query.
*
* @return string
* json we got from solr with an updated url directly to the target of the link field
*/
/*function roblib_search_solr_site_add_url($json) {
$responses = json_decode($json);
foreach ($responses->response->docs as &$doc) {
$nid = $doc->entity_id;
// The link field is a complex field and we get the text in solr but not the actual url.
// Since best best will only every return one result it's not much overhead to load the node
// and get the value of the link field. probably less overhead then installing additional
// modules to get the url in solr.
$node = node_load($nid);
$url = $node->field_link['und'][0]['url'];
$doc->url = $url;
}
return json_encode($responses);
}
*/
/**
* Implements hook_help().
*/
function roblib_search_solr_site_help($path, $arg) {
switch ($path) {
case 'admin/help#roblib_search_solr_site':
return t(
'<p>
provides a Solr target for the Roblib search module
</p>'
);
function roblib_search_solr_site_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.roblib_search_solr_site':
return t('<p>Provides a Solr target for the Roblib search module.</p>');
}
}

4
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'

42
modules/solr_site/src/Controller/RoblibSearchSolrsiteController.php

@ -3,28 +3,26 @@
namespace Drupal\roblib_search_solr_site\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\search_api\Entity\Index;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Controller for d3 graphs.
* Controller returning Search API (Solr) site results as JSON.
*/
class RoblibSearchSolrsiteController extends ControllerBase {
/**
* Print JSON.
*
* Returns best bet results as JSON.
*/
public function bestbetResults($query = NULL) {
print $this->getResults($query, 'BestBet');
exit();
return new JsonResponse($this->getResults($query, 'BestBet'));
}
/**
* Print JSON.
*
* Returns general site results as JSON.
*/
public function generalResults($query = NULL) {
print $this->getResults($query, 'General');
exit();
return new JsonResponse($this->getResults($query, 'General'));
}
/**
@ -33,24 +31,20 @@ class RoblibSearchSolrsiteController extends ControllerBase {
* The search term(s) to send to Solr.
* @param string type
* The type of search, either BestBet or General.
* @return string
* A JSON string
* @return array
* The results, ready to be JSON encoded.
*/
function getResults($queryString = NULL, $solr_type = 'General') {
global $base_url;
$config = \Drupal::config('roblib_search_solr_site.settings');
public function getResults($queryString = NULL, $solr_type = 'General') {
// Url parameters.
if (!isset($queryString)) {
if (isset($_GET['roblib_query'])) {
$queryString = $_GET['roblib_query'];
}
else {
return '';
$queryString = \Drupal::request()->query->get('roblib_query');
if (!isset($queryString)) {
return [];
}
}
//$queryString = urlencode($queryString);
// TODO make the index configurable.
$index = \Drupal\search_api\Entity\Index::load('library');
$index = Index::load('library');
$query = $index->query();
// Change the parse mode for the search.
@ -93,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());
@ -100,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();
@ -117,8 +112,7 @@ class RoblibSearchSolrsiteController extends ControllerBase {
$output['results'][$id]['field_link'] = empty($links) ? '' : $links;
}
echo json_encode($output);
exit();
return $output;
}
}

14
modules/solr_site/src/Form/SolrsiteSettingsForm.php

@ -23,26 +23,26 @@ class SolrsiteSettingsForm extends FormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = \Drupal::config('roblib_search_solr_site.settings');
$config = $this->config('roblib_search_solr_site.settings');
$form['solr_site_url'] = array(
'#type' => 'textfield',
'#title' => t('Solr URL'),
'#title' => $this->t('Solr URL'),
'#default_value' => $config->get('solr_site_url'),
'#description' => t('The base Solr URL, for example /search/node?keys='),
'#description' => $this->t('The base Solr URL, for example /search/node?keys='),
'#required' => TRUE,
);
$form['solr_site_num_results'] = array(
'#type' => 'textfield',
'#title' => t('Number of results to return'),
'#title' => $this->t('Number of results to return'),
'#default_value' => $config->get('solr_site_num_results'),
'#description' => t('The number of results to display in the Bento box'),
'#description' => $this->t('The number of results to display in the Bento box'),
'#required' => TRUE,
);
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
'#value' => $this->t('Save'),
'#button_type' => 'primary',
];
return $form;
@ -59,7 +59,7 @@ class SolrsiteSettingsForm extends FormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = \Drupal::configFactory()->getEditable('roblib_search_solr_site.settings');
$config = $this->configFactory()->getEditable('roblib_search_solr_site.settings');
$config->set('solr_site_url', $form_state->getValue('solr_site_url'))->save();
$config->set('solr_site_num_results', $form_state->getValue('solr_site_num_results'))->save();
}

13
modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteBestbet.php

@ -3,6 +3,7 @@
namespace Drupal\roblib_search_solr_site\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
/**
* Provides EDS Article search results.
@ -39,13 +40,13 @@ class RoblibSearchSolrSiteBestbet extends BlockBase {
/**
* {@inheritdoc}
*
* The block only renders a placeholder and settings; results are fetched
* client side from uncached JSON endpoints, so the markup can be cached.
* Setting max-age 0 here would make BigPipe stream the block in later.
*/
public function getCacheMaxAge() {
// Reading dynamic content from external source so no caching.
if (\Drupal::currentUser()->isAnonymous()) {
// Anonymous user...
return 0;
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), ['config:roblib_search_solr_site.settings']);
}
}

13
modules/solr_site/src/Plugin/Block/RoblibSearchSolrsiteGeneral.php

@ -3,6 +3,7 @@
namespace Drupal\roblib_search_solr_site\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
/**
* Provides EDS book search results.
@ -41,13 +42,13 @@ class RoblibSearchSolrsiteGeneral extends BlockBase {
/**
* {@inheritdoc}
*
* The block only renders a placeholder and settings; results are fetched
* client side from uncached JSON endpoints, so the markup can be cached.
* Setting max-age 0 here would make BigPipe stream the block in later.
*/
public function getCacheMaxAge() {
// Reading dynamic content from external source so no caching.
if (\Drupal::currentUser()->isAnonymous()) {
// Anonymous user...
return 0;
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), ['config:roblib_search_solr_site.settings']);
}
}

8
roblib_search.info.yml

@ -1,10 +1,8 @@
name: Roblib Search
type: module
core_version_requirement: ^9 || ^10
core_version_requirement: ^10 || ^11
configure: roblib_search.settings
description: Provides a search form and fires a hook when the search is submitted. Other modules should implement the search functionality by implementing the correct hooks.
package: Roblib Search
dependancies:
- drupal:views_ui
- drupal:message
dependencies:
- message:message

19
roblib_search.module

@ -5,6 +5,8 @@
* Implementation of Roblib search for searching several targets.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_theme().
*/
@ -20,23 +22,12 @@ function roblib_search_theme($existing, $type, $theme, $path) {
}
/**
* Implements hook_init().
*/
/*function roblib_search_init() {
drupal_add_js(drupal_get_path('module', 'roblib_search') . '/js/imagesloaded.pkg.min.js');
drupal_add_js(drupal_get_path('module', 'roblib_search') . '/js/roblib_search.js');
drupal_add_css(drupal_get_path('module', 'roblib_search') . '/css/jquery.qtip.min.css');
}*/
/**
* Implements hook_help().
*/
function roblib_search_help($path, $arg) {
switch ($path) {
case 'admin/help#roblib_search':
function roblib_search_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.roblib_search':
return t(
'<p>
The Roblib Search is a module to allow searching across multiple targets.

2
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:

36
src/Controller/RoblibSearchController.php

@ -2,35 +2,43 @@
namespace Drupal\roblib_search\Controller;
use Drupal\Component\Utility\Html;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Component\Utility\Html;
use Symfony\Component\HttpFoundation\Request;
/**
* Controller for d3 graphs.
* Controller for the Roblib Search landing page.
*/
class RoblibSearchController extends ControllerBase {
/**
* Display the markup.
* Display the search results landing page.
*
* @return array
* A renderable array.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
* @param string|null $query
* The search query from the path.
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
* A renderable array, or a redirect when a roblib_query parameter is used.
*/
public function searchResults($query = NULL) {
if ($query = $_GET['roblib_query']) {
$query = str_replace('/', ' ',$query);
$query = trim(Html::decodeEntities($query));
if(empty($query)) {
\Drupal::messenger()->addMessage($this->t('Please Enter a valid search string.'));
public function searchResults(Request $request, $query = NULL) {
$roblib_query = $request->query->get('roblib_query');
if (!empty($roblib_query)) {
$roblib_query = str_replace('/', ' ', $roblib_query);
$roblib_query = trim(Html::decodeEntities($roblib_query));
if (empty($roblib_query)) {
$this->messenger()->addMessage($this->t('Please Enter a valid search string.'));
$url = Url::fromRoute('roblib_search.landing_page');
} else {
$url = Url::fromRoute('roblib_search.landing_page', ['query' => $query]);
}
else {
$url = Url::fromRoute('roblib_search.landing_page', ['query' => $roblib_query]);
}
return new RedirectResponse($url->toString());
}
return [
return [
'#theme' => 'roblib_search_results',
'#query' => $query,
];

10
src/Form/RoblibSearchForm.php

@ -4,7 +4,6 @@ namespace Drupal\roblib_search\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\message\Entity\Message;
@ -27,12 +26,7 @@ class RoblibSearchForm extends FormBase{
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
if (isset($_GET['roblib_query'])) {
$query = $_GET['roblib_query'];
}
else {
$query = '';
}
$query = $this->getRequest()->query->get('roblib_query', '');
$form['simple'] = array(
'#type' => 'container',
'#attributes' => array(
@ -50,7 +44,7 @@ class RoblibSearchForm extends FormBase{
);
$form['simple']['submit'] = array(
'#type' => 'submit',
'#value' => t('search')
'#value' => $this->t('search')
);
return $form;
}

10
src/Form/RoblibSearchSettingsForm.php

@ -23,17 +23,17 @@ class RoblibSearchSettingsForm extends FormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = \Drupal::config('roblib_search.settings');
$config = $this->config('roblib_search.settings');
$form['search_panel_page'] = array(
'#type' => 'textfield',
'#title' => t('Path to the roblib search panel page'),
'#title' => $this->t('Path to the roblib search panel page'),
'#default_value' => $config->get('search_panel_page'),
'#description' => t('The Path to the Roblib Search Panel, for example /roblib/panel'),
'#description' => $this->t('The Path to the Roblib Search Panel, for example /roblib/panel'),
'#required' => TRUE,
);
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
'#value' => $this->t('Save'),
'#button_type' => 'primary',
];
return $form;
@ -50,7 +50,7 @@ class RoblibSearchSettingsForm extends FormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = \Drupal::configFactory()->getEditable('roblib_search.settings');
$config = $this->configFactory()->getEditable('roblib_search.settings');
$config->set('search_panel_page', $form_state->getValue('search_panel_page'))->save();
}

5
src/Plugin/Block/RoblibSearchBoxBlock.php

@ -3,6 +3,7 @@
namespace Drupal\roblib_search\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\roblib_search\Form\RoblibSearchForm;
/**
* Provides a search form.
@ -19,8 +20,6 @@ class RoblibSearchBoxBlock extends BlockBase {
* {@inheritdoc}
*/
public function build() {
$form = \Drupal::formBuilder()->getForm('\Drupal\roblib_search\Form\RoblibSearchForm');
return $form;
return \Drupal::formBuilder()->getForm(RoblibSearchForm::class);
}
}

10
src/Plugin/Block/RoblibSearchOtherSourcesBlock.php

@ -18,11 +18,10 @@ class RoblibSearchOtherSourcesBlock extends BlockBase {
/**
* {@inheritdoc}
*/
///roblib_search/
public function build() {
$prefix = '/roblib_search/';
$query = $_SERVER['REQUEST_URI'];
if (substr($query, 0, strlen($prefix)) == $prefix) {
$query = \Drupal::request()->getRequestUri();
if (str_starts_with($query, $prefix)) {
$query = substr($query, strlen($prefix));
}
return [
@ -31,8 +30,11 @@ class RoblibSearchOtherSourcesBlock extends BlockBase {
];
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
// don't cache this block
// Don't cache this block.
return 0;
}

20
theme/theme.inc

@ -1,20 +0,0 @@
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*function roblib_search_preprocess_roblib_search_other_sources(&$variables) {
global $base_url;
if (!isset($query)) {
if (isset($_GET['roblib_query'])) {
$query = $_GET['roblib_query'];
}
else {
$query = '';
}
}
$variables['query'] = urlencode($query);
}*/
?>
Loading…
Cancel
Save