2 # (c)2010 Christopher Gutteridge / University of Southampton
3 # some extra features and bugfixes by Bart Nagel
7 # Requires ARC2 to be included.
8 # suggested call method:
9 # include_once("arc/ARC2.php");
10 # include_once("Graphite.php");
13 # EasyRDF - http://code.google.com/p/easyrdf/
14 # SimpleGraph - http://code.google.com/p/moriarty/wiki/SimpleGraph
16 # I've used function calls in common with EasyRDF, where it makes sense
17 # to do so. Easy RDF now uses our dump() style. We're one big happy linked
21 # hasRelationValue, hasRelation, filter, loadString,
24 # added sioc: (needs to be documented in the parent of graphite-docs.html),
25 # addTriples, addTriple, loadSPARQL
29 public function __construct( $namespaces = array(), $uri = null )
31 $this->t = array( "sp" => array(), "op"=>array() );
32 foreach( $namespaces as $short=>$long )
34 $this->ns( $short, $long );
36 $this->ns( "foaf", "http://xmlns.com/foaf/0.1/" );
37 $this->ns( "dc", "http://purl.org/dc/elements/1.1/" );
38 $this->ns( "dct", "http://purl.org/dc/terms/" );
39 $this->ns( "rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" );
40 $this->ns( "rdfs", "http://www.w3.org/2000/01/rdf-schema#" );
41 $this->ns( "owl", "http://www.w3.org/2002/07/owl#" );
42 $this->ns( "xsd", "http://www.w3.org/2001/XMLSchema#" );
43 $this->ns( "cc", "http://creativecommons.org/ns#" );
44 $this->ns( "bibo", "http://purl.org/ontology/bibo/" );
45 $this->ns( "skos", "http://www.w3.org/2004/02/skos/core#" );
46 $this->ns( "geo", "http://www.w3.org/2003/01/geo/wgs84_pos#" );
47 $this->ns( "sioc", "http://rdfs.org/sioc/ns#" );
48 $this->ns( "oo", "http://purl.org/openorg/" );
50 $this->loaded = array();
53 $this->firstGraphURI = null;
56 $this->forceString( $uri );
60 $this->bnodeprefix = 0;
63 public function cacheDir( $dir, $age = 86400 ) # default age is 24 hours
66 if( !file_exists( $dir ) ) { $error = "No such directory: $dir"; }
67 elseif( !is_dir( $dir ) ) { $error = "Not a directory: $dir"; }
68 elseif( !is_writable( $dir ) ) { $error = "Not writable: $dir"; }
70 print "<ul><li>Graphite cacheDir error: $error</li></ul>";
74 $this->cacheDir = $dir;
75 $this->cacheAge = $age;
79 public function setDebug( $boolean ) { $this->debug = $boolean; }
81 function removeFragment( $uri )
83 return preg_replace( "/#.*/", "", $uri );
86 function loaded( $uri )
88 if( !array_key_exists( $this->removeFragment( $uri ), $this->loaded ) )
92 return $this->loaded[$this->removeFragment( $uri )];
95 public function load( $uri, $aliases = array(), $map = array() )
97 $this->forceString( $uri );
98 $uri = $this->expandURI( $uri );
100 if( substr( $uri,0,5 ) == "data:" )
102 $data = urldecode( preg_replace( "/^data:[^,]*,/","", $uri ) );
103 $parser = ARC2::getTurtleParser();
104 $parser->parse( $uri, $data );
108 if( $this->loaded( $uri ) !== false ) { return $this->loaded( $uri ); }
109 if( isset($this->cacheDir) )
111 $filename = $this->cacheDir."/".md5( $this->removeFragment( $uri ) );
113 if( !file_exists( $filename ) || filemtime($filename)+$this->cacheAge < time() )
115 # decache if out of date, even if we fail to re cache.
116 if( file_exists( $filename ) ) { unlink( $filename ); }
120 $old_user_agent = ini_get('user_agent');
121 ini_set('user_agent', "PHP\r\nAccept: application/rdf+xml");
125 # dirty hack to set the accept header without using curl
126 if( !$rdf_fp = fopen($url, 'r') ) { break; }
127 $meta_data = stream_get_meta_data($rdf_fp);
129 if( @!$meta_data['wrapper_data'] )
134 foreach($meta_data['wrapper_data'] as $response)
136 if (substr(strtolower($response), 0, 10) == 'location: ')
138 $newurl = substr($response, 10);
139 if( substr( $newurl, 0, 1 ) == "/" )
141 $parts = preg_split( "/\//",$url );
142 $newurl = $parts[0]."//".$parts[2].$newurl;
147 if (substr(strtolower($response), 0, 14) == 'content-type: ')
149 $mime = preg_replace( "/\s*;.*$/","", substr($response, 14));
152 if( !$redir ) { break; }
154 ini_set('user_agent', $old_user_agent);
155 if( $ttl > 0 && $mime == "application/rdf+xml" && $rdf_fp )
157 # candidate for caching!
158 if (!$cache_fp = fopen($filename, 'w'))
160 echo "Cannot write file ($filename)";
164 while (!feof($rdf_fp)) {
165 fwrite( $cache_fp, fread($rdf_fp, 8192) );
173 if( isset( $filename ) && file_exists( $filename ) )
175 $parser = ARC2::getRDFXMLParser();
176 $parser->parse( $uri, file_get_contents($filename) );
180 $parser = ARC2::getRDFParser();
181 # Don't try to load the same URI twice!
183 if( !isset( $this->firstGraphURI ) )
185 $this->firstGraphURI = $uri;
187 $parser->parse( $uri );
191 $errors = $parser->getErrors();
192 $parser->resetErrors();
193 if( sizeof($errors) )
197 print "<h3>Error loading: $uri</h3>";
198 print "<ul><li>".join( "</li><li>",$errors)."</li></ul>";
202 $this->loaded[$this->removeFragment( $uri )] = $this->addTriples( $parser->getTriples(), $aliases, $map );
203 return $this->loaded( $uri );
206 function loadSPARQL( $endpoint, $query )
208 return $this->load( $endpoint."?query=".urlencode($query) );
211 function addTurtle( $base, $data )
213 $parser = ARC2::getTurtleParser();
214 $parser->parse( $base, $data );
215 $errors = $parser->getErrors();
216 $parser->resetErrors();
217 if( sizeof($errors) )
221 print "<h3>Error loading turtle string</h3>";
222 print "<ul><li>".join( "</li><li>",$errors)."</li></ul>";
226 return $this->addTriples( $parser->getTriples() );
228 function addRDFXML( $base, $data )
230 $parser = ARC2::getRDFXMLParser();
231 $parser->parse( $base, $data );
232 $errors = $parser->getErrors();
233 $parser->resetErrors();
234 if( sizeof($errors) )
238 print "<h3>Error loading RDFXML string</h3>";
239 print "<ul><li>".join( "</li><li>",$errors)."</li></ul>";
243 return $this->addTriples( $parser->getTriples() );
246 function addBnodePrefix( $uri ) {
247 return preg_replace( "/^_:/", "_:g" . $this->bnodeprefix . "-", $uri );
250 function addTriples( $triples, $aliases = array(), $map = array() )
252 $this->bnodeprefix++;
254 foreach( $triples as $t )
256 $t["s"] = $this->addBnodePrefix( $this->cleanURI($t["s"]) );
257 if( !isset($map[$t["s"]]) ) { continue; }
258 $t["p"] = $this->cleanURI($t["p"]);
259 if( $t["p"] != "http://www.w3.org/2002/07/owl#sameAs" ) { continue; }
260 $aliases[$this->addBnodePrefix( $t["o"] )] = $t["s"];
262 foreach( $triples as $t )
264 $datatype = $t["o_datatype"];
265 if( $t["o_type"] == "literal" && !$datatype ) { $datatype = "literal"; }
266 $this->addTriple( $t["s"], $t["p"], $t["o"], $datatype, $t["o_lang"], $aliases );
268 return sizeof( $triples );
271 function addTriple( $s,$p,$o,$o_datatype=null,$o_lang=null,$aliases=array() )
273 $s = $this->addBnodePrefix( $this->cleanURI( $s ) );
274 if( $o_datatype != "literal" )
276 $o = $this->addBnodePrefix( $this->cleanURI( $o ) );
279 if( isset($aliases[$s]) ) { $s=$aliases[$s]; }
280 if( isset($aliases[$p]) ) { $p=$aliases[$p]; }
281 if( isset($aliases[$o]) ) { $o=$aliases[$o]; }
283 if( isset( $o_datatype ) && $o_datatype )
285 if( $o_datatype == 'literal' ) { $o_datatype = null; }
286 $this->t["sp"][$s][$p][] = array(
293 $this->t["sp"][$s][$p][] = $o;
295 $this->t["op"][$o][$p][] = $s;
298 public function toArcTriples()
300 $arcTriples = array();
301 foreach( $this->allSubjects() as $s )
303 $arcTriples = array_merge( $arcTriples, $s->toArcTriples( false ) );
308 public function serialize( $type = "RDFXML" )
310 $serializer = ARC2::getSer( $type, array( "ns" => $this->ns ) );
311 return $serializer->getSerializedTriples( $this->toArcTriples() );
314 public function cleanURI( $uri )
316 if( !$uri ) { return; }
317 return preg_replace( '/^(https?:\/\/[^:\/]+):80\//','$1/', $uri );
320 public function primaryTopic( $uri = null )
322 if( !$uri ) { $uri = $this->firstGraphURI; }
323 if( !$uri ) { return new Graphite_Null($this->g); }
324 $this->forceString( $uri );
326 return $this->resource( $uri )->get( "foaf:primaryTopic", "-foaf:isPrimaryTopicOf" );
329 public function ns( $short, $long )
331 if( preg_match( '/^(urn|doi|http|https|ftp|mailto|xmlns|file|data)$/', $short ) )
333 print "<ul><li>Setting a namespace called '$short' is just asking for trouble. Abort.</li></ul>";
336 $this->ns[$short] = $long;
339 public function resource( $uri )
341 $this->forceString( $uri );
342 $uri = $this->expandURI( $uri );
343 return new Graphite_Resource( $this, $uri );
346 public function allOfType( $uri )
348 return $this->resource( $uri )->all("-rdf:type");
351 public function shrinkURI( $uri )
353 $this->forceString( $uri );
354 if( $uri == "" ) { return "* This Document *"; }
355 foreach( $this->ns as $short=>$long )
357 if( substr( $uri, 0, strlen($long) ) == $long )
359 return $short.":".substr( $uri, strlen($long ));
365 public function expandURI( $uri )
367 $this->forceString( $uri );
368 if( preg_match( '/:/', $uri ) )
370 list( $ns, $tag ) = preg_split( "/:/", $uri, 2 );
371 if( isset($this->ns[$ns]) )
373 return $this->ns[$ns].$tag;
380 public function allSubjects()
382 $r = new Graphite_ResourceList( $this );
383 foreach( $this->t["sp"] as $subject_uri=>$foo )
385 $r[] = new Graphite_Resource( $this, $subject_uri );
390 public function allObjects()
392 $r = new Graphite_ResourceList( $this );
393 foreach( $this->t["op"] as $object_uri=>$foo )
395 $r[] = new Graphite_Resource( $this, $object_uri );
400 public function dump($options=array())
403 foreach( $this->t["sp"] as $subject_uri=>$foo )
405 $subject = new Graphite_Resource( $this, $subject_uri );
406 $r []= $subject->dump($options);
411 public function forceString( &$uri )
413 if( is_object( $uri ) ) { $uri = $uri->toString(); }
420 function __construct( $g )
424 function isNull() { return false; }
425 function has() { return false; }
426 function get() { return new Graphite_Null($this->g); }
427 function type() { return new Graphite_Null($this->g); }
428 function label() { return "[UNKNOWN]"; }
429 function hasLabel() { return false; }
430 function all() { return new Graphite_ResourceList($this->g, array()); }
431 function types() { return $this->all(); }
432 function relations() { return $this->all(); }
433 function load() { return 0; }
434 function loadSameAs() { return 0; }
435 function loadSameAsOrg($prefix) { return 0; }
436 function loadDataGovUKBackLinks() { return 0; }
438 function dumpText() { return "Non existant Node"; }
439 function dump() { return "<div style='padding:0.5em; background-color:lightgrey;border:dashed 1px grey;'>Non-existant Node</div>"; }
440 function nodeType() { return "#node"; }
441 function __toString() { return "[NULL]"; }
442 function toString() { return $this->__toString(); }
444 protected function parsePropertyArg( $arg )
446 if( is_a( $arg, "Graphite_Resource" ) )
448 if( is_a( $arg, "Graphite_InverseRelation" ) )
450 $this->g->forceString( $arg );
451 return array( "op", "$arg" );
453 $this->g->forceString( $arg );
454 return array( "sp", "$arg" );
458 if( substr( $arg,0,1) == "-" )
461 $arg = substr($arg,1);
463 return array( $set, $this->g->expandURI( "$arg" ) );
466 class Graphite_Null extends Graphite_Node
468 function nodeType() { return "#null"; }
469 function isNull() { return true; }
471 class Graphite_Literal extends Graphite_Node
473 function __construct( $g, $triple )
476 $this->triple = $triple;
477 $this->v = $triple["v"];
480 function __toString() { return $this->triple["v"]; }
482 function dumpValueText()
485 if( isset($this->triple["l"]) && $this->triple["l"])
487 $r.="@".$this->triple["l"];
489 if( isset($this->triple["d"]) )
491 $r.="^^".$this->g->shrinkURI($this->triple["d"]);
496 function dumpValueHTML()
498 $v = htmlspecialchars( $this->triple["v"],ENT_COMPAT,"UTF-8" );
500 $v = preg_replace( "/\t/", "<span class='special_char' style='font-size:70%'>[tab]</span>", $v );
501 $v = preg_replace( "/\n/", "<span class='special_char' style='font-size:70%'>[nl]</span><br />", $v );
502 $v = preg_replace( "/\r/", "<span class='special_char' style='font-size:70%'>[cr]</span>", $v );
503 $v = preg_replace( "/ +/e", "\"<span class='special_char' style='font-size:70%'>\".str_repeat(\"␣\",strlen(\"$0\")).\"</span>\"", $v );
506 if( isset($this->triple["l"]) && $this->triple["l"])
508 $r.="@".$this->triple["l"];
510 if( isset($this->triple["d"]) )
512 $r.="^^".$this->g->shrinkURI($this->triple["d"]);
519 if( isset($this->triple["d"]) )
521 return $this->triple["d"];
528 return "<span style='color:blue'>".$this->dumpValueHTML()."</span>";
532 class Graphite_Resource extends Graphite_Node
534 function __construct( $g, $uri )
537 $this->g->forceString( $uri );
541 public function get( /* List */ )
543 $args = func_get_args();
544 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
545 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
547 $l = $this->all( $args );
548 if( sizeof( $l ) == 0 ) { return new Graphite_Null($this->g); }
552 public function getString( /* List */ )
554 $args = func_get_args();
555 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
556 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
558 $l = $this->all( $args );
559 if( sizeof( $l ) == 0 ) { return; }
560 return $l[0]->toString();
563 public function allString( /* List */ )
565 $args = func_get_args();
566 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
567 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
570 foreach( $this->all( $args ) as $item )
572 $l []= $item->toString();
574 return new Graphite_ResourceList($this->g,$l);
577 public function has( /* List */ )
579 $args = func_get_args();
580 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
581 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
583 foreach( $args as $arg )
585 list( $set, $relation_uri ) = $this->parsePropertyArg( $arg );
586 if( isset($this->g->t[$set][$this->uri])
587 && isset($this->g->t[$set][$this->uri][$relation_uri]) )
595 public function all( /* List */ )
597 $args = func_get_args();
598 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
599 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
602 foreach( $args as $arg )
604 list( $set, $relation_uri ) = $this->parsePropertyArg( $arg );
605 if( !isset($this->g->t[$set][$this->uri])
606 || !isset($this->g->t[$set][$this->uri][$relation_uri]) )
611 foreach( $this->g->t[$set][$this->uri][$relation_uri] as $v )
615 $l []= new Graphite_Literal( $this->g, $v );
617 else if( !isset($done[$v]) )
619 $l []= new Graphite_Resource( $this->g, $v );
624 return new Graphite_ResourceList($this->g,$l);
627 public function relations()
630 if( isset( $this->g->t["sp"][$this->uri] ) )
632 foreach( array_keys( $this->g->t["sp"][$this->uri] ) as $pred )
634 $r []= new Graphite_Relation( $this->g, $pred );
637 if( isset( $this->g->t["op"][$this->uri] ) )
639 foreach( array_keys( $this->g->t["op"][$this->uri] ) as $pred )
641 $r []= new Graphite_InverseRelation( $this->g, $pred );
645 return new Graphite_ResourceList($this->g,$r);
648 public function toArcTriples( $bnodes = true )
650 $arcTriples = array();
651 $bnodes_to_add = array();
655 if( preg_match( '/^_:/', $s ) )
660 foreach( $this->g->t["sp"][$s] as $p => $os )
662 $p = $this->g->expandURI( $p );
664 if( preg_match( '/^_:/', $p ) )
676 if( isset( $o["l"] ) && $o["l"] )
680 if( isset( $o["d"] ) )
682 $o_datatype = $this->g->expandURI( $o["d"] );
688 $o = $this->g->expandURI( $o );
690 if( preg_match( '/^_:/', $o ) )
693 $bnodes_to_add[] = $o;
704 $triple["o_datatype"] = $o_datatype;
705 $triple["o_lang"] = $o_lang;
707 $arcTriples[] = $triple;
713 foreach( array_unique( $bnodes_to_add ) as $bnode )
715 $arcTriples = array_merge( $arcTriples, $this->g->resource( $bnode )->toArcTriples() );
721 public function serialize( $type = "RDFXML" )
723 $serializer = ARC2::getSer( $type, array( "ns" => $this->g->ns ) );
724 return $serializer->getSerializedTriples( $this->toArcTriples() );
727 public function load()
729 return $this->g->load( $this->uri );
732 public function loadSameAsOrg( $prefix )
734 $sameasorg_uri = "http://sameas.org/rdf?uri=".urlencode( $this->uri );
735 $n = $this->g->load( $sameasorg_uri );
736 $n+= $this->loadSameAs( $prefix );
740 function loadDataGovUKBackLinks()
742 $backurl = "http://backlinks.psi.enakting.org/resource/rdf/".$this->uri;
743 return $this->g->load( $backurl, array(), array( $this->uri=>1 ) );
746 public function loadSameAs( $prefix=null )
749 foreach( $this->all( "owl:sameAs" ) as $sameas )
751 $this->g->forceString( $sameas );
752 if( $prefix && substr( $sameas, 0, strlen($prefix )) != $prefix )
757 $cnt += $this->g->load( $sameas, array( $sameas=>$this->uri ) );
762 public function type()
764 return $this->get( "rdf:type" );
767 public function types()
769 return $this->all( "rdf:type" );
772 public function isType( /* List */ )
774 $args = func_get_args();
775 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
776 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
778 foreach( $this->allString( 'rdf:type' ) as $type )
780 foreach( $args as $arg )
782 $uri = $this->g->expandURI( $arg );
783 if( $uri == $type ) { return true; }
790 public function hasLabel()
792 return $this->has( "skos:prefLabel", "rdfs:label", "foaf:name", "dct:title", "dc:title", "sioc:name" );
794 public function label()
796 return $this->get( "skos:prefLabel", "rdfs:label", "foaf:name", "dct:title", "dc:title", "sioc:name" )->toString();
799 public function link()
801 return "<a title='".$this->uri."' href='".$this->uri."'>".$this->uri."</a>";
804 public function dumpText()
808 foreach( $this->relations() as $prop )
811 foreach( $this->all( $prop ) as $obj )
813 $olist []= $obj->dumpValueText();
815 $plist []= $this->g->shrinkURI($prop)." ".join( ", ",$olist );
817 return $this->g->shrinkURI($this->uri)."\n ".join( ";\n ", $plist )." .\n";
820 public function dump( $options = array() )
824 foreach( $this->relations() as $prop )
827 $all = $this->all( $prop );
828 foreach( $all as $obj )
830 $olist []= $obj->dumpValue($options);
832 if( is_a( $prop, "Graphite_InverseRelation" ) )
834 $pattern = "<span style='font-size:130%%'>←</span> is <a title='%s' href='%s' style='text-decoration:none;color: green'>%s</a> of <span style='font-size:130%%'>←</span> %s";
838 $pattern = "<span style='font-size:130%%'>→</span> <a title='%s' href='%s' style='text-decoration:none;color: green'>%s</a> <span style='font-size:130%%'>→</span> %s";
840 $this->g->forceString( $prop );
841 $plist []= sprintf( $pattern, $prop, $prop, $this->g->shrinkURI($prop), join( ", ",$olist ));
843 $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";
844 if( isset($options["label"] ) )
846 $label = $this->label();
847 if( $label == "[NULL]" ) { $label = ""; } else { $label = "<strong>$label</strong>"; }
848 if( $this->has( "rdf:type" ) )
850 if( $this->get( "rdf:type" )->hasLabel() )
852 $typename = $this->get( "rdf:type" )->label();
856 $bits = preg_split( "/[\/#]/", @$this->get( "rdf:type" )->uri );
857 $typename = array_pop( $bits );
858 $typename = preg_replace( "/([a-z])([A-Z])/","$1 $2",$typename );
860 $r .= preg_replace( "/>a ([AEIOU])/i", ">an $1", "<div style='float:right'>a $typename</div>" );
862 if( $label != "" ) { $r.="<div>$label</div>"; }
864 $r.= " <!-- DUMP:".$this->uri." -->\n <div><a title='".$this->uri."' href='".$this->uri."' style='text-decoration:none'>".$this->g->shrinkURI($this->uri)."</a></div>\n";
865 $r.=" <div style='padding-left: 3em'>\n <div>".join( "</div>\n <div>", $plist )."</div></div><div style='clear:both;height:1px; overflow:hidden'> </div></div>";
869 function __toString() { return $this->uri; }
870 function dumpValue($options=array())
872 $label = $this->dumpValueText();
873 if( $this->hasLabel() && @$options["labeluris"] )
875 $label = $this->label();
878 if( @$options["internallinks"] )
880 $href = "#".htmlentities($this->uri);
882 return "<a href='".$href."' title='".$this->uri."' style='text-decoration:none;color:red'>".$label."</a>";
884 function dumpValueText() { return $this->g->shrinkURI( $this->uri ); }
885 function nodeType() { return "#resource"; }
888 class Graphite_Relation extends Graphite_Resource
890 function nodeType() { return "#relation"; }
893 class Graphite_InverseRelation extends Graphite_Relation
895 function nodeType() { return "#inverseRelation"; }
897 class Graphite_ResourceList extends ArrayIterator
899 function __construct( $g, $a=array() )
903 if( $a instanceof Graphite_ResourceList )
905 print "<li>Graphite warning: passing a Graphite_ResourceList as the array passed to new Graphite_ResourceList will make weird stuff happen.</li>";
907 parent::__construct( $this->arr );
911 function join( $str )
915 foreach( $this as $resource )
917 if( !$first ) { $l []= $str; }
918 $this->g->forceString( $resource );
922 return join( "", $l );
928 foreach( $this as $resource )
930 $l [] = $resource->dump();
932 return join( "", $l );
935 public function duplicate()
938 foreach( $this as $resource ) { $l []= $resource; }
939 return new Graphite_ResourceList($this->g,$l);
942 public function sort( /* List */ )
944 $args = func_get_args();
945 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
946 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
948 global $graphite_sort_args;
949 $graphite_sort_args = array();
950 foreach( $args as $arg )
952 if( $arg instanceof Graphite_Resource ) { $arg = $arg->toString(); }
953 $graphite_sort_args [] = $arg;
956 $new_list = $this->duplicate();
957 usort($new_list->arr, "graphite_sort_list_cmp" );
962 public function uasort( $cmp )
964 usort($this->arr, $cmp );
967 public function get( /* List */ )
969 $args = func_get_args();
970 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
971 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
973 foreach( $this as $resource )
975 $l [] = $resource->get( $args );
977 return new Graphite_ResourceList($this->g,$l);
980 public function getString( /* List */ )
982 $args = func_get_args();
983 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
984 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
986 foreach( $this as $resource )
988 $l [] = $resource->getString( $args );
990 return new Graphite_ResourceList($this->g,$l);
993 public function label()
996 foreach( $this as $resource )
998 $l [] = $resource->label();
1000 return new Graphite_ResourceList($this->g,$l);
1003 public function load()
1006 foreach( $this as $resource )
1008 $n += $resource->load();
1013 public function allString( /* List */ )
1015 $args = func_get_args();
1016 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
1017 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
1020 foreach( $this as $resource )
1022 $all = $resource->all( $args );
1023 foreach( $all as $to_add )
1025 if( isset($done[$to_add->toString()]) ) { continue; }
1026 $l []= $to_add->toString();
1027 $done[$to_add->toString()] = 1;
1030 return new Graphite_ResourceList($this->g,$l);
1033 public function all( /* List */ )
1035 $args = func_get_args();
1036 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
1037 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
1040 foreach( $this as $resource )
1042 $all = $resource->all( $args );
1043 foreach( $all as $to_add )
1045 if( isset($done[$to_add->toString()]) ) { continue; }
1047 $done[$to_add->toString()] = 1;
1050 return new Graphite_ResourceList($this->g,$l);
1053 function append( $x /* List */ )
1055 $args = func_get_args();
1056 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
1057 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
1059 $list = $this->duplicate();
1060 foreach( $args as $arg )
1062 if( ! $arg instanceof Graphite_Resource ) { $arg = $this->g->resource( $arg ); }
1072 foreach( $this as $resource )
1074 if( isset( $done[$resource->toString()] ) ) { continue; }
1076 $done[$resource->toString()]=1;
1078 return new Graphite_ResourceList($this->g,$l);
1081 function union( /* List */ )
1083 $args = func_get_args();
1084 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
1085 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
1087 $list = new Graphite_ResourceList($this->g);
1089 foreach( $this as $resource )
1091 if( isset( $done[$resource->toString()] ) ) { continue; }
1092 $list [] = $resource;
1093 $done[$resource->toString()]=1;
1095 foreach( $args as $arg )
1097 if( ! $arg instanceof Graphite_Resource ) { $arg = $this->g->resource( $arg ); }
1098 if( isset( $done[$arg->toString()] ) ) { continue; }
1100 $done[$arg->toString()]=1;
1105 function intersection( /* List */ )
1107 $args = func_get_args();
1108 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
1109 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
1111 $list = new Graphite_ResourceList($this->g,array());
1113 foreach( $this as $arg )
1115 if( ! $arg instanceof Graphite_Resource ) { $arg = $this->g->resource( $arg ); }
1116 $seen[$arg->toString()]=1;
1118 foreach( $args as $arg )
1120 if( ! $arg instanceof Graphite_Resource ) { $arg = $this->g->resource( $arg ); }
1121 if( ! isset($seen[$arg->toString()]) ) { continue; }
1127 function except( /* List */ )
1129 $args = func_get_args();
1130 if( $args[0] instanceof Graphite_ResourceList ) { $args = $args[0]; }
1131 if( is_array( $args[0] ) ) { $args = func_get_arg( 0 ); }
1133 $list = new Graphite_ResourceList($this->g,array());
1135 foreach( $args as $arg )
1137 if( ! $arg instanceof Graphite_Resource ) { $arg = $this->g->resource( $arg ); }
1138 $exclude[$arg->toString()]=1;
1140 foreach( $this as $arg )
1142 if( ! $arg instanceof Graphite_Resource ) { $arg = $this->g->resource( $arg ); }
1143 if( isset($exclude[$arg->toString()]) ) { continue; }
1149 function allOfType( $uri )
1151 $list = new Graphite_ResourceList( $this->g, array() );
1152 foreach( $this as $item )
1154 if( $item->isType( $uri ) )
1163 function graphite_sort_list_cmp( $a, $b )
1165 global $graphite_sort_args;
1167 foreach( $graphite_sort_args as $arg )
1169 $va = $a->get( $arg );
1170 $vb = $b->get( $arg );
1171 if($va < $vb) return -1;
1172 if($va > $vb) return 1;