rdflib 2.2.35 → 2.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rdflib.min.js +1 -1
- package/dist/rdflib.min.js.LICENSE.txt +0 -2
- package/dist/rdflib.min.js.map +1 -1
- package/esm/blank-node.js +57 -82
- package/esm/class-order.js +1 -1
- package/esm/collection.js +70 -103
- package/esm/default-graph.js +13 -30
- package/esm/empty.js +8 -23
- package/esm/factories/canonical-data-factory.js +33 -29
- package/esm/factories/extended-term-factory.js +18 -13
- package/esm/factories/factory-types.js +1 -1
- package/esm/factories/rdflib-data-factory.js +9 -11
- package/esm/fetcher.js +1366 -1651
- package/esm/formula.js +631 -736
- package/esm/index.js +31 -48
- package/esm/jsonldparser.js +19 -26
- package/esm/jsonparser.js +1 -1
- package/esm/lists.js +38 -86
- package/esm/literal.js +120 -154
- package/esm/log.js +7 -7
- package/esm/n3parser.js +1009 -1086
- package/esm/named-node.js +69 -96
- package/esm/namespace.js +2 -4
- package/esm/node-internal.js +73 -96
- package/esm/node.js +1 -1
- package/esm/parse.js +3 -3
- package/esm/patch-parser.js +1 -1
- package/esm/query.js +15 -16
- package/esm/rdfaparser.js +775 -841
- package/esm/rdfxmlparser.js +348 -364
- package/esm/serialize.js +2 -2
- package/esm/serializer.js +835 -877
- package/esm/statement.js +52 -71
- package/esm/store.js +853 -957
- package/esm/types.js +21 -21
- package/esm/update-manager.js +965 -1100
- package/esm/updates-via.js +104 -132
- package/esm/uri.js +3 -3
- package/esm/utils/default-graph-uri.js +2 -2
- package/esm/utils/terms.js +4 -5
- package/esm/utils-js.js +5 -5
- package/esm/utils.js +6 -6
- package/esm/variable.js +32 -55
- package/esm/xsd.js +2 -2
- package/lib/blank-node.js +57 -80
- package/lib/class-order.js +1 -1
- package/lib/collection.js +70 -101
- package/lib/default-graph.js +14 -29
- package/lib/empty.js +9 -22
- package/lib/factories/canonical-data-factory.js +35 -31
- package/lib/factories/extended-term-factory.js +18 -13
- package/lib/factories/factory-types.js +1 -1
- package/lib/factories/rdflib-data-factory.js +9 -11
- package/lib/fetcher.js +1375 -1654
- package/lib/formula.js +632 -735
- package/lib/index.js +80 -84
- package/lib/jsonldparser.js +21 -32
- package/lib/jsonparser.js +1 -1
- package/lib/lists.js +47 -87
- package/lib/literal.js +121 -153
- package/lib/log.js +7 -7
- package/lib/n3parser.js +1012 -1090
- package/lib/named-node.js +70 -95
- package/lib/namespace.js +2 -4
- package/lib/node-internal.js +73 -94
- package/lib/node.js +1 -1
- package/lib/parse.js +5 -6
- package/lib/patch-parser.js +1 -1
- package/lib/query.js +20 -18
- package/lib/rdfaparser.js +778 -843
- package/lib/rdfxmlparser.js +351 -365
- package/lib/serialize.js +2 -2
- package/lib/serializer.js +840 -880
- package/lib/statement.js +55 -73
- package/lib/store.js +860 -958
- package/lib/types.js +21 -21
- package/lib/update-manager.js +970 -1102
- package/lib/updates-via.js +107 -132
- package/lib/uri.js +3 -3
- package/lib/utils/default-graph-uri.js +2 -2
- package/lib/utils/terms.js +4 -6
- package/lib/utils-js.js +8 -9
- package/lib/utils.js +6 -6
- package/lib/variable.js +35 -57
- package/lib/xsd.js +2 -2
- package/package.json +2 -2
- package/src/n3parser.js +1 -1
- package/src/serializer.js +1 -1
- package/src/update-manager.ts +2 -1
- package/.babelrc +0 -20
package/lib/update-manager.js
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
var _typeof3 = require("@babel/runtime/helpers/typeof");
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
10
|
-
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
11
|
-
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
12
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
13
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
14
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
15
9
|
var _store = _interopRequireDefault(require("./store"));
|
|
16
10
|
var _uri = require("./uri");
|
|
@@ -20,28 +14,26 @@ var _serializer = _interopRequireDefault(require("./serializer"));
|
|
|
20
14
|
var _terms = require("./utils/terms");
|
|
21
15
|
var Util = _interopRequireWildcard(require("./utils-js"));
|
|
22
16
|
var _termValue = require("./utils/termValue");
|
|
23
|
-
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function
|
|
24
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" !=
|
|
25
|
-
|
|
26
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
27
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } /* @file Update Manager Class
|
|
17
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
18
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
19
|
+
/* @file Update Manager Class
|
|
28
20
|
**
|
|
29
21
|
** 2007-07-15 original SPARQL Update module by Joe Presbrey <presbrey@mit.edu>
|
|
30
22
|
** 2010-08-08 TimBL folded in Kenny's WEBDAV
|
|
31
23
|
** 2010-12-07 TimBL added local file write code
|
|
32
24
|
*/
|
|
25
|
+
|
|
33
26
|
/**
|
|
34
27
|
* The UpdateManager is a helper object for a store.
|
|
35
28
|
* Just as a Fetcher provides the store with the ability to read and write,
|
|
36
29
|
* the Update Manager provides functionality for making small patches in real time,
|
|
37
30
|
* and also looking out for concurrent updates from other agents
|
|
38
31
|
*/
|
|
39
|
-
|
|
32
|
+
class UpdateManager {
|
|
40
33
|
/**
|
|
41
34
|
* @param store - The quadstore to store data and metadata. Created if not passed.
|
|
42
35
|
*/
|
|
43
|
-
|
|
44
|
-
(0, _classCallCheck2.default)(this, UpdateManager);
|
|
36
|
+
constructor(store) {
|
|
45
37
|
(0, _defineProperty2.default)(this, "store", void 0);
|
|
46
38
|
(0, _defineProperty2.default)(this, "ifps", void 0);
|
|
47
39
|
(0, _defineProperty2.default)(this, "fps", void 0);
|
|
@@ -71,1206 +63,1082 @@ var UpdateManager = exports.default = /*#__PURE__*/function () {
|
|
|
71
63
|
this.ns.owl = (0, _namespace.default)('http://www.w3.org/2002/07/owl#');
|
|
72
64
|
this.patchControl = [];
|
|
73
65
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (!this.patchControl[doc.value]) {
|
|
78
|
-
this.patchControl[doc.value] = [];
|
|
79
|
-
}
|
|
80
|
-
return this.patchControl[doc.value];
|
|
81
|
-
}
|
|
82
|
-
}, {
|
|
83
|
-
key: "isHttpUri",
|
|
84
|
-
value: function isHttpUri(uri) {
|
|
85
|
-
return uri.slice(0, 4) === 'http';
|
|
66
|
+
patchControlFor(doc) {
|
|
67
|
+
if (!this.patchControl[doc.value]) {
|
|
68
|
+
this.patchControl[doc.value] = [];
|
|
86
69
|
}
|
|
70
|
+
return this.patchControl[doc.value];
|
|
71
|
+
}
|
|
72
|
+
isHttpUri(uri) {
|
|
73
|
+
return uri.slice(0, 4) === 'http';
|
|
74
|
+
}
|
|
87
75
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
var _iterator = _createForOfIteratorHelper(requests),
|
|
106
|
-
_step;
|
|
107
|
-
try {
|
|
108
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
109
|
-
var request = _step.value;
|
|
110
|
-
var _response = kb.any(request, this.ns.link('response'), null, meta);
|
|
111
|
-
if (_response != undefined) {
|
|
112
|
-
// ts
|
|
113
|
-
kb.add(_response, this.ns.link('outOfDate'), true, meta); // @@ Boolean is fine - fix types
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
} catch (err) {
|
|
117
|
-
_iterator.e(err);
|
|
118
|
-
} finally {
|
|
119
|
-
_iterator.f();
|
|
76
|
+
/** Remove from the store HTTP authorization metadata
|
|
77
|
+
* The editable function below relies on copies we have in the store
|
|
78
|
+
* of the results of previous HTTP transactions. However, when
|
|
79
|
+
* the user logs in, then that data misrepresents what would happen
|
|
80
|
+
* if the user tried again.
|
|
81
|
+
*/
|
|
82
|
+
flagAuthorizationMetadata(kb) {
|
|
83
|
+
if (!kb) {
|
|
84
|
+
kb = this.store;
|
|
85
|
+
}
|
|
86
|
+
const meta = kb.fetcher?.appNode;
|
|
87
|
+
const requests = kb.statementsMatching(undefined, this.ns.link('requestedURI'), undefined, meta).map(st => st.subject);
|
|
88
|
+
for (const request of requests) {
|
|
89
|
+
const response = kb.any(request, this.ns.link('response'), null, meta);
|
|
90
|
+
if (response != undefined) {
|
|
91
|
+
// ts
|
|
92
|
+
kb.add(response, this.ns.link('outOfDate'), true, meta); // @@ Boolean is fine - fix types
|
|
120
93
|
}
|
|
121
94
|
}
|
|
95
|
+
}
|
|
122
96
|
|
|
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
|
-
return
|
|
172
|
-
}
|
|
173
|
-
return checkEditable;
|
|
174
|
-
}()
|
|
175
|
-
/**
|
|
176
|
-
* Tests whether a file is editable.
|
|
177
|
-
* If the file has a specific annotation that it is machine written,
|
|
178
|
-
* for safety, it is editable (this doesn't actually check for write access)
|
|
179
|
-
* If the file has wac-allow and accept patch headers, those are respected.
|
|
180
|
-
* and local write access is determined by those headers.
|
|
181
|
-
* This synchronous version only looks at past HTTP requests, does not make new ones.
|
|
182
|
-
*
|
|
183
|
-
* @returns The method string SPARQL or DAV or
|
|
184
|
-
* LOCALFILE or false if known, undefined if not known.
|
|
185
|
-
*/
|
|
186
|
-
)
|
|
187
|
-
}, {
|
|
188
|
-
key: "editable",
|
|
189
|
-
value: function editable(uri, kb) {
|
|
190
|
-
var _kb$fetcher3;
|
|
191
|
-
if (!uri) {
|
|
192
|
-
return false; // Eg subject is bnode, no known doc to write to
|
|
193
|
-
}
|
|
194
|
-
if (!kb) {
|
|
195
|
-
kb = this.store;
|
|
196
|
-
}
|
|
197
|
-
uri = (0, _termValue.termValue)(uri);
|
|
198
|
-
if (!this.isHttpUri(uri)) {
|
|
199
|
-
if (kb.holds(kb.rdfFactory.namedNode(uri), kb.rdfFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), kb.rdfFactory.namedNode('http://www.w3.org/2007/ont/link#MachineEditableDocument'))) {
|
|
200
|
-
return 'LOCALFILE';
|
|
201
|
-
}
|
|
97
|
+
/**
|
|
98
|
+
* Tests whether a file is editable.
|
|
99
|
+
* If the file has a specific annotation that it is machine written,
|
|
100
|
+
* for safety, it is editable (this doesn't actually check for write access)
|
|
101
|
+
* If the file has wac-allow and accept patch headers, those are respected.
|
|
102
|
+
* and local write access is determined by those headers.
|
|
103
|
+
* This async version not only looks at past HTTP requests, it also makes new ones if necessary.
|
|
104
|
+
*
|
|
105
|
+
* @returns The method string N3PATCH or SPARQL or DAV or
|
|
106
|
+
* LOCALFILE or false if known, undefined if not known.
|
|
107
|
+
*/
|
|
108
|
+
async checkEditable(uri, kb) {
|
|
109
|
+
if (!uri) {
|
|
110
|
+
return false; // Eg subject is bnode, no known doc to write to
|
|
111
|
+
}
|
|
112
|
+
if (!kb) {
|
|
113
|
+
kb = this.store;
|
|
114
|
+
}
|
|
115
|
+
const initial = this.editable(uri, kb);
|
|
116
|
+
if (initial !== undefined) {
|
|
117
|
+
return initial;
|
|
118
|
+
}
|
|
119
|
+
await kb.fetcher?.load(uri);
|
|
120
|
+
const final = this.editable(uri, kb);
|
|
121
|
+
// console.log(`Loaded ${uri} just to check editable, result: ${final}.`)
|
|
122
|
+
return final;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Tests whether a file is editable.
|
|
126
|
+
* If the file has a specific annotation that it is machine written,
|
|
127
|
+
* for safety, it is editable (this doesn't actually check for write access)
|
|
128
|
+
* If the file has wac-allow and accept patch headers, those are respected.
|
|
129
|
+
* and local write access is determined by those headers.
|
|
130
|
+
* This synchronous version only looks at past HTTP requests, does not make new ones.
|
|
131
|
+
*
|
|
132
|
+
* @returns The method string SPARQL or DAV or
|
|
133
|
+
* LOCALFILE or false if known, undefined if not known.
|
|
134
|
+
*/
|
|
135
|
+
editable(uri, kb) {
|
|
136
|
+
if (!uri) {
|
|
137
|
+
return false; // Eg subject is bnode, no known doc to write to
|
|
138
|
+
}
|
|
139
|
+
if (!kb) {
|
|
140
|
+
kb = this.store;
|
|
141
|
+
}
|
|
142
|
+
uri = (0, _termValue.termValue)(uri);
|
|
143
|
+
if (!this.isHttpUri(uri)) {
|
|
144
|
+
if (kb.holds(kb.rdfFactory.namedNode(uri), kb.rdfFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), kb.rdfFactory.namedNode('http://www.w3.org/2007/ont/link#MachineEditableDocument'))) {
|
|
145
|
+
return 'LOCALFILE';
|
|
202
146
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
147
|
+
}
|
|
148
|
+
var request;
|
|
149
|
+
var definitive = false;
|
|
150
|
+
const meta = kb.fetcher?.appNode;
|
|
151
|
+
// const kb = s
|
|
207
152
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
153
|
+
// @ts-ignore passes a string to kb.each, which expects a term. Should this work?
|
|
154
|
+
var requests = kb.each(undefined, this.ns.link('requestedURI'), (0, _uri.docpart)(uri), meta);
|
|
155
|
+
var method;
|
|
156
|
+
for (var r = 0; r < requests.length; r++) {
|
|
157
|
+
request = requests[r];
|
|
158
|
+
if (request !== undefined) {
|
|
159
|
+
const response = kb.any(request, this.ns.link('response'), null, meta);
|
|
160
|
+
if (response !== undefined) {
|
|
161
|
+
// ts
|
|
217
162
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
var lr = bit.split('=');
|
|
228
|
-
if (lr[0].includes('user') && !lr[1].includes('write') && !lr[1].includes('append')) {
|
|
229
|
-
// console.log(' editable? excluded by WAC-Allow: ', wacAllow)
|
|
230
|
-
return false;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
} catch (err) {
|
|
234
|
-
_iterator2.e(err);
|
|
235
|
-
} finally {
|
|
236
|
-
_iterator2.f();
|
|
163
|
+
const outOfDate = kb.anyJS(response, this.ns.link('outOfDate'), null, meta);
|
|
164
|
+
if (outOfDate) continue;
|
|
165
|
+
var wacAllow = kb.anyValue(response, this.ns.httph('wac-allow'));
|
|
166
|
+
if (wacAllow) {
|
|
167
|
+
for (var bit of wacAllow.split(',')) {
|
|
168
|
+
var lr = bit.split('=');
|
|
169
|
+
if (lr[0].includes('user') && !lr[1].includes('write') && !lr[1].includes('append')) {
|
|
170
|
+
// console.log(' editable? excluded by WAC-Allow: ', wacAllow)
|
|
171
|
+
return false;
|
|
237
172
|
}
|
|
238
173
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
174
|
+
}
|
|
175
|
+
var acceptPatch = kb.each(response, this.ns.httph('accept-patch'));
|
|
176
|
+
if (acceptPatch.length) {
|
|
177
|
+
for (let i = 0; i < acceptPatch.length; i++) {
|
|
178
|
+
method = acceptPatch[i].value.trim();
|
|
179
|
+
// 2025/02 temporarily priorize SPARQL until N3PATCH spec revised on inserts with blankNodes
|
|
180
|
+
if (method.indexOf('application/sparql-update') >= 0) return 'SPARQL';
|
|
181
|
+
if (method.indexOf('application/sparql-update-single-match') >= 0) return 'SPARQL';
|
|
182
|
+
if (method.indexOf('text/n3') >= 0) return 'N3PATCH';
|
|
247
183
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
184
|
+
}
|
|
185
|
+
var authorVia = kb.each(response, this.ns.httph('ms-author-via'));
|
|
186
|
+
if (authorVia.length) {
|
|
187
|
+
for (let i = 0; i < authorVia.length; i++) {
|
|
188
|
+
method = authorVia[i].value.trim();
|
|
189
|
+
if (method.indexOf('SPARQL') >= 0) {
|
|
190
|
+
return 'SPARQL';
|
|
191
|
+
}
|
|
192
|
+
if (method.indexOf('DAV') >= 0) {
|
|
193
|
+
return 'DAV';
|
|
258
194
|
}
|
|
259
195
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
196
|
+
}
|
|
197
|
+
if (!this.isHttpUri(uri)) {
|
|
198
|
+
if (!wacAllow) return false;else return 'LOCALFILE';
|
|
199
|
+
}
|
|
200
|
+
var status = kb.each(response, this.ns.http('status'));
|
|
201
|
+
if (status.length) {
|
|
202
|
+
for (let i = 0; i < status.length; i++) {
|
|
203
|
+
// @ts-ignore since statuses should be TFTerms, this should always be false
|
|
204
|
+
if (status[i] === 200 || status[i] === 404) {
|
|
205
|
+
definitive = true;
|
|
206
|
+
// return false // A definitive answer
|
|
271
207
|
}
|
|
272
208
|
}
|
|
273
|
-
} else {
|
|
274
|
-
// console.log('UpdateManager.editable: No response for ' + uri + '\n')
|
|
275
209
|
}
|
|
210
|
+
} else {
|
|
211
|
+
// console.log('UpdateManager.editable: No response for ' + uri + '\n')
|
|
276
212
|
}
|
|
277
213
|
}
|
|
278
|
-
if (requests.length === 0) {
|
|
279
|
-
// console.log('UpdateManager.editable: No request for ' + uri + '\n')
|
|
280
|
-
} else {
|
|
281
|
-
if (definitive) {
|
|
282
|
-
return false; // We have got a request and it did NOT say editable => not editable
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
// console.log('UpdateManager.editable: inconclusive for ' + uri + '\n')
|
|
286
|
-
return undefined; // We don't know (yet) as we haven't had a response (yet)
|
|
287
|
-
}
|
|
288
|
-
}, {
|
|
289
|
-
key: "anonymize",
|
|
290
|
-
value: function anonymize(obj) {
|
|
291
|
-
var anonymized = obj.toNT().substr(0, 2) === '_:' && this.mentioned(obj) ? '?' + obj.toNT().substr(2) : obj.toNT();
|
|
292
|
-
return anonymized;
|
|
293
|
-
}
|
|
294
|
-
}, {
|
|
295
|
-
key: "anonymizeNT",
|
|
296
|
-
value: function anonymizeNT(stmt) {
|
|
297
|
-
return this.anonymize(stmt.subject) + ' ' + this.anonymize(stmt.predicate) + ' ' + this.anonymize(stmt.object) + ' .';
|
|
298
214
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
215
|
+
if (requests.length === 0) {
|
|
216
|
+
// console.log('UpdateManager.editable: No request for ' + uri + '\n')
|
|
217
|
+
} else {
|
|
218
|
+
if (definitive) {
|
|
219
|
+
return false; // We have got a request and it did NOT say editable => not editable
|
|
220
|
+
}
|
|
303
221
|
}
|
|
222
|
+
// console.log('UpdateManager.editable: inconclusive for ' + uri + '\n')
|
|
223
|
+
return undefined; // We don't know (yet) as we haven't had a response (yet)
|
|
224
|
+
}
|
|
225
|
+
anonymize(obj) {
|
|
226
|
+
let anonymized = obj.toNT().substr(0, 2) === '_:' && this.mentioned(obj) ? '?' + obj.toNT().substr(2) : obj.toNT();
|
|
227
|
+
return anonymized;
|
|
228
|
+
}
|
|
229
|
+
anonymizeNT(stmt) {
|
|
230
|
+
return this.anonymize(stmt.subject) + ' ' + this.anonymize(stmt.predicate) + ' ' + this.anonymize(stmt.object) + ' .';
|
|
231
|
+
}
|
|
232
|
+
nTriples(stmt) {
|
|
233
|
+
return `${stmt.subject.toNT()} ${stmt.predicate.toNT()} ${stmt.object.toNT()} .`;
|
|
234
|
+
}
|
|
304
235
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
});
|
|
315
|
-
}
|
|
236
|
+
/**
|
|
237
|
+
* Returns a list of all bnodes occurring in a statement
|
|
238
|
+
* @private
|
|
239
|
+
*/
|
|
240
|
+
statementBnodes(st) {
|
|
241
|
+
return [st.subject, st.predicate, st.object].filter(function (x) {
|
|
242
|
+
return (0, _terms.isBlankNode)(x);
|
|
243
|
+
});
|
|
244
|
+
}
|
|
316
245
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
if (j === 0 || !bnodes[j].equals(bnodes[j - 1])) {
|
|
332
|
-
bnodes2.push(bnodes[j]);
|
|
333
|
-
}
|
|
246
|
+
/**
|
|
247
|
+
* Returns a list of all bnodes occurring in a list of statements
|
|
248
|
+
* @private
|
|
249
|
+
*/
|
|
250
|
+
statementArrayBnodes(sts) {
|
|
251
|
+
var bnodes = [];
|
|
252
|
+
for (let i = 0; i < sts.length; i++) {
|
|
253
|
+
bnodes = bnodes.concat(this.statementBnodes(sts[i]));
|
|
254
|
+
}
|
|
255
|
+
bnodes.sort(); // in place sort - result may have duplicates
|
|
256
|
+
var bnodes2 = [];
|
|
257
|
+
for (let j = 0; j < bnodes.length; j++) {
|
|
258
|
+
if (j === 0 || !bnodes[j].equals(bnodes[j - 1])) {
|
|
259
|
+
bnodes2.push(bnodes[j]);
|
|
334
260
|
}
|
|
335
|
-
return bnodes2;
|
|
336
261
|
}
|
|
262
|
+
return bnodes2;
|
|
263
|
+
}
|
|
337
264
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
for (var i = 0; i < a.length; i++) {
|
|
348
|
-
this.ifps[a[i].value] = true;
|
|
349
|
-
}
|
|
350
|
-
this.fps = {};
|
|
351
|
-
a = this.store.each(undefined, this.ns.rdf('type'), this.ns.owl('FunctionalProperty'));
|
|
352
|
-
for (var _i3 = 0; _i3 < a.length; _i3++) {
|
|
353
|
-
this.fps[a[_i3].value] = true;
|
|
354
|
-
}
|
|
265
|
+
/**
|
|
266
|
+
* Makes a cached list of [Inverse-]Functional properties
|
|
267
|
+
* @private
|
|
268
|
+
*/
|
|
269
|
+
cacheIfps() {
|
|
270
|
+
this.ifps = {};
|
|
271
|
+
var a = this.store.each(undefined, this.ns.rdf('type'), this.ns.owl('InverseFunctionalProperty'));
|
|
272
|
+
for (let i = 0; i < a.length; i++) {
|
|
273
|
+
this.ifps[a[i].value] = true;
|
|
355
274
|
}
|
|
275
|
+
this.fps = {};
|
|
276
|
+
a = this.store.each(undefined, this.ns.rdf('type'), this.ns.owl('FunctionalProperty'));
|
|
277
|
+
for (let i = 0; i < a.length; i++) {
|
|
278
|
+
this.fps[a[i].value] = true;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
356
281
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
if (
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
if (
|
|
377
|
-
res
|
|
378
|
-
if (res) {
|
|
379
|
-
return res.concat([sts[i]]);
|
|
380
|
-
}
|
|
282
|
+
/**
|
|
283
|
+
* Returns a context to bind a given node, up to a given depth
|
|
284
|
+
* @private
|
|
285
|
+
*/
|
|
286
|
+
bnodeContext2(x, source, depth) {
|
|
287
|
+
// Return a list of statements which indirectly identify a node
|
|
288
|
+
// Depth > 1 if try further indirection.
|
|
289
|
+
// Return array of statements (possibly empty), or null if failure
|
|
290
|
+
var sts = this.store.statementsMatching(undefined, undefined, x, source); // incoming links
|
|
291
|
+
var y;
|
|
292
|
+
var res;
|
|
293
|
+
for (let i = 0; i < sts.length; i++) {
|
|
294
|
+
if (this.fps[sts[i].predicate.value]) {
|
|
295
|
+
y = sts[i].subject;
|
|
296
|
+
if (!y.isBlank) {
|
|
297
|
+
return [sts[i]];
|
|
298
|
+
}
|
|
299
|
+
if (depth) {
|
|
300
|
+
res = this.bnodeContext2(y, source, depth - 1);
|
|
301
|
+
if (res) {
|
|
302
|
+
return res.concat([sts[i]]);
|
|
381
303
|
}
|
|
382
304
|
}
|
|
383
305
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
306
|
+
}
|
|
307
|
+
// outgoing links
|
|
308
|
+
sts = this.store.statementsMatching(x, undefined, undefined, source);
|
|
309
|
+
for (let i = 0; i < sts.length; i++) {
|
|
310
|
+
if (this.ifps[sts[i].predicate.value]) {
|
|
311
|
+
y = sts[i].object;
|
|
312
|
+
if (!y.isBlank) {
|
|
313
|
+
return [sts[i]];
|
|
314
|
+
}
|
|
315
|
+
if (depth) {
|
|
316
|
+
res = this.bnodeContext2(y, source, depth - 1);
|
|
317
|
+
if (res) {
|
|
318
|
+
return res.concat([sts[i]]);
|
|
397
319
|
}
|
|
398
320
|
}
|
|
399
321
|
}
|
|
400
|
-
return null; // Failure
|
|
401
322
|
}
|
|
323
|
+
return null; // Failure
|
|
324
|
+
}
|
|
402
325
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
//
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
var con = this.bnodeContext2(x, source, depth);
|
|
415
|
-
if (con !== null) return con;
|
|
416
|
-
}
|
|
417
|
-
// If we can't guarantee unique with logic just send all info about node
|
|
418
|
-
return this.store.connectedStatements(x, source); // was:
|
|
419
|
-
// throw new Error('Unable to uniquely identify bnode: ' + x.toNT())
|
|
326
|
+
/**
|
|
327
|
+
* Returns the smallest context to bind a given single bnode
|
|
328
|
+
* @private
|
|
329
|
+
*/
|
|
330
|
+
bnodeContext1(x, source) {
|
|
331
|
+
// Return a list of statements which indirectly identify a node
|
|
332
|
+
// Breadth-first
|
|
333
|
+
for (var depth = 0; depth < 3; depth++) {
|
|
334
|
+
// Try simple first
|
|
335
|
+
var con = this.bnodeContext2(x, source, depth);
|
|
336
|
+
if (con !== null) return con;
|
|
420
337
|
}
|
|
338
|
+
// If we can't guarantee unique with logic just send all info about node
|
|
339
|
+
return this.store.connectedStatements(x, source); // was:
|
|
340
|
+
// throw new Error('Unable to uniquely identify bnode: ' + x.toNT())
|
|
341
|
+
}
|
|
421
342
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
this.store.statementsMatching(null, x).length !== 0 || this.store.statementsMatching(null, null, x).length !== 0;
|
|
431
|
-
}
|
|
343
|
+
/**
|
|
344
|
+
* @private
|
|
345
|
+
*/
|
|
346
|
+
mentioned(x) {
|
|
347
|
+
return this.store.statementsMatching(x, null, null, null).length !== 0 ||
|
|
348
|
+
// Don't pin fresh bnodes
|
|
349
|
+
this.store.statementsMatching(null, x).length !== 0 || this.store.statementsMatching(null, null, x).length !== 0;
|
|
350
|
+
}
|
|
432
351
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
this
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
if (!this.mentioned(bnode)) continue;
|
|
446
|
-
context = context.concat(this.bnodeContext1(bnode, doc));
|
|
447
|
-
}
|
|
352
|
+
/**
|
|
353
|
+
* @private
|
|
354
|
+
*/
|
|
355
|
+
bnodeContext(bnodes, doc) {
|
|
356
|
+
var context = [];
|
|
357
|
+
if (bnodes.length) {
|
|
358
|
+
this.cacheIfps();
|
|
359
|
+
for (let i = 0; i < bnodes.length; i++) {
|
|
360
|
+
// Does this occur in old graph?
|
|
361
|
+
var bnode = bnodes[i];
|
|
362
|
+
if (!this.mentioned(bnode)) continue;
|
|
363
|
+
context = context.concat(this.bnodeContext1(bnode, doc));
|
|
448
364
|
}
|
|
449
|
-
return context;
|
|
450
365
|
}
|
|
366
|
+
return context;
|
|
367
|
+
}
|
|
451
368
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
return this.bnodeContext(bnodes, st.graph);
|
|
461
|
-
}
|
|
369
|
+
/**
|
|
370
|
+
* Returns the best context for a single statement
|
|
371
|
+
* @private
|
|
372
|
+
*/
|
|
373
|
+
statementContext(st) {
|
|
374
|
+
var bnodes = this.statementBnodes(st);
|
|
375
|
+
return this.bnodeContext(bnodes, st.graph);
|
|
376
|
+
}
|
|
462
377
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
}).join('\n') + ' }\n';
|
|
473
|
-
}
|
|
378
|
+
/**
|
|
379
|
+
* @private
|
|
380
|
+
*/
|
|
381
|
+
contextWhere(context) {
|
|
382
|
+
var updater = this;
|
|
383
|
+
return !context || context.length === 0 ? '' : 'WHERE { ' + context.map(function (x) {
|
|
384
|
+
return updater.anonymizeNT(x);
|
|
385
|
+
}).join('\n') + ' }\n';
|
|
386
|
+
}
|
|
474
387
|
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
throw new Error('No URI given for remote editing operation: ' + query);
|
|
486
|
-
}
|
|
487
|
-
// console.log('UpdateManager: sending update to <' + uri + '>')
|
|
388
|
+
/**
|
|
389
|
+
* @private
|
|
390
|
+
*/
|
|
391
|
+
fire(uri, query, callbackFunction) {
|
|
392
|
+
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
393
|
+
return Promise.resolve().then(() => {
|
|
394
|
+
if (!uri) {
|
|
395
|
+
throw new Error('No URI given for remote editing operation: ' + query);
|
|
396
|
+
}
|
|
397
|
+
// console.log('UpdateManager: sending update to <' + uri + '>')
|
|
488
398
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
399
|
+
options.noMeta = true;
|
|
400
|
+
options.contentType = options.contentType || 'application/sparql-update';
|
|
401
|
+
options.body = query;
|
|
402
|
+
return this.store.fetcher.webOperation('PATCH', uri, options);
|
|
403
|
+
}).then(response => {
|
|
404
|
+
if (!response.ok) {
|
|
405
|
+
let message = 'UpdateManager: update failed for <' + uri + '> status=' + response.status + ', ' + response.statusText + '\n for query: ' + query;
|
|
406
|
+
// console.log(message)
|
|
407
|
+
throw new Error(message);
|
|
408
|
+
}
|
|
499
409
|
|
|
500
|
-
|
|
410
|
+
// console.log('UpdateManager: update Ok for <' + uri + '>')
|
|
501
411
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
412
|
+
callbackFunction(uri, response.ok, response.responseText, response);
|
|
413
|
+
}).catch(err => {
|
|
414
|
+
callbackFunction(uri, false, err.message, err);
|
|
415
|
+
});
|
|
416
|
+
}
|
|
507
417
|
|
|
508
|
-
|
|
418
|
+
// ARE THESE THREE FUNCTIONS USED? DEPRECATE?
|
|
509
419
|
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
if (statement && !statement.graph) {
|
|
520
|
-
return;
|
|
521
|
-
}
|
|
522
|
-
var updater = this;
|
|
523
|
-
var context = this.statementContext(statement);
|
|
524
|
-
return {
|
|
525
|
-
statement: statement ? [statement.subject, statement.predicate, statement.object, statement.graph] : undefined,
|
|
526
|
-
statementNT: statement ? this.anonymizeNT(statement) : undefined,
|
|
527
|
-
where: updater.contextWhere(context),
|
|
528
|
-
set_object: function set_object(obj, callbackFunction) {
|
|
529
|
-
var query = this.where;
|
|
530
|
-
query += 'DELETE DATA { ' + this.statementNT + ' } ;\n';
|
|
531
|
-
query += 'INSERT DATA { ' +
|
|
532
|
-
// @ts-ignore `this` might refer to the wrong scope. Does this work?
|
|
533
|
-
this.anonymize(this.statement[0]) + ' ' +
|
|
534
|
-
// @ts-ignore
|
|
535
|
-
this.anonymize(this.statement[1]) + ' ' +
|
|
536
|
-
// @ts-ignore
|
|
537
|
-
this.anonymize(obj) + ' ' + ' . }\n';
|
|
538
|
-
updater.fire(this.statement[3].value, query, callbackFunction);
|
|
539
|
-
}
|
|
540
|
-
};
|
|
420
|
+
/** return a statemnet updating function
|
|
421
|
+
*
|
|
422
|
+
* This does NOT update the statement.
|
|
423
|
+
* It returns an object which includes
|
|
424
|
+
* function which can be used to change the object of the statement.
|
|
425
|
+
*/
|
|
426
|
+
update_statement(statement) {
|
|
427
|
+
if (statement && !statement.graph) {
|
|
428
|
+
return;
|
|
541
429
|
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
query += '
|
|
551
|
-
|
|
552
|
-
|
|
430
|
+
var updater = this;
|
|
431
|
+
var context = this.statementContext(statement);
|
|
432
|
+
return {
|
|
433
|
+
statement: statement ? [statement.subject, statement.predicate, statement.object, statement.graph] : undefined,
|
|
434
|
+
statementNT: statement ? this.anonymizeNT(statement) : undefined,
|
|
435
|
+
where: updater.contextWhere(context),
|
|
436
|
+
set_object: function (obj, callbackFunction) {
|
|
437
|
+
var query = this.where;
|
|
438
|
+
query += 'DELETE DATA { ' + this.statementNT + ' } ;\n';
|
|
439
|
+
query += 'INSERT DATA { ' +
|
|
440
|
+
// @ts-ignore `this` might refer to the wrong scope. Does this work?
|
|
441
|
+
this.anonymize(this.statement[0]) + ' ' +
|
|
442
|
+
// @ts-ignore
|
|
443
|
+
this.anonymize(this.statement[1]) + ' ' +
|
|
444
|
+
// @ts-ignore
|
|
445
|
+
this.anonymize(obj) + ' ' + ' . }\n';
|
|
446
|
+
updater.fire(this.statement[3].value, query, callbackFunction);
|
|
553
447
|
}
|
|
554
|
-
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
insert_statement(st, callbackFunction) {
|
|
451
|
+
var st0 = st instanceof Array ? st[0] : st;
|
|
452
|
+
var query = this.contextWhere(this.statementContext(st0));
|
|
453
|
+
if (st instanceof Array) {
|
|
454
|
+
var stText = '';
|
|
455
|
+
for (let i = 0; i < st.length; i++) stText += st[i] + '\n';
|
|
456
|
+
query += 'INSERT DATA { ' + stText + ' }\n';
|
|
457
|
+
} else {
|
|
458
|
+
query += 'INSERT DATA { ' + this.anonymize(st.subject) + ' ' + this.anonymize(st.predicate) + ' ' + this.anonymize(st.object) + ' ' + ' . }\n';
|
|
555
459
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
}
|
|
568
|
-
this.fire(st0.graph.value, query, callbackFunction);
|
|
460
|
+
this.fire(st0.graph.value, query, callbackFunction);
|
|
461
|
+
}
|
|
462
|
+
delete_statement(st, callbackFunction) {
|
|
463
|
+
var st0 = st instanceof Array ? st[0] : st;
|
|
464
|
+
var query = this.contextWhere(this.statementContext(st0));
|
|
465
|
+
if (st instanceof Array) {
|
|
466
|
+
var stText = '';
|
|
467
|
+
for (let i = 0; i < st.length; i++) stText += st[i] + '\n';
|
|
468
|
+
query += 'DELETE DATA { ' + stText + ' }\n';
|
|
469
|
+
} else {
|
|
470
|
+
query += 'DELETE DATA { ' + this.anonymize(st.subject) + ' ' + this.anonymize(st.predicate) + ' ' + this.anonymize(st.object) + ' ' + ' . }\n';
|
|
569
471
|
}
|
|
472
|
+
this.fire(st0.graph.value, query, callbackFunction);
|
|
473
|
+
}
|
|
570
474
|
|
|
571
|
-
|
|
475
|
+
/// //////////////////////
|
|
572
476
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
// Kludge compare
|
|
592
|
-
throw new Error("Can't wait for > 1 different downstream actions");
|
|
593
|
-
}
|
|
594
|
-
} else {
|
|
595
|
-
control.downstreamAction = action;
|
|
477
|
+
/**
|
|
478
|
+
* Requests a now or future action to refresh changes coming downstream
|
|
479
|
+
* This is designed to allow the system to re-request the server version,
|
|
480
|
+
* when a websocket has pinged to say there are changes.
|
|
481
|
+
* If the websocket, by contrast, has sent a patch, then this may not be necessary.
|
|
482
|
+
*
|
|
483
|
+
* @param doc
|
|
484
|
+
* @param action
|
|
485
|
+
*/
|
|
486
|
+
requestDownstreamAction(doc, action) {
|
|
487
|
+
var control = this.patchControlFor(doc);
|
|
488
|
+
if (!control.pendingUpstream) {
|
|
489
|
+
action(doc);
|
|
490
|
+
} else {
|
|
491
|
+
if (control.downstreamAction) {
|
|
492
|
+
if ('' + control.downstreamAction !== '' + action) {
|
|
493
|
+
// Kludge compare
|
|
494
|
+
throw new Error("Can't wait for > 1 different downstream actions");
|
|
596
495
|
}
|
|
496
|
+
} else {
|
|
497
|
+
control.downstreamAction = action;
|
|
597
498
|
}
|
|
598
499
|
}
|
|
500
|
+
}
|
|
599
501
|
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
502
|
+
/**
|
|
503
|
+
* We want to start counting websocket notifications
|
|
504
|
+
* to distinguish the ones from others from our own.
|
|
505
|
+
*/
|
|
506
|
+
clearUpstreamCount(doc) {
|
|
507
|
+
var control = this.patchControlFor(doc);
|
|
508
|
+
control.upstreamCount = 0;
|
|
509
|
+
}
|
|
510
|
+
getUpdatesVia(doc) {
|
|
511
|
+
var linkHeaders = this.store.fetcher.getHeader(doc, 'updates-via');
|
|
512
|
+
if (!linkHeaders || !linkHeaders.length) return null;
|
|
513
|
+
return linkHeaders[0].trim();
|
|
514
|
+
}
|
|
515
|
+
addDownstreamChangeListener(doc, listener) {
|
|
516
|
+
var control = this.patchControlFor(doc);
|
|
517
|
+
if (!control.downstreamChangeListeners) {
|
|
518
|
+
control.downstreamChangeListeners = [];
|
|
616
519
|
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
520
|
+
control.downstreamChangeListeners.push(listener);
|
|
521
|
+
this.setRefreshHandler(doc, doc => {
|
|
522
|
+
this.reloadAndSync(doc);
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
reloadAndSync(doc) {
|
|
526
|
+
var control = this.patchControlFor(doc);
|
|
527
|
+
var updater = this;
|
|
528
|
+
if (control.reloading) {
|
|
529
|
+
// console.log(' Already reloading - note this load may be out of date')
|
|
530
|
+
control.outOfDate = true;
|
|
531
|
+
return; // once only needed @@ Not true, has changed again
|
|
629
532
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
control.reloading = true;
|
|
641
|
-
var retryTimeout = 1000; // ms
|
|
642
|
-
var tryReload = function tryReload() {
|
|
643
|
-
// console.log('try reload - timeout = ' + retryTimeout)
|
|
644
|
-
updater.reload(updater.store, doc, function (ok, message, response) {
|
|
645
|
-
if (ok) {
|
|
646
|
-
if (control.downstreamChangeListeners) {
|
|
647
|
-
for (var i = 0; i < control.downstreamChangeListeners.length; i++) {
|
|
648
|
-
// console.log(' Calling downstream listener ' + i)
|
|
649
|
-
control.downstreamChangeListeners[i]();
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
control.reloading = false;
|
|
653
|
-
if (control.outOfDate) {
|
|
654
|
-
// console.log(' Extra reload because of extra update.')
|
|
655
|
-
control.outOfDate = false;
|
|
656
|
-
tryReload();
|
|
533
|
+
control.reloading = true;
|
|
534
|
+
var retryTimeout = 1000; // ms
|
|
535
|
+
var tryReload = function () {
|
|
536
|
+
// console.log('try reload - timeout = ' + retryTimeout)
|
|
537
|
+
updater.reload(updater.store, doc, function (ok, message, response) {
|
|
538
|
+
if (ok) {
|
|
539
|
+
if (control.downstreamChangeListeners) {
|
|
540
|
+
for (let i = 0; i < control.downstreamChangeListeners.length; i++) {
|
|
541
|
+
// console.log(' Calling downstream listener ' + i)
|
|
542
|
+
control.downstreamChangeListeners[i]();
|
|
657
543
|
}
|
|
544
|
+
}
|
|
545
|
+
control.reloading = false;
|
|
546
|
+
if (control.outOfDate) {
|
|
547
|
+
// console.log(' Extra reload because of extra update.')
|
|
548
|
+
control.outOfDate = false;
|
|
549
|
+
tryReload();
|
|
550
|
+
}
|
|
551
|
+
} else {
|
|
552
|
+
control.reloading = false;
|
|
553
|
+
if (response && response.status === 0) {
|
|
554
|
+
// console.log('Network error refreshing the data. Retrying in ' +
|
|
555
|
+
// retryTimeout / 1000)
|
|
556
|
+
control.reloading = true;
|
|
557
|
+
retryTimeout = retryTimeout * 2;
|
|
558
|
+
setTimeout(tryReload, retryTimeout);
|
|
658
559
|
} else {
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
// console.log('Network error refreshing the data. Retrying in ' +
|
|
662
|
-
// retryTimeout / 1000)
|
|
663
|
-
control.reloading = true;
|
|
664
|
-
retryTimeout = retryTimeout * 2;
|
|
665
|
-
setTimeout(tryReload, retryTimeout);
|
|
666
|
-
} else {
|
|
667
|
-
// console.log('Error ' + (response as Response).status + 'refreshing the data:' +
|
|
668
|
-
// message + '. Stopped' + doc)
|
|
669
|
-
}
|
|
560
|
+
// console.log('Error ' + (response as Response).status + 'refreshing the data:' +
|
|
561
|
+
// message + '. Stopped' + doc)
|
|
670
562
|
}
|
|
671
|
-
}
|
|
672
|
-
};
|
|
673
|
-
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
};
|
|
566
|
+
tryReload();
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Sets up websocket to listen on
|
|
571
|
+
*
|
|
572
|
+
* There is coordination between upstream changes and downstream ones
|
|
573
|
+
* so that a reload is not done in the middle of an upstream patch.
|
|
574
|
+
* If you use this API then you get called when a change happens, and you
|
|
575
|
+
* have to reload the file yourself, and then refresh the UI.
|
|
576
|
+
* Alternative is addDownstreamChangeListener(), where you do not
|
|
577
|
+
* have to do the reload yourself. Do mot mix them.
|
|
578
|
+
*
|
|
579
|
+
* kb contains the HTTP metadata from previous operations
|
|
580
|
+
*
|
|
581
|
+
* @param doc
|
|
582
|
+
* @param handler
|
|
583
|
+
*
|
|
584
|
+
* @returns {boolean}
|
|
585
|
+
*/
|
|
586
|
+
setRefreshHandler(doc, handler) {
|
|
587
|
+
let wssURI = this.getUpdatesVia(doc); // relative
|
|
588
|
+
// var kb = this.store
|
|
589
|
+
var theHandler = handler;
|
|
590
|
+
var self = this;
|
|
591
|
+
var updater = this;
|
|
592
|
+
var retryTimeout = 1500; // *2 will be 3 Seconds, 6, 12, etc
|
|
593
|
+
var retries = 0;
|
|
594
|
+
if (!wssURI) {
|
|
595
|
+
// console.log('Server does not support live updates through Updates-Via :-(')
|
|
596
|
+
return false;
|
|
674
597
|
}
|
|
598
|
+
wssURI = (0, _uri.join)(wssURI, doc.value);
|
|
599
|
+
const validWssURI = wssURI.replace(/^http:/, 'ws:').replace(/^https:/, 'wss:');
|
|
600
|
+
// console.log('Web socket URI ' + wssURI)
|
|
675
601
|
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
* kb contains the HTTP metadata from previous operations
|
|
687
|
-
*
|
|
688
|
-
* @param doc
|
|
689
|
-
* @param handler
|
|
690
|
-
*
|
|
691
|
-
* @returns {boolean}
|
|
692
|
-
*/
|
|
693
|
-
}, {
|
|
694
|
-
key: "setRefreshHandler",
|
|
695
|
-
value: function setRefreshHandler(doc, handler) {
|
|
696
|
-
var wssURI = this.getUpdatesVia(doc); // relative
|
|
697
|
-
// var kb = this.store
|
|
698
|
-
var theHandler = handler;
|
|
699
|
-
var self = this;
|
|
700
|
-
var updater = this;
|
|
701
|
-
var retryTimeout = 1500; // *2 will be 3 Seconds, 6, 12, etc
|
|
702
|
-
var retries = 0;
|
|
703
|
-
if (!wssURI) {
|
|
704
|
-
// console.log('Server does not support live updates through Updates-Via :-(')
|
|
705
|
-
return false;
|
|
602
|
+
var openWebsocket = function () {
|
|
603
|
+
// From https://github.com/solid/solid-spec#live-updates
|
|
604
|
+
var socket;
|
|
605
|
+
if (typeof WebSocket !== 'undefined') {
|
|
606
|
+
socket = new WebSocket(validWssURI);
|
|
607
|
+
} else if (typeof window !== 'undefined' && window.WebSocket) {
|
|
608
|
+
socket = window.WebSocket(validWssURI);
|
|
609
|
+
} else {
|
|
610
|
+
// console.log('Live update disabled, as WebSocket not supported by platform :-(')
|
|
611
|
+
return;
|
|
706
612
|
}
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
if (typeof WebSocket !== 'undefined') {
|
|
715
|
-
socket = new WebSocket(validWssURI);
|
|
716
|
-
} else if (typeof window !== 'undefined' && window.WebSocket) {
|
|
717
|
-
socket = window.WebSocket(validWssURI);
|
|
718
|
-
} else {
|
|
719
|
-
// console.log('Live update disabled, as WebSocket not supported by platform :-(')
|
|
720
|
-
return;
|
|
613
|
+
socket.onopen = function () {
|
|
614
|
+
// console.log(' websocket open')
|
|
615
|
+
retryTimeout = 1500; // reset timeout to fast on success
|
|
616
|
+
this.send('sub ' + doc.value);
|
|
617
|
+
if (retries) {
|
|
618
|
+
// console.log('Web socket has been down, better check for any news.')
|
|
619
|
+
updater.requestDownstreamAction(doc, theHandler);
|
|
721
620
|
}
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
updater.requestDownstreamAction(doc, theHandler);
|
|
729
|
-
}
|
|
730
|
-
};
|
|
731
|
-
var control = self.patchControlFor(doc);
|
|
732
|
-
control.upstreamCount = 0;
|
|
733
|
-
socket.onerror = function onerror(err) {
|
|
734
|
-
// console.log('Error on Websocket:', err)
|
|
735
|
-
};
|
|
621
|
+
};
|
|
622
|
+
var control = self.patchControlFor(doc);
|
|
623
|
+
control.upstreamCount = 0;
|
|
624
|
+
socket.onerror = function onerror(err) {
|
|
625
|
+
// console.log('Error on Websocket:', err)
|
|
626
|
+
};
|
|
736
627
|
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
}
|
|
628
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
|
|
629
|
+
//
|
|
630
|
+
// 1000 CLOSE_NORMAL Normal closure; the connection successfully completed whatever purpose for which it was created.
|
|
631
|
+
// 1001 CLOSE_GOING_AWAY The endpoint is going away, either
|
|
632
|
+
// because of a server failure or because the browser is navigating away from the page that opened the connection.
|
|
633
|
+
// 1002 CLOSE_PROTOCOL_ERROR The endpoint is terminating the connection due to a protocol error.
|
|
634
|
+
// 1003 CLOSE_UNSUPPORTED The connection is being terminated because the endpoint
|
|
635
|
+
// received data of a type it cannot accept (for example, a text-only endpoint received binary data).
|
|
636
|
+
// 1004 Reserved. A meaning might be defined in the future.
|
|
637
|
+
// 1005 CLOSE_NO_STATUS Reserved. Indicates that no status code was provided even though one was expected.
|
|
638
|
+
// 1006 CLOSE_ABNORMAL Reserved. Used to indicate that a connection was closed abnormally (
|
|
639
|
+
//
|
|
640
|
+
//
|
|
641
|
+
socket.onclose = function (event) {
|
|
642
|
+
// console.log('*** Websocket closed with code ' + event.code +
|
|
643
|
+
// ", reason '" + event.reason + "' clean = " + event.wasClean)
|
|
644
|
+
retryTimeout *= 2;
|
|
645
|
+
retries += 1;
|
|
646
|
+
// console.log('Retrying in ' + retryTimeout + 'ms') // (ask user?)
|
|
647
|
+
setTimeout(function () {
|
|
648
|
+
// console.log('Trying websocket again')
|
|
649
|
+
openWebsocket();
|
|
650
|
+
}, retryTimeout);
|
|
651
|
+
};
|
|
652
|
+
socket.onmessage = function (msg) {
|
|
653
|
+
if (msg.data && msg.data.slice(0, 3) === 'pub') {
|
|
654
|
+
if ('upstreamCount' in control) {
|
|
655
|
+
control.upstreamCount -= 1;
|
|
656
|
+
if (control.upstreamCount >= 0) {
|
|
657
|
+
// console.log('just an echo: ' + control.upstreamCount)
|
|
658
|
+
return; // Just an echo
|
|
769
659
|
}
|
|
770
|
-
// console.log('Assume a real downstream change: ' + control.upstreamCount + ' -> 0')
|
|
771
|
-
control.upstreamCount = 0;
|
|
772
|
-
self.requestDownstreamAction(doc, theHandler);
|
|
773
660
|
}
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
661
|
+
// console.log('Assume a real downstream change: ' + control.upstreamCount + ' -> 0')
|
|
662
|
+
control.upstreamCount = 0;
|
|
663
|
+
self.requestDownstreamAction(doc, theHandler);
|
|
664
|
+
}
|
|
665
|
+
};
|
|
666
|
+
}; // openWebsocket
|
|
667
|
+
openWebsocket();
|
|
668
|
+
return true;
|
|
669
|
+
}
|
|
779
670
|
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
return uniqueDoc.equals(doc);
|
|
801
|
-
})) uniqueDocs.push(doc);
|
|
802
|
-
});
|
|
803
|
-
var updates = uniqueDocs.map(function (doc) {
|
|
804
|
-
return thisUpdater.update(deletions.filter(function (st) {
|
|
805
|
-
return st.why.equals(doc);
|
|
806
|
-
}), insertions.filter(function (st) {
|
|
807
|
-
return st.why.equals(doc);
|
|
808
|
-
}));
|
|
809
|
-
});
|
|
810
|
-
if (updates.length > 1) {
|
|
811
|
-
// console.log(`@@ updateMany to ${updates.length}: ${uniqueDocs}`)
|
|
812
|
-
}
|
|
813
|
-
return Promise.all(updates);
|
|
671
|
+
/**
|
|
672
|
+
* This high-level function updates the local store iff the web is changed successfully.
|
|
673
|
+
* Deletions, insertions may be undefined or single statements or lists or formulae (may contain bnodes which can be indirectly identified by a where clause).
|
|
674
|
+
* The `why` property of each statement must be the give the web document to be updated.
|
|
675
|
+
* The statements to be deleted and inserted may span more than one web document.
|
|
676
|
+
* @param deletions - Statement or statements to be deleted.
|
|
677
|
+
* @param insertions - Statement or statements to be inserted.
|
|
678
|
+
* @returns a promise
|
|
679
|
+
*/
|
|
680
|
+
updateMany(deletions) {
|
|
681
|
+
let insertions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
682
|
+
const docs = deletions.concat(insertions).map(st => st.why);
|
|
683
|
+
const thisUpdater = this;
|
|
684
|
+
const uniqueDocs = [];
|
|
685
|
+
docs.forEach(doc => {
|
|
686
|
+
if (!uniqueDocs.find(uniqueDoc => uniqueDoc.equals(doc))) uniqueDocs.push(doc);
|
|
687
|
+
});
|
|
688
|
+
const updates = uniqueDocs.map(doc => thisUpdater.update(deletions.filter(st => st.why.equals(doc)), insertions.filter(st => st.why.equals(doc))));
|
|
689
|
+
if (updates.length > 1) {
|
|
690
|
+
// console.log(`@@ updateMany to ${updates.length}: ${uniqueDocs}`)
|
|
814
691
|
}
|
|
692
|
+
return Promise.all(updates);
|
|
693
|
+
}
|
|
815
694
|
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
if (
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
query +=
|
|
834
|
-
for (var i = 0; i < ds.length; i++) {
|
|
835
|
-
query += this.anonymizeNT(ds[i]) + '\n';
|
|
836
|
-
}
|
|
837
|
-
query += ' }\n';
|
|
695
|
+
/**
|
|
696
|
+
* @private
|
|
697
|
+
*
|
|
698
|
+
* This helper function constructs SPARQL Update query from resolved arguments.
|
|
699
|
+
*
|
|
700
|
+
* @param ds: deletions array.
|
|
701
|
+
* @param is: insertions array.
|
|
702
|
+
* @param bnodes_context: Additional context to uniquely identify any blank nodes.
|
|
703
|
+
*/
|
|
704
|
+
constructSparqlUpdateQuery(ds, is, bnodes_context) {
|
|
705
|
+
var whereClause = this.contextWhere(bnodes_context);
|
|
706
|
+
var query = '';
|
|
707
|
+
if (whereClause.length) {
|
|
708
|
+
// Is there a WHERE clause?
|
|
709
|
+
if (ds.length) {
|
|
710
|
+
query += 'DELETE { ';
|
|
711
|
+
for (let i = 0; i < ds.length; i++) {
|
|
712
|
+
query += this.anonymizeNT(ds[i]) + '\n';
|
|
838
713
|
}
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
query += '
|
|
714
|
+
query += ' }\n';
|
|
715
|
+
}
|
|
716
|
+
if (is.length) {
|
|
717
|
+
query += 'INSERT { ';
|
|
718
|
+
for (let i = 0; i < is.length; i++) {
|
|
719
|
+
query += this.anonymizeNT(is[i]) + '\n';
|
|
845
720
|
}
|
|
846
|
-
query +=
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
query +=
|
|
721
|
+
query += ' }\n';
|
|
722
|
+
}
|
|
723
|
+
query += whereClause;
|
|
724
|
+
} else {
|
|
725
|
+
// no where clause
|
|
726
|
+
if (ds.length) {
|
|
727
|
+
query += 'DELETE DATA { ';
|
|
728
|
+
for (let i = 0; i < ds.length; i++) {
|
|
729
|
+
query += this.anonymizeNT(ds[i]) + '\n';
|
|
855
730
|
}
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
query += '
|
|
731
|
+
query += ' } \n';
|
|
732
|
+
}
|
|
733
|
+
if (is.length) {
|
|
734
|
+
if (ds.length) query += ' ; ';
|
|
735
|
+
query += 'INSERT DATA { ';
|
|
736
|
+
for (let i = 0; i < is.length; i++) {
|
|
737
|
+
query += this.nTriples(is[i]) + '\n';
|
|
863
738
|
}
|
|
739
|
+
query += ' }\n';
|
|
864
740
|
}
|
|
865
|
-
return query;
|
|
866
741
|
}
|
|
742
|
+
return query;
|
|
743
|
+
}
|
|
867
744
|
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
}).join('\n '), "\n };");
|
|
897
|
-
}
|
|
898
|
-
query += " a solid:InsertDeletePatch .\n";
|
|
899
|
-
return query;
|
|
745
|
+
/**
|
|
746
|
+
* @private
|
|
747
|
+
*
|
|
748
|
+
* This helper function constructs n3-patch query from resolved arguments.
|
|
749
|
+
*
|
|
750
|
+
* @param ds: deletions array.
|
|
751
|
+
* @param is: insertions array.
|
|
752
|
+
* @param bnodes_context: Additional context to uniquely identify any blanknodes.
|
|
753
|
+
*/
|
|
754
|
+
constructN3PatchQuery(ds, is, bnodes_context) {
|
|
755
|
+
var query = `
|
|
756
|
+
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
|
|
757
|
+
@prefix ex: <http://www.example.org/terms#>.
|
|
758
|
+
|
|
759
|
+
_:patch
|
|
760
|
+
`;
|
|
761
|
+
// If bnode context is non trivial, express it as ?conditions formula.
|
|
762
|
+
if (bnodes_context && bnodes_context.length > 0) {
|
|
763
|
+
query += `
|
|
764
|
+
solid:where {
|
|
765
|
+
${bnodes_context.map(x => this.anonymizeNT(x)).join('\n ')}
|
|
766
|
+
};`;
|
|
767
|
+
}
|
|
768
|
+
if (ds.length > 0) {
|
|
769
|
+
query += `
|
|
770
|
+
solid:deletes {
|
|
771
|
+
${ds.map(x => this.anonymizeNT(x)).join('\n ')}
|
|
772
|
+
};`;
|
|
900
773
|
}
|
|
774
|
+
if (is.length > 0) {
|
|
775
|
+
query += `
|
|
776
|
+
solid:inserts {
|
|
777
|
+
${is.map(x => this.anonymizeNT(x)).join('\n ')}
|
|
778
|
+
};`;
|
|
779
|
+
}
|
|
780
|
+
query += " a solid:InsertDeletePatch .\n";
|
|
781
|
+
return query;
|
|
782
|
+
}
|
|
901
783
|
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
var
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
}, secondTry, options); // callbackFunction
|
|
928
|
-
}); // promise
|
|
929
|
-
} // if
|
|
784
|
+
/**
|
|
785
|
+
* This high-level function updates the local store if the web is changed successfully.
|
|
786
|
+
* Deletions, insertions may be undefined or single statements or lists or formulae (may contain bnodes which can be indirectly identified by a where clause).
|
|
787
|
+
* The `why` property of each statement must be the same and give the web document to be updated.
|
|
788
|
+
* @param deletions - Statement or statements to be deleted.
|
|
789
|
+
* @param insertions - Statement or statements to be inserted.
|
|
790
|
+
* @param callback - called as callbackFunction(uri, success, errorbody)
|
|
791
|
+
* OR returns a promise
|
|
792
|
+
* @param options - Options for the fetch call
|
|
793
|
+
*/
|
|
794
|
+
update(deletions, insertions, callback, secondTry) {
|
|
795
|
+
let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
796
|
+
if (!callback) {
|
|
797
|
+
var thisUpdater = this;
|
|
798
|
+
return new Promise(function (resolve, reject) {
|
|
799
|
+
// Promise version
|
|
800
|
+
thisUpdater.update(deletions, insertions, function (uri, ok, errorBody) {
|
|
801
|
+
if (!ok) {
|
|
802
|
+
reject(new Error(errorBody));
|
|
803
|
+
} else {
|
|
804
|
+
resolve();
|
|
805
|
+
}
|
|
806
|
+
}, secondTry, options); // callbackFunction
|
|
807
|
+
}); // promise
|
|
808
|
+
} // if
|
|
930
809
|
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
810
|
+
try {
|
|
811
|
+
var kb = this.store;
|
|
812
|
+
var ds = !deletions ? [] : (0, _terms.isStore)(deletions) ? deletions.statements : deletions instanceof Array ? deletions : [deletions];
|
|
813
|
+
var is = !insertions ? [] : (0, _terms.isStore)(insertions) ? insertions.statements : insertions instanceof Array ? insertions : [insertions];
|
|
814
|
+
if (!(ds instanceof Array)) {
|
|
815
|
+
throw new Error('Type Error ' + typeof ds + ': ' + ds);
|
|
816
|
+
}
|
|
817
|
+
if (!(is instanceof Array)) {
|
|
818
|
+
throw new Error('Type Error ' + typeof is + ': ' + is);
|
|
819
|
+
}
|
|
820
|
+
if (ds.length === 0 && is.length === 0) {
|
|
821
|
+
return callback(null, true); // success -- nothing needed to be done.
|
|
822
|
+
}
|
|
823
|
+
var doc = ds.length ? ds[0].graph : is[0].graph;
|
|
824
|
+
if (!doc) {
|
|
825
|
+
let message = 'Error patching: statement does not specify which document to patch:' + ds[0] + ', ' + is[0];
|
|
826
|
+
// console.log(message)
|
|
827
|
+
throw new Error(message);
|
|
828
|
+
}
|
|
829
|
+
if (doc.termType !== 'NamedNode') {
|
|
830
|
+
let message = 'Error patching: document not a NamedNode:' + ds[0] + ', ' + is[0];
|
|
831
|
+
// console.log(message)
|
|
832
|
+
throw new Error(message);
|
|
833
|
+
}
|
|
834
|
+
var control = this.patchControlFor(doc);
|
|
835
|
+
var startTime = Date.now();
|
|
836
|
+
var props = ['subject', 'predicate', 'object', 'why'];
|
|
837
|
+
var verbs = ['insert', 'delete'];
|
|
838
|
+
var clauses = {
|
|
839
|
+
'delete': ds,
|
|
840
|
+
'insert': is
|
|
841
|
+
};
|
|
842
|
+
verbs.map(function (verb) {
|
|
843
|
+
clauses[verb].map(function (st) {
|
|
844
|
+
if (!doc.equals(st.graph)) {
|
|
845
|
+
throw new Error('update: destination ' + doc + ' inconsistent with delete quad ' + st.graph);
|
|
846
|
+
}
|
|
847
|
+
props.map(function (prop) {
|
|
848
|
+
if (typeof st[prop] === 'undefined') {
|
|
849
|
+
throw new Error('update: undefined ' + prop + ' of statement.');
|
|
967
850
|
}
|
|
968
|
-
props.map(function (prop) {
|
|
969
|
-
if (typeof st[prop] === 'undefined') {
|
|
970
|
-
throw new Error('update: undefined ' + prop + ' of statement.');
|
|
971
|
-
}
|
|
972
|
-
});
|
|
973
851
|
});
|
|
974
852
|
});
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
853
|
+
});
|
|
854
|
+
var protocol = this.editable(doc.value, kb);
|
|
855
|
+
if (protocol === false) {
|
|
856
|
+
throw new Error('Update: Can\'t make changes in uneditable ' + doc);
|
|
857
|
+
}
|
|
858
|
+
if (protocol === undefined) {
|
|
859
|
+
// Not enough metadata
|
|
860
|
+
if (secondTry) {
|
|
861
|
+
throw new Error('Update: Loaded ' + doc + "but still can't figure out what editing protocol it supports.");
|
|
978
862
|
}
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
863
|
+
// console.log(`Update: have not loaded ${doc} before: loading now...`);
|
|
864
|
+
this.store.fetcher.load(doc).then(response => {
|
|
865
|
+
this.update(deletions, insertions, callback, true, options);
|
|
866
|
+
}, err => {
|
|
867
|
+
if (err.response.status === 404) {
|
|
868
|
+
// nonexistent files are fine
|
|
869
|
+
this.update(deletions, insertions, callback, true, options);
|
|
870
|
+
} else {
|
|
871
|
+
throw new Error(`Update: Can't get updatability status ${doc} before patching: ${err}`);
|
|
983
872
|
}
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
_this4.update(deletions, insertions, callback, true, options);
|
|
991
|
-
} else {
|
|
992
|
-
throw new Error("Update: Can't get updatability status ".concat(doc, " before patching: ").concat(err));
|
|
993
|
-
}
|
|
994
|
-
});
|
|
995
|
-
return;
|
|
996
|
-
} else if (protocol.indexOf('SPARQL') >= 0 || protocol.indexOf('N3PATCH') >= 0) {
|
|
997
|
-
var isSparql = protocol.indexOf('SPARQL') >= 0;
|
|
998
|
-
var bnodes = [];
|
|
999
|
-
// change ReadOnly type to Mutable type
|
|
873
|
+
});
|
|
874
|
+
return;
|
|
875
|
+
} else if (protocol.indexOf('SPARQL') >= 0 || protocol.indexOf('N3PATCH') >= 0) {
|
|
876
|
+
var isSparql = protocol.indexOf('SPARQL') >= 0;
|
|
877
|
+
var bnodes = [];
|
|
878
|
+
// change ReadOnly type to Mutable type
|
|
1000
879
|
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
880
|
+
if (ds.length) bnodes = this.statementArrayBnodes(ds);
|
|
881
|
+
if (is.length) bnodes = bnodes.concat(this.statementArrayBnodes(is));
|
|
882
|
+
var context = this.bnodeContext(bnodes, doc);
|
|
883
|
+
var query = isSparql ? this.constructSparqlUpdateQuery(ds, is, context) : this.constructN3PatchQuery(ds, is, context);
|
|
884
|
+
options.contentType = isSparql ? 'application/sparql-update' : 'text/n3';
|
|
1006
885
|
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
try {
|
|
1021
|
-
kb.remove(ds);
|
|
1022
|
-
} catch (e) {
|
|
1023
|
-
success = false;
|
|
1024
|
-
body = 'Remote Ok BUT error deleting ' + ds.length + ' from store!!! ' + e;
|
|
1025
|
-
} // Add in any case -- help recover from weirdness??
|
|
1026
|
-
for (var i = 0; i < is.length; i++) {
|
|
1027
|
-
kb.add(is[i].subject, is[i].predicate, is[i].object, doc);
|
|
1028
|
-
}
|
|
1029
|
-
}
|
|
1030
|
-
callback(uri, success, body, response);
|
|
1031
|
-
control.pendingUpstream -= 1;
|
|
1032
|
-
// When upstream patches have been sent, reload state if downstream waiting
|
|
1033
|
-
if (control.pendingUpstream === 0 && control.downstreamAction) {
|
|
1034
|
-
var downstreamAction = control.downstreamAction;
|
|
1035
|
-
delete control.downstreamAction;
|
|
1036
|
-
// console.log('delayed downstream action:')
|
|
1037
|
-
downstreamAction(doc);
|
|
1038
|
-
}
|
|
1039
|
-
}, options);
|
|
1040
|
-
} else if (protocol.indexOf('DAV') >= 0) {
|
|
1041
|
-
this.updateDav(doc, ds, is, callback, options);
|
|
1042
|
-
} else {
|
|
1043
|
-
if (protocol.indexOf('LOCALFILE') >= 0) {
|
|
886
|
+
// Track pending upstream patches until they have finished their callbackFunction
|
|
887
|
+
control.pendingUpstream = control.pendingUpstream ? control.pendingUpstream + 1 : 1;
|
|
888
|
+
if ('upstreamCount' in control) {
|
|
889
|
+
control.upstreamCount += 1; // count changes we originated ourselves
|
|
890
|
+
// console.log('upstream count up to : ' + control.upstreamCount)
|
|
891
|
+
}
|
|
892
|
+
this.fire(doc.value, query, (uri, success, body, response) => {
|
|
893
|
+
response.elapsedTimeMs = Date.now() - startTime;
|
|
894
|
+
/* console.log(' UpdateManager: Return ' +
|
|
895
|
+
(success ? 'success ' : 'FAILURE ') + (response as Response).status +
|
|
896
|
+
' elapsed ' + (response as any).elapsedTimeMs + 'ms')
|
|
897
|
+
*/
|
|
898
|
+
if (success) {
|
|
1044
899
|
try {
|
|
1045
|
-
|
|
900
|
+
kb.remove(ds);
|
|
1046
901
|
} catch (e) {
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
902
|
+
success = false;
|
|
903
|
+
body = 'Remote Ok BUT error deleting ' + ds.length + ' from store!!! ' + e;
|
|
904
|
+
} // Add in any case -- help recover from weirdness??
|
|
905
|
+
for (let i = 0; i < is.length; i++) {
|
|
906
|
+
kb.add(is[i].subject, is[i].predicate, is[i].object, doc);
|
|
1050
907
|
}
|
|
1051
|
-
} else {
|
|
1052
|
-
throw new Error("Unhandled edit method: '" + protocol + "' for " + doc);
|
|
1053
908
|
}
|
|
909
|
+
callback(uri, success, body, response);
|
|
910
|
+
control.pendingUpstream -= 1;
|
|
911
|
+
// When upstream patches have been sent, reload state if downstream waiting
|
|
912
|
+
if (control.pendingUpstream === 0 && control.downstreamAction) {
|
|
913
|
+
var downstreamAction = control.downstreamAction;
|
|
914
|
+
delete control.downstreamAction;
|
|
915
|
+
// console.log('delayed downstream action:')
|
|
916
|
+
downstreamAction(doc);
|
|
917
|
+
}
|
|
918
|
+
}, options);
|
|
919
|
+
} else if (protocol.indexOf('DAV') >= 0) {
|
|
920
|
+
this.updateDav(doc, ds, is, callback, options);
|
|
921
|
+
} else {
|
|
922
|
+
if (protocol.indexOf('LOCALFILE') >= 0) {
|
|
923
|
+
try {
|
|
924
|
+
this.updateLocalFile(doc, ds, is, callback, options);
|
|
925
|
+
} catch (e) {
|
|
926
|
+
callback(doc.value, false, 'Exception trying to write back file <' + doc.value + '>\n'
|
|
927
|
+
// + tabulator.Util.stackString(e))
|
|
928
|
+
);
|
|
929
|
+
}
|
|
930
|
+
} else {
|
|
931
|
+
throw new Error("Unhandled edit method: '" + protocol + "' for " + doc);
|
|
1054
932
|
}
|
|
1055
|
-
} catch (e) {
|
|
1056
|
-
callback(undefined, false, 'Exception in update: ' + e + '\n' + Util.stackString(e));
|
|
1057
933
|
}
|
|
934
|
+
} catch (e) {
|
|
935
|
+
callback(undefined, false, 'Exception in update: ' + e + '\n' + Util.stackString(e));
|
|
1058
936
|
}
|
|
1059
|
-
}
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
var contentType = kb.the(response, this.ns.httph('content-type')).value;
|
|
937
|
+
}
|
|
938
|
+
updateDav(doc, ds, is, callbackFunction) {
|
|
939
|
+
let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
940
|
+
let kb = this.store;
|
|
941
|
+
// The code below is derived from Kenny's UpdateCenter.js
|
|
942
|
+
var request = kb.any(doc, this.ns.link('request'));
|
|
943
|
+
if (!request) {
|
|
944
|
+
throw new Error('No record of our HTTP GET request for document: ' + doc);
|
|
945
|
+
} // should not happen
|
|
946
|
+
var response = kb.any(request, this.ns.link('response'));
|
|
947
|
+
if (!response) {
|
|
948
|
+
return null; // throw "No record HTTP GET response for document: "+doc
|
|
949
|
+
}
|
|
950
|
+
var contentType = kb.the(response, this.ns.httph('content-type')).value;
|
|
1074
951
|
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
952
|
+
// prepare contents of revised document
|
|
953
|
+
let newSts = kb.statementsMatching(undefined, undefined, undefined, doc).slice(); // copy!
|
|
954
|
+
for (let i = 0; i < ds.length; i++) {
|
|
955
|
+
Util.RDFArrayRemove(newSts, ds[i]);
|
|
956
|
+
}
|
|
957
|
+
for (let i = 0; i < is.length; i++) {
|
|
958
|
+
newSts.push(is[i]);
|
|
959
|
+
}
|
|
960
|
+
const documentString = this.serialize(doc.value, newSts, contentType);
|
|
961
|
+
|
|
962
|
+
// Write the new version back
|
|
963
|
+
var candidateTarget = kb.the(response, this.ns.httph('content-location'));
|
|
964
|
+
var targetURI;
|
|
965
|
+
if (candidateTarget) {
|
|
966
|
+
targetURI = (0, _uri.join)(candidateTarget.value, targetURI);
|
|
967
|
+
}
|
|
968
|
+
options.contentType = contentType;
|
|
969
|
+
options.noMeta = true;
|
|
970
|
+
options.body = documentString;
|
|
971
|
+
return kb.fetcher.webOperation('PUT', targetURI, options).then(response => {
|
|
972
|
+
if (!response.ok) {
|
|
973
|
+
throw new Error(response.error);
|
|
1079
974
|
}
|
|
1080
|
-
for (
|
|
1081
|
-
|
|
975
|
+
for (let i = 0; i < ds.length; i++) {
|
|
976
|
+
kb.remove(ds[i]);
|
|
1082
977
|
}
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
// Write the new version back
|
|
1086
|
-
var candidateTarget = kb.the(response, this.ns.httph('content-location'));
|
|
1087
|
-
var targetURI;
|
|
1088
|
-
if (candidateTarget) {
|
|
1089
|
-
targetURI = (0, _uri.join)(candidateTarget.value, targetURI);
|
|
978
|
+
for (let i = 0; i < is.length; i++) {
|
|
979
|
+
kb.add(is[i].subject, is[i].predicate, is[i].object, doc);
|
|
1090
980
|
}
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
throw new Error(response.error);
|
|
1097
|
-
}
|
|
1098
|
-
for (var _i9 = 0; _i9 < ds.length; _i9++) {
|
|
1099
|
-
kb.remove(ds[_i9]);
|
|
1100
|
-
}
|
|
1101
|
-
for (var _i10 = 0; _i10 < is.length; _i10++) {
|
|
1102
|
-
kb.add(is[_i10].subject, is[_i10].predicate, is[_i10].object, doc);
|
|
1103
|
-
}
|
|
1104
|
-
callbackFunction(doc.value, response.ok, response.responseText, response);
|
|
1105
|
-
}).catch(function (err) {
|
|
1106
|
-
callbackFunction(doc.value, false, err.message, err);
|
|
1107
|
-
});
|
|
1108
|
-
}
|
|
981
|
+
callbackFunction(doc.value, response.ok, response.responseText, response);
|
|
982
|
+
}).catch(err => {
|
|
983
|
+
callbackFunction(doc.value, false, err.message, err);
|
|
984
|
+
});
|
|
985
|
+
}
|
|
1109
986
|
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
var kb = this.store;
|
|
1124
|
-
// console.log('Writing back to local file\n')
|
|
987
|
+
/**
|
|
988
|
+
* Likely deprecated, since this lib no longer deals with browser extension
|
|
989
|
+
*
|
|
990
|
+
* @param doc
|
|
991
|
+
* @param ds
|
|
992
|
+
* @param is
|
|
993
|
+
* @param callbackFunction
|
|
994
|
+
* @param options
|
|
995
|
+
*/
|
|
996
|
+
updateLocalFile(doc, ds, is, callbackFunction) {
|
|
997
|
+
let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
998
|
+
const kb = this.store;
|
|
999
|
+
// console.log('Writing back to local file\n')
|
|
1125
1000
|
|
|
1126
|
-
|
|
1127
|
-
|
|
1001
|
+
// prepare contents of revised document
|
|
1002
|
+
let newSts = kb.statementsMatching(undefined, undefined, undefined, doc).slice(); // copy!
|
|
1128
1003
|
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1004
|
+
for (let i = 0; i < ds.length; i++) {
|
|
1005
|
+
Util.RDFArrayRemove(newSts, ds[i]);
|
|
1006
|
+
}
|
|
1007
|
+
for (let i = 0; i < is.length; i++) {
|
|
1008
|
+
newSts.push(is[i]);
|
|
1009
|
+
}
|
|
1010
|
+
// serialize to the appropriate format
|
|
1011
|
+
var dot = doc.value.lastIndexOf('.');
|
|
1012
|
+
if (dot < 1) {
|
|
1013
|
+
throw new Error('Rewriting file: No filename extension: ' + doc.value);
|
|
1014
|
+
}
|
|
1015
|
+
var ext = doc.value.slice(dot + 1);
|
|
1016
|
+
let contentType = _fetcher.default.CONTENT_TYPE_BY_EXT[ext];
|
|
1017
|
+
if (!contentType) {
|
|
1018
|
+
throw new Error('File extension .' + ext + ' not supported for data write');
|
|
1019
|
+
}
|
|
1020
|
+
options.body = this.serialize(doc.value, newSts, contentType);
|
|
1021
|
+
options.contentType = contentType;
|
|
1022
|
+
kb.fetcher.webOperation('PUT', doc.value, options).then(response => {
|
|
1023
|
+
if (!response.ok) return callbackFunction(doc.value, false, response.error);
|
|
1024
|
+
for (let i = 0; i < ds.length; i++) {
|
|
1025
|
+
kb.remove(ds[i]);
|
|
1139
1026
|
}
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
if (!contentType) {
|
|
1143
|
-
throw new Error('File extension .' + ext + ' not supported for data write');
|
|
1027
|
+
for (let i = 0; i < is.length; i++) {
|
|
1028
|
+
kb.add(is[i].subject, is[i].predicate, is[i].object, doc);
|
|
1144
1029
|
}
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1030
|
+
callbackFunction(doc.value, true, ''); // success!
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
/**
|
|
1035
|
+
* @throws {Error} On unsupported content type
|
|
1036
|
+
*
|
|
1037
|
+
* @returns {string}
|
|
1038
|
+
*/
|
|
1039
|
+
serialize(uri, data, contentType) {
|
|
1040
|
+
const kb = this.store;
|
|
1041
|
+
let documentString;
|
|
1042
|
+
if (typeof data === 'string') {
|
|
1043
|
+
return data;
|
|
1157
1044
|
}
|
|
1158
1045
|
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1046
|
+
// serialize to the appropriate format
|
|
1047
|
+
var sz = (0, _serializer.default)(kb);
|
|
1048
|
+
sz.suggestNamespaces(kb.namespaces);
|
|
1049
|
+
sz.setBase(uri);
|
|
1050
|
+
switch (contentType) {
|
|
1051
|
+
case 'text/xml':
|
|
1052
|
+
case 'application/rdf+xml':
|
|
1053
|
+
documentString = sz.statementsToXML(data);
|
|
1054
|
+
break;
|
|
1055
|
+
case 'text/n3':
|
|
1056
|
+
case 'text/turtle':
|
|
1057
|
+
case 'application/x-turtle': // Legacy
|
|
1058
|
+
case 'application/n3':
|
|
1059
|
+
// Legacy
|
|
1060
|
+
documentString = sz.statementsToN3(data);
|
|
1061
|
+
break;
|
|
1062
|
+
default:
|
|
1063
|
+
throw new Error('Content-type ' + contentType + ' not supported for data serialization');
|
|
1064
|
+
}
|
|
1065
|
+
return documentString;
|
|
1066
|
+
}
|
|
1172
1067
|
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
break;
|
|
1189
|
-
default:
|
|
1190
|
-
throw new Error('Content-type ' + contentType + ' not supported for data serialization');
|
|
1068
|
+
/**
|
|
1069
|
+
* This is suitable for an initial creation of a document.
|
|
1070
|
+
*/
|
|
1071
|
+
put(doc, data, contentType, callback) {
|
|
1072
|
+
const kb = this.store;
|
|
1073
|
+
let documentString;
|
|
1074
|
+
return Promise.resolve().then(() => {
|
|
1075
|
+
documentString = this.serialize(doc.value, data, contentType);
|
|
1076
|
+
return kb.fetcher.webOperation('PUT', doc.value, {
|
|
1077
|
+
contentType,
|
|
1078
|
+
body: documentString
|
|
1079
|
+
});
|
|
1080
|
+
}).then(response => {
|
|
1081
|
+
if (!response.ok) {
|
|
1082
|
+
return callback(doc.value, response.ok, response.error, response);
|
|
1191
1083
|
}
|
|
1192
|
-
|
|
1193
|
-
|
|
1084
|
+
delete kb.fetcher.nonexistent[doc.value];
|
|
1085
|
+
delete kb.fetcher.requested[doc.value]; // @@ could this mess with the requested state machine? if a fetch is in progress
|
|
1194
1086
|
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
}, {
|
|
1199
|
-
key: "put",
|
|
1200
|
-
value: function put(doc, data, contentType, callback) {
|
|
1201
|
-
var _this5 = this;
|
|
1202
|
-
var kb = this.store;
|
|
1203
|
-
var documentString;
|
|
1204
|
-
return Promise.resolve().then(function () {
|
|
1205
|
-
documentString = _this5.serialize(doc.value, data, contentType);
|
|
1206
|
-
return kb.fetcher.webOperation('PUT', doc.value, {
|
|
1207
|
-
contentType: contentType,
|
|
1208
|
-
body: documentString
|
|
1087
|
+
if (typeof data !== 'string') {
|
|
1088
|
+
data.map(st => {
|
|
1089
|
+
kb.addStatement(st);
|
|
1209
1090
|
});
|
|
1210
|
-
}
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
if (typeof data !== 'string') {
|
|
1218
|
-
data.map(function (st) {
|
|
1219
|
-
kb.addStatement(st);
|
|
1220
|
-
});
|
|
1221
|
-
}
|
|
1222
|
-
callback(doc.value, response.ok, '', response);
|
|
1223
|
-
}).catch(function (err) {
|
|
1224
|
-
callback(doc.value, false, err.message);
|
|
1225
|
-
});
|
|
1226
|
-
}
|
|
1091
|
+
}
|
|
1092
|
+
callback(doc.value, response.ok, '', response);
|
|
1093
|
+
}).catch(err => {
|
|
1094
|
+
callback(doc.value, false, err.message);
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1227
1097
|
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
doc.reloadTimeTotal += elapsedTimeMs;
|
|
1265
|
-
doc.reloadTimeCount += 1;
|
|
1098
|
+
/**
|
|
1099
|
+
* Reloads a document.
|
|
1100
|
+
*
|
|
1101
|
+
* Fast and cheap, no metadata. Measure times for the document.
|
|
1102
|
+
* Load it provisionally.
|
|
1103
|
+
* Don't delete the statements before the load, or it will leave a broken
|
|
1104
|
+
* document in the meantime.
|
|
1105
|
+
*
|
|
1106
|
+
* @param kb
|
|
1107
|
+
* @param doc {RDFlibNamedNode}
|
|
1108
|
+
* @param callbackFunction
|
|
1109
|
+
*/
|
|
1110
|
+
reload(kb, doc, callbackFunction) {
|
|
1111
|
+
var startTime = Date.now();
|
|
1112
|
+
// force sets no-cache and
|
|
1113
|
+
const options = {
|
|
1114
|
+
force: true,
|
|
1115
|
+
noMeta: true,
|
|
1116
|
+
clearPreviousData: true
|
|
1117
|
+
};
|
|
1118
|
+
kb.fetcher.nowOrWhenFetched(doc.value, options, function (ok, body, response) {
|
|
1119
|
+
if (!ok) {
|
|
1120
|
+
// console.log(' ERROR reloading data: ' + body)
|
|
1121
|
+
callbackFunction(false, 'Error reloading data: ' + body, response);
|
|
1122
|
+
//@ts-ignore Where does onErrorWasCalled come from?
|
|
1123
|
+
} else if (response.onErrorWasCalled || response.status !== 200) {
|
|
1124
|
+
// console.log(' Non-HTTP error reloading data! onErrorWasCalled=' +
|
|
1125
|
+
//@ts-ignore Where does onErrorWasCalled come from?
|
|
1126
|
+
// response.onErrorWasCalled + ' status: ' + response.status)
|
|
1127
|
+
callbackFunction(false, 'Non-HTTP error reloading data: ' + body, response);
|
|
1128
|
+
} else {
|
|
1129
|
+
var elapsedTimeMs = Date.now() - startTime;
|
|
1130
|
+
if (!doc.reloadTimeTotal) doc.reloadTimeTotal = 0;
|
|
1131
|
+
if (!doc.reloadTimeCount) doc.reloadTimeCount = 0;
|
|
1132
|
+
doc.reloadTimeTotal += elapsedTimeMs;
|
|
1133
|
+
doc.reloadTimeCount += 1;
|
|
1266
1134
|
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1135
|
+
// console.log(' Fetch took ' + elapsedTimeMs + 'ms, av. of ' +
|
|
1136
|
+
// doc.reloadTimeCount + ' = ' +
|
|
1137
|
+
// (doc.reloadTimeTotal / doc.reloadTimeCount) + 'ms.')
|
|
1270
1138
|
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1139
|
+
callbackFunction(true);
|
|
1140
|
+
}
|
|
1141
|
+
});
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
exports.default = UpdateManager;
|