Permalink
Browse files

only load arc2 if not already loaded

  • Loading branch information...
2 parents 8ca5676 + b132b20 commit 0be554fc13eda87a34462d71948cd0fe5a6147f6 @cgutteridge committed Mar 31, 2012
View
@@ -2,3 +2,6 @@ language: php
php:
- 5.3
- 5.4
+before_script:
+ - git clone https://CloCkWeRX@github.com/semsol/arc2.git lib/ARC2
+script: phpunit --coverage-text
View
@@ -27,10 +27,17 @@
require_once 'ARC2.php';
}
+require_once 'Graphite/Retriever.php';
+
class Graphite
{
/**
+ * @var Graphite_Retriever $retriever
+ */
+ protected $retriever;
+
+ /**
* Create a new instance of Graphite. @see ns() for how to specify a namespace map and a list of pre-declared namespaces.
*/
public function __construct( $namespaces = array(), $uri = null )
@@ -90,11 +97,15 @@ public function __construct( $namespaces = array(), $uri = null )
$this->firstGraphURI = null;
if( $uri )
{
- $this->forceString( $uri );
- $this->load( $uri );
+ $this->load( (string)$uri );
}
$this->bnodeprefix = 0;
+ $this->setRetriever(new Graphite_Retriever($this));
+ }
+
+ public function setRetriever(Graphite_Retriever $retriever) {
+ $this->retriever = $retriever;
}
/**
@@ -129,6 +140,8 @@ public static function __set_state($data) // As of PHP 5.1.0
/**
* $dir should be a directory the webserver has permission to read and write to. Any RDF/XML documents which graphite downloads will be saved here. If a cache exists and is newer than $age seconds then load() will use the document in the cache directory in preference to doing an HTTP request. $age defaults to 24*60*60 - 24 hours. This including this function can seriously improve graphite performance! If you want to always load certain documents, load them before setting the cache.
+ *
+ * @todo Shift to Graphite_Retriever
*/
public function cacheDir( $dir, $age = 86400 ) # default age is 24 hours
{
@@ -205,8 +218,7 @@ function loaded( $uri )
*/
public function load( $uri, $aliases = array(), $map = array() )
{
- $this->forceString( $uri );
- $uri = $this->expandURI( $uri );
+ $uri = $this->expandURI( (string)$uri );
if( substr( $uri,0,5 ) == "data:" )
{
@@ -216,75 +228,13 @@ public function load( $uri, $aliases = array(), $map = array() )
}
else
{
- if( $this->loaded( $uri ) !== false ) { return $this->loaded( $uri ); }
- if( isset($this->cacheDir) )
- {
- $filename = $this->cacheDir."/".md5( $this->removeFragment( $uri ) );
-
- if( !file_exists( $filename ) || filemtime($filename)+$this->cacheAge < time() )
- {
- # decache if out of date, even if we fail to re cache.
- if( file_exists( $filename ) ) { unlink( $filename ); }
- $url = $uri;
- $ttl = 16;
- $mime = "";
- $old_user_agent = ini_get('user_agent');
- ini_set('user_agent', "PHP\r\nAccept: application/rdf+xml");
- while( $ttl > 0 )
- {
- $ttl--;
- # dirty hack to set the accept header without using curl
- if( !$rdf_fp = fopen($url, 'r') ) { break; }
- $meta_data = stream_get_meta_data($rdf_fp);
- $redir = 0;
- if( @!$meta_data['wrapper_data'] )
- {
- fclose($rdf_fp);
- continue;
- }
- foreach($meta_data['wrapper_data'] as $response)
- {
- if (substr(strtolower($response), 0, 10) == 'location: ')
- {
- $newurl = substr($response, 10);
- if( substr( $newurl, 0, 1 ) == "/" )
- {
- $parts = preg_split( "/\//",$url );
- $newurl = $parts[0]."//".$parts[2].$newurl;
- }
- $url = $newurl;
- $redir = 1;
- }
- if (substr(strtolower($response), 0, 14) == 'content-type: ')
- {
- $mime = preg_replace( "/\s*;.*$/","", substr($response, 14));
- }
- }
- if( !$redir ) { break; }
- }
- ini_set('user_agent', $old_user_agent);
- if( $ttl > 0 && $mime == "application/rdf+xml" && $rdf_fp )
- {
- # candidate for caching!
- if (!$cache_fp = fopen($filename, 'w'))
- {
- echo "Cannot write file ($filename)";
- exit;
- }
-
- while (!feof($rdf_fp)) {
- fwrite( $cache_fp, fread($rdf_fp, 8192) );
- }
- fclose($cache_fp);
- }
- @fclose($rdf_fp);
- }
+ if( $this->loaded( $uri ) !== false ) { return $this->loaded( $uri ); }
+ $data = $this->retriever->retrieve($uri);
- }
- if( isset( $filename ) && file_exists( $filename ) )
+ if(!empty($data))
{
$parser = ARC2::getRDFXMLParser();
- $parser->parse( $uri, file_get_contents($filename) );
+ $parser->parse( $uri, $data );
}
else
{
@@ -498,9 +448,8 @@ public function primaryTopic( $uri = null )
{
if( !$uri ) { $uri = $this->firstGraphURI; }
if( !$uri ) { return new Graphite_Null($this->g); }
- $this->forceString( $uri );
- return $this->resource( $uri )->get( "foaf:primaryTopic", "-foaf:isPrimaryTopicOf" );
+ return $this->resource( (string)$uri )->get( "foaf:primaryTopic", "-foaf:isPrimaryTopicOf" );
}
/**
@@ -533,8 +482,7 @@ public function ns( $short, $long )
*/
public function resource( $uri )
{
- $this->forceString( $uri );
- $uri = $this->expandURI( $uri );
+ $uri = $this->expandURI( (string)$uri );
return new Graphite_Resource( $this, $uri );
}
@@ -552,16 +500,15 @@ public function allOfType( $uri )
*/
public function shrinkURI( $uri )
{
- $this->forceString( $uri );
- if( $uri == "" ) { return "* This Document *"; }
+ if( (string)$uri == "" ) { return "* This Document *"; }
foreach( $this->ns as $short=>$long )
{
- if( substr( $uri, 0, strlen($long) ) == $long )
+ if( substr( (string)$uri, 0, strlen($long) ) == $long )
{
- return $short.":".substr( $uri, strlen($long ));
+ return $short.":".substr( (string)$uri, strlen($long ));
}
}
- return $uri;
+ return (string)$uri;
}
/**
@@ -570,16 +517,15 @@ public function shrinkURI( $uri )
*/
public function expandURI( $uri )
{
- $this->forceString( $uri );
- if( preg_match( '/:/', $uri ) )
+ if( preg_match( '/:/', (string)$uri ) )
{
- list( $ns, $tag ) = preg_split( "/:/", $uri, 2 );
+ list( $ns, $tag ) = preg_split( "/:/", (string)$uri, 2 );
if( isset($this->ns[$ns]) )
{
return $this->ns[$ns].$tag;
}
}
- return $uri;
+ return (string)$uri;
}
/**
@@ -642,10 +588,10 @@ public function dumpText( $options=array() )
return join("\n",$r );
}
+ /** @deprecated All graphite objects should implement __toString() */
public function forceString( &$uri )
{
- if( is_object( $uri ) ) { $uri = $uri->toString(); }
- return $uri;
+ return (string)$uri;
}
}
View
@@ -70,12 +70,12 @@ function _jsonify( $tree, $resource, &$json )
{
if( is_a( $value, "Graphite_Literal" ) )
{
- $json[$jsonkey][] = $value->toString();
+ $json[$jsonkey][] = (string)$value;
}
else
{
$subjson = array();
- $uri = $value->toString();
+ $uri = (string)$value;
if( substr( $uri,0,2 ) != "_:" ) { $subjson["_uri"] = $uri; }
if( isset( $tree[$dir][$code]) )
{
@@ -116,9 +116,9 @@ function _tograph( $tree, $resource, &$new_graph )
if( is_a( $value, "Graphite_Literal" ) )
{
$new_graph->addTriple(
- $resource->toString(),
- $relation->toString(),
- $value->toString(),
+ (string)$resource,
+ (string)$relation,
+ (string)$value,
$value->datatype(),
$value->language() );
}
@@ -135,16 +135,16 @@ function _tograph( $tree, $resource, &$new_graph )
if( $dir == "+" )
{
$new_graph->addTriple(
- $resource->toString(),
- $relation->toString(),
- $value->toString() );
+ (string)$resource,
+ (string)$relation,
+ (string)$value );
}
else
{
$new_graph->addTriple(
- $value->toString(),
- $relation->toString(),
- $resource->toString() );
+ (string)$value,
+ (string)$relation,
+ (string)$resource );
}
}
}
@@ -171,7 +171,7 @@ function _toSPARQL($tree, $suffix, $in_dangler = null, $sparqlprefix = "" )
$bits = array();
if( !isset( $in_dangler ) )
{
- $in_dangler = "<".$this->resource->toString().">";
+ $in_dangler = "<".(string)$this->resource.">";
}
$i = 0;
View
@@ -23,6 +23,8 @@ function dumpText() { return "Non existant Node"; }
function dump() { return "<div style='padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'>Non-existant Node</div>"; }
function nodeType() { return "#node"; }
function __toString() { return "[NULL]"; }
+
+ /** @deprecated Use __toString() or (string) instead */
function toString() { return $this->__toString(); }
function datatype() { return null; }
function language() { return null; }
View
@@ -4,8 +4,7 @@ class Graphite_Resource extends Graphite_Node
function __construct(Graphite $g, $uri )
{
$this->g = $g;
- $this->g->forceString( $uri );
- $this->uri = $uri;
+ $this->uri = (string)$uri;
}
public function get( /* List */ )
@@ -27,7 +26,7 @@ public function getLiteral( /* List */ )
$l = $this->all( $args );
if( sizeof( $l ) == 0 ) { return; }
- return $l[0]->toString();
+ return (string)$l[0];
}
# getString deprecated in favour of getLiteral
public function getString( /* List */ ) { return $this->getLiteral( func_get_args() ); }
@@ -62,7 +61,7 @@ public function allString( /* List */ )
$l = array();
foreach( $this->all( $args ) as $item )
{
- $l []= $item->toString();
+ $l []= (string)$item;
}
return new Graphite_ResourceList($this->g,$l);
}
@@ -232,6 +231,10 @@ public function load()
public function loadSameAsOrg( $prefix )
{
+ if (empty($this->uri)) {
+ return 0;
+ }
+
$sameasorg_uri = "http://sameas.org/rdf?uri=".urlencode( $this->uri );
$n = $this->g->load( $sameasorg_uri );
$n+= $this->loadSameAs( $prefix );
@@ -249,13 +252,12 @@ public function loadSameAs( $prefix=null )
$cnt = 0;
foreach( $this->all( "owl:sameAs" ) as $sameas )
{
- $this->g->forceString( $sameas );
- if( $prefix && substr( $sameas, 0, strlen($prefix )) != $prefix )
+ if( $prefix && substr( (string)$sameas, 0, strlen($prefix )) != $prefix )
{
continue;
}
- $cnt += $this->g->load( $sameas, array( $sameas=>$this->uri ) );
+ $cnt += $this->g->load( (string)$sameas, array( (string)$sameas=>$this->uri ) );
}
return $cnt;
}
@@ -384,7 +386,7 @@ public function dump( $options = array() )
{
$pattern = "<span style='font-size:130%%'>&rarr;</span> <a title='%s' href='%s' style='text-decoration:none;color: green'>%s</a> <span style='font-size:130%%'>&rarr;</span> %s";
}
- $this->g->forceString( $prop );
+ $prop = (string)$prop;
$plist []= sprintf( $pattern, $prop, $prop, $this->g->shrinkURI($prop), join( ", ",$olist ));
}
$r.= "\n<a name='".htmlentities($this->uri)."'></a><div style='text-align:left;font-family: arial;padding:0.5em; background-color:lightgrey;border:dashed 1px grey;margin-bottom:2px;'>\n";
@@ -416,6 +418,7 @@ public function dump( $options = array() )
function __toString() {
return !empty($this->uri) ? (string)$this->uri : "";
}
+
function dumpValue($options=array())
{
$label = $this->dumpValueText();
@@ -444,11 +447,9 @@ protected function parsePropertyArg( $arg )
{
if( is_a( $arg, "Graphite_InverseRelation" ) )
{
- $this->g->forceString( $arg );
- return array( "op", "$arg" );
+ return array( "op", (string)$arg );
}
- $this->g->forceString( $arg );
- return array( "sp", "$arg" );
+ return array( "sp", (string)$arg );
}
$set = "sp";
Oops, something went wrong.

0 comments on commit 0be554f

Please sign in to comment.