function test_get_access_status() { $accessStatusUri = 'http://example.org/store/config/access-status'; $dummyGraph = new SimpleGraph(); $dummyGraph->add_resource_triple($accessStatusUri, 'http://schemas.talis.com/2006/bigfoot/configuration#accessMode', 'http://expected'); $response = new HttpResponse(200); $response->body = $dummyGraph->to_rdfxml(); $mockRequest = $this->getMock('HttpRequest', array('set_accept', 'execute'), array('GET', $accessStatusUri)); $mockRequest->expects($this->once())->method('set_accept')->with(MIME_RDFXML); $mockRequest->expects($this->once())->method('execute')->will($this->returnValue($response)); $mockRequestFactory = $this->getMock('HttpRequestFactory', array('make')); $mockRequestFactory->expects($this->once())->method('make')->will($this->returnValue($mockRequest)); $config = new Config("http://example.org/store/config", null, $mockRequestFactory); $actual = $config->get_access_status(); $this->assertEquals('http://expected', $actual, 'Access status should be correct'); }
/** * Create a new store on the platform. This is currently restricted to Talis administrators. * @param string name the name of the store * @param string template_uri the URI of the store template to use * @return HttpRequest */ function create_store($name, $template_uri) { if (empty($this->request_factory)) { $this->request_factory = new HttpRequestFactory(); } $uri = $this->uri; $mimetype = MIME_RDFXML; $request = $this->request_factory->make('POST', $uri, $this->credentials); $request->set_accept("*/*"); $request->set_content_type($mimetype); $sr = new SimpleGraph(); $sr->add_resource_triple('_:req', BF_STORETEMPLATE, $template_uri); $sr->add_literal_triple('_:req', BF_STOREREF, $name); $request->set_body($sr->to_rdfxml()); return $request->execute(); }
function test_to_html_renders_bnodes_as_anchors() { $g = new SimpleGraph(); $g->from_rdfxml($this->_single_triple); $g->add_resource_triple('http://example.org/subj', 'http://example.org/pred', '_:bn123'); $g->add_resource_triple('_:bn123', 'http://example.org/pred', 'http://example.org/obj'); $html = $g->to_html(); $this->assertContains('<a href="http://example.org/subj">subj</a>', $html, "html should contain links to bnode anchors"); $this->assertContains('<a href="#bn123">_:bn123</a>', $html, "html should contain links to bnode anchors"); $this->assertContains('<a id="bn123" href="#bn123">_:bn123</a>', $html, "html should contain anchors for bnodes"); }
public function testGetSequenceValues() { $graph = new SimpleGraph(); $graph->add_resource_triple('http://some/subject/1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_4', 'http://value/4'); $graph->add_resource_triple('http://some/subject/1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_2', 'http://value/2'); $graph->add_resource_triple('http://some/subject/1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_3', 'http://value/3'); $graph->add_resource_triple('http://some/subject/1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_5', 'http://value/5'); $graph->add_resource_triple('http://some/subject/1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_1', 'http://value/1'); $expectedArray = array('http://value/1', 'http://value/2', 'http://value/3', 'http://value/4', 'http://value/5'); $this->assertEquals($expectedArray, $graph->get_sequence_values('http://some/subject/1')); }
/** * mirror_from_url * * @return array of responses from http requests, and overall success status * @author Keith Alexander * * **/ function mirror_from_url($url, $rdf_content = false) { $return = array('get_page' => false, 'get_copy' => false, 'put_page' => false, 'update_data' => false, 'success' => false); if (empty($this->request_factory)) { $this->request_factory = new HttpRequestFactory(); } $last_cached_page_uri = $this->get_contentbox()->uri . '/mirrors/' . $url; if (!$rdf_content) { $web_page_request = $this->request_factory->make('GET', $url); $web_page_request->set_accept('application/rdf+xml;q=0.8,text/turtle;q=0.9,*/*;q=0.1'); $web_page_response = $web_page_request->execute(); $return['get_page'] = $web_page_response; $web_page_content = $web_page_response->body; } else { $web_page_content = $rdf_content; $return['rdf_content'] = $rdf_content; } if ($rdf_content or $web_page_response->is_success()) { $newGraph = new SimpleGraph(); $newGraph->add_rdf($web_page_content, $url); $newGraph->add_resource_triple($url, OPEN_LASTCACHEDPAGE, $last_cached_page_uri); $newGraph->skolemise_bnodes($last_cached_page_uri . '/'); $after = $newGraph->get_index(); # get previous copy if it exists $cached_page_request = $this->request_factory->make('GET', $last_cached_page_uri, $this->credentials); $cached_page_response = $cached_page_request->execute(); $return['get_copy'] = $cached_page_response; if ($cached_page_response->status_code == '200') { $before = json_decode($cached_page_response->body, true); } else { if ($cached_page_response->status_code == '404') { $before = false; } else { return $return; } } # build new changeset $Changeset = new ChangeSet(array('before' => $before, 'after' => $after, 'creatorName' => 'Store::mirror_from_url', 'changeReason' => 'mirroring from ' . $url)); if ($Changeset->has_changes()) { $return['update_data'] = $this->get_metabox()->apply_changeset($Changeset); if ($return['update_data']->is_success()) { $return['success'] = true; } else { if ($return['update_data']->status_code == '409') { # Conflict. some statements already removed. $before_graph = new SimpleGraph($before); $return['reapply_before_triples'] = $this->get_metabox()->submit_turtle($before_graph->to_turtle()); if ($return['reapply_before_triples']->status_code == '204') { #Succeeded. No content $return['update_data'] = $this->get_metabox()->apply_changeset($Changeset); $return['success'] = $return['update_data']->is_success(); } } else { return $return; } } $put_page_request = $this->request_factory->make('PUT', $last_cached_page_uri, $this->credentials); $put_page_request->set_body($newGraph->to_json()); $put_page_request->set_content_type('application/json'); $put_page_response = $put_page_request->execute(); $return['put_page'] = $put_page_response; $return['success'] = $put_page_response->is_success(); return $return; } else { $return['success'] = true; return $return; } } else { return $return; } }
/** * mirror_from_uri: * * @return array of responses from http requests, and overall success status * @author Keith Alexander * * **/ function mirror_from_uri($url, $rdf_content = false) { $return = array('get_page' => false, 'get_copy' => false, 'update_data' => false, 'success' => false); if (empty($this->request_factory)) { $this->request_factory = new HttpRequestFactory(); } if (!$rdf_content) { $web_page_request = $this->request_factory->make('GET', $url); $web_page_request->set_accept('application/rdf+xml;q=0.8,text/turtle;q=0.9,*/*;q=0.1'); $web_page_response = $web_page_request->execute(); $return['get_page'] = $web_page_response; $web_page_content = $web_page_response->body; } else { $web_page_content = $rdf_content; $return['rdf_content'] = $rdf_content; } if ($rdf_content or $web_page_response->is_success()) { $newGraph = new SimpleGraph(); $newGraph->add_rdf($web_page_content, $url); $jsonGraphContent = $newGraph->to_json(); $newGraph->add_resource_triple($url, OPEN_JSON, $jsonGraphContent); $newGraph->skolemise_bnodes(trim($url, '#') . '#'); $after = $newGraph->get_index(); # get previous copy if it exists $cached_page_response = $this->describe($url, 'json'); $return['get_copy'] = $cached_page_response; if ($cached_page_response->status_code == '200') { $description_index = json_decode($cached_page_response->body, true); if (isset($description_index[$url]) and isset($description_index[$url][OPEN_JSON])) { $before = json_decode($description_index[$url][OPEN_JSON][0]['value'], 1); } else { $before = false; } } else { if ($cached_page_response->status_code == '404') { $before = false; } else { return $return; } } # build new changeset $Changeset = new ChangeSet(array('before' => $before, 'after' => $after, 'creatorName' => 'Graph::mirror_from_uri', 'changeReason' => 'mirroring from ' . $url)); if ($Changeset->has_changes()) { $return['update_data'] = $this->apply_changeset($Changeset); if ($return['update_data']->is_success()) { $return['success'] = true; } else { if ($return['update_data']->status_code == '409') { # Conflict. some statements already removed. $before_graph = new SimpleGraph($before); $return['reapply_before_triples'] = $this->get_metabox()->submit_turtle($before_graph->to_turtle()); if ($return['reapply_before_triples']->status_code == '204') { #Succeeded. No content $return['update_data'] = $this->get_metabox()->apply_changeset($Changeset); $return['success'] = $return['update_data']->is_success(); } } else { return $return; } } return $return; } else { $return['success'] = true; return $return; } } else { return $return; } }
/** * @access private */ function make_job_request($jobtype, $time = null, $label = null) { $time = $time == null ? gmmktime() : $time; $formatted_time = gmdate("Y-m-d\\TH:i:s\\Z", $time); $label = $label == null ? 'Job submitted ' . $formatted_time : $label; $job = new SimpleGraph(); $job->add_resource_triple('_:job', BF_JOBTYPE, $jobtype); $job->add_resource_triple('_:job', RDF_TYPE, BF_JOBREQUEST); $job->add_literal_triple('_:job', BF_STARTTIME, $formatted_time); $job->add_literal_triple('_:job', RDFS_LABEL, $label); return $job; }
function test_match_compare_node_set_with_boolean() { $g = new SimpleGraph(); $g->set_namespace_mapping('ex', 'http://example.org/'); $g->add_resource_triple('http://example.org/subj', 'http://example.org/pred', 'http://example.org/obj'); $g->add_resource_triple('http://example.org/subj2', 'http://example.org/bogus', 'http://example.org/obj'); $gp = new GraphPath('*[ex:pred = true()]'); $this->assertPathSelects($gp, $g); $gp2 = new GraphPath('*[true() = ex:pred]'); $this->assertPathSelects($gp2, $g); }
/** * Get the changeset that would be applied by the update method. * * @return ChangeSet */ function get_update_changeset() { $store = new Store($this->_store_uri, $this->_credentials, $this->_request_factory); $query = $this->get_sparql(); $res = $this->get(); $cs = new SimpleGraph(); $diffs = $this->get_differences($res); $node_index = 0; foreach ($diffs as $s => $diff_info) { $removals = $diff_info['removals']; $additions = $diff_info['additions']; $cs_subj = '_:cs' . $node_index++; $cs->add_resource_triple($cs_subj, RDF_TYPE, CS_CHANGESET); $cs->add_resource_triple($cs_subj, CS_SUBJECTOFCHANGE, $s); $cs->add_literal_triple($cs_subj, CS_CHANGEREASON, "Update from DataTable"); $cs->add_literal_triple($cs_subj, CS_CREATEDDATE, gmdate(DATE_ATOM)); $cs->add_literal_triple($cs_subj, CS_CREATORNAME, "Moriarty DataTable"); if (count($removals) > 0) { foreach ($removals as $p => $p_list) { foreach ($p_list as $p_info) { $node = '_:r' . $node_index; $cs->add_resource_triple($cs_subj, CS_REMOVAL, $node); $cs->add_resource_triple($node, RDF_TYPE, RDF_STATEMENT); $cs->add_resource_triple($node, RDF_SUBJECT, $s); $cs->add_resource_triple($node, RDF_PREDICATE, $p); if ($p_info['type'] === 'literal') { $dt = array_key_exists('datatype', $p_info) ? $p_info['datatype'] : null; $lang = array_key_exists('lang', $p_info) ? $p_info['lang'] : null; $cs->add_literal_triple($node, RDF_OBJECT, $p_info['value'], $lang, $dt); } else { $cs->add_resource_triple($node, RDF_OBJECT, $p_info['value']); } $node_index++; } } } if (count($additions) > 0) { foreach ($additions as $p => $p_list) { foreach ($p_list as $p_info) { $node = '_:a' . $node_index; $cs->add_resource_triple($cs_subj, CS_ADDITION, $node); $cs->add_resource_triple($node, RDF_TYPE, RDF_STATEMENT); $cs->add_resource_triple($node, RDF_SUBJECT, $s); $cs->add_resource_triple($node, RDF_PREDICATE, $p); if ($p_info['type'] === 'literal') { $dt = array_key_exists('datatype', $p_info) ? $p_info['datatype'] : null; $lang = array_key_exists('lang', $p_info) ? $p_info['lang'] : null; $cs->add_literal_triple($node, RDF_OBJECT, $p_info['value'], $lang, $dt); } else { $cs->add_resource_triple($node, RDF_OBJECT, $p_info['value']); } $node_index++; } } } } return $cs; }
# build a graph $graph = new SimpleGraph(); //$def = new SimpleGraph() ; $graph->add_turtle(file_get_contents(PROJECT_ROOT . 'defs/eu-dataset-definition.ttl')); $graph->set_namespace_mapping('eedef', $schema_base_uri); $graph->set_namespace_mapping('ee', $data_base_uri); $graph->set_namespace_mapping('qb', 'http://purl.org/linked-data/cube#'); $graph->set_namespace_mapping('dct', 'http://purl.org/dc/terms/'); $electionPercentageData = $ee->getSeatsByPartyByStateTablePercentages(); // seats won counts foreach ($ee->getSeatsByPartyByStateTableCounts() as $k1 => $v) { foreach ($v as $k2 => $val) { if ($k2 == 'PoliticalGroupAbbr') { // add political groups $political_group = $data_base_uri . "political_group/" . $ee->uri_safe($v['PoliticalGroupAbbr']); $graph->add_resource_triple($political_group, $graph->qname_to_uri("rdf:type"), $schema_base_uri . "PoliticalGroup"); $graph->add_literal_triple($political_group, $graph->qname_to_uri("rdfs:label"), html_entity_decode($v['PoliticalGroupName'], ENT_QUOTES)); $graph->add_literal_triple($political_group, $graph->qname_to_uri("dct:identifier"), html_entity_decode($v['PoliticalGroupAbbr'], ENT_QUOTES)); } if (preg_match("/^[A-Z][A-Z]\$/", $k2)) { // add country $country_name = $ee->lookupCountry($k2); $country_code = $k2; $country_uri = $data_base_uri . "country/" . $ee->uri_safe($country_code); $graph->add_resource_triple($country_uri, $graph->qname_to_uri("rdf:type"), $schema_base_uri . "Country"); $graph->add_literal_triple($country_uri, $graph->qname_to_uri("rdfs:label"), $country_name); $graph->add_literal_triple($country_uri, $graph->qname_to_uri("dct:identifier"), $country_code); // add results per country foreach ($val as $party => $count) { if ($party == 'seatCount') { // total count for the political group
if ($response->is_success()) { if (array_key_exists('etag', $response->headers)) { $etag = $response->headers['etag']; } $g = new SimpleGraph(); $g->from_json($response->body); if (!$g->has_triples_about($resource_uri)) { send_not_found($uri, $template); } else { $g->remove_property_values($resource_uri, 'http://schemas.talis.com/2005/dir/schema#etag'); if ($uri != $doc_uri) { header("HTTP/1.1 303 See Other"); header("Location: " . $doc_uri); exit; } else { $g->add_resource_triple($doc_uri, RDF_TYPE, FOAF_DOCUMENT); $g->add_resource_triple($doc_uri, RDF_TYPE, 'http://purl.org/dc/dcmitype/Text'); $g->add_resource_triple($doc_uri, FOAF_PRIMARYTOPIC, $resource_uri); $g->add_literal_triple($doc_uri, 'http://purl.org/dc/terms/title', 'Linked Data for ' . $g->get_label($resource_uri, TRUE)); foreach ($media_types as $extension => $type_info) { $alt_uri = $resource_uri . '.' . $extension; $g->add_resource_triple($doc_uri, 'http://purl.org/dc/terms/hasFormat', $alt_uri); $g->add_resource_triple($alt_uri, 'http://purl.org/dc/terms/isFormatOf', $doc_uri); $g->add_resource_triple($alt_uri, RDF_TYPE, 'http://purl.org/dc/dcmitype/Text'); $g->add_resource_triple($alt_uri, RDF_TYPE, FOAF_DOCUMENT); $g->add_resource_triple($alt_uri, FOAF_PRIMARYTOPIC, $resource_uri); $g->add_literal_triple($alt_uri, 'http://purl.org/dc/terms/format', $type_info['type']); $g->add_literal_triple($alt_uri, 'http://purl.org/dc/terms/title', 'Linked Data in ' . $type_info['label'] . ' format for ' . $g->get_label($resource_uri, TRUE)); } if ($doc_type == 'rdf') { send_rdfxml('200 OK', $g->to_rdfxml(), $content_location, $etag);