11module . exports = handler
22
33var mime = require ( 'mime-types' )
4- var fs = require ( 'fs' )
5- var $rdf = require ( 'rdflib' )
64var debug = require ( '../debug' ) . handlers
75var utils = require ( '../utils.js' )
86var error = require ( '../http-error' )
9- const waterfall = require ( 'run-waterfall' )
7+ const sparqlPatch = require ( './patch/sparql-patcher.js' )
8+ const sparqlUpdatePatch = require ( './patch/sparql-update-patcher.js' )
109
1110const DEFAULT_CONTENT_TYPE = 'text/turtle'
1211
@@ -39,15 +38,15 @@ function patchHandler (req, res, next) {
3938 debug ( 'PATCH -- Content-type ' + patchContentType + ' patching target ' + targetContentType + ' <' + targetURI + '>' )
4039
4140 if ( patchContentType === 'application/sparql' ) {
42- sparql ( filename , targetURI , req . text , function ( err , result ) {
41+ sparqlPatch ( filename , targetURI , req . text , function ( err , result ) {
4342 if ( err ) {
4443 return next ( err )
4544 }
4645 res . json ( result )
4746 return next ( )
4847 } )
4948 } else if ( patchContentType === 'application/sparql-update' ) {
50- return sparqlUpdate ( filename , targetURI , req . text , function ( err , patchKB ) {
49+ return sparqlUpdatePatch ( filename , targetURI , req . text , function ( err , patchKB ) {
5150 if ( err ) {
5251 return next ( err )
5352 }
@@ -61,138 +60,3 @@ function patchHandler (req, res, next) {
6160 return next ( error ( 400 , 'Unknown patch content type: ' + patchContentType ) )
6261 }
6362} // postOrPatch
64-
65- function sparql ( filename , targetURI , text , callback ) {
66- debug ( 'PATCH -- parsing query ...' )
67- var patchURI = targetURI // @@@ beware the triples from the patch ending up in the same place
68- var patchKB = $rdf . graph ( )
69- var targetKB = $rdf . graph ( )
70- var targetContentType = mime . lookup ( filename ) || DEFAULT_CONTENT_TYPE
71- var query = $rdf . SPARQLToQuery ( text , false , patchKB , patchURI ) // last param not used ATM
72-
73- fs . readFile ( filename , { encoding : 'utf8' } , function ( err , dataIn ) {
74- if ( err ) {
75- return callback ( error ( 404 , 'Patch: Original file read error:' + err ) )
76- }
77-
78- debug ( 'PATCH -- File read OK ' + dataIn . length )
79- debug ( 'PATCH -- parsing target file ...' )
80-
81- try {
82- $rdf . parse ( dataIn , targetKB , targetURI , targetContentType )
83- } catch ( e ) {
84- debug ( 'Patch: Target ' + targetContentType + ' file syntax error:' + e )
85- return callback ( error ( 500 , 'Patch: Target ' + targetContentType + ' file syntax error:' + e ) )
86- }
87- debug ( 'PATCH -- Target parsed OK ' )
88-
89- var bindingsArray = [ ]
90-
91- var onBindings = function ( bindings ) {
92- var b = { }
93- var v
94- var x
95- for ( v in bindings ) {
96- if ( bindings . hasOwnProperty ( v ) ) {
97- x = bindings [ v ]
98- b [ v ] = x . uri ? { 'type' : 'uri' , 'value' : x . uri } : { 'type' : 'literal' , 'value' : x . value }
99- if ( x . lang ) {
100- b [ v ] [ 'xml:lang' ] = x . lang
101- }
102- if ( x . dt ) {
103- b [ v ] . dt = x . dt . uri // @@@ Correct? @@ check
104- }
105- }
106- }
107- debug ( 'PATCH -- bindings: ' + JSON . stringify ( b ) )
108- bindingsArray . push ( b )
109- }
110-
111- var onDone = function ( ) {
112- debug ( 'PATCH -- Query done, no. bindings: ' + bindingsArray . length )
113- return callback ( null , {
114- 'head' : {
115- 'vars' : query . vars . map ( function ( v ) {
116- return v . toNT ( )
117- } )
118- } ,
119- 'results' : {
120- 'bindings' : bindingsArray
121- }
122- } )
123- }
124-
125- var fetcher = new $rdf . Fetcher ( targetKB , 10000 , true )
126- targetKB . query ( query , onBindings , fetcher , onDone )
127- } )
128- }
129-
130- function sparqlUpdate ( filename , targetURI , text , callback ) {
131- var patchURI = targetURI // @@@ beware the triples from the patch ending up in the same place
132- var patchKB = $rdf . graph ( )
133- var targetKB = $rdf . graph ( )
134- var targetContentType = mime . lookup ( filename ) || DEFAULT_CONTENT_TYPE
135-
136- debug ( 'PATCH -- parsing patch ...' )
137- var patchObject
138- try {
139- // Must parse relative to document's base address but patch doc should get diff URI
140- patchObject = $rdf . sparqlUpdateParser ( text , patchKB , patchURI )
141- } catch ( e ) {
142- return callback ( error ( 400 , 'Patch format syntax error:\n' + e + '\n' ) )
143- }
144- debug ( 'PATCH -- reading target file ...' )
145-
146- waterfall ( [
147- ( cb ) => {
148- fs . stat ( filename , ( err ) => {
149- if ( ! err ) return cb ( )
150-
151- fs . writeFile ( filename , '' , ( err ) => {
152- if ( err ) {
153- return cb ( error ( err , 'Error creating the patch target' ) )
154- }
155- cb ( )
156- } )
157- } )
158- } ,
159- ( cb ) => {
160- fs . readFile ( filename , { encoding : 'utf8' } , function ( err , dataIn ) {
161- if ( err ) {
162- return cb ( error ( 500 , 'Error reading the patch target' ) )
163- }
164-
165- debug ( 'PATCH -- target read OK ' + dataIn . length + ' bytes. Parsing...' )
166-
167- try {
168- $rdf . parse ( dataIn , targetKB , targetURI , targetContentType )
169- } catch ( e ) {
170- debug ( 'Patch: Target ' + targetContentType + ' file syntax error:' + e )
171- return cb ( error ( 500 , 'Patch: Target ' + targetContentType + ' file syntax error:' + e ) )
172- }
173-
174- var target = patchKB . sym ( targetURI )
175- debug ( 'PATCH -- Target parsed OK, patching... ' )
176-
177- targetKB . applyPatch ( patchObject , target , function ( err ) {
178- if ( err ) {
179- var message = err . message || err // returns string at the moment
180- debug ( 'PATCH FAILED. Returning 409. Message: \'' + message + '\'' )
181- return cb ( error ( 409 , 'Error when applying the patch' ) )
182- }
183- debug ( 'PATCH -- Patched. Writeback URI base ' + targetURI )
184- var data = $rdf . serialize ( target , targetKB , targetURI , targetContentType )
185- // debug('Writeback data: ' + data)
186-
187- fs . writeFile ( filename , data , { encoding : 'utf8' } , function ( err , data ) {
188- if ( err ) {
189- return cb ( error ( 500 , 'Failed to write file back after patch: ' + err ) )
190- }
191- debug ( 'PATCH -- applied OK (sync)' )
192- return cb ( null , patchKB )
193- } )
194- } )
195- } )
196- }
197- ] , callback )
198- }
0 commit comments