[moriarty] r321 committed - fixed bug in snapshots.class.php not sending authentication credential...

1 view
Skip to first unread message

mori...@googlecode.com

unread,
Sep 9, 2011, 8:56:43 AM9/9/11
to moriarty...@googlegroups.com
Revision: 321
Author: k.j.w.a...@gmail.com
Date: Fri Sep 9 05:55:33 2011
Log: fixed bug in snapshots.class.php not sending authentication
credentials
http://code.google.com/p/moriarty/source/detail?r=321

Modified:
/trunk/curlhttpclient.class.php
/trunk/graph.class.php
/trunk/moriarty.inc.php
/trunk/snapshots.class.php
/trunk/tests/fakerequestfactory.class.php
/trunk/tests/graph.test.php
/trunk/tests/snapshots.test.php

=======================================
--- /trunk/curlhttpclient.class.php Tue Aug 2 01:13:40 2011
+++ /trunk/curlhttpclient.class.php Fri Sep 9 05:55:33 2011
@@ -35,7 +35,7 @@

curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT,TRUE);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
-
+ curl_setopt($curl_handle, CURLOPT_BINARYTRANSFER, TRUE);
/**
* @see http://bugs.typo3.org/view.php?id=4292
*/
=======================================
--- /trunk/graph.class.php Tue Aug 2 01:29:25 2011
+++ /trunk/graph.class.php Fri Sep 9 05:55:33 2011
@@ -3,7 +3,8 @@
require_once MORIARTY_ARC_DIR . "ARC2.php";
require_once MORIARTY_DIR . 'httprequest.class.php';
require_once MORIARTY_DIR . 'httprequestfactory.class.php';
-
+require_once MORIARTY_DIR . 'simplegraph.class.php';
+require_once MORIARTY_DIR . 'changeset.class.php';
/**
* The base class for graphs in a store.
*/
@@ -256,5 +257,95 @@
return false;
}
}
+
+ /**
+ * 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;
+ }
+ }
+
+
+
+
}
?>
=======================================
--- /trunk/moriarty.inc.php Wed Aug 3 01:11:46 2011
+++ /trunk/moriarty.inc.php Fri Sep 9 05:55:33 2011
@@ -143,6 +143,7 @@

define('OWL_ANNOTATIONPROPERTY', 'http://www.w3.org/2002/07/owl#AnnotationProperty');
define('OWL_DISJOINTWITH', 'http://www.w3.org/2002/07/owl#disjointWith');

define('OPEN_LASTCACHEDPAGE', 'http://open.vocab.org/terms/lastCachedPage');
+define('OPEN_JSON', 'http://open.vocab.org/terms/json');

define('OUTPUT_TYPE_RDF', 'rdf');
define('OUTPUT_TYPE_XML', 'xml');
=======================================
--- /trunk/snapshots.class.php Tue Aug 2 01:13:40 2011
+++ /trunk/snapshots.class.php Fri Sep 9 05:55:33 2011
@@ -6,24 +6,35 @@

class Snapshots{

- var $uri, $errors, $credentials;
-
- function __construct($uri, $credentials=false)
+ var $uri, $errors, $credentials, $request_factory;
+
+ function __construct($uri, $credentials=false, $request_factory=false)
{
$this->uri = $uri;
- $this->credentials = $credentials;
- }
- /**
- * get_snapshots
+ $this->credentials = $credentials;
+ $this->request_factory = $request_factory;
+ if (empty( $this->request_factory) ) {
+ $this->request_factory = new HttpRequestFactory();
+ }
+
+ }
+
+/**
+ * get_item_uris
*
* @return array
- * @author Keith Alexander
+ * @author Chris Clarke
**/
public function get_item_uris()
{
+ $request = $this->request_factory->make( 'GET', $this->uri,
$this->credentials );
+ $request->set_accept("application/rdf+xml");
+ $response = $request->execute();
+
$parser = ARC2::getRDFXMLParser();
- $parser->parse($this->uri);
+ $parser->parse('',$response->body);
$triples = $parser->getTriples();
+
$this->errors = $parser->getErrors();
$uris = array();
foreach($triples as $t)
@@ -32,7 +43,7 @@
}
return $uris;
}
-
+
/**
* get_errors
*
=======================================
--- /trunk/tests/fakerequestfactory.class.php Tue Aug 9 01:22:12 2011
+++ /trunk/tests/fakerequestfactory.class.php Fri Sep 9 05:55:33 2011
@@ -23,7 +23,6 @@

function make( $method, $uri, $credentials = null) {
$this->_received[] = $method . ' ' . $uri;
-
if ($request = $this->get_registered_request($method,$uri)) {
if ( $credentials != null) {
$request->set_auth( $credentials->get_auth());
=======================================
--- /trunk/tests/graph.test.php Tue Aug 2 01:29:25 2011
+++ /trunk/tests/graph.test.php Fri Sep 9 05:55:33 2011
@@ -374,6 +374,40 @@
$this->assertEquals(count($result), 1);
}

+ function test_mirror_from_uri(){
+
+ $url = "http://example.org/web-page";
+ $fake_request_factory = new FakeRequestFactory();
+ $webpage_response = new HttpResponse('200') ;
+ $webpage_response->body =
file_get_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'documents/after.ttl');
+ $fake_webpage_request = new FakeHttpRequest($webpage_response);
+ $fake_request_factory->register('GET',$url, $fake_webpage_request );
+
+
+ $contentbox_copy = new HttpResponse('200');
+ $contentbox_copy->body =
file_get_contents(dirname(__FILE__).DIRECTORY_SEPARATOR.'documents/before.ttl');
+ $fake_copy_request = new FakeHttpRequest($contentbox_copy);
+
$fake_request_factory->register('GET','http://api.talis.com/stores/example/meta?about='.urlencode($url).'&output=json' ,
$fake_copy_request);
+
+ $postDataResponse = new HttpResponse('201');
+ $fake_postData_request = new FakeHttpRequest($postDataResponse);
+
$fake_request_factory->register('POST', 'http://api.talis.com/stores/example/meta' ,
$fake_postData_request );
+
+ $graph = new Graph("http://api.talis.com/stores/example/meta", new
FakeCredentials(), $fake_request_factory);
+ $response = $graph->mirror_from_uri($url);
+ $this->assertTrue($fake_webpage_request->was_executed(), "The webpage
$url should be retrieved over HTTP");
+ $this->assertTrue($fake_copy_request->was_executed(), "");
+ $this->assertTrue($fake_postData_request->was_executed(), "The data
from $url (and its metadata) should be added to the store by POSTing a
document containing changesets to /meta");
+
+ $expected_response = array(
+ 'get_page' => $webpage_response,
+ 'get_copy' => $contentbox_copy,
+ 'update_data' => $postDataResponse,
+ 'success' => true,
+ );
+ $this->assertEquals($expected_response, $response,"");
+
+ }

}

=======================================
--- /trunk/tests/snapshots.test.php Tue Aug 26 01:06:05 2008
+++ /trunk/tests/snapshots.test.php Fri Sep 9 05:55:33 2011
@@ -2,12 +2,18 @@
require_once dirname(dirname(__FILE__)) .
DIRECTORY_SEPARATOR . 'constants.inc.php';
require_once MORIARTY_DIR . 'snapshots.class.php';
require_once MORIARTY_DIR . 'credentials.class.php';
+require_once MORIARTY_TEST_DIR . 'fakecredentials.class.php';

class SnapshotsTest extends PHPUnit_Framework_TestCase {

function test_get_item_uris(){
-
- $snapshots = new
Snapshots(dirname(__FILE__).DIRECTORY_SEPARATOR.'documents/snapshots.rdf');
+ $fake_request_factory = new FakeRequestFactory();
+ $fake_response = new HttpResponse();
+
$fake_response->body=file_get_contents(realpath(dirname(__FILE__)).'/documents/snapshots.rdf');
+ $fake_request = new FakeHttpRequest($fake_response);
+ $snapshotsUri = "http://example.com/store/snapshots";
+ $fake_request_factory->register('GET',$snapshotsUri, $fake_request );
+ $snapshots = new Snapshots($snapshotsUri, false, $fake_request_factory);
$expected =
array("http://api.talis.com/stores/schema-cache/snapshots/20071129173353.tar");
$actual = $snapshots->get_item_uris();
$this->assertEquals($expected, $actual);

Reply all
Reply to author
Forward
0 new messages