Skip to content
Snippets Groups Projects
Commit d325f96d authored by Ere Maijala's avatar Ere Maijala
Browse files

Added getRecords() to Primo connector so that multiple records can be retrieved at once.

parent a190a3a0
No related merge requests found
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
*/ */
namespace VuFindSearch\Backend\Primo; namespace VuFindSearch\Backend\Primo;
use VuFindSearch\Feature\RetrieveBatchInterface;
use VuFindSearch\Query\AbstractQuery; use VuFindSearch\Query\AbstractQuery;
use VuFindSearch\ParamBag; use VuFindSearch\ParamBag;
...@@ -47,7 +49,7 @@ use VuFindSearch\Backend\Exception\BackendException; ...@@ -47,7 +49,7 @@ use VuFindSearch\Backend\Exception\BackendException;
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org * @link http://vufind.org
*/ */
class Backend extends AbstractBackend class Backend extends AbstractBackend implements RetrieveBatchInterface
{ {
/** /**
* Connector. * Connector.
...@@ -146,6 +148,49 @@ class Backend extends AbstractBackend ...@@ -146,6 +148,49 @@ class Backend extends AbstractBackend
return $collection; return $collection;
} }
/**
* Retrieve a batch of documents.
*
* @param array $ids Array of document identifiers
* @param ParamBag $params Search backend parameters
*
* @return RecordCollectionInterface
*/
public function retrieveBatch($ids, ParamBag $params = null)
{
// Load 100 records at a time; this is a good number to avoid memory
// problems while still covering a lot of ground.
$pageSize = 100;
// Retrieve records a page at a time:
$results = false;
while (count($ids) > 0) {
$currentPage = array_splice($ids, 0, $pageSize, []);
try {
$response = $this->connector->getRecords(
$currentPage, $this->connector->getInstitutionCode()
);
} catch (\Exception $e) {
throw new BackendException(
$e->getMessage(),
$e->getCode(),
$e
);
}
$next = $this->createRecordCollection($response);
if (!$results) {
$results = $next;
} else {
foreach ($next->getRecords() as $record) {
$results->add($record);
}
}
}
$this->injectSourceIdentifier($results);
return $results;
}
/** /**
* Set the query builder. * Set the query builder.
* *
......
...@@ -604,7 +604,8 @@ class Connector implements \Zend\Log\LoggerAwareInterface ...@@ -604,7 +604,8 @@ class Connector implements \Zend\Log\LoggerAwareInterface
// Query String Parameters // Query String Parameters
if (isset($recordId)) { if (isset($recordId)) {
$qs = []; $qs = [];
$qs[] = "query=rid,exact,\"$recordId\""; $qs[] = 'query=rid,exact,"' . urlencode(addcslashes($recordId, '"'))
. '"';
$qs[] = "institution=$inst_code"; $qs[] = "institution=$inst_code";
$qs[] = "onCampus=true"; $qs[] = "onCampus=true";
$qs[] = "indx=1"; $qs[] = "indx=1";
...@@ -620,6 +621,42 @@ class Connector implements \Zend\Log\LoggerAwareInterface ...@@ -620,6 +621,42 @@ class Connector implements \Zend\Log\LoggerAwareInterface
return $result; return $result;
} }
/**
* Retrieves multiple documents specified by the ID.
*
* @param array $recordIds The documents to retrieve from the Primo API
* @param string $inst_code Institution code (optional)
*
* @throws \Exception
* @return string The requested resource
*/
public function getRecords($recordIds, $inst_code = null)
{
// Callback function for formatting IDs:
$formatIds = function ($i) {
return '"' . addcslashes($i, '"') . '"';
};
// Query String Parameters
if ($recordIds) {
$qs = [];
$recordIds = array_map($formatIds, $recordIds);
$qs[] = 'query=rid,contains,' . urlencode(implode(' OR ', $recordIds));
$qs[] = "institution=$inst_code";
$qs[] = "onCampus=true";
$qs[] = "indx=1";
$qs[] = "bulkSize=" . count($recordIds);
$qs[] = "loc=adaptor,primo_central_multiple_fe";
// Send Request
$result = $this->call(implode('&', $qs));
} else {
throw new \Exception('Primo API does not accept a null query');
}
return $result;
}
/** /**
* Get the institution code based on user IP. If user is coming from * Get the institution code based on user IP. If user is coming from
* off campus return * off campus return
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment