From a77354fd2a978f285c91dfe69a3d950408add05d Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 19 Jul 2017 13:02:00 -0400
Subject: [PATCH] Add useFrbrGroupingForHoldings WorldCat setting.

---
 config/vufind/WorldCat.ini                    |  7 +++++
 .../Search/Factory/WorldCatBackendFactory.php | 14 +++++++++-
 .../Backend/WorldCat/Connector.php            | 27 ++++++++++++++-----
 3 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/config/vufind/WorldCat.ini b/config/vufind/WorldCat.ini
index d8fce1f8e44..d78e36b365b 100644
--- a/config/vufind/WorldCat.ini
+++ b/config/vufind/WorldCat.ini
@@ -69,3 +69,10 @@ related[] = "WorldCatSimilar"
 ;       regular Syndetics if necessary.
 [List]
 view=full
+
+; This section contains additional settings to pass to the WorldCat connector
+; code.
+[Connector]
+; When looking up holdings at other libraries, should we retrieve holdings for
+; any record matching the FRBR group (true) or only for exact matches (false)?
+;useFrbrGroupingForHoldings = false
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/Search/Factory/WorldCatBackendFactory.php b/module/VuFind/src/VuFind/Search/Factory/WorldCatBackendFactory.php
index b904866bab0..d39d468a09e 100644
--- a/module/VuFind/src/VuFind/Search/Factory/WorldCatBackendFactory.php
+++ b/module/VuFind/src/VuFind/Search/Factory/WorldCatBackendFactory.php
@@ -69,6 +69,13 @@ class WorldCatBackendFactory implements FactoryInterface
      */
     protected $config;
 
+    /**
+     * WorldCat configuration
+     *
+     * @var \Zend\Config\Config
+     */
+    protected $wcConfig;
+
     /**
      * Create the backend.
      *
@@ -80,6 +87,8 @@ class WorldCatBackendFactory implements FactoryInterface
     {
         $this->serviceLocator = $serviceLocator;
         $this->config = $this->serviceLocator->get('VuFind\Config')->get('config');
+        $this->wcConfig = $this->serviceLocator
+            ->get('VuFind\Config')->get('WorldCat');
         if ($this->serviceLocator->has('VuFind\Logger')) {
             $this->logger = $this->serviceLocator->get('VuFind\Logger');
         }
@@ -112,8 +121,11 @@ class WorldCatBackendFactory implements FactoryInterface
     {
         $wsKey = isset($this->config->WorldCat->apiKey)
             ? $this->config->WorldCat->apiKey : null;
+        $connectorOptions = isset($this->wcConfig->Connector)
+            ? $this->wcConfig->Connector->toArray() : [];
         $connector = new Connector(
-            $wsKey, $this->serviceLocator->get('VuFind\Http')->createClient()
+            $wsKey, $this->serviceLocator->get('VuFind\Http')->createClient(),
+            $connectorOptions
         );
         $connector->setLogger($this->logger);
         return $connector;
diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Connector.php b/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Connector.php
index 207baa86840..056dfe3f8de 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Connector.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/WorldCat/Connector.php
@@ -47,18 +47,28 @@ class Connector extends \VuFindSearch\Backend\SRU\Connector
      */
     protected $wskey;
 
+    /**
+     * Additional options
+     *
+     * @var array
+     */
+    protected $options;
+
     /**
      * Constructor
      *
-     * @param string            $wsKey  Web services key
-     * @param \Zend\Http\Client $client An HTTP client object
+     * @param string            $wsKey   Web services key
+     * @param \Zend\Http\Client $client  An HTTP client object
+     * @param array             $options Additional config settings
      */
-    public function __construct($wsKey, \Zend\Http\Client $client)
-    {
+    public function __construct($wsKey, \Zend\Http\Client $client,
+        array $options = []
+    ) {
         parent::__construct(
             'http://www.worldcat.org/webservices/catalog/search/sru', $client
         );
         $this->wskey = $wsKey;
+        $this->options = $options;
     }
 
     /**
@@ -72,8 +82,13 @@ class Connector extends \VuFindSearch\Backend\SRU\Connector
     public function getHoldings($id)
     {
         $this->client->resetParameters();
-        $uri = "http://www.worldcat.org/webservices/catalog/content/libraries/{$id}";
-        $uri .= "?wskey={$this->wskey}&servicelevel=full";
+        if (!isset($this->options['useFrbrGroupingForHoldings'])) {
+            $grouping = 'on';   // default to "on" for backward compatibility
+        } else {
+            $grouping = $this->options['useFrbrGroupingForHoldings'] ? 'on' : 'off';
+        }
+        $uri = "http://www.worldcat.org/webservices/catalog/content/libraries/{$id}"
+            . "?wskey={$this->wskey}&servicelevel=full&frbrGrouping=$grouping";
         $this->client->setUri($uri);
         $this->debug('Connect: ' . $uri);
         $result = $this->client->setMethod('POST')->send();
-- 
GitLab