CWIS Developer Documentation
Vocabulary.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: Vocabulary.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2007-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
13 class Vocabulary {
14 
15  # ---- PUBLIC INTERFACE --------------------------------------------------
16 
22  function Vocabulary($FileName)
23  {
24  # save file name
25  $this->FileName = $FileName;
26 
27  # attempt to load vocabulary from file
28  $this->Xml = simplexml_load_file($FileName);
29 
30  # set error code if load failed
31  $this->StatusString = ($this->Xml === FALSE) ? "XML Load Failed" : "OK";
32  $this->Xml = isset($this->Xml->vocabulary) ? $this->Xml->vocabulary : $this->Xml;
33  }
34 
38  function Status() { return $this->StatusString; }
39 
44  function Hash()
45  {
46  return self::HashForFile($this->FileName);
47  }
48 
54  static function HashForFile($FileName = NULL)
55  {
56  return strtoupper(md5($FileName));
57  }
58 
63  function Name() { return $this->XmlVal("name"); }
64 
69  function Description() { return $this->XmlVal("description"); }
70 
75  function Url() { return $this->XmlVal("url"); }
76 
81  function Version() { return $this->XmlVal("version"); }
82 
87  function HasQualifier()
88  {
89  return (strlen($this->QualifierName())
90  && (strlen($this->QualifierNamespace())
91  || strlen($this->QualifierUrl()))) ? TRUE : FALSE;
92  }
93 
99  function QualifierName()
100  {
101  return isset($this->Xml->qualifier->name)
102  ? (string)$this->Xml->qualifier->name : "";
103  }
104 
111  {
112  return isset($this->Xml->qualifier->namespace)
113  ? (string)$this->Xml->qualifier->namespace : "";
114  }
115 
121  function QualifierUrl()
122  {
123  return isset($this->Xml->qualifier->url)
124  ? (string)$this->Xml->qualifier->url : "";
125  }
126 
131  function OwnerName()
132  {
133  return isset($this->Xml->owner->name)
134  ? (string)$this->Xml->owner->name : "";
135  }
136 
141  function OwnerUrl()
142  {
143  return isset($this->Xml->owner->url)
144  ? (string)$this->Xml->owner->url : "";
145  }
146 
151  function TermArray()
152  {
153  $Terms = $this->ExtractTermSet($this->Xml);
154 
155  # return array of terms to caller
156  return $Terms;
157  }
158 
163  function TermList()
164  {
165  $TermTree = $this->TermArray();
166  $Terms = $this->BuildTermList("", $TermTree);
167  return $Terms;
168  }
169 
170  # ---- PRIVATE INTERFACE -------------------------------------------------
171 
172  private $FileName;
173  private $StatusString;
174  private $Xml;
175 
180  private function XmlVal($ValueName)
181  {
182  return isset($this->Xml->{$ValueName})
183  ? (string)$this->Xml->{$ValueName} : "";
184  }
185 
191  private function ExtractTermSet($Tree)
192  {
193  # make sure a valid SimpleXMLElement was given and return an empty
194  # array if not
195  if (!($Tree instanceof SimpleXMLElement))
196  {
197  return array();
198  }
199 
200  $Terms = array();
201  foreach ($Tree->term as $Term)
202  {
203  if (isset($Term->value))
204  {
205  $Terms[(string)$Term->value] = $this->ExtractTermSet($Term);
206  }
207  else
208  {
209  $Terms[(string)$Term] = array();
210  }
211  }
212  return $Terms;
213  }
214 
221  private function BuildTermList($Prefix, $TermTree)
222  {
223  $Terms = array();
224  foreach ($TermTree as $Term => $Children)
225  {
226  $Term = trim($Term);
227  $NewTerm = strlen($Prefix) ? $Prefix." -- ".$Term : $Term;
228  $Terms[] = $NewTerm;
229  $Terms = array_merge($Terms, $this->BuildTermList($NewTerm, $Children));
230  }
231  return $Terms;
232  }
233 }
static HashForFile($FileName=NULL)
Get hash string for specified vocabulary file name.
Definition: Vocabulary.php:54
Status()
Get string indicate status of last action.
Definition: Vocabulary.php:38
Description()
Get vocabulary description.
Definition: Vocabulary.php:69
HasQualifier()
Get whether vocabulary has associated qualifier.
Definition: Vocabulary.php:87
OwnerName()
Get name of owning (maintaining) organization.
Definition: Vocabulary.php:131
QualifierName()
Get qualifier name.
Definition: Vocabulary.php:99
QualifierNamespace()
Get qualifier namespace.
Definition: Vocabulary.php:110
TermArray()
Get vocabulary terms as multi-dimensional array.
Definition: Vocabulary.php:151
Controlled vocabulary.
Definition: Vocabulary.php:13
Hash()
Get hash string for vocabulary (generated from file name).
Definition: Vocabulary.php:44
Name()
Get vocabulary name.
Definition: Vocabulary.php:63
OwnerUrl()
Get primary URL for owning (maintaining) organization.
Definition: Vocabulary.php:141
TermList()
Get vocabulary terms as flat array with double-dash separators.
Definition: Vocabulary.php:163
Url()
Get URL attached to vocabulary.
Definition: Vocabulary.php:75
QualifierUrl()
Get qualifier URL.
Definition: Vocabulary.php:121
Version()
Get version number for vocabulary.
Definition: Vocabulary.php:81
Vocabulary($FileName)
Object constructor.
Definition: Vocabulary.php:22