-
Notifications
You must be signed in to change notification settings - Fork 7
/
TemplateOptionsClass.php
215 lines (184 loc) · 7.46 KB
/
TemplateOptionsClass.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
<?php
/**
* This file is part of the {@link http://ontowiki.net OntoWiki} project.
*
* @copyright Copyright (c) 2013, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
/**
* An utility class for templateOptions template helper.
*
* @category OntoWiki
* @package OntoWiki_extensions_components_site
*/
class TemplateOptionsClass
{
const RESOURCE_SPECIFIC_OPTION = 1;
const CLASS_SPECIFIC_OPTION = 2;
const DATASET_SPECIFIC_OPTION = 3;
const PRIORITY_LAST = 4;
const SITE_TEMPLATE_OPTION = 'http://ns.ontowiki.net/SysOnt/Site/TemplateOption';
const SITE_FETCH_OPTIONS_FROM = 'http://ns.ontowiki.net/SysOnt/Site/fetchOptionsFrom';
/**
* Template options
* @var array
*/
protected $_options = array();
/**
* Mapping of local names to template option keys
* @var array
*/
protected $_optionLocalNames = array();
public function __construct($resourceUri)
{
$owApp = OntoWiki::getInstance();
$helper = $owApp->extensionManager->getComponentHelper('site');
$siteConfig = $helper->getSiteConfig();
$store = $owApp->erfurt->getStore();
if (isset($siteConfig['model']) && !empty($siteConfig['model'])) {
$model = $store->getModel($siteConfig['model']);
} else {
$model = $owApp->selectedModel;
}
$resource = new Erfurt_Sparql_Query2_IriRef($resourceUri);
$ontology = new Erfurt_Sparql_Query2_IriRef(EF_OWL_ONTOLOGY);
$fetchFrom = new Erfurt_Sparql_Query2_IriRef(static::SITE_FETCH_OPTIONS_FROM);
// fetch options from this resource
$query = new Erfurt_Sparql_Query2();
list($key, $value, $class, $dataset) = $this->_queryAddVars($query, array('key', 'value', 'class', 'dataset'));
$union = new Erfurt_Sparql_Query2_GroupOrUnionGraphPattern();
// resource options
$group = new Erfurt_Sparql_Query2_GroupGraphPattern();
$group->addTriple($resource, $key, $value);
$union->addElement($group);
// class options
$group = new Erfurt_Sparql_Query2_GroupGraphPattern();
$group->addTriple($resource, EF_RDF_TYPE, $class);
$group->addTriple($class, $key, $value);
$union->addElement($group);
// dataset options
$group = new Erfurt_Sparql_Query2_GroupGraphPattern();
$group->addTriple($dataset, EF_RDF_TYPE, new Erfurt_Sparql_Query2_IriRef(EF_OWL_ONTOLOGY));
$group->addTriple($dataset, $key, $value);
$union->addElement($group);
$query->addElement($union);
$query->addTriple($key, EF_RDF_TYPE, new Erfurt_Sparql_Query2_IriRef(static::SITE_TEMPLATE_OPTION));
$results = $model->sparqlQuery($query);
foreach ($results as $result) {
$arrayKey = $result[$key->getName()];
$this->_options[$arrayKey][$this->_getPriority($result)][] = $result[$value->getName()];
$templateOption = new Erfurt_Rdf_Resource($arrayKey, $model);
$arrayKeyLocalName = $templateOption->getLocalName();
$this->_optionLocalNames[$arrayKeyLocalName][] = $arrayKey;
}
// fetch options from linked resources
$query = new Erfurt_Sparql_Query2();
list($other, $class, $dataset) = $this->_queryAddVars($query, array('other', 'class', 'dataset'));
$union = new Erfurt_Sparql_Query2_GroupOrUnionGraphPattern();
// resource links
$group = new Erfurt_Sparql_Query2_GroupGraphPattern();
$group->addTriple($resource, $fetchFrom, $other);
$union->addElement($group);
// class links
$group = new Erfurt_Sparql_Query2_GroupGraphPattern();
$group->addTriple($resource, EF_RDF_TYPE, $class);
$group->addTriple($class, $fetchFrom, $other);
$union->addElement($group);
// dataset links
$group = new Erfurt_Sparql_Query2_GroupGraphPattern();
$group->addTriple($dataset, EF_RDF_TYPE, $ontology);
$group->addTriple($dataset, $fetchFrom, $other);
$union->addElement($group);
$query->addElement($union);
$results = $model->sparqlQuery($query);
foreach ($results as $result) {
$options = new TemplateOptionsClass($result[$other->getName()]);
foreach ($options->_options as $arrayKey => $priorities) {
foreach ($priorities as $priority => $values) {
$this->_options[$arrayKey][(string)($this->_getPriority($result) + 0.1*$priority)] = $values;
}
}
foreach ($options->_optionLocalNames as $arrayKeyLocalName => $keys) {
foreach ($keys as $key) {
if (!isset($this->_optionLocalNames[$arrayKeyLocalName])
|| !in_array($key, $this->_optionLocalNames[$arrayKeyLocalName])) {
$this->_optionLocalNames[$arrayKeyLocalName][] = $key;
}
}
}
}
foreach (array_keys($this->_options) as $key) {
ksort($this->_options[$key]);
}
}
private function _queryAddVars($query, $vars)
{
return array_map(function($name) use($query) {
$query->addProjectionVar($var = new Erfurt_Sparql_Query2_Var($name));
return $var;
}, $vars);
}
private function _getPriority($result)
{
if (!is_null($result['dataset'])) {
return static::DATASET_SPECIFIC_OPTION;
} elseif (!is_null($result['class'])) {
return static::CLASS_SPECIFIC_OPTION;
} else {
return static::RESOURCE_SPECIFIC_OPTION;
}
}
/**
* Returns an array with the template options in sub arrays sorted by priority
* @param $key the key of the template option, either as URI or as localName
* @return 2d-array with the template options
*/
public function getArray($key)
{
if (!isset($this->_options[$key])) {
if (isset($this->_optionLocalNames[$key])) {
$keys = $this->_optionLocalNames[$key];
} else {
return array();
}
} else {
$keys = array($key);
}
$options = array();
foreach($keys as $optionKey) {
if ($this->_options[$optionKey]) {
$options = array_merge($options, $this->_options[$optionKey]);
}
}
return $options;
}
/**
* Returns the template options value with the highest priority
* @param $key the key of the template option, either as URI or as localName
* @param $default (optional) the optional default value if the template option is not set
* @return the template options value
*/
public function getValue($key, $default = null)
{
if ($array = $this->getArray($key)) {
$values = reset($array);
return reset($values);
}
return $default;
}
/**
* Same as getValue(), with comma separated values exploded to an array
*/
public function getValueAsArray($key, $default = '')
{
$value = $this->getValue($key, $default);
return $value ? preg_split('/\s*,\s*/', $value) : array();
}
public function dump()
{
return print_r(array(
'options' => $this->_options,
'optionLocalNames' => $this->_optionLocalNames,
), true);
}
}