@@ -339,6 +339,7 @@ private function handleSparqlUpdate(Response $response, string $path, $contents)
339339 $ response = $ response ->withStatus ($ success ? 201 : 500 );
340340
341341 if ($ success ) {
342+ $ this ->removeLinkFromMetaFileFor ($ path );
342343 $ this ->sendWebsocketUpdate ($ path );
343344 }
344345 } catch (EasyRdf_Exception $ exception ) {
@@ -385,6 +386,7 @@ private function handleCreateRequest(Response $response, string $path, $contents
385386 }
386387
387388 if ($ success ) {
389+ $ this ->removeLinkFromMetaFileFor ($ path );
388390 $ response = $ response ->withHeader ("Location " , $ this ->baseUrl . $ path );
389391 $ response = $ response ->withStatus (201 );
390392 $ this ->sendWebsocketUpdate ($ path );
@@ -420,6 +422,7 @@ private function handleCreateDirectoryRequest(Response $response, string $path):
420422 $ success = $ filesystem ->createDir ($ path );
421423 $ response = $ response ->withStatus ($ success ? 201 : 500 );
422424 if ($ success ) {
425+ $ this ->removeLinkFromMetaFileFor ($ path );
423426 $ this ->sendWebsocketUpdate ($ path );
424427 }
425428 }
@@ -507,6 +510,7 @@ private function handleUpdateRequest(Response $response, string $path, string $c
507510 $ success = $ filesystem ->update ($ path , $ contents );
508511 $ response = $ response ->withStatus ($ success ? 201 : 500 );
509512 if ($ success ) {
513+ $ this ->removeLinkFromMetaFileFor ($ path );
510514 $ this ->sendWebsocketUpdate ($ path );
511515 }
512516 }
@@ -913,4 +917,61 @@ private function findPath(array $rdfPaths, string $path)
913917
914918 return $ path ;
915919 }
920+
921+ private function removeLinkFromMetaFileFor ($ path ): bool
922+ {
923+ $ result = false ;
924+
925+ if ($ this ->hasDescribedBy ($ path )) {
926+ $ describedByPath = $ this ->getDescribedByPath ($ path );
927+
928+ $ graph = $ this ->getGraph ();
929+
930+ try {
931+ $ contents = $ this ->filesystem ->read ($ describedByPath );
932+ $ graph ->parse ($ contents , 'turtle ' , '/ ' .$ describedByPath );
933+ } catch (\Throwable $ e ) {
934+ return false ;
935+ }
936+
937+ // A resource might be added for a folder but written to a file,
938+ // or vice-versa. In both cases, the _other_ entry also needs to be
939+ // removed. And depending on the RDF entry, the resource might have
940+ // a leading slash or not, so that also needs to be checked.
941+ $ normalizedPath = trim ($ path , '/ ' );
942+ $ resourcePaths = array_unique ([
943+ $ normalizedPath ,
944+ $ normalizedPath . '/ ' ,
945+ '/ ' . $ normalizedPath ,
946+ '/ ' . $ normalizedPath . '/ ' ,
947+ ]);
948+
949+ // @CHECKME: If an entry for a sub-folder is present but then a file is written,
950+ // removing the folder, should the sub-folder entry also be removed?
951+
952+ $ changed = false ;
953+ foreach ($ resourcePaths as $ resourcePath ) {
954+ $ resource = $ graph ->resource ($ resourcePath );
955+
956+ $ predicates = $ resource ->propertyUris ();
957+ foreach ($ predicates as $ predicate ) {
958+ if (strpos ($ predicate , 'https://purl.org/pdsinterop/link-metadata# ' ) === 0 ) {
959+ $ changed = true ;
960+ $ graph ->deleteSingleProperty ($ resource , $ predicate );
961+ }
962+ }
963+ }
964+
965+ if ($ changed ) {
966+ $ changedContents = $ graph ->serialise ('turtle ' );
967+ try {
968+ $ result = $ this ->filesystem ->update ($ describedByPath , $ changedContents );
969+ } catch (FileNotFoundException $ exception ) {
970+ // $result is already false;
971+ }
972+ }
973+ }
974+
975+ return $ result ;
976+ }
916977}
0 commit comments