Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@pfrischmuth pfrischmuth authored
View
112 application/classes/OntoWiki/Model/TitleHelper.php
@@ -75,7 +75,7 @@ class OntoWiki_Model_TitleHelper
* Array of resource titles found
* @var array
*/
- protected $_resourceTitles = null;
+ protected $_resourceTitles = array();
/**
* Erfurt store object
@@ -100,7 +100,7 @@ class OntoWiki_Model_TitleHelper
* Result set from the title query
* @var array
*/
- protected $_titleQueryResults = null;
+ protected $_titleQueryResults = array();
private static $_instance = null;
@@ -111,9 +111,9 @@ class OntoWiki_Model_TitleHelper
/**
* Constructs a new title helper instance.
*
- * @param Erfrt_Rdf_Model $model The model instance to operate on
+ * @param Erfurt_Rdf_Model $model The model instance to operate on
*/
- public function __construct(Erfurt_Rdf_Model $model = null, Erfurt_Store $store = null)
+ public function __construct(Erfurt_Rdf_Model $model = null, Erfurt_Store $store = null, $config = null)
{
if (null !== $model) {
$this->_model = $model;
@@ -124,24 +124,41 @@ public function __construct(Erfurt_Rdf_Model $model = null, Erfurt_Store $store
} else {
$this->_store = Erfurt_App::getInstance()->getStore();
}
-
- $config = OntoWiki::getInstance()->config;
- if (isset($config->titleHelper->properties)) {// naming properties for resources
- $this->_titleProperties = array_values($config->titleHelper->properties->toArray());
+
+ if (null == $config) {
+ $config = OntoWiki::getInstance()->config;
+ }
+ if (is_array($config)) {
+ if (isset($config['titleHelper']['properties'])) {// naming properties for resources
+ $this->_titleProperties = array_values($config['titleHelper']['properties']);
+ } else {
+ $this->_titleProperties = array();
+ }
+
+ // fetch mode
+ if (isset($config['titleHelper']['searchMode'])) {
+ $this->_alwaysSearchAllProperties = (strtolower($config['titleHelper']['searchMode']) === 'language');
+ }
+ } else if ($config instanceof Zend_Config) {
+ if (isset($config->titleHelper->properties)) {// naming properties for resources
+ $this->_titleProperties = array_values($config->titleHelper->properties->toArray());
+ } else {
+ $this->_titleProperties = array();
+ }
+
+ // fetch mode
+ if (isset($config->titleHelper->searchMode)) {
+ $this->_alwaysSearchAllProperties = (strtolower($config->titleHelper->searchMode) == 'language');
+ }
} else {
$this->_titleProperties = array();
}
-
- // fetch mode
- if (isset($config->titleHelper->searchMode)) {
- $this->_alwaysSearchAllProperties = (strtolower($config->titleHelper->searchMode) == 'language');
- }
-
+
// always use local name for unknown resources?
if (isset($config->titleHelper->useLocalNames)) {
$this->_alwaysUseLocalNames = (bool)$config->titleHelper->useLocalNames;
}
-
+
if (null === $this->_languages) {
$this->_languages = array();
}
@@ -296,7 +313,7 @@ public function getTitle($resourceUri, $language = null)
}
// if this is the first getTitle request, fetch titles
- if (null === $this->_resourceTitles) {
+ if (!isset($this->_resourceTitles[$resourceUri])) {
$this->_fetchResourceTitlesFromQueryResult();
}
@@ -306,6 +323,7 @@ public function getTitle($resourceUri, $language = null)
if (null !== $language) {
array_unshift($languages, (string)$language);
}
+ $languages = array_unique($languages);
$title = null;
// has anything been found for the resource?
@@ -326,7 +344,7 @@ public function getTitle($resourceUri, $language = null)
) {
$title = $titleProperties[$currentTitleProperty][$currentLanguage];
$currentBestLanguage = $i;
- // var_dump(sprintf('%d/%d: %s', $currentBestLanguage, $i, $title));
+ #var_dump(sprintf('%d/%d: %s', $currentBestLanguage, $i, $title));
if (!$this->_alwaysSearchAllProperties || ($currentBestLanguage === 0)) {
// it won't get better :)
@@ -347,6 +365,8 @@ public function getTitle($resourceUri, $language = null)
$title = OntoWiki_Utils::getUriLocalPart($resourceUri);
}
}
+
+ self::$_titleCache[(string)$this->_model][$resourceUri] = $title;
} else {
// cached title
$title = self::$_titleCache[(string) $this->_model][$resourceUri];
@@ -362,35 +382,39 @@ public function getTitle($resourceUri, $language = null)
*/
public function getTitleQueryResult()
{
- if (null === $this->_titleQueryResults) {
- $execObject = $this->_model ? $this->_model : $this->_store;
- $this->_titleQueryResults = array();
-
- // get results for all queries
- $queries = $this->getTitleQueries();
- foreach ($queries as $resourceUri=>$currentQuery) {
- $queryResults = $execObject->sparqlQuery($currentQuery, array('result_format' => 'extended'));
-
- if (
- is_array($queryResults)
- && isset($queryResults['head']['vars'])
- && !empty($queryResults['head']['vars'])
- ) {
- $this->_titleQueryResults[$resourceUri] = $queryResults;
- }
+ $execObject = $this->_model;
+ if (null !== $this->_store) {
+ $execObject = $this->_store;
+ }
+
+ // get results for all queries
+ $queries = $this->getTitleQueries();
+ foreach ($queries as $resourceUri=>$currentQuery) {
+ if (isset($this->_titleQueryResults[$resourceUri])) {
+ continue;
}
- if (defined('_OWDEBUG')) {
- $numQueries = count($queries);
-
- $logger = OntoWiki::getInstance()->logger;
-
- $logger->info(
- 'TitleHelper: ' . $numQueries . ' queries with ' . count($this->_resources) . ' resources.'
- );
+ $queryResults = $execObject->sparqlQuery($currentQuery, array('result_format' => 'extended'));
+
+ if (
+ is_array($queryResults)
+ && isset($queryResults['head']['vars'])
+ && !empty($queryResults['head']['vars'])
+ ) {
+ $this->_titleQueryResults[$resourceUri] = $queryResults;
}
}
+ if (defined('_OWDEBUG')) {
+ $numQueries = count($queries);
+
+ $logger = OntoWiki::getInstance()->logger;
+
+ $logger->info(
+ 'TitleHelper: ' . $numQueries . ' queries with ' . count($this->_resources) . ' resources.'
+ );
+ }
+
return $this->_titleQueryResults;
}
@@ -444,10 +468,10 @@ public function reset()
{
$this->_resources = null;
$this->_resourceQuery = null;
- $this->_resourceTitles = null;
+ $this->_resourceTitles = array();
$this->_titleQuery = null;
- $this->_titleQueryResults = null;
+ $this->_titleQueryResults = array();
}
// ------------------------------------------------------------------------
@@ -460,8 +484,6 @@ public function reset()
*/
protected function _fetchResourceTitlesFromQueryResult()
{
- $this->_resourceTitles = array();
-
foreach ($this->getTitleQueryResult() as $resourceUri=>$titleQueryResult) {
// fetch result
$queryResult = $titleQueryResult;
View
127 application/tests/unit/OntoWiki/Model/TitleHelperTest.php
@@ -0,0 +1,127 @@
+<?php
+ /**
+ *
+ * @author Philipp Frischmuth <pfrischmuth@gmail.com>
+ */
+class OntoWiki_Model_TitleHelperTest extends Erfurt_TestCase
+{
+ /** @var Erfurt_Store_Adapter_test */
+ private $_storeAdapter = null;
+
+ /** @var Erfurt_Store */
+ private $_store = null;
+
+ public function setUp()
+ {
+ $this->markTestNeedsTestConfig();
+
+ $this->_storeAdapter = new Erfurt_Store_Adapter_Test();
+
+ $this->_store = new Erfurt_Store(
+ array(
+ 'adapterInstance' => $this->_storeAdapter
+ ),
+ 'Test'
+ );
+ }
+
+ public function testMultipleAddResourceGetTitleCallsGithubIssue65()
+ {
+ $queryResult1 = array(
+ 'head' => array(
+ 'vars' => array(
+ 'property',
+ 'value'
+ )
+ ),
+ 'results' => array(
+ 'bindings' => array(
+ array(
+ 'property' => array(
+ 'value' => 'http://www.w3.org/2000/01/rdf-schema#label',
+ ),
+ 'value' => array(
+ 'value' => 'testABC_en',
+ 'xml:lang' => 'en'
+ )
+ )
+ )
+ )
+ );
+ $this->_storeAdapter->addQueryResult($queryResult1);
+
+ $queryResult2 = array(
+ 'head' => array(
+ 'vars' => array(
+ 'property',
+ 'value'
+ )
+ ),
+ 'results' => array(
+ 'bindings' => array(
+ array(
+ 'property' => array(
+ 'value' => 'http://www.w3.org/2004/02/skos/core#prefLabel',
+ ),
+ 'value' => array(
+ 'value' => 'testABC_noLang'
+ )
+ ),
+ array(
+ 'property' => array(
+ 'value' => 'http://www.w3.org/2000/01/rdf-schema#label',
+ ),
+ 'value' => array(
+ 'value' => 'testABC_de',
+ 'xml:lang' => 'de'
+ )
+ )
+ )
+ )
+ );
+ $this->_storeAdapter->addQueryResult($queryResult2);
+
+ $graph = new Erfurt_Rdf_Model('http://example.org/graph123/');
+ $properties = array(
+ 'testABC_en@en' => 'http://purl.org/dc/terms/title',
+ 'testABC_de@de' => 'http://example.org/resourceXYZ',
+ 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
+ );
+
+ $config = array(
+ 'titleHelper' => array(
+ 'properties' => array(
+ 'skosPlabel' => 'http://www.w3.org/2004/02/skos/core#prefLabel',
+ 'dcTitle' => 'http://purl.org/dc/elements/1.1/title',
+ 'dcTitle2' => 'http://purl.org/dc/terms/title',
+ 'swrcTitle' => 'http://swrc.ontoware.org/ontology#title',
+ 'foafName' => 'http://xmlns.com/foaf/0.1/name',
+ 'doapName' => 'http://usefulinc.com/ns/doap#name',
+ 'siocName' => 'http://rdfs.org/sioc/ns#name',
+ 'tagName' => 'http://www.holygoat.co.uk/owl/redwood/0.1/tags/name',
+ 'lgeodName' => 'http://linkedgeodata.org/vocabulary#name',
+ 'geoName' => 'http://www.geonames.org/ontology#name',
+ 'goName' => 'http://www.geneontology.org/dtds/go.dtd#name',
+ 'rdfsLabel' => 'http://www.w3.org/2000/01/rdf-schema#label'
+ ),
+ 'searchMode' => 'language'
+ )
+ );
+
+ $titleHelper = new OntoWiki_Model_TitleHelper($graph, $this->_store, $config);
+ foreach ($properties as $expected => $property) {
+ $resource = $property;
+ $lang = null;
+ $expectedTitle = $expected;
+ if (strpos($expected, '@') !== false) {
+ $parts = explode('@', $expected);
+ $expectedTitle = $parts[0];
+ $lang = $parts[1];
+ }
+
+ $titleHelper->addResource($resource);
+ $title = $titleHelper->getTitle($property, $lang);
+ $this->assertEquals($expectedTitle, $title);
+ }
+ }
+}
2  libraries/Erfurt
@@ -1 +1 @@
-Subproject commit 8fc1e3ed37391197848f65ceb4b462e407108d1f
+Subproject commit e8d1b3e1deb5601d7db6c2e3dc427db622f49217
Please sign in to comment.
Something went wrong with that request. Please try again.