From f6d4bd2a4bf87357b90601abf04e9ef274bd2755 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 3 May 2013 10:53:43 -0400
Subject: [PATCH] Tag parsing is now a pluggable service; also added deduping.

---
 module/VuFind/config/module.config.php        |  3 ++
 .../src/VuFind/Controller/AbstractRecord.php  |  3 +-
 .../src/VuFind/Controller/AjaxController.php  |  3 +-
 .../Controller/MyResearchController.php       |  3 +-
 .../VuFind/Controller/Plugin/Favorites.php    |  3 +-
 module/VuFind/src/VuFind/Tags.php             |  4 +--
 .../VuFind/tests/unit-tests/src/TagsTest.php  | 29 +++++++++++++++++--
 7 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/module/VuFind/config/module.config.php b/module/VuFind/config/module.config.php
index 0ef73c0bcf1..12fc5d496de 100644
--- a/module/VuFind/config/module.config.php
+++ b/module/VuFind/config/module.config.php
@@ -277,6 +277,9 @@ $config = array(
                     $sm->get('VuFind\DbTablePluginManager')->get('changetracker')
                 );
             },
+            'VuFind\Tags' => function ($sm) {
+                return new \VuFind\Tags();
+            },
             'VuFind\Translator' => function ($sm) {
                 $factory = new \Zend\I18n\Translator\TranslatorServiceFactory();
                 $translator = $factory->createService($sm);
diff --git a/module/VuFind/src/VuFind/Controller/AbstractRecord.php b/module/VuFind/src/VuFind/Controller/AbstractRecord.php
index f190a7c99a6..71f61efdd84 100644
--- a/module/VuFind/src/VuFind/Controller/AbstractRecord.php
+++ b/module/VuFind/src/VuFind/Controller/AbstractRecord.php
@@ -188,7 +188,8 @@ class AbstractRecord extends AbstractBase
         // Save tags, if any:
         if ($this->params()->fromPost('submit')) {
             $tags = $this->params()->fromPost('tag');
-            $driver->addTags($user, \VuFind\Tags::parse($tags));
+            $tagParser = $this->getServiceLocator()->get('VuFind\Tags');
+            $driver->addTags($user, $tagParser->parse($tags));
             return $this->redirectToRecord();
         }
 
diff --git a/module/VuFind/src/VuFind/Controller/AjaxController.php b/module/VuFind/src/VuFind/Controller/AjaxController.php
index ff96d9e8c1c..bd2759e0a0c 100644
--- a/module/VuFind/src/VuFind/Controller/AjaxController.php
+++ b/module/VuFind/src/VuFind/Controller/AjaxController.php
@@ -617,8 +617,9 @@ class AjaxController extends AbstractBase
                 $this->params()->fromPost('source', 'VuFind')
             );
             $tag = $this->params()->fromPost('tag', '');
+            $tagParser = $this->getServiceLocator()->get('VuFind\Tags');
             if (strlen($tag) > 0) { // don't add empty tags
-                $driver->addTags($user, \VuFind\Tags::parse($tag));
+                $driver->addTags($user, $tagParser->parse($tag));
             }
         } catch (\Exception $e) {
             return $this->output(
diff --git a/module/VuFind/src/VuFind/Controller/MyResearchController.php b/module/VuFind/src/VuFind/Controller/MyResearchController.php
index 992ce214948..9729d0bd9da 100644
--- a/module/VuFind/src/VuFind/Controller/MyResearchController.php
+++ b/module/VuFind/src/VuFind/Controller/MyResearchController.php
@@ -386,12 +386,13 @@ class MyResearchController extends AbstractBase
     protected function processEditSubmit($user, $driver, $listID)
     {
         $lists = $this->params()->fromPost('lists');
+        $tagParser = $this->getServiceLocator()->get('VuFind\Tags');
         foreach ($lists as $list) {
             $tags = $this->params()->fromPost('tags'.$list);
             $driver->saveToFavorites(
                 array(
                     'list'  => $list,
-                    'mytags'  => \VuFind\Tags::parse($tags),
+                    'mytags'  => $tagParser->parse($tags),
                     'notes' => $this->params()->fromPost('notes'.$list)
                 ),
                 $user
diff --git a/module/VuFind/src/VuFind/Controller/Plugin/Favorites.php b/module/VuFind/src/VuFind/Controller/Plugin/Favorites.php
index 58f47ed533f..6226e828c65 100644
--- a/module/VuFind/src/VuFind/Controller/Plugin/Favorites.php
+++ b/module/VuFind/src/VuFind/Controller/Plugin/Favorites.php
@@ -73,6 +73,7 @@ class Favorites extends AbstractPlugin
         }
 
         // Loop through all the IDs and save them:
+        $tagParser = $this->getController()->getServiceLocator()->get('VuFind\Tags');
         foreach ($params['ids'] as $current) {
             // Break apart components of ID:
             list($source, $id) = explode('|', $current, 2);
@@ -83,7 +84,7 @@ class Favorites extends AbstractPlugin
 
             // Add the information to the user's account:
             $tags = isset($params['mytags'])
-                ? \VuFind\Tags::parse($params['mytags']) : array();
+                ? $tagParser->parse($params['mytags']) : array();
             $user->saveResource($resource, $list, $tags, '', false);
         }
     }
diff --git a/module/VuFind/src/VuFind/Tags.php b/module/VuFind/src/VuFind/Tags.php
index f7c376cb695..1716490aa64 100644
--- a/module/VuFind/src/VuFind/Tags.php
+++ b/module/VuFind/src/VuFind/Tags.php
@@ -45,13 +45,13 @@ class Tags
      *
      * @return array
      */
-    public static function parse($tags)
+    public function parse($tags)
     {
         preg_match_all('/"[^"]*"|[^ ]+/', trim($tags), $words);
         $result = array();
         foreach ($words[0] as $tag) {
             $result[] = str_replace('"', '', $tag);
         }
-        return $result;
+        return array_unique($result);
     }
 }
diff --git a/module/VuFind/tests/unit-tests/src/TagsTest.php b/module/VuFind/tests/unit-tests/src/TagsTest.php
index 1fb820b23f0..5b338962b92 100644
--- a/module/VuFind/tests/unit-tests/src/TagsTest.php
+++ b/module/VuFind/tests/unit-tests/src/TagsTest.php
@@ -38,6 +38,21 @@ namespace VuFindTest;
  */
 class TagsTest extends \PHPUnit_Framework_TestCase
 {
+    /**
+     * Tag parser
+     *
+     * @var \VuFind\Tags
+     */
+    protected $parser;
+
+    /**
+     * Constructor
+     */
+    public function __construct()
+    {
+        $this->parser = new \VuFind\Tags();
+    }
+
     /**
      * Test tag parsing
      *
@@ -47,7 +62,7 @@ class TagsTest extends \PHPUnit_Framework_TestCase
     {
         $this->assertEquals(
             array('this', 'that', 'the other'),
-            \VuFind\Tags::parse('this that "the other"')
+            $this->parser->parse('this that "the other"')
         );
     }
 
@@ -58,6 +73,16 @@ class TagsTest extends \PHPUnit_Framework_TestCase
      */
     public function testEmptyTagParsing()
     {
-        $this->assertEquals(array(), \VuFind\Tags::parse(''));
+        $this->assertEquals(array(), $this->parser->parse(''));
+    }
+
+    /**
+     * Test deduplication
+     *
+     * @return void
+     */
+    public function testDeduplication()
+    {
+        $this->assertEquals(array('test'), $this->parser->parse('test test test'));
     }
 }
\ No newline at end of file
-- 
GitLab