Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like GenericSparql often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GenericSparql, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class GenericSparql { |
||
| 7 | /** |
||
| 8 | * A SPARQL Client eg. an EasyRDF instance. |
||
| 9 | * @property EasyRdf\Sparql\Client $client |
||
| 10 | */ |
||
| 11 | protected $client; |
||
| 12 | /** |
||
| 13 | * Graph uri. |
||
| 14 | * @property object $graph |
||
| 15 | */ |
||
| 16 | protected $graph; |
||
| 17 | /** |
||
| 18 | * A SPARQL query graph part template. |
||
| 19 | * @property string $graphClause |
||
| 20 | */ |
||
| 21 | protected $graphClause; |
||
| 22 | /** |
||
| 23 | * Model instance. |
||
| 24 | * @property Model $model |
||
| 25 | */ |
||
| 26 | protected $model; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Cache used to avoid expensive shorten() calls |
||
| 30 | * @property array $qnamecache |
||
| 31 | */ |
||
| 32 | private $qnamecache = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Requires the following three parameters. |
||
| 36 | * @param string $endpoint SPARQL endpoint address. |
||
| 37 | * @param string|null $graph Which graph to query: Either an URI, the special value "?graph" |
||
| 38 | * to use the default graph, or NULL to not use a GRAPH clause. |
||
| 39 | * @param object $model a Model instance. |
||
| 40 | */ |
||
| 41 | public function __construct($endpoint, $graph, $model) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns prefix-definitions for a query |
||
| 65 | * |
||
| 66 | * @param string $query |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | protected function generateQueryPrefixes($query) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Execute the SPARQL query using the SPARQL client, logging it as well. |
||
| 85 | * @param string $query SPARQL query to perform |
||
| 86 | * @return \EasyRdf\Sparql\Result|\EasyRdf\Graph query result |
||
| 87 | */ |
||
| 88 | protected function query($query) { |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Generates FROM clauses for the queries |
||
| 108 | * @param Vocabulary[]|null $vocabs |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | protected function generateFromClause($vocabs=null) { |
||
| 122 | protected function initializeHttpClient() { |
||
| 123 | // configure the HTTP client used by EasyRdf\Sparql\Client |
||
| 124 | $httpclient = EasyRdf\Http::getDefaultHttpClient(); |
||
| 125 | $httpclient->setConfig(array('timeout' => $this->model->getConfig()->getSparqlTimeout())); |
||
| 126 | |||
| 127 | // if special cache control (typically no-cache) was requested by the |
||
| 128 | // client, set the same type of cache control headers also in subsequent |
||
| 129 | // in the SPARQL requests (this is useful for performance testing) |
||
| 130 | // @codeCoverageIgnoreStart |
||
| 131 | $cacheControl = filter_input(INPUT_SERVER, 'HTTP_CACHE_CONTROL', FILTER_SANITIZE_STRING); |
||
| 132 | $pragma = filter_input(INPUT_SERVER, 'HTTP_PRAGMA', FILTER_SANITIZE_STRING); |
||
| 133 | if ($cacheControl !== null || $pragma !== null) { |
||
| 134 | $val = $pragma !== null ? $pragma : $cacheControl; |
||
| 135 | $httpclient->setHeaders('Cache-Control', $val); |
||
| 136 | } |
||
| 137 | // @codeCoverageIgnoreEnd |
||
| 138 | |||
| 139 | EasyRdf\Http::setDefaultHttpClient($httpclient); // actually redundant.. |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Return true if this is the default SPARQL endpoint, used as the facade to query |
||
| 144 | * all vocabularies. |
||
| 145 | */ |
||
| 146 | |||
| 147 | protected function isDefaultEndpoint() { |
||
| 148 | return !is_null($this->graph) && $this->graph[0] == '?'; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Returns the graph instance |
||
| 153 | * @return object EasyRDF graph instance. |
||
| 154 | */ |
||
| 155 | public function getGraph() { |
||
| 156 | return $this->graph; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Shorten a URI |
||
| 161 | * @param string $uri URI to shorten |
||
| 162 | * @return string shortened URI, or original URI if it cannot be shortened |
||
| 163 | */ |
||
| 164 | private function shortenUri($uri) { |
||
| 165 | if (!array_key_exists($uri, $this->qnamecache)) { |
||
| 166 | $res = new EasyRdf\Resource($uri); |
||
| 167 | $qname = $res->shorten(); // returns null on failure |
||
| 168 | $this->qnamecache[$uri] = ($qname !== null) ? $qname : $uri; |
||
| 169 | } |
||
| 170 | return $this->qnamecache[$uri]; |
||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * Generates the sparql query for retrieving concept and collection counts in a vocabulary. |
||
| 176 | * @return string sparql query |
||
| 177 | */ |
||
| 178 | private function generateCountConceptsQuery($array, $group) { |
||
| 179 | $fcl = $this->generateFromClause(); |
||
| 180 | $optional = $array ? "UNION { ?type rdfs:subClassOf* <$array> }" : ''; |
||
| 181 | $optional .= $group ? "UNION { ?type rdfs:subClassOf* <$group> }" : ''; |
||
| 182 | $query = <<<EOQ |
||
| 183 | SELECT (COUNT(?conc) as ?c) ?type ?typelabel $fcl WHERE { |
||
| 184 | { ?conc a ?type . |
||
| 185 | { ?type rdfs:subClassOf* skos:Concept . } UNION { ?type rdfs:subClassOf* skos:Collection . } $optional } |
||
| 186 | OPTIONAL { ?type rdfs:label ?typelabel . } |
||
| 187 | } |
||
| 188 | GROUP BY ?type ?typelabel |
||
| 189 | EOQ; |
||
| 190 | return $query; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Used for transforming the concept count query results. |
||
| 195 | * @param EasyRdf\Sparql\Result $result query results to be transformed |
||
| 196 | * @param string $lang language of labels |
||
| 197 | * @return Array containing the label counts |
||
| 198 | */ |
||
| 199 | private function transformCountConceptsResults($result, $lang) { |
||
| 200 | $ret = array(); |
||
| 201 | foreach ($result as $row) { |
||
| 202 | if (!isset($row->type)) { |
||
| 203 | continue; |
||
| 204 | } |
||
| 205 | $ret[$row->type->getUri()]['type'] = $row->type->getUri(); |
||
| 206 | $ret[$row->type->getUri()]['count'] = $row->c->getValue(); |
||
| 207 | if (isset($row->typelabel) && $row->typelabel->getLang() === $lang) { |
||
| 208 | $ret[$row->type->getUri()]['label'] = $row->typelabel->getValue(); |
||
| 209 | } |
||
| 210 | |||
| 211 | } |
||
| 212 | return $ret; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Used for counting number of concepts and collections in a vocabulary. |
||
| 217 | * @param string $lang language of labels |
||
| 218 | * @return array with number of concepts in this vocabulary per label |
||
| 219 | */ |
||
| 220 | public function countConcepts($lang = null, $array = null, $group = null) { |
||
| 221 | $query = $this->generateCountConceptsQuery($array, $group); |
||
| 222 | $result = $this->query($query); |
||
| 223 | return $this->transformCountConceptsResults($result, $lang); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param array $langs Languages to query for |
||
| 228 | * @param string[] $props property names |
||
| 229 | * @return string sparql query |
||
| 230 | */ |
||
| 231 | private function generateCountLangConceptsQuery($langs, $classes, $props) { |
||
| 232 | $gcl = $this->graphClause; |
||
| 233 | $classes = ($classes) ? $classes : array('http://www.w3.org/2004/02/skos/core#Concept'); |
||
| 234 | |||
| 235 | $quote_string = function($val) { return "'$val'"; }; |
||
| 236 | $quoted_values = array_map($quote_string, $langs); |
||
| 237 | $langFilter = "FILTER(?lang IN (" . implode(',', $quoted_values) . "))"; |
||
| 238 | |||
| 239 | $values = $this->formatValues('?type', $classes, 'uri'); |
||
| 240 | $valuesProp = $this->formatValues('?prop', $props, null); |
||
| 241 | |||
| 242 | $query = <<<EOQ |
||
| 243 | SELECT ?lang ?prop |
||
| 244 | (COUNT(?label) as ?count) |
||
| 245 | WHERE { |
||
| 246 | $gcl { |
||
| 247 | $values |
||
| 248 | $valuesProp |
||
| 249 | ?conc a ?type . |
||
| 250 | ?conc ?prop ?label . |
||
| 251 | BIND(LANG(?label) AS ?lang) |
||
| 252 | $langFilter |
||
| 253 | } |
||
| 254 | } |
||
| 255 | GROUP BY ?lang ?prop ?type |
||
| 256 | EOQ; |
||
| 257 | return $query; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Transforms the CountLangConcepts results into an array of label counts. |
||
| 262 | * @param EasyRdf\Sparql\Result $result query results to be transformed |
||
| 263 | * @param array $langs Languages to query for |
||
| 264 | * @param string[] $props property names |
||
| 265 | */ |
||
| 266 | private function transformCountLangConceptsResults($result, $langs, $props) { |
||
| 267 | $ret = array(); |
||
| 268 | // set default count to zero; overridden below if query found labels |
||
| 269 | foreach ($langs as $lang) { |
||
| 270 | foreach ($props as $prop) { |
||
| 271 | $ret[$lang][$prop] = 0; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | foreach ($result as $row) { |
||
| 275 | if (isset($row->lang) && isset($row->prop) && isset($row->count)) { |
||
| 276 | $ret[$row->lang->getValue()][$row->prop->shorten()] += |
||
| 277 | $row->count->getValue(); |
||
| 278 | } |
||
| 279 | |||
| 280 | } |
||
| 281 | ksort($ret); |
||
| 282 | return $ret; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Counts the number of concepts in a easyRDF graph with a specific language. |
||
| 287 | * @param array $langs Languages to query for |
||
| 288 | * @return Array containing count of concepts for each language and property. |
||
| 289 | */ |
||
| 290 | public function countLangConcepts($langs, $classes = null) { |
||
| 291 | $props = array('skos:prefLabel', 'skos:altLabel', 'skos:hiddenLabel'); |
||
| 292 | $query = $this->generateCountLangConceptsQuery($langs, $classes, $props); |
||
| 293 | // Count the number of terms in each language |
||
| 294 | $result = $this->query($query); |
||
| 295 | return $this->transformCountLangConceptsResults($result, $langs, $props); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Formats a VALUES clause (SPARQL 1.1) which states that the variable should be bound to one |
||
| 300 | * of the constants given. |
||
| 301 | * @param string $varname variable name, e.g. "?uri" |
||
| 302 | * @param array $values the values |
||
| 303 | * @param string $type type of values: "uri", "literal" or null (determines quoting style) |
||
| 304 | */ |
||
| 305 | protected function formatValues($varname, $values, $type = null) { |
||
| 306 | $constants = array(); |
||
| 307 | foreach ($values as $val) { |
||
| 308 | if ($type == 'uri') { |
||
| 309 | $val = "<$val>"; |
||
| 310 | } |
||
| 311 | |||
| 312 | if ($type == 'literal') { |
||
| 313 | $val = "'$val'"; |
||
| 314 | } |
||
| 315 | |||
| 316 | $constants[] = "($val)"; |
||
| 317 | } |
||
| 318 | $values = implode(" ", $constants); |
||
| 319 | |||
| 320 | return "VALUES ($varname) { $values }"; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Filters multiple instances of the same vocabulary from the input array. |
||
| 325 | * @param \Vocabulary[]|null $vocabs array of Vocabulary objects |
||
| 326 | * @return \Vocabulary[] |
||
| 327 | */ |
||
| 328 | private function filterDuplicateVocabs($vocabs) { |
||
| 329 | // filtering duplicates |
||
| 330 | $uniqueVocabs = array(); |
||
| 331 | if ($vocabs !== null && sizeof($vocabs) > 0) { |
||
| 332 | foreach ($vocabs as $voc) { |
||
| 333 | $uniqueVocabs[$voc->getId()] = $voc; |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | return $uniqueVocabs; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Generates a sparql query for one or more concept URIs |
||
| 342 | * @param mixed $uris concept URI (string) or array of URIs |
||
| 343 | * @param string|null $arrayClass the URI for thesaurus array class, or null if not used |
||
| 344 | * @param \Vocabulary[]|null $vocabs array of Vocabulary objects |
||
| 345 | * @return string sparql query |
||
| 346 | */ |
||
| 347 | private function generateConceptInfoQuery($uris, $arrayClass, $vocabs) { |
||
| 348 | $gcl = $this->graphClause; |
||
| 349 | $fcl = empty($vocabs) ? '' : $this->generateFromClause($vocabs); |
||
| 350 | $values = $this->formatValues('?uri', $uris, 'uri'); |
||
| 351 | $uniqueVocabs = $this->filterDuplicateVocabs($vocabs); |
||
| 352 | $valuesGraph = empty($vocabs) ? $this->formatValuesGraph($uniqueVocabs) : ''; |
||
| 353 | |||
| 354 | if ($arrayClass === null) { |
||
| 355 | $construct = $optional = ""; |
||
| 356 | } else { |
||
| 357 | // add information that can be used to format narrower concepts by |
||
| 358 | // the array they belong to ("milk by source animal" use case) |
||
| 359 | $construct = "\n ?x skos:member ?o . ?x skos:prefLabel ?xl . ?x a <$arrayClass> ."; |
||
| 360 | $optional = "\n OPTIONAL { |
||
| 361 | ?x skos:member ?o . |
||
| 362 | ?x a <$arrayClass> . |
||
| 363 | ?x skos:prefLabel ?xl . |
||
| 364 | FILTER NOT EXISTS { |
||
| 365 | ?x skos:member ?other . |
||
| 366 | MINUS { ?other skos:broader ?uri } |
||
| 367 | } |
||
| 368 | }"; |
||
| 369 | } |
||
| 370 | $query = <<<EOQ |
||
| 371 | CONSTRUCT { |
||
| 372 | ?s ?p ?uri . |
||
| 373 | ?sp ?uri ?op . |
||
| 374 | ?uri ?p ?o . |
||
| 375 | ?p rdfs:label ?proplabel . |
||
| 376 | ?p rdfs:comment ?propcomm . |
||
| 377 | ?p skos:definition ?propdef . |
||
| 378 | ?p rdfs:subPropertyOf ?pp . |
||
| 379 | ?pp rdfs:label ?plabel . |
||
| 380 | ?o a ?ot . |
||
| 381 | ?o skos:prefLabel ?opl . |
||
| 382 | ?o rdfs:label ?ol . |
||
| 383 | ?o rdf:value ?ov . |
||
| 384 | ?o skos:notation ?on . |
||
| 385 | ?o ?oprop ?oval . |
||
| 386 | ?o ?xlprop ?xlval . |
||
| 387 | ?directgroup skos:member ?uri . |
||
| 388 | ?parent skos:member ?group . |
||
| 389 | ?group skos:prefLabel ?grouplabel . |
||
| 390 | ?b1 rdf:first ?item . |
||
| 391 | ?b1 rdf:rest ?b2 . |
||
| 392 | ?item a ?it . |
||
| 393 | ?item skos:prefLabel ?il . |
||
| 394 | ?group a ?grouptype . $construct |
||
| 395 | } $fcl WHERE { |
||
| 396 | $values |
||
| 397 | $gcl { |
||
| 398 | { |
||
| 399 | ?s ?p ?uri . |
||
| 400 | FILTER(!isBlank(?s)) |
||
| 401 | FILTER(?p != skos:inScheme) |
||
| 402 | FILTER NOT EXISTS { ?s owl:deprecated true . } |
||
| 403 | } |
||
| 404 | UNION |
||
| 405 | { ?sp ?uri ?op . } |
||
| 406 | UNION |
||
| 407 | { |
||
| 408 | ?directgroup skos:member ?uri . |
||
| 409 | ?group skos:member+ ?uri . |
||
| 410 | ?group skos:prefLabel ?grouplabel . |
||
| 411 | ?group a ?grouptype . |
||
| 412 | OPTIONAL { ?parent skos:member ?group } |
||
| 413 | } |
||
| 414 | UNION |
||
| 415 | { |
||
| 416 | ?uri ?p ?o . |
||
| 417 | OPTIONAL { |
||
| 418 | ?o rdf:rest* ?b1 . |
||
| 419 | ?b1 rdf:first ?item . |
||
| 420 | ?b1 rdf:rest ?b2 . |
||
| 421 | OPTIONAL { ?item a ?it . } |
||
| 422 | OPTIONAL { ?item skos:prefLabel ?il . } |
||
| 423 | } |
||
| 424 | OPTIONAL { |
||
| 425 | { ?p rdfs:label ?proplabel . } |
||
| 426 | UNION |
||
| 427 | { ?p rdfs:comment ?propcomm . } |
||
| 428 | UNION |
||
| 429 | { ?p skos:definition ?propdef . } |
||
| 430 | UNION |
||
| 431 | { ?p rdfs:subPropertyOf ?pp . } |
||
| 432 | } |
||
| 433 | OPTIONAL { |
||
| 434 | { ?o a ?ot . } |
||
| 435 | UNION |
||
| 436 | { ?o skos:prefLabel ?opl . } |
||
| 437 | UNION |
||
| 438 | { ?o rdfs:label ?ol . } |
||
| 439 | UNION |
||
| 440 | { ?o rdf:value ?ov . |
||
| 441 | OPTIONAL { ?o ?oprop ?oval . } |
||
| 442 | } |
||
| 443 | UNION |
||
| 444 | { ?o skos:notation ?on . } |
||
| 445 | UNION |
||
| 446 | { ?o a skosxl:Label . |
||
| 447 | ?o ?xlprop ?xlval } |
||
| 448 | } $optional |
||
| 449 | } |
||
| 450 | } |
||
| 451 | } |
||
| 452 | $valuesGraph |
||
| 453 | EOQ; |
||
| 454 | return $query; |
||
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Transforms ConceptInfo query results into an array of Concept objects |
||
| 459 | * @param EasyRdf\Graph $result query results to be transformed |
||
| 460 | * @param array $uris concept URIs |
||
| 461 | * @param \Vocabulary[] $vocabs array of Vocabulary object |
||
| 462 | * @param string|null $clang content language |
||
| 463 | * @return Concept[] array of Concept objects |
||
| 464 | */ |
||
| 465 | private function transformConceptInfoResults($result, $uris, $vocabs, $clang) { |
||
| 466 | $conceptArray = array(); |
||
| 467 | foreach ($uris as $index => $uri) { |
||
| 468 | $conc = $result->resource($uri); |
||
| 469 | if (is_array($vocabs)) { |
||
| 470 | $vocab = (sizeof($vocabs) == 1) ? $vocabs[0] : $vocabs[$index]; |
||
| 471 | } else { |
||
| 472 | $vocab = null; |
||
| 473 | } |
||
| 474 | $conceptArray[] = new Concept($this->model, $vocab, $conc, $result, $clang); |
||
| 475 | } |
||
| 476 | return $conceptArray; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Returns information (as a graph) for one or more concept URIs |
||
| 481 | * @param mixed $uris concept URI (string) or array of URIs |
||
| 482 | * @param string|null $arrayClass the URI for thesaurus array class, or null if not used |
||
| 483 | * @param \Vocabulary[]|null $vocabs vocabularies to target |
||
| 484 | * @return \EasyRdf\Graph |
||
| 485 | */ |
||
| 486 | public function queryConceptInfoGraph($uris, $arrayClass = null, $vocabs = array()) { |
||
| 487 | // if just a single URI is given, put it in an array regardless |
||
| 488 | if (!is_array($uris)) { |
||
| 489 | $uris = array($uris); |
||
| 490 | } |
||
| 491 | |||
| 492 | $query = $this->generateConceptInfoQuery($uris, $arrayClass, $vocabs); |
||
| 493 | $result = $this->query($query); |
||
| 494 | return $result; |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Returns information (as an array of Concept objects) for one or more concept URIs |
||
| 499 | * @param mixed $uris concept URI (string) or array of URIs |
||
| 500 | * @param string|null $arrayClass the URI for thesaurus array class, or null if not used |
||
| 501 | * @param \Vocabulary[] $vocabs vocabularies to target |
||
| 502 | * @param string|null $clang content language |
||
| 503 | * @return Concept[] |
||
| 504 | */ |
||
| 505 | public function queryConceptInfo($uris, $arrayClass = null, $vocabs = array(), $clang = null) { |
||
| 506 | // if just a single URI is given, put it in an array regardless |
||
| 507 | if (!is_array($uris)) { |
||
| 508 | $uris = array($uris); |
||
| 509 | } |
||
| 510 | $result = $this->queryConceptInfoGraph($uris, $arrayClass, $vocabs); |
||
| 511 | if ($result->isEmpty()) { |
||
| 512 | return []; |
||
| 513 | } |
||
| 514 | return $this->transformConceptInfoResults($result, $uris, $vocabs, $clang); |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Generates the sparql query for queryTypes |
||
| 519 | * @param string $lang |
||
| 520 | * @return string sparql query |
||
| 521 | */ |
||
| 522 | private function generateQueryTypesQuery($lang) { |
||
| 523 | $fcl = $this->generateFromClause(); |
||
| 524 | $query = <<<EOQ |
||
| 525 | SELECT DISTINCT ?type ?label ?superclass $fcl |
||
| 526 | WHERE { |
||
| 527 | { |
||
| 528 | { BIND( skos:Concept as ?type ) } |
||
| 529 | UNION |
||
| 530 | { BIND( skos:Collection as ?type ) } |
||
| 531 | UNION |
||
| 532 | { BIND( isothes:ConceptGroup as ?type ) } |
||
| 533 | UNION |
||
| 534 | { BIND( isothes:ThesaurusArray as ?type ) } |
||
| 535 | UNION |
||
| 536 | { ?type rdfs:subClassOf/rdfs:subClassOf* skos:Concept . } |
||
| 537 | UNION |
||
| 538 | { ?type rdfs:subClassOf/rdfs:subClassOf* skos:Collection . } |
||
| 539 | } |
||
| 540 | OPTIONAL { |
||
| 541 | ?type rdfs:label ?label . |
||
| 542 | FILTER(langMatches(lang(?label), '$lang')) |
||
| 543 | } |
||
| 544 | OPTIONAL { |
||
| 545 | ?type rdfs:subClassOf ?superclass . |
||
| 546 | } |
||
| 547 | FILTER EXISTS { |
||
| 548 | ?s a ?type . |
||
| 549 | ?s skos:prefLabel ?prefLabel . |
||
| 550 | } |
||
| 551 | } |
||
| 552 | EOQ; |
||
| 553 | return $query; |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Transforms the results into an array format. |
||
| 558 | * @param EasyRdf\Sparql\Result $result |
||
| 559 | * @return array Array with URIs (string) as key and array of (label, superclassURI) as value |
||
| 560 | */ |
||
| 561 | private function transformQueryTypesResults($result) { |
||
| 562 | $ret = array(); |
||
| 563 | foreach ($result as $row) { |
||
| 564 | $type = array(); |
||
| 565 | if (isset($row->label)) { |
||
| 566 | $type['label'] = $row->label->getValue(); |
||
| 567 | } |
||
| 568 | |||
| 569 | if (isset($row->superclass)) { |
||
| 570 | $type['superclass'] = $row->superclass->getUri(); |
||
| 571 | } |
||
| 572 | |||
| 573 | $ret[$row->type->getURI()] = $type; |
||
| 574 | } |
||
| 575 | return $ret; |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Retrieve information about types from the endpoint |
||
| 580 | * @param string $lang |
||
| 581 | * @return array Array with URIs (string) as key and array of (label, superclassURI) as value |
||
| 582 | */ |
||
| 583 | public function queryTypes($lang) { |
||
| 584 | $query = $this->generateQueryTypesQuery($lang); |
||
| 585 | $result = $this->query($query); |
||
| 586 | return $this->transformQueryTypesResults($result); |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Generates the concept scheme query. |
||
| 591 | * @param string $conceptscheme concept scheme URI |
||
| 592 | * @return string sparql query |
||
| 593 | */ |
||
| 594 | private function generateQueryConceptSchemeQuery($conceptscheme) { |
||
| 595 | $fcl = $this->generateFromClause(); |
||
| 596 | $query = <<<EOQ |
||
| 597 | CONSTRUCT { |
||
| 598 | <$conceptscheme> ?property ?value . |
||
| 599 | } $fcl WHERE { |
||
| 600 | <$conceptscheme> ?property ?value . |
||
| 601 | FILTER (?property != skos:hasTopConcept) |
||
| 602 | } |
||
| 603 | EOQ; |
||
| 604 | return $query; |
||
| 605 | } |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Retrieves conceptScheme information from the endpoint. |
||
| 609 | * @param string $conceptscheme concept scheme URI |
||
| 610 | * @return \EasyRdf\Sparql\Result|\EasyRdf\Graph query result graph |
||
| 611 | */ |
||
| 612 | public function queryConceptScheme($conceptscheme) { |
||
| 613 | $query = $this->generateQueryConceptSchemeQuery($conceptscheme); |
||
| 614 | return $this->query($query); |
||
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Generates the queryConceptSchemes sparql query. |
||
| 619 | * @param string $lang language of labels |
||
| 620 | * @return string sparql query |
||
| 621 | */ |
||
| 622 | private function generateQueryConceptSchemesQuery($lang) { |
||
| 623 | $fcl = $this->generateFromClause(); |
||
| 624 | $query = <<<EOQ |
||
| 625 | SELECT ?cs ?label ?preflabel ?title ?domain ?domainLabel $fcl |
||
| 626 | WHERE { |
||
| 627 | ?cs a skos:ConceptScheme . |
||
| 628 | OPTIONAL{ |
||
| 629 | ?cs dcterms:subject ?domain. |
||
| 630 | ?domain skos:prefLabel ?domainLabel. |
||
| 631 | FILTER(langMatches(lang(?domainLabel), '$lang')) |
||
| 632 | } |
||
| 633 | OPTIONAL { |
||
| 634 | ?cs rdfs:label ?label . |
||
| 635 | FILTER(langMatches(lang(?label), '$lang')) |
||
| 636 | } |
||
| 637 | OPTIONAL { |
||
| 638 | ?cs skos:prefLabel ?preflabel . |
||
| 639 | FILTER(langMatches(lang(?preflabel), '$lang')) |
||
| 640 | } |
||
| 641 | OPTIONAL { |
||
| 642 | { ?cs dc11:title ?title } |
||
| 643 | UNION |
||
| 644 | { ?cs dc:title ?title } |
||
| 645 | FILTER(langMatches(lang(?title), '$lang')) |
||
| 646 | } |
||
| 647 | } |
||
| 648 | ORDER BY ?cs |
||
| 649 | EOQ; |
||
| 650 | return $query; |
||
| 651 | } |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Transforms the queryConceptScheme results into an array format. |
||
| 655 | * @param EasyRdf\Sparql\Result $result |
||
| 656 | * @return array |
||
| 657 | */ |
||
| 658 | private function transformQueryConceptSchemesResults($result) { |
||
| 659 | $ret = array(); |
||
| 660 | foreach ($result as $row) { |
||
| 661 | $conceptscheme = array(); |
||
| 662 | if (isset($row->label)) { |
||
| 663 | $conceptscheme['label'] = $row->label->getValue(); |
||
| 664 | } |
||
| 665 | |||
| 666 | if (isset($row->preflabel)) { |
||
| 667 | $conceptscheme['prefLabel'] = $row->preflabel->getValue(); |
||
| 668 | } |
||
| 669 | |||
| 670 | if (isset($row->title)) { |
||
| 671 | $conceptscheme['title'] = $row->title->getValue(); |
||
| 672 | } |
||
| 673 | // add dct:subject and their labels in the result |
||
| 674 | if(isset($row->domain) && isset($row->domainLabel)){ |
||
| 675 | $conceptscheme['subject']['uri']=$row->domain->getURI(); |
||
| 676 | $conceptscheme['subject']['prefLabel']=$row->domainLabel->getValue(); |
||
| 677 | } |
||
| 678 | |||
| 679 | $ret[$row->cs->getURI()] = $conceptscheme; |
||
| 680 | } |
||
| 681 | return $ret; |
||
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * return a list of skos:ConceptScheme instances in the given graph |
||
| 686 | * @param string $lang language of labels |
||
| 687 | * @return array Array with concept scheme URIs (string) as keys and labels (string) as values |
||
| 688 | */ |
||
| 689 | public function queryConceptSchemes($lang) { |
||
| 690 | $query = $this->generateQueryConceptSchemesQuery($lang); |
||
| 691 | $result = $this->query($query); |
||
| 692 | return $this->transformQueryConceptSchemesResults($result); |
||
| 693 | } |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Generate a VALUES clause for limiting the targeted graphs. |
||
| 697 | * @param Vocabulary[]|null $vocabs the vocabularies to target |
||
| 698 | * @return string[] array of graph URIs |
||
| 699 | */ |
||
| 700 | protected function getVocabGraphs($vocabs) { |
||
| 701 | if ($vocabs === null || sizeof($vocabs) == 0) { |
||
| 702 | // searching from all vocabularies - limit to known graphs |
||
| 703 | $vocabs = $this->model->getVocabularies(); |
||
| 704 | } |
||
| 705 | $graphs = array(); |
||
| 706 | foreach ($vocabs as $voc) { |
||
| 707 | $graph = $voc->getGraph(); |
||
| 708 | if (!is_null($graph) && !in_array($graph, $graphs)) { |
||
| 709 | $graphs[] = $graph; |
||
| 710 | } |
||
| 711 | } |
||
| 712 | return $graphs; |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Generate a VALUES clause for limiting the targeted graphs. |
||
| 717 | * @param Vocabulary[]|null $vocabs array of Vocabulary objects to target |
||
| 718 | * @return string VALUES clause, or "" if not necessary to limit |
||
| 719 | */ |
||
| 720 | protected function formatValuesGraph($vocabs) { |
||
| 721 | if (!$this->isDefaultEndpoint()) { |
||
| 722 | return ""; |
||
| 723 | } |
||
| 724 | $graphs = $this->getVocabGraphs($vocabs); |
||
| 725 | return $this->formatValues('?graph', $graphs, 'uri'); |
||
| 726 | } |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Generate a FILTER clause for limiting the targeted graphs. |
||
| 730 | * @param array $vocabs array of Vocabulary objects to target |
||
| 731 | * @return string FILTER clause, or "" if not necessary to limit |
||
| 732 | */ |
||
| 733 | protected function formatFilterGraph($vocabs) { |
||
| 734 | if (!$this->isDefaultEndpoint()) { |
||
| 735 | return ""; |
||
| 736 | } |
||
| 737 | $graphs = $this->getVocabGraphs($vocabs); |
||
| 738 | $values = array(); |
||
| 739 | foreach ($graphs as $graph) { |
||
| 740 | $values[] = "<$graph>"; |
||
| 741 | } |
||
| 742 | if (count($values)) { |
||
| 743 | return "FILTER (?graph IN (" . implode(',', $values) . "))"; |
||
| 744 | } |
||
| 745 | } |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Formats combined limit and offset clauses for the sparql query |
||
| 749 | * @param int $limit maximum number of hits to retrieve; 0 for unlimited |
||
| 750 | * @param int $offset offset of results to retrieve; 0 for beginning of list |
||
| 751 | * @return string sparql query clauses |
||
| 752 | */ |
||
| 753 | protected function formatLimitAndOffset($limit, $offset) { |
||
| 754 | $limit = ($limit) ? 'LIMIT ' . $limit : ''; |
||
| 755 | $offset = ($offset) ? 'OFFSET ' . $offset : ''; |
||
| 756 | // eliminating whitespace and line changes when the conditions aren't needed. |
||
| 757 | $limitandoffset = ''; |
||
| 758 | if ($limit && $offset) { |
||
| 759 | $limitandoffset = "\n" . $limit . "\n" . $offset; |
||
| 760 | } elseif ($limit) { |
||
| 761 | $limitandoffset = "\n" . $limit; |
||
| 762 | } elseif ($offset) { |
||
| 763 | $limitandoffset = "\n" . $offset; |
||
| 764 | } |
||
| 765 | |||
| 766 | return $limitandoffset; |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Formats a sparql query clause for limiting the search to specific concept types. |
||
| 771 | * @param array $types limit search to concepts of the given type(s) |
||
| 772 | * @return string sparql query clause |
||
| 773 | */ |
||
| 774 | protected function formatTypes($types) { |
||
| 775 | $typePatterns = array(); |
||
| 776 | if (!empty($types)) { |
||
| 777 | foreach ($types as $type) { |
||
| 778 | $unprefixed = EasyRdf\RdfNamespace::expand($type); |
||
| 779 | $typePatterns[] = "{ ?s a <$unprefixed> }"; |
||
| 780 | } |
||
| 781 | } |
||
| 782 | |||
| 783 | return implode(' UNION ', $typePatterns); |
||
| 784 | } |
||
| 785 | |||
| 786 | /** |
||
| 787 | * @param string $prop property to include in the result eg. 'broader' or 'narrower' |
||
| 788 | * @return string sparql query clause |
||
| 789 | */ |
||
| 790 | private function formatPropertyCsvClause($prop) { |
||
| 791 | # This expression creates a CSV row containing pairs of (uri,prefLabel) values. |
||
| 792 | # The REPLACE is performed for quotes (" -> "") so they don't break the CSV format. |
||
| 793 | $clause = <<<EOV |
||
| 794 | (GROUP_CONCAT(DISTINCT CONCAT( |
||
| 795 | '"', IF(isIRI(?$prop),STR(?$prop),''), '"', ',', |
||
| 796 | '"', REPLACE(IF(BOUND(?{$prop}lab),?{$prop}lab,''), '"', '""'), '"', ',', |
||
| 797 | '"', REPLACE(IF(isLiteral(?{$prop}),?{$prop},''), '"', '""'), '"' |
||
| 798 | ); separator='\\n') as ?{$prop}s) |
||
| 799 | EOV; |
||
| 800 | return $clause; |
||
| 801 | } |
||
| 802 | |||
| 803 | /** |
||
| 804 | * @return string sparql query clause |
||
| 805 | */ |
||
| 806 | private function formatPrefLabelCsvClause() { |
||
| 807 | # This expression creates a CSV row containing pairs of (prefLabel, lang) values. |
||
| 808 | # The REPLACE is performed for quotes (" -> "") so they don't break the CSV format. |
||
| 809 | $clause = <<<EOV |
||
| 810 | (GROUP_CONCAT(DISTINCT CONCAT( |
||
| 811 | '"', STR(?pref), '"', ',', '"', lang(?pref), '"' |
||
| 812 | ); separator='\\n') as ?preflabels) |
||
| 813 | EOV; |
||
| 814 | return $clause; |
||
| 815 | } |
||
| 816 | |||
| 817 | /** |
||
| 818 | * @param string $lang language code of the returned labels |
||
| 819 | * @param array|null $fields extra fields to include in the result (array of strings). (default: null = none) |
||
| 820 | * @return array sparql query clause |
||
| 821 | */ |
||
| 822 | protected function formatExtraFields($lang, $fields) { |
||
| 823 | // extra variable expressions to request and extra fields to query for |
||
| 824 | $ret = array('extravars' => '', 'extrafields' => ''); |
||
| 825 | |||
| 826 | if ($fields === null) { |
||
| 827 | return $ret; |
||
| 828 | } |
||
| 829 | |||
| 830 | if (in_array('prefLabel', $fields)) { |
||
| 831 | $ret['extravars'] .= $this->formatPreflabelCsvClause(); |
||
| 832 | $ret['extrafields'] .= <<<EOF |
||
| 833 | OPTIONAL { |
||
| 834 | ?s skos:prefLabel ?pref . |
||
| 835 | } |
||
| 836 | EOF; |
||
| 837 | // removing the prefLabel from the fields since it has been handled separately |
||
| 838 | $fields = array_diff($fields, array('prefLabel')); |
||
| 839 | } |
||
| 840 | |||
| 841 | foreach ($fields as $field) { |
||
| 842 | $ret['extravars'] .= $this->formatPropertyCsvClause($field); |
||
| 843 | $ret['extrafields'] .= <<<EOF |
||
| 844 | OPTIONAL { |
||
| 845 | ?s skos:$field ?$field . |
||
| 846 | FILTER(!isLiteral(?$field)||langMatches(lang(?{$field}), '$lang')) |
||
| 847 | OPTIONAL { ?$field skos:prefLabel ?{$field}lab . FILTER(langMatches(lang(?{$field}lab), '$lang')) } |
||
| 848 | } |
||
| 849 | EOF; |
||
| 850 | } |
||
| 851 | |||
| 852 | return $ret; |
||
| 853 | } |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Generate condition for matching labels in SPARQL |
||
| 857 | * @param string $term search term |
||
| 858 | * @param string $searchLang language code used for matching labels (null means any language) |
||
| 859 | * @return string sparql query snippet |
||
| 860 | */ |
||
| 861 | protected function generateConceptSearchQueryCondition($term, $searchLang) |
||
| 862 | { |
||
| 863 | # use appropriate matching function depending on query type: =, strstarts, strends or full regex |
||
| 864 | if (preg_match('/^[^\*]+$/', $term)) { // exact query |
||
| 865 | $term = str_replace('\\', '\\\\', $term); // quote slashes |
||
| 866 | $term = str_replace('\'', '\\\'', mb_strtolower($term, 'UTF-8')); // make lowercase and escape single quotes |
||
| 867 | $filtercond = "LCASE(STR(?match)) = '$term'"; |
||
| 868 | } elseif (preg_match('/^[^\*]+\*$/', $term)) { // prefix query |
||
| 869 | $term = substr($term, 0, -1); // remove the final asterisk |
||
| 870 | $term = str_replace('\\', '\\\\', $term); // quote slashes |
||
| 871 | $term = str_replace('\'', '\\\'', mb_strtolower($term, 'UTF-8')); // make lowercase and escape single quotes |
||
| 872 | $filtercond = "STRSTARTS(LCASE(STR(?match)), '$term')"; |
||
| 873 | } elseif (preg_match('/^\*[^\*]+$/', $term)) { // suffix query |
||
| 874 | $term = substr($term, 1); // remove the preceding asterisk |
||
| 875 | $term = str_replace('\\', '\\\\', $term); // quote slashes |
||
| 876 | $term = str_replace('\'', '\\\'', mb_strtolower($term, 'UTF-8')); // make lowercase and escape single quotes |
||
| 877 | $filtercond = "STRENDS(LCASE(STR(?match)), '$term')"; |
||
| 878 | } else { // too complicated - have to use a regex |
||
| 879 | # make sure regex metacharacters are not passed through |
||
| 880 | $term = str_replace('\\', '\\\\', preg_quote($term)); |
||
| 881 | $term = str_replace('\\\\*', '.*', $term); // convert asterisk to regex syntax |
||
| 882 | $term = str_replace('\'', '\\\'', $term); // ensure single quotes are quoted |
||
| 883 | $filtercond = "REGEX(STR(?match), '^$term$', 'i')"; |
||
| 884 | } |
||
| 885 | |||
| 886 | $labelcondMatch = ($searchLang) ? "&& (?prop = skos:notation || LANGMATCHES(lang(?match), ?langParam))" : ""; |
||
| 887 | |||
| 888 | return "?s ?prop ?match . FILTER ($filtercond $labelcondMatch)"; |
||
| 889 | } |
||
| 890 | |||
| 891 | |||
| 892 | /** |
||
| 893 | * Inner query for concepts using a search term. |
||
| 894 | * @param string $term search term |
||
| 895 | * @param string $lang language code of the returned labels |
||
| 896 | * @param string $searchLang language code used for matching labels (null means any language) |
||
| 897 | * @param string[] $props properties to target e.g. array('skos:prefLabel','skos:altLabel') |
||
| 898 | * @param boolean $unique restrict results to unique concepts (default: false) |
||
| 899 | * @return string sparql query |
||
| 900 | */ |
||
| 901 | protected function generateConceptSearchQueryInner($term, $lang, $searchLang, $props, $unique, $filterGraph) |
||
| 902 | { |
||
| 903 | $valuesProp = $this->formatValues('?prop', $props); |
||
| 904 | $textcond = $this->generateConceptSearchQueryCondition($term, $searchLang); |
||
| 905 | |||
| 906 | $rawterm = str_replace(array('\\', '*', '"'), array('\\\\', '', '\"'), $term); |
||
| 907 | // graph clause, if necessary |
||
| 908 | $graphClause = $filterGraph != '' ? 'GRAPH ?graph' : ''; |
||
| 909 | |||
| 910 | // extra conditions for label language, if specified |
||
| 911 | $labelcondLabel = ($lang) ? "LANGMATCHES(lang(?label), '$lang')" : "lang(?match) = '' || LANGMATCHES(lang(?label), lang(?match))"; |
||
| 912 | // if search language and UI/display language differ, must also consider case where there is no prefLabel in |
||
| 913 | // the display language; in that case, should use the label with the same language as the matched label |
||
| 914 | $labelcondFallback = ($searchLang != $lang) ? |
||
| 915 | "OPTIONAL { # in case previous OPTIONAL block gives no labels\n" . |
||
| 916 | "?s skos:prefLabel ?label . FILTER (LANGMATCHES(LANG(?label), LANG(?match))) }" : ""; |
||
| 917 | |||
| 918 | // Including the labels if there is no query term given. |
||
| 919 | if ($rawterm === '') { |
||
| 920 | $labelClause = "?s skos:prefLabel ?label ."; |
||
| 921 | $labelClause = ($lang) ? $labelClause . " FILTER (LANGMATCHES(LANG(?label), '$lang'))" : $labelClause . ""; |
||
| 922 | return $labelClause . " BIND(?label AS ?match)"; |
||
| 923 | } |
||
| 924 | |||
| 925 | /* |
||
| 926 | * This query does some tricks to obtain a list of unique concepts. |
||
| 927 | * From each match generated by the text index, a string such as |
||
| 928 | * "1en@example" is generated, where the first character is a number |
||
| 929 | * encoding the property and priority, then comes the language tag and |
||
| 930 | * finally the original literal after an @ sign. Of these, the MIN |
||
| 931 | * function is used to pick the best match for each concept. Finally, |
||
| 932 | * the structure is unpacked to get back the original string. Phew! |
||
| 933 | */ |
||
| 934 | $hitvar = $unique ? '(MIN(?matchstr) AS ?hit)' : '(?matchstr AS ?hit)'; |
||
| 935 | $hitgroup = $unique ? 'GROUP BY ?s ?label ?notation' : ''; |
||
| 936 | |||
| 937 | $langClause = $this->generateLangClause($searchLang); |
||
| 938 | |||
| 939 | $query = <<<EOQ |
||
| 940 | SELECT DISTINCT ?s ?label ?notation $hitvar |
||
| 941 | WHERE { |
||
| 942 | $graphClause { |
||
| 943 | { |
||
| 944 | $valuesProp |
||
| 945 | VALUES (?prop ?pri ?langParam) { (skos:prefLabel 1 $langClause) (skos:altLabel 3 $langClause) (skos:notation 5 '') (skos:hiddenLabel 7 $langClause)} |
||
| 946 | $textcond |
||
| 947 | ?s ?prop ?match } |
||
| 948 | OPTIONAL { |
||
| 949 | ?s skos:prefLabel ?label . |
||
| 950 | FILTER ($labelcondLabel) |
||
| 951 | } $labelcondFallback |
||
| 952 | BIND(IF(langMatches(LANG(?match),'$lang'), ?pri, ?pri+1) AS ?npri) |
||
| 953 | BIND(CONCAT(STR(?npri), LANG(?match), '@', STR(?match)) AS ?matchstr) |
||
| 954 | OPTIONAL { ?s skos:notation ?notation } |
||
| 955 | } |
||
| 956 | $filterGraph |
||
| 957 | } |
||
| 958 | $hitgroup |
||
| 959 | EOQ; |
||
| 960 | |||
| 961 | return $query; |
||
| 962 | } |
||
| 963 | /** |
||
| 964 | * This function can be overwritten in other SPARQL dialects for the possibility of handling the different language clauses |
||
| 965 | * @param string $lang |
||
| 966 | * @return string formatted language clause |
||
| 967 | */ |
||
| 968 | protected function generateLangClause($lang) { |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Query for concepts using a search term. |
||
| 974 | * @param array|null $fields extra fields to include in the result (array of strings). (default: null = none) |
||
| 975 | * @param boolean $unique restrict results to unique concepts (default: false) |
||
| 976 | * @param boolean $showDeprecated whether to include deprecated concepts in search results (default: false) |
||
| 977 | * @param ConceptSearchParameters $params |
||
| 978 | * @return string sparql query |
||
| 979 | */ |
||
| 980 | protected function generateConceptSearchQuery($fields, $unique, $params, $showDeprecated = false) { |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * Transform a single concept search query results into the skosmos desired return format. |
||
| 1076 | * @param $row SPARQL query result row |
||
| 1077 | * @param array $vocabs array of Vocabulary objects to search; empty for global search |
||
| 1078 | * @return array query result object |
||
| 1079 | */ |
||
| 1080 | private function transformConceptSearchResult($row, $vocabs, $fields) |
||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Transform the concept search query results into the skosmos desired return format. |
||
| 1159 | * @param EasyRdf\Sparql\Result $results |
||
| 1160 | * @param array $vocabs array of Vocabulary objects to search; empty for global search |
||
| 1161 | * @return array query result object |
||
| 1162 | */ |
||
| 1163 | private function transformConceptSearchResults($results, $vocabs, $fields) { |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Query for concepts using a search term. |
||
| 1178 | * @param array $vocabs array of Vocabulary objects to search; empty for global search |
||
| 1179 | * @param array $fields extra fields to include in the result (array of strings). (default: null = none) |
||
| 1180 | * @param boolean $unique restrict results to unique concepts (default: false) |
||
| 1181 | * @param boolean $showDeprecated whether to include deprecated concepts in the result (default: false) |
||
| 1182 | * @param ConceptSearchParameters $params |
||
| 1183 | * @return array query result object |
||
| 1184 | */ |
||
| 1185 | public function queryConcepts($vocabs, $fields = null, $unique = false, $params, $showDeprecated = false) { |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Generates sparql query clauses used for creating the alphabetical index. |
||
| 1193 | * @param string $letter the letter (or special class) to search for |
||
| 1194 | * @return array of sparql query clause strings |
||
| 1195 | */ |
||
| 1196 | private function formatFilterConditions($letter, $lang) { |
||
| 1221 | |||
| 1222 | /** |
||
| 1223 | * Generates the sparql query used for rendering the alphabetical index. |
||
| 1224 | * @param string $letter the letter (or special class) to search for |
||
| 1225 | * @param string $lang language of labels |
||
| 1226 | * @param integer $limit limits the amount of results |
||
| 1227 | * @param integer $offset offsets the result set |
||
| 1228 | * @param array|null $classes |
||
| 1229 | * @param boolean $showDeprecated whether to include deprecated concepts in the result (default: false) |
||
| 1230 | * @param \EasyRdf\Resource|null $qualifier alphabetical list qualifier resource or null (default: null) |
||
| 1231 | * @return string sparql query |
||
| 1232 | */ |
||
| 1233 | protected function generateAlphabeticalListQuery($letter, $lang, $limit, $offset, $classes, $showDeprecated = false, $qualifier = null) { |
||
| 1279 | |||
| 1280 | /** |
||
| 1281 | * Transforms the alphabetical list query results into an array format. |
||
| 1282 | * @param EasyRdf\Sparql\Result $results |
||
| 1283 | * @return array |
||
| 1284 | */ |
||
| 1285 | private function transformAlphabeticalListResults($results) { |
||
| 1321 | |||
| 1322 | /** |
||
| 1323 | * Query for concepts with a term starting with the given letter. Also special classes '0-9' (digits), |
||
| 1324 | * '*!' (special characters) and '*' (everything) are accepted. |
||
| 1325 | * @param string $letter the letter (or special class) to search for |
||
| 1326 | * @param string $lang language of labels |
||
| 1327 | * @param integer $limit limits the amount of results |
||
| 1328 | * @param integer $offset offsets the result set |
||
| 1329 | * @param array $classes |
||
| 1330 | * @param boolean $showDeprecated whether to include deprecated concepts in the result (default: false) |
||
| 1331 | * @param \EasyRdf\Resource|null $qualifier alphabetical list qualifier resource or null (default: null) |
||
| 1332 | */ |
||
| 1333 | public function queryConceptsAlphabetical($letter, $lang, $limit = null, $offset = null, $classes = null, $showDeprecated = false, $qualifier = null) { |
||
| 1334 | if ($letter === '') { |
||
| 1335 | return array(); // special case: no letter given, return empty list |
||
| 1336 | } |
||
| 1337 | $query = $this->generateAlphabeticalListQuery($letter, $lang, $limit, $offset, $classes, $showDeprecated, $qualifier); |
||
| 1338 | $results = $this->query($query); |
||
| 1339 | return $this->transformAlphabeticalListResults($results); |
||
| 1340 | } |
||
| 1341 | |||
| 1342 | /** |
||
| 1343 | * Creates the query used for finding out which letters should be displayed in the alphabetical index. |
||
| 1344 | * Note that we force the datatype of the result variable otherwise Virtuoso does not properly interpret the DISTINCT and we have duplicated results |
||
| 1345 | * @param string $lang language |
||
| 1346 | * @return string sparql query |
||
| 1347 | */ |
||
| 1348 | private function generateFirstCharactersQuery($lang, $classes) { |
||
| 1349 | $gcl = $this->graphClause; |
||
| 1350 | $classes = (isset($classes) && sizeof($classes) > 0) ? $classes : array('http://www.w3.org/2004/02/skos/core#Concept'); |
||
| 1351 | $values = $this->formatValues('?type', $classes, 'uri'); |
||
| 1352 | $query = <<<EOQ |
||
| 1353 | SELECT DISTINCT (ucase(str(substr(?label, 1, 1))) as ?l) WHERE { |
||
| 1354 | $gcl { |
||
| 1355 | ?c skos:prefLabel ?label . |
||
| 1356 | ?c a ?type |
||
| 1364 | |||
| 1365 | /** |
||
| 1366 | * Transforms the first characters query results into an array format. |
||
| 1367 | * @param EasyRdf\Sparql\Result $result |
||
| 1368 | * @return array |
||
| 1369 | */ |
||
| 1370 | private function transformFirstCharactersResults($result) { |
||
| 1377 | |||
| 1378 | /** |
||
| 1379 | * Query for the first characters (letter or otherwise) of the labels in the particular language. |
||
| 1380 | * @param string $lang language |
||
| 1381 | * @return array array of characters |
||
| 1382 | */ |
||
| 1383 | public function queryFirstCharacters($lang, $classes = null) { |
||
| 1388 | |||
| 1389 | /** |
||
| 1390 | * @param string $uri |
||
| 1391 | * @param string $lang |
||
| 1392 | * @return string sparql query string |
||
| 1393 | */ |
||
| 1394 | private function generateLabelQuery($uri, $lang) { |
||
| 1421 | |||
| 1422 | |||
| 1423 | /** |
||
| 1424 | * @param string $uri |
||
| 1425 | * @param string $lang |
||
| 1426 | * @return string sparql query string |
||
| 1427 | */ |
||
| 1428 | private function generateAllLabelsQuery($uri, $lang) { |
||
| 1444 | |||
| 1445 | /** |
||
| 1446 | * Query for a label (skos:prefLabel, rdfs:label, dc:title, dc11:title) of a resource. |
||
| 1447 | * @param string $uri |
||
| 1448 | * @param string $lang |
||
| 1449 | * @return array array of labels (key: lang, val: label), or null if resource doesn't exist |
||
| 1450 | */ |
||
| 1451 | View Code Duplication | public function queryLabel($uri, $lang) { |
|
| 1471 | |||
| 1472 | /** |
||
| 1473 | * Query for skos:prefLabels, skos:altLabels and skos:hiddenLabels of a resource. |
||
| 1474 | * @param string $uri |
||
| 1475 | * @param string $lang |
||
| 1476 | * @return array array of prefLabels, altLabels and hiddenLabels - or null if resource doesn't exist |
||
| 1477 | */ |
||
| 1478 | public function queryAllConceptLabels($uri, $lang) { |
||
| 1496 | |||
| 1497 | /** |
||
| 1498 | * Generates a SPARQL query to retrieve the super properties of a given property URI. |
||
| 1499 | * Note this must be executed in the graph where this information is available. |
||
| 1500 | * @param string $uri |
||
| 1501 | * @return string sparql query string |
||
| 1502 | */ |
||
| 1503 | private function generateSubPropertyOfQuery($uri) { |
||
| 1513 | |||
| 1514 | /** |
||
| 1515 | * Query the super properties of a provided property URI. |
||
| 1516 | * @param string $uri URI of a propertyes |
||
| 1517 | * @return array array super properties, or null if none exist |
||
| 1518 | */ |
||
| 1519 | View Code Duplication | public function querySuperProperties($uri) { |
|
| 1538 | |||
| 1539 | |||
| 1540 | /** |
||
| 1541 | * Generates a sparql query for queryNotation. |
||
| 1542 | * @param string $uri |
||
| 1543 | * @return string sparql query |
||
| 1544 | */ |
||
| 1545 | private function generateNotationQuery($uri) { |
||
| 1556 | |||
| 1557 | /** |
||
| 1558 | * Query for the notation of the concept (skos:notation) of a resource. |
||
| 1559 | * @param string $uri |
||
| 1560 | * @return string notation or null if it doesn't exist |
||
| 1561 | */ |
||
| 1562 | public function queryNotation($uri) { |
||
| 1572 | |||
| 1573 | /** |
||
| 1574 | * Generates a sparql query for queryProperty. |
||
| 1575 | * @param string $uri |
||
| 1576 | * @param string $prop the name of the property eg. 'skos:broader'. |
||
| 1577 | * @param string $lang |
||
| 1578 | * @param boolean $anylang if you want a label even when it isn't available in the language you requested. |
||
| 1579 | * @return string sparql query |
||
| 1580 | */ |
||
| 1581 | private function generatePropertyQuery($uri, $prop, $lang, $anylang) { |
||
| 1605 | |||
| 1606 | /** |
||
| 1607 | * Transforms the sparql query result into an array or null if the concept doesn't exist. |
||
| 1608 | * @param EasyRdf\Sparql\Result $result |
||
| 1609 | * @param string $lang |
||
| 1610 | * @return array array of property values (key: URI, val: label), or null if concept doesn't exist |
||
| 1611 | */ |
||
| 1612 | private function transformPropertyQueryResults($result, $lang) { |
||
| 1637 | |||
| 1638 | /** |
||
| 1639 | * Query a single property of a concept. |
||
| 1640 | * @param string $uri |
||
| 1641 | * @param string $prop the name of the property eg. 'skos:broader'. |
||
| 1642 | * @param string $lang |
||
| 1643 | * @param boolean $anylang if you want a label even when it isn't available in the language you requested. |
||
| 1644 | * @return array array of property values (key: URI, val: label), or null if concept doesn't exist |
||
| 1645 | */ |
||
| 1646 | public function queryProperty($uri, $prop, $lang, $anylang = false) { |
||
| 1652 | |||
| 1653 | /** |
||
| 1654 | * Query a single transitive property of a concept. |
||
| 1655 | * @param string $uri |
||
| 1656 | * @param array $props the name of the property eg. 'skos:broader'. |
||
| 1657 | * @param string $lang |
||
| 1658 | * @param integer $limit |
||
| 1659 | * @param boolean $anylang if you want a label even when it isn't available in the language you requested. |
||
| 1660 | * @return string sparql query |
||
| 1661 | */ |
||
| 1662 | private function generateTransitivePropertyQuery($uri, $props, $lang, $limit, $anylang) { |
||
| 1693 | |||
| 1694 | /** |
||
| 1695 | * Transforms the sparql query result object into an array. |
||
| 1696 | * @param EasyRdf\Sparql\Result $result |
||
| 1697 | * @param string $lang |
||
| 1698 | * @param string $fallbacklang language to use if label is not available in the preferred language |
||
| 1699 | * @return array of property values (key: URI, val: label), or null if concept doesn't exist |
||
| 1700 | */ |
||
| 1701 | private function transformTransitivePropertyResults($result, $lang, $fallbacklang) { |
||
| 1747 | |||
| 1748 | /** |
||
| 1749 | * Query a single transitive property of a concept. |
||
| 1750 | * @param string $uri |
||
| 1751 | * @param array $props the property/properties. |
||
| 1752 | * @param string $lang |
||
| 1753 | * @param string $fallbacklang language to use if label is not available in the preferred language |
||
| 1754 | * @param integer $limit |
||
| 1755 | * @param boolean $anylang if you want a label even when it isn't available in the language you requested. |
||
| 1756 | * @return array array of property values (key: URI, val: label), or null if concept doesn't exist |
||
| 1757 | */ |
||
| 1758 | public function queryTransitiveProperty($uri, $props, $lang, $limit, $anylang = false, $fallbacklang = '') { |
||
| 1763 | |||
| 1764 | /** |
||
| 1765 | * Generates the query for a concepts skos:narrowers. |
||
| 1766 | * @param string $uri |
||
| 1767 | * @param string $lang |
||
| 1768 | * @param string $fallback |
||
| 1769 | * @return string sparql query |
||
| 1770 | */ |
||
| 1771 | private function generateChildQuery($uri, $lang, $fallback, $props) { |
||
| 1800 | |||
| 1801 | /** |
||
| 1802 | * Transforms the sparql result object into an array. |
||
| 1803 | * @param EasyRdf\Sparql\Result $result |
||
| 1804 | * @param string $lang |
||
| 1805 | * @return array array of arrays describing each child concept, or null if concept doesn't exist |
||
| 1806 | */ |
||
| 1807 | private function transformNarrowerResults($result, $lang) { |
||
| 1844 | |||
| 1845 | /** |
||
| 1846 | * Query the narrower concepts of a concept. |
||
| 1847 | * @param string $uri |
||
| 1848 | * @param string $lang |
||
| 1849 | * @param string $fallback |
||
| 1850 | * @return array array of arrays describing each child concept, or null if concept doesn't exist |
||
| 1851 | */ |
||
| 1852 | public function queryChildren($uri, $lang, $fallback, $props) { |
||
| 1857 | |||
| 1858 | /** |
||
| 1859 | * Query the top concepts of a vocabulary. |
||
| 1860 | * @param string $conceptSchemes concept schemes whose top concepts to query for |
||
| 1861 | * @param string $lang language of labels |
||
| 1862 | * @param string $fallback language to use if label is not available in the preferred language |
||
| 1863 | */ |
||
| 1864 | public function queryTopConcepts($conceptSchemes, $lang, $fallback) { |
||
| 1910 | |||
| 1911 | /** |
||
| 1912 | * Generates a sparql query for finding the hierarchy for a concept. |
||
| 1913 | * A concept may be a top concept in multiple schemes, returned as a single whitespace-separated literal. |
||
| 1914 | * @param string $uri concept uri. |
||
| 1915 | * @param string $lang |
||
| 1916 | * @param string $fallback language to use if label is not available in the preferred language |
||
| 1917 | * @return string sparql query |
||
| 1918 | */ |
||
| 1919 | private function generateParentListQuery($uri, $lang, $fallback, $props) { |
||
| 1967 | |||
| 1968 | /** |
||
| 1969 | * Transforms the result into an array. |
||
| 1970 | * @param EasyRdf\Sparql\Result |
||
| 1971 | * @param string $lang |
||
| 1972 | * @return array|null an array for the REST controller to encode. |
||
| 1973 | */ |
||
| 1974 | private function transformParentListResults($result, $lang) |
||
| 2050 | |||
| 2051 | /** |
||
| 2052 | * Query for finding the hierarchy for a concept. |
||
| 2053 | * @param string $uri concept uri. |
||
| 2054 | * @param string $lang |
||
| 2055 | * @param string $fallback language to use if label is not available in the preferred language |
||
| 2056 | * @param array $props the hierarchy property/properties to use |
||
| 2057 | * @return an array for the REST controller to encode. |
||
| 2058 | */ |
||
| 2059 | public function queryParentList($uri, $lang, $fallback, $props) { |
||
| 2064 | |||
| 2065 | /** |
||
| 2066 | * return a list of concept group instances, sorted by label |
||
| 2067 | * @param string $groupClass URI of concept group class |
||
| 2068 | * @param string $lang language of labels to return |
||
| 2069 | * @return string sparql query |
||
| 2070 | */ |
||
| 2071 | private function generateConceptGroupsQuery($groupClass, $lang) { |
||
| 2090 | |||
| 2091 | /** |
||
| 2092 | * Transforms the sparql query result into an array. |
||
| 2093 | * @param EasyRdf\Sparql\Result $result |
||
| 2094 | * @return array |
||
| 2095 | */ |
||
| 2096 | private function transformConceptGroupsResults($result) { |
||
| 2124 | |||
| 2125 | /** |
||
| 2126 | * return a list of concept group instances, sorted by label |
||
| 2127 | * @param string $groupClass URI of concept group class |
||
| 2128 | * @param string $lang language of labels to return |
||
| 2129 | * @return array Result array with group URI as key and group label as value |
||
| 2130 | */ |
||
| 2131 | public function listConceptGroups($groupClass, $lang) { |
||
| 2136 | |||
| 2137 | /** |
||
| 2138 | * Generates the sparql query for listConceptGroupContents |
||
| 2139 | * @param string $groupClass URI of concept group class |
||
| 2140 | * @param string $group URI of the concept group instance |
||
| 2141 | * @param string $lang language of labels to return |
||
| 2142 | * @param boolean $showDeprecated whether to include deprecated in the result |
||
| 2143 | * @return string sparql query |
||
| 2144 | */ |
||
| 2145 | private function generateConceptGroupContentsQuery($groupClass, $group, $lang, $showDeprecated = false) { |
||
| 2169 | |||
| 2170 | /** |
||
| 2171 | * Transforms the sparql query result into an array. |
||
| 2172 | * @param EasyRdf\Sparql\Result $result |
||
| 2173 | * @param string $lang language of labels to return |
||
| 2174 | * @return array |
||
| 2175 | */ |
||
| 2176 | private function transformConceptGroupContentsResults($result, $lang) { |
||
| 2210 | |||
| 2211 | /** |
||
| 2212 | * return a list of concepts in a concept group |
||
| 2213 | * @param string $groupClass URI of concept group class |
||
| 2214 | * @param string $group URI of the concept group instance |
||
| 2215 | * @param string $lang language of labels to return |
||
| 2216 | * @param boolean $showDeprecated whether to include deprecated concepts in search results |
||
| 2217 | * @return array Result array with concept URI as key and concept label as value |
||
| 2218 | */ |
||
| 2219 | public function listConceptGroupContents($groupClass, $group, $lang,$showDeprecated = false) { |
||
| 2224 | |||
| 2225 | /** |
||
| 2226 | * Generates the sparql query for queryChangeList. |
||
| 2227 | * @param string $prop the property uri pointing to timestamps, eg. 'dc:modified' |
||
| 2228 | * @param string $lang language of labels to return |
||
| 2229 | * @param int $offset offset of results to retrieve; 0 for beginning of list |
||
| 2230 | * @param int $limit maximum number of results to return |
||
| 2231 | * @param boolean $showDeprecated whether to include deprecated concepts in the change list |
||
| 2232 | * @return string sparql query |
||
| 2233 | */ |
||
| 2234 | private function generateChangeListQuery($prop, $lang, $offset, $limit=200, $showDeprecated) { |
||
| 2260 | |||
| 2261 | /** |
||
| 2262 | * Transforms the sparql query result into an array. |
||
| 2263 | * @param EasyRdf\Sparql\Result $result |
||
| 2264 | * @return array |
||
| 2265 | */ |
||
| 2266 | private function transformChangeListResults($result) { |
||
| 2291 | |||
| 2292 | /** |
||
| 2293 | * return a list of recently changed or entirely new concepts |
||
| 2294 | * @param string $prop the property uri pointing to timestamps, eg. 'dc:modified' |
||
| 2295 | * @param string $lang language of labels to return |
||
| 2296 | * @param int $offset offset of results to retrieve; 0 for beginning of list |
||
| 2297 | * @param int $limit maximum number of results to return |
||
| 2298 | * @param boolean $showDeprecated whether to include deprecated concepts in the change list |
||
| 2299 | * @return array Result array |
||
| 2300 | */ |
||
| 2301 | public function queryChangeList($prop, $lang, $offset, $limit, $showDeprecated=false) { |
||
| 2307 | } |
||
| 2308 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: