1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under |
5
|
|
|
* the terms of the GPL-3 license. |
6
|
|
|
* |
7
|
|
|
* (c) Konrad Abicht <[email protected]> |
8
|
|
|
* (c) Benjamin Nowack |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace sweetrdf\InMemoryStoreSqlite\Parser; |
15
|
|
|
|
16
|
|
|
use sweetrdf\InMemoryStoreSqlite\Log\Logger; |
17
|
|
|
use sweetrdf\InMemoryStoreSqlite\NamespaceHelper; |
18
|
|
|
use sweetrdf\InMemoryStoreSqlite\StringReader; |
19
|
|
|
|
20
|
|
|
abstract class BaseParser |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $added_triples; |
26
|
|
|
|
27
|
|
|
protected string $base; |
28
|
|
|
|
29
|
|
|
protected string $bnode_id; |
30
|
|
|
|
31
|
|
|
protected array $blocks; |
32
|
|
|
|
33
|
|
|
protected Logger $logger; |
34
|
|
|
|
35
|
|
|
protected NamespaceHelper $namespaceHelper; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Query infos container. |
39
|
|
|
*/ |
40
|
|
|
protected array $r = []; |
41
|
|
|
|
42
|
|
|
protected StringReader $stringReader; |
43
|
|
|
|
44
|
|
|
protected array $triples = []; |
45
|
|
|
|
46
|
|
|
protected int $t_count = 0; |
47
|
|
|
|
48
|
96 |
|
public function __construct(Logger $logger, NamespaceHelper $namespaceHelper, StringReader $stringReader) |
49
|
|
|
{ |
50
|
96 |
|
$this->reader = $stringReader; |
|
|
|
|
51
|
|
|
|
52
|
96 |
|
$this->logger = $logger; |
53
|
|
|
|
54
|
96 |
|
$this->namespaceHelper = $namespaceHelper; |
55
|
|
|
|
56
|
|
|
// generates random prefix for blank nodes |
57
|
96 |
|
$this->bnode_prefix = bin2hex(random_bytes(4)).'b'; |
|
|
|
|
58
|
|
|
|
59
|
96 |
|
$this->bnode_id = 0; |
60
|
96 |
|
} |
61
|
|
|
|
62
|
94 |
|
public function getQueryInfos() |
63
|
|
|
{ |
64
|
94 |
|
return $this->r; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function getTriples() |
68
|
|
|
{ |
69
|
2 |
|
return $this->triples; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getSimpleIndex($flatten_objects = 1, $vals = ''): array |
73
|
|
|
{ |
74
|
|
|
return $this->_getSimpleIndex($this->getTriples(), $flatten_objects, $vals); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @todo port from ARC2::getSimpleIndex; refactor and merge it with $this->getSimpleIndex |
79
|
|
|
*/ |
80
|
|
|
private function _getSimpleIndex($triples, $flatten_objects = 1, $vals = ''): array |
81
|
|
|
{ |
82
|
|
|
$r = []; |
83
|
|
|
foreach ($triples as $t) { |
84
|
|
|
$skip_t = 0; |
85
|
|
|
foreach (['s', 'p', 'o'] as $term) { |
86
|
|
|
$$term = $t[$term]; |
87
|
|
|
/* template var */ |
88
|
|
|
if (isset($t[$term.'_type']) && ('var' == $t[$term.'_type'])) { |
89
|
|
|
$val = isset($vals[$$term]) ? $vals[$$term] : ''; |
90
|
|
|
$skip_t = isset($vals[$$term]) ? $skip_t : 1; |
91
|
|
|
$type = ''; |
92
|
|
|
$type = !$type && isset($vals[$$term.' type']) ? $vals[$$term.' type'] : $type; |
93
|
|
|
$type = !$type && preg_match('/^\_\:/', $val) ? 'bnode' : $type; |
94
|
|
|
if ('o' == $term) { |
95
|
|
|
$type = !$type && (preg_match('/\s/s', $val) || !preg_match('/\:/', $val)) ? 'literal' : $type; |
96
|
|
|
$type = !$type && !preg_match('/[\/]/', $val) ? 'literal' : $type; |
97
|
|
|
} |
98
|
|
|
$type = !$type ? 'uri' : $type; |
99
|
|
|
$t[$term.'_type'] = $type; |
100
|
|
|
$$term = $val; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
if ($skip_t) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
if (!isset($r[$s])) { |
|
|
|
|
107
|
|
|
$r[$s] = []; |
108
|
|
|
} |
109
|
|
|
if (!isset($r[$s][$p])) { |
|
|
|
|
110
|
|
|
$r[$s][$p] = []; |
111
|
|
|
} |
112
|
|
|
if ($flatten_objects) { |
113
|
|
|
if (!\in_array($o, $r[$s][$p])) { |
|
|
|
|
114
|
|
|
$r[$s][$p][] = $o; |
115
|
|
|
} |
116
|
|
|
} else { |
117
|
|
|
$o = ['value' => $o]; |
118
|
|
|
foreach (['lang', 'type', 'datatype'] as $suffix) { |
119
|
|
|
if (isset($t['o_'.$suffix]) && $t['o_'.$suffix]) { |
120
|
|
|
$o[$suffix] = $t['o_'.$suffix]; |
121
|
|
|
} elseif (isset($t['o '.$suffix]) && $t['o '.$suffix]) { |
122
|
|
|
$o[$suffix] = $t['o '.$suffix]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
if (!\in_array($o, $r[$s][$p])) { |
126
|
|
|
$r[$s][$p][] = $o; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $r; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|