Skip to content
This repository
Browse code

Fix #65, add test for title helper, forward Erfurt

- fix caching of titles within title helper class
- fix querying for titles on subsequent calls to addResource and getTitle
- add test class with test for #65
- modify title helper to allow for array config
- forward Erfurt submodule (contains an adapted test store adapter)
  • Loading branch information...
commit caaf1b1ad7d1052e26481704b7f8ce30c03aa4e1 1 parent 5b76ec1
Philipp Frischmuth authored September 04, 2012
112  application/classes/OntoWiki/Model/TitleHelper.php
@@ -75,7 +75,7 @@ class OntoWiki_Model_TitleHelper
75 75
      * Array of resource titles found
76 76
      * @var array
77 77
      */
78  
-    protected $_resourceTitles = null;
  78
+    protected $_resourceTitles = array();
79 79
 
80 80
     /**
81 81
      * Erfurt store object
@@ -100,7 +100,7 @@ class OntoWiki_Model_TitleHelper
100 100
      * Result set from the title query
101 101
      * @var array
102 102
      */
103  
-    protected $_titleQueryResults = null;
  103
+    protected $_titleQueryResults = array();
104 104
 
105 105
     private static $_instance = null;
106 106
 
@@ -111,9 +111,9 @@ class OntoWiki_Model_TitleHelper
111 111
     /**
112 112
      * Constructs a new title helper instance.
113 113
      *
114  
-     * @param Erfrt_Rdf_Model $model The model instance to operate on
  114
+     * @param Erfurt_Rdf_Model $model The model instance to operate on
115 115
      */
116  
-    public function __construct(Erfurt_Rdf_Model $model = null, Erfurt_Store $store = null)
  116
