public function filterBrowse($p_array)
 {
     $browserFilter = new SMWOntologyBrowserFilter();
     $type = $p_array[0];
     $hint = explode(" ", $p_array[1]);
     $hint = smwfEliminateStopWords($hint);
     if ($type != 'instance') {
         return parent::filterBrowse($p_array);
     }
     global $wgServer, $wgScript, $smwgWebserviceUser, $smwgWebservicePassword, $smwgDeployVersion;
     $client = TSConnection::getConnector();
     $client->connect();
     try {
         global $smwgTripleStoreGraph;
         $dataSpace = $this->getDataSourceParameters();
         //query
         for ($i = 0; $i < count($hint); $i++) {
             $hint[$i] = preg_quote($hint[$i]);
             $hint[$i] = str_replace("\\", "\\\\", $hint[$i]);
         }
         $filter = "";
         if (count($hint) > 0) {
             $filter = "FILTER (";
             for ($i = 0; $i < count($hint); $i++) {
                 if ($i > 0) {
                     $filter .= " && ";
                 }
                 $filter .= "regex(str(?s), \"{$hint[$i]}\", \"i\")";
             }
             $filter .= ")";
         }
         $response = $client->query(TSNamespaces::getW3CPrefixes() . " SELECT ?s ?cat WHERE { ?s ?p ?o. OPTIONAL { ?s rdf:type ?cat. } {$filter} }", "limit=1000{$dataSpace}");
         $titles = array();
         $this->parseInstances($response, $titles);
     } catch (Exception $e) {
         return "Internal error: " . $e->getMessage();
     }
     // do not show partitions. 1000 instances is maximum here.
     return SMWOntologyBrowserXMLGenerator::encapsulateAsInstancePartition($titles, 1001, 0);
 }
예제 #2
0
 function __construct()
 {
     global $smwgTripleStoreGraph, $smwgDefaultStore, $smwgBaseStore, $wgContLang, $wgExtraNamespaces;
     // use initialize flag because PHP classes do not have static initializers.
     if (self::$initialized) {
         return;
     }
     self::$CAT_NS = $smwgTripleStoreGraph . self::$CAT_NS_SUFFIX;
     self::$PROP_NS = $smwgTripleStoreGraph . self::$PROP_NS_SUFFIX;
     self::$INST_NS = $smwgTripleStoreGraph . self::$INST_NS_SUFFIX;
     self::$TYPE_NS = $smwgTripleStoreGraph . self::$TYPE_NS_SUFFIX;
     self::$IMAGE_NS = $smwgTripleStoreGraph . self::$IMAGE_NS_SUFFIX;
     self::$HELP_NS = $smwgTripleStoreGraph . self::$HELP_NS_SUFFIX;
     self::$TEMPLATE_NS = $smwgTripleStoreGraph . self::$TEMPLATE_NS_SUFFIX;
     self::$USER_NS = $smwgTripleStoreGraph . self::$USER_NS_SUFFIX;
     self::$UNKNOWN_NS = $smwgTripleStoreGraph . self::$UNKNOWN_NS_SUFFIX;
     self::$ALL_NAMESPACES = array(NS_MAIN => self::$INST_NS, NS_CATEGORY => self::$CAT_NS, SMW_NS_PROPERTY => self::$PROP_NS, SMW_NS_TYPE => self::$TYPE_NS_SUFFIX, NS_IMAGE => self::$IMAGE_NS, NS_HELP => self::$HELP_NS, NS_TEMPLATE => self::$TEMPLATE_NS, NS_USER => self::$USER_NS);
     // declare all common namespaces as SPARQL PREFIX statement (W3C + standard wiki + SMW)
     self::$ALL_PREFIXES = 'PREFIX xsd:<' . self::$XSD_NS . '> PREFIX owl:<' . self::$OWL_NS . '> PREFIX rdfs:<' . self::$RDFS_NS . '> PREFIX rdf:<' . self::$RDF_NS . '> PREFIX cat:<' . self::$CAT_NS . '> PREFIX prop:<' . self::$PROP_NS . '> PREFIX a:<' . self::$INST_NS . '> PREFIX type:<' . self::$TYPE_NS . '> PREFIX image:<' . self::$IMAGE_NS . '> PREFIX help:<' . self::$HELP_NS . '> PREFIX template:<' . self::$TEMPLATE_NS . '> PREFIX user: <' . self::$USER_NS . '> ';
     self::$W3C_PREFIXES = 'PREFIX xsd:<' . self::$XSD_NS . '> PREFIX owl:<' . self::$OWL_NS . '> PREFIX rdfs:<' . self::$RDFS_NS . '> PREFIX rdf:<' . self::$RDF_NS . '> ';
     // declare all other namespaces using ns_$index as prefix
     $extraNamespaces = array_diff(array_keys($wgExtraNamespaces), array(NS_CATEGORY, SMW_NS_PROPERTY, SMW_NS_TYPE, NS_IMAGE, NS_HELP, NS_MAIN));
     foreach ($extraNamespaces as $nsIndex) {
         $nsText = strtolower($wgContLang->getNsText($nsIndex));
         self::$ALL_PREFIXES .= " PREFIX {$nsText}:<" . $smwgTripleStoreGraph . "/ns_{$nsIndex}#> ";
     }
 }
