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-2016 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
14 {
15  # ---- PUBLIC INTERFACE --------------------------------------------------
16 
22  public function __construct($FileName)
23  {
24  # if provided filename is not found
25  if (!file_exists($FileName))
26  {
27  # look in configured search paths
28  foreach (self::$SearchPaths as $Path)
29  {
30  $TestPath = $Path."/".$FileName;
31  if (file_exists($TestPath))
32  {
33  $FileName = $TestPath;
34  break;
35  }
36  }
37  }
38 
39  # save file name
40  $this->FileName = $FileName;
41 
42  # attempt to load vocabulary from file
43  $this->Xml = simplexml_load_file($FileName);
44 
45  # set error code if load failed
46  $this->StatusString = ($this->Xml === FALSE) ? "XML Load Failed" : "OK";
47  $this->Xml = isset($this->Xml->vocabulary) ? $this->Xml->vocabulary : $this->Xml;
48  }
49 
53  public function Status()
54  {
55  return $this->StatusString;
56  }
57 
62  public function Hash()
63  {
64  return self::HashForFile($this->FileName);
65  }
66 
72  static public function HashForFile($FileName = NULL)
73  {
74  return strtoupper(md5($FileName));
75  }
76 
81  public function Name()
82  {
83  return $this->XmlVal("name");
84  }
85 
90  public function Description()
91  {
92  return $this->XmlVal("description");
93  }
94 
99  public function Url()
100  {
101  return $this->XmlVal("url");
102  }
103 
108  public function Version()
109  {
110  return $this->XmlVal("version");
111  }
112 
117  public function HasQualifier()
118  {
119  return (strlen($this->QualifierName())
120  && (strlen($this->QualifierNamespace())
121  || strlen($this->QualifierUrl()))) ? TRUE : FALSE;
122  }
123 
129  public function QualifierName()
130  {
131  return isset($this->Xml->qualifier->name)
132  ? (string)$this->Xml->qualifier->name : "";
133  }
134 
140  public function QualifierNamespace()
141  {
142  return isset($this->Xml->qualifier->namespace)
143  ? (string)$this->Xml->qualifier->namespace : "";
144  }
145 
151  public function QualifierUrl()
152  {
153  return isset($this->Xml->qualifier->url)
154  ? (string)$this->Xml->qualifier->url : "";
155  }
156 
161  public function OwnerName()
162  {
163  return isset($this->Xml->owner->name)
164  ? (string)$this->Xml->owner->name : "";
165  }
166 
171  public function OwnerUrl()
172  {
173  return isset($this->Xml->owner->url)
174  ? (string)$this->Xml->owner->url : "";
175  }
176 
181  public function TermArray()
182  {
183  $Terms = $this->ExtractTermSet($this->Xml);
184 
185  # return array of terms to caller
186  return $Terms;
187  }
188 
193  public function TermList()
194  {
195  $TermTree = $this->TermArray();
196  $Terms = $this->BuildTermList("", $TermTree);
197  return $Terms;
198  }
199 
205  public static function FileSearchPaths($NewValue = NULL)
206  {
207  if ($NewValue !== NULL)
208  {
209  self::$SearchPaths = $NewValue;
210  }
211  return self::$SearchPaths;
212  }
213 
214  # ---- PRIVATE INTERFACE -------------------------------------------------
215 
216  private $FileName;
217  private $StatusString;
218  private $Xml;
219  private static $SearchPaths = array();
220 
226  private function XmlVal($ValueName)
227  {
228  return isset($this->Xml->{$ValueName})
229  ? (string)$this->Xml->{$ValueName} : "";
230  }
231 
237  private function ExtractTermSet($Tree)
238  {
239  # make sure a valid SimpleXMLElement was given and return an empty
240  # array if not
241  if (!($Tree instanceof SimpleXMLElement))
242  {
243  return array();
244  }
245 
246  $Terms = array();
247  foreach ($Tree->term as $Term)
248  {
249  if (isset($Term->value))
250  {
251  $Terms[(string)$Term->value] = $this->ExtractTermSet($Term);
252  }
253  else
254  {
255  $Terms[(string)$Term] = array();
256  }
257  }
258  return $Terms;
259  }
260 
267  private function BuildTermList($Prefix, $TermTree)
268  {
269  $Terms = array();
270  foreach ($TermTree as $Term => $Children)
271  {
272  $Term = trim($Term);
273  $NewTerm = strlen($Prefix) ? $Prefix." -- ".$Term : $Term;
274  $Terms[] = $NewTerm;
275  $Terms = array_merge($Terms, $this->BuildTermList($NewTerm, $Children));
276  }
277  return $Terms;
278  }
279 }
static HashForFile($FileName=NULL)
Get hash string for specified vocabulary file name.
Definition: Vocabulary.php:72
Status()
Get string indicate status of last action.
Definition: Vocabulary.php:53
static FileSearchPaths($NewValue=NULL)
Get/set the list of paths where vocabulary files will be searched for.
Definition: Vocabulary.php:205
Description()
Get vocabulary description.
Definition: Vocabulary.php:90
HasQualifier()
Get whether vocabulary has associated qualifier.
Definition: Vocabulary.php:117
OwnerName()
Get name of owning (maintaining) organization.
Definition: Vocabulary.php:161
QualifierName()
Get qualifier name.
Definition: Vocabulary.php:129
QualifierNamespace()
Get qualifier namespace.
Definition: Vocabulary.php:140
TermArray()
Get vocabulary terms as multi-dimensional array.
Definition: Vocabulary.php:181
Controlled vocabulary.
Definition: Vocabulary.php:13
Hash()
Get hash string for vocabulary (generated from file name).
Definition: Vocabulary.php:62
Name()
Get vocabulary name.
Definition: Vocabulary.php:81
OwnerUrl()
Get primary URL for owning (maintaining) organization.
Definition: Vocabulary.php:171
TermList()
Get vocabulary terms as flat array with double-dash separators.
Definition: Vocabulary.php:193
Url()
Get URL attached to vocabulary.
Definition: Vocabulary.php:99
QualifierUrl()
Get qualifier URL.
Definition: Vocabulary.php:151
Version()
Get version number for vocabulary.
Definition: Vocabulary.php:108
__construct($FileName)
Object constructor.
Definition: Vocabulary.php:22