diff --git a/config/vufind/LibGuides.ini b/config/vufind/LibGuides.ini index 68a8f2bd4e8..e81126bb21a 100644 --- a/config/vufind/LibGuides.ini +++ b/config/vufind/LibGuides.ini @@ -31,4 +31,7 @@ default_limit = 20 ; string that can then be used to retrieve those guides here. Note that when ; changing tags, you may need to wait some time for content to be reindexed on ; the LibGuides end before the search will begin working here. -;defaultSearch = "general" \ No newline at end of file +;defaultSearch = "general" + +; Parse and display the description below each title +;displayDescription = true diff --git a/config/vufind/LibGuidesAZ.ini b/config/vufind/LibGuidesAZ.ini index f6ad12856e7..77558af81d0 100644 --- a/config/vufind/LibGuidesAZ.ini +++ b/config/vufind/LibGuidesAZ.ini @@ -12,3 +12,4 @@ relative_path = ./LibGuides.ini ;default_limit = 20 ;limit_options = 10,20,40,60,80,100 ;defaultSearch = "general" +;displayDescription = true diff --git a/module/VuFind/src/VuFind/Search/Factory/LibGuidesBackendFactory.php b/module/VuFind/src/VuFind/Search/Factory/LibGuidesBackendFactory.php index 87443fd49ed..8d1d6d3c5aa 100644 --- a/module/VuFind/src/VuFind/Search/Factory/LibGuidesBackendFactory.php +++ b/module/VuFind/src/VuFind/Search/Factory/LibGuidesBackendFactory.php @@ -131,12 +131,16 @@ protected function createConnector() // Get base URI, if available: $baseUrl = $this->libGuidesConfig->General->baseUrl ?? null; + // Optionally parse the resource description + $displayDescription = $this->libGuidesConfig->General->displayDescription ?? false; + // Create connector: $connector = new Connector( $iid, $this->createHttpClient($this->libGuidesConfig->General->timeout ?? 30), $ver, - $baseUrl + $baseUrl, + $displayDescription ); $connector->setLogger($this->logger); return $connector; diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Connector.php b/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Connector.php index a041eba0ee2..09bdaa6a804 100644 --- a/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Connector.php +++ b/module/VuFindSearch/src/VuFindSearch/Backend/LibGuides/Connector.php @@ -78,17 +78,25 @@ class Connector implements \Laminas\Log\LoggerAwareInterface */ protected $apiVersion; + /** + * Optionally load & display the description of each resource + * + * @var bool + */ + protected $displayDescription; + /** * Constructor * * Sets up the LibGuides Client * - * @param string $iid Institution ID - * @param HttpClient $client HTTP client - * @param float $apiVersion API version number - * @param string $baseUrl API base URL (optional) + * @param string $iid Institution ID + * @param HttpClient $client HTTP client + * @param float $apiVersion API version number + * @param string $baseUrl API base URL (optional) + * @param bool $displayDescription Optionally load & display the description of each resource */ - public function __construct($iid, $client, $apiVersion = 1, $baseUrl = null) + public function __construct($iid, $client, $apiVersion = 1, $baseUrl = null, $displayDescription = false) { $this->apiVersion = $apiVersion; if (empty($baseUrl)) { @@ -101,6 +109,7 @@ public function __construct($iid, $client, $apiVersion = 1, $baseUrl = null) } $this->iid = $iid; $this->client = $client; + $this->displayDescription = $displayDescription; } /** @@ -187,15 +196,33 @@ protected function process($data) $items = []; - // Extract titles and URLs from response: - $regex = '/]*>([^<]*)(.*?)<\/li>/'; + $linkRegex = '/]*>([^<]*)(.*?)<\/div>/'; + + // Extract each result item + $itemCount = preg_match_all($itemRegex, $data, $itemMatches); - for ($i = 0; $i < $count; $i++) { - $items[] = [ - 'id' => $matches[1][$i], // ID = URL - 'title' => $matches[2][$i], + for ($i = 0; $i < $itemCount; $i++) { + // Extract the link, which contains both the title and URL. + $linkCount = preg_match_all($linkRegex, $itemMatches[1][$i], $linkMatches); + if ($linkCount != 1) { + throw new \Exception('LibGuides result item included more than one link: ' . $itemMatches[1][$i]); + } + $item = [ + 'id' => $linkMatches[1][0], // ID = URL + 'title' => $linkMatches[2][0], ]; + + // Extract the description. + if ($this->displayDescription) { + $descriptionCount = preg_match_all($descriptionRegex, $itemMatches[1][$i], $descriptionMatches); + if ($descriptionCount >= 1) { + $item['description'] = html_entity_decode(strip_tags($descriptionMatches[1][0])); + } + } + + $items[] = $item; } $results = [ @@ -233,7 +260,7 @@ protected function prepareParams(array $params) 'list_format' => 1, 'output_format' => 1, 'load_type' => 2, - 'enable_description' => 0, + 'enable_description' => $this->displayDescription ? 1 : 0, 'enable_group_search_limit' => 0, 'enable_subject_search_limit' => 0, 'widget_embed_type' => 2, diff --git a/module/VuFindSearch/tests/fixtures/libguides/response/search b/module/VuFindSearch/tests/fixtures/libguides/response/search index ac5d9f21104..29176e428a8 100644 Binary files a/module/VuFindSearch/tests/fixtures/libguides/response/search and b/module/VuFindSearch/tests/fixtures/libguides/response/search differ diff --git a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/LibGuides/BackendTest.php b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/LibGuides/BackendTest.php index f17e1af5ccf..461e1c9e203 100644 --- a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/LibGuides/BackendTest.php +++ b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/LibGuides/BackendTest.php @@ -82,13 +82,13 @@ public function testSearch() $this->assertEquals('test', $coll->getSourceIdentifier()); $rec = $coll->first(); $this->assertEquals('test', $rec->getSourceIdentifier()); - $this->assertEquals('http://libguides.brynmawr.edu/tests-measures?hs=a', $rec->getUniqueID()); + $this->assertEquals('https://guides.tricolib.brynmawr.edu/testprep', $rec->getUniqueID()); $recs = $coll->getRecords(); $this->assertEquals('test', $recs[1]->getSourceIdentifier()); - $this->assertEquals('http://libguides.brynmawr.edu/psyctests-measures?hs=a', $recs[1]->getUniqueID()); + $this->assertEquals('https://guides.tricolib.brynmawr.edu/tests-measures', $recs[1]->getUniqueID()); $this->assertEquals('test', $recs[2]->getSourceIdentifier()); - $this->assertEquals('http://libguides.brynmawr.edu/social-work?hs=a', $recs[2]->getUniqueID()); - $this->assertEquals(40, $coll->getTotal()); + $this->assertEquals('https://guides.tricolib.brynmawr.edu/psyctests-measures', $recs[2]->getUniqueID()); + $this->assertEquals(53, $coll->getTotal()); $this->assertEquals(0, $coll->getOffset()); } diff --git a/themes/bootstrap3/templates/RecordDriver/LibGuides/result-list.phtml b/themes/bootstrap3/templates/RecordDriver/LibGuides/result-list.phtml index 0f15374cdbf..d7b3a4f0b72 100644 --- a/themes/bootstrap3/templates/RecordDriver/LibGuides/result-list.phtml +++ b/themes/bootstrap3/templates/RecordDriver/LibGuides/result-list.phtml @@ -10,5 +10,12 @@ + driver->getSummary() as $summaryItem): ?> + +
+ escapeHtml($summaryItem)?> +
+ +