1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | <?php // https://github.com/jadell/neo4jphp spl_autoload_register(function ($className) { $libPath = '../lib/neo4jphp/lib/'; $classFile = str_replace('\\',DIRECTORY_SEPARATOR,$className).'.php'; $classPath = $libPath.$classFile; if (file_exists($classPath)) { require($classPath); } }); $nodes = ""; $edges = ""; $description = ""; $nodeList = Array(); function add_node($node) { global $nodes,$nodeList; if (!in_array($node->getId(),$nodeList)) { $start = $node->getProperty("start_date") . $node->getProperty("date"); $end = $node->getProperty("end_date"); $nodes.= "<node id='".urlencode($node->getId())."'". ' label="'.htmlentities($node->getProperty("name")).'" ' . ($start != ""? 'start="'.$start.'" ' : ""). ($end != ""? 'end="'.$end.'" ' : "").'>' ."<viz:color b='".rand(0,255)."' g='".rand(0,255)."' r='".rand(0,255)."'/>" ."</node>". PHP_EOL; $nodeList[] = $node->getId(); } } function add_edge($rel) { global $edges; $start = $rel->getProperty("start_date"); $end = $rel->getProperty("end_date"); $edges.= "<edge id='".urlencode($rel->getId())."' source='".urlencode($rel->getStartNode()->getId())."' target='".urlencode($rel->getEndNode()->getId())."' ".($start != ""? 'start="'.$start.'" ' : ""). ($end != ""? 'end="'.$end.'" ' : "")."/>". PHP_EOL; } function expandNode($node) { //global $description; //$description .= ($description == ""? "" : " and ").$node->getProperty("name"); add_node($node); foreach ($node->getProperties() as $key => $value) { // echo "$key: $value\n"; } foreach ($node->getRelationships() as $rel) { //echo($rel->getStartNode()->getId()." -> ".$rel->getEndNode()->getId()."<br>"); add_edge($rel); add_node($rel->getStartNode()); add_node($rel->getEndNode()); } } //$ids = (isset($_REQUEST['ids']) ? $_REQUEST['ids'] : ""); // Connecting to the default port 7474 on localhost $client = new Everyman\Neo4j\Client(); //$client = new Everyman\Neo4j\Client('192.168.1.127'); //print_r($client->getServerInfo()); //https://github.com/jadell/neo4jphp/wiki/Caching $plugin = new Everyman\Neo4j\Cache\Variable(); $client->getEntityCache()->setCache($plugin); /*$memcached = new Memcached(); $memcached->addServer('localhost', 11211); $plugin = new Everyman\Neo4j\Cache\Memcached($memcached); $client->getEntityCache()->setCache($plugin);*/ /*$requests = explode(";",$ids); foreach ($requests as $request) { // Array("type" => "path", from=>"1234", to=>"4321","options" => Array()) $parts = explode("-",$request); $requestType = $parts[0]; $requestId = $parts[1]; if ($requestType == 'node') { expandNode($client->getNode($requestId)); } else { findNode($requestType,$requestId); } } function findNode($type,$id) { global $client; $typeMapping = Array ( "agency" => Array("label" => "Agency", "id" => "agencyID") ); $queryString = "MATCH (n:".$typeMapping[$type]["label"].")". "WHERE n.".$typeMapping[$type]["id"]." = {nodeId}". "RETURN n"; $query = new Everyman\Neo4j\Cypher\Query($client, $queryString, array('nodeId' => $id)); $result = $query->getResultSet(); foreach ($result as $row) { expandNode( $row[0]); } } */ // https://github.com/jadell/neo4jphp/wiki/Paths /* https://github.com/jadell/neo4jphp/wiki/Traversals * $traversal = new Everyman\Neo4j\Traversal($client); $traversal->addRelationship('KNOWS', Relationship::DirectionOut) ->setPruneEvaluator(Traversal::PruneNone) ->setReturnFilter(Traversal::ReturnAll) ->setMaxDepth(4); $nodes = $traversal->getResults($startNode, Traversal::ReturnTypeNode); */ /*https://github.com/jadell/neo4jphp/wiki/Cypher-and-gremlin-queries $queryString = "START n=node({nodeId}) ". "MATCH (n)<-[:KNOWS]-(x)". "WHERE x.name = {name}". "RETURN x"; $query = new Everyman\Neo4j\Cypher\Query($client, $queryString, array('nodeId' => 1, 'name' => 'Bob')); $result = $query->getResultSet(); foreach ($result as $row) { echo $row['x']->getProperty('name') . "\n"; }*/ $queryString = "MATCH (n) WHERE has(n.start_date) and n.start_date < 18870000 RETURN n"; $description = '1887'; $query = new Everyman\Neo4j\Cypher\Query($client, $queryString); $result = $query->getResultSet(); foreach ($result as $row) { expandNode( $row[0]); } if (!isset($_REQUEST['debug'])) { header('Content-Type: application/gexf+xml'); header('Content-Disposition: attachment; filename="'.urlencode(str_replace(" ","_",strtolower($description))).'.gexf"'); } echo '<?xml version="1.0" encoding="UTF-8"?> <gexf xmlns="http://www.gexf.net/1.2draft" xmlns:viz="http://www.gexf.net/1.2draft/viz" version="1.2"> <meta lastmodifieddate="2009-03-20"> <creator>lobbyist.disclosurelo.gs</creator> <description>'. $description. '</description> </meta> <graph mode="dynamic" defaultedgetype="directed" timeformat="date"> <nodes>'. $nodes. '</nodes> <edges>'. $edges.' </edges> </graph> </gexf>'. PHP_EOL; ?> |