|
<!doctype html> |
|
<meta charset="UTF-8"> |
|
<meta property="og:image" content="https://user-images.githubusercontent.com/195757/149627370-f1e57a0a-bbe6-44e8-90a6-59be4acf5611.png" /> |
|
<title>Easy RDF Example with Flysystem</title> |
|
<link rel="icon" href="https://www.easyrdf.org/favicon.ico"> |
|
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet"> |
|
<style> |
|
body { |
|
background-color: #4D5760; |
|
font-family: 'Roboto', sans-serif; |
|
color: white; |
|
} |
|
|
|
dl { |
|
display: flex; |
|
flex-flow: row; |
|
flex-wrap: wrap; |
|
} |
|
|
|
dd, dt { |
|
border-bottom: 1px solid #ccc; |
|
flex: 0 0 50%; |
|
overflow: hidden; |
|
padding: 0.15em 0; |
|
text-overflow: ellipsis; |
|
} |
|
|
|
dd:last-of-type, dt:last-of-type { |
|
border-bottom: none; |
|
} |
|
|
|
dd { |
|
font-family: monospace; |
|
margin-left: auto; |
|
text-align: left; |
|
} |
|
|
|
details { |
|
background-color: #4D5760; |
|
border-radius: 0.5em; |
|
border: 1px solid #ccc; |
|
margin: 0.5em 0; |
|
opacity: 0.65; |
|
} |
|
|
|
details:hover { |
|
opacity: 1; |
|
} |
|
|
|
summary { |
|
border-radius: 0.5em; |
|
background-color: #4D5760; |
|
color: white; |
|
cursor: pointer; |
|
font-weight: bold; |
|
margin: 0; |
|
padding: 0.25em 0.5em; |
|
} |
|
|
|
ol { |
|
list-style-type: none; |
|
list-style-position: inside; |
|
margin-bottom: 1em; |
|
padding-left: 0; |
|
} |
|
|
|
ol li > * { |
|
padding: 0.3em; |
|
} |
|
|
|
ol li { |
|
background: white; |
|
border-radius: 0.5em; |
|
border: 1px solid #666666; |
|
margin: 1em; |
|
} |
|
|
|
pre, .php { |
|
background-color: #eee; |
|
border-radius: 0.5em; |
|
position: relative; |
|
white-space: pre-wrap; |
|
} |
|
|
|
pre { |
|
padding: 1em; |
|
color: #000; |
|
} |
|
|
|
section { |
|
background-color: white; |
|
border: 1px solid #ccc; |
|
border-radius: 0.5em; |
|
color: #333; |
|
margin: 1em; |
|
padding: 1em; |
|
} |
|
|
|
/* |
|
li div[style]:not([id]), |
|
li div[id] { |
|
border-top-left-radius: 0.5em; |
|
border-top-right-radius: 0.5em; |
|
} |
|
*/ |
|
|
|
.compare { |
|
display: flex; |
|
flex-wrap: wrap; |
|
font-size: 0.85em; |
|
justify-content: space-evenly; |
|
} |
|
|
|
.compare h3 { |
|
text-align: center; |
|
margin: 0; |
|
} |
|
|
|
.php { |
|
line-height: 0.85em; |
|
} |
|
|
|
.php.php-inline { |
|
border-radius: 0.5em; |
|
display: inline; |
|
line-height: inherit; |
|
padding: 0.15em 0 0.35em 0.35em; |
|
margin-right: 0.35em; |
|
position: static; |
|
white-space: inherit; |
|
} |
|
|
|
.php::before, |
|
.turtle::before { |
|
border-bottom-left-radius: 0.5em; |
|
border-bottom: 1px solid; |
|
border-left: 1px solid; |
|
border-top-right-radius: 0.5em; |
|
display: block; |
|
font-family: sans-serif; |
|
font-size: 0.8em; |
|
padding: 0.3em; |
|
position: absolute; |
|
right: 0; |
|
top: 0; |
|
} |
|
|
|
.php:not(.php-inline)::before { |
|
background: #8892BF; |
|
color: white; |
|
content: 'PHP'; |
|
} |
|
|
|
.turtle::before { |
|
background: #0C4393; |
|
color: white; |
|
content: 'Turtle'; |
|
} |
|
</style> |
|
|
|
<h1>Easy RDF Example</h1> |
|
<p> |
|
This example shows how to read a RDF file, find a node, remove that node or |
|
a property from that node and write the result back to the file, using Flysystem |
|
</p> |
|
|
|
<section> |
|
<h2>Set things up</h2> |
|
<p> |
|
Creating a Flysystem and EasyRDF Graph is simplicity itself: |
|
</p> |
|
<div class="php"><code><span style="color: #000000"> |
|
<span style="color: #0000BB"> $filesystem </span><span style="color: #007700">= new </span><span style="color: #0000BB">Filesystem</span><span style="color: #007700">(new </span><span style="color: #0000BB">Local</span><span style="color: #007700">(</span><span style="color: #0000BB">__DIR__</span><span style="color: #007700">));<br /> <br /> </span><span style="color: #0000BB">$graph </span><span style="color: #007700">= new </span><span style="color: #0000BB">EasyRdf_Graph</span><span style="color: #007700">(); </span> |
|
</span> |
|
</code></div> |
|
<p> |
|
To make your live easier and the Turtle pretttier, it is possible to specify |
|
specific namespaces to be used as a prefix: |
|
</p> |
|
<div class="php"><code><span style="color: #000000"> |
|
<span style="color: #0000BB"> $namespace </span><span style="color: #007700">= </span><span style="color: #DD0000">'https://purl.org/pdsinterop/link-metadata#'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">EasyRdf_Namespace</span><span style="color: #007700">::</span><span style="color: #0000BB">set</span><span style="color: #007700">(</span><span style="color: #DD0000">'lm'</span><span style="color: #007700">, </span><span style="color: #0000BB">$namespace</span><span style="color: #007700">);</span> |
|
</span> |
|
</code></div> |
|
</section> |
|
|
|
<section> |
|
<h2>Read a RDF file</h2> |
|
<p> |
|
The next thing we will want to do is read the contents from a file and feed it to the Graph to parse it. |
|
</p> |
|
<p>Read file <code>./input.ttl</code> as RDF resource:</p> |
|
<div class="php"><code><span style="color: #000000"> |
|
<span style="color: #0000BB"> $filename </span><span style="color: #007700">= </span><span style="color: #DD0000">'./input.ttl'</span><span style="color: #007700">;<br /><br /> </span><span style="color: #0000BB">$contents </span><span style="color: #007700">= </span><span style="color: #0000BB">$filesystem</span><span style="color: #007700">-></span><span style="color: #0000BB">read</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">);<br /><br /> </span><span style="color: #0000BB">$graph</span><span style="color: #007700">-></span><span style="color: #0000BB">parse</span><span style="color: #007700">(</span><span style="color: #0000BB">$contents</span><span style="color: #007700">, </span><span style="color: #0000BB">$format</span><span style="color: #007700">, </span><span style="color: #0000BB">$filename</span><span style="color: #007700">);</span> |
|
</span> |
|
</code></div> |
|
<p> |
|
If we compare the original file contents with the parsed contents, we can see the graph is slightly different, |
|
according to Easy RDF: |
|
</p> |
|
<div class="compare"> |
|
<div> |
|
<h3>Original</h3> |
|
<pre class="turtle"><code>@prefix : <#>. |
|
@prefix tes: <testDeleted/>. |
|
@prefix lin: <https://purl.org/pdsinterop/link-metadata#>. |
|
@prefix stor: <./>. |
|
@prefix te: <testForget/>. |
|
|
|
@prefix dc: <http://purl.org/dc/terms/> . |
|
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
|
|
|
|
|
tes: lin:deleted "Because we say so". |
|
|
|
stor:testExtraRedirect lin:redirectTemporary <https://localhost/redirect-extra.html>. |
|
|
|
te: lin:forget "You have the right to be forgotten"; |
|
dc:title "Nested Test document" ; |
|
rdfs:comment "Dummy file for testing metadata file in a parent directory" ; |
|
rdfs:comment "It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'" . |
|
|
|
stor:testPermanentRedirect lin:redirectPermanent <https://localhost/redirect-permanent.html>. |
|
|
|
stor:testTempRedirect lin:redirectTemporary <https://localhost/redirect-temporary.html>. |
|
</code></pre> |
|
</div> |
|
<div> |
|
<h3>Parsed</h3> |
|
<pre class="turtle"><code>@prefix lm: <https://purl.org/pdsinterop/link-metadata#> . |
|
@prefix dc: <http://purl.org/dc/terms/> . |
|
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
|
|
|
<testDeleted/> lm:deleted "Because we say so" . |
|
<testExtraRedirect> lm:redirectTemporary <https://localhost/redirect-extra.html> . |
|
<testForget/> |
|
lm:forget "You have the right to be forgotten" ; |
|
dc:title "Nested Test document" ; |
|
rdfs:comment "Dummy file for testing metadata file in a parent directory", "It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'" . |
|
|
|
<testPermanentRedirect> lm:redirectPermanent <https://localhost/redirect-permanent.html> . |
|
<testTempRedirect> lm:redirectTemporary <https://localhost/redirect-temporary.html> . |
|
</code></pre> |
|
</div> |
|
</div> |
|
<p> |
|
To see what the parsed graph looks like, we can use <span class="php php-inline"><code><span style="color: #000000"> |
|
<span style="color: #0000BB">$graph</span><span style="color: #007700">-></span><span style="color: #0000BB">dump</span><span style="color: #007700">()</span> |
|
</span> |
|
</code></span> to get nice output: |
|
</p> |
|
<div style='font-family:arial; font-weight: bold; padding:0.5em; color: black; background-color:lightgrey;border:dashed 1px grey;'>Graph: </div> |
|
<div id='testDeleted/' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testDeleted/' style='text-decoration:none;color:blue'>testDeleted/</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:deleted</span> <span style='font-size:130%'>→</span> <span style='color:black'>"Because we say so"</span></div></div></div> |
|
<div id='testExtraRedirect' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testExtraRedirect' style='text-decoration:none;color:blue'>testExtraRedirect</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:redirectTemporary</span> <span style='font-size:130%'>→</span> <a href='https://localhost/redirect-extra.html' style='text-decoration:none;color:blue'>https://localhost/redirect-extra.html</a></div></div></div> |
|
<div id='testForget/' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testForget/' style='text-decoration:none;color:blue'>testForget/</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:forget</span> <span style='font-size:130%'>→</span> <span style='color:black'>"You have the right to be forgotten"</span></div> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>dc:title</span> <span style='font-size:130%'>→</span> <span style='color:black'>"Nested Test document"</span></div> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>rdfs:comment</span> <span style='font-size:130%'>→</span> <span style='color:black'>"Dummy file for testing metadata file in a parent directory"</span>, <span style='color:black'>"It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'"</span></div></div></div> |
|
<div id='testPermanentRedirect' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testPermanentRedirect' style='text-decoration:none;color:blue'>testPermanentRedirect</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:redirectPermanent</span> <span style='font-size:130%'>→</span> <a href='https://localhost/redirect-permanent.html' style='text-decoration:none;color:blue'>https://localhost/redirect-permanent.html</a></div></div></div> |
|
<div id='testTempRedirect' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testTempRedirect' style='text-decoration:none;color:blue'>testTempRedirect</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:redirectTemporary</span> <span style='font-size:130%'>→</span> <a href='https://localhost/redirect-temporary.html' style='text-decoration:none;color:blue'>https://localhost/redirect-temporary.html</a></div></div></div> |
|
</section> |
|
|
|
<section> |
|
<h2>Find a node</h2> |
|
|
|
<p> |
|
There are various ways to get a node from a graph. Which one is best for |
|
you depends on the usecase you find yourself in. |
|
</p> |
|
|
|
<p> |
|
Given that an RDF statement is: <code>(<strong>subject</strong>) (<strong>predicate</strong>) (<strong>object</strong>)</code> |
|
</p> |
|
<p>If we use the following for our example:</p> |
|
|
|
<pre class="turtle"><code><testForget/> lm:forget "You have the right to be forgotten"</code></pre> |
|
|
|
<p>It is possible to get that resource based on its subject or predicate.</p> |
|
|
|
<div class="php"><code><span style="color: #000000"> |
|
<span style="color: #0000BB"> </span><span style="color: #FF8000">/*/ Get Resource by subject /*/<br /> </span><span style="color: #0000BB">$resource </span><span style="color: #007700">= </span><span style="color: #0000BB">$graph</span><span style="color: #007700">-></span><span style="color: #0000BB">resource</span><span style="color: #007700">(</span><span style="color: #DD0000">'<testForget/>'</span><span style="color: #007700">);<br /> <br /> </span><span style="color: #FF8000">/*/ Get Resources by predicate /*/<br /> </span><span style="color: #0000BB">$resourceMatching </span><span style="color: #007700">= </span><span style="color: #0000BB">$graph</span><span style="color: #007700">-></span><span style="color: #0000BB">resourcesMatching</span><span style="color: #007700">(</span><span style="color: #DD0000">'lm:forget'</span><span style="color: #007700">);<br /> <br /> </span><span style="color: #FF8000">/*/ Check if a resource has a specific predicate /*/<br /> </span><span style="color: #0000BB">$hasPredicate </span><span style="color: #007700">= </span><span style="color: #0000BB">$resource</span><span style="color: #007700">-></span><span style="color: #0000BB">hasProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'lm:forget'</span><span style="color: #007700">);<br /> <br /> </span><span style="color: #FF8000">/*/ Get predicates, with prefix /*/<br /> </span><span style="color: #0000BB">$predicatesPrefixed </span><span style="color: #007700">= </span><span style="color: #0000BB">$resource</span><span style="color: #007700">-></span><span style="color: #0000BB">properties</span><span style="color: #007700">();<br /><br /> </span><span style="color: #FF8000">/*/ Get predicates, as full URIs /*/<br /> </span><span style="color: #0000BB">$predicatesUris </span><span style="color: #007700">= </span><span style="color: #0000BB">$resource</span><span style="color: #007700">-></span><span style="color: #0000BB">propertyUris</span><span style="color: #007700">();<br /> <br /> </span><span style="color: #FF8000">/*/ Get value (i.e. object) based on subject and predicate /*/<br /> </span><span style="color: #0000BB">$object </span><span style="color: #007700">= </span><span style="color: #0000BB">$graph</span><span style="color: #007700">-></span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'<testForget/>'</span><span style="color: #007700">, </span><span style="color: #DD0000">'lm:forget'</span><span style="color: #007700">);<br /> </span> |
|
</span> |
|
</code></div> |
|
<p>It is possible to get all resources, so they can be iterated</p> |
|
|
|
<div class="php"><code><span style="color: #000000"> |
|
<span style="color: #0000BB"> $resources </span><span style="color: #007700">= </span><span style="color: #0000BB">$graph</span><span style="color: #007700">-></span><span style="color: #0000BB">resources</span><span style="color: #007700">();</span> |
|
</span> |
|
</code></div> |
|
<details> |
|
<summary>Read 8 resources</summary> |
|
<ol> |
|
<li> |
|
<div><a href='testDeleted/' style='text-decoration:none;color:blue'>testDeleted/</a></div> |
|
<div id='testDeleted/' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testDeleted/' style='text-decoration:none;color:blue'>testDeleted/</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:deleted</span> <span style='font-size:130%'>→</span> <span style='color:black'>"Because we say so"</span></div></div></div> |
|
<dl> |
|
<dt>Local Name</dt> |
|
<dd></dd> |
|
|
|
|
|
<dt>Properties</dt> |
|
<dd>array ( |
|
0 => 'lm:deleted', |
|
)</dd> |
|
|
|
<dt>PropertyUris</dt> |
|
<dd>array ( |
|
0 => 'https://purl.org/pdsinterop/link-metadata#deleted', |
|
)</dd> |
|
|
|
|
|
|
|
<dt>URI</dt> |
|
<dd>testDeleted/</dd> |
|
|
|
</dl> |
|
</li> |
|
<li> |
|
<div><a href='testExtraRedirect' style='text-decoration:none;color:blue'>testExtraRedirect</a></div> |
|
<div id='testExtraRedirect' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testExtraRedirect' style='text-decoration:none;color:blue'>testExtraRedirect</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:redirectTemporary</span> <span style='font-size:130%'>→</span> <a href='https://localhost/redirect-extra.html' style='text-decoration:none;color:blue'>https://localhost/redirect-extra.html</a></div></div></div> |
|
<dl> |
|
<dt>Local Name</dt> |
|
<dd>testExtraRedirect</dd> |
|
|
|
|
|
<dt>Properties</dt> |
|
<dd>array ( |
|
0 => 'lm:redirectTemporary', |
|
)</dd> |
|
|
|
<dt>PropertyUris</dt> |
|
<dd>array ( |
|
0 => 'https://purl.org/pdsinterop/link-metadata#redirectTemporary', |
|
)</dd> |
|
|
|
|
|
|
|
<dt>URI</dt> |
|
<dd>testExtraRedirect</dd> |
|
|
|
</dl> |
|
</li> |
|
<li> |
|
<div><a href='testForget/' style='text-decoration:none;color:blue'>testForget/</a></div> |
|
<div id='testForget/' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testForget/' style='text-decoration:none;color:blue'>testForget/</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:forget</span> <span style='font-size:130%'>→</span> <span style='color:black'>"You have the right to be forgotten"</span></div> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>dc:title</span> <span style='font-size:130%'>→</span> <span style='color:black'>"Nested Test document"</span></div> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>rdfs:comment</span> <span style='font-size:130%'>→</span> <span style='color:black'>"Dummy file for testing metadata file in a parent directory"</span>, <span style='color:black'>"It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'"</span></div></div></div> |
|
<dl> |
|
<dt>Local Name</dt> |
|
<dd></dd> |
|
|
|
|
|
<dt>Properties</dt> |
|
<dd>array ( |
|
0 => 'lm:forget', |
|
1 => 'dc:title', |
|
2 => 'rdfs:comment', |
|
)</dd> |
|
|
|
<dt>PropertyUris</dt> |
|
<dd>array ( |
|
0 => 'https://purl.org/pdsinterop/link-metadata#forget', |
|
1 => 'http://purl.org/dc/terms/title', |
|
2 => 'http://www.w3.org/2000/01/rdf-schema#comment', |
|
)</dd> |
|
|
|
|
|
|
|
<dt>URI</dt> |
|
<dd>testForget/</dd> |
|
|
|
</dl> |
|
</li> |
|
<li> |
|
<div><a href='testPermanentRedirect' style='text-decoration:none;color:blue'>testPermanentRedirect</a></div> |
|
<div id='testPermanentRedirect' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testPermanentRedirect' style='text-decoration:none;color:blue'>testPermanentRedirect</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:redirectPermanent</span> <span style='font-size:130%'>→</span> <a href='https://localhost/redirect-permanent.html' style='text-decoration:none;color:blue'>https://localhost/redirect-permanent.html</a></div></div></div> |
|
<dl> |
|
<dt>Local Name</dt> |
|
<dd>testPermanentRedirect</dd> |
|
|
|
|
|
<dt>Properties</dt> |
|
<dd>array ( |
|
0 => 'lm:redirectPermanent', |
|
)</dd> |
|
|
|
<dt>PropertyUris</dt> |
|
<dd>array ( |
|
0 => 'https://purl.org/pdsinterop/link-metadata#redirectPermanent', |
|
)</dd> |
|
|
|
|
|
|
|
<dt>URI</dt> |
|
<dd>testPermanentRedirect</dd> |
|
|
|
</dl> |
|
</li> |
|
<li> |
|
<div><a href='testTempRedirect' style='text-decoration:none;color:blue'>testTempRedirect</a></div> |
|
<div id='testTempRedirect' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testTempRedirect' style='text-decoration:none;color:blue'>testTempRedirect</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:redirectTemporary</span> <span style='font-size:130%'>→</span> <a href='https://localhost/redirect-temporary.html' style='text-decoration:none;color:blue'>https://localhost/redirect-temporary.html</a></div></div></div> |
|
<dl> |
|
<dt>Local Name</dt> |
|
<dd>testTempRedirect</dd> |
|
|
|
|
|
<dt>Properties</dt> |
|
<dd>array ( |
|
0 => 'lm:redirectTemporary', |
|
)</dd> |
|
|
|
<dt>PropertyUris</dt> |
|
<dd>array ( |
|
0 => 'https://purl.org/pdsinterop/link-metadata#redirectTemporary', |
|
)</dd> |
|
|
|
|
|
|
|
<dt>URI</dt> |
|
<dd>testTempRedirect</dd> |
|
|
|
</dl> |
|
</li> |
|
<li> |
|
<div><a href='https://localhost/redirect-extra.html' style='text-decoration:none;color:blue'>https://localhost/redirect-extra.html</a></div> |
|
<dl> |
|
<dt>Local Name</dt> |
|
<dd>redirect-extra.html</dd> |
|
|
|
|
|
|
|
|
|
<dt>ReversePropertyUris</dt> |
|
<dd>array ( |
|
0 => 'https://purl.org/pdsinterop/link-metadata#redirectTemporary', |
|
)</dd> |
|
|
|
|
|
<dt>URI</dt> |
|
<dd>https://localhost/redirect-extra.html</dd> |
|
|
|
</dl> |
|
</li> |
|
<li> |
|
<div><a href='https://localhost/redirect-permanent.html' style='text-decoration:none;color:blue'>https://localhost/redirect-permanent.html</a></div> |
|
<dl> |
|
<dt>Local Name</dt> |
|
<dd>redirect-permanent.html</dd> |
|
|
|
|
|
|
|
|
|
<dt>ReversePropertyUris</dt> |
|
<dd>array ( |
|
0 => 'https://purl.org/pdsinterop/link-metadata#redirectPermanent', |
|
)</dd> |
|
|
|
|
|
<dt>URI</dt> |
|
<dd>https://localhost/redirect-permanent.html</dd> |
|
|
|
</dl> |
|
</li> |
|
<li> |
|
<div><a href='https://localhost/redirect-temporary.html' style='text-decoration:none;color:blue'>https://localhost/redirect-temporary.html</a></div> |
|
<dl> |
|
<dt>Local Name</dt> |
|
<dd>redirect-temporary.html</dd> |
|
|
|
|
|
|
|
|
|
<dt>ReversePropertyUris</dt> |
|
<dd>array ( |
|
0 => 'https://purl.org/pdsinterop/link-metadata#redirectTemporary', |
|
)</dd> |
|
|
|
|
|
<dt>URI</dt> |
|
<dd>https://localhost/redirect-temporary.html</dd> |
|
|
|
</dl> |
|
</li> |
|
</ol> |
|
</details> |
|
</section> |
|
|
|
<section> |
|
<h2>Remove predicate from a resource for a specific namespace (URI base)</h2> |
|
<p> |
|
Lets say we want to remove all predicates that have the namespace |
|
<code>https://purl.org/pdsinterop/link-metadata</code> from all the resource: |
|
</p> |
|
<div class="php"><code><span style="color: #000000"> |
|
<span style="color: #0000BB"><br /> $namespace </span><span style="color: #007700">= </span><span style="color: #DD0000">'https://purl.org/pdsinterop/link-metadata#'</span><span style="color: #007700">;<br /><br /> foreach (</span><span style="color: #0000BB">$graph</span><span style="color: #007700">-></span><span style="color: #0000BB">resources</span><span style="color: #007700">() as </span><span style="color: #0000BB">$resource</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$predicates </span><span style="color: #007700">= </span><span style="color: #0000BB">$resource</span><span style="color: #007700">-></span><span style="color: #0000BB">propertyUris</span><span style="color: #007700">();<br /><br /> foreach (</span><span style="color: #0000BB">$predicates </span><span style="color: #007700">as </span><span style="color: #0000BB">$predicate</span><span style="color: #007700">) {<br /> if (</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$predicate</span><span style="color: #007700">, </span><span style="color: #0000BB">$namespace</span><span style="color: #007700">) === </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$graph</span><span style="color: #007700">-></span><span style="color: #0000BB">deleteSingleProperty</span><span style="color: #007700">(</span><span style="color: #0000BB">$resource</span><span style="color: #007700">, </span><span style="color: #0000BB">$predicate</span><span style="color: #007700">);<br /> }<br /> }<br /> }</span> |
|
</span> |
|
</code></div> |
|
<p> |
|
If we compare the original dump with that after the predicate was removed, |
|
we can see that all subjects that <em>only</em> had the predicate are removed. |
|
</p> |
|
|
|
<div class="compare"> |
|
<div> |
|
<h3>Parsed before delete</h3> |
|
<div style='font-family:arial; font-weight: bold; padding:0.5em; color: black; background-color:lightgrey;border:dashed 1px grey;'>Graph: </div> |
|
<div id='testDeleted/' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testDeleted/' style='text-decoration:none;color:blue'>testDeleted/</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:deleted</span> <span style='font-size:130%'>→</span> <span style='color:black'>"Because we say so"</span></div></div></div> |
|
<div id='testExtraRedirect' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testExtraRedirect' style='text-decoration:none;color:blue'>testExtraRedirect</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:redirectTemporary</span> <span style='font-size:130%'>→</span> <a href='https://localhost/redirect-extra.html' style='text-decoration:none;color:blue'>https://localhost/redirect-extra.html</a></div></div></div> |
|
<div id='testForget/' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testForget/' style='text-decoration:none;color:blue'>testForget/</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:forget</span> <span style='font-size:130%'>→</span> <span style='color:black'>"You have the right to be forgotten"</span></div> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>dc:title</span> <span style='font-size:130%'>→</span> <span style='color:black'>"Nested Test document"</span></div> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>rdfs:comment</span> <span style='font-size:130%'>→</span> <span style='color:black'>"Dummy file for testing metadata file in a parent directory"</span>, <span style='color:black'>"It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'"</span></div></div></div> |
|
<div id='testPermanentRedirect' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testPermanentRedirect' style='text-decoration:none;color:blue'>testPermanentRedirect</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:redirectPermanent</span> <span style='font-size:130%'>→</span> <a href='https://localhost/redirect-permanent.html' style='text-decoration:none;color:blue'>https://localhost/redirect-permanent.html</a></div></div></div> |
|
<div id='testTempRedirect' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testTempRedirect' style='text-decoration:none;color:blue'>testTempRedirect</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>lm:redirectTemporary</span> <span style='font-size:130%'>→</span> <a href='https://localhost/redirect-temporary.html' style='text-decoration:none;color:blue'>https://localhost/redirect-temporary.html</a></div></div></div> |
|
</div> |
|
<div> |
|
<h3>Parsed after delete</h3> |
|
<div style='font-family:arial; font-weight: bold; padding:0.5em; color: black; background-color:lightgrey;border:dashed 1px grey;'>Graph: </div> |
|
<div id='testForget/' style='font-family:arial; padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'> |
|
<div><a href='testForget/' style='text-decoration:none;color:blue'>testForget/</a> <span style='font-size: 0.8em'>(EasyRdf_Resource)</span></div> |
|
<div style='padding-left: 3em'> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>dc:title</span> <span style='font-size:130%'>→</span> <span style='color:black'>"Nested Test document"</span></div> |
|
<div><span style='font-size:130%'>→</span> <span style='text-decoration:none;color:green'>rdfs:comment</span> <span style='font-size:130%'>→</span> <span style='color:black'>"Dummy file for testing metadata file in a parent directory"</span>, <span style='color:black'>"It is also used to be directed to by the non-existent file 'redirectTemporary.ttl'"</span></div></div></div> |
|
</div> |
|
</div> |
|
<p> |
|
The resource that also had other predicates still exists, with those predicates intact. |
|
</p> |
|
</section> |
|
|
|
<section> |
|
<h2>Write result back to file</h2> |
|
<p> |
|
Once we have the graph to our liking, we can write it back to a file: |
|
</p> |
|
<div class="php"><code><span style="color: #000000"> |
|
<span style="color: #0000BB">$filesystem</span><span style="color: #007700">-></span><span style="color: #0000BB">update</span><span style="color: #007700">(</span><span style="color: #DD0000">'./output.ttl'</span><span style="color: #007700">, </span><span style="color: #0000BB">$graph</span><span style="color: #007700">-></span><span style="color: #0000BB">serialise</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">));</span> |
|
</span> |
|
</code></div> </section> |