-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhresnel.php
140 lines (125 loc) · 4.56 KB
/
Phresnel.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
<?php
/**
* Copyright 2011 Felix Ostrowski, hbz
*
* This file is part of Phresnel.
*
* Phresnel is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Phresnel is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Phresnel. If not, see <http://www.gnu.org/licenses/>.
*/
require_once(dirname(__FILE__) . '/lib/LibRDF/LibRDF/LibRDF.php');
require_once(dirname(__FILE__) . '/lib/KLogger/src/KLogger.php');
require_once(dirname(__FILE__) . '/Lens.php');
require_once(dirname(__FILE__) . '/AbstractBoxModel.php');
require_once(dirname(__FILE__) . '/AbstractFormBoxModel.php');
require_once(dirname(__FILE__) . '/HTMLTableBoxModel.php');
require_once(dirname(__FILE__) . '/HTMLTableFormBoxModel.php');
require_once(dirname(__FILE__) . '/RemoteSPARQLEndpoint.php');
require_once(dirname(__FILE__) . '/LocalSPARQLEndpoint.php');
/**
* Phresnel lens factory
*
*/
class Phresnel {
/**
* The graph containing lens definitions.
*
* @var LibRDF_Model Defaults to null.
*/
protected static $_lensGraph = null;
/**
* The uri containing lens definitions.
*
* @var LibRDF_Model Defaults to null.
*/
protected static $_lensGraphURI = null;
/**
* The remote SPARQL endpoint to connect to.
*
* @var SPARQLEndpoint Defaults to null.
*/
protected static $_endpoint = null;
/**
* The logger to write to.
*
* @var KLogger Defaults to null.
*/
protected static $_logger = null;
/**
* Namespace mappings.
*
* @var array
*/
public static $_namespaces = array();
public static function registerNamespaces($namespaces) {
self::$_namespaces = $namespaces;
}
public static function init($conf, KLogger $logger) {
self::$_logger = $logger;
$setup = array();
if (!(is_array($conf))) {
$rdfconf = new LibRDF_Model(new LibRDF_Storage());
$rdfconf->loadStatementsFromURI(new LibRDF_Parser("turtle"), $conf);
$epProp = new LibRDF_URINode("http://literarymachine.net/ontology/phresnel#endpoint");
$epNode = $rdfconf->findStatements(null, $epProp, null)->current();
if (null !== $epNode) {
$epURL = $epNode->getObject();
$epURL = substr($epURL, 1, strlen($epURL) - 2);
$setup['endpoint'] = $epURL;
}
$lensProp = new LibRDF_URINode("http://literarymachine.net/ontology/phresnel#lenses");
$lensDef = $rdfconf->findStatements(null, $lensProp, null)->current();
$lensGraph = new LibRDF_Model(new LibRDF_Storage());
$setup["lensDef"] = substr($lensDef->getObject(), 1, strlen($lensDef->getObject()) - 2);
} else {
$setup = $conf;
}
if (array_key_exists("endpoint", $setup)) {
self::$_endpoint = new RemoteSPARQLEndpoint($setup["endpoint"], $logger);
} else {
$model = new LibRDF_Model(new LibRDF_Storage);
self::$_endpoint = new LocalSPARQLEndpoint($model, $logger);
}
LibRDF_Serializer::setNamespaces(self::$_namespaces);
$lensGraph = new LibRDF_Model(new LibRDF_Storage());
$lensGraph->loadStatementsFromURI(new LibRDF_Parser("turtle"), $setup["lensDef"]);
self::$_lensGraph = $lensGraph;
self::$_lensGraphURI = $setup["lensDef"];
}
/**
* Get a Lens
*
* @param string $lensURI
* @return Lens
*/
public static function getLens($lensURI) {
if (self::$_endpoint instanceof localSPARQLEndpoint) {
$caching = false;
} else {
$caching = true;
}
if ($lensURI instanceof LibRDF_Node) {
$lensURI = substr($lensURI, 1, strlen($lensURI) - 2);
} else {
$lensURI = self::$_lensGraphURI . "#$lensURI";
}
return new Lens(self::$_lensGraph, $lensURI,
null, self::$_endpoint, self::$_logger, $caching);
}
public static function getEndpoint() {
return self::$_endpoint;
}
public static function setEndpoint(SPARQLEndpoint $ep) {
self::$_endpoint = $ep;
}
}