/** * 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; } }
<?php define('MORIARTY_ARC_DIR', 'arc/'); require 'inc.php'; require 'credentials.inc.php'; require_once 'moriarty/credentials.class.php'; function report($r) { var_dump($r->status_code); if ($r->is_success() === false) { var_dump($r); die; } } $void = new SimpleGraph(); $void->add_turtle(file_get_contents('void.ttl')); $void->add_literal_triple(WHOISWHO, DCT . 'modified', date('c'), false, XSDT . 'dateTime'); $graph = new Graph('http://api.talis.com/stores/euwhoiswho/meta', new Credentials(STORE_USER, STORE_PASS)); $graph->mirror_from_uri(WHOISWHO, $void->to_json()); $graph->submit_ntriples_in_batches_from_file('roles.nt', 500, 'report'); $graph->mirror_from_uri("http://institutions.publicdata.eu/", file_get_contents('institutions.publicdata.eu.ttl')); $graph->submit_ntriples_in_batches_from_file('all.nt', 500, 'report');
$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); } else { if ($doc_type == 'ttl') { send_turtle('200 OK', $g->to_turtle(), $content_location, $etag); } else { if ($doc_type == 'json') { send_json('200 OK', $g->to_json(), $content_location, $etag); } else { header("Content-Type: text/html; charset=UTF-8"); $title = $g->get_label($resource_uri, TRUE); $page_title = 'Linked Data for ' . $g->get_label($resource_uri, TRUE) . ' | ' . htmlspecialchars($this_host); $body .= '<p>A description of the resource identified by <a href="' . htmlspecialchars($resource_uri) . '">' . htmlspecialchars($resource_uri) . '</a></p>' . "\n"; $body .= ' <table class="linkeddata" summary="RDF description of the resource identified by ' . htmlspecialchars($resource_uri) . '. Property names are in the first column, values are in the second.">' . "\n"; $body .= ' <tbody>' . "\n"; $properties = $g->get_subject_properties($resource_uri, TRUE); $priority_properties = array_intersect($ordered_properties, $properties); $remaining_properties = array_diff($properties, $priority_properties); sort($remaining_properties); $properties = array_merge($priority_properties, $remaining_properties); $class = 'oddrow'; $index = $g->get_index(); foreach ($properties as $p) {