/** * Returns the property values for all resources at once. * This method generate one SPARQL query to receive all values . * Original Name was getValues() * @return array */ public function getValuesBySingleQuery() { if ($this->_valuesUptodate) { return $this->_values; } else { $this->updateValueQuery(); } if (empty($this->_resources)) { return array(); } $this->getResults(); $result = array(); if (isset($this->_results['results']['bindings'])) { $result = $this->_results['results']['bindings']; } //fill titlehelper foreach ($result as $row) { foreach ($this->_shownProperties as $property) { if (isset($row[$property['varName']]) && $row[$property['varName']]['type'] == 'uri' && substr($row[$property['varName']]['value'], 0, 2) != '_:') { $this->_titleHelper->addResource($row[$property['varName']]['value']); } } if (isset($row['__TYPE']) && $row['__TYPE']['type'] == 'uri') { $this->_titleHelper->addResource($row['__TYPE']['value']); } $this->_titleHelper->addResource($row[$this->_resourceVar->getName()]['value']); } $valueResults = array(); foreach ($result as $row) { $resourceUri = $row[$this->_resourceVar->getName()]['value']; if (!array_key_exists($resourceUri, $valueResults)) { $valueResults[$resourceUri] = array(); } $url = new OntoWiki_Url(array('route' => 'properties'), array('r')); $value = null; $link = null; foreach ($row as $varName => $data) { if (!isset($valueResults[$resourceUri][$varName])) { $valueResults[$resourceUri][$varName] = array(); } if ($data['type'] == 'uri') { if (substr($data['value'], 0, 2) == '_:') { continue; // skip blanknode values here due to backend problems with filters } // object type is uri --> handle object property $objectUri = $data['value']; $url->setParam('r', $objectUri, true); $link = (string) $url; // set up event $event = new Erfurt_Event('onDisplayObjectPropertyValue'); //find uri foreach ($this->_shownProperties as $property) { if ($varName == $property['varName']) { $event->property = $property['uri']; } } $event->value = $objectUri; // trigger $value = $event->trigger(); // set default if event has not been handled if (!$event->handled()) { $value = $this->_titleHelper->getTitle($objectUri); } } else { // object is a literal $object = $data['value']; $propertyUri = null; foreach ($this->_shownProperties as $property) { if ($varName == $property['varName']) { $propertyUri = $property['uri']; } } if ($object !== null) { // set up event $event = new Erfurt_Event('onDisplayLiteralPropertyValue'); $event->property = $propertyUri; $event->value = $object; $event->setDefault($object); // trigger $value = $event->trigger(); } } //check for dulplicate values if (isset($valueResults[$resourceUri][$varName])) { foreach ($valueResults[$resourceUri][$varName] as $old) { if ($old['origvalue'] == $data['value'] && $old['type'] == $data['type']) { $link = null; continue 2; // dont add this value } } } //add value $valueResults[$resourceUri][$varName][] = array('value' => $value, 'origvalue' => $data['value'], 'type' => $data['type'], 'url' => $link, 'uri' => $data['value']); $value = null; $link = null; } } foreach ($this->getShownResources() as $resource) { if (!isset($valueResults[$resource['value']])) { //there are no statements about this resource $valueResults[$resource['value']] = array(); } } $this->_values = $valueResults; $this->_valuesUptodate = true; return $valueResults; }
/** * 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; } }
/** * Builds the query part of the URL */ protected function _buildQuery() { $event = new Erfurt_Event('onBuildUrl'); $event->base = $this->_base; $event->route = $this->_route; $event->controller = $this->_controller; $event->action = $this->_action; $event->params = $this->_params; $urlCreated = $event->trigger(); if ($event->handled()) { if ($urlCreated && isset($event->url)) { return $event->url; } else { $this->_params = $event->params; $this->_controller = $event->controller; $this->_action = $event->action; $this->_route = $event->route; } } // check params foreach ($this->_params as $name => $value) { if (is_string($value) && preg_match('/\\//', $value)) { $this->_useSefUrls = false; } } $url = ''; if ($this->_route) { // checking if reset of route-defaults necessary // fixes pager usage fails on versioning pages if (count($this->_route->getDefaults()) == 0) { $resetRoute = false; } else { $resetRoute = true; } if ($this->_useSefUrls) { // let the route assemble the whole URL $url = $this->_route->assemble($this->_params, $resetRoute, true); } else { // we will assign parameters ourselves $url = $this->_route->assemble(array(), $resetRoute); $url = sprintf('%s/%s', $url, '?' . http_build_query($this->_params, '&')); } } else { if ($this->_useSefUrls) { $query = ''; $lastKey = ''; foreach ($this->_params as $key => $value) { if (is_scalar($value)) { $value = urlencode($value); $query .= "{$key}/{$value}/"; $lastKey = $key; } } // remove trailing slash $query = rtrim($query, '/'); } else { $query = '?' . http_build_query($this->_params, '&'); } $parts = array_filter(array($this->_controller, $this->_action, $query)); $url = implode('/', $parts); } // HACK: $this->_useSefUrls = true; return $this->_base . ltrim($url, '/'); }