utilities.php
Go to the documentation of this file.
00001 <?php 00002 00003 00004 00007 00036 function get_uri_label($uri) 00037 { 00038 // Find the base URI of the ontology 00039 $pos = strripos($uri, "#"); 00040 00041 if ($pos === FALSE) 00042 { 00043 $pos = strripos($uri, "/"); 00044 } 00045 00046 if ($pos !== FALSE) 00047 { 00048 $pos++; 00049 } 00050 00051 // Save the URI of the ontology 00052 $onto = substr($uri, 0, $pos); 00053 00054 // Save the URI of the class or property passed in parameter 00055 $resource = substr($uri, $pos, strlen($uri) - $pos); 00056 00057 // This statement associate a base ontology URI to its prefix. 00058 switch ($onto) 00059 { 00060 case "http://xmlns.com/foaf/0.1/": 00061 return "foaf:" . $resource; 00062 break; 00063 00064 case "http://blogs.yandex.ru/schema/foaf/": 00065 return "yandex_foaf:" . $resource; 00066 break; 00067 00068 case "http://purl.org/rss/1.0/": 00069 return "rss:" . $resource; 00070 break; 00071 00072 case "http://rdfs.org/sioc/ns#": 00073 return "sioc:" . $resource; 00074 break; 00075 00076 case "http://usefulinc.com/ns/doap#": 00077 return "doap:" . $resource; 00078 break; 00079 00080 case "http://www.w3.org/2003/01/geo/wgs84_pos#": 00081 return "geo:" . $resource; 00082 break; 00083 00084 case "http://www.geonames.org/ontology#": 00085 return "geonames:" . $resource; 00086 break; 00087 00088 case "http://creativecommons.org/ns#": 00089 return "cc:" . $resource; 00090 break; 00091 00092 case "http://sw.cyc.com/2006/07/27/cyc/": 00093 00094 case "http://sw.opencyc.org/2008/06/10/concept/en/": 00095 00096 case "http://sw.opencyc.org/2008/06/10/concept/": 00097 00098 case "http://sw.opencyc.org/concept/": 00099 00100 case "http://sw.cyc.com/concept/": 00101 return "cyc:" . $resource; 00102 break; 00103 00104 case "http://purl.org/umbel/sc/": 00105 00106 case "http://purl.org/umbel/ac/": 00107 00108 case "http://umbel.org/ns/": 00109 00110 case "http://umbel.org/ns/sc/": 00111 00112 case "http://umbel.org/ns/ac/": 00113 00114 case "http://umbel.org/umbel/": 00115 00116 case "http://umbel.org/umbel#": 00117 00118 case "http://umbel.org/umbel/sc/": 00119 00120 case "http://umbel.org/umbel/ac/": 00121 return "umbel:" . $resource; 00122 break; 00123 00124 case "http://www.w3.org/2000/01/rdf-schema#": 00125 return "rdfs:" . $resource; 00126 break; 00127 00128 case "http://www.w3.org/1999/02/22-rdf-syntax-ns#": 00129 return "rdf:" . $resource; 00130 break; 00131 00132 case "http://www.w3.org/2002/07/owl#": 00133 return "owl:" . $resource; 00134 break; 00135 00136 case "http://purl.org/dc/terms/": 00137 return "dcterms:" . $resource; 00138 break; 00139 00140 case "http://www.w3.org/2004/02/skos/core#": 00141 00142 case "http://www.w3.org/2008/05/skos#": 00143 return "skos:" . $resource; 00144 break; 00145 00146 case "http://purl.org/ontology/po/": 00147 return "po:" . $resource; 00148 break; 00149 00150 case "http://purl.org/NET/c4dm/event.owl#": 00151 return "event:" . $resource; 00152 break; 00153 00154 case "http://purl.org/vocab/frbr/core#": 00155 return "frbr:" . $resource; 00156 break; 00157 00158 case "http://purl.org/ontology/mo/": 00159 return "mo:" . $resource; 00160 break; 00161 00162 case "http://purl.org/ontology/bibo/": 00163 return "bibo:" . $resource; 00164 break; 00165 00166 case "http://purl.org/ontology/bkn#": 00167 return "bkn:" . $resource; 00168 break; 00169 00170 case "http://purl.org/ontology/bkn/central#": 00171 return "bkn_central:" . $resource; 00172 break; 00173 00174 case "http://purl.org/ontology/bkn/temp#": 00175 return "bkn-temp:" . $resource; 00176 break; 00177 00178 case "http://purl.org/ontology/bkn/base/": 00179 return "bkn-base:" . $resource; 00180 break; 00181 00182 case "http://purl.org/vocab/bio/0.1/": 00183 return "bio:" . $resource; 00184 break; 00185 00186 case "http://schemas.talis.com/2005/address/schema#": 00187 return "address:" . $resource; 00188 break; 00189 00190 case "http://schemas.talis.com/2005/address/schema#": 00191 return "address:" . $resource; 00192 break; 00193 00194 case "http://purl.org/ontology/swt#": 00195 return "swt:" . $resource; 00196 break; 00197 00198 case "http://purl.org/ontology/iron#": 00199 return "iron:" . $resource; 00200 break; 00201 00202 case "http://purl.org/ontology/muni#": 00203 return "muni:" . $resource; 00204 break; 00205 00206 case "http://purl.org/ontology/peg#": 00207 return "peg:" . $resource; 00208 break; 00209 00210 case "http://www.geonames.org/ontology#": 00211 return "geoname:" . $resource; 00212 break; 00213 00214 // By default, we return the "un-prefixed" URI passed in parameter. 00215 default: 00216 return $uri; 00217 break; 00218 } 00219 } 00220 00236 function get_label_uri($label) 00237 { 00238 $pos = strripos($label, ":"); 00239 00240 if ($pos === FALSE) 00241 { 00242 return ($label); 00243 } 00244 00245 $pos++; 00246 00247 $onto = substr($label, 0, $pos); 00248 00249 $resource = substr($label, $pos, strlen($label) - $pos); 00250 00251 switch ($onto) 00252 { 00253 case "foaf:": 00254 return "http://xmlns.com/foaf/0.1/" . $resource; 00255 break; 00256 00257 case "yandex_foaf:": 00258 return "http://blogs.yandex.ru/schema/foaf/" . $resource; 00259 break; 00260 00261 case "rss:": 00262 return "http://purl.org/rss/1.0/" . $resource; 00263 break; 00264 00265 case "sioc:": 00266 return "http://rdfs.org/sioc/ns#" . $resource; 00267 break; 00268 00269 case "doap:": 00270 return "http://usefulinc.com/ns/doap#" . $resource; 00271 break; 00272 00273 case "geo:": 00274 return "http://www.w3.org/2003/01/geo/wgs84_pos#" . $resource; 00275 break; 00276 00277 case "geonames:": 00278 return "http://www.geonames.org/ontology#" . $resource; 00279 break; 00280 00281 case "cc:": 00282 return "http://creativecommons.org/ns#" . $resource; 00283 break; 00284 00285 case "rdfs:": 00286 return "http://www.w3.org/2000/01/rdf-schema#" . $resource; 00287 break; 00288 00289 case "rdf:": 00290 return "http://www.w3.org/1999/02/22-rdf-syntax-ns#" . $resource; 00291 break; 00292 00293 case "owl:": 00294 return "http://www.w3.org/2002/07/owl#" . $resource; 00295 break; 00296 00297 case "dcterms:": 00298 return "http://purl.org/dc/terms/" . $resource; 00299 break; 00300 00301 case "skos:": 00302 return "http://www.w3.org/2008/05/skos#" . $resource; 00303 break; 00304 00305 case "po:": 00306 return "http://purl.org/ontology/po/" . $resource; 00307 break; 00308 00309 case "event:": 00310 return "http://purl.org/NET/c4dm/event.owl#" . $resource; 00311 break; 00312 00313 case "frbr:": 00314 return "http://purl.org/vocab/frbr/core#" . $resource; 00315 break; 00316 00317 case "mo:": 00318 return "http://purl.org/ontology/mo/" . $resource; 00319 break; 00320 00321 case "bibo:": 00322 return "http://purl.org/ontology/bibo/" . $resource; 00323 break; 00324 00325 case "bkn:": 00326 return "http://purl.org/ontology/bkn#" . $resource; 00327 break; 00328 00329 case "bkn-temp:": 00330 return "http://purl.org/ontology/bkn/temp#" . $resource; 00331 break; 00332 00333 case "bkn_central:": 00334 return "http://purl.org/ontology/bkn/central#" . $resource; 00335 break; 00336 00337 case "bkn-base:": 00338 return "http://purl.org/ontology/bkn/base/" . $resource; 00339 break; 00340 00341 case "bio:": 00342 return "http://purl.org/vocab/bio/0.1/" . $resource; 00343 break; 00344 00345 case "address:": 00346 return "http://schemas.talis.com/2005/address/schema#" . $resource; 00347 break; 00348 00349 case "swt:": 00350 return "http://purl.org/ontology/swt#" . $resource; 00351 break; 00352 00353 case "iron:": 00354 return "http://purl.org/ontology/iron#" . $resource; 00355 break; 00356 00357 case "peg:": 00358 return "http://purl.org/ontology/peg#" . $resource; 00359 break; 00360 00361 case "muni:": 00362 return "http://purl.org/ontology/muni#" . $resource; 00363 break; 00364 00365 case "geoname:": 00366 return "http://www.geonames.org/ontology#" . $resource; 00367 break; 00368 00369 case "umbel:": 00370 return "http://umbel.org/umbel#" . $resource; 00371 break; 00372 00373 default: 00374 return $label; 00375 break; 00376 } 00377 } 00378 00391 function get_domain($url) 00392 { 00393 if (strlen($url) > 8) 00394 { 00395 $pos = strpos($url, "/", 8); 00396 00397 if ($pos === FALSE) 00398 { 00399 return $url; 00400 } 00401 else 00402 { 00403 return substr($url, 0, $pos); 00404 } 00405 } 00406 else 00407 { 00408 return $url; 00409 } 00410 } 00411 00426 function rdfDocumentSerialization(&$data) 00427 { 00428 // Check if we have a RDF/XML document 00429 if (preg_match('/<.*:rdf>/i', $data)) 00430 { 00431 return "application/rdf+xml"; 00432 } 00433 00434 // Check if we have a RDF/N3 document 00435 if (preg_match('/prefix:.*|<.*>.*[\.;]{1}|:.*[\.;]{1}/i', $data)) 00436 { 00437 return "application/rdf+n3"; 00438 } 00439 00440 // Return FALSE if the serialization is unknown 00441 return FALSE; 00442 } 00443 00454 function getCoreGraphURI() 00455 { 00456 global $base_url; 00457 00458 // Nomalization of the URL of the node. Here we remove the "www" to normalize it. 00459 $normalized_base_url = str_replace("www.", "", $base_url); 00460 00461 $graph = variable_get('conStruct_UrisDomain', str_replace("http://", "", get_domain($normalized_base_url))); 00462 00463 return ("http://" . $graph . "/data/core/"); 00464 } 00465 00478 function getResourceBaseURI($dataset = "") 00479 { 00480 global $base_url; 00481 00482 $pos = strpos($dataset, "/datasets/"); 00483 00484 if ($pos !== FALSE) 00485 { 00486 // Nomalization of the URL of the node. Here we remove the "www" to normalize it. 00487 $normalized_base_url = str_replace("www.", "", $base_url); 00488 00489 $domain = variable_get('conStruct_UrisDomain', str_replace("http://", "", get_domain($normalized_base_url))); 00490 00491 // Make sure we keep the relative folder of the drupal installation (normally /drupal for default installations) 00492 $domain = str_replace(str_replace("http://", "", get_domain($base_url)), $domain, $base_url); 00493 00494 return ($domain . "/conStruct/datasets/" . substr($dataset, $pos + 10) . "resource/"); 00495 } 00496 00497 return (FALSE); 00498 } 00499 00513 function convertResourceCurrentReference($uri) 00514 { 00515 global $base_url; 00516 00517 return (str_replace(get_domain($uri), get_domain($base_url), $uri)); 00518 } 00519 00535 function getDatasetFromUri($uri) 00536 { 00537 global $base_url; 00538 // http://....org/drupal/bkn/datasets/146/resource/test 00539 // http://....org/wsf/datasets/144/ 00540 00541 $pos = strpos($uri, "/datasets/"); 00542 $posEnd = strpos($uri, "/", ($pos + 10)); 00543 00544 if ($pos !== FALSE) 00545 { 00546 // Nomalization of the URL of the node. Here we remove the "www" to normalize it. 00547 $normalized_base_url = str_replace("www.", "", $base_url); 00548 00549 $domain = variable_get('conStruct_UrisDomain', str_replace("http://", "", get_domain($normalized_base_url))); 00550 00551 return ("http://" . $domain . "/wsf/datasets/" . substr($uri, ($pos + 10), ($posEnd - ($pos + 10))) . "/"); 00552 } 00553 00554 return (FALSE); 00555 } 00556 00569 function getGroupIdFromDatasetUri($dataset) 00570 { 00571 $pos = strpos($dataset, "/datasets/"); 00572 00573 if ($pos !== FALSE) 00574 { 00575 $posEnd = strpos($dataset, "/", ($pos + 10)); 00576 00577 if ($posEnd !== FALSE) 00578 { 00579 return (substr($dataset, ($pos + 10), ($posEnd - ($pos + 10)))); 00580 } 00581 } 00582 00583 return (FALSE); 00584 } 00585 00599 function getResourceLabel($subjectTriples, $defaultLabel) 00600 { 00601 include_once("Namespaces.php"); 00602 00603 $labelProperties = array( 00604 Namespaces::$iron . "prefLabel", 00605 Namespaces::$iron . "altLabel", 00606 Namespaces::$dcterms . "title", 00607 Namespaces::$foaf . "name", 00608 Namespaces::$foaf . "givenname", 00609 Namespaces::$foaf . "family_name", 00610 Namespaces::$rdfs . "label", 00611 Namespaces::$skos_2004 . "prefLabel", 00612 Namespaces::$skos_2004 . "altLabel", 00613 Namespaces::$skos_2008 . "prefLabel", 00614 Namespaces::$skos_2008 . "altLabel", 00615 Namespaces::$doap . "name", 00616 Namespaces::$geonames."name" 00617 ); 00618 00619 $labelCompositeProperties = array( 00620 Namespaces::$foaf . "givenname" => array( 00621 Namespaces::$foaf . "givenname", 00622 Namespaces::$foaf . "family_name" 00623 ), 00624 Namespaces::$foaf . "family_name" => array( 00625 Namespaces::$foaf . "givenname", 00626 Namespaces::$foaf . "family_name" 00627 ) 00628 ); 00629 00630 foreach ($labelProperties as $property) 00631 { 00632 if (isset($subjectTriples[$property])) 00633 { 00634 // Check if it is part of a composite label 00635 if (isset($labelCompositeProperties[$property])) 00636 { 00637 $label = ""; 00638 00639 foreach ($labelCompositeProperties[$property] as $clabel) 00640 { 00641 $label .= $subjectTriples[$clabel][0]["text"] . " "; 00642 } 00643 00644 return (substr($label, 0, (strlen($label) - 1))); 00645 } 00646 00647 return $subjectTriples[$property][0]["text"]; 00648 } 00649 } 00650 00651 return ($defaultLabel); 00652 } 00653 00654 /* 00655 Working with XML. Usage: 00656 $xml=xml2ary(file_get_contents('1.xml')); 00657 $link=&$xml['ddd']['_c']; 00658 $link['twomore']=$link['onemore']; 00659 // ins2ary(); // dot not insert a link, and arrays with links inside! 00660 echo ary2xml($xml); 00661 00662 from: http://mysrc.blogspot.com/2007/02/php-xml-to-array-and-backwards.html 00663 */ 00664 00665 // XML to Array 00666 function xml2ary(&$string) 00667 { 00668 $parser = xml_parser_create(); 00669 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 00670 xml_parse_into_struct($parser, $string, $vals, $index); 00671 xml_parser_free($parser); 00672 00673 $mnary = array(); 00674 $ary = &$mnary; 00675 00676 foreach ($vals as $r) 00677 { 00678 $t = $r['tag']; 00679 00680 if ($r['type'] == 'open') 00681 { 00682 if (isset($ary[$t])) 00683 { 00684 if (isset($ary[$t][0])) $ary[$t][] = array(); 00685 else $ary[$t] = array( 00686 $ary[$t], 00687 array() 00688 ); 00689 00690 $cv = &$ary[$t][count($ary[$t]) - 1]; 00691 } 00692 else $cv = &$ary[$t]; 00693 00694 if (isset($r['attributes'])) 00695 { 00696 foreach ($r['attributes'] as $k => $v) 00697 $cv['_a'][$k] = $v; 00698 } 00699 $cv['_c'] = array(); 00700 $cv['_c']['_p'] = &$ary; 00701 $ary = &$cv['_c']; 00702 } 00703 elseif ($r['type'] == 'complete') 00704 { 00705 if (isset($ary[$t])) 00706 { // same as open 00707 if (isset($ary[$t][0])) $ary[$t][] = array(); 00708 else $ary[$t] = array( 00709 $ary[$t], 00710 array() 00711 ); 00712 00713 $cv = &$ary[$t][count($ary[$t]) - 1]; 00714 } 00715 else $cv = &$ary[$t]; 00716 00717 if (isset($r['attributes'])) 00718 { 00719 foreach ($r['attributes'] as $k => $v) 00720 $cv['_a'][$k] = $v; 00721 } 00722 $cv['_v'] = (isset($r['value']) ? $r['value'] : ''); 00723 } 00724 elseif ($r['type'] == 'close') 00725 { 00726 $ary = &$ary['_p']; 00727 } 00728 } 00729 00730 _del_p($mnary); 00731 return $mnary; 00732 } 00733 00734 // _Internal: Remove recursion in result array 00735 function _del_p(&$ary) 00736 { 00737 foreach ($ary as $k => $v) 00738 { 00739 if ($k === '_p') unset($ary[$k]); 00740 00741 elseif (is_array($ary[$k])) _del_p($ary[$k]); 00742 } 00743 } 00744 00745 // Array to XML 00746 function ary2xml($cary, $d = 0, $forcetag = '') 00747 { 00748 $res = array(); 00749 00750 foreach ($cary as $tag => $r) 00751 { 00752 if (isset($r[0])) 00753 { 00754 $res[] = ary2xml($r, $d, $tag); 00755 } 00756 else 00757 { 00758 if ($forcetag) $tag = $forcetag; 00759 $sp = str_repeat("\t", $d); 00760 $res[] = "$sp<$tag"; 00761 00762 if (isset($r['_a'])) 00763 { 00764 foreach ($r['_a'] as $at => $av) 00765 $res[] = " $at=\"$av\""; 00766 } 00767 $res[] = ">" . ((isset($r['_c'])) ? "\n" : ''); 00768 00769 if (isset($r['_c'])) $res[] = ary2xml($r['_c'], $d + 1); 00770 00771 elseif (isset($r['_v'])) $res[] = $r['_v']; 00772 $res[] = (isset($r['_c']) ? $sp : '') . "</$tag>\n"; 00773 } 00774 } 00775 return implode('', $res); 00776 } 00777 00778 // Insert element into array 00779 function ins2ary(&$ary, $element, $pos) 00780 { 00781 $ar1 = array_slice($ary, 0, $pos); 00782 $ar1[] = $element; 00783 $ary = array_merge($ar1, array_slice($ary, $pos)); 00784 } 00785 00786 function linkify($text) 00787 { 00788 $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\">\\0</a>", $text); 00789 00790 return $text; 00791 } 00792 00793 00795 00796 ?>
