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
/
Registry.php
428 lines (364 loc) · 13.4 KB
/
Registry.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
<?php
/**
* This file is part of the {@link http://ontowiki.net OntoWiki} project.
*
* @copyright Copyright (c) 2009-2016, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
/**
* OntoWiki menu registry class.
*
* Serves as a central registry for menus and provides methods for setting
* and retrieving menu instances.
*
* @category OntoWiki
* @package OntoWiki_Classes_Menu
* @author Norman Heino <norman.heino@gmail.com>
*/
class OntoWiki_Menu_Registry
{
/**
* Menu registry; an array of menu instances
*
* @var array
*/
private $_menus = array();
/**
* Singleton instance
*
* @var OntoWiki_Menu_Registry
*/
private static $_instance = null;
/**
* Singleton instance
*
* @return OntoWiki_Menu_Registry
*/
public static function getInstance()
{
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Returns the menu denoted by $menuKey.
*
* @param string $menuKey
*
* @return OntoWiki_Menu
*/
public function getMenu($menuKey, $context = null)
{
if (!is_string($menuKey)) {
throw new OntoWiki_Exception('Menu key must be string.');
}
if (!isset($this->_menus[$context])) {
$this->_menus[$context] = array();
}
if (!array_key_exists($menuKey, $this->_menus[$context])) {
$getMethod = '_get' . ucfirst($menuKey) . 'Menu';
if (method_exists($this, $getMethod)) {
$this->setMenu($menuKey, $context, $this->$getMethod($context));
} else {
$this->setMenu($menuKey, $context, new OntoWiki_Menu());
}
}
return $this->_menus[$context][$menuKey];
}
/**
* Stores the menu $menu with key $menuKey in the registry.
*
* @param string $menuKey
* @param OntoWiki_Menu $menu
* @param boolean $replace
*
* @return OntoWiki_Menu_Registry
*/
public function setMenu($menuKey, $context, OntoWiki_Menu $menu, $replace = true)
{
if (!is_string($menuKey)) {
throw new OntoWiki_Exception('Menu key must be string.');
}
if (!isset($this->_menus[$context])) {
$this->_menus[$context] = array();
}
if (!$replace && array_key_exists($menuKey, $this->_menus[$context])) {
throw new OntoWiki_Exception("Menu with key '$menuKey' already registered.");
}
$this->_menus[$context][$menuKey] = $menu;
return $this;
}
private function __construct()
{
$owApp = OntoWiki::getInstance();
$this->setMenu('application', null, $this->_getApplicationMenu());
// check if a resource is selected
if (isset($owApp->selectedResource) && $owApp->selectedResource) {
$resource = (string)$owApp->selectedResource;
$this->setMenu('resource', $resource, $this->_getResourceMenu($resource));
}
}
/**
* Create the application menu and fill it with its default entries
*/
private function _getApplicationMenu($context = null)
{
$owApp = OntoWiki::getInstance();
// user sub menu
if ($owApp->erfurt->isActionAllowed('RegisterNewUser')
&& !(isset($owApp->config->ac)
&& ((boolean)$owApp->config->ac->deactivateRegistration === true))
) {
if (!($owApp->erfurt->getAc() instanceof Erfurt_Ac_None)) {
$userMenu = new OntoWiki_Menu();
$userMenu->setEntry('Register New User', $owApp->config->urlBase . 'application/register');
}
}
if ($owApp->user && !$owApp->user->isAnonymousUser()) {
if (!isset($userMenu)) {
$userMenu = new OntoWiki_Menu();
}
if (!$owApp->user->isDbUser()) {
$userMenu->setEntry('Preferences', $owApp->config->urlBase . 'application/preferences');
}
$userMenu->setEntry('Logout', $owApp->config->urlBase . 'application/logout');
}
// view sub menu
$viewMenu = new OntoWiki_Menu();
// extras sub menu
$extrasMenu = new OntoWiki_Menu();
$extrasMenu->setEntry('News', $owApp->config->urlBase . 'index/news');
// help sub menue
$helpMenu = new OntoWiki_Menu();
if (isset($owApp->config->help->documentation) && (trim($owApp->config->help->documentation) !== '')) {
$helpMenu->setEntry('Documentation', trim($owApp->config->help->documentation));
}
if (isset($owApp->config->help->issues) && (trim($owApp->config->help->issues) !== '')) {
$helpMenu->setEntry('Bug Report', trim($owApp->config->help->issues));
}
if (isset($owApp->config->help->versioninfo) && (trim($owApp->config->help->versioninfo) !== '')) {
$helpMenu->setEntry('Version Info', trim($owApp->config->help->versioninfo));
}
$helpMenu->setEntry('About', $owApp->config->urlBase . 'application/about');
// build menu out of sub menus
$applicationMenu = new OntoWiki_Menu();
if (isset($userMenu)) {
$applicationMenu->setEntry('User', $userMenu);
}
$applicationMenu->setEntry('Extras', $extrasMenu)
->setEntry('Help', $helpMenu);
// add cache entry only if use is allowed to use debug action
if ($owApp->erfurt->isActionAllowed('Debug')) {
$debugMenu = new OntoWiki_Menu();
$debugMenu->setEntry('Clear Module Cache', $owApp->config->urlBase . 'debug/clearmodulecache')
->setEntry('Clear Translation Cache', $owApp->config->urlBase . 'debug/cleartranslationcache')
->setEntry('Clear Object & Query Cache', $owApp->config->urlBase . 'debug/clearquerycache')
->setEntry('Start xdebug Session', $owApp->config->urlBase . '?XDEBUG_SESSION_START=xdebug')
->setEntry('Reset Session', $owApp->config->urlBase . 'debug/destroysession');
$applicationMenu->setEntry('Debug', $debugMenu);
}
return $applicationMenu;
}
/**
* Create the context menu for models/knowledge bases and fill it with its default entries
*/
private function _getModelMenu($model = null)
{
$owApp = OntoWiki::getInstance();
if ($model === null) {
$model = $owApp->selectedModel;
}
$config = $owApp->config;
$modelMenu = new OntoWiki_Menu();
// Select Knowledge Base
$url = new OntoWiki_Url(
array('controller' => 'model', 'action' => 'select'),
array()
);
$url->setParam('m', $model, false);
$modelMenu->appendEntry(
'Select Knowledge Base',
(string)$url
);
// View resource
$url = new OntoWiki_Url(
array('action' => 'view'),
array()
);
$url->setParam('m', $model, false);
$url->setParam('r', $model, true);
$modelMenu->appendEntry(
'View as Resource',
(string)$url
);
// check if model could be edited (prefixes and data)
if ($owApp->erfurt->getAc()->isModelAllowed('edit', $model)) {
// Configure Knowledge Base
$url = new OntoWiki_Url(
array('controller' => 'model', 'action' => 'config'),
array()
);
$url->setParam('m', $model, false);
$modelMenu->appendEntry(
'Configure Knowledge Base',
(string)$url
);
// Add Data to Knowledge Base
$url = new OntoWiki_Url(
array('controller' => 'model', 'action' => 'add'),
array()
);
$url->setParam('m', $model, false);
$modelMenu->appendEntry(
'Add Data to Knowledge Base',
(string)$url
);
}
// Model export
if ($owApp->erfurt->getAc()->isActionAllowed(Erfurt_Ac_Default::ACTION_MODEL_EXPORT)) {
// add entries for supported export formats
foreach (Erfurt_Syntax_RdfSerializer::getSupportedFormats() as $key => $format) {
$url = new OntoWiki_Url(
array('controller' => 'model', 'action' => 'export'),
array()
);
$url->setParam('m', $model, false);
$url->setParam('f', $key);
$modelMenu->appendEntry(
'Export Knowledge Base as ' . $format,
(string)$url
);
}
}
// can user delete models?
if ($owApp->erfurt->getAc()->isModelAllowed('edit', $model)
&& $owApp->erfurt->getAc()->isActionAllowed('ModelManagement')
) {
$url = new OntoWiki_Url(
array('controller' => 'model', 'action' => 'delete'),
array()
);
$url->setParam('model', $model, false);
$modelMenu->appendEntry(
'Delete Knowledge Base',
(string)$url
);
}
// add a seperator
$modelMenu->appendEntry(OntoWiki_Menu::SEPARATOR);
return $modelMenu;
}
/**
* Create the (context) menu for resource and fill it with its default entries
*/
private function _getResourceMenu($resource = null)
{
$owApp = OntoWiki::getInstance();
if ($resource === null) {
$resource = $owApp->selectedResource;
}
$config = $owApp->config;
$resourceMenu = new OntoWiki_Menu();
// Add the class Menu if the current resource is a class
$classMenu = $this->_getClassMenu($resource)->toArray();
foreach ($classMenu as $key => $value) {
$resourceMenu->appendEntry($key, $value);
}
if (count($classMenu) > 0) {
$resourceMenu->appendEntry(OntoWiki_Menu::SEPARATOR);
}
// View resource
$url = new OntoWiki_Url(
array('action' => 'view'),
array()
);
$url->setParam('r', $resource, true);
$resourceMenu->appendEntry(
'View Resource',
(string)$url
);
// Edit entries
if ($owApp->erfurt->getAc()->isModelAllowed('edit', $owApp->selectedModel)) {
// edit resource option
$resourceMenu->appendEntry(
'Edit Resource',
'javascript:editResourceFromURI(\'' . (string)$resource . '\')'
);
// Delete resource option
$url = new OntoWiki_Url(
array('controller' => 'resource', 'action' => 'delete'),
array('r')
);
$url->setParam('r', (string)$resource, false);
$resourceMenu->appendEntry('Delete Resource', (string)$url);
}
$resourceMenu->appendEntry(
'Go to Resource (external)',
(string)$resource
);
$resourceMenu->appendEntry(OntoWiki_Menu::SEPARATOR);
foreach (Erfurt_Syntax_RdfSerializer::getSupportedFormats() as $key => $format) {
$resourceMenu->appendEntry(
'Export Resource as ' . $format,
$config->urlBase . 'resource/export/f/' . $key . '?r=' . urlencode($resource)
);
}
return $resourceMenu;
}
/**
* Create the (context) menu for classes and fill it with its default entries
*/
private function _getClassMenu($resource = null)
{
$owApp = OntoWiki::getInstance();
$classMenu = new OntoWiki_Menu();
$query = Erfurt_Sparql_SimpleQuery::initWithString(
'SELECT *
FROM <' . (string)$owApp->selectedModel . '>
WHERE {
<' . $resource . '> a ?type .
}'
);
$results[] = $owApp->erfurt->getStore()->sparqlQuery($query);
$query = Erfurt_Sparql_SimpleQuery::initWithString(
'SELECT *
FROM <' . (string)$owApp->selectedModel . '>
WHERE {
?inst a <' . $resource . '> .
} LIMIT 2'
);
if (count($owApp->erfurt->getStore()->sparqlQuery($query)) > 0) {
$hasInstances = true;
} else {
$hasInstances = false;
}
$typeArray = array();
foreach ($results[0] as $row) {
$typeArray[] = $row['type'];
}
if (in_array(EF_RDFS_CLASS, $typeArray)
|| in_array(EF_OWL_CLASS, $typeArray)
|| $hasInstances
) {
$url = new OntoWiki_Url(
array('action' => 'list'),
array()
);
$url->setParam('class', $resource, false);
$url->setParam('init', "true", true);
$classMenu->appendEntry(
'List Instances',
(string)$url
);
// add class menu entries
if ($owApp->erfurt->getAc()->isModelAllowed('edit', $owApp->selectedModel)) {
$classMenu->appendEntry(
'Create Instance',
"javascript:createInstanceFromClassURI('$resource');"
);
}
}
return $classMenu;
}
}