-
Notifications
You must be signed in to change notification settings - Fork 6
/
SiteHelper.php
691 lines (586 loc) · 24.1 KB
/
SiteHelper.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
<?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)
*/
require_once 'MarkupInterface.php';
/**
* A helper class for the site component.
*
* @category OntoWiki
* @package OntoWiki_extensions_components_site
*/
class SiteHelper extends OntoWiki_Component_Helper
{
/**
* Name of per-site config file
*/
const SITE_CONFIG_FILENAME = 'config.ini';
/**
* The main template filename
*/
const MAIN_TEMPLATE_NAME = 'layout.phtml';
/**
* The model URI of the selected model or the uri which is given
* by the m parameter
*
* @var string|null
*/
private $_modelUri = null;
/**
* The selected model or the model which is given
* by the m parameter
*/
private $_model = null;
/**
* The resource URI of the requested resource or the uri which is given
* by the r parameter
*
* @var string|null
*/
private $_resourceUri = null;
/**
* relative Path to the extension template folder
*/
private $_relativeTemplatePath = 'templates';
/**
* Current site (if in use)
* @var string|null
*/
protected $_site = null;
/**
* Site config for the current site.
* @var array
*/
protected $_siteConfig = null;
/**
* Current pseudo file extension.
* @var string
*/
protected $_currentSuffix = '';
/*
* used schema URIs
*/
const TEMPLATE_PROP_CLASS = 'http://ns.ontowiki.net/SysOnt/Site/classTemplate';
const TEMPLATE_PROP_RESOURCE = 'http://ns.ontowiki.net/SysOnt/Site/template';
const TYPE_PROP = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
const SUBCLASS_PROP = 'http://www.w3.org/2000/01/rdf-schema#subClassOf';
public function init()
{
$this->_relativeTemplatePath = $this->_owApp->extensionManager->getExtensionConfig('site')->templates;
}
public function onAnnounceWorker($event)
{
$event->registry->registerJob(
'makePageCache',
'extensions/site/jobs/MakePageCache.php',
'Site_Job_MakePageCache'
);
$event->registry->registerJob(
'makeSiteCache',
'extensions/site/jobs/MakeSiteCache.php',
'Site_Job_MakeSiteCache'
);
$event->registry->registerJob(
'exportPage',
'extensions/site/jobs/ExportPage.php',
'Site_Job_ExportPage'
);
$event->registry->registerJob(
'exportSite',
'extensions/site/jobs/ExportSite.php',
'Site_Job_ExportSite'
);
}
public function onPostBootstrap($event)
{
$router = $event->bootstrap->getResource('Router');
$request = Zend_Controller_Front::getInstance()->getRequest();
$controller = $request->getControllerName();
$action = $request->getActionName();
if ($router->hasRoute('empty')) {
$emptyRoute = $router->getRoute('empty');
$defaults = $emptyRoute->getDefaults();
$defaultController = $defaults['controller'];
$defaultAction = $defaults['action'];
// are we currently following the empty route?
if ($controller === $defaultController && $action === $defaultAction) {
/* TODO: this should not be the default site but the site which
matches the model of the selected resource */
$siteConfig = $this->getSiteConfig();
if (isset($siteConfig['index'])) {
// TODO: detect accept header
$indexResource = $siteConfig['index'] . $this->getCurrentSuffix();
$requestUri = $this->_config->urlBase
. ltrim($request->getRequestUri(), '/');
// redirect if request URI does not match index resource
if ($requestUri !== $indexResource) {
// response not ready yet, do it the PHP way
header('Location: ' . $indexResource, true, 303);
exit;
}
}
}
$emptyRoute = new Zend_Controller_Router_Route(
'',
array(
'controller' => 'site',
'action' => $this->_privateConfig->defaultSite
)
);
$router->addRoute('empty', $emptyRoute);
}
}
public function onCreateMenu($event) {
$request = Zend_Controller_Front::getInstance()->getRequest();
$controller = $request->getControllerName();
$action = $request->getActionName();
if ($controller === 'resource' && $action === 'properties') {
$site = $this->getSiteConfig();
$resourceUri = $this->_owApp->selectedResource;
if (!empty($resourceUri) && $resourceUri != (string)$this->_owApp->selectedModel) {
if (strpos($resourceUri, $site['model']) === 0) {
$resourceUri .= '.html';
} else {
$resourceUri = new OntoWiki_Url(array('controller' => 'site', 'action' => $site['id']), array('r'));
$resourceUri = (string)$resourceUri; // build URL while site is set
}
}
$toolbar = OntoWiki_Toolbar::getInstance();
$toolbar->prependButton(OntoWiki_Toolbar::SEPARATOR)
->prependButton(
OntoWiki_Toolbar::SUBMIT,
array(
'name' => 'Back to Site',
'url' => $resourceUri
)
);
}
}
public function onResolveDomainAliasInput($event)
{
$siteconfig = $this->getSiteConfig();
$aliasconfig = array();
if (!isset($siteconfig['alias'])) {
return;
}
$siteconfig['alias'];
if (!is_array($siteconfig['alias']) && trim($siteconfig['alias'])) {
$aliasconfig = array($siteconfig['alias']);
}
else
{
$aliasconfig = $siteconfig['alias'];
}
if (count($aliasconfig) < 1) {
return;
}
$event->uri = str_replace($aliasconfig, $siteconfig['model'], $event->uri);
return;
}
public function onShouldLinkedDataRedirect($event)
{
if ($event->type) {
// Supported type?
$requestUri = $event->request->getServer('REQUEST_URI');
$requestUriPath = parse_url($requestUri, PHP_URL_PATH);
$parts = explode('.', $requestUriPath);
if ($parts[count($parts)-1] != $event->type) {
header('Location: ' . $event->uri . '.' . $event->type, true, 302);
exit;
}
} else {
return false;
}
if ($event->type === 'html') {
$event->request->setControllerName('site');
$event->request->setActionName($this->_privateConfig->defaultSite);
if ($event->flag) {
$this->_currentSuffix = '.html';
}
} else {
// export
$event->request->setControllerName('resource');
$event->request->setActionName('export');
$event->request->setParam('f', $event->type);
$event->request->setParam('r', $event->uri);
}
$event->request->setDispatched(false);
return false;
}
public function getSiteConfig()
{
if (null === $this->_siteConfig) {
$this->_siteConfig = array();
$site = $this->_privateConfig->defaultSite;
$typesfolder = $this->_privateConfig->subfolderTypes;
$relativeTemplatePath = OntoWiki::getInstance()->extensionManager->getExtensionConfig('site')->templates;
// load the site config
$configFilePath = sprintf('%s/%s/%s/%s', $this->getComponentRoot(), $relativeTemplatePath, $site, self::SITE_CONFIG_FILENAME);
if (is_readable($configFilePath)) {
if ($config = parse_ini_file($configFilePath, true)) {
$this->_siteConfig = $config;
}
}
// add the site id to the config in order to allow correct URIs
$this->_siteConfig['id'] = $site;
// subfolder for resource type templates
if (!isset($this->_siteConfig['subfolderTypes'])) {
$this->_siteConfig['subfolderTypes'] = $typesfolder;
}
}
return $this->_siteConfig;
}
public function getCurrentSuffix()
{
return $this->_currentSuffix;
}
public static function skosNavigationAsArray($titleHelper)
{
$store = OntoWiki::getInstance()->erfurt->getStore();
$model = OntoWiki::getInstance()->selectedModel;
$query = '
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX sysont: <http://ns.ontowiki.net/SysOnt/>
SELECT ?topConcept ?altLabel
FROM <' . (string)$model . '>
WHERE {
?cs a skos:ConceptScheme .
?topConcept skos:topConceptOf ?cs
OPTIONAL {
?topConcept sysont:order ?order
}
OPTIONAL {
?topConcept skos:altLabel ?altLabel
}
}
ORDER BY ASC(?order)
';
if ($result = $store->sparqlQuery($query)) {
$tree = array();
foreach ($result as $row) {
$topConcept = new stdClass;
$topConcept->uri = $row['topConcept'];
$titleHelper->addResource($topConcept->uri);
if ($row['altLabel'] != null) {
$topConcept->altLabel = $row['altLabel'];
}
$subQuery = '
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX sysont: <http://ns.ontowiki.net/SysOnt/>
SELECT ?subConcept ?altLabel
FROM <' . (string)$model . '>
WHERE {
?subConcept skos:broader <' . $topConcept->uri . '>
OPTIONAL {
?subConcept sysont:order ?order
}
OPTIONAL {
?subConcept skos:altLabel ?altLabel
}
}
ORDER BY ASC(?order)
';
if ($subConceptsResult = $store->sparqlQuery($subQuery)) {
$subconcepts = array();
foreach ($subConceptsResult as $subConceptRow) {
$subConcept = new stdClass;
$subConcept->uri = $subConceptRow['subConcept'];
$subConcept->subconcepts = array();
if ($subConceptRow['altLabel'] != null) {
$subConcept->altLabel = $subConceptRow['altLabel'];
}
$titleHelper->addResource($subConcept->uri);
$subconcepts[$subConcept->uri] = $subConcept;
}
$topConcept->subconcepts = $subconcepts;
} else {
$topConcept->subconcepts = array();
}
$tree[$topConcept->uri] = $topConcept;
}
return $tree;
}
return array();
}
public function setSite($site)
{
$this->_site = (string)$site;
}
public function getAllURIs()
{
$this->loadModel();
$store = OntoWiki::getInstance()->erfurt->getStore();
// get all classes with template set in some way
$classes = $this->_model->sparqlQuery('
SELECT DISTINCT ?resourceUri
FROM <http://starpages.dev.eccenca.com/>
WHERE {
?resourceUri <' . self::TEMPLATE_PROP_CLASS . '> ?template.
}
');
$classes = array_map(function($_) { return $_['resourceUri']; }, $classes);
$closure = $store->getTransitiveClosure($this->_model, self::SUBCLASS_PROP, $classes, true);
$classes = array_keys($closure);
// get all resources with templates set directly or with class
$query = '
SELECT DISTINCT ?resourceUri
FROM <http://starpages.dev.eccenca.com/>
WHERE {
{
?resourceUri <' . self::TEMPLATE_PROP_RESOURCE . '> ?template.
}
UNION
{
?resourceUri <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?class.
FILTER (
?class IN (' . implode(',', array_map(function($_) { return "<$_>"; }, $classes)) . ')
)
}
}
';
$URIs = $this->_model->sparqlQuery($query);
$URIs = array_map(function($_) { return $_['resourceUri']; }, $URIs);
return $URIs;
}
public function getAllSitemapUris(){
$siteConfig = $this->getSiteConfig();
// determine resource types
$types = array('?type');
if (!empty($siteConfig['sitemap_types'])) {
$types = explode(',', $siteConfig['sitemap_types']);
}
foreach ($types as $nr => $type) {
$types[$nr] = '{?resourceUri <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> '.$type.'}';
}
$query = '
SELECT DISTINCT ?resourceUri ?modified
FROM <http://schema.org/>
WHERE {
'.join(' UNION ', $types).' .
FILTER strStarts(str(?resourceUri), "'.$siteConfig['model'].'") .
OPTIONAL {
?resourceUri <http://purl.org/dc/terms/modified> ?modified .
}
}
ORDER BY DESC(?modified)';
$this->loadModel();
return $this->_model->sparqlQuery($query);
}
public function getPage($uri = null)
{
$this->loadModel();
$this->_loadResource();
// prepare view
$moduleTemplatePath = $this->_componentRoot
. $this->_relativeTemplatePath
. DIRECTORY_SEPARATOR
. $this->_privateConfig->defaultSite
. DIRECTORY_SEPARATOR
. 'modules';
$config = OntoWiki::getInstance()->config;
// TODO merge with Bootstrap::_initView
$defaultTemplatePath = ONTOWIKI_ROOT
. 'application/views/templates';
$themeTemplatePath = ONTOWIKI_ROOT
. $config->themes->path
. $config->themes->default
. 'templates';
$viewOptions = array(
'use_module_cache' => (bool)$config->cache->modules,
'cache_path' => $config->cache->path,
'lang' => $config->languages->locale,
);
$translate = $GLOBALS['application']->getBootstrap()->getResource('Translate');
$view = new OntoWiki_View($viewOptions, $translate);
$view->addScriptPath($defaultTemplatePath) // default templates
->addScriptPath($themeTemplatePath) // theme templates override default ones
->addScriptPath($config->extensions->base) // extension templates
->setEncoding($config->encoding)
->setHelperPath(ONTOWIKI_ROOT . 'application/classes/OntoWiki/View/Helper', 'OntoWiki_View_Helper');
$view->strictVars(defined('_OWDEBUG'));
// TODO merge with Controller::Base
$view->themeUrlBase = $config->themeUrlBase;
$view->urlBase = $config->urlBase;
$view->staticUrlBase = $config->staticUrlBase;
$view->libraryUrlBase = $config->staticUrlBase . 'libraries/';
$cache = array(
'code' => 200,
'headers' => array('Content-Type' => 'text/html; encoding=utf-8'),
);
$templatePath = $this->_owApp->extensionManager->getComponentTemplatePath('site');
$mainTemplate = sprintf('%s/%s', $this->_site, static::MAIN_TEMPLATE_NAME);
// TODO merge with Controller::Component
$cm = $this->_owApp->extensionManager;
$name = 'site';
// set component specific template path
if ($tp = $cm->getComponentTemplatePath($name)) {
$view->addScriptPath($tp);
}
// set component specific helper path
if ($hp = $cm->getComponentHelperPath($name)) {
$view->addHelperPath($hp, ucfirst($name) . '_View_Helper_');
}
// set component root dir
$this->_componentRoot = $cm->getExtensionPath()
. $name
. '/';
// set component root url
$this->_componentUrlBase = $this->_config->staticUrlBase
. $this->_config->extensions->base
. $name
. '/';
if (!is_readable($templatePath . $mainTemplate)) {
$cache['code'] = 404;
$cache['body'] = $view->render('404.phtml');
return $cache;
}
$description = $this->_resource->getDescription();
if (!empty($description[$this->_resourceUri][EF_RDF_TYPE])) {
$type = $description[$this->_resourceUri][EF_RDF_TYPE][0]['value'];
if ($type === 'http://ns.ontowiki.net/SysOnt/Site/MovedResource') {
if (!empty($description[$this->_resourceUri]['http://ns.ontowiki.net/SysOnt/Site/seeAlso'])) {
$cache['code'] = 303;
$cache['headers']['Location'] = $description[$this->_resourceUri]['http://ns.ontowiki.net/SysOnt/Site/seeAlso'][0]['value'];
} else {
// FIXME
$cache['code'] = 500;
}
// TODO use different template?
}
}
// add module template override path
if (is_readable($moduleTemplatePath)) {
$scriptPaths = $view->getScriptPaths();
array_push($scriptPaths, $moduleTemplatePath);
$view->setScriptPath($scriptPaths);
}
// with assignment, direct access is possible ($this->basePath).
$view->assign($this->_getTemplateData($view));
// this allows for easy re-assignment of everything
$view->templateData = $this->_getTemplateData($view);
// generate the page body
$cache['body'] = $view->render($mainTemplate);
return $cache;
}
protected function _cacheId($uri)
{
if ($uri === null) {
$uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $this->_config->languages->locale . var_export($_POST, true);
}
return 'site_' . md5($uri);
}
public function testCache($uri = null)
{
$id = $this->_cacheId($uri);
$erfurtObjectCache = OntoWiki::getInstance()->erfurt->getCache();
return $erfurtObjectCache->test($id);
}
public function removeCache($uri = null)
{
$id = $this->_cacheId($uri);
$erfurtObjectCache = OntoWiki::getInstance()->erfurt->getCache();
return $erfurtObjectCache->remove($id);
}
public function makeCache($uri = null)
{
$id = $this->_cacheId($uri);
$erfurtObjectCache = OntoWiki::getInstance()->erfurt->getCache();
$erfurtQueryCache = OntoWiki::getInstance()->erfurt->getQueryCache();
$erfurtQueryCache->startTransaction($id);
$erfurtObjectCache->save($cache = $this->getPage($uri), $id);
$erfurtQueryCache->endTransaction($id);
return $cache;
}
public function loadCache($uri = null)
{
$id = $this->_cacheId($uri);
$erfurtObjectCache = OntoWiki::getInstance()->erfurt->getCache();
return $erfurtObjectCache->load($id);
}
public function setUrlBase($url)
{
$this->_urlBase = $url;
}
public function getUrlBase()
{
if (!isset($this->_urlBase)) {
return $this->_owApp->getUrlBase();
}
return $this->_urlBase;
}
private function _getTemplateData($view)
{
$namespaces = $this->_model->getNamespacePrefixes();
// add presets for rdf, rdfs and owl
$namespaces['rdf'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
$namespaces['rdfs'] = 'http://www.w3.org/2000/01/rdf-schema#';
$namespaces['owl'] = 'http://www.w3.org/2002/07/owl#';
// this template data is given to ALL templates (with renderx)
$templateData = array(
'siteId' => $this->_site,
'systemConfig' => $this->_config,
'siteConfig' => $this->getSiteConfig(),
'generator' => $this->_config->version->label . ' ' . $this->_config->version->number,
'pingbackUrl' => $this->getUrlBase() . 'pingback/ping',
'wikiBaseUrl' => $this->getUrlBase(),
'themeUrlBase' => $view->themeUrlBase,
'libraryUrlBase' => $view->libraryUrlBase,
'basePath' => sprintf('%s%s/%s', $this->_componentRoot, $this->_relativeTemplatePath, $this->_site),
'baseUri' => sprintf('%s%s/%s', $this->_componentUrlBase, $this->_relativeTemplatePath, $this->_site),
'context' => 'site.' . $this->_site,
'namespaces' => $namespaces,
'model' => $this->_model,
'modelUri' => $this->_modelUri,
'title' => $this->_resource->getTitle(),
'resourceUri' => (string) $this->_resourceUri,
'description' => $this->_resource->getDescription(),
'descriptionHelper' => $this->_resource->getDescriptionHelper(),
'site' => array(
'index' => 0,
'name' => 'Home'
),
'navigation' => array(),
'options' => array(),
);
return $templateData;
}
public function loadModel()
{
$siteConfig = $this->getSiteConfig();
// m is automatically used and selected
if ((!isset($this->_request->m)) && (!$this->_owApp->selectedModel)) {
// TODO: what if no site model configured?
if (!Erfurt_Uri::check($siteConfig['model'])) {
$site = $this->_privateConfig->defaultSite;
$root = $this->getComponentHelper()->getComponentRoot();
$configFilePath = sprintf('%s%s/%s/%s', $root, $this->_relativeTemplatePath, $site, SiteHelper::SITE_CONFIG_FILENAME);
throw new OntoWiki_Exception(
'No model selected! Please, configure a site model by setting the option '
. '"model=..." in "' . $configFilePath . '" or specify parameter m in the URL.'
);
} else {
// setup the model
$this->_modelUri = $siteConfig['model'];
$store = OntoWiki::getInstance()->erfurt->getStore();
$this->_model = $store->getModel($this->_modelUri);
OntoWiki::getInstance()->selectedModel = $this->_model;
}
} else {
$this->_model = $this->_owApp->selectedModel;
$this->_modelUri = (string) $this->_owApp->selectedModel;
}
return $this->_model;
}
protected function _loadResource()
{
// r is automatically used and selected, if not then we use the model uri as starting point
if ((!isset($this->_request->r)) && (!$this->_owApp->selectedResource)) {
OntoWiki::getInstance()->selectedResource = new OntoWiki_Resource($this->_modelUri, $this->_model);
}
$this->_resource = $this->_owApp->selectedResource;
$this->_resourceUri = (string) $this->_owApp->selectedResource;
}
}