Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 463 lines (316 sloc) 19.767 kb
56f7675 @jadell Initial API documentation.
authored
1 Neo4jPHP
2 ========
3 Author: Josh Adell <josh.adell@gmail.com>
4 Copyright (c) 2011
5
6 PHP Wrapper for the Neo4j graph database REST interface
7
b546587 @jadell Running neo4jphp.phar on the command line provides useful information.
authored
8 In-depth documentation and examples can be found on the wiki: http://github.com/jadell/Neo4jPHP/wiki
039988c @jadell README points to wiki
authored
9
b546587 @jadell Running neo4jphp.phar on the command line provides useful information.
authored
10 Install
11 -------
12 1. Download latest PHAR from http://github.com/downloads/jadell/Neo4jPHP/neo4jphp.phar
13 2. `include` or `require` neo4jphp.phar in your project
039988c @jadell README points to wiki
authored
14
b546587 @jadell Running neo4jphp.phar on the command line provides useful information.
authored
15 Connection Test
16 ---------------
17 From the command line, execute the following:
9ac9336 @jadell GEOFF API and usage added to README
authored
18
b546587 @jadell Running neo4jphp.phar on the command line provides useful information.
authored
19 > php neo4jphp.phar localhost
20
21 Change localhost to the host name of your Neo4j instance. Port defaults to 7474, or can be specified as the second parameter after the host name.
22
23 Execute the following to see more command line options:
24
25 > php neo4jphp.phar
9ac9336 @jadell GEOFF API and usage added to README
authored
26
6591784 @jadell More API documentation.
authored
27
28
29 Examples
30 --------
ed06d34 @jadell Add example scripts and readme.
authored
31 "Real" usage examples can be found in the `examples` directory.
6591784 @jadell More API documentation.
authored
32
ed06d34 @jadell Add example scripts and readme.
authored
33 ### Initialize the connection
34 $client = new Client(new Transport());
35
36 ### Create a node
37 $node = new Node($client);
38 $node->setProperty('name', 'John Smith')
39 ->setProperty('age', '29')
40 ->setProperty('role', 'admin')
41 ->save();
42
43 ### Index a node
5a35ee9 @jadell Update README with Batch API documentation and usage.
authored
44 $users = new Index($client, Index::TypeNode, 'users');
ed06d34 @jadell Add example scripts and readme.
authored
45 $users->add($node, 'name', $node->getProperty('name'));
46 $users->add($node, 'foo', 'bar');
47
48 ### Find a node
49 $user = $users->findOne('name', 'John Smith');
50 $allFooBars = $user->find('foo', 'bar');
51
52 ### Create a relationship
53 $relationship = $node->relateTo($otherNode, 'KNOWS');
54 $relationship->setProperty('from', 'school')
55 ->setProperty('how_long', '38 months')
56 ->save();
57
58 ### Find a path between two nodes
59 $path = $node->findPathsTo($otherNode, 'KNOWS', Relationship::DirectionOut)
60 ->maxDepth(4)
61 ->getSinglePath();
6591784 @jadell More API documentation.
authored
62
60fdbb3 @jadell Add traversal API and example usage to README
authored
63 ### Traverse the graph
64 $traversal = new Traversal($client);
65 $traversal->setPruneEvaluator(Traversal::PruneNone)
66 ->setReturnFilter('javascript', 'return true;')
67 ->setOrder(Traversal::OrderBreadthFirst);
68 $nodes = $traversal->getResults($node, Traversal::ReturnTypeNode);
69 $firstRelationship = $traversal->getSingleResult($node, Traversal::ReturnTypeRelationship);
70 $paths = $traversal->getResults($node, Traversal::ReturnTypePath);
71
f030805 @jadell Paged traversal example added to README
authored
72 ### Paged traversal
73 $pager = new Pager($traversal, $node, Traversal::ReturnTypeNode);
74 $pager->setPageSize(10)
75 ->setLeaseTime(30);
76
77 while ($results = $pager->getNextResults()) {
78 foreach ($results as $node) {
79 echo $node->getProperty('name');
80 }
81 }
8cb5cfb @jadell Update README with caching example.
authored
82
83 ### Setting a cache
84 // By default, the caching back-end is set to `Cache\Null`.
7afb443 @jadell Specify the cache expiration time when setting a cache.
authored
85 // New cache back-ends can be created by implementing the `Cache` interface.
86 $cacheExpire = 30;
87 $client->setCache(new Cache\Variable(), $cacheExpire);
8cb5cfb @jadell Update README with caching example.
authored
88
5a35ee9 @jadell Update README with Batch API documentation and usage.
authored
89 ### Batch operations
90 $batch = new Batch($client);
91
92 $nodeA = new Node($client);
93 $nodeB = new Node($client);
94 $relationship = $nodeA->relateTo($nodeB, 'KNOWS');
95 $userIndex = new Index($client, Index::TypeNode, 'users');
96
97 $batch->save($nodeA);
98 $batch->save($nodeB);
99 $batch->save($relationship);
100 $batch->addToIndex($nodeIndex, $nodeB, 'username', 'bob123');
101 $batch->commit();
102
9ac9336 @jadell GEOFF API and usage added to README
authored
103 ### GEOFF import and export
104 // Import from a file handle, or a GEOFF string.
105 // The same batch can be used to import more than one file before committing
106 $geoff = new Geoff($client);
107 $handle = fopen('/path/to/import.geoff','r');
108 $batch = $geoff->load($handle);
109 $batch->commit();
110
111 // Export to either a string or a file handle.
112 // Takes a single Path or an array of Paths
113 $handle = fopen('/path/to/export.geoff','w');
114 $geoff->dump($path, $handle);
f030805 @jadell Paged traversal example added to README
authored
115
56f7675 @jadell Initial API documentation.
authored
116 API
117 ---
118
119 ### Transport
120
121 __construct(string $host='localhost', integer $port=7474)
122 Create a new REST transport.
123
124 ### Client
125
126 __construct(Transport $transport)
127 Create a new Neo4j client.
128
b901f62 @jadell List known indexes.
authored
129 getIndexes() : array
130 Return an array of all the indexes known to the server. Each element of the returned array is an `Index` object.
131
b433f03 @jadell Fix README
authored
132 getRelationshipTypes() : array
133 Return an array of all the relationship types known to the server. Each element of the returned array is a string.
134
7afb443 @jadell Specify the cache expiration time when setting a cache.
authored
135 setCache(Cache $cache, integer $cacheTimeout)
57407cc @jadell Add Memcached to README.
authored
136 Set the caching mechanism to use. $cacheTimeout is indicated in seconds. There are three built-in cache wrappers that can be used: `Cache\Null` which does not cache anything (this is the default if no cache is set); and `Cache\Variable` which holds all cached values in memory for the length of the process or request; and `Cache\Memcached` which uses the PHP Memcached extension to persist values across requests and processes. New cache back-ends can be created by extending the `Cache` interface.
8cb5cfb @jadell Update README with caching example.
authored
137
56f7675 @jadell Initial API documentation.
authored
138 ### Node
139
140 __construct(Client $client)
141 Create a new Node instance. Nodes are not saved to the server until `save` is called.
142
143 delete() : boolean
144 Delete this node from the server. Returns true on success, false otherwise.
145
146 findPathsTo(Node $to, string $type=null, string $dir=null) : PathFinder
147 Return a PathFinder that can be used to get paths from the node to $to. $dir must be one of `Relationship::DirectionAll`, `Relationship::DirectionIn`, `Relationship::DirectionOut`, or null. If $dir is specified, $type must also be specified.
148
149 getId() : integer
150 Return this node's unique identifier.
151
152 getProperties() : array
153 Return an array of all properties set on this node, indexed by key.
154
155 getProperty(string $property) : string
156 Return the value of the named property. Returns null if the named property is not set.
157
158 getRelationships($types=array(), $dir=null) : array
159 Return an array of Relationships that match the given criteria. $dir must be one of `Relationship::DirectionAll`, `Relationship::DirectionIn`, `Relationship::DirectionOut`, or null.
160
161 hasId() : boolean
162 Returns true if the node is identified, false otherwise.
163
164 load() : boolean
165 Load this node's data from the server. Returns true on success, false otherwise.
166
167 relateTo(Node $to, string $type) : Relationship
168 Create a relationship to $to. Note that the Relationship is not saved to the server until its `save` method is called.
169
170 removeProperty(string $property) : Node
171 Removes the named property from the node. Returns the node.
172
173 save() : boolean
174 Save this node to the server. Returns true on success, false otherwise.
175
176 setId(integer $id) : Node
177 Set the id of this node before calling `load` to retrieve the node's data from the server. Returns the node.
178
179 setProperties(array $properties) : Node
180 Set multiple properties, indexed by key. Returns the node.
181
182 setProperty(string $property, mixed $value) : Node
183 Set the named property to the given value. $value must be scalar. Returns the node.
184
185 ### Relationship
186
187 __construct(Client $client)
188 Create a new Relationship instance. Relationships are not saved to the server until `save` is called.
189
190 delete() : boolean
191 Delete this relationship from the server. Returns true on success, false otherwise.
192
193 getId() : integer
194 Return this relationship's unique identifier.
195
196 getEndNode() : Node
197 Return the node on the incoming end of this relationship. Returns null of none is set.
198
199 getProperties() : array
200 Return an array of all properties set on this relationship, indexed by key.
201
202 getProperty(string $property) : string
203 Return the value of the named property. Returns null if the named property is not set.
204
205 getStartNode() : Node
206 Return the node on the outgoing end of this relationship. Returns null of none is set.
207
208 getType() : string
209 Return the type of relationship.
210
211 hasId() : boolean
212 Returns true if the relationship is identified, false otherwise.
213
214 load() : boolean
215 Load this relationships's data from the server. Returns true on success, false otherwise.
216
217 removeProperty(string $property) : Relationship
218 Removes the named property from the relationship. Returns the relationship.
219
220 save() : boolean
221 Save this relationship to the server. Returns true on success, false otherwise.
222
223 setEndNode(Node $end) : Relationship
224 Set the relationship's incoming node. Returns the relationship.
225
226 setId(integer $id) : Relationship
227 Set the id of this relationship before calling `load` to retrieve the relationship's data from the server. Returns the relationship.
228
229 setProperties(array $properties) : Relationship
230 Set multiple properties, indexed by key. Returns the relationship.
231
232 setProperty(string $property, mixed $value) : Relationship
233 Set the named property to the given value. $value must be scalar. Returns the relationship.
234
235 setStartNode(Node $start) : Relationship
236 Set the relationship's outgoing node. Returns the relationship.
237
238 setType($type) : Relationship
239 Set the type of relationship. Returns the relationship.
240
241 ### Index
242
243 __construct(Client $client, string $type, string $name)
244 Create a new index. Indexes are not saved to the server until `save` or `add` are called. $type must be one of `Index::TypeNode` or `Index::TypeRelationship`
245
246 add(mixed $entity, string $key, mixed $value) : boolean
247 Add a Node or Relationship to the index. $value must be scalar. If the index did not exist on the server previously, it is created. Returns true on success, false otherwise.
248
249 delete() : boolean
250 Remove the index from the server. Returns true on success, false otherwise.
251
252 find(string $key, mixed $value) : array
253 Find Nodes or Relationships where the named property has the given value. $value must be scalar.
254
255 findOne(string $key, mixed $value) : mixed
256 Find the first Node or Relationship where the named property has the given value. $value must be scalar. Returns null if no match is found.
257
258 getName() : string
259 Return the index name.
260
261 getType() : string
262 Return the index type, one of `Index::TypeNode` or `Index::TypeRelationship`.
263
7b013ef @jadell Fix syntax bug when querying an index.
authored
264 query(string $query) : array
265 Find Nodes or Relationships matching the given query (query language is Lucene).
266
267 queryOne($query) : mixed
268 Find the first Node or Relationship matching the given query (query language is Lucene). Returns null if no match is found.
269
56f7675 @jadell Initial API documentation.
authored
270 remove(mixed $entity, string $key=null, mixed $value=null) : boolean
271 Remove the given Node or Relationship from the index. If given, $value must be scalar.
272
273 save() : boolean
274 Save this index to the server. Return true on success, false otherwise.
275
276 ### Path
277
6591784 @jadell More API documentation.
authored
278 __construct()
279 Create a new Path. The default context for a Path is nodes.
280
281 count() : integer
282 If current context is `Path::ContextNode`, returns the number of nodes in the Path. Otherwise, returns the number of relationships in the Path.
283
284 Path implements the `Countable` interface, which means it can be used as `count($path)`.
285
286 getContext() : string
287 Returns the current context, one of `Path::ContextNode` or `Path::ContextRelationship`.
288
289 getEndNode() : Node
290 Return the Node at the end of the path. Returns null if there are no nodes.
291
292 getLength() : integer
293 Alias for `count`
294
295 getIterator() : ArrayIterator
296 If current context is `Path::ContextNode`, returns the nodes in an ArrayIterator. Otherwise, returns the relationships in an ArrayIterator.
297
298 Path implement the `IteratorAggregate` interface, which means it can be looped over in `foreach` loops. If the current context is nodes, `foreach` will loop over the nodes in the path, otherwise it will loop over the relationships.
299
300 getNodes() : array
301 Return the ordered array of Node objects that make up this path.
302
303 getRelationships() : array
304 Return the ordered array of Relationship objects that make up this path.
305
306 getStartNode() : Node
307 Return the Node at the beginning of the path. Returns null if there are no nodes.
308
309 setContext($context) : Path
310 Set whether `count` and `foreach` will refer to the nodes or relationships of this path. $context should be one of `Path::ContextNode` or `Path::ContextRelationship`. Returns the Path.
311
56f7675 @jadell Initial API documentation.
authored
312 ### PathFinder
313
6591784 @jadell More API documentation.
authored
314 __construct(Client $client)
315 Create a new PathFinder object.
316
a3909eb @jadell Find paths via different algorithms.
authored
317 getAlgorithm() : string
318 Return the current path search algorithm. One of `PathFinder::AlgoShortest` (default), `PathFinder::AlgoAll`, `PathFinder::AlgoAllSimple` or `PathFinder::AlgoDijkstra`.
319
320 getCostProperty() : string
321 Return the current relationship property to use to determine path cost. Only used when `PathFinder::AlgoDijkstra` is the search algorithm.
322
323 getDefaultCost() : numeric
324 Return the default cost to assign relationships without a cost property. Only used when `PathFinder::AlgoDijkstra` is the search algorithm.
325
6591784 @jadell More API documentation.
authored
326 getDirection() : string
327 Return the current path finding direction, one of the `Relationship::Direction` constants.
328
329 getEndNode() : Node
330 Return the Node to find paths to.
331
332 getMaxDepth() : integer
333 Return the current maximum length for found paths.
334
335 getPaths() : array
336 Return an array of Path objects matching the search criteria.
337
338 getSinglePath() : Path
339 Return the first Path matching the search criteria.
340
341 getStartNode() : Node
342 Return the Node to find paths from.
343
344 getType() : string
345 Return the current relationship type to which path relationships will be limited.
346
a3909eb @jadell Find paths via different algorithms.
authored
347 setAlgorithm(string $algorithm) : PathFinder
348 Set the path search algorithm. One of `PathFinder::AlgoShortest` (default), `PathFinder::AlgoAll`, `PathFinder::AlgoAllSimple` or `PathFinder::AlgoDijkstra`. Returns the PathFinder.
349
350 setCostProperty(string $property) : PathFinder
351 Set the relationship property to use to determine path cost. Only used when `PathFinder::AlgoDijkstra` is the search algorithm. Returns the PathFinder.
352
353 setDefaultCost(numeric $cost) : PathFinder
354 Set the default cost to assign relationships without a cost property. Only used when `PathFinder::AlgoDijkstra` is the search algorithm. Returns the PathFinder.
355
6591784 @jadell More API documentation.
authored
356 setDirection($dir) : PathFinder
357 Set the path finding direction, one of the `Relationship::Direction` constants. Returns the PathFinder.
358
359 setEndNode(Node $end) : PathFinder
360 Set the Node to find paths to. Returns the PathFinder.
361
362 setMaxDepth($max) : PathFinder
363 Set the maximum length for found paths. Returns the PathFinder.
364
365 setStartNode(Node $start) : PathFinder
366 Set the Node to find paths from. Returns the PathFinder.
367
368 setType($type) : PathFinder
369 Set the relationship type to which path relationships will be limited. Returns the PathFinder.
56f7675 @jadell Initial API documentation.
authored
370
371
60fdbb3 @jadell Add traversal API and example usage to README
authored
372 ### Traversal
373
374 __construct(Client $client)
375 Create a new Traversal object.
376
377 addRelationship(string $type, string $direction=null) : Traversal
378 Add a new Relationship type to the Traversal. $direction should be one of `Relationship::DirectionAll`, `Relationship::DirectionOut` or `Relationship::DirectionIn`. Returns the Traversal.
379
380 getMaxDepth() : integer
381 Return the maximum length to traverse before pruning. If no prune evaluator is given, this defaults to 1. If a prune evaluator is given, this value is ignored.
382
383 getOrder() : string
384 Return the traversal order. One of `Traversal::OrderDepthFirst` or `Traversal::OrderBreadthFirst`.
385
386 getPruneEvaluator() : array
387 Returns the current prune evaluator function if set. Array contains two elements 'language' and 'body'.
388
389 getRelationships() : array
390 Return the set relationship types. Each element is an array with elements 'type' and 'direction' (if direction is set.)
391
392 getResults(Node $startNode, string $returnType) : array
393 Run the traversal and get the array of results. $returnType is one of `Traversal::ReturnTypeNode` (returns an array of Node objects), `Traversal::ReturnTypeRelationship` (returns an array of Relationship objects), `Traversal::ReturnTypePath` or `Traversal::ReturnTypeFullPath` (returns an array of Path objects).
394
395 getReturnFilter() : array
396 Returns the current return filter function if set. Array contains two elements 'language' and 'body'.
397
398 getSingleResult(Node $startNode, string $returnType)
399 Return the first result of the traversal. $returnType is one of `Traversal::ReturnTypeNode` (returns an array of Node objects), `Traversal::ReturnTypeRelationship` (returns an array of Relationship objects), `Traversal::ReturnTypePath` or `Traversal::ReturnTypeFullPath` (returns an array of Path objects).
400
401 getUniqueness() : string
402 Return the current uniqueness filter. One of `Traversal::UniquenessNone`, `Traversal::UniquenessNodeGlobal`, `Traversal::UniquenessRelationshipGlobal`, `Traversal::UniquenessNodePath` or `Traversal::UniquenessRelationshipPath`.
403
404 setMaxDepth(integer $max) : Traversal
405 Set the maximum length to traverse before pruning. If a prune evaluator is given, this value is ignored. Returns the Traversal.
406
407 setOrder(string $order) : Traversal
408 Set the traversal order. One of `Traversal::OrderDepthFirst` or `Traversal::OrderBreadthFirst`. Returns the Traversal.
409
410 setPruneEvaluator(string $language=null, string $body=null) : Traversal
411 Set the prune evaluator function. If language is the `Traversal::PruneNone` constant, the evaluator language will be set to 'builtin' and the body will be set to the value of the constant. Returns the Traversal.
412
413 setReturnFilter(string $language=null, string $body=null) : Traversal
414 Set the return filter function. If language is one of the `Traversal::ReturnAll` or `Traversal::ReturnAllButStart` constants, the filter language will be set to 'builtin' and the body will be set to the value of the constant. Returns the Traversal.
56f7675 @jadell Initial API documentation.
authored
415
60fdbb3 @jadell Add traversal API and example usage to README
authored
416 setUniqueness(string $uniqueness) : Traversal
417 Set the current uniqueness filter. One of `Traversal::UniquenessNone`, `Traversal::UniquenessNodeGlobal`, `Traversal::UniquenessRelationshipGlobal`, `Traversal::UniquenessNodePath` or `Traversal::UniquenessRelationshipPath`. Returns the Traversal.
56f7675 @jadell Initial API documentation.
authored
418
5a35ee9 @jadell Update README with Batch API documentation and usage.
authored
419 ### Batch
420
421 __construct(Client $client)
422 Create a new batch operation. Batches are committed to the server in one request, and the entire batch succeeds or fails as a whole.
423
424 addToIndex(Index $index, mixed $entity, string $key, mixed $value) : integer
425 Add a Node or Relationship to the Index. $value must be scalar. If the index does not exist on the server, it is created. If the Relationship or Node does not exist on the server, it is created. Returns the operation id.
426
427 commit() : boolean
428 Commit the batch to the server. A Batch cannot be committed more than once. Returns true on success, false otherwise.
429
430 delete(mixed $entity) : integer
431 Remove a Node or Relationship from the server. Returns the operation id.
432
433 removeFromIndex(Index $index, mixed $entity, string $key=null, mixed $value=null) : integer
434 Remove the given Node or Relationship from the index. If given, $value must be scalar. Returns the operation id.
435
682b61c @jadell Update README with Batch API documentation and usage.
authored
436 save(mixed $entity) : integer
5a35ee9 @jadell Update README with Batch API documentation and usage.
authored
437 Save a Node or Relationship to the server. If the Node or Relationship does not exist, it is created, otherwise, it is updated. If the start or end nodes of the Relationship do not exist, they will be created. Returns the operation id.
438
9ac9336 @jadell GEOFF API and usage added to README
authored
439 ### Geoff
440 See [http://py2neo.org/geoff/](http://py2neo.org/geoff/) for more information
441
442 __construct(Client $client)
443 Create a new GEOFF importer/exporter.
444
445 dump($paths, $handle=null) : string
446 Dump either a single Path or an array of Paths. If $handle is given and is a stream, the result string will be written to the stream. Otherwise, the result string will be returned.
447
448 load($handle, Batch $batch=null) : Batch
449 Import a GEOFF file or string. $handle must be a valid stream resource or a string. If $batch is provided, imported node, relationship and index operations will be appended to it. Otherwise, a new batch representing the exported operations will be returned.
5a35ee9 @jadell Update README with Batch API documentation and usage.
authored
450
451
b546587 @jadell Running neo4jphp.phar on the command line provides useful information.
authored
452 Contributions
453 -------------
454 * Jacob Hansson <jacob@voltvoodoo.com> - Cypher query support
455 * Nigel Small <nigel@nigelsmall.name> - GEOFF import/export
456 * [http://py2neo.org/](http://py2neo.org/)
457
458
56f7675 @jadell Initial API documentation.
authored
459 To Do
460 -----
c4501b4 @jadell Added a few to-dos.
authored
461 * UTF-8 support?
462 * Paginated Cypher/Gremlin results?
Something went wrong with that request. Please try again.