/
JosekiStore.php
executable file
·230 lines (193 loc) · 7.71 KB
/
JosekiStore.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
<?php
if( !defined( 'MEDIAWIKI' ) ) {
die( 'Not a valid entry point.' );
}
global $IP;
require_once("$IP/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php");
/**
* JosekiStore extends SMWSQLStore2 and forwards all update/delete to sparql endpoint.
* All updates are done via SPARQL Update syntax (http://www.w3.org/TR/sparql11-update/).
* The class was loosely based on/insipred by RAPStore.
* @author A.Chmieliauskas@tudeflt.nl , C.B.Davis@tudelft.nl
* @package SparqlExtension
*/
class JosekiStore extends SMWSQLStore2 {
public function JosekiStore() {
}
/**
* wraps removeDataForURI
* @param $subject
*/
function deleteSubject(Title $subject) {
$subject_uri = SMWExporter::expandURI($this->getURI($subject));
$this->removeDataForURI($subject_uri);
return parent::deleteSubject($subject);
}
/**
* deletes triples that have $uri as subject
* @param $uri
*/
function removeDataForURI($uri) {
$sparqlDeleteText = $this->writeDeleteText($uri);
wfDebugLog('SPARQL_LOG', "#===DELETE===\n".$sparqlDeleteText);
$response = $this->do_joseki_post($sparqlDeleteText);
return $response;
}
/**
* Does update. First deletes, then inserts.
* @param $data
*/
function updateData(SMWSemanticData $data) {
$export = SMWExporter::makeExportData($data);
$sparqlDeleteText = "";
$sparqlUpdateText = "INSERT DATA {\n";
// let other extensions add additional RDF data for this page (i.e. Semantic Internal Objects)
//this code is based on the modifications made on SemanticMediaWiki/includes/export/SMW_OWLExport.php
$additionalDataArray = array();
$fullexport = true;
$backlinks = false;
wfRunHooks( 'smwAddToRDFExport', array( $data->getSubject()->getTitle(), &$additionalDataArray, $fullexport, $backlinks ) );
// this writes update text for each of the Semantic Internal Objects
foreach ( $additionalDataArray as $additionalData ) {
$subject_uri = SMWExporter::expandURI($additionalData->getSubject()->getName());
// remove subject from triple store
$sparqlDeleteText .= $this->writeDeleteText($subject_uri);
//add new data associated with internal objects
$sparqlUpdateText .= $this->writeUpdateText($additionalData->getTripleList());
}
$subject_uri = SMWExporter::expandURI($export->getSubject()->getName());
// remove subject from triple store
$sparqlDeleteText .= $this->writeDeleteText($subject_uri);
$triple_list = $export->getTripleList();
$sparqlUpdateText .= $this->writeUpdateText($triple_list);
$sparqlUpdateText .= "}";
//delete the old triples
wfDebugLog('SPARQL_LOG', "#===DELETE===\n".$sparqlDeleteText);
$response = $this->do_joseki_post($sparqlDeleteText);
//insert the new triples
wfDebugLog('SPARQL_LOG', "#===INSERT===\n".$sparqlUpdateText);
$response = $this->do_joseki_post($sparqlUpdateText);
//can the delete and insert statements be combined, or will this lead to concurrency issues?
return parent::updateData($data);
}
/**
*write the SPARUL command to delete all triples with the subject specified by the uri
* @param $uri the subject to be deleted
*/
function writeDeleteText($uri) {
$sparqlDeleteText = "DELETE { <".$uri."> ?y ?z } WHERE { <".$uri."> ?y ?z }\n";
return $sparqlDeleteText;
}
/**
* write the SPARUL command to create an update for the triples
* @param $triplelist an array of triples
*/
function writeUpdateText($triplelist) {
$updateText = "";
foreach ($triplelist as $triple) {
$subject = $triple[0];
$predicate = $triple[1];
$object = $triple[2];
$obj_str = "";
$sub_str = "";
$pre_str = "";
if ($object instanceof SMWExpLiteral) {
$obj_str = "\"".$object->getName()."\"".(($object->getDatatype() == "") ? "" : "^^<".$object->getDatatype().">");
} else if ($object instanceof SMWExpResource) {
$obj_str = "<".SMWExporter::expandURI($object->getName()).">";
} else {
$obj_str = "\"\"";
}
if ($subject instanceof SMWExpResource) {
$sub_str = "<".SMWExporter::expandURI($subject->getName()).">";
}
if ($predicate instanceof SMWExpResource) {
$pre_str = "<".SMWExporter::expandURI($predicate->getName()).">";
}
$updateText .= $sub_str." ".$pre_str." ".$obj_str." .\n";
}
return $updateText;
}
/**
* Insert new pages into endpoint. Used to import data.
* @param $title
*/
function insertData(Title $title, $pageid) {
$newpage = SMWDataValueFactory::newTypeIDValue('_wpg');
$newpage->setValues($title->getDBkey(), $title->getNamespace(), $pageid);
$semdata = $this->getSemanticData($newpage);
$this->updateData($semdata);
}
/**
* Move/rename page
* @param $oldtitle
* @param $newtitle
* @param $pageid
* @param $redirid
*/
function changeTitle(Title $oldtitle, Title $newtitle, $pageid, $redirid=0) {
// Save it in parent store now!
// We need that so we get all information correctly!
$result = parent::changeTitle($oldtitle, $newtitle, $pageid, $redirid);
// delete old stuff
$old_uri = SMWExporter::expandURI($this->getURI($oldtitle));
$this->removeDataForURI($old_uri);
$newpage = SMWDataValueFactory::newTypeIDValue('_wpg');
$newpage->setValues($newtitle->getDBkey(), $newtitle->getNamespace(), $pageid);
$semdata = $this->getSemanticData($newpage);
$this->updateData($semdata);
$oldpage = SMWDataValueFactory::newTypeIDValue('_wpg');
$oldpage->setValues($oldtitle->getDBkey(), $oldtitle->getNamespace(), $redirid);
$semdata = $this->getSemanticData($oldpage);
$this->updateData($semdata,false);
return $result;
}
/**
* no setup required
* @param unknown_type $verbose
*/
function setup($verbose = true) {
return parent::setup($verbose);
}
function drop($verbose = true) {
return parent::drop();
}
/**
* Communicates with joseki update service via post
* @param $requestString
*/
function do_joseki_post($requestString) {
global $sparqlEndpointConfiguration;
$postdata = http_build_query(
array(
'request' => $requestString
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($sparqlEndpointConfiguration["update_url"], false, $context);
return $result;
}
/**
* Having a title of a page, what is the URI that is described by that page?
* The result still requires expandURI()
*/
protected function getURI($title) {
$uri = "";
if($title instanceof Title) {
$dv = SMWDataValueFactory::newTypeIDValue('_wpg');
$dv->setTitle($title);
$exp = $dv->getExportData();
$uri = $exp->getSubject()->getName();
} else {
// There could be other types as well that we do NOT handle here
}
return $uri; // still requires expandURI()
}
}