forked from AKSW/site.ontowiki
/
SiteController.php
352 lines (310 loc) · 14.6 KB
/
SiteController.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
<?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)
*/
/**
* The main controller class for the site component. This class
* provides an action to render a given resource
*
* @category OntoWiki
* @package OntoWiki_extensions_components_site
*/
class SiteController extends OntoWiki_Controller_Component
{
/**
* 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';
/**
* The site id which is part of the request URI as well as the template structure
*
* @var string|null
*/
private $_site = null;
public function init()
{
parent::init();
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout()->disableLayout();
$this->_relativeTemplatePath = $this->_owApp->extensionManager->getExtensionConfig('site')->templates;
}
/**
* Prints a simple robots.txt containing nothing but a sitemap rule.
* @access public
* @return void
* @todo Create zend route from ./robots.txt to site/robots
* @todo Change URL to .../sitemap.xml after zend route has been created
*/
public function robotsAction(){
header("Content-Type: text/plain");
print("Sitemap: ".$this->_config->urlBase."site/sitemap");
exit;
}
/**
* Renders and prints sitemap XML.
* For gzip compression add paramter "compression" with compression method "bzip" or "gzip" as value.
* Appending a name paramter with a file name will name your download file if you request via browser.
* @access public
* @return void
* @todo Create zend route from ./sitemap.xml to site/sitemap
* @todo Create zend route from ./sitemap.xml.gz to site/sitemap/compression/gzip/name/sitemap.xml.gz
* @todo Create zend route from ./sitemap.xml.bz2 to site/sitemap/compression/bzip/name/sitemap.xml.bz2
* @todo add support for sitemap index
*/
public function sitemapAction(){
$compression = $this->getParam( 'compression' );
# $page = (integer) $this->getParam( 'page' );
$pathGenerator = __DIR__.'/libraries/SitemapGenerator/classes/';
require_once ($pathGenerator.'Sitemap.php');
require_once ($pathGenerator.'Sitemap/URL.php');
require_once ($pathGenerator.'XML/Builder.php');
require_once ($pathGenerator.'XML/Node.php');
// Here we start the object cache id
$sitemapObjectCacheIdSource = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$sitemapObjectCacheId = 'sitemap_' . md5($sitemapObjectCacheIdSource);
// try to load the cached value
$erfurtObjectCache = OntoWiki::getInstance()->erfurt->getCache();
$erfurtQueryCache = OntoWiki::getInstance()->erfurt->getQueryCache();
$sitemapXml = $erfurtObjectCache->load($sitemapObjectCacheId);
if ($sitemapXml === false) {
$erfurtQueryCache->startTransaction($sitemapObjectCacheId);
$siteConfig = $this->_getSiteConfig();
$this->_loadModel();
$query = '
SELECT DISTINCT ?resourceUri ?modified
WHERE {
?resourceUri <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type.
OPTIONAL {?resourceUri <http://purl.org/dc/terms/modified> ?modified }
FILTER strstarts(str(?resourceUri), "'.$siteConfig['model'].'")
} ';
//OPTIONAL {?resourceUri <http://purl.org/dc/terms/modified> ?modified }
//?resourceUri <http://purl.org/dc/terms/modified> ?modified
$results = $this->_model->sparqlQuery($query);
$sitemap = new Sitemap();
foreach ($results as $result) {
$url = new Sitemap_URL ($result['resourceUri']);
if (isset($result['modified']) && strlen($result['modified']))
$url->setDatetime ($result['modified']);
$sitemap->addUrl ($url);
}
$sitemapXml = $sitemap->render();
// save the page body as an object value for the object cache
$erfurtObjectCache->save($sitemapXml, $sitemapObjectCacheId);
// close the object cache transaction
$erfurtQueryCache->endTransaction($sitemapObjectCacheId);
}
$contentType = "application/xml";
// compression has been requested
if(strlen(trim($compression))){
switch(strtolower($compression)){
case 'bzip':
$sitemapXml = bzcompress($sitemapXml);
$contentType = "application/x-bzip";
header('Content-Encoding: bzip2');
break;
case 'gzip':
$sitemapXml = gzencode($sitemapXml);
$contentType = "application/x-gzip";
header('Content-Encoding: gzip');
break;
}
}
header('Content-Length: '.strlen($sitemapXml));
header("Content-Type: ".$contentType);
print($sitemapXml);
exit;
}
/*
* to allow multiple template sets, every action is mapped to a template directory
*/
public function __call($method, $args)
{
$action = $this->_request->getActionName();
$router = $this->_owApp->getBootstrap()->getResource('Router');
if ($router->hasRoute('empty')) {
$emptyRoute = $router->getRoute('empty');
$defaults = $emptyRoute->getDefaults();
$defaultAction = $defaults['action'];
}
if (empty($action) || (isset($defaultAction) && $action === $defaultAction) || $action === 'index') {
// use default site for empty or default action (index)
$this->_site = $this->_privateConfig->defaultSite;
} else {
// use action as site otherwise
$this->_site = $action;
}
$this->getComponentHelper()->setSite($this->_site);
$templatePath = $this->_owApp->extensionManager->getComponentTemplatePath('site');
$mainTemplate = sprintf('%s/%s', $this->_site, self::MAIN_TEMPLATE_NAME);
if (is_readable($templatePath . $mainTemplate)) {
$this->moduleContext = 'site.' . $this->_site;
// $this->addModuleContext($this->moduleContext);
$this->_loadModel();
$this->_loadResource();
// Here we start the object cache id
$siteModuleObjectCacheIdSource = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$siteModuleObjectCacheId = 'site_' . md5($siteModuleObjectCacheIdSource);
// try to load the cached value
$erfurtObjectCache = OntoWiki::getInstance()->erfurt->getCache();
$erfurtQueryCache = OntoWiki::getInstance()->erfurt->getQueryCache();
$cache = $erfurtObjectCache->load($siteModuleObjectCacheId);
if (!is_array($cache)) {
$erfurtQueryCache->startTransaction($siteModuleObjectCacheId);
$cache = array(
'code' => 200,
'headers' => array('Content-Type' => 'text/html; encoding=utf-8'),
);
$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?
}
}
$moduleTemplatePath = $this->_componentRoot
. $this->_relativeTemplatePath
. DIRECTORY_SEPARATOR
. $this->_privateConfig->defaultSite
. DIRECTORY_SEPARATOR
. 'modules';
// add module template override path
if (is_readable($moduleTemplatePath)) {
$scriptPaths = $this->view->getScriptPaths();
array_push($scriptPaths, $moduleTemplatePath);
$this->view->setScriptPath($scriptPaths);
}
// with assignment, direct access is possible ($this->basePath).
$this->view->assign($this->_getTemplateData());
// this allows for easy re-assignment of everything
$this->view->templateData = $this->_getTemplateData();
// generate the page body
$cache['body'] = $this->view->render($mainTemplate);
// save the page body as an object value for the object cache
$erfurtObjectCache->save($cache, $siteModuleObjectCacheId);
// close the object cache transaction
$erfurtQueryCache->endTransaction($siteModuleObjectCacheId);
}
// set the page content
$this->_response->setHttpResponseCode($cache['code']);
foreach ($cache['headers'] as $header => $content) {
$this->_response->setHeader($header, $content);
}
$this->_response->setBody($cache['body']);
} else {
$this->_response->setHttpResponseCode(404);
$this->_response->setRawHeader('HTTP/1.0 404 Not Found');
$this->_response->setBody($this->view->render('404.phtml'));
}
}
private function _getTemplateData()
{
// prepare namespace array with presets of rdf, rdfs and owl
$namespaces = array(
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'owl' => 'http://www.w3.org/2002/07/owl#'
);
foreach ($this->_model->getNamespaces() as $ns => $prefix) {
$namespaces[$prefix] = $ns;
}
// this template data is given to ALL templates (with renderx)
$templateData = array(
'siteId' => $this->_site,
'siteConfig' => $this->_getSiteConfig(),
'generator' => $this->_config->version->label . ' ' . $this->_config->version->number,
'pingbackUrl' => $this->_owApp->getUrlBase() . 'pingback/ping',
'wikiBaseUrl' => $this->_owApp->getUrlBase(),
'themeUrlBase' => $this->view->themeUrlBase,
'libraryUrlBase' => $this->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' => $this->moduleContext,
'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;
}
protected 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;
}
}
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;
}
protected function _getSiteConfig()
{
return $this->getComponentHelper()->getSiteConfig();
}
}