Esempio n. 1
0
 /**
  * Import data into wiki pages
  */
 function importData(RDFIORequestData $requestData)
 {
     $rdfImporter = new RDFIORDFImporter();
     if ($requestData->importSource === 'url') {
         if ($requestData->externalRdfUrl === '') {
             throw new RDFIOException('URL field is empty!');
         } else {
             if (!RDFIOUtils::isURI($requestData->externalRdfUrl)) {
                 throw new RDFIOException('Invalid URL provided!');
             }
         }
         $rdfData = file_get_contents($requestData->externalRdfUrl);
     } else {
         if ($requestData->importSource === 'textfield') {
             if ($requestData->importData === '') {
                 throw new RDFIOException('RDF field is empty!');
             }
             $rdfData = $requestData->importData;
         } else {
             throw new RDFIOException('Import source is not selected!');
         }
     }
     switch ($requestData->dataFormat) {
         case 'rdfxml':
             $importInfo = $rdfImporter->importRdfXml($rdfData);
             $triples = $importInfo['triples'];
             break;
         case 'turtle':
             $importInfo = $rdfImporter->importTurtle($rdfData);
             $triples = $importInfo['triples'];
             break;
     }
     return $output = array('triples' => $triples);
 }
Esempio n. 2
0
 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);
 }
 /**
  * Use the namespaces from the RDF / SPARQL source, to shorten the URIs.
  * @param string $uri
  * @param array $nsPrefixes
  * @return string
  */
 function abbreviateParserNSPrefixes($uri, $nsPrefixes)
 {
     foreach ($nsPrefixes as $namespace => $prefix) {
         $nslength = strlen($namespace);
         $basepart = '';
         $localpart = '';
         $uriContainsNamepace = substr($uri, 0, $nslength) === $namespace;
         if ($uriContainsNamepace) {
             $localpart = substr($uri, $nslength);
             $basepart = $prefix;
             break;
         }
     }
     /*
      * Take care of some special cases:
      */
     if ($basepart === '' && $localpart === '') {
         $uriParts = $this->splitURI($uri);
         $basepart = $uriParts[0];
         $localpart = $uriParts[1];
     }
     if ($localpart === '') {
         $abbreviatedUri = $basepart;
     } elseif (RDFIOUtils::isURI($basepart)) {
         // FIXME: Shouldn't the above check the local part instead??
         // Change ARC:s default "random string", to indicate more clearly that
         // it lacks title
         $abbreviatedUri = str_replace('arc', 'untitled', $localpart);
     } elseif (RDFIOUtils::isURI($basepart)) {
         // If the abbreviation does not seem to have succeeded,
         // fall back to use only the local part
         $abbreviatedUri = $localpart;
     } elseif (RDFIOUtils::endsWithColon($basepart)) {
         // Don't add another colon
         $abbreviatedUri = $basepart . $localpart;
     } elseif ($basepart == false || $basepart == '') {
         $abbreviatedUri = $localpart;
     } else {
         $abbreviatedUri = $basepart . ':' . $localpart;
     }
     return $abbreviatedUri;
 }
Esempio n. 4
0
    function showImportedTriples($importedTriples)
    {
        $output = "";
        $style_css = <<<EOD
\t    \t    table .rdfio- th {
\t    \t        font-weight: bold;
\t    \t        padding: 2px 4px;
\t    \t    }
\t    \t    table.rdfio-table td,
\t    \t    table.rdfio-table th {
\t    \t        font-size: 11px;
\t    \t    }
EOD;
        $output .= "<style>{$style_css}</style>";
        //$wgOut->addInlineStyle($style_css);
        $output .= RDFIOUtils::formatSuccessMessageHTML("Success!", "Successfully imported the triples shown below!");
        $output .= "<table class=\"wikitable sortable rdfio-table\"><tbody><tr><th>Subject</th><th>Predicate</th><th>Object</th></tr>";
        foreach ($importedTriples as $triple) {
            $s = $triple['s'];
            $p = $triple['p'];
            $o = $triple['o'];
            if (RDFIOUtils::isURI($s)) {
                $s = "<a href=\"{$s}\">{$s}</a>";
            }
            if (RDFIOUtils::isURI($p)) {
                $p = "<a href=\"{$p}\">{$p}</a>";
            }
            if (RDFIOUtils::isURI($o)) {
                $o = "<a href=\"{$o}\">{$o}</a>";
            }
            $output .= "<tr><td>{$s}</td><td>{$p}</td><td>{$o}</td></tr>";
        }
        $output .= "</tbody></table>";
        return $output;
    }