public function importFile($pathname) { $doc = RDF::documentFromURL($pathname); $info = null; if (is_object($doc)) { $topic = ProgrammesOntology::instanceFromDocument($doc); if (is_object($topic)) { if ($topic instanceof POBrand) { $info = $this->importBrand($topic); $info['_recurse'] = array(); foreach ($topic->episodes as $ep) { $info['_recurse'][] = strval($ep); } foreach ($topic->series as $ep) { $info['_recurse'][] = strval($ep); } foreach ($topic->clips as $ep) { $info['_recurse'][] = strval($ep); } } else { if ($topic instanceof POSeries) { $info = $this->importSeries($topic); $info['_recurse'] = array(); foreach ($topic->episodes as $ep) { if ($ep->isA(ProgrammesOntology::po . 'episode')) { $ep = $ep->first(ProgrammesOntology::po . 'Episode'); } $url = strval($ep); if (!strlen($url)) { print_r($ep); die; } $info['_recurse'][] = $url; } foreach ($topic->clips as $ep) { $info['_recurse'][] = strval($ep); } } else { if ($topic instanceof POEpisode) { $info = $this->importEpisode($topic); } else { echo "Unsupported class " . get_class($topic) . "\n"; } } } } else { echo "No primary topic found.\n"; } } else { echo "No document found.\n"; } return $info; }
function testGetModelByRDQL3() { if (isset($model2)) { } unset($model2); $_SESSION['test'] = 'simple rdql query3'; $model = new MemModel(); $model->load('employees.rdf'); $query = ' SELECT ?x WHERE (?x,<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, <http://www.w3.org/2001/vcard-rdf/3.0#work>)'; $model2 = $model->getMemModelByRDQL($query); $this->assertEqual($model2->size(), 6); $model3 = $model2->find(null, RDF::TYPE(), new Resource('http://www.w3.org/2001/vcard-rdf/3.0#work')); $this->assertEqual(6, $model3->size()); }
/** * * @param STRING $elementName * @access private */ function getElementText($elementName) { $namespace = RDF_Util::guessNamespace($elementName); $localName = RDF_Util::guessName($elementName); if ($namespace == "") { return $localName; } $prefix = array_search($namespace, $this->m_namespaces); if ($prefix === false) { $errmsg = "Prefix for element '{$elementName}' cannot be found."; return RDF::raiseError(RDF_ERROR, null, null, $errmsg); } if ($prefix != RDF_NAMESPACE_PREFIX) { return $prefix . ':' . $localName; } else { return $this->rdf_qname_prefix . $localName; } }
$result[] = stringify($trip->subject) . ' <' . $trip->predicate . '> ' . stringify($trip->object) . ' . ' . (isset($trip->canonicalObjectValue) ? '#=> '. $trip->canonicalObjectValue : ''); } } } $result = null; if(get_magic_quotes_gpc()) { $_POST['rdf'] = @stripslashes($_POST['rdf']); } if(isset($_POST['rdf']) && strlen($_POST['rdf'])) { $result = array(); $triples = RDF::tripleSetFromXMLString($_POST['rdf']); $subjects = array(); $nodes = array(); dumpTriples($triples, 'Source:'); emit('Processing...'); $hasher = new GraphHasher($triples); $hasher->markInbound(); $hasher->updateHashes(); $hasher->replace(); $hasher->sort(); $hasher->dump('Result:'); } class GraphHasher { public $nodes = array();
/** * Add the given model to this Model_MDB. * This function monitors for SQL errors, and will commit if no errors have occured, * otherwise it will rollback. * If any statement of the model to be added to this model contains a blankNode * with an identifier already existing in this model, a new blankNode is generated. * * @param object Model $model * @throw PhpError * @access public */ function addModel(&$model) { if (!is_a($model, 'RDF_Model')) { $errmsg = 'Model expected, got unexpected: ' . (is_object($model) ? get_class($model) : gettype($model)); return RDF::raiseError(RDF_ERROR_UNEXPECTED, null, null, $errmsg); } $blankNodes_tmp = array(); if (is_a($model, 'RDF_Model_Memory')) { $this->dbConn->autoCommit(false); foreach ($model->triples as $statement) { $this->_addStatementFromAnotherModel($statement, $blankNodes_tmp); } $this->dbConn->commit(); $this->dbConn->autoCommit(true); } elseif (is_a($model, 'RDF_Model_MDB')) { $this->dbConn->autoCommit(false); $Model_Memory =& $model->getMemModel(); foreach ($Model_Memory->triples as $statement) { $this->_addStatementFromAnotherModel($statement, $blankNodes_tmp); } $this->dbConn->commit(); $this->dbConn->autoCommit(true); } }
private function _getAnnotations(&$m, $lang) { $ann = array(); $it = $m->findAsIterator(NULL, RDF::TYPE(), LOOMP::ANNOTATION()); while ($it->hasNext()) { $s = $it->next()->getSubject(); $uri = $this->_getPropValue($m, $s, LOOMP::REFERS_TO()); $label = $this->_getLiteralInLang($m->find($s, RDFS::LABEL(), NULL), $lang); $description = $this->_getLiteralInLang($m->find($s, RDFS::COMMENT(), NULL), $lang); $type = $this->_getPropValue($m, $s, LOOMP::ANNOTATION_TYPE()); $annDomain = $this->_getPropValue($m, $s, LOOMP::ANNOTATION_DOMAIN()); if ($annDomain == '') { $annDomain = RDF::RESOURCE()->getURI(); } $annRange = $this->_getPropValue($m, $s, LOOMP::ANNOTATION_RANGE()); $ann[] = new Annotation($uri, $label, $description, $type, $annDomain, $annRange); } return $ann; }
/** * Generates a new Model_Memory from a URI, a file or from memory. * If you want to parse an RDF document, pass the URI or location in the filesystem * of the RDF document. You can also pass RDF code direct to the function. If you pass * RDF code directly to the parser and there is no xml:base included, you should set * the base URI manually using the optional second parameter $rdfBaseURI. * Make sure that here are proper namespace declarations in your input document. * * @access public * @param string $base * @param boolean $rdfBaseURI * @return object Model_Memory */ function &generateModel($base, $rdfBaseURI = false) { // Check if $base is a URI or filename or a string containing RDF code. if (substr(ltrim($base), 0, 1) != '<') { // $base is URL or filename $this->model =& new RDF_Model_Memory($base); $input = @fopen($base, 'r'); if (!$input) { $errmsg = "RDF Parser: Could not open File: {$base}. Stopped parsing."; return RDF::raiseError(RDF_ERROR, null, null, $errmsg); } $this->rdf_parser_create(null); $this->rdf_set_base($base); $done = false; while (!$done) { $buf = fread($input, 512); $done = feof($input); if (!$this->rdf_parse($buf, feof($input))) { $err_code = xml_get_error_code($this->rdf_get_xml_parser()); $line = xml_get_current_line_number($this->rdf_get_xml_parser()); $errmsg = 'XML-parser-error ' . $err_code . ' in Line ' . $line . ' of input document.'; return RDF::raiseError(RDF_ERROR, null, null, $errmsg); } } /* close file. */ fclose($input); } else { // $base is RDF string $this->model =& new RDF_Model_Memory(); $this->rdf_parser_create(null); if ($rdfBaseURI !== false) { $this->rdf_set_base($rdfBaseURI); } else { $this->rdf_set_base(null); } if (!$this->rdf_parse($base, true)) { $err_code = xml_get_error_code($this->rdf_get_xml_parser()); $line = xml_get_current_line_number($this->rdf_get_xml_parser()); $errmsg = RDF_ERROR . '(class: parser; method: generateModel): XML-parser-error ' . $err_code . ' in Line ' . $line . ' of input document.'; return RDF::raiseError(RDF_RDQL_ERR, null, null, $errmsg); } } // base_uri could have changed while parsing $this->model->setBaseURI($this->rdf_parser['base_uri']); $this->rdf_parser_free(); return $this->model; }
protected function importFile($pathname) { $class = null; $base = $pathname; if (!strncmp($pathname, 'http:', 5)) { uses('rdf'); $doc = RDF::documentFromURL($pathname); if (is_object($doc)) { require_once dirname(__FILE__) . '/import-rdf.php'; $class = 'MediaImportRDF'; } else { echo "No RDF found at {$pathname}\n"; } } else { $info = pathinfo($pathname); $base = basename($pathname); if (!isset($info['extension'])) { $info['extension'] = null; } switch ($info['extension']) { case 'xml': require_once dirname(__FILE__) . '/import-xml.php'; $class = 'MediaImportXML'; break; case 'json': require_once dirname(__FILE__) . '/import-json.php'; $class = 'MediaImportJSON'; break; case 'rdf': require_once dirname(__FILE__) . '/import-rdf.php'; $class = 'MediaImportRDF'; break; default: echo $base . ": Error: Unsupported file type\n"; return false; } } if (!strlen($class) || !class_exists($class)) { echo $base . ": Error: Unable to import (internal error -- class {$class} does not exist)\n"; return false; } $inst = new $class(); $data = $inst->importFile($pathname); if (!$data) { echo $base . ": Error: Unable to import: import class failed\n"; return false; } $recurse = null; if (isset($data['_recurse']) && is_array($data['_recurse'])) { $recurse = $data['_recurse']; unset($data['_recurse']); } if (!($asset = Asset::objectForData($data))) { echo $base . ": Error: Unable to import: could not create asset object\n"; return false; } if (true !== ($r = $asset->verify())) { echo $base . ": Error: Unable to import: " . $r . "\n"; return false; } if (!isset($asset->kind)) { $asset->kind = null; } if (!isset($asset->uuid)) { if (isset($asset->curie)) { if (null !== ($uuid = $this->model->uuidForCurie($asset->curie))) { echo $base . ": Note: Matched CURIE [" . $asset->curie . "] to existing UUID " . $uuid . "\n"; $asset->uuid = $uuid; } } if ($asset instanceof Classification) { if (!isset($asset->uuid) && strlen($asset->slug)) { if ($obj = $this->model->locateObject($asset->slug, $asset->parent, $asset->kind)) { $asset->uuid = $obj->uuid; } } if (!isset($asset->uuid) && isset($asset->sameAs)) { foreach ($asset->sameAs as $sameAs) { if ($obj = $this->model->locateObject($sameAs, false, $asset->kind)) { $asset->uuid = $obj->uuid; break; } } } if (!isset($asset->slug) && !isset($asset->sameAs)) { echo $base . ": Refusing to import a " . $asset->kind . " with no useful information. Sorry.\n"; return false; } } } if (isset($asset->uuid)) { /* Check to see whether asset already exists with that UUID, * and if so whether it's the same kind. */ if ($old = $this->model->dataForUuid($asset->uuid)) { if (!isset($old['kind'])) { $old['kind'] = null; } if (strcmp($asset->kind, $old['kind'])) { echo $base . ": Warning: Updating asset " . $asset->uuid . " from being a '" . $old['kind'] . "' to being a '" . $asset->kind . "'\n"; } else { echo $base . ": Updating " . $asset->uuid . "\n"; } } } if (isset($asset->uuid)) { $created = false; } else { $created = true; } $asset->store(); if ($created) { echo $base . ": Created with UUID " . $asset->uuid . "\n"; } if (is_array($recurse) && count($recurse)) { return $recurse; } return true; }
/** * RDF_Error constructor. * * @param mixed $code RDF error code, or string with error message. * @param integer $mode what 'error mode' to operate in * @param integer $level what error level to use for * $mode & PEAR_ERROR_TRIGGER * @param smixed $debuginfo additional debug info, such as the last query */ function RDF_Error($code = RDF_ERROR, $mode = PEAR_ERROR_RETURN, $level = E_USER_NOTICE, $debuginfo = null) { if (is_int($code)) { $this->PEAR_Error('RDF Error: ' . RDF::errorMessage($code), $code, $mode, $level, $debuginfo); } else { $this->PEAR_Error("RDF Error: {$code}", RDF_ERROR, $mode, $level, $debuginfo); } }
/** * Set the object of the triple. * * @access public * @return object node */ function setObject($obj) { if (!(is_a($obj, 'RDF_Resource') or is_a($obj, 'RDF_Literal'))) { $errmsg = 'Resource or Literal expected as object, got unexpected: ' . (is_object($obj) ? get_class($obj) : gettype($obj)); return RDF::raiseError(RDF_ERROR_UNEXPECTED, null, null, $errmsg); } $this->obj = $obj; }