public function testBuildLiteralString() { $literals = array(array('"false"^^<http://www.w3.org/2001/XMLSchema#boolean>', 'false', 'http://www.w3.org/2001/XMLSchema#boolean', null, true), array('"false"^^<http://www.w3.org/2001/XMLSchema#boolean>', '0', 'http://www.w3.org/2001/XMLSchema#boolean', null, true), array('"false"^^<http://www.w3.org/2001/XMLSchema#boolean>', false, 'http://www.w3.org/2001/XMLSchema#boolean', null, true), array('"true"^^<http://www.w3.org/2001/XMLSchema#boolean>', 'true', 'http://www.w3.org/2001/XMLSchema#boolean', null, true), array('"true"^^<http://www.w3.org/2001/XMLSchema#boolean>', '1', 'http://www.w3.org/2001/XMLSchema#boolean', null, true), array('"true"^^<http://www.w3.org/2001/XMLSchema#boolean>', true, 'http://www.w3.org/2001/XMLSchema#boolean', null, true), array('"123456789"^^<http://www.w3.org/2001/XMLSchema#int>', '123456789', 'http://www.w3.org/2001/XMLSchema#int', null, true), array('"1234.56789"^^<http://www.w3.org/2001/XMLSchema#float>', '1234.56789', 'http://www.w3.org/2001/XMLSchema#float', null, true), array('"Hallo"@de', 'Hallo', null, 'de', true), array('"Hello"@en-US', 'Hello', null, 'en-US', true), array('"Mit Backslash \\\\ im Text"^^<http://www.w3.org/2001/XMLSchema#string>', 'Mit Backslash \\ im Text', 'http://www.w3.org/2001/XMLSchema#string', null, true), array("\"\"\"This\nis\na\nmulti\nline\ntext\"\"\"^^<http://www.w3.org/2001/XMLSchema#string>", "This\nis\na\nmulti\nline\ntext", 'http://www.w3.org/2001/XMLSchema#string', null, true), array("\"\"\"This\nis\na\nmulti\nline\ntext with\\\\backslash\"\"\"^^<http://www.w3.org/2001/XMLSchema#string>", "This\nis\na\nmulti\nline\ntext with\\backslash", 'http://www.w3.org/2001/XMLSchema#string', null, true), array("'''This\nis\na\n\"\"\"multi\nline\"\"\"\ntext'''^^<http://www.w3.org/2001/XMLSchema#string>", "This\nis\na\n\"\"\"multi\nline\"\"\"\ntext", 'http://www.w3.org/2001/XMLSchema#string', null, true), array('"This\\nis\\na\\nmulti\\nline\\ntext"^^<http://www.w3.org/2001/XMLSchema#string>', "This\nis\na\nmulti\nline\ntext", 'http://www.w3.org/2001/XMLSchema#string', null, false), array('"Literal with\\tTab"^^<http://www.w3.org/2001/XMLSchema#string>', "Literal with\tTab", 'http://www.w3.org/2001/XMLSchema#string', null, true)); foreach ($literals as $literalDesc) { $literal = $literalDesc[0]; $value = $literalDesc[1]; $datatype = $literalDesc[2]; $lang = $literalDesc[3]; $longString = $literalDesc[4]; $this->assertEquals($literal, Erfurt_Utils::buildLiteralString($value, $datatype, $lang, $longString)); } }
/** * Returns an array of predicate values for the current resource. * The array is indexed with the predicate's URIs. * * @return array */ public function getValues() { if (null === $this->_valueResults) { $this->_valueResults = array(); // get results $results = $this->getQueryResults(); // load predicates first $this->getPredicates(); // URL object to build URLs $url = new OntoWiki_Url(array('route' => 'properties'), array('r')); // keep track of URI objects already used $objects = array(); foreach ($results as $graph => $resultsForGraph) { $this->_valueResults[$graph] = array(); foreach ($resultsForGraph as $row) { $predicateUri = $row['predicate']['value']; if (!array_key_exists($predicateUri, $objects)) { $objects[$predicateUri] = array(); } // create space for value information if not exists if (!array_key_exists($predicateUri, $this->_valueResults[$graph])) { $this->_valueResults[$graph][$predicateUri] = array(); } // default values $value = array('content' => null, 'object' => null, 'object_hash' => null, 'datatype' => null, 'lang' => null, 'url' => null, 'uri' => null, 'curi' => null); switch ($row['object']['type']) { case 'uri': // every URI objects is only used once for each statement if (in_array($row['object']['value'], $objects[$predicateUri])) { continue; } // URL $url->setParam('r', $row['object']['value'], true); $value['url'] = (string) $url; // URI $value['uri'] = $row['object']['value']; // title $title = $this->_titleHelper->getTitle($row['object']['value']); /** * @trigger onDisplayObjectPropertyValue Triggered if an object value of some * property is returned. Plugins can attach to this trigger in order to modify * the value that gets displayed. * Event payload: value, property, title and link */ // set up event $event = new Erfurt_Event('onDisplayObjectPropertyValue'); $event->value = $row['object']['value']; $event->property = $predicateUri; $event->title = $title; $event->link = (string) $url; // trigger $value['object'] = $event->trigger(); if (!$event->handled()) { // object (modified by plug-ins) $value['object'] = $title; } array_push($objects[$predicateUri], $row['object']['value']); break; case 'typed-literal': $event = new Erfurt_Event('onDisplayLiteralPropertyValue'); $value['datatype'] = OntoWiki_Utils::compactUri($row['object']['datatype']); $literalString = Erfurt_Utils::buildLiteralString($row['object']['value'], $row['object']['datatype']); $value['object_hash'] = md5($literalString); $event->value = $row['object']['value']; $event->datatype = $row['object']['datatype']; $event->property = $predicateUri; $value['object'] = $event->trigger(); // keep unmodified value in content $value['content'] = $row['object']['value']; if (!$event->handled()) { // object (modified by plug-ins) $value['object'] = $row['object']['value']; } break; case 'literal': // original (unmodified) for RDFa $value['content'] = $row['object']['value']; $literalString = Erfurt_Utils::buildLiteralString($row['object']['value'], null, isset($row['object']['xml:lang']) ? $row['object']['xml:lang'] : null); $value['object_hash'] = md5($literalString); /** * @trigger onDisplayLiteralPropertyValue Triggered if a literal value of some * property is returned. Plugins can attach to this trigger in order to modify * the value that gets displayed. */ $event = new Erfurt_Event('onDisplayLiteralPropertyValue'); $event->value = $row['object']['value']; $event->property = $predicateUri; // set literal language if (isset($row['object']['xml:lang'])) { $value['lang'] = $row['object']['xml:lang']; $event->language = $row['object']['xml:lang']; } // trigger $value['object'] = $event->trigger(); // keep unmodified value in content $value['content'] = $row['object']['value']; // set default if event has not been handled if (!$event->handled()) { $value['object'] = $row['object']['value']; } break; } // push it only if it doesn't exceed number of items to display if (count($this->_valueResults[$graph][$predicateUri]) < $this->_limit) { array_push($this->_valueResults[$graph][$predicateUri], $value); } else { $this->_predicateResults[$graph][$predicateUri]['has_more'] = true; } if (count($this->_valueResults[$graph][$predicateUri]) > 1) { // create the "has more link" (used for area context menu as well) // do it only once per predicate if (!isset($this->_predicateResults[$graph][$predicateUri]['has_more_link'])) { //when all values are literal, we dont use a link to the list,but to the query editor $allValuesAreLiterals = true; foreach ($this->_valueResults[$graph][$predicateUri] as $value) { if (isset($value['uri'])) { $allValuesAreLiterals = false; } } if (!$allValuesAreLiterals) { $hasMoreUrl = new OntoWiki_Url(array('route' => 'instances', 'action' => 'list'), array()); $filterExp = json_encode(array('filter' => array(array('action' => 'add', 'mode' => 'box', 'id' => 'allvalues', 'property' => $predicateUri, 'isInverse' => true, 'propertyLabel' => "value", 'filter' => 'equals', 'value1' => $this->_uri, 'value2' => null, 'valuetype' => 'uri', 'literaltype' => null, 'hidden' => false)))); $hasMoreUrl->setParam('instancesconfig', $filterExp)->setParam('init', true); } else { $hasMoreUrl = new OntoWiki_Url(array('controller' => 'queries', 'action' => 'editor'), array()); $hasMoreUrl->setParam('query', 'SELECT ?value WHERE {<' . $this->_uri . '> <' . $predicateUri . '> ?value}')->setParam('immediate', true); } $this->_predicateResults[$graph][$predicateUri]['has_more_link'] = (string) $hasMoreUrl; } } } } return $this->_valueResults; } }
protected function _findStatementsForObjectsWithHashes($graphUri, $indexWithHashedObjects, $hashFunc = 'md5') { $queryOptions = array('result_format' => 'extended'); $result = array(); foreach ($indexWithHashedObjects as $subject => $predicates) { foreach ($predicates as $predicate => $hashedObjects) { $query = "SELECT ?o FROM <{$graphUri}> WHERE {<{$subject}> <{$predicate}> ?o .}"; $queryObj = Erfurt_Sparql_SimpleQuery::initWithString($query); if ($queryResult = $this->_owApp->erfurt->getStore()->sparqlQuery($queryObj, $queryOptions)) { $bindings = $queryResult['results']['bindings']; for ($i = 0, $max = count($bindings); $i < $max; $i++) { $currentObject = $bindings[$i]['o']; $objectString = Erfurt_Utils::buildLiteralString($currentObject['value'], isset($currentObject['datatype']) ? $currentObject['datatype'] : null, isset($currentObject['xml:lang']) ? $currentObject['xml:lang'] : null); $hash = $hashFunc($objectString); if (in_array($hash, $hashedObjects)) { // add current statement to result if (!isset($result[$subject])) { $result[$subject] = array(); } if (!isset($result[$subject][$predicate])) { $result[$subject][$predicate] = array(); } $objectSpec = array('value' => $currentObject['value'], 'type' => str_replace('typed-', '', $currentObject['type'])); if (isset($currentObject['datatype'])) { $objectSpec['datatype'] = $currentObject['datatype']; } else { if (isset($currentObject['xml:lang'])) { $objectSpec['lang'] = $currentObject['xml:lang']; } } array_push($result[$subject][$predicate], $objectSpec); } } } } } return $result; }
/** * Builds a SPARQL-compatible literal string with long literals if necessary. * * @param string $value * @param string|null $datatype * @param string|null $lang * @param boolean $longStringEnabled decides if the output can be a long string (""" """) or not * @return string */ public function buildLiteralString($value, $datatype = null, $lang = null, $longStringEnabled = true) { // This is a Virtuoso commercial edition feature... If we are running on open-source version, we remove the // datatype, since otherwise loading will fail. if ($this->_isOpenSourceVersion) { if ($datatype === 'http://www.openlinksw.com/schemas/virtrdf#Geometry') { $datatype = null; } } return Erfurt_Utils::buildLiteralString($value, $datatype, $lang, $longStringEnabled); }