/** * After a query is parsed, import the parsed data to the wiki */ function importTriplesInQuery() { if ($this->checkAllowInsert()) { $triples = $this->requestdata->query_parsed['query']['construct_triples']; $rdfImporter = new RDFIORDFImporter(); $rdfImporter->importTriples($triples); $this->successMsg("Successfully imported the triples!"); } }
protected function import($limit = 10, $offset = 0) { global $wgOut, $wgRequest; //$rdfioUtils = new RDFIOUtils(); $externalSparqlUrl = $wgRequest->getText('extsparqlurl'); if ($externalSparqlUrl === '') { throw new RDFIOException('Empty SPARQL Url provided!'); } else { if (!RDFIOUtils::isURI($externalSparqlUrl)) { throw new RDFIOException('Invalid SPARQL Url provided! (Must start with \'http://\' or \'https://\')'); } } $sparqlQuery = urlencode("SELECT DISTINCT * WHERE { ?s ?p ?o } OFFSET {$offset} LIMIT {$limit}"); $sparqlQueryUrl = $externalSparqlUrl . '/' . '?query=' . $sparqlQuery; $sparqlResultXml = file_get_contents($sparqlQueryUrl); $sparqlResultXmlObj = simplexml_load_string($sparqlResultXml); $importTriples = array(); if (is_object($sparqlResultXmlObj)) { foreach ($sparqlResultXmlObj->results->children() as $result) { $triple = array(); // $wgOut->addHTML( print_r($result, true) ); foreach ($result as $binding) { if ($binding['name'] == 's') { $s = (string) $binding->uri[0]; if ($s == '') { throw new Exception('Could not extract subject from empty string (' . print_r($binding->uri, true) . '), in SPARQLImport'); } $triple['s'] = $s; $triple['s_type'] = $this->resourceType($triple['s']); } else { if ($binding['name'] == 'p') { $p = (string) $binding->uri[0]; if ($p == '') { throw new Exception('Could not extract predicate from empty string (' . print_r($binding->uri, true) . '), in SPARQLImport'); } $triple['p'] = $p; $triple['p_type'] = $this->resourceType($triple['p']); } else { if ($binding['name'] == 'o') { $o = (string) $binding->uri[0]; if ($o == '') { throw new Exception('Could not extract object from empty string (' . print_r($binding->uri, true) . '), in SPARQLImport'); } $triple['o'] = $o; $triple['o_type'] = $this->resourceType($triple['o']); $triple['o_datatype'] = ''; } } } } $importTriples[] = $triple; } $rdfImporter = new RDFIORDFImporter(); $rdfImporter->importTriples($importTriples); $wgOut->addHTML($rdfImporter->showImportedTriples($importTriples)); } else { RDFIOUtils::formatErrorHTML("Error", "There was a problem importing from the endpoint. Are you sure that the given URL is a valid SPARQL endpoint?"); } return $output = array('externalSparqlUrl' => $externalSparqlUrl); }