/** * The main code goes here */ function execute($par) { global $wgOut; try { # Set HTML headers sent to the browser $this->setHeaders(); # The main code $requestData = $this->getRequestData(); if ($requestData->hasWriteAccess && $requestData->action === 'import') { $importInfo = $this->importData($requestData); $triples = $importInfo['triples']; if ($triples) { $rdfImporter = new RDFIORDFImporter(); $wgOut->addHTML($this->getHTMLForm($requestData)); $wgOut->addHTML($rdfImporter->showImportedTriples($triples)); if ($requestData->externalRdfUrl) { $rdfImporter->addDataSource($requestData->externalRdfUrl, 'RDF'); } } else { if (!$triples) { throw new RDFIOException("No new triples to import"); } } } else { if (!$requestData->hasWriteAccess) { throw new RDFIOException("User does not have write access"); } else { $wgOut->addHTML($this->getHTMLForm($requestData)); $wgOut->addHTML('<div id=sources style="display:none">'); $wgOut->addWikiText('{{#ask: [[Category:RDFIO Data Source]] [[RDFIO Import Type::RDF]] |format=list }}'); $wgOut->addHTML('</div>'); } } } catch (MWException $e) { RDFIOUtils::showErrorMessage('Error!', $e->getMessage()); } }
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); }