This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Instances.php
1668 lines (1479 loc) · 56.7 KB
/
Instances.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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* OntoWiki resource list model class.
*
* Represents a list of resources (specified by filters) and their properties.
*
* This file is part of the {@link http://ontowiki.net OntoWiki} project.
*
* @category OntoWiki
* @package Model
* @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
* @author Norman Heino <norman.heino@gmail.com>
* @author Jonas Brekle <jonas.brekle@gmail.com>
*/
class OntoWiki_Model_Instances extends OntoWiki_Model
{
//all triple (?s ?p ?o). is added and removed to the resorceQuery on demand, but always stored here
protected $allTriple;
/**
* Properties whose values are to be fetched for each resource.
* @var array
*/
protected $_shownProperties = array();
protected $_shownPropertiesConverted;
protected $_shownPropertiesConvertedUptodate = false;
protected $_ignoredShownProperties = array(
//EF_RDF_TYPE
);
/**
* values of the set properties for all resources
*/
protected $_values;
protected $_valuesUptodate = false;
protected $_valueQueryUptodate = false;
/**
* all resources
*/
protected $_resources;
protected $_resourcesUptodate = false;
/**
*
* @var array transformed
*/
protected $_resourcesConverted;
protected $_resourcesConvertedUptodate = false;
/**
*
* @var Erfurt_Sparql_Query2_Var the var that is used in the resourcequery to bind all uri of list elements
*/
protected $_resourceVar = null;
/**
* @var array stores all configured filters
*/
protected $_filter = array();
/**
* Result array - what comes back when evaluating the query.
* @var array
*/
protected $_results = null;
protected $_resultsUptodate = false;
/**
* @var Erfurt_Sparql_Query2 the manged query that selects the resources in the list
*/
protected $_resourceQuery = null;
protected $_sortTriple = null;
/**
* @var Erfurt_Sparql_Query2
*/
protected $_valueQuery = null;
protected $_valueQueryResourceFilter = null;
protected $_useCache = true;
/**
* Constructor
*/
public function __construct (Erfurt_Store $store, Erfurt_Rdf_Model $model, $options = array())
{
parent::__construct($store, $model);
if(isset($options[STORE_USE_CACHE])){
$this->_useCache = $options[STORE_USE_CACHE];
}
$this->_defaultUrl['resource'] = new OntoWiki_Url(array('route' => 'properties'), array());
$this->_defaultUrlParam['resource'] = 'r';
$this->_resourceQuery = new Erfurt_Sparql_Query2();
$this->_resourceVar = new Erfurt_Sparql_Query2_Var("resourceUri");
$this->allTriple = new Erfurt_Sparql_Query2_Triple($this->_resourceVar, new Erfurt_Sparql_Query2_Var("p"), new Erfurt_Sparql_Query2_Var("o"));
$this->addAllTriple();
//show resource uri
$this->_resourceQuery->addProjectionVar($this->_resourceVar);
$this->_resourceQuery
->setLimit(10) //per default query only for 10 resources
->setDistinct(true);
//->getOrder()
// ->add($this->_resourceVar);
// when resourceVar is the object - prevent literals
$this->_resourceQuery->addFilter(
new Erfurt_Sparql_Query2_ConditionalAndExpression(
array(
//new Erfurt_Sparql_Query2_isUri($this->_resourceVar),
new Erfurt_Sparql_Query2_UnaryExpressionNot(
new Erfurt_Sparql_Query2_isBlank($this->_resourceVar)
)
)
)
);
//build value query
$this->_valueQuery = new Erfurt_Sparql_Query2();
$this->_valueQuery
->addProjectionVar($this->_resourceVar)
->setDistinct(true);
//always query for type (not optional)
$typeVar = new Erfurt_Sparql_Query2_Var('__TYPE');
$optional = new Erfurt_Sparql_Query2_OptionalGraphPattern();
$optional->addTriple(
$this->_resourceVar,
new Erfurt_Sparql_Query2_IriRef(EF_RDF_TYPE),
$typeVar
);
$this->_valueQuery->addElement($optional);
$this->_valueQuery->addProjectionVar($typeVar);
//set froms to the requested graph
$this->_valueQuery->addFrom((string)$model);
$this->_resourceQuery->addFrom((string)$model);
//$this->updateValueQuery();
}
function __wakeUp(){
$this->_lang = NULL;
}
private function _getLanguage () {
if ($this->_lang == NULL) {
$this->_lang = OntoWiki::getInstance()->config->languages->locale;
}
return $this->_lang;
}
/**
*
* Method for setting the store explicitely
* (necessary after unserialization from session for unavailable _dbconn
* resource handles etc.)
* @param Erfurt_Store $store
* @throws Exception for parameter type being incorrect (no Erfurt_Store Object)
* @return null
*/
public function setStore($store) {
if ($store instanceof Erfurt_Store) {
$this->_store = $store;
} else {
throw new Exception('No Store Object in ' . __CLASS__ . '::' . __METHOD__);
}
}
/**
* dont keep the references to the query objects in $this->resourceQuery (the must be cloned too)
*/
public function __clone(){
foreach ($this as $key => $val) {
if (is_object($val)||(is_array($val))) {
$this->{$key} = unserialize(serialize($val));
//$this->$key= clone($this->$key);
}
}
}
/**
* redirect calls that ant be handled both to the resource and value query. currently only methods regarding the from are allowed
* @param string $name
* @param array $arguments
*/
public function __call($name, $arguments) {
$allowedMethods = array("addFrom","addFroms","removeFrom","removeFroms","hasFrom","getFrom","getFroms","setFrom", "setFroms");
if(in_array($name, $allowedMethods)){
call_user_func(array($this->_valueQuery, $name), $arguments);
call_user_func(array($this->_resourceQuery, $name), $arguments);
}
}
/**
* add ?resourceUri ?p ?o to the resource query
* TODO: support objects as resources? optionally?
*/
public function addAllTriple(){
$this->_resourceQuery->addElement($this->allTriple);
}
/**
* Adds a property to the properties fetched for every resource.
*
* @param $propertyUri The URI of the property
* @param $propertyName Name to be used for the variable
* @return OntoWiki_Model_Instances
*/
public function addShownProperty ($propertyUri, $propertyName = null, $inverse = false, $datatype = null, $hidden = false)
{
if (in_array($propertyUri, $this->_ignoredShownProperties)) {
return $this; //no action
}
if (!$propertyName) {
$propertyName = preg_replace('/^.*[#\/]/', '', $propertyUri);
$propertyName = str_replace('-', '', $propertyName);
}
$used = false;
foreach ($this->_shownProperties as $shownProp) {
if ($shownProp['name'] == $propertyName) {
$used = true;
}
}
//solve duplicate name problem by adding counter
if ($used) {
$counter = 2;
while ($used) {
$name = $propertyName . $counter++;
$used = false;
foreach ($this->_shownProperties as $shownProp) {
if ($shownProp['name'] == $name){
$used = true;
}
}
}
$propertyName = $name;
}
$ret = Erfurt_Sparql_Query2_Abstraction_ClassNode::addShownPropertyHelper(
$this->_valueQuery,
$this->_resourceVar,
$propertyUri,
$propertyName,
$inverse
);
$this->_shownProperties[$propertyUri.'-'.($inverse?"inverse":"direct")] = array(
'uri' => $propertyUri,
'name' => $propertyName,
'inverse' => $inverse,
'datatype' => $datatype,
'varName' => $ret['var']->getName(),
'var' => $ret['var'],
'optionalpart' => $ret['optional'],
'filter' => $ret['filter'],
'hidden' => $hidden
);
$this->_valuesUptodate = false; // getValues will not use the cache next time
$this->_resultsUptodate = false;
return $this;
}
/**
* add a shown property, that is more complex.
* provide own triples, they will wrapped in an optional and a projection var will be added.
* @param array $triples
* @param Erfurt_Sparql_Query2_Var $var
* @param boolean $hidden
* @return OntoWiki_Model_Instances
*/
public function addShownPropertyCustom($triples, $var, $hidden = false){
//add
$optional = new Erfurt_Sparql_Query2_OptionalGraphPattern();
$optional->addElements($triples);
$this->_valueQuery->getWhere()->addElement($optional);
$this->_valueQuery->addProjectionVar($var);
//save
$this->_shownProperties['custom'.count($this->_shownProperties)] = array(
'uri' => null,
'name' => 'custom'.count($this->_shownProperties),
'inverse' => false,
'datatype' => null,
'varName' => $var->getName(),
'var' => $var,
'optionalpart' => $triples,
'filter' => array(),
'hidden' => $hidden
);
$this->_valuesUptodate = false; // getValues will not use the cache next time
$this->_resultsUptodate = false;
return $this;
}
/**
* remove a property that has been added before
* @param string $key the uri
*/
public function removeShownProperty($property, $inverse){
$key = $property.'-'.($inverse?"inverse":"direct");
if(isset($this->_shownProperties[$key])){
$prop = $this->_shownProperties[$key];
$this->_valueQuery->removeProjectionVar($prop['var']);
$prop['optionalpart']->remove($this->_resourceQuery);
//$prop['filter']->remove($this->_resourceQuery);
unset($this->_shownProperties[$key]);
}
}
/**
* queries for values (unconverted)
* @return array
*/
public function getResults ()
{
$this->updateValueQuery();
if (!$this->_resultsUptodate) {
$this->_results = $this->_store->sparqlQuery(
$this->_valueQuery,
array(STORE_RESULTFORMAT => STORE_RESULTFORMAT_EXTENDED, STORE_USE_CACHE => $this->_useCache)
);
$this->_resultsUptodate = true;
}
return $this->_results;
}
/**
* add a filter from the filter box - these filters match some predefined schemes
* (like "equals", "contains")
* @param string $id
* @param string $property
* @param boolean $isInverse
* @param string $propertyLabel
* @param string $filter
* @param string $value1
* @param string $value2
* @param string $valuetype
* @param string $literaltype
* @return string id
*/
public function addFilter ($property, $isInverse, $propertyLabel, $filter, $value1 = null, $value2 = null, $valuetype = 'literal', $literaltype = null, $hidden = false, $id = null, $negate = false, $optional = false)
{
if($id == null){
$id = "box" . count($this->_filter);
} else {
if(isset($this->_filter[$id])){
$this->removeFilter($id);
}
}
$prop = new Erfurt_Sparql_Query2_IriRef($property);
if(!empty($value1)){
switch($valuetype) {
case 'uri':
$value1_obj = new Erfurt_Sparql_Query2_IriRef($value1);
if (!empty($value2)){
$value2_obj = new Erfurt_Sparql_Query2_IriRef($value2);
}
break;
case 'literal':
if (!empty($literaltype)) {
//with language tags
$value1_obj = new Erfurt_Sparql_Query2_RDFLiteral(
$value1,
$literaltype
);
if (!empty($value2)){
$value2_obj = new Erfurt_Sparql_Query2_RDFLiteral(
$value2,
$literaltype);
}
} else {
//no language tags
$value1_obj = new Erfurt_Sparql_Query2_RDFLiteral($value1);
if (!empty($value2)){
$value2_obj = new Erfurt_Sparql_Query2_RDFLiteral($value2);
}
}
break;
case 'typed-literal':
if (in_array($literaltype, Erfurt_Sparql_Query2_RDFLiteral::$knownShortcuts)) {
//is something like "bool" or "int" - will be converted from "1"^^xsd:int to 1
$value1_obj = new Erfurt_Sparql_Query2_RDFLiteral($value1, $literaltype);
if (!empty($value2)){
$value2_obj =
new Erfurt_Sparql_Query2_RDFLiteral($value2, $literaltype);
}
} else {
// is a uri
$value1_obj = new Erfurt_Sparql_Query2_RDFLiteral($value1, new Erfurt_Sparql_Query2_IriRef($literaltype));
if (!empty($value2)){
$value2_obj = new Erfurt_Sparql_Query2_RDFLiteral(
$value2,
new Erfurt_Sparql_Query2_IriRef($literaltype)
);
}
}
break;
default:
throw new RuntimeException(
'called Ontowiki_Model_Instances::addFilter with '.
'unknown param-value: valuetype = "'.$valuetype.'"'
);
break;
}
}
switch($filter) {
case 'contains':
$var = new Erfurt_Sparql_Query2_Var($propertyLabel);
if (!$isInverse) {
$triple = $this->_resourceQuery->addTriple(
$this->_resourceVar,
$prop,
$var
);
} else {
$triple = $this->_resourceQuery->addTriple(
$var,
$prop,
$this->_resourceVar
);
}
$value1_obj->setValue(str_replace("\\", "\\\\", preg_quote($value1_obj->getValue())));
$filterObj = $this->_resourceQuery->addFilter(
!$negate ?
new Erfurt_Sparql_Query2_Regex(
new Erfurt_Sparql_Query2_Str($var),
$value1_obj
)
:
new Erfurt_Sparql_Query2_UnaryExpressionNot(
new Erfurt_Sparql_Query2_Regex(
new Erfurt_Sparql_Query2_Str($var),
$value1_obj
)
)
);
break;
case 'equals':
if ($valuetype=="literal") {
$valueVar = new Erfurt_Sparql_Query2_Var($propertyLabel);
if (!$isInverse) {
$triple = new Erfurt_Sparql_Query2_Triple(
$this->_resourceVar,
$prop,
$valueVar
);
} else {
throw new RuntimeException(
'literal as value for an inverse property '.
'is a literal subject which is not allowed');
}
if ($negate) {
$optional = new Erfurt_Sparql_Query2_OptionalGraphPattern();
$optional->addElement($triple);
$this->_resourceQuery->addElement($optional);
$triple = $optional;
if ($optional) {
$orExpression = new Erfurt_Sparql_Query2_ConditionalOrExpression();
$orExpression->addElement(new Erfurt_Sparql_Query2_UnaryExpressionNot(
new Erfurt_Sparql_Query2_bound($valueVar)
));
$orExpression->addElement(new Erfurt_Sparql_Query2_NotEquals($valueVar, $value1_obj));
$filterObj = $this->_resourceQuery->addFilter($orExpression);
} else {
$filterObj = $this->_resourceQuery->addFilter(
new Erfurt_Sparql_Query2_NotEquals($valueVar, $value1_obj)
);
}
} else {
$this->_resourceQuery->addTriple($triple);
$filterObj = $this->_resourceQuery->addFilter(
new Erfurt_Sparql_Query2_Regex(
new Erfurt_Sparql_Query2_Str($valueVar),
new Erfurt_Sparql_Query2_RDFLiteral(
'^'.str_replace("\\", "\\\\", preg_quote($value1)).'$'
)
)
);
}
} else {
if (!$isInverse) {
$triple = $this->_resourceQuery->addTriple(
$this->_resourceVar,
$prop,
$value1_obj
);
} else {
$triple = $this->_resourceQuery->addTriple(
$value1_obj,
$prop,
$this->_resourceVar
);
}
}
break;
case 'larger':
$var = new Erfurt_Sparql_Query2_Var($propertyLabel);
if (!$isInverse) {
$triple = $this->_resourceQuery->addTriple(
$this->_resourceVar,
$prop,
$var
);
} else {
$triple = $this->_resourceQuery->addTriple(
$var,
$prop,
$this->_resourceVar
);
}
$filterObj = $this->_resourceQuery->addFilter(
new Erfurt_Sparql_Query2_Larger($var, $value1_obj)
);
break;
case 'smaller':
$var = new Erfurt_Sparql_Query2_Var($propertyLabel);
if (!$isInverse) {
$triple = $this->_resourceQuery->addTriple(
$this->_resourceVar,
$prop,
$var
);
} else {
$triple = $this->_resourceQuery->addTriple(
$var,
$prop,
$this->_resourceVar
);
}
$filterObj = $this->_resourceQuery->addFilter(
new Erfurt_Sparql_Query2_Smaller($var, $value1_obj)
);
break;
case 'between':
$var = new Erfurt_Sparql_Query2_Var($propertyLabel);
if (!$isInverse) {
$triple = $this->_resourceQuery->addTriple(
$this->_resourceVar,
$prop,
$var
);
} else {
$triple = $this->_resourceQuery->addTriple(
$var,
$prop,
$this->_resourceVar
);
}
$filterObj = $this->_resourceQuery->addFilter(
new Erfurt_Sparql_Query2_ConditionalAndExpression(
array(
new Erfurt_Sparql_Query2_Larger($var, $value1_obj),
new Erfurt_Sparql_Query2_Smaller($var, $value2_obj)
)
)
);
break;
case 'bound':
$var = new Erfurt_Sparql_Query2_Var($propertyLabel);
if (!$isInverse) {
$triple = new Erfurt_Sparql_Query2_Triple(
$this->_resourceVar,
$prop,
$var
);
} else {
$triple = new Erfurt_Sparql_Query2_Triple(
$var,
$prop,
$this->_resourceVar
);
}
if($negate){
$optional = new Erfurt_Sparql_Query2_OptionalGraphPattern();
$optional->addElement($triple);
$this->_resourceQuery->addElement($optional);
$triple = $optional; // to save this obj (see underneath 20 lines)
} else {
$this->_resourceQuery->addElement($triple);
}
if($negate){
$filterObj = $this->_resourceQuery->addFilter(
new Erfurt_Sparql_Query2_UnaryExpressionNot(
new Erfurt_Sparql_Query2_bound($var)
)
);
}
break;
default:
throw new RuntimeException(
'called Ontowiki_Model_Instances::addFilter with '.
'unknown param-value: filtertype='.$filter
);
break;
}
//these filters bring there own triple
$this->allTriple->remove($this->_resourceQuery);
//save
$this->_filter[$id] = array(
'id' => $id,
'mode' => 'box',
'property' => $property,
'isInverse' => $isInverse,
'propertyLabel' => $propertyLabel,
'filter' => $filter,
'value1' => $value1,
'value2' => $value2,
'valuetype' => $valuetype,
'literaltype' => $literaltype,
'hidden' => $hidden,
'negate' => $negate,
'objects' => array($triple, isset($filterObj) ? $filterObj : null)
);
$this->invalidate();
return $id;
}
/**
* remove a filter by id
* @param string $id
* @return OntoWiki_Model_Instances $this
*/
public function removeFilter($id){
if (isset($this->_filter[$id])){
foreach($this->_filter[$id]['objects'] as $obj){
if($obj instanceof Erfurt_Sparql_Query2_ElementHelper){
$obj->remove($this->_resourceQuery);
}
}
unset($this->_filter[$id]);
//when all deleted
if (count($this->_resourceQuery->getWhere()->getElements()) == 1){ //this last element is the "isUri and !isBlank"-filter
$this->addAllTriple();
}
$this->invalidate();
return $this;
}
}
/**
* get the array that holds the filters
* @return array
*/
public function getFilter(){
return $this->_filter;
}
/**
*
* @param string $type the uri of the class
* @param string $id
* @param array $options
* @return int id
*/
public function addTypeFilter($type, $id = null, $option = array()){
if($id == null){
$id = "type" . count($this->_filter);
} else {
if(isset($this->_filter[$id])){
$this->removeFilter($id);
}
}
//shortcut navigation - only a rdfs class given
$options['mode'] = 'instances';
$options['type'] = $type;
$options['memberPredicate'] = EF_RDF_TYPE;
$options['withChilds'] = isset($option['withChilds']) ? $option['withChilds'] : true;
$options['hierarchyUp'] = EF_RDFS_SUBCLASSOF;
$options['hierarchyIsInverse'] = true;
//$options['hierarchyDown'] = null;
$options['direction'] = 1; // down the tree
$member_predicate = $options['memberPredicate'];
if (is_string($member_predicate)){
$member_predicate = new Erfurt_Sparql_Query2_IriRef($member_predicate);
}
if (!($member_predicate instanceof Erfurt_Sparql_Query2_Verb)) {
throw new RuntimeException(
'Option "member_predicate" passed to Ontowiki_Model_Instances '.
'must be an instance of Erfurt_Sparql_Query2_IriRef '.
'or string instance of '.typeHelper($member_predicate).' given');
}
$type = new Erfurt_Sparql_Query2_IriRef($options['type']);
$subClasses = array();
if ($options['withChilds']) {
$subClasses =
array_keys(
// get subclasses:
$this->_store->getTransitiveClosure(
$this->_graph,
$options['hierarchyUp'],
array($type->getIri()),
$options['hierarchyIsInverse']
)
);
} else if(isset($options['subtypes'])){ //dont query, take the given. maybe the new navigation can use this
$subClasses = $options['subtypes'];
}
if (count($subClasses)>1) {
// there are subclasses. "1" because the class itself is somehow included in the subclasses...
$typeVar = new Erfurt_Sparql_Query2_Var($type);
$triple = $this->_resourceQuery->addTriple(
$this->_resourceVar,
$member_predicate,
$typeVar);
$or = new Erfurt_Sparql_Query2_ConditionalOrExpression();
foreach ($subClasses as $subclass) {
$or->addElement(
new Erfurt_Sparql_Query2_sameTerm(
$typeVar,
new Erfurt_Sparql_Query2_IriRef($subclass)
)
);
}
$filterObj = $this->_resourceQuery->addFilter($or);
} else {
// no subclasses
$triple = $this->_resourceQuery->addTriple(
$this->_resourceVar,
$member_predicate,
new Erfurt_Sparql_Query2_IriRef($type->getIri())
);
}
//save
$this->_filter[$id] = array(
'id' => $id,
'mode' => 'rdfsclass',
'rdfsclass' => $options['type'],
'withChilds' => $options['withChilds'],
'objects' => array($triple, isset($filterObj) ? $filterObj : null)
);
//these filters bring there own triple
$this->allTriple->remove($this->_resourceQuery);
$this->invalidate();
return $id;
}
/**
*
* @param string $str
* @param string $id optional
* @return string the id used
*/
public function addSearchFilter($str, $id = null){
if($id == null){
$id = "search" . count($this->_filter);
} else {
if(isset($this->_filter[$id])){
$this->removeFilter($id);
}
}
$pattern = $this->_store->getSearchPattern(
$str,
$this->_graph
);
$vars = array();
foreach($pattern as $element){
if(method_exists($element, 'getVars')){
$vars = array_merge($vars, $element->getVars());
}
}
$count = count($this->_filter);
foreach($vars as $var){
if($var->getName() == 'o' || $var->getName() == 'p') {
$var->setName($var->getName().$count);
}
}
$this->_resourceQuery->addElements($pattern);
//var_dump($pattern);
//save
$this->_filter[$id] = array(
'id' => $id,
'mode' => 'search',
'searchText' => $str,
'objects' => $pattern
);
//these filters bring there own triple
$this->allTriple->remove($this->_resourceQuery);
$this->invalidate();
return $id;
}
/**
* add arbitrary triples to the query to filter (used by the navigation)
* @param array $triples
* @param string $id
* @return string the id used
*/
public function addTripleFilter($triples, $id = null){
if($id == null){
$id = 'triple' . count($this->_filter);
} else {
if(isset($this->_filter[$id])){
$this->removeFilter($id);
}
}
$this->_resourceQuery->addElements($triples);
//save
$this->_filter[$id] = array(
'id' => $id,
'mode' => 'triples',
'objects' => $triples
);
//these filters bring there own triple
$this->allTriple->remove($this->_resourceQuery);
$this->invalidate();
return $id;
}
/**
* get the query used to get the values (shownproperties)
* @return Erfurt_Sparql_Query2
*/
public function getQuery ()
{
return $this->_valueQuery;
}
/**
* get the query used for getting the resources. incl. filter
* @return Erfurt_Sparql_Query2
*/
public function getResourceQuery ()
{
return $this->_resourceQuery;
}
/**
* build a link that recreates the current state on a different system
* @return string
*/
public function getPermalink($listname){
$url = new OntoWiki_Url();
$url->init = true;
if($this->getOffset() != 0 && $this->getLimit() != 0){
$url->p=(($this->getOffset() / $this->getLimit()) + 1 );
}
if($this->getLimit() != 10){
$url->limit=$this->getLimit();
}
$url->list=$listname;
$conf = array();
if(is_array($this->_shownProperties) && count($this->_shownProperties) > 0){
$conf['shownProperties'] = array();
foreach($this->_shownProperties as $shownProperty){
$conf['shownProperties'][] = array(
'uri' => $shownProperty['uri'],
'label' => $shownProperty['name'],
'inverse' => $shownProperty['inverse'],
'action' => 'add'
);
}
}
if(is_array($this->_filter) && count($this->_filter) > 0){
$conf['filter'] = array();
foreach($this->_filter as $filter){
switch($filter['mode']){
case 'box':
$arr = array(
'action' => 'add',
'mode' => 'box'
);
$arr = array_merge($arr, $filter);
$conf['filter'][] = $arr;
break;
case "search":
$conf['filter'][] = array(
'action' => 'add',
'mode' => 'search',
'searchText' => $filter['searchText']
);
break;
case "rdfsclass":
$conf['filter'][] = array(
'action' => 'add',
'mode' => 'rdfsclass',
'rdfsclass' => $filter['rdfsclass']
);
break;
case "triples":
//problem: php objects can not be json encoded ...
/*
$conf['filter'][] = array(
'action' => 'add',
'mode' => 'triples',
'triples' => $filter['triples']
);
*/
break;
}
}
$url->m= (string) $this->_model;
if(!empty($conf)){
$url->instancesconfig = json_encode($conf);
}
}
// $url = '?init=1';
// if($this->getOffset() != 0 && $this->getLimit() != 0){
// $url .= '&p='. (($this->getOffset() / $this->getLimit()) + 1 );
// }
// if($this->getLimit() != 10){
// $url .= '&limit='.$this->getLimit();
// }
// $url = '&list='.$listname;
//
// $conf = array();
//
// if(is_array($this->_shownProperties) && count($this->_shownProperties) > 0){
// $conf['shownProperties'] = array();
//
// foreach($this->_shownProperties as $shownProperty){
// $conf['shownProperties'][] = array(
// 'uri' => $shownProperty['uri'],
// 'label' => $shownProperty['name'],
// 'inverse' => $shownProperty['inverse'],
// 'action' => 'add'
// );
// }
// }
// if(is_array($this->_filter) && count($this->_filter) > 0){
// $conf['filter'] = array();
//
// foreach($this->_filter as $filter){
// switch($filter['mode']){
// case 'box':
// $arr = array(
// 'action' => 'add',
// 'mode' => 'box'
// );
// $arr = array_merge($arr, $filter);
// $conf['filter'][] = $arr;
// break;
// case "search":
// $conf['filter'][] = array(
// 'action' => 'add',
// 'mode' => 'search',
// 'searchText' => $filter['searchText']
// );
// break;