function query_bm($ref) { $textRefs = array(); $db = sparql_connect("http://collection.britishmuseum.org/sparql"); if (!$db) { print sparql_errno() . ": " . sparql_error() . "\n"; exit; } sparql_ns("bmo", "http://collection.britishmuseum.org/id/ontology/"); sparql_ns("ecrm", "http://erlangen-crm.org/current/"); sparql_ns("object", "http://collection.britishmuseum.org/id/object/"); $sparql = 'SELECT DISTINCT ?coin ?text WHERE { ?coin ecrm:P70i_is_documented_in <REF> . FILTER regex(str(?coin), "C[A-Z]{2}[0-9]+") . ?coin a ecrm:E22_Man-Made_Object ; ecrm:P50_has_current_keeper <http://collection.britishmuseum.org/id/thesauri/department/C> ; bmo:PX_display_wrap ?text . FILTER regex(?text, "Bibliograpic\\\\sreference") } LIMIT 10'; $result = sparql_query(str_replace('REF', $ref, $sparql)); if (!$result) { print sparql_errno() . ": " . sparql_error() . "\n"; exit; } $fields = sparql_field_array($result); while ($row = sparql_fetch_array($result)) { $textRefs[$row['coin']][] = $row['text']; /*foreach( $fields as $field ) { $textRefs }*/ } return $textRefs; }
function sparql_lookup($string, $base_uri, $endpoint) { $ret = ''; $ret .= "<table>\n"; // check if we have a WebID uri in the search input if (strpos(urldecode($string), '#') === false) { // search the local cache for a match $sparql = sparql_connect($endpoint); // Try to match against the name, nickname or webid. $query = 'SELECT DISTINCT ?webid WHERE { ?webid foaf:name ?name . ?webid foaf:nick ?nick . FILTER (regex(?name, "' . $string . '", "i") || regex(?nick, "' . $string . '", "i") || regex(?webid, "' . $string . '", "i")) MINUS { ?webid a foaf:Person . FILTER (regex(?webid, "nodeID", "i")) . FILTER (regex(?webid, "_:", "i")) } }'; $result = $sparql->query($query); if (!$result) { $ret .= error(sparql_errno() . ": " . sparql_error()); } while ($row = $result->fetch_array($result)) { $ret .= viewShortInfo($row['webid'], $_SESSION['webid'], $base_uri, $endpoint); } } else { // use the WebID source $ret .= viewShortInfo($string, $_SESSION['webid'], $base_uri, $endpoint); } $ret .= "</table>\n"; return $ret; }
function sparql_cache() { // Insert only real WebIDs (i.e. skip bnodes) $db = sparql_connect($this->endpoint); // first delete previous data for the graph $sql = "CLEAR GRAPH <" . $this->webid . ">"; $res = $db->query($sql); // Load URI into the triple store $sql = "LOAD <" . $this->webid . ">"; $res = $db->query($sql); if (!$res) { return sparql_errno() . ": " . sparql_error() . "\n"; exit; } if ($res->num_rows($res) > 0) { // Add the timestamp for the date at which it was inserted $time = time(); $date = date("Y", $time) . '-' . date("m", $time) . '-' . date("d", $time) . 'T' . date("H", $time) . ':' . date("i", $time) . ':' . date("s", $time); $sql = 'INSERT DATA INTO GRAPH IDENTIFIED BY <' . $this->webid . '> ' . '{<' . $this->webid . '> dc:date "' . $date . '"^^xsd:dateTime . }'; $res = $db->query($sql); return true; } else { return false; } }
<?php require_once "sparqllib.php"; $data = sparql_get("http://www.linkedfood.org:8890/sparql/", "prefix lgdo: <http://linkedgeodata.org/ontology/>\r\n\t\t\t\t\t\t\tselect distinct ?restaurant\r\n\t\t\t\t\t\t\twhere {\r\n\t\t\t\t\t\t\t\t\t?link a lgdo:Restaurant . ?link rdfs:label ?restaurant\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\torder by(?restaurant)\r\n\t\t\t\t\t\t"); if (!isset($data)) { print sparql_errno() . ": " . sparql_error() . "\n"; exit; } foreach ($data as $row) { $data_sparql[] = array($data->fields()[0] => $row[$data->fields()[0]]); } $json['results'] = $data_sparql; $information = json_encode($json); echo $information; ?>
function query_bm($writer, $uri) { $db = sparql_connect("http://collection.britishmuseum.org/sparql"); if (!$db) { print sparql_errno() . ": " . sparql_error() . "\n"; exit; } sparql_ns("thesDimension", "http://collection.britishmuseum.org/id/thesauri/dimension/"); sparql_ns("bmo", "http://collection.britishmuseum.org/id/ontology/"); sparql_ns("ecrm", "http://erlangen-crm.org/current/"); sparql_ns("object", "http://collection.britishmuseum.org/id/object/"); $sparql = "SELECT ?image ?weight ?axis ?diameter ?objectId ?hoard WHERE {\n OPTIONAL {<OBJECT> bmo:PX_has_main_representation ?image }\n OPTIONAL { <OBJECT> ecrm:P43_has_dimension ?wDim .\n ?wDim ecrm:P2_has_type thesDimension:weight .\n ?wDim ecrm:P90_has_value ?weight}\n OPTIONAL {\n <OBJECT> ecrm:P43_has_dimension ?wAxis .\n ?wAxis ecrm:P2_has_type thesDimension:die-axis .\n ?wAxis ecrm:P90_has_value ?axis\n }\n OPTIONAL {\n <OBJECT> ecrm:P43_has_dimension ?wDiameter .\n ?wDiameter ecrm:P2_has_type thesDimension:diameter .\n ?wDiameter ecrm:P90_has_value ?diameter\n }\n OPTIONAL {\n <OBJECT> ecrm:P1_is_identified_by ?identifier.\n ?identifier ecrm:P2_has_type <http://collection.britishmuseum.org/id/thesauri/identifier/codexid> ;\n rdfs:label ?objectId\n }\n OPTIONAL {\n <OBJECT> bmo:PX_display_wrap ?hoard . FILTER regex(?hoard, 'IGCH')\n }\n }"; $result = sparql_query(str_replace('OBJECT', $uri, $sparql)); if (!$result) { print sparql_errno() . ": " . sparql_error() . "\n"; exit; } $fields = sparql_field_array($result); while ($row = sparql_fetch_array($result)) { foreach ($fields as $field) { if (strlen($row[$field]) > 0) { if ($field == 'hoard') { preg_match('/IGCH\\s([0-9]+)/', $row[$field], $matches); if (isset($matches[1])) { $num = str_pad($matches[1], 4, "0", STR_PAD_LEFT); $hoardURI = 'http://coinhoards.org/id/igch' . $num; echo "Found hoard {$hoardURI}\n"; $writer->startElement('dcterms:isPartOf'); $writer->writeAttribute('rdf:resource', $hoardURI); $writer->endElement(); } } else { switch ($field) { case 'image': $writer->startElement('foaf:depiction'); $writer->writeAttribute('rdf:resource', $row[$field]); $writer->endElement(); break; case 'objectId': $writer->startElement('foaf:homepage'); $writer->writeAttribute('rdf:resource', "http://www.britishmuseum.org/research/collection_online/collection_object_details.aspx?objectId={$row[$field]}&partId=1"); $writer->endElement(); break; case 'axis': $writer->startElement('nmo:hasAxis'); $writer->writeAttribute('rdf:datatype', 'http://www.w3.org/2001/XMLSchema#integer'); $writer->text($row[$field]); $writer->endElement(); break; case 'weight': $writer->startElement('nmo:hasWeight'); $writer->writeAttribute('rdf:datatype', 'http://www.w3.org/2001/XMLSchema#decimal'); $writer->text($row[$field]); $writer->endElement(); break; case 'diameter': $writer->startElement('nmo:hasDiameter'); $writer->writeAttribute('rdf:datatype', 'http://www.w3.org/2001/XMLSchema#decimal'); $writer->text($row[$field]); $writer->endElement(); break; } } } } } }
require_once "include.php"; $sparql = sparql_connect(SPARQL_ENDPOINT); $input = $_REQUEST["term"]; $data = array(); // query the triple store looking for a match to $input $query = 'SELECT DISTINCT ?webid, ?name, ?img WHERE { ?webid foaf:name ?name . ?webid foaf:nick ?nick . ?webid foaf:img ?img . FILTER (regex(?name, "' . $input . '", "i") || regex(?nick, "' . $input . '", "i") || regex(?webid, "' . $input . '", "i")) MINUS { ?webid a foaf:Person . FILTER (regex(?webid, "nodeID", "i")) } } LIMIT 10'; $result = $sparql->query($query); if (!$result) { die(sparql_errno() . ": " . sparql_error()); } while ($row = sparql_fetch_array($result)) { $json = array(); $json['webid'] = $row['webid']; $json['name'] = $row['name']; $json['img'] = $row['img']; $json['label'] = '<table><tr>'; $json['label'] .= '<td><img width="30" src="' . $row['img'] . '"/></td>'; $json['label'] .= '<td><strong>' . $row['name'] . '</strong><br/>' . $row['webid'] . '</td>'; $json['label'] .= '</tr></table>'; $json['value'] = '<' . strtolower($row['webid']) . '>'; $data[] = $json; } // return data header("Content-type: application/json");
<? require_once( "sparqllib.php" ); $endpoints = array( "http://rdf.ecs.soton.ac.uk/sparql/"=>"Real endpoint", "http://"=>"Bad URL", "http://graphite.ecs.soton.ac.uk/not-real"=>"404 URL", "http://graphite.ecs.soton.ac.uk/sparqllib/examples/not-an-endpoint.txt"=>"Valid URL, but not an endpoint" ); foreach( $endpoints as $endpoint=>$desc) { $db = sparql_connect( $endpoint ); if( !$db ) { print sparql_errno() . ": " . sparql_error(). "\n"; exit; } print "<h3 style='border-top:solid 1px #666; padding-top:8px;margin-top:8px'>$desc</h3>"; print "<p>$endpoint</p>"; if( $db->alive() ) { print "<p>OK</p>"; } else { print "<p>Not alive: ".$db->error()."</p>"; } }
<body> <h1>Estados Brasileiros</h1> <h3>Dados trazidos de <a href="http://dbpedia.org/">dbpedia.org</a></h3> <?php require_once "sparqllib.php"; $db = sparql_connect("http://dbpedia.org/sparql/"); if (!$db) { print "<p class=\"error\">" . sparql_errno() . ": " . sparql_error() . "</p>"; exit; } sparql_ns("foaf", "http://xmlns.com/foaf/0.1/"); $sparql = "select distinct ?Estado ?Populacao where {\n ?estados <http://dbpedia.org/ontology/type> <http://dbpedia.org/resource/States_of_Brazil>. \n ?estados <http://dbpedia.org/property/name> ?Estado. \n ?estados <http://dbpedia.org/property/populationTotal> ?Populacao.\n } ORDER BY DESC(?Populacao)"; $result = sparql_query($sparql); if (!$result) { print "<p class=\"error\">" . sparql_errno() . ": " . sparql_error() . "</p>"; exit; } $fields = sparql_field_array($result); print "<table>"; print "<caption>População dos Estados brasileiros</caption>"; print "<tr>"; foreach ($fields as $field) { print "<th scope=\"col\">{$field}</th>"; } while ($row = sparql_fetch_array($result)) { print "<tr>"; foreach ($fields as $field) { $uf = str_ireplace("State", "", "{$row[$field]}"); $uf = str_ireplace(" of", "", "{$uf}"); $uf = str_ireplace("Brazilian Federal District", "Distrito Federal", "{$uf}");
<?php require_once "sparqllib.php"; $data = sparql_get("http://rdf.ecs.soton.ac.uk/sparql/", "\nPREFIX foaf: <http://xmlns.com/foaf/0.1/>\nSELECT * WHERE { ?person a foaf:Person . ?person foaf:name ?name } LIMIT 5\n"); if (!isset($data)) { print "<p>Error: " . sparql_errno() . ": " . sparql_error() . "</p>"; } print "<table class='example_table'>"; print "<tr>"; foreach ($data->fields() as $field) { print "<th>{$field}</th>"; } print "</tr>"; foreach ($data as $row) { print "<tr>"; foreach ($data->fields() as $field) { print "<td>{$row[$field]}</td>"; } print "</tr>"; } print "</table>";