forked from AKSW/site.ontowiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SiteController.php
240 lines (206 loc) · 9.3 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
<?php
/**
* This file is part of the {@link http://ontowiki.net OntoWiki} project.
*
* @copyright Copyright (c) 2011, {@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;
}
/*
* 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'];
}
$siteConfig = $this->_getSiteConfig();
if (empty($action) || (isset($defaultAction) && $action === $defaultAction) || $action === 'index') {
// use default site for empty or default action (index)
$this->_site = $siteConfig['id'];
} 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();
$cachePageContent = $erfurtObjectCache->load($siteModuleObjectCacheId);
if ($cachePageContent != false) {
$this->_response->setBody($cachePageContent); // send cached body instead of generating a new one
return;
} else {
$erfurtQueryCache->startTransaction($siteModuleObjectCacheId);
}
$moduleTemplatePath = $this->_componentRoot
. $this->_relativeTemplatePath
. DIRECTORY_SEPARATOR
. $siteConfig['id']
. 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
$bodyContent = $this->view->render($mainTemplate);
// save the page body as an object value for the object cache
$erfurtObjectCache->save($bodyContent, $siteModuleObjectCacheId);
// close the object cache transaction
$erfurtQueryCache->endTransaction($siteModuleObjectCacheId);
// set the page content
$this->_response->setBody($bodyContent);
$this->_response->setHeader('Content-Type', 'text/html; encoding=utf-8');
} else {
$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' => 'OntoWiki ' . $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 = $siteConfig['id'];
$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();
}
}