/**
  * 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;
     }
 }
示例#2
0
                } else {
                    // seats won by the national parties
                    $obs2 = $data_base_uri . "2009/national_parties/observation/" . $ee->getObservationId();
                    $graph->add_resource_triple($obs2, $graph->qname_to_uri('rdf:type'), $graph->qname_to_uri('eedef:ElectionResult'));
                    $graph->add_resource_triple($obs2, $graph->qname_to_uri('eedef:year'), 'http://reference.data.gov.uk/id/year/2009');
                    $graph->add_resource_triple($obs2, $graph->qname_to_uri('eedef:votingCountry'), $country_uri);
                    $graph->add_resource_triple($obs2, $graph->qname_to_uri('eedef:nationalParty'), $ee->getPartyURI($data_base_uri, $party));
                    $graph->add_literal_triple($obs2, $graph->qname_to_uri('eedef:seatsWon'), $count);
                    $graph->add_literal_triple($obs2, $graph->qname_to_uri('eedef:percentageOfSeatsWon'), $electionPercentageData[$k1][$k2][$party]);
                    $graph->add_resource_triple($obs2, $graph->qname_to_uri('qb:dataset'), $data_base_uri . '2009/political_groups/seats_won');
                    $graph->add_resource_triple($ee->getPartyURI($data_base_uri, $party), $graph->qname_to_uri('rdf:type'), $graph->qname_to_uri('eedef:NationalParty'));
                    $graph->add_resource_triple($ee->getPartyURI($data_base_uri, $party), $graph->qname_to_uri('rdfs:label'), html_entity_decode($ee->lookupPartyName($party), ENT_QUOTES));
                }
            }
        }
    }
}
echo $graph->to_turtle();
//echo $def->to_turtle();
file_put_contents(PROJECT_ROOT . "rdf/election-results.nt", $graph->to_ntriples());
// TODO
// link political groups to national parties. (columns in the original HTML)
// do we need slices?
//
/*
 * country specific scrapings
 *
 *
 */
// http://www.europarl.europa.eu/parliament/archive/elections2009/en/belgium_en_txt.html
//
 /**
  * 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;
     }
 }
 $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);
 } 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);