-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsparqlwriter.class.php
698 lines (621 loc) · 28.3 KB
/
sparqlwriter.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
<?php
require_once 'graphs/vocabularygraph.class.php';
class SparqlWriter {
private $_config;
private $_request;
var $_unknownPropertiesFromRequestParameter = array();
var $_unknownPropertiesFromConfig = array();
function __construct($config, $request){
$this->_config = $config;
$this->_request = $request;
}
function addPrefixesToQuery($query){
$prefixesString='';
$prefixes = $this->getConfigGraph()->getPrefixesFromLoadedTurtle();
preg_match_all('/([a-zA-Z\-]+)\:[a-zA-Z0-9_\-]+/', $query, $matches);
foreach($matches[1] as $prefix){
if(isset($prefixes[$prefix])){
$ns = $prefixes[$prefix];
$prefixesString.="PREFIX {$prefix}: <{$ns}>\n";
unset($prefixes[$prefix]);
}
}
return $prefixesString.$query;
}
function getLimit(){
$maxPageSize = $this->getConfigGraph()->getMaxPageSize();
$requestedPageSize = $this->_request->getParam('_pageSize');
$endpointDefaultPageSize = $this->getConfigGraph()->getEndpointDefaultPageSize();
$apiDefaultPageSize = $this->getConfigGraph()->getApiDefaultPageSize();
if($requestedPageSize > $maxPageSize) return $apiDefaultPageSize;
else if($requestedPageSize) return $requestedPageSize;
else if($endpointDefaultPageSize) return $endpointDefaultPageSize;
else if($apiDefaultPageSize) return $apiDefaultPageSize;
else return 10;
}
function getDefaultSelectLangs(){
$requestedDefaultLangs = $this->_request->getParam('_lang');
$endpointDefaultLangs = $this->getConfigGraph()->getEndpointDefaultLangs();
$apiDefaultLangs = $this->getConfigGraph()->getApiDefaultLangs();
if ($requestedDefaultLangs) return explode(',', $requestedDefaultLangs);
else if($endpointDefaultLangs) return explode(',', $endpointDefaultLangs);
else if($apiDefaultLangs) return explode(',', $apiDefaultLangs);
else return null;
}
function getSelectTemplate(){
if($select = $this->_request->getParam('_select')){
return $select;
} else {
return $this->getConfigGraph()->getSelectQuery();
}
}
function getExplicitSelectQuery(){
if($template = $this->getSelectTemplate()){
$bindings = $this->getConfigGraph()->getAllProcessedVariableBindings();
return $this->fillQueryTemplate($template, $bindings);
} else {
return false;
}
}
function variableBindingToSparqlTerm($props, $propertyUri=false){
if(isset($props['type']) AND $props['type'] == RDFS.'Resource'){
$sparqlVal = "<{$props['value']}>";
} else {
$sparqlVal = '"""'.$props['value'].'"""';
if(isset($props['lang'])){
$sparqlVal.='@'.$props['lang'];
} else if(isset($props['datatype'])){
$sparqlVal.='^^<'.$props['datatype'].'>';
} else if(isset($props['type'])){
$sparqlVal.='^^<'.$props['type'].'>';
} else {
$sparqlVal = $this->addDatatypeOrLangToLiteral($sparqlVal, $propertyUri);
$sparqlVal = $sparqlVal[0];
}
}
return $sparqlVal;
}
function filterValueToSparqlTerm($val, $langs, $propertyUri){
$varNames = $this->getConfigGraph()->variableNamesInValue($val);
$bindings = $this->getConfigGraph()->getAllProcessedVariableBindings();
if($varNames){
foreach($varNames as $varName){
if(isset($bindings[$varName])){
$binding = $bindings[$varName];
return array($this->variableBindingToSparqlTerm($binding, $propertyUri));
} else {
throw new ConfigGraphException("The variable {$varName} has no binding");
}
}
} else if($uri = $this->getConfigGraph()->getUriForVocabPropertyLabel($val)){
$namespaces = $this->getConfigGraph()->getPrefixesFromLoadedTurtle();
return array($this->qnameOrUri($uri, $namespaces));
} else {
$literal = '"""'.$val.'"""';
return $this->addDatatypeOrLangToLiteral($literal, $propertyUri, $langs);
}
}
function addDatatypeOrLangToLiteral($literal, $propertyUri=false, $langs=null){
if($propertyUri){
if($propertyRange = $this->getConfigGraph()->getVocabPropertyRange($propertyUri) AND $propertyRange!=RDFS_LITERAL){
$literal .= '^^<'.$propertyRange.'>';
return array($literal);
} else {
return $this->addLangToLiteral($literal, $langs);
}
} else {
return $this->addLangToLiteral($literal, $langs);
}
}
function addLangToLiteral($literal, $langs){
if ($langs){
$literals = array();
foreach($langs as $lang) {
$literals[] = $literal.'@'.$lang;
}
return $literals;
} else {
return array($literal);
}
}
function fillQueryTemplate($template, $bindings){
foreach($bindings as $name => $props){
$sparqlVal = $this->variableBindingToSparqlTerm($props);
$sparqlVar = '?'.$name;
logDebug("SPARQL Variable binding: {$sparqlVal} = {$sparqlVar}");
//replace all variables with values
//(but not variables that simply start with this variable name)
$template = preg_replace('/\\'.$sparqlVar.'[^_a-zA-Z0-9]/', $sparqlVal, $template); # the \ is to escape the ? and needs \\ because it is escape char in php ...
}
return $template;
}
function getGroupGraphPattern(){
$whereRequestParam = $this->_request->getParam('_where');
$selectorConfigWhereProperty = $this->getConfigGraph()->getSelectWhere();
$bindings = $this->getConfigGraph()->getAllProcessedVariableBindings();
$selectorConfigWhereProperty = $this->fillQueryTemplate($selectorConfigWhereProperty, $bindings);
if(!empty($whereRequestParam)) $whereRequestParam = '{'.$whereRequestParam.'}';
if(!empty($selectorConfigWhereProperty)) $selectorConfigWhereProperty = '{'.$selectorConfigWhereProperty.'}';
$GGP = "{$whereRequestParam}\n{$selectorConfigWhereProperty}\n ";
$filter = implode( '&', $this->getConfigGraph()->getAllFilters());
foreach($this->_request->getUnreservedParams() as $k => $v){
list($k, $v) = array(urlencode($k), urlencode($v));
$filter.="&{$k}={$v}";
}
logDebug("Filter is: {$filter}");
$params = queryStringToParams($filter);
$langs = array();
foreach($params as $k => $v) {
if (strpos($k, 'lang-') === 0) {
$langs[substr($k, 5)] = $v;
unset($params[$k]);
}
}
$GGP .= $this->paramsToSparql($params, $langs);
$GGP = trim($GGP);
if(empty($GGP)){
$GGP = "\n ?item ?property ?value .";
}
return $GGP;
}
function getGeneratedSelectQuery(){
$GroupGraphPattern = $this->getGroupGraphPattern();
$order = $this->getOrderBy();
$limit = $this->getLimit();
$offset = $this->getOffset();
$query = <<<_SPARQL_
SELECT DISTINCT ?item
WHERE {
{$GroupGraphPattern}
{$order['graphConditions']}
}
{$order['orderBy']}
LIMIT {$limit}
OFFSET {$offset}
_SPARQL_;
return $this->addPrefixesToQuery($query);
}
function paramsToSparql($paramsArray, $langArray=array()){
$sparql = '';
$filters = '';
$namespaces = $this->getConfigGraph()->getPrefixesFromLoadedTurtle();
$rdfsLabelQnameOrUri = $this->qnameOrUri(RDFS_LABEL, $namespaces);
$defaultLangs = $this->getDefaultSelectLangs();
foreach($paramsArray as $k => $v){
$prefix = $this->prefixFromParamName($k);
$propertiesList = $this->mapParamNameToProperties($k);
$propertyNames = array_keys($propertiesList);
$counter=0;
$name = $propertyNames[0];
$varName = $name;
$nextVarName = '';
$propUri = $propertiesList[$name];
$propQnameOrUri = $this->qnameOrUri($propUri, $namespaces);
$lastPropUri = array_pop(array_values($propertiesList));
$langs = array_key_exists($k, $langArray) ? array($langArray[$k]) : $defaultLangs;
$processedFilterValues = $this->filterValueToSparqlTerm($v, $langs, $lastPropUri);
$nValues = count($processedFilterValues);
if(count($propertyNames) > 1){
$sparql.= "\n ?item {$propQnameOrUri} ?{$name} . ";
}
foreach($propertiesList as $name => $propUri){
if(isset($propertyNames[$counter+1]) OR count($propertyNames)==1){ //if this ISN'T the last property or is the only property
if(count($propertyNames)==1){
$varName = 'item';
$nextName = $propertyNames[0];
$nextVarName = $nextName;
} else {
$nextName = $propertyNames[$counter+1];
$nextVarName = $varName.'_'.$nextName;
}
$nextProp = $propertiesList[$nextName];
$nextPropQnameOrUri = $this->qnameOrUri($nextProp, $namespaces);
//need to cast $nextVarName to compare it with $processedFilterValue
$castNextVarName = $this->castOrderByVariable($nextVarName, $nextProp);
if ( (($counter+2) == count($propertyNames) OR count($propertyNames)==1)){ //if last item or only item
if (!$prefix) {
if ($nValues > 1) {
foreach($processedFilterValues as $position => $processedFilterValue) {
if ($position) {
$sparql .= "\n UNION";
}
$sparql.="\n { ?{$varName} {$nextPropQnameOrUri} {$processedFilterValue} . }";
}
} else {
$processedFilterValue = $processedFilterValues[0];
$sparql .= "\n ?{$varName} {$nextPropQnameOrUri} {$processedFilterValue} . ";
}
} else if($prefix=='min') {
$sparql.="\n ?{$varName} {$nextPropQnameOrUri} ?{$nextVarName} . \n FILTER (?{$nextVarName} >= {$processedFilterValues[0]})";
} else if($prefix=='max') {
$sparql.="\n ?{$varName} {$nextPropQnameOrUri} ?{$nextVarName} . \n FILTER (?{$nextVarName} <= {$processedFilterValues[0]})";
} else if($prefix == 'minEx') {
$sparql.="\n ?{$varName} {$nextPropQnameOrUri} ?{$nextVarName} . \n FILTER (?{$nextVarName} > {$processedFilterValues[0]})";
} else if($prefix == 'maxEx') {
$sparql.="\n ?{$varName} {$nextPropQnameOrUri} ?{$nextVarName} . \n FILTER (?{$nextVarName} < {$processedFilterValues[0]})";
} else if($prefix == 'name') {
$sparql.="\n ?{$varName} {$nextPropQnameOrUri} ?{$nextVarName} .\n";
foreach($processedFilterValues as $position => $processedFilterValue) {
if ($nValues > 1) {
$sparql.="\n {";
}
$sparql.="\n ?{$nextVarName} {$rdfsLabelQnameOrUri} {$processedFilterValue} . ";
if ($nValues > 1) {
$sparql.="\n } ";
if ($position + 1 < $nValues) {
$sparql.="\n UNION ";
}
}
}
} else if($prefix == 'exists') {
if($v=="true"){
$sparql.="\n ?{$varName} {$nextPropQnameOrUri} [] . ";
} else {
$sparql.="\n OPTIONAL { \n ?{$varName} {$nextPropQnameOrUri} ?{$nextVarName} . \n } \n FILTER (!bound(?{$nextVarName})) ";
}
}
} else {
$sparql.="\n ?{$varName} {$nextPropQnameOrUri} ?{$nextVarName} . ";
}
$varName = $nextVarName;
}
$counter++;
}
}
return $sparql;
}
function qnameOrUri($uri, $prefixes) {
$hash = strpos($uri, '#');
if (!$hash) {
$parts = explode('/', $uri);
$localPart = $parts[count($parts) - 1];
$namespace = substr($uri, 0, strlen($uri) - strlen($localPart));
} else {
$localPart = substr($uri, $hash + 1);
$namespace = substr($uri, 0, $hash + 1);
}
foreach ($prefixes as $prefix=>$ns) {
if ($ns == $namespace) {
return $prefix.':'.$localPart;
}
}
return '<'.$uri.'>';
}
function paramNameToPropertyNames($name){
#remove min-/max-
$nameArray = $this->splitPrefixAndName($name);
$name = $nameArray['name'];
#split on dot
$splitNames = explode('.', $name);
return $splitNames;
}
function mapParamNameToProperties($name){
$splitNames = $this->paramNameToPropertyNames($name);
$list = array();
foreach($splitNames as $sn){
$uri = $this->getConfigGraph()->getUriForVocabPropertyLabel($sn);
$list[$sn] = $uri;
}
return $list;
}
function splitPrefixAndName($name){
$prefixes = array('min', 'max', 'minEx', 'maxEx', 'name', 'exists', 'lang', 'true', 'false');
foreach($prefixes as $prefix){
if(strpos($name, $prefix.'-')===0){
$name = substr($name, strlen($prefix.'-'));
return array(
'name' => $name,
'prefix' => $prefix,
);
}
}
return array('name' => $name, 'prefix' => false);
}
function prefixFromParamName($name){
$a = $this->splitPrefixAndName($name);
return $a['prefix'];
}
function getOffset(){
$pageNo = $this->_request->getPage();
return ($pageNo - 1 ) * $this->getLimit();
}
function getUnknownPropertiesFromRequest(){
if($this->hasUnknownPropertiesFromRequest()){
return $this->_unknownPropertiesFromRequestParameter;
} else {
return false;
}
}
function getUnknownPropertiesFromConfig(){
if($this->hasUnknownPropertiesFromConfig()){
return $this->_unknownPropertiesFromConfig;
} else {
return false;
}
}
function hasUnknownPropertiesFromRequest(){
if(!empty($this->_unknownPropertiesFromRequestParameter)){
return true;
}
foreach($this->_request->getUnreservedParams() as $k => $v){
$propertyNames = $this->paramNameToPropertyNames($k);
$propertyNamesWithUris = $this->mapParamNameToProperties($k);
foreach($propertyNames as $pn){
if(empty($propertyNamesWithUris[$pn])){
$this->_unknownPropertiesFromRequestParameter[]=$pn;
}
}
}
try{
$chain = $this->getConfigGraph()->getRequestPropertyChainArray();
} catch (UnknownPropertyException $e){
$this->_unknownPropertiesFromRequestParameter[]=$e->getMessage();
}
if(!empty($this->_unknownPropertiesFromRequestParameter)){
return true;
}
return false;
}
function hasUnknownPropertiesFromConfig($viewerUri=false){
if(!empty($this->_unknownPropertiesFromConfig)){
return true;
}
$filters = $this->getConfigGraph()->getAllFilters();
foreach($filters as $filter){
$paramsArray = queryStringToParams($filter);
foreach(array_keys($paramsArray) as $paramName){
$propertyNames = $this->paramNameToPropertyNames($paramName);
$propertyNamesWithUris = $this->mapParamNameToProperties($paramName);
foreach($propertyNames as $pn){
if(empty($propertyNamesWithUris[$pn])){
$this->_unknownPropertiesFromConfig[]=$pn;
}
}
}
}
if($viewerUri){
try{
$chain = $this->getConfigGraph()->getViewerDisplayPropertiesValueAsPropertyChainArray($viewerUri);
} catch (Exception $e){
$this->_unknownPropertiesFromConfig[]=$e->getMessage();
}
}
if(!empty($this->_unknownPropertiesFromConfig)){
return true;
}
return false;
}
function getOrderBy(){
$graphConditions = false;
$orderBy = false;
if($orderByRequestParam = $this->_request->getParam('_orderBy')){
$orderBy = 'ORDER BY '.$orderByRequestParam;
} else if($sort = $this->_request->getParam('_sort')){
return $this->sortToOrderBy($sort, 'request');
} else if($orderByConfig = $this->getConfigGraph()->getOrderBy()){
$orderBy = 'ORDER BY '.$orderByConfig;
} else if($sort = $this->getConfigGraph()->getSort()){
return $this->sortToOrderBy($sort, 'config');
}
return array(
'graphConditions' => $graphConditions,
'orderBy' => $orderBy,
);
}
function sortToOrderBy($sort, $source){
$sortPropNames = explode(',',$sort);
$propertyLists = array();
foreach($sortPropNames as $sortName){
$ascOrDesc = ($sortName[0]=='-')? 'DESC' : 'ASC';
$sortName = ltrim($sortName, '-');
$propertyLists[]= array(
'sort-order' => $ascOrDesc,
'property-list'=> $this->mapParamNameToProperties($sortName),
);
}
foreach($propertyLists as $propertyList){
$properties = $propertyList['property-list'];
foreach($properties as $name => $uri){
if(empty($uri)){
if($source == 'request') $this->_unknownPropertiesFromRequestParameter[]=$name;
else if($source == 'config') $this->_unknownPropertiesFromConfig[]=$name;
else throw new Exception("source parameter for sortToOrderBy must be 'request' or 'config'");
}
}
}
return $this->propertyNameListToOrderBySparql($propertyLists);
}
function propertyNameListToOrderBySparql($propertyLists){
$namespaces = $this->getConfigGraph()->getPrefixesFromLoadedTurtle();
$sparql = '';
$orderBy = "ORDER BY ";
$variableNames = array();
foreach($propertyLists as $propertiesListHash){
$propertiesList = $propertiesListHash['property-list'];
$sortOrder = $propertiesListHash['sort-order'];
$propertyNames = array_keys($propertiesList);
$counter=0;
$name = $propertyNames[0];
$varName = $name;
$propUri = $propertiesList[$name];
$propQnameOrUri = $this->qnameOrUri($propUri, $namespaces);
$sparql.= "\n ?item {$propQnameOrUri} ?{$name} .";
$variableNames[$name] = $propUri;
foreach($propertiesList as $name => $propUri){
if(isset($propertyNames[$counter+1])){ //if this ISN'T the last property
$nextName = $propertyNames[$counter+1];
} else if (count($propertyNames) ==1){
$orderBy.= $sortOrder.'(?'.$name.') ';
$varName = 'item';
$nextName = $propertyNames[0];
}
$nextProp = $propertiesList[$nextName];
$nextPropQnameOrUri = $this->qnameOrUri($nextProp, $namespaces);
$nextVarName = $varName.'_'.$nextName;
if ( ($counter+1) < count($propertyNames) ){ //if not last item
$sparql.="\n ?{$varName} {$nextPropQnameOrUri} ?{$nextVarName} .";
$variableNames[$nextVarName] = $nextProp;
}
if ( ($counter+2) == count($propertyNames) ){
//if this is the last property in the chain, add to the order by
$orderBy.= $sortOrder.'(?'.$nextVarName.') ';
}
$varName = $nextVarName;
$counter++;
}
}
return array('graphConditions' => $sparql, 'orderBy' => $orderBy);
}
function getSelectQueryForUriList(){
if($query = $this->getExplicitSelectQuery()){
return $this->addPrefixesToQuery($query);
} else {
return $this->getGeneratedSelectQuery();
}
}
function castOrderByVariable($varName, $propertyUri){
$xsdDatatypes = array(
XSD."integer" ,
XSD."int" ,
XSD."decimal" ,
XSD."float" ,
XSD."double" ,
XSD."string" ,
XSD."boolean" ,
XSD."dateTime" ,
);
if($propertyRange = $this->getConfigGraph()->getVocabPropertyRange($propertyUri) AND in_array($propertyRange, $xsdDatatypes)){
return "<{$propertyRange}>(?{$varName})";
} else {
return "?{$varName}";
}
}
function getViewQueryForUri($uri, $viewerUri){
return $this->getViewQueryForUriList(array($uri), $viewerUri);
}
function getViewQueryForUriList($uriList, $viewerUri){
if(($template = $this->_request->getParam('_template') OR $template = $this->_config->getViewerTemplate($viewerUri)) AND !empty($template)){
$uriSetFilter = "FILTER( ?item = <http://puelia.example.org/fake-uri/x> ";
foreach($uriList as $describeUri){
$uriSetFilter.= "|| ?item = <{$describeUri}> \n";
}
$uriSetFilter.= ")\n";
return $this->addPrefixesToQuery("CONSTRUCT { {$template} } WHERE { {$template} {$uriSetFilter} }");
/*
FILTER doesn't work so well with all triplestores, could do it by adding incrementers to every variable in the pattern which increment for ever loop of the URI list. If do so, it would be good to change the propertypath->sparql code to map to a plain pattern which is then passed to the same code as this is, to add the incrementers
*/
} else {
$namespaces = $this->getConfigGraph()->getPrefixesFromLoadedTurtle();
$conditionsGraph = '';
$whereGraph = '';
$chains = $this->getViewerPropertyChains($viewerUri);
$props = array();
foreach($chains as $chain) {
$props = $this->mapPropertyChainToStructure($chain, $props);
}
foreach ($uriList as $position => $uri) {
if ($position) {
$whereGraph .= " UNION\n";
}
$conditionsGraph .= "\n # constructing properties of {$uri} \n";
$whereGraph .= "\n # identifying properties of {$uri} \n";
$counter = 0;
foreach ($props as $prop => $substruct) {
if ($counter) {
$whereGraph .= "UNION {\n";
} else {
$whereGraph .= " {\n";
}
$propvar = $substruct['var'] . '_' . $position;
if ($prop == API.'allProperties') {
$triple = " <{$uri}> {$propvar}_prop {$propvar} .\n";
} else {
$propQnameOrUri = $this->qnameOrUri($prop, $namespaces);
$triple = " <{$uri}> {$propQnameOrUri} {$propvar} .\n";
}
$whereGraph .= $triple;
$conditionsGraph .= $triple;
if (array_key_exists('props', $substruct)) {
$whereGraph .= $this->mapPropertyStructureToWhereGraph($substruct, $position, $namespaces);
$conditionsGraph .= $this->mapPropertyStructureToConstructGraph($substruct, $position, $namespaces);
}
$whereGraph .= " } ";
$counter += 1;
}
}
return $this->addPrefixesToQuery("CONSTRUCT { {$conditionsGraph}} WHERE { {$whereGraph}\n}\n");
}
}
function mapPropertyStructureToWhereGraph($structure, $uriPosition, $namespaces) {
$var = $structure['var'];
$props = $structure['props'];
$graph = '';
foreach($props as $prop => $substruct) {
$propvar = $substruct['var'] . '_' . $uriPosition;
if ($prop == API.'allProperties') {
$graph .= " OPTIONAL { {$var}_{$uriPosition} {$propvar}_prop {$propvar} .";
} else {
$propQnameOrUri = $this->qnameOrUri($prop, $namespaces);
$graph .= " OPTIONAL { {$var}_{$uriPosition} {$propQnameOrUri} {$propvar} .";
}
if (array_key_exists('props', $substruct)) {
$graph .= $this->mapPropertyStructureToWhereGraph($substruct, $uriPosition, $namespaces);
}
$graph .= " }\n";
}
return $graph;
}
function mapPropertyStructureToConstructGraph($structure, $uriPosition, $namespaces) {
$var = $structure['var'];
$props = $structure['props'];
$graph = '';
foreach($props as $prop => $substruct) {
$propvar = $substruct['var'] . '_' . $uriPosition;
if ($prop == API.'allProperties') {
$graph .= " {$var}_{$uriPosition} {$propvar}_prop {$propvar} .\n";
} else {
$propQnameOrUri = $this->qnameOrUri($prop, $namespaces);
$graph .= " {$var}_{$uriPosition} {$propQnameOrUri} {$propvar} .\n";
}
if (array_key_exists('props', $substruct)) {
$graph .= $this->mapPropertyStructureToConstructGraph($substruct, $uriPosition, $namespaces);
}
}
return $graph;
}
/*
Creating a structure that looks like:
array(
"var" => "?s",
"props" => array(
rdfs:label => array("var" => "?var_1"),
org:reportsTo => array(
"var" => "?var_2",
"props" => array(
rdfs:label => array("var" => "?var_2_1")
)
)
)
)
*/
function mapPropertyChainToStructure($chain, $structure, $varbase = '?var') {
$prop = array_shift($chain);
if (array_key_exists($prop, $structure)) {
$varbase = $structure[$prop]['var'];
} else {
$varbase = $varbase . '_' . (count($structure) + 1);
$structure[$prop] = array('var' => $varbase, 'props' => array());
}
if (count($chain) != 0) {
$structure[$prop]['props'] = $this->mapPropertyChainToStructure($chain, $structure[$prop]['props'], $varbase);
}
return $structure;
}
function getViewerPropertyChains($viewerUri){
return array_merge($this->getConfigGraph()->getRequestPropertyChainArray(), $this->getConfigGraph()->getViewerDisplayPropertiesValueAsPropertyChainArray($viewerUri), $this->getConfigGraph()->getAllViewerPropertyChains($viewerUri));
}
function getConfigGraph(){
return $this->_config;
}
}
?>