Open Semantic Framework PHP API  3.0
 All Data Structures Namespaces Files Functions Variables Pages
Resultset.php
Go to the documentation of this file.
1 <?php
2 
5 
13 
14 use \SimpleXMLElement;
15 
16 use \StructuredDynamics\osf\framework\Namespaces;
17 use \StructuredDynamics\osf\ws\converter\irjson\ConverterIrJSON;
18 use \StructuredDynamics\osf\ws\converter\irjson\Dataset;
19 use \StructuredDynamics\osf\ws\converter\irjson\InstanceRecord;
20 use \StructuredDynamics\osf\ws\converter\irjson\irJSONParser;
21 use \StructuredDynamics\osf\ws\converter\irjson\LinkageSchema;
22 use \StructuredDynamics\osf\ws\converter\irjson\StructureSchema;
23 use \StructuredDynamics\osf\ws\converter\common\CommonParser;
24 use \StructuredDynamics\osf\ws\converter\common\ConverterCommON;
25 
26 
33 class Resultset
34 {
94  private $resultset = array();
95 
97  private $prefixes = array(
98  "http://www.w3.org/2002/07/owl#" => "owl",
99  "http://www.w3.org/1999/02/22-rdf-syntax-ns#" => "rdf",
100  "http://www.w3.org/2000/01/rdf-schema#" => "rdfs",
101  "http://purl.org/ontology/iron#" => "iron",
102  "http://www.w3.org/2001/XMLSchema#" => "xsd",
103  "http://purl.org/ontology/wsf#" => "wsf"
104  );
105 
107  private $newPrefixesCounter = 0;
108 
110  private $wsf_base_path = "/usr/share/osf";
111 
117  private $namespaces;
118 
126  function __construct($wsf_base_path = "/usr/share/osf/")
127  {
128  $this->namespaces = new Namespaces();
129 
130  $this->wsf_base_path = rtrim($wsf_base_path, "/")."/";
131  }
132 
133  function __destruct() { }
134 
143  public function addSubject($subject, $dataset = "")
144  {
145  if(get_class($subject) == 'StructuredDynamics\osf\framework\Subject')
146  {
147  if($dataset == "")
148  {
149  $dataset = "unspecified";
150  }
151 
152  $uri = $subject->getUri();
153 
154  if(!isset($this->resultset[$dataset][$uri]))
155  {
156  $this->resultset[$dataset][$uri] = $subject->getSubject();
157 
158  return(TRUE);
159  }
160  else
161  {
162  return(FALSE);
163  }
164  }
165  else
166  {
167  return(FALSE);
168  }
169  }
170 
178  public function getSubjects()
179  {
180  $subjects = array();
181 
182  foreach($this->resultset as $dataset => $subjectsArr)
183  {
184  foreach($subjectsArr as $uri => $s)
185  {
186  $subject = new Subject($uri);
187 
188  $subject->setSubject($s);
189 
190  array_push($subjects, $subject);
191  }
192  }
193 
194  return($subjects);
195  }
196 
204  public function getSubjectByUri($uri)
205  {
206  foreach($this->resultset as $dataset => $subjects)
207  {
208  foreach($subjects as $suri => $s)
209  {
210  if($suri == $uri)
211  {
212  $subject = new Subject($uri);
213  $subject->setSubject($s);
214 
215  return($subject);
216  }
217  }
218  }
219 
220  return(FALSE);
221  }
222 
230  public function getResultset()
231  {
232  return($this->resultset);
233  }
234 
287  public function setResultset($resultset)
288  {
289  $this->resultset = $resultset;
290  }
291 
299  public function importStructXMLResultset($structXMLResultset)
300  {
301  $xml = new SimpleXMLElement($structXMLResultset);
302 
303  /*
304  $resultset = Array("dataset-uri" =>
305  Array("record-uri" =>
306  Array(
307  "type" => Array(URIs...),
308  "prefLabel" => "preferred label",
309  "altLabel" => Array(alternative label literals...),
310  "prefURL" => "http://preferred-url.com",
311  "description" => "some description of the record",
312 
313  "other-data-attribute-uri" => Array(
314  Array(
315  "value" => "some value",
316  "lang" => "language string of the value",
317  "type" => "type of the value"
318  "type" => "optional type of the referenced URI",
319  "reify" => Array(
320  "reification-attribute-uri" => Array("value of the reification statement"),
321  "more-reification-attribute-uri" => ...
322  )
323  ),
324  Array(
325  ...
326  )
327  ),
328  "more-data-attribute-uri": ...,
329 
330  "other-object-attribute-uri" => Array(
331  Array(
332  "uri" => "some uri",
333  "type" => "optional type of the referenced URI",
334  "reify" => Array(
335  "reification-attribute-uri" => Array("value of the reification statement"),
336  "more-reification-attribute-uri" => ...
337  )
338  ),
339  Array(
340  ...
341  )
342  )
343  "more-object-attribute-uri": ...
344  ),
345 
346  "more-record-uri": ...
347  )
348  )
349  */
350 
351  $resultset = array();
352 
353  foreach($xml->prefix as $prefix)
354  {
355  if(!isset($this->prefixes[(string) $prefix["uri"]]))
356  {
357  $this->prefixes[(string) $prefix["uri"]] = (string) $prefix["entity"];
358  }
359  }
360 
361  foreach($xml->subject as $s)
362  {
363  $type = $this->unprefixize((string) $s["type"]);
364  $uri = $this->unprefixize((string) $s["uri"]);
365 
366  $subject = array(
367  "type" => array($type)
368  );
369 
370  foreach($s->predicate as $predicate)
371  {
372  $predicateType = $this->unprefixize((string) $predicate["type"]);
373 
374  foreach($predicate->object as $object)
375  {
376  //$objectType = $this->unprefixize((string) $object["type"]);
377 
378  // check if object property
379  if(isset($object["uri"]))
380  {
381  if($predicateType == Namespaces::$rdf."type")
382  {
383  array_push($subject["type"], (string) $object["uri"]);
384  continue;
385  }
386 
387  if(!isset($subject[$predicateType]))
388  {
389  $subject[$predicateType] = array();
390  }
391 
392  $value = array();
393 
394  if(isset($object["type"]))
395  {
396  $value["type"] = $this->unprefixize((string) $object["type"]);
397  }
398 
399  if(isset($object["uri"]))
400  {
401  $value["uri"] = $this->unprefixize((string) $object["uri"]);
402  }
403 
404  // Add possible reification statement.
405  if(isset($object->reify))
406  {
407  $reifyType = $this->unprefixize((string) $object->reify["type"]);
408  $reifyValue = $this->unprefixize((string) $object->reify["value"]);
409 
410  $value["reify"][$reifyType] = array($reifyValue);
411  }
412 
413  array_push($subject[$predicateType], $value);
414  }
415  else
416  {
417  if(!isset($subject[$predicateType]))
418  {
419  $subject[$predicateType] = array();
420  }
421 
422  $value = array();
423 
424  if(isset($object["type"]))
425  {
426  $value["type"] = $this->unprefixize((string) $object["type"]);
427  }
428 
429  if(isset($object["lang"]))
430  {
431  $value["lang"] = (string) $object["lang"];
432  }
433 
434  $value["value"] = (string) $object;
435 
436  // Add possible reification statement.
437 
438  if(isset($predicate->reify))
439  {
440  $reifyType = $this->unprefixize((string) $predicate->reify["type"]);
441  $reifyValue = $this->unprefixize((string) $predicate->reify["value"]);
442 
443  $value["reify"][$reifyType] = array($reifyValue);
444  }
445 
446  array_push($subject[$predicateType], $value);
447  }
448  }
449  }
450 
451  // Try to find a prefLabel
452  $prefLabelProperty = "";
453 
454  foreach(Namespaces::getLabelProperties() as $labelProperty)
455  {
456  if(isset($subject[$labelProperty]))
457  {
458  $subject["prefLabel"] = $subject[$labelProperty][0]["value"];
459 
460  $prefLabelProperty = $labelProperty;
461 
462  break;
463  }
464  }
465 
466  // Try to find alternative labels
467  foreach(Namespaces::getLabelProperties() as $labelProperty)
468  {
469  if($labelProperty != $prefLabelProperty && isset($subject[$labelProperty]))
470  {
471  if(!isset($subject["altLabel"]))
472  {
473  $subject["altLabel"] = array();
474  }
475 
476  array_push($subject["altLabel"], $subject[$labelProperty][0]["value"]);
477  }
478  }
479 
480  // Try to find description
481  foreach(Namespaces::getDescriptionProperties() as $descriptionProperty)
482  {
483  if(isset($subject[$descriptionProperty]) && !empty($subject[$descriptionProperty]) && isset($subject[$descriptionProperty][0]["value"]))
484  {
485  $subject["description"] = $subject[$descriptionProperty][0]["value"];
486 
487  break;
488  }
489  }
490 
491  // Try to find preferred URLs
492  if(isset($subject[Namespaces::$iron."prefURL"]))
493  {
494  $subject["prefURL"] = $subject[Namespaces::$iron."prefURL"][0]["value"];
495  }
496 
497  // Try to get a reference to the dataset where the record could come from
498  $dataset = "unspecified";
499 
500  if(isset($subject[Namespaces::$dcterms."isPartOf"]) &&
501  is_array($subject[Namespaces::$dcterms."isPartOf"]) &&
502  isset($subject[Namespaces::$dcterms."isPartOf"][0]["uri"]))
503  {
504 
505  $dataset = $subject[Namespaces::$dcterms."isPartOf"][0]["uri"];
506  }
507 
508  if(!isset($resultset[$dataset]))
509  {
510  $resultset[$dataset] = array();
511  }
512 
513  $resultset[$dataset][$uri] = $subject;
514  }
515 
516  $this->resultset = $resultset;
517  }
518 
526  public function getResultsetXML()
527  {
528  $xml = '';
529 
530  foreach($this->resultset as $datasetURI => $records)
531  {
532  foreach($records as $recordURI => $record)
533  {
534  // Determine the first (main) type of the record
535  $firstType = "http://www.w3.org/2002/07/owl#Thing";
536 
537  if(isset($record["type"][0]))
538  {
539  $firstType = $record["type"][0];
540  }
541 
542  $xml .= ' <subject type="'.$this->xmlEncode($this->prefixize($firstType)).'" uri="'.$this->xmlEncode($recordURI).'">'."\n";
543 
544  foreach($record as $attributeURI => $attributeValues)
545  {
546  switch($attributeURI)
547  {
548  case "type":
549  foreach($attributeValues as $key => $type)
550  {
551  if($key > 0 && $type != "")
552  {
553  $xml .= ' <predicate type="rdf:type">'."\n";
554  $xml .= ' <object uri="'.$this->xmlEncode($type).'" />'."\n";
555  $xml .= ' </predicate>'."\n";
556  }
557  }
558  break;
559  case "prefLabel":
560  if($attributeValues != "")
561  {
562  $xml .= ' <predicate type="iron:prefLabel">'."\n";
563  $xml .= ' <object type="rdfs:Literal">'.$this->xmlEncode($attributeValues).'</object>'."\n";
564  $xml .= ' </predicate>'."\n";
565  }
566  break;
567  case "altLabel":
568  foreach($attributeValues as $altLabel)
569  {
570  if($altLabel != "")
571  {
572  $xml .= ' <predicate type="iron:altLabel">'."\n";
573  $xml .= ' <object type="rdfs:Literal">'.$this->xmlEncode($altLabel).'</object>'."\n";
574  $xml .= ' </predicate>'."\n";
575  }
576  }
577  break;
578  case "description":
579  if($attributeValues != "")
580  {
581  $xml .= ' <predicate type="iron:description">'."\n";
582  $xml .= ' <object type="rdfs:Literal">'.$this->xmlEncode($attributeValues).'</object>'."\n";
583  $xml .= ' </predicate>'."\n";
584  }
585  break;
586  case "prefURL":
587  if($attributeValues != "")
588  {
589  $xml .= ' <predicate type="iron:prefURL">'."\n";
590  $xml .= ' <object type="rdfs:Literal">'.$this->xmlEncode($attributeValues).'</object>'."\n";
591  $xml .= ' </predicate>'."\n";
592  }
593  break;
594 
595  // Any other attribute describing this record
596  default:
597  foreach($attributeValues as $value)
598  {
599  $xml .= ' <predicate type="'.$this->xmlEncode($this->prefixize($attributeURI)).'">'."\n";
600 
601  if(isset($value["value"]))
602  {
603  // It is a literal value, or a literal value of some type (int, bool, etc)
604  if($value["value"] !== "")
605  {
606  $xml .= ' <object type="'.$this->xmlEncode($this->prefixize($value["type"])).'"'.(isset($value["lang"]) && $value["lang"] != "" ? ' lang="'.$this->xmlEncode($value["lang"]).'"' : "").'>'.$this->xmlEncode($value["value"]).'</object>'."\n";
607 
608  if(isset($value["reify"]))
609  {
610  foreach($value["reify"] as $reifyAttributeUri => $reifiedValues)
611  {
612  foreach($reifiedValues as $reifiedValue)
613  {
614  if($reifiedValue != "")
615  {
616  $xml .= ' <reify type="'.$this->xmlEncode($this->prefixize($reifyAttributeUri)).'" value="'.$this->xmlEncode($reifiedValue).'" />'."\n";
617  }
618  }
619  }
620  }
621  }
622  }
623  elseif(isset($value["uri"]))
624  {
625  // It is a resource URI
626  if(!isset($value["reify"]))
627  {
628  if($value["uri"] != "")
629  {
630  $xml .= ' <object uri="'.$this->xmlEncode($value["uri"]).'" '.(isset($value["type"]) && $value["type"] != "" ? 'type="'.$value["type"].'"' : '').' />'."\n";
631  }
632  }
633  else
634  {
635  if($value["uri"] != "")
636  {
637  $xml .= ' <object uri="'.$this->xmlEncode($value["uri"]).'" '.(isset($value["type"]) && $value["type"] != "" ? 'type="'.$value["type"].'"' : '').'>'."\n";
638 
639  foreach($value["reify"] as $reifyAttributeUri => $reifiedValues)
640  {
641  foreach($reifiedValues as $reifiedValue)
642  {
643  if($reifiedValue != "")
644  {
645  $xml .= ' <reify type="'.$this->xmlEncode($this->prefixize($reifyAttributeUri)).'" value="'.$this->xmlEncode($reifiedValue).'" />'."\n";
646  }
647  }
648  }
649 
650  $xml .= ' </object>'."\n";
651  }
652  }
653  }
654 
655 
656  $xml .= ' </predicate>'."\n";
657  }
658  break;
659  }
660  }
661 
662  $xml .= " </subject>\n";
663  }
664  }
665 
666  $xmlHeader = '<?xml version="1.0" encoding="utf-8"?>'."\n";
667  $xmlHeader .= " <resultset>\n";
668 
669  foreach($this->prefixes as $uri => $entity)
670  {
671  $xmlHeader .= ' <prefix entity="'.$entity.'" uri="'.$uri.'" />'."\n";
672  }
673 
674  $xml = $xmlHeader.$xml;
675 
676  $xml .= '</resultset>';
677 
678  return($xml);
679  }
680 
688  public function getResultsetJSON()
689  {
690  $json = "";
691 
692  foreach($this->resultset as $datasetURI => $records)
693  {
694  foreach($records as $recordURI => $record)
695  {
696  // Determine the first (main) type of the record
697  $firstType = "http://www.w3.org/2002/07/owl#Thing";
698 
699  if(isset($record["type"][0]))
700  {
701  $firstType = $record["type"][0];
702  }
703 
704  $json .= ' {'."\n";
705  $json .= ' "uri": "'.$this->jsonEncode($recordURI).'", '."\n";
706  $json .= ' "type": "'.$this->jsonEncode($this->prefixize($firstType)).'", '."\n";
707 
708  $json .= ' "predicate": [ '."\n";
709 
710  ksort($record);
711 
712  foreach($record as $attributeURI => $attributeValues)
713  {
714  switch($attributeURI)
715  {
716  case "type":
717  foreach($attributeValues as $key => $type)
718  {
719  if($key > 0 && $type != "")
720  {
721  $json .= ' { '."\n";
722  $json .= ' "rdf:type": "'.$this->jsonEncode($type).'" '."\n";
723  $json .= ' }, '."\n";
724  }
725  }
726  break;
727  case "prefLabel":
728  if($attributeValues != "")
729  {
730  $json .= ' { '."\n";
731  $json .= ' "iron:prefLabel": "'.$this->jsonEncode($attributeValues).'" '."\n";
732  $json .= ' }, '."\n";
733  }
734  break;
735  case "altLabel":
736  foreach($attributeValues as $altLabel)
737  {
738  if($altLabel != "")
739  {
740  $json .= ' { '."\n";
741  $json .= ' "iron:altLabel": "'.$this->jsonEncode($altLabel).'" '."\n";
742  $json .= ' }, '."\n";
743  }
744  }
745  break;
746  case "description":
747  if($attributeValues != "")
748  {
749  $json .= ' { '."\n";
750  $json .= ' "iron:description": "'.$this->jsonEncode($attributeValues).'" '."\n";
751  $json .= ' }, '."\n";
752  }
753  break;
754  case "prefURL":
755  if($attributeValues != "")
756  {
757  $json .= ' { '."\n";
758  $json .= ' "iron:prefURL": "'.$this->jsonEncode($attributeValues).'" '."\n";
759  $json .= ' }, '."\n";
760  }
761  break;
762 
763  // Any other attribute describing this record
764  default:
765  foreach($attributeValues as $value)
766  {
767  if(isset($value["value"]))
768  {
769  // It is a literal value, or a literal value of some type (int, bool, etc)
770  if($value["value"] !== "")
771  {
772  // We simply return the literal value
773  if((isset($value["lang"]) && $value["lang"] != "") ||
774  (isset($value["type"]) && ($value["type"] != "" && $value["type"] != "rdfs:Literal" && $value["type"] != Namespaces::$rdfs."Literal")) ||
775  (isset($value["reify"]) && !empty($value['reify'])))
776  {
777  /*
778  If we have a type, or a lang defined for this literal value, we return and object of the kind:
779  {
780  "value": "the literal value",
781  "type": "xsd:string",
782  "lang": "en"
783  }
784  */
785 
786  $json .= ' { '."\n";
787  $json .= ' "'.$this->jsonEncode($this->prefixize($attributeURI)).'": {'."\n";
788  $json .= ' "value": "'.$this->jsonEncode($value["value"]).'", '."\n";
789 
790  if(isset($value["type"]) && ($value["type"] != "" && $value["type"] != "rdfs:Literal" && $value["type"] != Namespaces::$rdfs."Literal"))
791  {
792  $json .= ' "type": "'.$this->jsonEncode($this->prefixize($value["type"])).'", '."\n";
793  }
794 
795  if(isset($value["lang"]) && $value["lang"] != "")
796  {
797  $json .= ' "lang": "'.$this->jsonEncode($value["lang"]).'", '."\n";
798  }
799 
800  $json = substr($json, 0, strlen($json) - 3)." \n";
801 
802  if(isset($value["reify"]))
803  {
804  $jsonReifyData = '';
805 
806  foreach($value["reify"] as $reifyAttributeUri => $reifiedValues)
807  {
808  foreach($reifiedValues as $reifiedValue)
809  {
810  if($reifiedValue !== "")
811  {
812  $jsonReifyData .= ' { '."\n";
813  $jsonReifyData .= ' "type": "'.$this->jsonEncode($this->prefixize($reifyAttributeUri)).'", '."\n";
814  $jsonReifyData .= ' "value": "'.$this->jsonEncode($this->prefixize($reifiedValue)).'" '."\n";
815  $jsonReifyData .= ' }, '."\n";
816  }
817  }
818  }
819 
820  if($jsonReifyData != "")
821  {
822  $json .= ", \n";
823  $json .= ' "reify": [ '."\n";
824 
825  $jsonReifyData = substr($jsonReifyData, 0, strlen($jsonReifyData) - 3);
826 
827  $json .= $jsonReifyData;
828 
829  $json .= "\n";
830 
831  $json .= ' ] '."\n";
832  }
833  }
834  else
835  {
836  $json .= " \n";
837  }
838 
839  $json .= " }\n";
840  $json .= ' }, '."\n";
841  }
842  else
843  {
844  $json .= ' { '."\n";
845  $json .= ' "'.$this->jsonEncode($this->prefixize($attributeURI)).'": "'.$this->jsonEncode($value["value"]).'" '."\n";
846  $json .= ' }, '."\n";
847  }
848  }
849  }
850  elseif(isset($value["uri"]))
851  {
852  // It is a resource URI
853  if($value["uri"] != "")
854  {
855  $json .= ' { '."\n";
856  $json .= ' "'.$this->jsonEncode($this->prefixize($attributeURI)).'": { '."\n";
857  $json .= ' "uri": "'.$this->jsonEncode($value["uri"]).'"';
858 
859  if(isset($value["type"]) && $value["type"] != "")
860  {
861  $json .= ", \n";
862  $json .= ' "type": "'.$this->jsonEncode($this->prefixize($value["type"])).'"';
863  }
864 
865  if(isset($value["reify"]))
866  {
867  $jsonReifyData = '';
868 
869  foreach($value["reify"] as $reifyAttributeUri => $reifiedValues)
870  {
871  foreach($reifiedValues as $reifiedValue)
872  {
873  if($reifiedValue !== "")
874  {
875  $jsonReifyData .= ' { '."\n";
876  $jsonReifyData .= ' "type": "'.$this->jsonEncode($this->prefixize($reifyAttributeUri)).'", '."\n";
877  $jsonReifyData .= ' "value": "'.$this->jsonEncode($this->prefixize($reifiedValue)).'" '."\n";
878  $jsonReifyData .= ' }, '."\n";
879  }
880  }
881  }
882 
883  if($jsonReifyData != "")
884  {
885  $json .= ", \n";
886 
887  $json .= ' "reify": [ '."\n";
888 
889 
890  $jsonReifyData = substr($jsonReifyData, 0, strlen($jsonReifyData) - 3);
891 
892  $json .= $jsonReifyData;
893 
894  $json .= "\n";
895 
896  $json .= ' ] '."\n";
897  }
898  }
899  else
900  {
901  $json .= " \n";
902  }
903 
904  $json .= ' } '."\n";
905  $json .= ' }, '."\n";
906  }
907  }
908  }
909  break;
910  }
911  }
912 
913  $json = substr($json, 0, strlen($json) - 3);
914 
915  $json .= "\n";
916 
917  $json .= ' ] '."\n";
918 
919  $json .= " }, \n";
920  }
921  }
922 
923  $json = substr($json, 0, strlen($json) - 3);
924 
925  $json .= "\n";
926 
927  $jsonHeader = "{\n";
928 
929  if(count($this->prefixes) > 0)
930  {
931  $jsonHeader .= ' "prefixes": {'."\n";
932 
933 
934  foreach($this->prefixes as $uri => $entity)
935  {
936  $jsonHeader .= ' "'.$entity.'": "'.$uri.'", '."\n";
937  }
938 
939  $jsonHeader = substr($jsonHeader, 0, strlen($jsonHeader) - 3);
940 
941  $jsonHeader .= "\n";
942 
943  $jsonHeader .= ' }, '."\n";
944  }
945 
946  $jsonHeader .= ' "resultset": {'."\n";
947  $jsonHeader .= ' "subject": ['."\n";
948 
949  $xmlHeader = '<?xml version="1.0" encoding="utf-8"?>'."\n";
950  $xmlHeader .= " <resultset>\n";
951 
952 
953 
954  $json = $jsonHeader.$json;
955 
956  $json .= ' ]'."\n";
957  $json .= ' }'."\n";
958  $json .= '}';
959 
960  return($json);
961  }
962 
970  public function getResultsetIronJSON()
971  {
972  $ws_irv = new ConverterIrJSON($this->getResultsetXML(), "text/xml", "true");
973 
974  $ws_irv->pipeline_conneg("application/iron+json", "", "text/xml", "");
975 
976  $ws_irv->process();
977 
978  if($ws_irv->pipeline_getResponseHeaderStatus() != 200)
979  {
980  /*
981  $this->conneg->setStatus($ws_irv->pipeline_getResponseHeaderStatus());
982  $this->conneg->setStatusMsg($ws_irv->pipeline_getResponseHeaderStatusMsg());
983  $this->conneg->setStatusMsgExt($ws_irv->pipeline_getResponseHeaderStatusMsgExt());
984  $this->conneg->setError($ws_irv->pipeline_getError()->id, $ws_irv->pipeline_getError()->webservice,
985  $ws_irv->pipeline_getError()->name, $ws_irv->pipeline_getError()->description,
986  $ws_irv->pipeline_getError()->debugInfo, $ws_irv->pipeline_getError()->level);
987  */
988 
989  return;
990  }
991 
992  return ($ws_irv->pipeline_serialize());
993  }
994 
1002  public function getResultsetIronCOMMON()
1003  {
1004  $ws_irc = new ConverterCommON($this->getResultsetXML(), "text/xml", "true");
1005 
1006  $ws_irc->pipeline_conneg("application/iron+csv", "", "text/xml", "");
1007 
1008  $ws_irc->process();
1009 
1010  if($ws_irc->pipeline_getResponseHeaderStatus() != 200)
1011  {
1012  /*
1013  $this->conneg->setStatus($ws_irv->pipeline_getResponseHeaderStatus());
1014  $this->conneg->setStatusMsg($ws_irv->pipeline_getResponseHeaderStatusMsg());
1015  $this->conneg->setStatusMsgExt($ws_irv->pipeline_getResponseHeaderStatusMsgExt());
1016  $this->conneg->setError($ws_irv->pipeline_getError()->id, $ws_irv->pipeline_getError()->webservice,
1017  $ws_irv->pipeline_getError()->name, $ws_irv->pipeline_getError()->description,
1018  $ws_irv->pipeline_getError()->debugInfo, $ws_irv->pipeline_getError()->level);
1019  */
1020 
1021  return;
1022  }
1023 
1024  return ($ws_irc->pipeline_serialize());
1025  }
1026 
1036  public function getResultsetRDFXML()
1037  {
1038  $xml = '';
1039 
1040  foreach($this->resultset as $datasetURI => $records)
1041  {
1042  foreach($records as $recordURI => $record)
1043  {
1044  $xmlRei = '';
1045 
1046  // Determine the first (main) type of the record
1047  $firstType = "http://www.w3.org/2002/07/owl#Thing";
1048 
1049  if(isset($record["type"][0]))
1050  {
1051  $firstType = $record["type"][0];
1052  }
1053 
1054  $xml .= ' <'.$this->xmlEncode($this->prefixize($firstType)).' rdf:about="'.$this->xmlEncode($recordURI).'">'."\n";
1055 
1056  foreach($record as $attributeURI => $attributeValues)
1057  {
1058  switch($attributeURI)
1059  {
1060  case "type":
1061  foreach($attributeValues as $key => $type)
1062  {
1063  if($key > 0 && $type != "")
1064  {
1065  $xml .= ' <rdf:type rdf:resource="'.$this->xmlEncode($type).'" />'."\n";
1066  }
1067  }
1068  break;
1069 
1070  case "prefLabel":
1071  if($attributeValues != "")
1072  {
1073  if(!isset($record[Namespaces::$iron.'prefLabel']) || !$this->in_array_r($attributeValues, $record[Namespaces::$iron.'prefLabel']))
1074  {
1075  $xml .= ' <iron:prefLabel>'.$this->xmlEncode($attributeValues).'</iron:prefLabel>'."\n";
1076  }
1077  }
1078  break;
1079  case "altLabel":
1080  foreach($attributeValues as $altLabel)
1081  {
1082  if($altLabel != "" && (!isset($record[Namespaces::$iron.'altLabel']) || !$this->in_array_r($altLabel, $record[Namespaces::$iron.'altLabel'])))
1083  {
1084  $xml .= ' <iron:altLabel>'.$this->xmlEncode($altLabel).'</iron:altLabel>'."\n";
1085  }
1086  }
1087  break;
1088  case "description":
1089  if($attributeValues != "")
1090  {
1091  if(!isset($record[Namespaces::$iron.'description']) || !$this->in_array_r($attributeValues, $record[Namespaces::$iron.'description']))
1092  {
1093  $xml .= ' <iron:description>'.$this->xmlEncode($attributeValues).'</iron:description>'."\n";
1094  }
1095  }
1096  break;
1097  case "prefURL":
1098  if($attributeValues != "")
1099  {
1100  if(!isset($record[Namespaces::$iron.'prefURL']) || !$this->in_array_r($attributeValues, $record[Namespaces::$iron.'prefURL']))
1101  {
1102  $xml .= ' <iron:prefURL>'.$this->xmlEncode($attributeValues).'</iron:prefURL>'."\n";
1103  }
1104  }
1105  break;
1106 
1107  // Any other attribute describing this record
1108  default:
1109  foreach($attributeValues as $value)
1110  {
1111  if(isset($value["value"]))
1112  {
1113  // It is a literal value, or a literal value of some type (int, bool, etc)
1114  if($value["value"] !== "")
1115  {
1116  $xml .= ' <'.$this->xmlEncode($this->prefixize($attributeURI)).''.(isset($value["lang"]) && $value["lang"] != "" ? ' xml:lang="'.$this->xmlEncode($value["lang"]).'"' : "").''.(isset($value["type"]) && $value["type"] != "" && $value["type"] != "rdfs:Literal" && $value["type"] != Namespaces::$rdfs."Literal" ? ' rdf:datatype="'.$this->xmlEncode($this->unprefixize($value["type"])).'"' : "").'>'.$this->xmlEncode($value["value"]).'</'.$this->xmlEncode($this->prefixize($attributeURI)).'>'."\n";
1117 
1118  if(isset($value["reify"]))
1119  {
1120  foreach($value["reify"] as $reifyAttributeUri => $reifiedValues)
1121  {
1122  foreach($reifiedValues as $reifiedValue)
1123  {
1124  if($reifiedValue !== "")
1125  {
1126  $xmlRei .= " <rdf:Statement rdf:about=\"". $this->xmlEncode("bnode:".md5($recordURI . $attributeURI . $value["value"])) . "\">\n";
1127  $xmlRei .= " <rdf:subject rdf:resource=\"" . $recordURI . "\" />\n";
1128  $xmlRei .= " <rdf:predicate rdf:resource=\"" . $this->xmlEncode($attributeURI) . "\" />\n";
1129  $xmlRei .= " <rdf:object>" . $this->xmlEncode($value["value"]) . "</rdf:object>\n";
1130  $xmlRei .= " <".$this->prefixize($reifyAttributeUri).">". $this->xmlEncode($reifiedValue) . "</".$this->prefixize($reifyAttributeUri).">\n";
1131  $xmlRei .= " </rdf:Statement> \n\n";
1132  }
1133  }
1134  }
1135  }
1136  }
1137  }
1138  elseif(isset($value["uri"]))
1139  {
1140  // It is a resource URI
1141  if($value["uri"] != "")
1142  {
1143  $xml .= ' <'.$this->xmlEncode($this->prefixize($attributeURI)).' rdf:resource="'.$this->xmlEncode($value["uri"]).'" />'."\n";
1144 
1145  if(isset($value["reify"]))
1146  {
1147  foreach($value["reify"] as $reifyAttributeUri => $reifiedValues)
1148  {
1149  foreach($reifiedValues as $reifiedValue)
1150  {
1151  if($reifiedValue !== "")
1152  {
1153  $xmlRei .= " <rdf:Statement rdf:about=\"". $this->xmlEncode("bnode:".md5($recordURI . $attributeURI . $value["uri"])) . "\">\n";
1154  $xmlRei .= " <rdf:subject rdf:resource=\"" . $recordURI . "\" />\n";
1155  $xmlRei .= " <rdf:predicate rdf:resource=\"" . $this->xmlEncode($attributeURI) . "\" />\n";
1156  $xmlRei .= " <rdf:object rdf:resource=\"" . $this->xmlEncode($value["uri"]) . "\" />\n";
1157  $xmlRei .= " <".$this->prefixize($reifyAttributeUri).">". $this->xmlEncode($reifiedValue) . "</".$this->prefixize($reifyAttributeUri).">\n";
1158  $xmlRei .= " </rdf:Statement> \n\n";
1159  }
1160  }
1161  }
1162  }
1163  }
1164  }
1165  }
1166  break;
1167  }
1168  }
1169 
1170  $xml .= ' </'.$this->xmlEncode($this->prefixize($firstType)).'>'."\n";
1171  $xml .= $xmlRei;
1172  }
1173  }
1174 
1175 
1176  $xmlHeader = "<?xml version=\"1.0\"?>\n";
1177  $xmlHeader .= "<rdf:RDF";
1178 
1179  $nb = 0;
1180  foreach($this->prefixes as $uri => $entity)
1181  {
1182  if($nb == 0)
1183  {
1184  $xmlHeader .= " xmlns:$entity=\"$uri\"\n";
1185  }
1186  else
1187  {
1188  $xmlHeader .= " xmlns:$entity=\"$uri\"\n";
1189  }
1190 
1191  $nb++;
1192  }
1193 
1194  $xmlHeader = rtrim($xmlHeader, "\n");
1195 
1196  $xmlHeader .= ">\n\n";
1197 
1198 
1199  $xml = $xmlHeader.$xml;
1200 
1201  $xml .= "</rdf:RDF>";
1202 
1203  return($xml);
1204  }
1205 
1215  public function getResultsetRDFN3()
1216  {
1217  $json = '';
1218 
1219  foreach($this->resultset as $datasetURI => $records)
1220  {
1221  foreach($records as $recordURI => $record)
1222  {
1223  $jsonRei = '';
1224 
1225  // Determine the first (main) type of the record
1226  $firstType = "http://www.w3.org/2002/07/owl#Thing";
1227 
1228  if(isset($record["type"][0]))
1229  {
1230  $firstType = $record["type"][0];
1231  }
1232 
1233  $json .= '<'.$recordURI.'> a '.$this->prefixize($firstType).' ;'."\n";
1234 
1235  $jsonPaddingSize = "";
1236 
1237  for($i = 0; $i < strlen('<'.$recordURI.'> '); $i++)
1238  {
1239  $jsonPaddingSize .= " ";
1240  }
1241 
1242  foreach($record as $attributeURI => $attributeValues)
1243  {
1244  switch($attributeURI)
1245  {
1246  case "type":
1247  foreach($attributeValues as $key => $type)
1248  {
1249  if($key > 0 && $type != "")
1250  {
1251  $json .= $jsonPaddingSize.'a'.' '.$this->prefixize($type)." ;\n";
1252  }
1253  }
1254  break;
1255  case "prefLabel":
1256  if($attributeValues != "")
1257  {
1258  if(!isset($record[Namespaces::$iron.'prefLabel']) || !$this->in_array_r($attributeValues, $record[Namespaces::$iron.'prefLabel']))
1259  {
1260  $json .= $jsonPaddingSize.'iron:prefLabel """'.$this->jsonEncode($attributeValues).'"""'." ;\n";
1261  }
1262  }
1263  break;
1264  case "altLabel":
1265  foreach($attributeValues as $altLabel)
1266  {
1267  if($altLabel != "")
1268  {
1269  if(!isset($record[Namespaces::$iron.'altLabel']) || !$this->in_array_r($altLabel, $record[Namespaces::$iron.'altLabel']))
1270  {
1271  $json .= $jsonPaddingSize.'iron:altLabel """'.$this->jsonEncode($altLabel).'"""'." ;\n";
1272  }
1273  }
1274  }
1275  break;
1276  case "description":
1277  if($attributeValues != "")
1278  {
1279  if(!isset($record[Namespaces::$iron.'description']) || !$this->in_array_r($attributeValues, $record[Namespaces::$iron.'description']))
1280  {
1281  $json .= $jsonPaddingSize.'iron:description """'.$this->jsonEncode($attributeValues).'"""'." ;\n";
1282  }
1283  }
1284  break;
1285  case "prefURL":
1286  if($attributeValues != "")
1287  {
1288  if(!isset($record[Namespaces::$iron.'prefURL']) || !$this->in_array_r($attributeValues, $record[Namespaces::$iron.'prefURL']))
1289  {
1290  $json .= $jsonPaddingSize.'iron:prefURL """'.$this->jsonEncode($attributeValues).'"""'." ;\n";
1291  }
1292  }
1293  break;
1294 
1295  // Any other attribute describing this record
1296  default:
1297  foreach($attributeValues as $value)
1298  {
1299  if(isset($value["value"]))
1300  {
1301  // It is a literal value, or a literal value of some type (int, bool, etc)
1302  if($value["value"] !== "")
1303  {
1304  if(isset($value["type"]) && $value["type"] != "" && $value["type"] != "rdfs:Literal" && $value["type"] != Namespaces::$rdfs."Literal")
1305  {
1306  $json .= $jsonPaddingSize.$this->prefixize($attributeURI).' """'.$this->jsonEncode($value["value"]).'"""^^<'.$this->unprefixize($value["type"])."> ;\n";
1307  }
1308  elseif(isset($value["lang"]) && $value["lang"] != "")
1309  {
1310  $json .= $jsonPaddingSize.$this->prefixize($attributeURI).' """'.$this->jsonEncode($value["value"]).'"""@'.$value["lang"]." ;\n";
1311  }
1312  else
1313  {
1314  $json .= $jsonPaddingSize.$this->prefixize($attributeURI).' """'.$this->jsonEncode($value["value"]).'"""'." ;\n";
1315  }
1316 
1317  if(isset($value["reify"]))
1318  {
1319  foreach($value["reify"] as $reifyAttributeUri => $reifiedValues)
1320  {
1321  foreach($reifiedValues as $reifiedValue)
1322  {
1323  if($reifiedValue !== "")
1324  {
1325  $jsonRei .= "_:b" . md5($recordURI . $attributeURI . $value["value"]). " a rdf:Statement ;\n";
1326  $jsonRei .= " rdf:subject <" . $recordURI . "> ;\n";
1327  $jsonRei .= " rdf:predicate <" . $attributeURI . "> ;\n";
1328  $jsonRei .= " rdf:object \"\"\"" . $value["value"] . "\"\"\" ;\n";
1329  $jsonRei .= " " . $this->prefixize($reifyAttributeUri) . " \"\"\"" . $this->jsonEncode($reifiedValue). "\"\"\" .\n\n";
1330  }
1331  }
1332  }
1333  }
1334  }
1335  }
1336  elseif(isset($value["uri"]))
1337  {
1338  // It is a resource URI
1339  if($value["uri"] != "")
1340  {
1341  $json .= $jsonPaddingSize.$this->prefixize($attributeURI).' <'.$this->jsonEncode($value["uri"]).'>'." ;\n";
1342 
1343  if(isset($value["reify"]))
1344  {
1345  foreach($value["reify"] as $reifyAttributeUri => $reifiedValues)
1346  {
1347  foreach($reifiedValues as $reifiedValue)
1348  {
1349  if($reifiedValue !== "")
1350  {
1351  $jsonRei .= "_:b" . md5($recordURI . $attributeURI . $value["uri"]). " a rdf:Statement ;\n";
1352  $jsonRei .= " rdf:subject <" . $recordURI . "> ;\n";
1353  $jsonRei .= " rdf:predicate <" . $attributeURI . "> ;\n";
1354  $jsonRei .= " rdf:object <" . $value["uri"] . "> ;\n";
1355  $jsonRei .= " " . $this->prefixize($reifyAttributeUri) . " \"\"\"" . $this->jsonEncode($reifiedValue). "\"\"\" .\n\n";
1356  }
1357  }
1358  }
1359  }
1360  }
1361  }
1362  }
1363  break;
1364  }
1365  }
1366 
1367  if(substr($json, strlen($json) - 2) == ";\n")
1368  {
1369  // Set the proper terminason for the N3 record.
1370  $json = substr($json, 0, strlen($json) - 2).".\n\n";
1371  }
1372 
1373  $json .= $jsonRei;
1374  }
1375  }
1376 
1377 
1378  $jsonHeader = "";
1379 
1380  foreach($this->prefixes as $uri => $entity)
1381  {
1382  $jsonHeader .= "@prefix $entity: <$uri> .\n";
1383  }
1384 
1385  $jsonHeader .= "\n";
1386 
1387 
1388  $json = $jsonHeader.$json;
1389 
1390  return($json);
1391  }
1392 
1402  private function xmlEncode($string)
1403  {
1404  // Replace all the possible entities by their character. That way, we won't "double encode"
1405  // these entities. Otherwise, we can endup with things such as "&amp;amp;" which some
1406  // XML parsers doesn't seem to like (and throws errors).
1407  $string = str_replace(array ("&amp;", "&lt;", "&gt;", "&quot;"), array ("&", "<", ">", '"'), $string);
1408 
1409  return str_replace(array ("&", "<", ">", '"'), array ("&amp;", "&lt;", "&gt;", "&quot;"), $string);
1410  }
1411 
1421  private function jsonEncode($string) { return str_replace(array ('\\', '"', "\n", "\r", "\t"), array ('\\\\', '\\"', " ", " ", "\\t"), $string); }
1422 
1431  private function prefixize($uri)
1432  {
1433  // Check if it is already prefixed
1434  if(substr($uri, 0, 7) != "http://")
1435  {
1436  return($uri);
1437  }
1438 
1439  $prefixedUri = $this->namespaces->getPrefixedUri($uri);
1440 
1441  if($prefixedUri == $uri)
1442  {
1443  $pos = strripos($uri, "#");
1444 
1445  if($pos)
1446  {
1447  $baseUri = substr($uri, 0, $pos + 1);
1448  $uriExt = substr($uri, $pos + 1);
1449 
1450  if(!isset($this->prefixes[$baseUri]))
1451  {
1452  // A new prefix needs to be created
1453  $newPrefix = "ns".$this->newPrefixesCounter;
1454 
1455  $this->prefixes[$baseUri] = $newPrefix;
1456 
1457  $this->newPrefixesCounter++;
1458  }
1459 
1460  return($this->prefixes[$baseUri].':'.$uriExt);
1461  }
1462  else
1463  {
1464  $pos = strripos($uri, "/");
1465 
1466  $baseUri = substr($uri, 0, $pos + 1);
1467  $uriExt = substr($uri, $pos + 1);
1468 
1469  if(!isset($this->prefixes[$baseUri]))
1470  {
1471  // A new prefix needs to be created
1472  $newPrefix = "ns".$this->newPrefixesCounter;
1473 
1474  $this->prefixes[$baseUri] = $newPrefix;
1475 
1476  $this->newPrefixesCounter++;
1477  }
1478 
1479  if($pos)
1480  {
1481  return($this->prefixes[$baseUri].':'.$uriExt);
1482  }
1483  else
1484  {
1485  return($uri);
1486  }
1487  }
1488  }
1489  else
1490  {
1491  // The URI got prefixed
1492  $pieces = explode(":", $prefixedUri);
1493 
1494  if(!isset($this->prefixes[$this->namespaces->getUri($pieces[0])]))
1495  {
1496  $this->prefixes[$this->namespaces->getUri($pieces[0])] = $pieces[0];
1497  }
1498 
1499  return($prefixedUri);
1500  }
1501  }
1502 
1510  private function unprefixize($uri)
1511  {
1512  $pos = strpos($uri, ":");
1513 
1514  if($pos !== FALSE)
1515  {
1516  $prefix = substr($uri, 0, $pos);
1517 
1518  foreach($this->prefixes as $namespace => $px)
1519  {
1520  if($prefix == $px)
1521  {
1522  return($namespace.substr($uri, ($pos + 1)));
1523  }
1524  }
1525 
1526  return($uri);
1527  }
1528  else
1529  {
1530  return($uri);
1531  }
1532  }
1533 
1537  private function in_array_r($needle, $haystack, $strict = false)
1538  {
1539  foreach($haystack as $item)
1540  {
1541  if(($strict ? $item === $needle : $item == $needle) || (is_array($item) && $this->in_array_r($needle, $item, $strict)))
1542  {
1543  return true;
1544  }
1545  }
1546 
1547  return false;
1548  }
1549 }
1550 
1552 
1553 ?>
getResultsetIronCOMMON()
Convert an internal OSF resultset array structure in irON commON.
Definition: Resultset.php:1002
Internal Resultset representation of OSF results.
Definition: Resultset.php:33
getResultsetRDFN3()
Convert an internal OSF resultset array structure in RDF+N3.
Definition: Resultset.php:1215
getResultset()
Get the array internal description of the resultset.
Definition: Resultset.php:230
getSubjects()
Get all the subjects defined in this resultset.
Definition: Resultset.php:178
addSubject($subject, $dataset="")
Add a Subject object to the resultset.
Definition: Resultset.php:143
getResultsetXML()
Convert an internal OSF resultset array structure in structXML.
Definition: Resultset.php:526
getResultsetIronJSON()
Convert an internal OSF resultset array structure in irON JSON.
Definition: Resultset.php:970
getResultsetRDFXML()
Convert an internal OSF resultset array structure in RDF+XML.
Definition: Resultset.php:1036
getResultsetJSON()
Convert an internal OSF resultset array structure in structJSON.
Definition: Resultset.php:688
getSubjectByUri($uri)
Get a subject by its URI.
Definition: Resultset.php:204
setResultset($resultset)
Import an array that describes the resultset to use with this Resultset object.
Definition: Resultset.php:287
importStructXMLResultset($structXMLResultset)
Import a structXML resultset.
Definition: Resultset.php:299
__construct($wsf_base_path="/usr/share/osf/")
Constructor.
Definition: Resultset.php:126