Code Duplication    Length = 16-30 lines in 4 locations

model/sparql/GenericSparql.php 4 locations

@@ 93-108 (lines=16) @@
90
     * Generates the sparql query for retrieving concept and collection counts in a vocabulary.
91
     * @return string sparql query
92
     */
93
    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.
@@ 1189-1214 (lines=26) @@
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
    private function generatePropertyQuery($uri, $prop, $lang, $anylang) {
1190
        $gc = $this->graphClause;
1191
        $anylang = $anylang ? "OPTIONAL { ?object skos:prefLabel ?label }" : "";
1192
1193
        $query = <<<EOQ
1194
SELECT *
1195
WHERE {
1196
  $gc {
1197
    <$uri> a skos:Concept .
1198
    OPTIONAL {
1199
      <$uri> $prop ?object .
1200
      OPTIONAL {
1201
        ?object skos:prefLabel ?label .
1202
        FILTER (langMatches(lang(?label), "$lang"))
1203
      }
1204
      OPTIONAL {
1205
        ?object skos:prefLabel ?label .
1206
        FILTER (lang(?label) = "")
1207
      }
1208
      $anylang
1209
    }
1210
  }
1211
}
1212
EOQ;
1213
        return $query;
1214
    }
1215
1216
    /**
1217
     * Transforms the sparql query result into an array or null if the concept doesn't exist.
@@ 1381-1410 (lines=30) @@
1378
     * @param string $fallback
1379
     * @return string sparql query
1380
     */
1381
    private function generateNarrowerQuery($uri, $lang, $fallback) {
1382
        $uri = is_array($uri) ? $uri[0] : $uri;
1383
        $gc = $this->graphClause;
1384
        $query = <<<EOQ
1385
SELECT ?child ?label ?child ?grandchildren ?notation WHERE {
1386
  $gc {
1387
    <$uri> a skos:Concept .
1388
    OPTIONAL {
1389
      <$uri> skos:narrower ?child .
1390
      OPTIONAL {
1391
        ?child skos:prefLabel ?label .
1392
        FILTER (langMatches(lang(?label), "$lang"))
1393
      }
1394
      OPTIONAL {
1395
        ?child skos:prefLabel ?label .
1396
        FILTER (langMatches(lang(?label), "$fallback"))
1397
      }
1398
      OPTIONAL { # other language case
1399
        ?child skos:prefLabel ?label .
1400
      }
1401
      OPTIONAL {
1402
        ?child skos:notation ?notation .
1403
      }
1404
      BIND ( EXISTS { ?child skos:narrower ?a . } AS ?grandchildren )
1405
    }
1406
  }
1407
}
1408
EOQ;
1409
        return $query;
1410
    }
1411
1412
    /**
1413
     * Transforms the sparql result object into an array.
@@ 1822-1840 (lines=19) @@
1819
     * @param int $offset offset of results to retrieve; 0 for beginning of list
1820
     * @return string sparql query
1821
     */
1822
    private function generateChangeListQuery($lang, $offset, $prop) {
1823
        $gc = $this->graphClause;
1824
        $offset = ($offset) ? 'OFFSET ' . $offset : '';
1825
1826
        $query = <<<EOQ
1827
SELECT DISTINCT ?concept ?date ?label
1828
WHERE {
1829
  $gc {
1830
    ?concept a skos:Concept .
1831
    ?concept $prop ?date .
1832
    ?concept skos:prefLabel ?label .
1833
    FILTER (langMatches(lang(?label), '$lang'))
1834
  }
1835
}
1836
ORDER BY DESC(YEAR(?date)) DESC(MONTH(?date)) LCASE(?label)
1837
LIMIT 200 $offset
1838
EOQ;
1839
        return $query;
1840
    }
1841
1842
    /**
1843
     * Transforms the sparql query result into an array.