Commit 39ad4b00 authored by Krzysztof Kutt's avatar Krzysztof Kutt
Browse files

SPARQL UPDATE form by Michal Dobija

No related merge requests found
Showing with 80 additions and 0 deletions
+80 -0
<?php
#ini_set('display_errors',1);
#ini_set('display_startup_errors',1);
#error_reporting(E_ALL);
if(isset($_GET['query']) && !empty($_GET['query'])){
$result = parse_command($_GET['query']);
if($result){
echo "Query successfull";
}else{
echo "Query failed";
}
}else {
require_once './update_form.php';
}
function insert_clause($name, $value, $filename){
$fileContents = file_get_contents($filename);
$fileContents = $fileContents.'[['.$name.':'.$value.'|]]'.PHP_EOL;
file_put_contents($filename, $fileContents);
}
function remove_clause($name, $value, $filename){
$fileContents = file_get_contents($filename);
$fileContents = preg_replace("/\[\[".$name.":".$value."\|\]\]\n?/",'',$fileContents);
file_put_contents($filename, $fileContents);
}
//WARNING: This assumes this file is in /dokuwiki/sparql
function convert_to_path($pagename){
$output = dirname(__DIR__).'/data/pages'.'/'.preg_replace('/:/','/',$pagename).'.txt';
return $output;
}
function parse_command($command){
$result = preg_match("/delete data.*{[\n\s]*([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*\.\n}/is",$command,$matches);
if($result == 1 && check_can_edit($matches[1])){
remove_clause($matches[2],$matches[3],convert_to_path($matches[1]));
return TRUE;
}
$result = preg_match("/insert data.*{[\n\s]*([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*\.\n}/is",$command,$matches);
if($result == 1 && check_can_edit($matches[1])){
insert_clause($matches[2],$matches[3],convert_to_path($matches[1]));
return TRUE;
}
return FALSE;
}
//WARNING: This assumes this file is in /dokuwiki/sparql. Takes page in the "category:page" form
function check_can_edit($pagename){
$address = 'http://'.preg_replace("#sparql/test.php#",'doku.php?id='.$pagename.'&do=edit',$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$contents = file_get_contents($address);
if(preg_match("/.*this page is read only.*/i",$contents) || preg_match("/.*This page is currently locked.*/i",$contents)){
return FALSE;
}
return TRUE;
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Loki SPARQL Endpoint</title>
<link rel="stylesheet" type="text/css" href="fuseki.css" />
</head>
<body>
<h1>Loki SPARQL SELECT Endpoint</h1>
<div class="moreindent">
<form method="get">
<p> Currently the only accepted format is "INSERT DATA"/"DELETE DATA" (case insensitive), followed by "{". then the triple, separated with any whitespace and ending with a dot (there has to be whitespace between the triple and the dot), then "}". Newlines are ignored.
<p>
<textarea style="background-color: #F0F0F0;" name="query" id="query" cols="80" rows="20"></textarea>
<br/>
<input type="submit" value="Modify Data"/>
</p>
</form>
</div>
<hr/>
</body>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment