__construct($a, $caller);
}
function __init() {
parent::__init();
}
// Plugin code
//File formats (by extension) that should be parsed (and the parse mode (not used yet))
var $format_map = array("xml" => "xml", "rdf" => "xml", "n3" => "n3", "ttl" => "n3", "xhtml" => "rdfa", "html" => "rdfa", "xht2" => "rdfa");
//Start traversing root folder.
function sync_with_folder($root_folder) {
echo "Starting in $root_folder\n";
$this->load_dir($root_folder);
}
//Not done yet - 'o' should really be .first or similar in PHP...
function queryScalar($query) {
$rs = $this->query($query);
return $rs['result']['rows']['0']['o'];
}
//Get the stored modification time for a graph
function get_graph_modtime($graph) {
$graphcontext = $graph."#context";
$dquery = <<
SELECT ?o
FROM <$graphcontext>
WHERE {
<$graph> dcterms:modified ?o .
}
ENDS;
$result = $this->queryScalar($dquery);
if($result) {
return date("U", strtotime($result));
} else {
return -1;
}
}
//Update a graph from file if it has been modified since last time
function load_if_modified($graph, $path, $format) {
$file_modtime = filemtime($path);
//load last mod time for this graph from store
$loaded_modtime = $this->get_graph_modtime($graph);
//compare
if($file_modtime > $loaded_modtime) {
$filemstr = date("c", $file_modtime);
echo "Updating $graph\n";
//delete $graph from store
$this->query("DELETE FROM <$graph>");
//delete context from store
$this->query("DELETE FROM <$graph" . "#context" . ">");
//load $graph into store
$this->query("LOAD INTO <$graph>");
//save modified information in context
$this->query("INSERT INTO <$graph"."#context"."> { <$graph> \"$filemstr\" . }");
} else {
echo "Skipping $graph (not modified)\n";
}
}
//Get format to use when parsing RDF from file
function get_parsing_format($file) {
$path_info = pathinfo($file);
if(array_key_exists($path_info['extension'], $this->format_map)) {
return $this->format_map[strval($path_info['extension'])];
} else {
return null;
}
}
//Get the graph name for the file.
//Modify this if you want something else but the filename to identify the data form the file.
function get_graph_name($file) {
return "file://".$file;
}
//Traverse the folder
function load_dir($rootDir, $allData=array()) {
//do not follow these
$invisibleFileNames = array(".", "..");
$dirContent = scandir($rootDir);
foreach($dirContent as $key => $content) {
$path = $rootDir.'/'.$content;
if(!in_array($content, $invisibleFileNames)) {
if(is_file($path) && is_readable($path)) {
$format = $this->get_parsing_format($path);
$graph = $this->get_graph_name($path);
if($format != null) {
$this->load_if_modified($graph, $path, $format);
}
} elseif(is_dir($path) && is_readable($path)) {
$allData = $this->load_dir($path, $allData);
}
}
}
}
}
?>