function parse($path, $data = '') { $this->state = 0; /* reader */ if (!$this->v('reader')) { ARC2::inc('Reader'); $this->reader =& new ARC2_Reader($this->a, $this); } $this->reader->setAcceptHeader('Accept: sparql-results+xml; q=0.9, */*; q=0.1'); $this->reader->activate($path, $data); $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; /* xml parser */ $this->initXMLParser(); /* parse */ $first = true; while ($d = $this->reader->readStream()) { if (!xml_parse($this->xml_parser, $d, false)) { $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); $line = xml_get_current_line_number($this->xml_parser); $this->tmp_error = 'XML error: "' . $error_str . '" at line ' . $line . ' (parsing as ' . $this->getEncoding() . ')'; return $this->addError($this->tmp_error); } } $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); xml_parser_free($this->xml_parser); $this->reader->closeStream(); return $this->done(); }
function runQuery($infos, $keep_bnode_ids = 0) { $this->infos = $infos; $con = $this->store->getDBCon(); ARC2::inc('StoreDumper'); $d = new ARC2_StoreDumper($this->a, $this->store); $d->dumpSPOG(); return 1; }
function parse($path, $data = '', $iso_fallback = false) { /* reader */ if (!$this->v('reader')) { ARC2::inc('Reader'); $this->reader = new ARC2_Reader($this->a, $this); } $this->reader->setAcceptHeader('Accept: application/rdf+xml; q=0.9, */*; q=0.1'); $this->reader->activate($path, $data); $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; /* xml parser */ $this->initXMLParser(); /* parse */ $first = true; while ($d = $this->reader->readStream()) { if (!$this->keep_time_limit) { @set_time_limit($this->v('time_limit', 60, $this->a)); } if ($iso_fallback && $first) { $d = '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . preg_replace('/^\\<\\?xml [^\\>]+\\?\\>\\s*/s', '', $d); $first = false; } preg_match_all('/<<<(.+?\\s*?)*?>>>/', $d, $res); if (isset($res)) { foreach ($res[0] as $templ) { $key = 'template_' . count($this->templates); $this->templates[$key] = str_replace('<<<', '', str_replace('>>>', '', $templ)); $d = str_replace($templ, $key, $d); } } if (!xml_parse($this->xml_parser, $d, false)) { $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); $line = xml_get_current_line_number($this->xml_parser); $this->tmp_error = 'XML error: "' . $error_str . '" at line ' . $line . ' (parsing as ' . $this->getEncoding() . ')'; if (!$iso_fallback && preg_match("/Invalid character/i", $error_str)) { xml_parser_free($this->xml_parser); unset($this->xml_parser); $this->reader->closeStream(); $this->__init(); $this->encoding = 'ISO-8859-1'; unset($this->xml_parser); unset($this->reader); return $this->parse($path, $data, true); } else { return $this->addError($this->tmp_error); } } } $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); xml_parser_free($this->xml_parser); $this->reader->closeStream(); unset($this->reader); return $this->done(); }
public function setNegotiators() { if ($handle = opendir('./')) { while (false !== ($file = readdir($handle))) { if (substr($file, 0, 12) == 'negotiators_' && $file != 'negotiators_DataNegotiatorPlugin' && substr($file, -1) != '~') { ARC2::inc(rtrim($file, '.php')); $this->negotiators[] = rtrim($file, '.php'); } } closedir($handle); } }
public function testInc() { $actual = ARC2::inc('Class'); $this->assertNotEquals(0, $actual); $actual = ARC2::inc('RDFParser'); $this->assertNotEquals(0, $actual); $actual = ARC2::inc('ARC2_RDFParser'); $this->assertNotEquals(0, $actual); $actual = ARC2::inc('Foo'); $this->assertEquals(0, $actual); $actual = ARC2::inc('Vendor_Foo'); $this->assertEquals(0, $actual); }
function runQuery($infos, $keep_bnode_ids = 0) { $this->infos = $infos; $con = $this->store->getDBCon(); /* insert */ if (!$this->v('pattern', array(), $this->infos['query'])) { return $this->store->insert($this->infos['query']['construct_triples'], $this->infos['query']['target_graph'], $keep_bnode_ids); } else { ARC2::inc('StoreConstructQueryHandler'); $h =& new ARC2_StoreConstructQueryHandler($this->a, $this->store); $sub_r = $h->runQuery($this->infos); return $this->store->insert($sub_r, $this->infos['query']['target_graph'], $keep_bnode_ids); } }
function parse($path, $data = '', $iso_fallback = false) { $this->nodes = array(); $this->node_count = 0; $this->level = 0; /* reader */ if (!$this->v('reader')) { ARC2::inc('Reader'); $this->reader = new ARC2_Reader($this->a, $this); } $this->reader->setAcceptHeader('Accept: application/xml; q=0.9, */*; q=0.1'); $this->reader->activate($path, $data); $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; $this->base = $this->x_base; $this->doc_url = $this->reader->base; /* xml parser */ $this->initXMLParser(); /* parse */ $first = true; while ($d = $this->reader->readStream(1)) { if ($iso_fallback && $first) { $d = '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . preg_replace('/^\\<\\?xml [^\\>]+\\?\\>\\s*/s', '', $d); } if (!xml_parse($this->xml_parser, $d, false)) { $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); $line = xml_get_current_line_number($this->xml_parser); if (!$iso_fallback && preg_match("/Invalid character/i", $error_str)) { xml_parser_free($this->xml_parser); unset($this->xml_parser); $this->reader->closeStream(); unset($this->reader); $this->__init(); $this->encoding = 'ISO-8859-1'; $this->initXMLParser(); return $this->parse($path, $data, true); } else { return $this->addError('XML error: "' . $error_str . '" at line ' . $line . ' (parsing as ' . $this->getEncoding() . ')'); } } $first = false; } $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); xml_parser_free($this->xml_parser); $this->reader->closeStream(); unset($this->reader); return $this->done(); }
function parse($path, $data = '') { /* reader */ if (!isset($this->reader)) { ARC2::inc('Reader'); $this->reader =& new ARC2_Reader($this->a, $this); } $this->reader->activate($path, $data); /* format detection */ $mappings = array('rdfxml' => 'RDFXML', 'turtle' => 'Turtle', 'sparqlxml' => 'SPOG', 'ntriples' => 'Turtle', 'html' => 'SemHTML', 'rss' => 'RSS', 'atom' => 'Atom', 'sgajson' => 'SGAJSON', 'cbjson' => 'CBJSON'); $format = $this->reader->getFormat(); if (!$format || !isset($mappings[$format])) { return $this->addError('No parser available for "' . $format . '".'); } /* format parser */ $suffix = $mappings[$format] . 'Parser'; ARC2::inc($suffix); $cls = 'ARC2_' . $suffix; $this->parser =& new $cls($this->a, $this); $this->parser->setReader($this->reader); return $this->parser->parse($path, $data); }
function parse($path, $data = '') { $this->state = 0; /* reader */ if (!$this->v('reader')) { ARC2::inc('Reader'); $this->reader =& new ARC2_Reader($this->a, $this); } $this->reader->setAcceptHeader('Accept: application/json; q=0.9, */*; q=0.1'); $this->reader->activate($path, $data); $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; /* parse */ $doc = ''; while ($d = $this->reader->readStream()) { $doc .= $d; } $this->reader->closeStream(); $doc = preg_replace('/^[^\\{]*(.*\\})[^\\}]*$/is', '\\1', $doc); $this->unparsed_code = $doc; list($this->struct, $rest) = $this->extractObject($doc); return $this->done(); }
function runQuery($infos, $keep_bnode_ids = 0) { $this->infos = $infos; $con = $this->store->getDBCon(); /* insert */ if (!$this->v('pattern', array(), $this->infos['query'])) { $triples = $this->infos['query']['construct_triples']; /* don't execute empty INSERTs as they trigger a LOAD on the graph URI */ if ($triples) { return $this->store->insert($triples, $this->infos['query']['target_graph'], $keep_bnode_ids); } else { return array('t_count' => 0, 'load_time' => 0); } } else { $keep_bnode_ids = 1; ARC2::inc('StoreConstructQueryHandler'); $h = new ARC2_StoreConstructQueryHandler($this->a, $this->store); $sub_r = $h->runQuery($this->infos); if ($sub_r) { return $this->store->insert($sub_r, $this->infos['query']['target_graph'], $keep_bnode_ids); } return array('t_count' => 0, 'load_time' => 0); } }
<?php /* homepage: http://arc.semsol.org/ license: http://arc.semsol.org/license class: ARC2 DC Extractor author: Benjamin Nowack version: 2010-11-16 */ ARC2::inc('RDFExtractor'); class ARC2_DcExtractor extends ARC2_RDFExtractor { function __construct($a, &$caller) { parent::__construct($a, $caller); } function __init() { parent::__init(); $this->a['ns']['dc'] = 'http://purl.org/dc/elements/1.1/'; } /* */ function extractRDF() { $t_vals = array(); $t = ''; foreach ($this->nodes as $n) { foreach (array('title', 'link', 'meta') as $tag) { if ($n['tag'] == $tag) { $m = 'extract' . ucfirst($tag);
function getComponent($name, $a = '') { ARC2::inc($name); $prefix = 'ARC2'; if (preg_match('/^([^\\_]+)\\_(.+)$/', $name, $m)) { $prefix = $m[1]; $name = $m[2]; } $cls = $prefix . '_' . $name; return new $cls($a, new stdClass()); }
<?php /* homepage: http://arc.semsol.org/ license: http://arc.semsol.org/license class: ARC2 SPARQL+ Parser (SPARQL + Aggregates + LOAD + INSERT + DELETE) author: Benjamin Nowack version: 2008-01-07 (Tweak: CONSTRUCT keyword is now optional) */ ARC2::inc('SPARQLParser'); class ARC2_SPARQLPlusParser extends ARC2_SPARQLParser { function __construct($a = '', &$caller) { parent::__construct($a, $caller); } function ARC2_SPARQLPlusParser($a = '', &$caller) { $this->__construct($a, $caller); } function __init() { parent::__init(); } /* +1 */ function xQuery($v) { list($r, $v) = $this->xPrologue($v); foreach (array('Select', 'Construct', 'Describe', 'Ask', 'Insert', 'Delete', 'Load') as $type) { $m = 'x' . $type . 'Query';
function handleQueryRequest($q) { if (preg_match('/^dump/i', $q)) { $infos = array('query' => array('type' => 'dump')); $this->is_dump = 1; } else { ARC2::inc('SPARQLPlusParser'); $p = new ARC2_SPARQLPlusParser($this->a, $this); $p->parse($q); $infos = $p->getQueryInfos(); } /* errors? */ if ($errors = $this->getErrors()) { $this->setHeader('http', 'HTTP/1.1 400 Bad Request'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = htmlspecialchars(join("\n", $errors)); return true; } $qt = $infos['query']['type']; /* wrong read key? */ if ($this->read_key && $this->p('key') != $this->read_key && preg_match('/^(select|ask|construct|describe|dump)$/', $qt)) { $this->setHeader('http', 'HTTP/1.1 401 Access denied'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Access denied. Missing or wrong "key" parameter.'; return true; } /* wrong write key? */ if ($this->write_key && $this->p('key') != $this->write_key && preg_match('/^(load|insert|delete|update)$/', $qt)) { $this->setHeader('http', 'HTTP/1.1 401 Access denied'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Access denied. Missing or wrong "key" parameter.'; return true; } /* non-allowed query type? */ if (!in_array($qt, $this->getFeatures())) { $this->setHeader('http', 'HTTP/1.1 401 Access denied'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Access denied for "' . $qt . '" query'; return true; } /* load/insert/delete via GET */ if (in_array($qt, array('load', 'insert', 'delete')) && isset($_GET['query'])) { $this->setHeader('http', 'HTTP/1.1 501 Not Implemented'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Query type "' . $qt . '" not supported via GET'; return true; } /* unsupported query type */ if (!in_array($qt, array('select', 'ask', 'describe', 'construct', 'load', 'insert', 'delete', 'dump'))) { $this->setHeader('http', 'HTTP/1.1 501 Not Implemented'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Unsupported query type "' . $qt . '"'; return true; } /* adjust infos */ $infos = $this->adjustQueryInfos($infos); $t1 = ARC2::mtime(); $r = array('result' => $this->runQuery($infos, $qt)); $t2 = ARC2::mtime(); $r['query_time'] = $t2 - $t1; /* query errors? */ if ($errors = $this->getErrors()) { $this->setHeader('http', 'HTTP/1.1 400 Bad Request'); $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Error: ' . join("\n", $errors); return true; } /* result */ $m = 'get' . ucfirst($qt) . 'ResultDoc'; if (method_exists($this, $m)) { $this->result = $this->{$m}($r); } else { $this->setHeader('content-type', 'Content-type: text/plain; charset=utf-8'); $this->result = 'Result serializer not available, dumping raw data:' . "\n" . print_r($r, 1); } }
<?php /** * ARC2 SPARQL Parser * * @author Benjamin Nowack * @license <http://arc.semsol.org/license> * @homepage <http://arc.semsol.org/> * @package ARC2 * @version 2010-11-16 */ ARC2::inc('TurtleParser'); class ARC2_SPARQLParser extends ARC2_TurtleParser { function __construct($a, &$caller) { parent::__construct($a, $caller); } function __init() { parent::__init(); $this->bnode_prefix = $this->v('bnode_prefix', 'arc' . substr(md5(uniqid(rand())), 0, 4) . 'b', $this->a); $this->bnode_id = 0; $this->bnode_pattern_index = array('patterns' => array(), 'bnodes' => array()); } /* */ function parse($q, $src = '', $iso_fallback = 'ignore') { $this->setDefaultPrefixes(); $this->base = $src ? $this->calcBase($src) : ARC2::getRequestURI(); $this->r = array('base' => '', 'vars' => array(), 'prefixes' => array());
function toHTML($v, $ns = '') { ARC2::inc('MicroRDFSerializer'); if (!$ns) { $ns = isset($this->a['ns']) ? $this->a['ns'] : array(); } $ser = new ARC2_MicroRDFSerializer(array_merge($this->a, array('ns' => $ns)), $this); return isset($v[0]) && isset($v[0]['s']) ? $ser->getSerializedTriples($v) : $ser->getSerializedIndex($v); }
<?php ARC2::inc('negotiators_DOMNodePlugin'); class negotiators_RSSItemPlugin extends negotiators_DOMNodePlugin { function __construct($a = '', &$caller) { parent::__construct($a, $caller); } function negotiators_DOMNodePlugin($a = '', &$caller) { $this->__construct($a, $caller); } function __init() { parent::__init(); $this->revProp = 'sioc:has_container'; $this->ns['xsd'] = "http://www.w3.org/2001/XMLSchema#"; } function preProcess() { date_default_timezone_set('America/New_York'); $linkNodes = $this->xpath->query('link', $this->source); $postPage = $this->trimSlash($linkNodes->item(0)->textContent); $postURI = $postPage . '#this'; $postRes = ARC2::getComponent('PMJ_ResourcePlusPlugin', array('ns' => $this->ns)); $postRes->setURI($postURI); $postRes->addPropValue($this->revProp, $this->revURI); $titleNodes = $this->xpath->query('title', $this->source); $postRes->addPropValue('dcterms:title', $titleNodes->item(0)->textContent); $createdNodes = $this->xpath->query('pubDate', $this->source);
function runQuery($q, $qt = '') { /* ep */ $ep = $this->v('remote_store_endpoint', 0, $this->a); if (!$ep) { return false; } /* prefixes */ $ns = isset($this->a['ns']) ? $this->a['ns'] : array(); $added_prefixes = array(); $prologue = ''; foreach ($ns as $k => $v) { $k = rtrim($k, ':'); if (in_array($k, $added_prefixes)) { continue; } if (preg_match('/(^|\\s)' . $k . ':/s', $q) && !preg_match('/PREFIX\\s+' . $k . '\\:/is', $q)) { $prologue .= "\n" . 'PREFIX ' . $k . ': <' . $v . '>'; } $added_prefixes[] = $k; } $q = $prologue . "\n" . $q; /* http verb */ $mthd = in_array($qt, array('load', 'insert', 'delete')) ? 'POST' : 'GET'; /* reader */ ARC2::inc('Reader'); $reader =& new ARC2_Reader($this->a, $this); $reader->setAcceptHeader('Accept: application/sparql-results+xml; q=0.9, application/rdf+xml; q=0.9, */*; q=0.1'); if ($mthd == 'GET') { $url = $ep; $url .= strpos($ep, '?') ? '&' : '?'; $url .= 'query=' . urlencode($q); if ($k = $this->v('store_read_key', '', $this->a)) { $url .= '&key=' . urlencode($k); } } else { $url = $ep; $reader->setHTTPMethod($mthd); $reader->setCustomHeaders("Content-Type: application/x-www-form-urlencoded"); $suffix = ($k = $this->v('store_write_key', '', $this->a)) ? '&key=' . rawurlencode($k) : ''; $reader->setMessageBody('query=' . rawurlencode($q) . $suffix); } $to = $this->v('remote_store_timeout', 0, $this->a); $reader->activate($url, '', 0, $to); $format = $reader->getFormat(); $resp = ''; while ($d = $reader->readStream()) { $resp .= $d; } $reader->closeStream(); $ers = $reader->getErrors(); unset($this->reader); if ($ers) { return array('errors' => $ers); } $mappings = array('rdfxml' => 'RDFXML', 'sparqlxml' => 'SPARQLXMLResult', 'turtle' => 'Turtle'); if (!$format || !isset($mappings[$format])) { return $resp; //return $this->addError('No parser available for "' . $format . '" SPARQL result'); } /* format parser */ $suffix = $mappings[$format] . 'Parser'; ARC2::inc($suffix); $cls = 'ARC2_' . $suffix; $parser =& new $cls($this->a, $this); $parser->parse($ep, $resp); /* ask|load|insert|delete */ if (in_array($qt, array('ask', 'load', 'insert', 'delete'))) { $bid = $parser->getBooleanInsertedDeleted(); switch ($qt) { case 'ask': return $bid['boolean']; default: return $bid; } } /* select */ if ($qt == 'select' && !method_exists($parser, 'getRows')) { return $resp; } if ($qt == 'select') { return array('rows' => $parser->getRows(), 'variables' => $parser->getVariables()); } /* any other */ return $parser->getSimpleIndex(0); }
function runQuery($q, $qt = '', $infos = '') { /* ep */ $ep = $this->v('remote_store_endpoint', 0, $this->a); if (!$ep) { return false; } /* prefixes */ $q = $this->completeQuery($q); /* custom handling */ $mthd = 'run' . $this->camelCase($qt) . 'Query'; if (method_exists($this, $mthd)) { return $this->{$mthd}($q, $infos); } /* http verb */ $mthd = in_array($qt, array('load', 'insert', 'delete')) ? 'POST' : 'GET'; /* reader */ ARC2::inc('Reader'); $reader =& new ARC2_Reader($this->a, $this); $reader->setAcceptHeader('Accept: application/sparql-results+xml; q=0.9, application/rdf+xml; q=0.9, */*; q=0.1'); if ($mthd == 'GET') { $url = $ep; $url .= strpos($ep, '?') ? '&' : '?'; $url .= 'query=' . urlencode($q); if ($k = $this->v('store_read_key', '', $this->a)) { $url .= '&key=' . urlencode($k); } } else { $url = $ep; $reader->setHTTPMethod($mthd); $reader->setCustomHeaders("Content-Type: application/x-www-form-urlencoded"); $suffix = ($k = $this->v('store_write_key', '', $this->a)) ? '&key=' . rawurlencode($k) : ''; $reader->setMessageBody('query=' . rawurlencode($q) . $suffix); } $to = $this->v('remote_store_timeout', 0, $this->a); $reader->activate($url, '', 0, $to); $format = $reader->getFormat(); $resp = ''; while ($d = $reader->readStream()) { $resp .= $d; } $reader->closeStream(); $ers = $reader->getErrors(); $this->a['reader_auth_infos'] = $reader->getAuthInfos(); unset($this->reader); if ($ers) { return array('errors' => $ers); } $mappings = array('rdfxml' => 'RDFXML', 'sparqlxml' => 'SPARQLXMLResult', 'turtle' => 'Turtle'); if (!$format || !isset($mappings[$format])) { return $resp; //return $this->addError('No parser available for "' . $format . '" SPARQL result'); } /* format parser */ $suffix = $mappings[$format] . 'Parser'; ARC2::inc($suffix); $cls = 'ARC2_' . $suffix; $parser =& new $cls($this->a, $this); $parser->parse($ep, $resp); /* ask|load|insert|delete */ if (in_array($qt, array('ask', 'load', 'insert', 'delete'))) { $bid = $parser->getBooleanInsertedDeleted(); if ($qt == 'ask') { $r = $bid['boolean']; } else { $r = $bid; } } elseif ($qt == 'select' && !method_exists($parser, 'getRows')) { $r = $resp; } elseif ($qt == 'select') { $r = array('rows' => $parser->getRows(), 'variables' => $parser->getVariables()); } else { $r = $parser->getSimpleIndex(0); } unset($parser); return $r; }
<?php /* homepage: http://arc.semsol.org/ license: http://arc.semsol.org/license class: ARC2 Store RSS(2) Loader author: Benjamin Nowack version: 2008-06-28 (Tweak: adjusted to normalized "literal" type) */ ARC2::inc('RSSParser'); class ARC2_StoreRSSLoader extends ARC2_RSSParser { function __construct($a = '', &$caller) { parent::__construct($a, $caller); } function __init() { parent::__init(); } /* */ function addT($t) { $this->caller->addT($t['s'], $t['p'], $t['o'], $t['s_type'], $t['o_type'], $t['o_datatype'], $t['o_lang']); $this->t_count++; } }
<?php /* homepage: http://arc.semsol.org/ license: http://arc.semsol.org/license class: ARC2 Store SPOG Loader author: Morten H�ybye Frederiksen / Benjamin Nowack version: 2008-07-02 */ ARC2::inc('SPOGParser'); class ARC2_StoreSPOGLoader extends ARC2_SPOGParser { function __construct($a = '', &$caller) { parent::__construct($a, $caller); } function __init() { parent::__init(); } /* */ function addT($s, $p, $o, $s_type, $o_type, $o_dt = '', $o_lang = '', $g) { if (!($s && $p && $o)) { return 0; } if (!$g) { $g = $this->caller->target_graph; } if ($this->caller->fixed_target_graph) {
function changeNamespaceURI($old_uri, $new_uri) { ARC2::inc('StoreHelper'); $c = new ARC2_StoreHelper($this->a, $this); return $c->changeNamespaceURI($old_uri, $new_uri); }
<?php /* homepage: http://arc.semsol.org/ license: http://arc.semsol.org/license class: ARC2 microformats Extractor author: Benjamin Nowack version: */ ARC2::inc('ARC2_PoshRdfExtractor'); class ARC2_MicroformatsExtractor extends ARC2_PoshRdfExtractor { function __construct($a = '', &$caller) { parent::__construct($a, $caller); } function ARC2_MicroformatsExtractor($a = '', &$caller) { $this->__construct($a, $caller); } function __init() { parent::__init(); $this->terms = $this->getTerms(); $this->ns_prefix = 'mf'; $this->a['ns']['mf'] = 'http://poshrdf.org/ns/mf#'; } /* */ function preProcessNode($n) {
function deleteConstructedGraph() { ARC2::inc('StoreConstructQueryHandler'); $h =& new ARC2_StoreConstructQueryHandler($this->a, $this->store); $sub_r = $h->runQuery($this->infos); $triples = ARC2::getTriplesFromIndex($sub_r); $tgs = $this->infos['query']['target_graphs']; $this->infos = array('query' => array('construct_triples' => $triples, 'target_graphs' => $tgs)); return $this->deleteTriples(); }
function parse($path, $data = '', $iso_fallback = false) { $this->setDefaultPrefixes(); /* reader */ if (!$this->v('reader')) { ARC2::inc('Reader'); $this->reader =& new ARC2_Reader($this->a, $this); } $this->reader->setAcceptHeader('Accept: application/x-turtle; q=0.9, */*; q=0.1'); $this->reader->activate($path, $data); $this->base = $this->v1('base', $this->reader->base, $this->a); $this->r = array('vars' => array()); /* parse */ $buffer = ''; $more_triples = array(); $sub_v = ''; $sub_v2 = ''; $loops = 0; $prologue_done = 0; while ($d = $this->reader->readStream(0)) { $buffer .= $d; $sub_v = $buffer; do { $proceed = 0; if (!$prologue_done) { $proceed = 1; if ((list($sub_r, $sub_v) = $this->xPrologue($sub_v)) && $sub_r) { $loops = 0; $sub_v .= $this->reader->readStream(0, 128); if ($this->x("\\@?(base|prefix)", $sub_v)) { /* more prologue to come, use outer loop */ $proceed = 0; } } else { $prologue_done = 1; } } if ($prologue_done && (list($sub_r, $sub_v, $more_triples, $sub_v2) = $this->xTriplesBlock($sub_v)) && is_array($sub_r)) { $proceed = 1; $loops = 0; foreach ($sub_r as $t) { $this->addT($t); } } } while ($proceed); $loops++; $buffer = $sub_v; if ($loops > 100) { /* most probably a parser or code bug, might also be a huge object value, though */ $this->addError('too many loops: ' . $loops); break; } } foreach ($more_triples as $t) { $this->addT($t); } $sub_v = count($more_triples) ? $sub_v2 : $sub_v; $buffer = $sub_v; $this->reader->closeStream(); return $this->done(); }
function runQuery($type, $q) { /* load/insert/delete must use POST */ if (in_array($type, array('load', 'insert', 'delete'))) { return $this->addError('Remote Endpoint: "' . $type . '" query must use POST, not supported (yet)'); } /* construct url */ $url = $this->getEndpointURL(); $url .= '?query=' . urlencode($q); /* reader */ ARC2::inc('Reader'); $reader =& new ARC2_Reader('', $this); $reader->setAcceptHeader('Accept: application/sparql-results+xml; q=0.9, application/rdf+xml; q=0.9, */*; q=0.1'); $reader->activate($url); if ($reader->getErrors()) { $this->errors = $reader->errors; return false; } /* check result format */ $format = $reader->getFormat(); $mappings = array('rdfxml' => 'RDFXML', 'xml' => 'XML'); if (!$format || !isset($mappings[$format])) { $reader->closeStream(); return $this->addError('No remote endpoint result handler available for "' . $format . '", returned from "' . $this->getEndpointURL() . '"'); } /* handle result */ $m = 'parse' . $mappings[$format] . 'ResultDoc'; if (method_exists($this, $m)) { $this->{$m}($url, $type, $reader); } else { return $this->addError('No remote endpoint result handler available for "' . $mappings[$format] . '"'); } return $this->result; }
function parse($path, $data = '', $iso_fallback = false) { $this->setDefaultPrefixes(); /* reader */ if (!$this->v('reader')) { ARC2::inc('Reader'); $this->reader = new ARC2_Reader($this->a, $this); } $this->reader->setAcceptHeader('Accept: application/x-turtle; q=0.9, */*; q=0.1'); $this->reader->activate($path, $data); $this->base = $this->v1('base', $this->reader->base, $this->a); $this->r = array('vars' => array()); /* parse */ $buffer = ''; $more_triples = array(); $sub_v = ''; $sub_v2 = ''; $loops = 0; $prologue_done = 0; while ($d = $this->reader->readStream(0, 8192)) { $buffer .= $d; $sub_v = $buffer; do { $proceed = 0; if (!$prologue_done) { $proceed = 1; if ((list($sub_r, $sub_v) = $this->xPrologue($sub_v)) && $sub_r) { $loops = 0; $sub_v .= $this->reader->readStream(0, 128); /* we might have missed the final DOT in the previous prologue loop */ if ($sub_r = $this->x('\\.', $sub_v)) { $sub_v = $sub_r[1]; } if ($this->x("\\@?(base|prefix)", $sub_v)) { /* more prologue to come, use outer loop */ $proceed = 0; } } else { $prologue_done = 1; } } if ($prologue_done && (list($sub_r, $sub_v, $more_triples, $sub_v2) = $this->xTriplesBlock($sub_v)) && is_array($sub_r)) { $proceed = 1; $loops = 0; foreach ($sub_r as $t) { $this->addT($t); } } } while ($proceed); $loops++; $buffer = $sub_v; if ($loops > $this->max_parsing_loops) { /* most probably a parser or code bug, might also be a huge object value, though */ $this->addError('too many loops: ' . $loops . '. Could not parse "' . substr($buffer, 0, 200) . '..."'); break; } } foreach ($more_triples as $t) { $this->addT($t); } $sub_v = count($more_triples) ? $sub_v2 : $sub_v; $buffer = $sub_v; $this->unparsed_code = $buffer; $this->reader->closeStream(); unset($this->reader); /* remove trailing comments */ while (preg_match('/^\\s*(\\#[^\\xd\\xa]*)(.*)$/si', $this->unparsed_code, $m)) { $this->unparsed_code = $m[2]; } if ($this->unparsed_code && !$this->getErrors()) { $rest = preg_replace('/[\\x0a|\\x0d]/i', ' ', substr($this->unparsed_code, 0, 30)); if (trim($rest)) { $this->addError('Could not parse "' . $rest . '"'); } } return $this->done(); }
<?php /* homepage: http://arc.semsol.org/ license: http://arc.semsol.org/license class: ARC2 RDF/JSON Serializer author: Benjamin Nowack version: 2008-07-01 (Fix: Proper jsonEscape method, thx to Keith Alexander) */ ARC2::inc('RDFSerializer'); class ARC2_RDFJSONSerializer extends ARC2_RDFSerializer { function __construct($a = '', &$caller) { parent::__construct($a, $caller); } function ARC2_RDFJSONSerializer($a = '', &$caller) { $this->__construct($a, $caller); } function __init() { parent::__init(); $this->content_header = 'application/json'; } /* */ function getTerm($v, $term = 's') { if (!is_array($v)) { if (preg_match('/^\\_\\:/', $v)) {
<?php /** * ARC2 Graph object * * @author Benjamin Nowack <*****@*****.**> * @license W3C Software License * @homepage <http://arc.semsol.org/> * @package ARC2 */ ARC2::inc('Class'); class ARC2_Graph extends ARC2_Class { protected $index; function __construct($a, &$caller) { parent::__construct($a, $caller); } function __init() { parent::__init(); $this->index = array(); } function setIndex($index) { $this->index = $index; return $this; } function getIndex() { return $this->index;
function toHTML($v, $ns = '', $label_store = '') { ARC2::inc('MicroRDFSerializer'); if (!$ns) { $ns = isset($this->a['ns']) ? $this->a['ns'] : array(); } $conf = array_merge($this->a, array('ns' => $ns)); if ($label_store) { $conf['label_store'] = $label_store; } $ser = new ARC2_MicroRDFSerializer($conf, $this); return isset($v[0]) && isset($v[0]['s']) ? $ser->getSerializedTriples($v) : $ser->getSerializedIndex($v); }