Mercurial > hg > rr-repo
comparison sites/all/libraries/ARC2/arc/store/ARC2_StoreConstructQueryHandler.php @ 4:ce11bbd8f642
added modules
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Thu, 19 Sep 2013 10:38:44 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:b28be78d8160 | 4:ce11bbd8f642 |
---|---|
1 <?php | |
2 /* | |
3 homepage: http://arc.semsol.org/ | |
4 license: http://arc.semsol.org/license | |
5 | |
6 class: ARC2 RDF Store CONSTRUCT Query Handler | |
7 author: Benjamin Nowack | |
8 version: 2010-11-16 | |
9 */ | |
10 | |
11 ARC2::inc('StoreSelectQueryHandler'); | |
12 | |
13 class ARC2_StoreConstructQueryHandler extends ARC2_StoreSelectQueryHandler { | |
14 | |
15 function __construct($a, &$caller) {/* caller has to be a store */ | |
16 parent::__construct($a, $caller); | |
17 } | |
18 | |
19 function __init() {/* db_con */ | |
20 parent::__init(); | |
21 $this->store = $this->caller; | |
22 } | |
23 | |
24 /* */ | |
25 | |
26 function runQuery($infos) { | |
27 $this->infos = $infos; | |
28 $this->buildResultVars(); | |
29 $this->infos['query']['distinct'] = 1; | |
30 $sub_r = parent::runQuery($this->infos); | |
31 $rf = $this->v('result_format', '', $infos); | |
32 if (in_array($rf, array('sql', 'structure', 'index'))) { | |
33 return $sub_r; | |
34 } | |
35 return $this->getResultIndex($sub_r); | |
36 } | |
37 | |
38 /* */ | |
39 | |
40 function buildResultVars() { | |
41 $r = array(); | |
42 foreach ($this->infos['query']['construct_triples'] as $t) { | |
43 foreach (array('s', 'p', 'o') as $term) { | |
44 if ($t[$term . '_type'] == 'var') { | |
45 if (!in_array($t[$term], $r)) { | |
46 $r[] = array('var' => $t[$term], 'aggregate' => '', 'alias' => ''); | |
47 } | |
48 } | |
49 } | |
50 } | |
51 $this->infos['query']['result_vars'] = $r; | |
52 } | |
53 | |
54 /* */ | |
55 | |
56 function getResultIndex($qr) { | |
57 $r = array(); | |
58 $added = array(); | |
59 $rows = $this->v('rows', array(), $qr); | |
60 $cts = $this->infos['query']['construct_triples']; | |
61 $bnc = 0; | |
62 foreach ($rows as $row) { | |
63 $bnc++; | |
64 foreach ($cts as $ct) { | |
65 $skip_t = 0; | |
66 $t = array(); | |
67 foreach (array('s', 'p', 'o') as $term) { | |
68 $val = $ct[$term]; | |
69 $type = $ct[$term . '_type']; | |
70 $val = ($type == 'bnode') ? $val . $bnc : $val; | |
71 if ($type == 'var') { | |
72 $skip_t = !isset($row[$val]) ? 1 : $skip_t; | |
73 $type = !$skip_t ? $row[$val . ' type'] : ''; | |
74 $val = (!$skip_t) ? $row[$val] : ''; | |
75 } | |
76 $t[$term] = $val; | |
77 $t[$term . '_type'] = $type; | |
78 if (isset($row[$ct[$term] . ' lang'])) { | |
79 $t[$term . '_lang'] = $row[$ct[$term] . ' lang']; | |
80 } | |
81 if (isset($row[$ct[$term] . ' datatype'])) { | |
82 $t[$term . '_datatype'] = $row[$ct[$term] . ' datatype']; | |
83 } | |
84 } | |
85 if (!$skip_t) { | |
86 $s = $t['s']; | |
87 $p = $t['p']; | |
88 $o = $t['o']; | |
89 if (!isset($r[$s])) { | |
90 $r[$s] = array(); | |
91 } | |
92 if (!isset($r[$s][$p])) { | |
93 $r[$s][$p] = array(); | |
94 } | |
95 $o = array('value' => $o); | |
96 foreach (array('lang', 'type', 'datatype') as $suffix) { | |
97 if (isset($t['o_' . $suffix]) && $t['o_' . $suffix]) { | |
98 $o[$suffix] = $t['o_' . $suffix]; | |
99 } | |
100 } | |
101 if (!isset($added[md5($s . ' ' . $p . ' ' . serialize($o))])) { | |
102 $r[$s][$p][] = $o; | |
103 $added[md5($s . ' ' . $p . ' ' . serialize($o))] = 1; | |
104 } | |
105 } | |
106 } | |
107 } | |
108 return $r; | |
109 } | |
110 | |
111 /* */ | |
112 | |
113 } | |
114 | |
115 |