function test_number_of_resources_remains_constant_after_skolemise_bnodes()
 {
     $graph = new SimpleGraph(file_get_contents(dirname(__FILE__) . '/documents/ckan-ds.ttl'));
     $index = $graph->get_index();
     $before = count($graph->get_subjects());
     $graph->skolemise_bnodes('http://example.com/test/');
     $after = count($graph->get_subjects());
     $this->assertEquals($before, $after, "skolemise_bnodes shouldn't reduce the number of resources");
 }
 function test_from_turtle_parses_datatypes()
 {
     $g = new SimpleGraph();
     $g->from_turtle('<http://example.org/subj> <http://example.org/pred> "1390"^^<http://www.w3.org/2001/XMLSchema#gYear> .');
     $this->assertEquals(1, count($g->get_triples()));
     $index = $g->get_index();
     $this->assertEquals("1390", $index['http://example.org/subj']['http://example.org/pred'][0]['value']);
     $this->assertEquals("http://www.w3.org/2001/XMLSchema#gYear", $index['http://example.org/subj']['http://example.org/pred'][0]['datatype']);
 }
 /**
  * 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;
     }
 }
 function get_differences($query_results)
 {
     $res = $query_results;
     $g_before = new SimpleGraph();
     $g_after = new SimpleGraph();
     $row_count = $res->num_rows();
     $node_index = 0;
     $diffs = array();
     if ($row_count === 0) {
         if ($this->_subject) {
             $s = $this->_subject;
         } else {
             $s = "_:s";
         }
         $g_after = $this->get_data_as_graph($s);
         $index = $g_after->get_index();
         if (array_key_exists($s, $index)) {
             $additions = $index[$s];
         } else {
             $additions = array();
         }
         $diffs[$s] = array('additions' => $additions, 'removals' => array());
     } else {
         $subjects = array();
         for ($i = 0; $i < $row_count; $i++) {
             $row = $res->row_array($i);
             $rowdata = $res->rowdata($i);
             if (array_key_exists('_uri', $row)) {
                 $s = $row['_uri'];
             } else {
                 if ($this->_subject) {
                     $s = $this->_subject;
                 } else {
                     $s = "_:s";
                 }
             }
             $subjects[] = $s;
             foreach ($this->_data as $field => $field_info) {
                 if (array_key_exists($field, $row)) {
                     $p = $this->_rmap[$field];
                     if ($rowdata[$field]['type'] === 'literal') {
                         $g_before->add_literal_triple($s, $p, $row[$field], $rowdata[$field]['lang'], $rowdata[$field]['datatype']);
                     } else {
                         if ($rowdata[$field]['type'] === 'uri') {
                             $g_before->add_resource_triple($s, $p, $row[$field]);
                         } else {
                             if ($rowdata[$field]['type'] === 'bnode') {
                                 $g_before->add_resource_triple($s, $p, '_:' . $row[$field]);
                             }
                         }
                     }
                 }
             }
         }
         if (count($subjects) > 0) {
             $subjects = array_unique($subjects);
             foreach ($subjects as $s) {
                 $g_after->add_graph($this->get_data_as_graph($s));
             }
         }
         if ($g_after->is_empty()) {
             $additions = array();
             if ($g_before->is_empty()) {
                 $removals = array();
             } else {
                 $removals = $g_before->get_index();
             }
         } else {
             if ($g_before->is_empty()) {
                 $additions = $g_after->get_index();
                 $removals = array();
             } else {
                 $removals = SimpleGraph::diff($g_before->get_index(), $g_after->get_index());
                 $additions = SimpleGraph::diff($g_after->get_index(), $g_before->get_index());
             }
         }
         foreach ($subjects as $s) {
             $diff = array('additions' => array(), 'removals' => array());
             if (array_key_exists($s, $additions)) {
                 $diff['additions'] = $additions[$s];
             }
             if (array_key_exists($s, $removals)) {
                 $diff['removals'] = $removals[$s];
             }
             $diffs[$s] = $diff;
         }
     }
     return $diffs;
 }
 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) {
         $body .= '              <tr class="' . $class . '">' . "\n";
         $body .= '                <th valign="top" class="' . $class . '"><a href="' . htmlspecialchars($p) . '">' . htmlspecialchars($g->get_label($p, TRUE)) . '</a></th>' . "\n";
         $body .= '                <td valign="top" class="' . $class . '">';
         for ($i = 0; $i < count($index[$resource_uri][$p]); $i++) {
             if ($i > 0) {
                 $body .= ', ';
             }
             if ($index[$resource_uri][$p][$i]['type'] === 'literal') {
                 if (array_key_exists('datatype', $index[$resource_uri][$p][$i]) && $index[$resource_uri][$p][$i]['datatype'] == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral') {
                     $body .= $index[$resource_uri][$p][$i]['value'];
                 } else {
                     $body .= htmlspecialchars($index[$resource_uri][$p][$i]['value']);
                 }
             } else {