Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibGuides and AZ: Display descriptions #3128

Merged
merged 13 commits into from
Oct 3, 2023
Merged
5 changes: 4 additions & 1 deletion config/vufind/LibGuides.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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"
;defaultSearch = "general"

; Parse and display the description below each title
;displayDescription = true
1 change: 1 addition & 0 deletions config/vufind/LibGuidesAZ.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ relative_path = ./LibGuides.ini
;default_limit = 20
;limit_options = 10,20,40,60,80,100
;defaultSearch = "general"
;displayDescription = true
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -101,6 +109,7 @@ public function __construct($iid, $client, $apiVersion = 1, $baseUrl = null)
}
$this->iid = $iid;
$this->client = $client;
$this->displayDescription = $displayDescription;
}

/**
Expand Down Expand Up @@ -187,15 +196,33 @@ protected function process($data)

$items = [];

// Extract titles and URLs from response:
$regex = '/<a href="([^"]*)"[^>]*>([^<]*)</';
$count = preg_match_all($regex, $data, $matches);
$itemRegex = '/<li>(.*?)<\/li>/';
$linkRegex = '/<a href="([^"]*)"[^>]*>([^<]*)</';
$descriptionRegex = '/<div class="s-lg-(?:widget|guide)-list-description">(.*?)<\/div>/';
maccabeelevine marked this conversation as resolved.
Show resolved Hide resolved

// 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 = [
Expand Down Expand Up @@ -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,
Expand Down
Binary file modified module/VuFindSearch/tests/fixtures/libguides/response/search
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@
</a>
</h2>
</div>
<?php foreach ($this->driver->getSummary() as $summaryItem): ?>
<?php if (!empty($summaryItem)): ?>
<div class="resultItemLine1 summary">
<?=$this->escapeHtml($summaryItem)?>
demiankatz marked this conversation as resolved.
Show resolved Hide resolved
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>