Author: david
Date: Fri Oct 28 16:01:52 2011
New Revision: 10229

Log:
Refactor SKOS import to support expanded RDF syntax - but breaks hierarchy (for 
now).  Refs issue #2005

Modified:
   trunk/plugins/sfSkosPlugin/lib/sfSkosPlugin.class.php
   
trunk/plugins/sfSkosPlugin/modules/sfSkosPlugin/actions/importAction.class.php
   trunk/plugins/sfSkosPlugin/modules/sfSkosPlugin/templates/importSuccess.php

Modified: trunk/plugins/sfSkosPlugin/lib/sfSkosPlugin.class.php
==============================================================================
--- trunk/plugins/sfSkosPlugin/lib/sfSkosPlugin.class.php       Fri Oct 28 
13:43:33 2011        (r10228)
+++ trunk/plugins/sfSkosPlugin/lib/sfSkosPlugin.class.php       Fri Oct 28 
16:01:52 2011        (r10229)
@@ -19,8 +19,9 @@
 
 class sfSkosPlugin
 {
-  protected
-    $terms = array();
+  public
+    $terms = array(),
+    $length = 0;
 
   public static function parse($doc, $options = array())
   {
@@ -59,36 +60,52 @@
       $skos->parent = $options['parent'];
     }
 
-    // Select all top-level concepts (no 'broader' child-nodes)
-    foreach ($skos->xpath->query('skos:Concept[not(skos:broader)]') as 
$concept)
+    // XPath selector for expanded RDF syntax
+    $rdfsel = 
"rdf:Description[rdf:type[@rdf:resource='http://www.w3.org/2004/02/skos/core#Concept']]";
+
+    // Get all concepts
+    $concepts = $skos->xpath->query("skos:Concept | $rdfsel");
+
+    // Create terms from concepts
+    foreach ($concepts as $concept)
     {
       if (!($concept instanceof domElement))
       {
         continue;
       }
 
-      $skos->addTerm($concept, null);
+      $skos->addTerm($concept);
     }
 
-    $skos->addTermAssociations();
+    // Built term associations (including hierarchy)
+    foreach ($concepts as $concept)
+    {
+      if (!($concept instanceof domElement))
+      {
+        continue;
+      }
 
-    return $skos->terms;
+      if (0 < $skos->xpath->query('./skos:related', $concept)->length)
+      {
+        $skos->addTermRelations($concept);
+      }
+
+      if (0 < $skos->xpath->query('./skos:broader | ./skos:narrower', 
$concept)->length)
+      {
+        $skos->buildHierarchy($concept);
+      }
+    }
+
+    return $skos;
   }
 
-  protected function addTerm($concept, $parent)
+  protected function addTerm($concept)
   {
     $term = new QubitTerm;
     $term->taxonomy = $this->taxonomy;
 
-    // Set parent
-    if (isset($parent))
-    {
-      $term->parentId = $parent->id;
-    }
-    else
-    {
-      $term->parent = $this->parent;
-    }
+    // Parent to current root (we'll update later)
+    $term->parent = $this->parent;
 
     // Preferred label
     $prefLabels = $this->xpath->query('./skos:prefLabel', $concept);
@@ -157,80 +174,80 @@
     // Map dc.coverage to term.code for place terms
     // Hacky Hackerton was here
     if (QubitTaxonomy::PLACE_ID == $this->taxonomy->getId()) {
-        foreach ($this->xpath->query('./dc:coverage', $concept) as $coverage)
-        {
-            $term->code = $coverage->nodeValue;
-        }
+      foreach ($this->xpath->query('./dc:coverage', $concept) as $coverage)
+      {
+          $term->code = $coverage->nodeValue;
+      }
     }
 
     // Save the term
     $term->save();
-//    $this->terms[] = $term;
 
-    // Find and add narrow terms
-    // TODO: Merge broader/narrower relations for this term, as defining
-    // inverse of relationship is not required by SKOS
-    // http://www.w3.org/TR/2009/NOTE-skos-primer-20090818/#sechierarchy
-    if ($uri instanceof DOMAttr)
-    {
-      foreach 
($this->xpath->query('./skos:Concept[skos:broader[@rdf:resource="'.$uri->nodeValue.'"]]')
 as $narrower)
-      {
-        if (!($narrower instanceof DOMElement))
-        {
-          continue;
-        }
-        $this->addTerm($narrower, $term);
-      }
-    }
-    unset($term);
+    $this->terms[] = $term;
+    $this->length++;
 
     return $this;
   }
 
-  protected function addTermAssociations()
+  protected function addTermRelations($concept)
   {
-    $count = 0;
-    $relations = array();
+    $subjectUri = 
$concept->getAttributeNodeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 
'about');
+    if (!($subjectUri instanceof DOMAttr) || null === $subject = 
self::getTermBySourceNote($subjectUri->nodeValue))
+    {
+      continue;
+    }
 
