/** * Answers a SPARQL query. * * @param string $rawQuery * @return SPARQL XML string * @throws Exception, SOAPExeption */ function answerSPARQL($query, $params) { global $wgServer, $wgScript, $smwgWebserviceProtocol, $smwgWebserviceUser, $smwgWebservicePassword; if (isset($smwgWebserviceProtocol) && strtolower($smwgWebserviceProtocol) === 'rest') { global $smwgTripleStoreGraph; if (stripos(trim($query), 'SELECT') === 0 || stripos(trim($query), 'PREFIX') === 0) { // SPARQL, attach common prefixes $query = TSNamespaces::getAllPrefixes() . $query; } $queryRequest = "<query>"; $queryRequest .= "<text><![CDATA[{$query}]]></text>"; $queryRequest .= "<params><![CDATA[{$params}]]></params>"; $queryRequest .= "<graph><![CDATA[{$smwgTripleStoreGraph}]]></graph>"; $queryRequest .= "</query>"; global $smwgWebserviceUser, $smwgWebservicePassword, $smwgWebserviceEndpoint; list($host, $port) = explode(":", $smwgWebserviceEndpoint); $credentials = isset($smwgWebserviceUser) ? $smwgWebserviceUser . ":" . $smwgWebservicePassword : ""; $queryClient = new RESTWebserviceConnector($host, $port, "sparql", $credentials); list($header, $status, $result) = $queryClient->send($queryRequest); if ($status != 200) { throw new Exception(strip_tags($result), $status); } return $result; } else { trigger_error("SOAP requests to TSC are not supported anymore."); } }
public function query($query, $params = "", $graph = "") { global $smwgTripleStoreGraph; if (empty($graph)) { $graph = $smwgTripleStoreGraph; } if (stripos(trim($query), 'SELECT') === 0 || stripos(trim($query), 'PREFIX') === 0) { // SPARQL, attach common prefixes $query = TSNamespaces::getAllPrefixes() . $query; } $queryRequest = "query=" . urlencode($query); $queryRequest .= "&default-graph-uri=" . urlencode($graph); $queryRequest .= "¶ms=" . urlencode($params); list($header, $status, $result) = $this->queryClient->send($queryRequest); if ($status != 200) { throw new Exception(strip_tags($result), $status); } return $result; }
public function addWSAsDataSource($wsId) { if (defined('LOD_LINKEDDATA_VERSION')) { global $IP; $this->removeWSFromDataSourceInformationGraph($wsId); //require_once($IP."/extensions/LinkedData/storage/TripleStore/LOD_PersistentTripleStoreAccess.php"); //require_once($IP."/extensions/LinkedData/storage/TripleStore/LOD_Triple.php"); $tsA = new LODPersistentTripleStoreAccess(true); $lAS = LODAdministrationStore::getInstance(); $tsA->createGraph($lAS->getSMWGraphsURI() . 'DataSourceInformationGraph'); $tsA->addPrefixes(TSNamespaces::getAllPrefixes()); $tsA->addPrefixes(LODAdministrationStore::getInstance()->getSourceDefinitionPrefixes()); $triples = array(); $triples[] = new LODTriple($this->getDataSourceURI("WS_" . $wsId), "rdf:type", "smw-lde:Datasource", "__objectURI"); $triples[] = new LODTriple($this->getDataSourceURI("WS_" . $wsId), "smw-lde:label", Title::newFromID($wsId)->getFullText(), "xsd:string"); $triples[] = new LODTriple($this->getDataSourceURI("WS_" . $wsId), "smw-lde:ID", Title::newFromID($wsId)->getFullText(), "xsd:string"); //todo: deal with persistency id=wsid $tsA->insertTriples($lAS->getSMWGraphsURI() . 'DataSourceInformationGraph', $triples); $tsA->flushCommands('di_ws', $wsId . '_dsinfo'); } }
private function getSPARQLQueryParts($query) { //todo:deal with categories $properties = array(); $categories = array(); $description = $query->getDescription(); $extraPrintOuts = $query->getExtraPrintouts(); if (!$description instanceof SMWSPARQLDescription) { //echo('<pre>'.print_r($description, true).'</pre>'); //echo('<pre>'.print_r($extraPrintOuts, true).'</pre>'); list($properties, $categories) = $this->getQueryParts($description); } else { if (!class_exists('ARC2')) { global $smwgHaloIP; require_once "{$smwgHaloIP}/libs/arc/ARC2.php"; } $prefixes = str_replace(':<', ': <', TSNamespaces::getAllPrefixes()); $queryString = $prefixes . $query->getQueryString(); $parser = ARC2::getSPARQLParser(); $parser->parse($queryString); $queryInfo = $parser->getQueryInfos(); if (array_key_exists('query', $queryInfo)) { if ($queryInfo['query']['type'] == 'select') { list($properties, $categories) = $this->getPropertiesInSPARQLPattern($queryInfo['query']['pattern'], $queryInfo['prefixes']['prop:'], $queryInfo['prefixes']['rdf:'] . 'type', $queryInfo['prefixes']['cat:']); } } } //deal with extra printouts $properties = array_merge($properties, $this->getPrintRequestsProperties($extraPrintOuts)); return array($properties, $categories); }