From 8e920494da5838336276676da88a738f6f653f74 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 13 Sep 2012 11:02:24 -0400
Subject: [PATCH] Load WorldCatUtils through service manager; set up logging
 through dependency injection; fixed namespace bug related to File_MARC class.

---
 module/VuFind/config/module.config.php        |  7 +++
 .../src/VuFind/Connection/WorldCatUtils.php   | 43 ++++++++++++++++---
 .../VuFind/Recommend/WorldCatIdentities.php   |  7 ++-
 .../src/VuFind/Recommend/WorldCatTerms.php    |  5 +--
 module/VuFind/src/VuFind/Related/Editions.php |  3 +-
 .../src/VuFind/Related/WorldCatEditions.php   |  3 +-
 6 files changed, 52 insertions(+), 16 deletions(-)

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index d1805c3fdae..2c725564370 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -259,6 +259,13 @@ $config = array(
         ),
     ),
     'service_manager' => array(
+        'factories' => array(
+            'worldcatutils' => function ($sm) {
+                $wcu = new \VuFind\Connection\WorldCatUtils();
+                $wcu->setLogger(\VuFind\Log\Logger::getInstance());
+                return $wcu;
+            },
+        ),
         'invokables' => array(
             'authmanager' => 'VuFind\Auth\Manager',
             'cart' => 'VuFind\Cart',
diff --git a/module/VuFind/src/VuFind/Connection/WorldCatUtils.php b/module/VuFind/src/VuFind/Connection/WorldCatUtils.php
index e9d4d8b1884..bc2c5a7c208 100644
--- a/module/VuFind/src/VuFind/Connection/WorldCatUtils.php
+++ b/module/VuFind/src/VuFind/Connection/WorldCatUtils.php
@@ -26,8 +26,8 @@
  * @link     http://vufind.org/wiki/system_classes Wiki
  */
 namespace VuFind\Connection;
-use VuFind\Config\Reader as ConfigReader, VuFind\Log\Logger,
-    VuFind\XSLT\Processor as XSLTProcessor;
+use File_MARCXML, VuFind\Config\Reader as ConfigReader,
+    VuFind\XSLT\Processor as XSLTProcessor, Zend\Log\Logger;
 
 /**
  * World Cat Utilities
@@ -42,6 +42,39 @@ use VuFind\Config\Reader as ConfigReader, VuFind\Log\Logger,
  */
 class WorldCatUtils
 {
+    /**
+     * Logger (or false for none)
+     *
+     * @var Logger|bool
+     */
+    protected $logger = false;
+
+    /**
+     * Set the logger
+     *
+     * @param Logger $logger Logger to use.
+     *
+     * @return void
+     */
+    public function setLogger(Logger $logger)
+    {
+        $this->logger = $logger;
+    }
+
+    /**
+     * Log a debug message.
+     *
+     * @param string $msg Message to log.
+     *
+     * @return void
+     */
+    protected function debug($msg)
+    {
+        if ($this->logger) {
+            $this->logger->debug($msg);
+        }
+    }
+
     /**
      * Get the WorldCat ID from the config file.
      *
@@ -76,7 +109,7 @@ class WorldCatUtils
         }
 
         // Print Debug code
-        Logger::getInstance()->debug("XISBN: $url");
+        $this->debug("XISBN: $url");
 
         // Fetch results
         $isbns = array();
@@ -114,7 +147,7 @@ class WorldCatUtils
         }
 
         // Print Debug code
-        Logger::getInstance()->debug("XOCLCNUM: $url");
+        $this->debug("XOCLCNUM: $url");
 
         // Fetch results
         $results = array();
@@ -153,7 +186,7 @@ class WorldCatUtils
         }
 
         // Print Debug code
-        Logger::getInstance()->debug("XISSN: $url");
+        $this->debug("XISSN: $url");
 
         // Fetch results
         $issns = array();
diff --git a/module/VuFind/src/VuFind/Recommend/WorldCatIdentities.php b/module/VuFind/src/VuFind/Recommend/WorldCatIdentities.php
index 13d38630276..86369a457c7 100644
--- a/module/VuFind/src/VuFind/Recommend/WorldCatIdentities.php
+++ b/module/VuFind/src/VuFind/Recommend/WorldCatIdentities.php
@@ -26,7 +26,6 @@
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
 namespace VuFind\Recommend;
-use VuFind\Connection\WorldCatUtils;
 
 /**
  * WorldCatIdentities Recommendations Module
@@ -39,7 +38,7 @@ use VuFind\Connection\WorldCatUtils;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
-class WorldCatIdentities implements RecommendInterface
+class WorldCatIdentities extends AbstractSearchManagerAwareModule
 {
     protected $searchObject;
     protected $settings;
@@ -106,7 +105,7 @@ class WorldCatIdentities implements RecommendInterface
         $lookfor = isset($search[0]['lookfor']) ? $search[0]['lookfor'] : '';
 
         // Get terminology information:
-        $wc = new WorldCatUtils();
-        return $wc->getRelatedIdentities($lookfor);
+        return $this->getServiceLocator()->getServiceLocator()->get('WorldCatUtils')
+            ->getRelatedIdentities($lookfor);
     }
 }
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/Recommend/WorldCatTerms.php b/module/VuFind/src/VuFind/Recommend/WorldCatTerms.php
index 58bc4c3844e..16ddd8da4ac 100644
--- a/module/VuFind/src/VuFind/Recommend/WorldCatTerms.php
+++ b/module/VuFind/src/VuFind/Recommend/WorldCatTerms.php
@@ -26,7 +26,6 @@
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
 namespace VuFind\Recommend;
-use VuFind\Connection\WorldCatUtils;
 
 /**
  * WorldCatTerms Recommendations Module
@@ -39,7 +38,7 @@ use VuFind\Connection\WorldCatUtils;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
-class WorldCatTerms implements RecommendInterface
+class WorldCatTerms extends AbstractSearchManagerAwareModule
 {
     protected $searchObject;
     protected $vocab = 'lcsh';
@@ -107,7 +106,7 @@ class WorldCatTerms implements RecommendInterface
         $lookfor = isset($search[0]['lookfor']) ? $search[0]['lookfor'] : '';
 
         // Get terminology information:
-        $wc = new WorldCatUtils();
+        $wc = $this->getServiceLocator()->getServiceLocator()->get('WorldCatUtils');
         $terms = $wc->getRelatedTerms($lookfor, $this->vocab);
 
         // Wipe out any empty or unexpected sections of the related terms array;
diff --git a/module/VuFind/src/VuFind/Related/Editions.php b/module/VuFind/src/VuFind/Related/Editions.php
index f0fd16915f6..136706857e2 100644
--- a/module/VuFind/src/VuFind/Related/Editions.php
+++ b/module/VuFind/src/VuFind/Related/Editions.php
@@ -26,7 +26,6 @@
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
 namespace VuFind\Related;
-use VuFind\Connection\WorldCatUtils;
 
 /**
  * Related Records: WorldCat-based editions list (Solr results)
@@ -103,7 +102,7 @@ class Editions extends AbstractServiceLocator
      */
     protected function getQueryParts($driver)
     {
-        $wc = new WorldCatUtils();
+        $wc = $this->getServiceLocator()->get('WorldCatUtils');
         $parts = array();
         if (method_exists($driver, 'getCleanOCLCNum')) {
             $oclcNum = $driver->getCleanOCLCNum();
diff --git a/module/VuFind/src/VuFind/Related/WorldCatEditions.php b/module/VuFind/src/VuFind/Related/WorldCatEditions.php
index 68d90bf4591..38bc297f150 100644
--- a/module/VuFind/src/VuFind/Related/WorldCatEditions.php
+++ b/module/VuFind/src/VuFind/Related/WorldCatEditions.php
@@ -26,7 +26,6 @@
  * @link     http://vufind.org/wiki/building_a_recommendations_module Wiki
  */
 namespace VuFind\Related;
-use VuFind\Connection\WorldCatUtils;
 
 /**
  * Related Records: WorldCat-based editions list (WorldCat results)
@@ -81,7 +80,7 @@ class WorldCatEditions extends Editions
      */
     protected function getQueryParts($driver)
     {
-        $wc = new WorldCatUtils();
+        $wc = $this->getServiceLocator()->get('WorldCatUtils');
         $parts = array();
         if (method_exists($driver, 'getCleanISBN')) {
             $isbn = $driver->getCleanISBN();
-- 
GitLab