+    public function __construct(Erfurt_Rdf_Model $model = null, Erfurt_Store $store = null, $config = null)
117 117
     {
118 118
         if (null !== $model) {
119 119
             $this->_model = $model;
@@ -124,24 +124,41 @@ public function __construct(Erfurt_Rdf_Model $model = null, Erfurt_Store $store
124 124
         } else {
125 125
             $this->_store = Erfurt_App::getInstance()->getStore();
126 126
         }
127  
-        
128  
-        $config  = OntoWiki::getInstance()->config;
129  
-        if (isset($config->titleHelper->properties)) {// naming properties for resources
130  
-             $this->_titleProperties = array_values($config->titleHelper->properties->toArray());
  127
+
  128
+        if (null == $config) {
  129
+            $config  = OntoWiki::getInstance()->config;
  130
+        }
  131
+        if (is_array($config)) {
  132
+            if (isset($config['titleHelper']['properties'])) {// naming properties for resources
  133
+                $this->_titleProperties = array_values($config['titleHelper']['properties']);
  134
+            } else {
  135
+                $this->_titleProperties = array();
  136
+            }
  137
+
  138
+            // fetch mode
  139
+            if (isset($config['titleHelper']['searchMode'])) {
  140
+                $this->_alwaysSearchAllProperties = (strtolower($config['titleHelper']['searchMode']) === 'language');
  141
+            }
  142
+        } else if ($config instanceof Zend_Config) {
  143
+            if (isset($config->titleHelper->properties)) {// naming properties for resources
  144
+                $this->_titleProperties = array_values($config->titleHelper->properties->toArray());
  145
+            } else {
  146
+                $this->_titleProperties = array();
  147
+            }
  148
+
  149
+            // fetch mode
  150
+            if (isset($config->titleHelper->searchMode)) {
  151
+                $this->_alwaysSearchAllProperties = (strtolower($config->titleHelper->searchMode) == 'language');
  152
+            }
131 153
         } else {
132 154
             $this->_titleProperties = array();
133 155
         }
134  
-       
135  
-        // fetch mode
136  
-        if (isset($config->titleHelper->searchMode)) {
137  
-            $this->_alwaysSearchAllProperties = (strtolower($config->titleHelper->searchMode) == 'language');
138  
-        }
139  
-        
  156
+
140 157
         // always use local name for unknown resources?
141 158
         if (isset($config->titleHelper->useLocalNames)) {
142 159
             $this->_alwaysUseLocalNames = (bool)$config->titleHelper->useLocalNames;
143 160
         }
144  
-        
  161
+
145 162
         if (null === $this->_languages) {
146 163
             $this->_languages = array();
147 164
         }
@@ -296,7 +313,7 @@ public function getTitle($resourceUri, $language = null)
296 313
             }
297 314
 
298 315
             // if this is the first getTitle request, fetch titles
299  
-            if (null === $this->_resourceTitles) {
  316
+            if (!isset($this->_resourceTitles[$resourceUri])) {
300 317
                 $this->_fetchResourceTitlesFromQueryResult();
301 318
             }
302 319
 
@@ -306,6 +323,7 @@ public function getTitle($resourceUri, $language = null)
306 323
             if (null !== $language) {
307 324
                 array_unshift($languages, (string)$language);
308 325
             }
  326
+            $languages = array_unique($languages);
309 327
 
310 328
             $title = null;
311 329
             // has anything been found for the resource?
@@ -326,7 +344,7 @@ public function getTitle($resourceUri, $language = null)
326 344
                             ) {
327 345
                                 $title = $titleProperties[$currentTitleProperty][$currentLanguage];
328 346
                                 $currentBestLanguage = $i;
329  
-                                // var_dump(sprintf('%d/%d: %s', $currentBestLanguage, $i, $title));
  347
+                                #var_dump(sprintf('%d/%d: %s', $currentBestLanguage, $i, $title));
330 348
 
331 349
                                 if (!$this->_alwaysSearchAllProperties || ($currentBestLanguage === 0)) {
332 350
                                     // it won't get better :)
@@ -347,6 +365,8 @@ public function getTitle($resourceUri, $language = null)
347 365
                     $title = OntoWiki_Utils::getUriLocalPart($resourceUri);
348 366
                 }
349 367
             }
  368
+
  369
+            self::$_titleCache[(string)$this->_model][$resourceUri] = $title;
350 370
         } else {
351 371
             // cached title
352 372
             $title = self::$_titleCache[(string) $this->_model][$resourceUri];
@@ -362,35 +382,39 @@ public function getTitle($resourceUri, $language = null)
362 382
      */
363 383
     public function getTitleQueryResult()
364 384
     {
365  
-        if (null === $this->_titleQueryResults) {
366  
-            $execObject = $this->_model ? $this->_model : $this->_store;
367  
-            $this->_titleQueryResults = array();
368  
-
369  
-            // get results for all queries
370  
-            $queries = $this->getTitleQueries();
371  
-            foreach ($queries as $resourceUri=>$currentQuery) {
372  
-                $queryResults = $execObject->sparqlQuery($currentQuery, array('result_format' => 'extended'));
373  
-
374  
-                if (
375  
-                    is_array($queryResults)
376  
-                    && isset($queryResults['head']['vars'])
377  
-                    && !empty($queryResults['head']['vars'])
378  
-                ) {
379  
-                    $this->_titleQueryResults[$resourceUri] = $queryResults;
380  
-                }
  385
+        $execObject = $this->_model;
  386
+        if (null !== $this->_store) {
  387
+            $execObject = $this->_store;
  388
+        }
  389
+
  390
+        // get results for all queries
  391
+        $queries = $this->getTitleQueries();
  392
+        foreach ($queries as $resourceUri=>$currentQuery) {
  393
+            if (isset($this->_titleQueryResults[$resourceUri])) {
  394
+                continue;
381 395
             }
382 396
 
383  
-            if (defined('_OWDEBUG')) {
384  
-                $numQueries = count($queries);
385  
-                
386  
-                $logger = OntoWiki::getInstance()->logger;
387  
-                
388  
-                $logger->info(
389  
-                    'TitleHelper: ' . $numQueries . ' queries with ' . count($this->_resources) . ' resources.'
390  
-                );
  397
+            $queryResults = $execObject->sparqlQuery($currentQuery, array('result_format' => 'extended'));
  398
+
  399
+            if (
  400
+                is_array($queryResults)
  401
+                && isset($queryResults['head']['vars'])
  402
+                && !empty($queryResults['head']['vars'])
  403
+            ) {
  404
+                $this->_titleQueryResults[$resourceUri] = $queryResults;
391 405
             }
392 406
         }
393 407
 
  408
+        if (defined('_OWDEBUG')) {
  409
+            $numQueries = count($queries);
  410
+
  411
+            $logger = OntoWiki::getInstance()->logger;
  412
+
  413
+            $logger->info(
  414
+                'TitleHelper: ' . $numQueries . ' queries with ' . count($this->_resources) . ' resources.'
  415
+            );
  416
+        }
  417
+
394 418
         return $this->_titleQueryResults;
395 419
     }
396 420
 
@@ -444,10 +468,10 @@ public function reset()
444 468
     {
445 469
         $this->_resources      = null;
446 470
         $this->_resourceQuery  = null;
447  
-        $this->_resourceTitles = null;
  471
+        $this->_resourceTitles = array();
448 472
 
449 473
         $this->_titleQuery        = null;
450  
-        $this->_titleQueryResults = null;
  474
+        $this->_titleQueryResults = array();
451 475
     }
452 476
 
453 477
     // ------------------------------------------------------------------------
@@ -460,8 +484,6 @@ public function reset()
460 484
      */
461 485
     protected function _fetchResourceTitlesFromQueryResult()
462 486
     {
463  
-        $this->_resourceTitles = array();
464  
-
465 487
         foreach ($this->getTitleQueryResult() as $resourceUri=>$titleQueryResult) {
466 488
             // fetch result
467 489
             $queryResult = $titleQueryResult;
127  application/tests/unit/OntoWiki/Model/TitleHelperTest.php
... ...
@@ -0,0 +1,127 @@
  1
+<?php
  2
+    /**
  3
+     *
  4
+     * @author Philipp Frischmuth <pfrischmuth@gmail.com>
  5
+     */
  6
+class OntoWiki_Model_TitleHelperTest extends Erfurt_TestCase
  7
+{
  8
+    /** @var Erfurt_Store_Adapter_test */
  9
+    private $_storeAdapter = null;
  10
+
  11
+    /** @var Erfurt_Store */
  12
+    private  $_store = null;
  13
+
  14
+    public function setUp()
  15
+    {
  16
+        $this->markTestNeedsTestConfig();
  17
+
  18
+        $this->_storeAdapter = new Erfurt_Store_Adapter_Test();
  19
+
  20
+        $this->_store = new Erfurt_Store(
  21
+            array(
  22
+                 'adapterInstance' => $this->_storeAdapter
  23
+            ),
  24
+            'Test'
  25
+        );
  26
+    }
  27
+
  28
+    public function testMultipleAddResourceGetTitleCallsGithubIssue65()
  29
+    {
  30
+        $queryResult1 = array(
  31
+            'head' => array(
  32
+                'vars' => array(
  33
+                    'property',
  34
+                    'value'
  35
+                )
  36
+            ),
  37
+            'results' => array(
  38
+                'bindings' => array(
  39
+                    array(
  40
+                        'property' => array(
  41
+                            'value' => 'http://www.w3.org/2000/01/rdf-schema#label',
  42
+                        ),
  43
+                        'value' => array(
  44
+                            'value'    => 'testABC_en',
  45
+                            'xml:lang' => 'en'
  46
+                        )
  47
+                    )
  48
+                )
  49
+            )
  50
+        );
  51
+        $this->_storeAdapter->addQueryResult($queryResult1);
  52
+
  53
+        $queryResult2 = array(
  54
+            'head' => array(
  55
+                'vars' => array(
  56
+                    'property',
  57
+                    'value'
  58
+                )
  59
+            ),
  60
+            'results' => array(
  61
+                'bindings' => array(
  62
+                    array(
  63
+                        'property' => array(
  64
+                            'value' => 'http://www.w3.org/2004/02/skos/core#prefLabel',
  65
+                        ),
  66
+                        'value' => array(
  67
+                            'value'    => 'testABC_noLang'
  68
+                        )
  69
+                    ),
  70
+                    array(
  71
+                        'property' => array(
  72
+                            'value' => 'http://www.w3.org/2000/01/rdf-schema#label',
  73
+                        ),
  74
+                        'value' => array(
  75
+                            'value'    => 'testABC_de',
  76
+                            'xml:lang' => 'de'
  77
+                        )
  78
+                    )
  79
+                )
  80
+            )
  81
+        );
  82
+        $this->_storeAdapter->addQueryResult($queryResult2);
  83
+
  84
+        $graph = new Erfurt_Rdf_Model('http://example.org/graph123/');
  85
+        $properties = array(
  86
+            'testABC_en@en' => 'http://purl.org/dc/terms/title',
  87
+            'testABC_de@de' => 'http://example.org/resourceXYZ',
  88
+            'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
  89
+        );
  90
+
  91
+        $config = array(
  92
+            'titleHelper' => array(
  93
+                'properties' => array(
  94
+                    'skosPlabel' => 'http://www.w3.org/2004/02/skos/core#prefLabel',
  95
+                    'dcTitle'    => 'http://purl.org/dc/elements/1.1/title',
  96
+                    'dcTitle2'   => 'http://purl.org/dc/terms/title',
  97
+                    'swrcTitle'  => 'http://swrc.ontoware.org/ontology#title',
  98
+                    'foafName'   => 'http://xmlns.com/foaf/0.1/name',
  99
+                    'doapName'   => 'http://usefulinc.com/ns/doap#name',
  100
+                    'siocName'   => 'http://rdfs.org/sioc/ns#name',
  101
+                    'tagName'    => 'http://www.holygoat.co.uk/owl/redwood/0.1/tags/name',
  102
+                    'lgeodName'  => 'http://linkedgeodata.org/vocabulary#name',
  103
+                    'geoName'    => 'http://www.geonames.org/ontology#name',
  104
+                    'goName'     => 'http://www.geneontology.org/dtds/go.dtd#name',
  105
+                    'rdfsLabel'  => 'http://www.w3.org/2000/01/rdf-schema#label'
  106
+                ),
  107
+                'searchMode' => 'language'
  108
+            )
  109
+        );
  110
+
  111
+        $titleHelper = new OntoWiki_Model_TitleHelper($graph, $this->_store, $config);
  112
+        foreach ($properties as $expected => $property) {
  113
+            $resource = $property;
  114
+            $lang = null;
  115
+            $expectedTitle = $expected;
  116
+            if (strpos($expected, '@') !== false) {
  117
+                $parts = explode('@', $expected);
  118
+                $expectedTitle = $parts[0];
  119
+                $lang = $parts[1];
  120
+            }
  121
+
  122
+            $titleHelper->addResource($resource);
  123
+            $title = $titleHelper->getTitle($property, $lang);
  124
+            $this->assertEquals($expectedTitle, $title);
  125
+        }
  126
+    }
  127
+}
2  libraries/Erfurt
... ...
@@ -1 +1 @@
1  
-Subproject commit 8fc1e3ed37391197848f65ceb4b462e407108d1f
  1
+Subproject commit e8d1b3e1deb5601d7db6c2e3dc427db622f49217

0 notes on commit caaf1b1

Please sign in to comment.
Something went wrong with that request. Please try again.