예제 #3
0
 /**
  * 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.");
     }
 }
예제 #4
0
 /**
  * Gets an array of tuples (URI, metadata-uri) and creates SMWWikiPageValue objects.
  *
  * @param array of tuples (uri, hash array metadata) $uris
  * @param array SMWDataValue (out) & $allValues
  */
 protected function addURIToResult($uris, &$allValues)
 {
     foreach ($uris as $uri) {
         list($sv, $metadata) = $uri;
         // check if common namespace from MW or SMW
         // and create SMWDataValue accoringly
         $nsFound = false;
         foreach (TSNamespaces::getAllNamespaces() as $nsIndsex => $ns) {
             if (stripos($sv, $ns) === 0) {
                 $allValues[] = $this->createSMWDataValue($sv, $metadata, $ns, $nsIndsex);
                 $nsFound = true;
             }
         }
         if ($nsFound) {
             continue;
         }
         // result with unknown namespace
         // unknown means the namespace has a suffix: /ns_<index>#
         // where <index> is the namespace index.
         if (stripos($sv, TSNamespaces::$UNKNOWN_NS) === 0) {
             if (empty($sv)) {
                 $v = SMWDataValueFactory::newTypeIDValue('_wpg');
                 foreach ($metadata as $mdProperty => $mdValue) {
                     if (strpos($mdProperty, "_meta_") === 0) {
                         $v->setMetadata(substr($mdProperty, 6), explode("|||", $mdValue));
                     }
                 }
                 $allValues[] = $v;
             } else {
                 $startNS = strlen(TSNamespaces::$UNKNOWN_NS);
                 $length = strpos($sv, "#") - $startNS;
                 $ns = intval(substr($sv, $startNS, $length));
                 $local = substr($sv, strpos($sv, "#") + 1);
                 $title = Title::newFromText($local, $ns);
                 if (is_null($title)) {
                     $title = Title::newFromText(wfMsg('smw_ob_invalidtitle'), $ns);
                 }
                 $v = SMWDataValueFactory::newTypeIDValue('_wpg');
                 $v->setValues($title->getDBkey(), $ns, $title->getArticleID());
                 foreach ($metadata as $mdProperty => $mdValue) {
                     if (strpos($mdProperty, "_meta_") === 0) {
                         $v->setMetadata(substr($mdProperty, 6), explode("|||", $mdValue));
                     }
                 }
                 $allValues[] = $v;
             }
         } else {
             // external URI
             $v = SMWDataValueFactory::newTypeIDValue('_uri');
             $v->setDBkeys(array($sv));
             foreach ($metadata as $mdProperty => $mdValue) {
                 if (strpos($mdProperty, "_meta_") === 0) {
                     $v->setMetadata(substr($mdProperty, 6), explode("|||", $mdValue));
                 }
             }
             $allValues[] = $v;
         }
     }
 }
예제 #5
0
 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');
     }
 }
예제 #6
0
 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 .= "&params=" . urlencode($params);
     list($header, $status, $result) = $this->queryClient->send($queryRequest);
     if ($status != 200) {
         throw new Exception(strip_tags($result), $status);
     }
     return $result;
 }
 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);
 }