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 |
||
| 11 | class GenericSparql { |
||
|
|
|||
| 12 | /** |
||
| 13 | * A SPARQL Client eg. an EasyRDF instance. |
||
| 14 | * @property EasyRdf_Sparql_Client $client |
||
| 15 | */ |
||
| 16 | protected $client; |
||
| 17 | /** |
||
| 18 | * Graph uri. |
||
| 19 | * @property string $graph |
||
| 20 | */ |
||
| 21 | protected $graph; |
||
| 22 | /** |
||
| 23 | * A SPARQL query graph part template. |
||
| 24 | * @property string $graph |
||
| 25 | */ |
||
| 26 | protected $graphClause; |
||
| 27 | /** |
||
| 28 | * Model instance. |
||
| 29 | * @property Model $model |
||
| 30 | */ |
||
| 31 | protected $model; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Requires the following three parameters. |
||
| 35 | * @param string $endpoint SPARQL endpoint address. |
||
| 36 | * @param object $graph an EasyRDF SPARQL graph instance. |
||
| 37 | * @param object $model a Model instance. |
||
| 38 | */ |
||
| 39 | public function __construct($endpoint, $graph, $model) { |
||
| 40 | // if special cache control (typically no-cache) was requested by the |
||
| 41 | // client, set the same type of cache control headers also in subsequent |
||
| 42 | // in the SPARQL requests (this is useful for performance testing) |
||
| 43 | $cache_control = filter_input(INPUT_SERVER, 'HTTP_CACHE_CONTROL', FILTER_SANITIZE_STRING); |
||
| 44 | $pragma = filter_input(INPUT_SERVER, 'HTTP_PRAGMA', FILTER_SANITIZE_STRING); |
||
| 45 | if ($cache_control !== null || $pragma !== null) { |
||
| 46 | $val = $pragma !== null ? $pragma : $cache_control; |
||
| 47 | // configure the HTTP client used by EasyRdf_Sparql_Client |
||
| 48 | $httpclient = EasyRdf_Http::getDefaultHttpClient(); |
||
| 49 | $httpclient->setHeaders('Cache-Control', $val); |
||
| 50 | EasyRdf_Http::setDefaultHttpClient($httpclient); // actually redundant.. |
||
| 51 | } |
||
| 52 | |||
| 53 | // create the EasyRDF SPARQL client instance to use |
||
| 54 | $this->client = new EasyRdf_Sparql_Client($endpoint); |
||
| 55 | $this->graph = $graph; |
||
| 56 | $this->model = $model; |
||
| 57 | |||
| 58 | // set graphClause so that it can be used by all queries |
||
| 59 | if ($this->isDefaultEndpoint()) // default endpoint; query any graph (and catch it in a variable) |
||
| 60 | { |
||
| 61 | $this->graphClause = "GRAPH $graph"; |
||
| 62 | } elseif ($graph) // query a specific graph |
||
| 63 | { |
||
| 64 | $this->graphClause = "GRAPH <$graph>"; |
||
| 65 | } else // query the default graph |
||
| 66 | { |
||
| 67 | $this->graphClause = ""; |
||
| 68 | } |
||
| 69 | |||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Return true if this is the default SPARQL endpoint, used as the facade to query |
||
| 74 | * all vocabularies. |
||
| 75 | */ |
||
| 76 | |||
| 77 | protected function isDefaultEndpoint() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns the graph instance |
||
| 83 | * @return object EasyRDF graph instance. |
||
| 84 | */ |
||
| 85 | public function getGraph() { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Generates the sparql query for retrieving concept and collection counts in a vocabulary. |
||
| 91 | * @return string sparql query |
||
| 92 | */ |
||
| 93 | View Code Duplication | private function generateCountConceptsQuery($array, $group) { |
|
| 94 | $gc = $this->graphClause; |
||
| 95 | $optional = $array ? "UNION { ?type rdfs:subClassOf* <$array> }" : ''; |
||
| 96 | $optional .= $group ? "UNION { ?type rdfs:subClassOf* <$group> }" : ''; |
||
| 97 | $query = <<<EOQ |
||
| 98 | SELECT (COUNT(?conc) as ?c) ?type ?typelabel WHERE { |
||
| 99 | $gc { |
||
| 100 | { ?conc a ?type . |
||
| 101 | { ?type rdfs:subClassOf* skos:Concept . } UNION { ?type rdfs:subClassOf* skos:Collection . } $optional } |
||
| 102 | OPTIONAL { ?type rdfs:label ?typelabel . } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | GROUP BY ?type ?typelabel |
||
| 106 | EOQ; |
||
| 107 | return $query; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Used for transforming the concept count query results. |
||
| 112 | * @param EasyRdf_Sparql_Result $result query results to be transformed |
||
| 113 | * @param string $lang language of labels |
||
| 114 | * @return Array containing the label counts |
||
| 115 | */ |
||
| 116 | private function transformCountConceptsResults($result, $lang) { |
||
| 117 | $ret = array(); |
||
| 118 | foreach ($result as $row) { |
||
| 119 | if (!isset($row->type)) { |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | $ret[$row->type->getUri()]['type'] = $row->type->getUri(); |
||
| 123 | $ret[$row->type->getUri()]['count'] = $row->c->getValue(); |
||
| 124 | if (isset($row->typelabel) && $row->typelabel->getLang() === $lang) { |
||
| 125 | $ret[$row->type->getUri()]['label'] = $row->typelabel->getValue(); |
||
| 126 | } |
||
| 127 | |||
| 128 | } |
||
| 129 | return $ret; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Used for counting number of concepts and collections in a vocabulary. |
||
| 134 | * @param string $lang language of labels |
||
| 135 | * @return int number of concepts in this vocabulary |
||
| 136 | */ |
||
| 137 | public function countConcepts($lang = null, $array = null, $group = null) { |
||
| 138 | $query = $this->generateCountConceptsQuery($array, $group); |
||
| 139 | $result = $this->client->query($query); |
||
| 140 | return $this->transformCountConceptsResults($result, $lang); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param array $langs Languages to query for |
||
| 145 | * @param string[] $props property names |
||
| 146 | * @return string sparql query |
||
| 147 | */ |
||
| 148 | private function generateCountLangConceptsQuery($langs, $classes, $props) { |
||
| 149 | $gc = $this->graphClause; |
||
| 150 | $classes = ($classes) ? $classes : array('http://www.w3.org/2004/02/skos/core#Concept'); |
||
| 151 | |||
| 152 | $values = $this->formatValues('?type', $classes, 'uri'); |
||
| 153 | $values_lang = $this->formatValues('?lang', $langs, 'literal'); |
||
| 154 | $values_prop = $this->formatValues('?prop', $props, null); |
||
| 155 | |||
| 156 | $query = <<<EOQ |
||
| 157 | SELECT ?lang ?prop |
||
| 158 | (COUNT(?label) as ?count) |
||
| 159 | WHERE { |
||
| 160 | $gc { |
||
| 161 | ?conc a ?type . |
||
| 162 | ?conc ?prop ?label . |
||
| 163 | FILTER (langMatches(lang(?label), ?lang)) |
||
| 164 | $values_lang |
||
| 165 | $values_prop |
||
| 166 | } |
||
| 167 | $values |
||
| 168 | } |
||
| 169 | GROUP BY ?lang ?prop ?type |
||
| 170 | EOQ; |
||
| 171 | return $query; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Transforms the CountLangConcepts results into an array of label counts. |
||
| 176 | * @param EasyRdf_Sparql_Result $result query results to be transformed |
||
| 177 | * @param array $langs Languages to query for |
||
| 178 | * @param string[] $props property names |
||
| 179 | */ |
||
| 180 | private function transformCountLangConceptsResults($result, $langs, $props) { |
||
| 181 | $ret = array(); |
||
| 182 | // set default count to zero; overridden below if query found labels |
||
| 183 | foreach ($langs as $lang) { |
||
| 184 | foreach ($props as $prop) { |
||
| 185 | $ret[$lang][$prop] = 0; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | foreach ($result as $row) { |
||
| 189 | if (isset($row->lang) && isset($row->prop) && isset($row->count)) { |
||
| 190 | $ret[$row->lang->getValue()][$row->prop->shorten()] = |
||
| 191 | $row->count->getValue(); |
||
| 192 | } |
||
| 193 | |||
| 194 | } |
||
| 195 | ksort($ret); |
||
| 196 | return $ret; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Counts the number of concepts in a easyRDF graph with a specific language. |
||
| 201 | * @param array $langs Languages to query for |
||
| 202 | * @return Array containing count of concepts for each language and property. |
||
| 203 | */ |
||
| 204 | public function countLangConcepts($langs, $classes = null) { |
||
| 205 | $props = array('skos:prefLabel', 'skos:altLabel', 'skos:hiddenLabel'); |
||
| 206 | $query = $this->generateCountLangConceptsQuery($langs, $classes, $props); |
||
| 207 | // Count the number of terms in each language |
||
| 208 | $result = $this->client->query($query); |
||
| 209 | return $this->transformCountLangConceptsResults($result, $langs, $props); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Formats a VALUES clause (SPARQL 1.1) which states that the variable should be bound to one |
||
| 214 | * of the constants given. |
||
| 215 | * @param string $varname variable name, e.g. "?uri" |
||
| 216 | * @param array $values the values |
||
| 217 | * @param string $type type of values: "uri", "literal" or null (determines quoting style) |
||
| 218 | */ |
||
| 219 | protected function formatValues($varname, $values, $type = null) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Filters multiple instances of the same vocabulary from the input array. |
||
| 239 | * @param \Vocabulary[] $vocabs array of Vocabulary objects |
||
| 240 | * @return \Vocabulary[] |
||
| 241 | */ |
||
| 242 | private function filterDuplicateVocabs($vocabs) { |
||
| 243 | // filtering duplicates |
||
| 244 | $unique_vocabs = array(); |
||
| 245 | if (sizeof($vocabs) > 0) { |
||
| 246 | foreach ($vocabs as $voc) { |
||
| 247 | $unique_vocabs[$voc->getId()] = $voc; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | return $unique_vocabs; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Generates a sparql query for one or more concept URIs |
||
| 256 | * @param mixed $uris concept URI (string) or array of URIs |
||
| 257 | * @param string|null $arrayClass the URI for thesaurus array class, or null if not used |
||
| 258 | * @param \Vocabulary[] $vocabs array of Vocabulary objects |
||
| 259 | * @return string sparql query |
||
| 260 | */ |
||
| 261 | private function generateConceptInfoQuery($uris, $arrayClass, $vocabs) { |
||
| 262 | $gc = $this->graphClause; |
||
| 263 | $values = $this->formatValues('?uri', $uris, 'uri'); |
||
| 264 | $unique_vocabs = $this->filterDuplicateVocabs($vocabs); |
||
| 265 | $values_graph = $this->formatValuesGraph($unique_vocabs); |
||
| 266 | |||
| 267 | if ($arrayClass === null) { |
||
| 268 | $construct = $optional = ""; |
||
| 269 | } else { |
||
| 270 | // add information that can be used to format narrower concepts by |
||
| 271 | // the array they belong to ("milk by source animal" use case) |
||
| 272 | $construct = "\n ?x skos:member ?o . ?x skos:prefLabel ?xl . ?x a <$arrayClass> ."; |
||
| 273 | $optional = "\n OPTIONAL { |
||
| 274 | ?x skos:member ?o . |
||
| 275 | ?x a <$arrayClass> . |
||
| 276 | ?x skos:prefLabel ?xl . |
||
| 277 | }"; |
||
| 278 | } |
||
| 279 | $query = <<<EOQ |
||
| 280 | CONSTRUCT { |
||
| 281 | ?s ?p ?uri . |
||
| 282 | ?sp ?uri ?op . |
||
| 283 | ?uri ?p ?o . |
||
| 284 | ?p rdfs:label ?proplabel . |
||
| 285 | ?p rdfs:subPropertyOf ?pp . |
||
| 286 | ?pp rdfs:label ?plabel . |
||
| 287 | ?o a ?ot . |
||
| 288 | ?o skos:prefLabel ?opl . |
||
| 289 | ?o rdfs:label ?ol . |
||
| 290 | ?o rdf:value ?ov . |
||
| 291 | ?o skos:notation ?on . |
||
| 292 | ?directgroup skos:member ?uri . |
||
| 293 | ?parent skos:member ?group . |
||
| 294 | ?group skos:prefLabel ?grouplabel . |
||
| 295 | ?b1 rdf:first ?item . |
||
| 296 | ?b1 rdf:rest ?b2 . |
||
| 297 | ?item a ?it . |
||
| 298 | ?item skos:prefLabel ?il . |
||
| 299 | ?group a ?grouptype . $construct |
||
| 300 | } WHERE { |
||
| 301 | $gc { |
||
| 302 | { |
||
| 303 | ?s ?p ?uri . |
||
| 304 | FILTER(!isBlank(?s)) |
||
| 305 | } |
||
| 306 | UNION |
||
| 307 | { ?sp ?uri ?op . } |
||
| 308 | UNION |
||
| 309 | { |
||
| 310 | ?directgroup skos:member ?uri . |
||
| 311 | ?group skos:member+ ?uri . |
||
| 312 | ?group skos:prefLabel ?grouplabel . |
||
| 313 | ?group a ?grouptype . |
||
| 314 | OPTIONAL { ?parent skos:member ?group } |
||
| 315 | } |
||
| 316 | UNION |
||
| 317 | { |
||
| 318 | ?uri ?p ?o . |
||
| 319 | OPTIONAL { |
||
| 320 | ?o rdf:rest* ?b1 . |
||
| 321 | ?b1 rdf:first ?item . |
||
| 322 | ?b1 rdf:rest ?b2 . |
||
| 323 | OPTIONAL { ?item a ?it . } |
||
| 324 | OPTIONAL { ?item skos:prefLabel ?il . } |
||
| 325 | } |
||
| 326 | OPTIONAL { |
||
| 327 | { ?p rdfs:label ?proplabel . } |
||
| 328 | UNION |
||
| 329 | { ?p rdfs:subPropertyOf ?pp . } |
||
| 330 | UNION |
||
| 331 | { ?o a ?ot . } |
||
| 332 | UNION |
||
| 333 | { ?o skos:prefLabel ?opl . } |
||
| 334 | UNION |
||
| 335 | { ?o rdfs:label ?ol . } |
||
| 336 | UNION |
||
| 337 | { ?o rdf:value ?ov . } |
||
| 338 | UNION |
||
| 339 | { ?o skos:notation ?on . } |
||
| 340 | } $optional |
||
| 341 | } |
||
| 342 | } |
||
| 343 | $values |
||
| 344 | } |
||
| 345 | $values_graph |
||
| 346 | EOQ; |
||
| 347 | return $query; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Transforms ConceptInfo query results into an array of Concept objects |
||
| 352 | * @param EasyRdf_Graph $result query results to be transformed |
||
| 353 | * @param mixed $uris concept URI (string) or array of URIs |
||
| 354 | * @param \Vocabulary[] $vocabs array of Vocabulary object |
||
| 355 | * @param string|null $clang content language |
||
| 356 | * @return mixed query result graph (EasyRdf_Graph), or array of Concept objects |
||
| 357 | */ |
||
| 358 | private function transformConceptInfoResults($result, $uris, $vocabs, $clang) { |
||
| 359 | $conceptArray = array(); |
||
| 360 | foreach ($uris as $index => $uri) { |
||
| 361 | $conc = $result->resource($uri); |
||
| 362 | $vocab = sizeof($vocabs) == 1 ? $vocabs[0] : $vocabs[$index]; |
||
| 363 | $conceptArray[] = new Concept($this->model, $vocab, $conc, $result, $clang); |
||
| 364 | } |
||
| 365 | return $conceptArray; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Returns information (as a graph) for one or more concept URIs |
||
| 370 | * @param mixed $uris concept URI (string) or array of URIs |
||
| 371 | * @param string|null $arrayClass the URI for thesaurus array class, or null if not used |
||
| 372 | * @param \Vocabulary[]|null $vocabs array of Vocabulary object |
||
| 373 | * @param boolean $as_graph whether to return a graph (true) or array of Concepts (false) |
||
| 374 | * @param string|null $clang content language |
||
| 375 | * @return mixed query result graph (EasyRdf_Graph), or array of Concept objects |
||
| 376 | */ |
||
| 377 | public function queryConceptInfo($uris, $arrayClass = null, $vocabs = array(), $as_graph = false, $clang = null) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Generates the sparql query for queryTypes |
||
| 398 | * @param string $lang |
||
| 399 | * @return string sparql query |
||
| 400 | */ |
||
| 401 | private function generateQueryTypesQuery($lang) { |
||
| 402 | $gc = $this->graphClause; |
||
| 403 | $query = <<<EOQ |
||
| 404 | SELECT DISTINCT ?type ?label ?superclass |
||
| 405 | WHERE { |
||
| 406 | $gc { |
||
| 407 | { |
||
| 408 | { BIND( skos:Concept as ?type ) } |
||
| 409 | UNION |
||
| 410 | { BIND( skos:Collection as ?type ) } |
||
| 411 | UNION |
||
| 412 | { BIND( isothes:ConceptGroup as ?type ) } |
||
| 413 | UNION |
||
| 414 | { BIND( isothes:ThesaurusArray as ?type ) } |
||
| 415 | UNION |
||
| 416 | { ?type rdfs:subClassOf/rdfs:subClassOf* skos:Concept . } |
||
| 417 | UNION |
||
| 418 | { ?type rdfs:subClassOf/rdfs:subClassOf* skos:Collection . } |
||
| 419 | } |
||
| 420 | OPTIONAL { |
||
| 421 | ?type rdfs:label ?label . |
||
| 422 | FILTER(langMatches(lang(?label), '$lang')) |
||
| 423 | } |
||
| 424 | OPTIONAL { |
||
| 425 | ?type rdfs:subClassOf ?superclass . |
||
| 426 | } |
||
| 427 | FILTER EXISTS { |
||
| 428 | ?s a ?type . |
||
| 429 | ?s skos:prefLabel ?prefLabel . |
||
| 430 | } |
||
| 431 | } |
||
| 432 | } |
||
| 433 | EOQ; |
||
| 434 | return $query; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Transforms the results into an array format. |
||
| 439 | * @param EasyRdf_Sparql_Result $result |
||
| 440 | * @return array Array with URIs (string) as key and array of (label, superclassURI) as value |
||
| 441 | */ |
||
| 442 | View Code Duplication | private function transformQueryTypesResults($result) { |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Retrieve information about types from the endpoint |
||
| 461 | * @param string $lang |
||
| 462 | * @return array Array with URIs (string) as key and array of (label, superclassURI) as value |
||
| 463 | */ |
||
| 464 | public function queryTypes($lang) { |
||
| 465 | $query = $this->generateQueryTypesQuery($lang); |
||
| 466 | $result = $this->client->query($query); |
||
| 467 | return $this->transformQueryTypesResults($result); |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Generates the concept scheme query. |
||
| 472 | * @param string $conceptscheme concept scheme URI |
||
| 473 | * @return string sparql query |
||
| 474 | */ |
||
| 475 | private function generateQueryConceptSchemeQuery($conceptscheme) { |
||
| 476 | $gc = $this->graphClause; |
||
| 477 | $query = <<<EOQ |
||
| 478 | CONSTRUCT { |
||
| 479 | <$conceptscheme> ?property ?value . |
||
| 480 | } WHERE { |
||
| 481 | $gc { |
||
| 482 | <$conceptscheme> ?property ?value . |
||
| 483 | FILTER (?property != skos:hasTopConcept) |
||
| 484 | } |
||
| 485 | } |
||
| 486 | EOQ; |
||
| 487 | return $query; |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Retrieves conceptScheme information from the endpoint. |
||
| 492 | * @param string $conceptscheme concept scheme URI |
||
| 493 | * @return EasyRDF_Graph query result graph |
||
| 494 | */ |
||
| 495 | public function queryConceptScheme($conceptscheme) { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Generates the queryConceptSchemes sparql query. |
||
| 502 | * @param string $lang language of labels |
||
| 503 | * @return string sparql query |
||
| 504 | */ |
||
| 505 | private function generateQueryConceptSchemesQuery($lang) { |
||
| 506 | $gc = $this->graphClause; |
||
| 507 | $query = <<<EOQ |
||
| 508 | SELECT ?cs ?label ?preflabel ?title |
||
| 509 | WHERE { |
||
| 510 | $gc { |
||
| 511 | ?cs a skos:ConceptScheme . |
||
| 512 | OPTIONAL { |
||
| 513 | ?cs rdfs:label ?label . |
||
| 514 | FILTER(langMatches(lang(?label), '$lang')) |
||
| 515 | } |
||
| 516 | OPTIONAL { |
||
| 517 | ?cs skos:prefLabel ?preflabel . |
||
| 518 | FILTER(langMatches(lang(?preflabel), '$lang')) |
||
| 519 | } |
||
| 520 | OPTIONAL { |
||
| 521 | { ?cs dc11:title ?title } |
||
| 522 | UNION |
||
| 523 | { ?cs dc:title ?title } |
||
| 524 | FILTER(langMatches(lang(?title), '$lang')) |
||
| 525 | } |
||
| 526 | } |
||
| 527 | } ORDER BY ?cs |
||
| 528 | EOQ; |
||
| 529 | return $query; |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Transforms the queryConceptScheme results into an array format. |
||
| 534 | * @param EasyRdf_Sparql_Result $result |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | private function transformQueryConceptSchemesResults($result) { |
||
| 538 | $ret = array(); |
||
| 539 | foreach ($result as $row) { |
||
| 540 | $conceptscheme = array(); |
||
| 541 | if (isset($row->label)) { |
||
| 542 | $conceptscheme['label'] = $row->label->getValue(); |
||
| 543 | } |
||
| 544 | |||
| 545 | if (isset($row->preflabel)) { |
||
| 546 | $conceptscheme['prefLabel'] = $row->preflabel->getValue(); |
||
| 547 | } |
||
| 548 | |||
| 549 | if (isset($row->title)) { |
||
| 550 | $conceptscheme['title'] = $row->title->getValue(); |
||
| 551 | } |
||
| 552 | |||
| 553 | $ret[$row->cs->getURI()] = $conceptscheme; |
||
| 554 | } |
||
| 555 | return $ret; |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * return a list of skos:ConceptScheme instances in the given graph |
||
| 560 | * @param string $lang language of labels |
||
| 561 | * @return array Array with concept scheme URIs (string) as keys and labels (string) as values |
||
| 562 | */ |
||
| 563 | public function queryConceptSchemes($lang) { |
||
| 564 | $query = $this->generateQueryConceptSchemesQuery($lang); |
||
| 565 | $result = $this->client->query($query); |
||
| 566 | return $this->transformQueryConceptSchemesResults($result); |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Generate a VALUES clause for limiting the targeted graphs. |
||
| 571 | * @param Vocabulary[] $vocabs the vocabularies to target |
||
| 572 | * @return string[] array of graph URIs |
||
| 573 | */ |
||
| 574 | protected function getVocabGraphs($vocabs) { |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Generate a VALUES clause for limiting the targeted graphs. |
||
| 588 | * @param array $vocabs array of Vocabulary objects to target |
||
| 589 | * @return string VALUES clause, or "" if not necessary to limit |
||
| 590 | */ |
||
| 591 | protected function formatValuesGraph($vocabs) { |
||
| 592 | if (!$this->isDefaultEndpoint()) { |
||
| 593 | return ""; |
||
| 594 | } |
||
| 595 | $graphs = $this->getVocabGraphs($vocabs); |
||
| 596 | return $this->formatValues('?graph', $graphs, 'uri'); |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Generate a FILTER clause for limiting the targeted graphs. |
||
| 601 | * @param array $vocabs array of Vocabulary objects to target |
||
| 602 | * @return string FILTER clause, or "" if not necessary to limit |
||
| 603 | */ |
||
| 604 | protected function formatFilterGraph($vocabs) { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Formats combined limit and offset clauses for the sparql query |
||
| 618 | * @param int $limit maximum number of hits to retrieve; 0 for unlimited |
||
| 619 | * @param int $offset offset of results to retrieve; 0 for beginning of list |
||
| 620 | * @return string sparql query clauses |
||
| 621 | */ |
||
| 622 | protected function formatLimitAndOffset($limit, $offset) { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Formats a sparql query clause for limiting the search to specific concept types. |
||
| 640 | * @param array $types limit search to concepts of the given type(s) |
||
| 641 | * @return string sparql query clause |
||
| 642 | */ |
||
| 643 | protected function formatTypes($types) { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @param string $lang language code of the returned labels |
||
| 657 | * @param array $fields extra fields to include in the result (array of strings). (default: null = none) |
||
| 658 | * @return string sparql query clause |
||
| 659 | */ |
||
| 660 | protected function formatBroader($lang, $fields) { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Generate condition for matching labels in SPARQL |
||
| 687 | * @param string $term search term |
||
| 688 | * @param string $search_lang language code used for matching labels (null means any language) |
||
| 689 | * @return string sparql query snippet |
||
| 690 | */ |
||
| 691 | protected function generateConceptSearchQueryCondition($term, $search_lang) |
||
| 720 | |||
| 721 | |||
| 722 | /** |
||
| 723 | * Inner query for concepts using a search term. |
||
| 724 | * @param string $term search term |
||
| 725 | * @param string $lang language code of the returned labels |
||
| 726 | * @param string $search_lang language code used for matching labels (null means any language) |
||
| 727 | * @param string[] $props properties to target e.g. array('skos:prefLabel','skos:altLabel') |
||
| 728 | * @param boolean $unique restrict results to unique concepts (default: false) |
||
| 729 | * @return string sparql query |
||
| 730 | */ |
||
| 731 | protected function generateConceptSearchQueryInner($term, $lang, $search_lang, $props, $unique) |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Query for concepts using a search term. |
||
| 778 | * @param array $fields extra fields to include in the result (array of strings). (default: null = none) |
||
| 779 | * @param boolean $unique restrict results to unique concepts (default: false) |
||
| 780 | * @param ConceptSearchParameters $params |
||
| 781 | * @return string sparql query |
||
| 782 | */ |
||
| 783 | protected function generateConceptSearchQuery($fields, $unique, $params) { |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Transform the concept search query results into the skosmos desired return format. |
||
| 853 | * @param EasyRdf_Sparql_Result $results |
||
| 854 | * @param array $vocabs array of Vocabulary objects to search; empty for global search |
||
| 855 | * @return array query result object |
||
| 856 | */ |
||
| 857 | private function transformConceptSearchResults($results, $vocabs) { |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Query for concepts using a search term. |
||
| 929 | * @param array $vocabs array of Vocabulary objects to search; empty for global search |
||
| 930 | * @param array $fields extra fields to include in the result (array of strings). (default: null = none) |
||
| 931 | * @param boolean $unique restrict results to unique concepts (default: false) |
||
| 932 | * @param ConceptSearchParameters $params |
||
| 933 | * @return array query result object |
||
| 934 | */ |
||
| 935 | public function queryConcepts($vocabs, $fields = null, $unique = false, $params) { |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Generates sparql query clauses used for creating the alphabetical index. |
||
| 943 | * @param string $letter the letter (or special class) to search for |
||
| 944 | * @return array of sparql query clause strings |
||
| 945 | */ |
||
| 946 | private function formatFilterConditions($letter) { |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Generates the sparql query used for rendering the alphabetical index. |
||
| 974 | * @param string $letter the letter (or special class) to search for |
||
| 975 | * @param string $lang language of labels |
||
| 976 | * @param integer $limit limits the amount of results |
||
| 977 | * @param integer $offset offsets the result set |
||
| 978 | * @param array $classes |
||
| 979 | * @return string sparql query |
||
| 980 | */ |
||
| 981 | protected function generateAlphabeticalListQuery($letter, $lang, $limit, $offset, $classes) { |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Transforms the alphabetical list query results into an array format. |
||
| 1026 | * @param EasyRdf_Sparql_Result $results |
||
| 1027 | * @return array |
||
| 1028 | */ |
||
| 1029 | private function transformAlphabeticalListResults($results) { |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Query for concepts with a term starting with the given letter. Also special classes '0-9' (digits), |
||
| 1059 | * '*!' (special characters) and '*' (everything) are accepted. |
||
| 1060 | * @param string $letter the letter (or special class) to search for |
||
| 1061 | * @param string $lang language of labels |
||
| 1062 | * @param integer $limit limits the amount of results |
||
| 1063 | * @param integer $offset offsets the result set |
||
| 1064 | * @param array $classes |
||
| 1065 | */ |
||
| 1066 | public function queryConceptsAlphabetical($letter, $lang, $limit = null, $offset = null, $classes = null) { |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Creates the query used for finding out which letters should be displayed in the alphabetical index. |
||
| 1074 | * @param string $lang language |
||
| 1075 | * @return string sparql query |
||
| 1076 | */ |
||
| 1077 | private function generateFirstCharactersQuery($lang, $classes) { |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * Transforms the first characters query results into an array format. |
||
| 1096 | * @param EasyRdf_Sparql_Result $result |
||
| 1097 | * @return array |
||
| 1098 | */ |
||
| 1099 | private function transformFirstCharactersResults($result) { |
||
| 1106 | |||
| 1107 | /** |
||
| 1108 | * Query for the first characters (letter or otherwise) of the labels in the particular language. |
||
| 1109 | * @param string $lang language |
||
| 1110 | * @return array array of characters |
||
| 1111 | */ |
||
| 1112 | public function queryFirstCharacters($lang, $classes = null) { |
||
| 1117 | |||
| 1118 | /** |
||
| 1119 | * @param string $uri |
||
| 1120 | * @param string $lang |
||
| 1121 | * @return string sparql query string |
||
| 1122 | */ |
||
| 1123 | private function generateLabelQuery($uri, $lang) { |
||
| 1152 | |||
| 1153 | /** |
||
| 1154 | * Query for a label (skos:prefLabel, rdfs:label, dc:title, dc11:title) of a resource. |
||
| 1155 | * @param string $uri |
||
| 1156 | * @param string $lang |
||
| 1157 | * @return array array of labels (key: lang, val: label), or null if resource doesn't exist |
||
| 1158 | */ |
||
| 1159 | public function queryLabel($uri, $lang) { |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Generates a sparql query for queryProperty. |
||
| 1183 | * @param string $uri |
||
| 1184 | * @param string $prop the name of the property eg. 'skos:broader'. |
||
| 1185 | * @param string $lang |
||
| 1186 | * @param boolean $anylang if you want a label even when it isn't available in the language you requested. |
||
| 1187 | * @return string sparql query |
||
| 1188 | */ |
||
| 1189 | View Code Duplication | private function generatePropertyQuery($uri, $prop, $lang, $anylang) { |
|
| 1215 | |||
| 1216 | /** |
||
| 1217 | * Transforms the sparql query result into an array or null if the concept doesn't exist. |
||
| 1218 | * @param EasyRdf_Sparql_Result $result |
||
| 1219 | * @param string $lang |
||
| 1220 | * @return array array of property values (key: URI, val: label), or null if concept doesn't exist |
||
| 1221 | */ |
||
| 1222 | private function transformPropertyQueryResults($result, $lang) { |
||
| 1247 | |||
| 1248 | /** |
||
| 1249 | * Query a single property of a concept. |
||
| 1250 | * @param string $uri |
||
| 1251 | * @param string $prop the name of the property eg. 'skos:broader'. |
||
| 1252 | * @param string $lang |
||
| 1253 | * @param boolean $anylang if you want a label even when it isn't available in the language you requested. |
||
| 1254 | * @return array array of property values (key: URI, val: label), or null if concept doesn't exist |
||
| 1255 | */ |
||
| 1256 | public function queryProperty($uri, $prop, $lang, $anylang = false) { |
||
| 1262 | |||
| 1263 | /** |
||
| 1264 | * Query a single transitive property of a concept. |
||
| 1265 | * @param string $uri |
||
| 1266 | * @param string $prop the name of the property eg. 'skos:broader'. |
||
| 1267 | * @param string $lang |
||
| 1268 | * @param integer $limit |
||
| 1269 | * @param boolean $anylang if you want a label even when it isn't available in the language you requested. |
||
| 1270 | * @return string sparql query |
||
| 1271 | */ |
||
| 1272 | private function generateTransitivePropertyQuery($uri, $prop, $lang, $limit, $anylang) { |
||
| 1303 | |||
| 1304 | /** |
||
| 1305 | * Transforms the sparql query result object into an array. |
||
| 1306 | * @param EasyRdf_Sparql_Result $result |
||
| 1307 | * @param string $lang |
||
| 1308 | * @param string $fallbacklang language to use if label is not available in the preferred language |
||
| 1309 | * @return array of property values (key: URI, val: label), or null if concept doesn't exist |
||
| 1310 | */ |
||
| 1311 | private function transformTransitivePropertyResults($result, $lang, $fallbacklang) { |
||
| 1357 | |||
| 1358 | /** |
||
| 1359 | * Query a single transitive property of a concept. |
||
| 1360 | * @param string $uri |
||
| 1361 | * @param string $prop the name of the property eg. 'skos:broader'. |
||
| 1362 | * @param string $lang |
||
| 1363 | * @param string $fallbacklang language to use if label is not available in the preferred language |
||
| 1364 | * @param integer $limit |
||
| 1365 | * @param boolean $anylang if you want a label even when it isn't available in the language you requested. |
||
| 1366 | * @return array array of property values (key: URI, val: label), or null if concept doesn't exist |
||
| 1367 | */ |
||
| 1368 | public function queryTransitiveProperty($uri, $prop, $lang, $limit, $anylang = false, $fallbacklang = '') { |
||
| 1373 | |||
| 1374 | /** |
||
| 1375 | * Generates the query for a concepts skos:narrowers. |
||
| 1376 | * @param string $uri |
||
| 1377 | * @param string $lang |
||
| 1378 | * @param string $fallback |
||
| 1379 | * @return string sparql query |
||
| 1380 | */ |
||
| 1381 | View Code Duplication | private function generateNarrowerQuery($uri, $lang, $fallback) { |
|
| 1411 | |||
| 1412 | /** |
||
| 1413 | * Transforms the sparql result object into an array. |
||
| 1414 | * @param EasyRdf_Sparql_Result $result |
||
| 1415 | * @param string $lang |
||
| 1416 | * @return array array of arrays describing each child concept, or null if concept doesn't exist |
||
| 1417 | */ |
||
| 1418 | private function transformNarrowerResults($result, $lang) { |
||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * Query the narrower concepts of a concept. |
||
| 1458 | * @param string $uri |
||
| 1459 | * @param string $lang |
||
| 1460 | * @param string $fallback |
||
| 1461 | * @return array array of arrays describing each child concept, or null if concept doesn't exist |
||
| 1462 | */ |
||
| 1463 | public function queryChildren($uri, $lang, $fallback) { |
||
| 1468 | |||
| 1469 | /** |
||
| 1470 | * Query the top concepts of a vocabulary. |
||
| 1471 | * @param string $conceptSchemes concept schemes whose top concepts to query for |
||
| 1472 | * @param string $lang language of labels |
||
| 1473 | */ |
||
| 1474 | public function queryTopConcepts($conceptSchemes, $lang) { |
||
| 1509 | |||
| 1510 | /** |
||
| 1511 | * Generates a sparql query for finding the hierarchy for a concept. |
||
| 1512 | * @param string $uri concept uri. |
||
| 1513 | * @param string $lang |
||
| 1514 | * @param string $fallback language to use if label is not available in the preferred language |
||
| 1515 | * @return string sparql query |
||
| 1516 | */ |
||
| 1517 | private function generateParentListQuery($uri, $lang, $fallback) { |
||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Transforms the result into an array. |
||
| 1568 | * @param EasyRdf_Sparql_Result |
||
| 1569 | * @param string $lang |
||
| 1570 | * @return an array for the REST controller to encode. |
||
| 1571 | */ |
||
| 1572 | private function transformParentListResults($result, $lang) { |
||
| 1644 | |||
| 1645 | /** |
||
| 1646 | * Query for finding the hierarchy for a concept. |
||
| 1647 | * @param string $uri concept uri. |
||
| 1648 | * @param string $lang |
||
| 1649 | * @param string $fallback language to use if label is not available in the preferred language |
||
| 1650 | * @return an array for the REST controller to encode. |
||
| 1651 | */ |
||
| 1652 | public function queryParentList($uri, $lang, $fallback) { |
||
| 1657 | |||
| 1658 | /** |
||
| 1659 | * return a list of concept group instances, sorted by label |
||
| 1660 | * @param string $groupClass URI of concept group class |
||
| 1661 | * @param string $lang language of labels to return |
||
| 1662 | * @return string sparql query |
||
| 1663 | */ |
||
| 1664 | private function generateConceptGroupsQuery($groupClass, $lang) { |
||
| 1685 | |||
| 1686 | /** |
||
| 1687 | * Transforms the sparql query result into an array. |
||
| 1688 | * @param EasyRdf_Sparql_Result $result |
||
| 1689 | * @return array |
||
| 1690 | */ |
||
| 1691 | private function transformConceptGroupsResults($result) { |
||
| 1719 | |||
| 1720 | /** |
||
| 1721 | * return a list of concept group instances, sorted by label |
||
| 1722 | * @param string $groupClass URI of concept group class |
||
| 1723 | * @param string $lang language of labels to return |
||
| 1724 | * @return array Result array with group URI as key and group label as value |
||
| 1725 | */ |
||
| 1726 | public function listConceptGroups($groupClass, $lang) { |
||
| 1731 | |||
| 1732 | /** |
||
| 1733 | * Generates the sparql query for listConceptGroupContents |
||
| 1734 | * @param string $groupClass URI of concept group class |
||
| 1735 | * @param string $group URI of the concept group instance |
||
| 1736 | * @param string $lang language of labels to return |
||
| 1737 | * @return string sparql query |
||
| 1738 | */ |
||
| 1739 | private function generateConceptGroupContentsQuery($groupClass, $group, $lang) { |
||
| 1761 | |||
| 1762 | /** |
||
| 1763 | * Transforms the sparql query result into an array. |
||
| 1764 | * @param EasyRdf_Sparql_Result $result |
||
| 1765 | * @param string $lang language of labels to return |
||
| 1766 | * @return array |
||
| 1767 | */ |
||
| 1768 | private function transformConceptGroupContentsResults($result, $lang) { |
||
| 1802 | |||
| 1803 | /** |
||
| 1804 | * return a list of concepts in a concept group |
||
| 1805 | * @param string $groupClass URI of concept group class |
||
| 1806 | * @param string $group URI of the concept group instance |
||
| 1807 | * @param string $lang language of labels to return |
||
| 1808 | * @return array Result array with concept URI as key and concept label as value |
||
| 1809 | */ |
||
| 1810 | public function listConceptGroupContents($groupClass, $group, $lang) { |
||
| 1815 | |||
| 1816 | /** |
||
| 1817 | * Generates the sparql query for queryChangeList. |
||
| 1818 | * @param string $lang language of labels to return. |
||
| 1819 | * @param int $offset offset of results to retrieve; 0 for beginning of list |
||
| 1820 | * @return string sparql query |
||
| 1821 | */ |
||
| 1822 | View Code Duplication | private function generateChangeListQuery($lang, $offset, $prop) { |
|
| 1841 | |||
| 1842 | /** |
||
| 1843 | * Transforms the sparql query result into an array. |
||
| 1844 | * @param EasyRdf_Sparql_Result $result |
||
| 1845 | * @return array |
||
| 1846 | */ |
||
| 1847 | View Code Duplication | private function transformChangeListResults($result) { |
|
| 1863 | |||
| 1864 | /** |
||
| 1865 | * return a list of recently changed or entirely new concepts |
||
| 1866 | * @param string $lang language of labels to return |
||
| 1867 | * @param int $offset offset of results to retrieve; 0 for beginning of list |
||
| 1868 | * @return array Result array |
||
| 1869 | */ |
||
| 1870 | public function queryChangeList($lang, $offset, $prop) { |
||
| 1875 | } |
||
| 1876 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.