-    foreach ($this->xpath->query('skos:Concept[skos:related]') as $concept)
+    foreach ($this->xpath->query('./skos:related', $concept) as $related)
     {
-      $subjectUri = 
$concept->getAttributeNodeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 
'about');
-      if (!($subjectUri instanceof DOMAttr) || null === $subject = 
self::getTermBySourceNote($subjectUri->nodeValue))
+      $objectUri = 
$related->getAttributeNodeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 
'resource');
+      if (!($objectUri instanceof DomAttr) || null === $obj = 
self::getTermBySourceNote($objectUri->nodeValue))
       {
         continue;
       }
 
-      foreach ($this->xpath->query('./skos:related', $concept) as $related)
+      // Don't duplicate reciprocal relationship
+      foreach ($relations as $r)
       {
-        $objectUri = 
$related->getAttributeNodeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 
'resource');
-        if (!($objectUri instanceof DomAttr) || null === $obj = 
self::getTermBySourceNote($objectUri->nodeValue))
+        if ($r['subject'] == $objectUri->nodeValue && $r['object'] == 
$subjectUri->nodeValue)
         {
-          continue;
-        }
-
-        // Don't duplicate reciprocal relationship
-        foreach ($relations as $r)
-        {
-          if ($r['subject'] == $objectUri->nodeValue && $r['object'] == 
$subjectUri->nodeValue)
-          {
-            continue 2;
-          }
+          continue 2;
         }
+      }
 
-        $relation = new QubitRelation;
-        $relation->typeId = QubitTerm::TERM_RELATION_ASSOCIATIVE_ID;
-        $relation->subject = $subject;
-        $relation->object = $obj;
+      $relation = new QubitRelation;
+      $relation->typeId = QubitTerm::TERM_RELATION_ASSOCIATIVE_ID;
+      $relation->subject = $subject;
+      $relation->object = $obj;
 
-        $relation->save();
+      $relation->save();
 
-        $relations[] = array('subject' => $subjectUri->nodeValue, 'object' => 
$objectUri->nodeValue);
-      }
+      $relations[] = array('subject' => $subjectUri->nodeValue, 'object' => 
$objectUri->nodeValue);
     }
 
     return $this;
   }
 
+  protected function buildHierarchy($concept)
+  {
+    return;
+
+    // Find and add narrow terms
+    // TODO: Merge broader/narrower relations for this term, as defining
+    // inverse of relationship is not required by SKOS
+    // http://www.w3.org/TR/2009/NOTE-skos-primer-20090818/#sechierarchy
+    if ($uri instanceof DOMAttr)
+    {
+      foreach 
($this->xpath->query('./skos:Concept[skos:broader[@rdf:resource="'.$uri->nodeValue.'"]]')
 as $narrower)
+      {
+        if (!($narrower instanceof DOMElement))
+        {
+          continue;
+        }
+        $this->addTerm($narrower, $term);
+      }
+    }
+  }
+
   protected static function getTermBySourceNote($sourceNote)
   {
     $criteria = new Criteria;

Modified: 
trunk/plugins/sfSkosPlugin/modules/sfSkosPlugin/actions/importAction.class.php
==============================================================================
--- 
trunk/plugins/sfSkosPlugin/modules/sfSkosPlugin/actions/importAction.class.php  
    Fri Oct 28 13:43:33 2011        (r10228)
+++ 
trunk/plugins/sfSkosPlugin/modules/sfSkosPlugin/actions/importAction.class.php  
    Fri Oct 28 16:01:52 2011        (r10229)
@@ -29,7 +29,7 @@
 
     $this->taxonomy = null;
     $this->parent = QubitTerm::getById(QubitTerm::ROOT_ID);
-    
+
     if (isset($this->getRoute()->resource))
     {
       $resource = $this->getRoute()->resource;
@@ -71,10 +71,10 @@
           $doc->substituteEntities = true;
           $doc->load($file->getTempName());
 
-          $this->terms = sfSkosPlugin::parse($doc, array('taxonomy' => 
$this->taxonomy, 'parent' => $this->parent));
+          $this->skos = sfSkosPlugin::parse($doc, array('taxonomy' => 
$this->taxonomy, 'parent' => $this->parent));
 
           $this->topLevelTerms = array();
-          foreach ($this->terms as $term)
+          foreach ($this->skos->terms as $term)
           {
             if ($term->parent == $this->parent)
             {

Modified: 
trunk/plugins/sfSkosPlugin/modules/sfSkosPlugin/templates/importSuccess.php
==============================================================================
--- trunk/plugins/sfSkosPlugin/modules/sfSkosPlugin/templates/importSuccess.php 
Fri Oct 28 13:43:33 2011        (r10228)
+++ trunk/plugins/sfSkosPlugin/modules/sfSkosPlugin/templates/importSuccess.php 
Fri Oct 28 16:01:52 2011        (r10229)
@@ -12,7 +12,7 @@
   </ul>
 
   <div>
-    <?php echo __('A total of %1% terms were imported in %2%s', array('%1%' => 
count($terms), '%2%' => $timer->elapsed())) ?>
+    <?php echo __('A total of %1% terms were imported in %2%s', array('%1%' => 
$skos->length, '%2%' => $timer->elapsed())) ?>
   </div>
 
   <div class="actions section">
@@ -28,4 +28,4 @@
 
   </div>
 
-</form>
\ No newline at end of file
+</form>

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to