/
CouchDB.php
398 lines (348 loc) · 12.2 KB
/
CouchDB.php
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
require_once 'Duckk/CouchDB/Connection.php';
require_once 'Duckk/CouchDB/Util.php';
require_once 'Duckk/CouchDB/Document.php';
class Duckk_CouchDB
{
/**
* Connection to CouchDB
*
* @var Duckk_CouchDB_Connection
*/
private $connection = null;
/**
* Whether the method calls to this class should return the unserialized JSON (an stdClass)
* from CouchDB or an "interpretted" PHP value.
*
* When set to TRUE a successful call to $this->createDatabase('foo') will
* return an stdClass like:
*
* <pre>
* object(stdClass)#3 (1) {
* ["ok"]=>
* bool(true)
* }
* </pre>
*
* When set to FALSE the class will "examine" the response and, in the case of
* the example above (a successful call), return a bool TRUE.
*
* Upon a failed request the class will return the unserialized JSON (an stdClass)
* from CouchDB if $this->returnJsonFromCouch === TRUE. If $this->returnJsonFromCouch
* === FALSE then the class will throw an exception instead.
*
* @var bool
*/
public $returnJsonFromCouch = false;
/**
* Constructor
*
* @param string $host The CouchDB server's hostname or IP
* @param int $port The CouchDB server's port
* @param string $keepalive The value for the connection's Keep-Alive request header
*
* @return void
*/
public function __construct($host = Duckk_CouchDB_Connection::DEFAULT_HOST,
$port = Duckk_CouchDB_Connection::DEFAULT_PORT,
$keepalive = Duckk_CouchDB_Connection::DEFAULT_KEEPALIVE)
{
$this->connection = new Duckk_CouchDB_Connection($host, $port, $keepalive);
}
/**
* Get a list of databases on the connected CouchDB server
*
* @return array The names of the db's in an array
*/
public function getDatabases()
{
return $this->connection->get('/_all_dbs');
}
/**
* Create a database
*
*/
public function createDatabase($database)
{
$status = $this->connection->put(
Duckk_CouchDB_Util::makeDatabaseURI($database, null, 'application/json')
);
// return the unserialized json from couch
if ($this->returnJsonFromCouch) {
return $status;
}
// return php-ish values
if (isset($status->ok) && $status->ok == 1) {
return true;
} else {
require_once 'Duckk/CouchDB/Exception/DatabaseExists.php';
throw new Duckk_CouchDB_Exception_DatabaseExists($status);
}
}
/**
* Delete a database
*
* @param string $database The name of the DB
*/
public function deleteDatabase($database)
{
$status = $this->connection->delete(
Duckk_CouchDB_Util::makeDatabaseURI($database)
);
// return the unserialized json from couch
if ($this->returnJsonFromCouch) {
return $status;
}
// return php-ish values
if (isset($status->ok) && $status->ok == 1) {
return true;
} else {
require_once 'Duckk/CouchDB/Exception/DatabaseMissing.php';
throw new Duckk_CouchDB_Exception_DatabaseMissing($status);
}
}
/**
* Run compaction on a DB
*
* @param string $database Name of the DB
*
* @return stdClass Status of the request
*/
public function compactDatabase($database)
{
$status = $this->connection->post(
Duckk_CouchDB_Util::makeDatabaseURI($database)
. '_compact'
);
return $status;
}
/**
* Get info about a DB
*
* @param string $database The name of the DB
*
* @return stdClass
*/
public function getDatabaseInfo($database)
{
$status = $this->connection->get(
Duckk_CouchDB_Util::makeDatabaseURI($database)
);
if (isset($status->error)) {
require_once 'Duckk/CouchDB/Exception/DatabaseMissing.php';
throw new Duckk_CouchDB_Exception_DatabaseMissing($status);
} else {
return $status;
}
}
/**
* Get a document
*
* This method allows you to get a document and also append an arbitary set of
* query string paramaters to the request URI
*
* @param string $database The name of the DB the document lives in
* @param string $documentId The ID of the document
* @param array $queryParams An assoc array of query string parameters
*
* @return stdClass
*/
public function _getDocument($database, $documentId, array $queryParams = array())
{
$databaseURI = Duckk_CouchDB_Util::makeDatabaseURI($database);
$documentURI = $databaseURI . $documentId;
$queryString = '';
if (! empty($queryParams)) {
$qryParts = array();
foreach ($queryParams as $k => $v) {
$qryParts[] = urlencode($k) . '=' . urlencode($v);
}
$documentURI .= '?' . implode('&', $qryParts);
}
// TODO: deal with 404
$response = $this->connection->get($documentURI);
if (isset($response->error)) {
require_once 'Duckk/CouchDB/Exception/DocumentNotFound.php';
throw new Duckk_CouchDB_Exception_DocumentNotFound($documentURI, $response);
}
return $response;
}
/**
* Get a document by ID
*
* You can optionally get a list of revisions and/or info about each revision.
*
* @param string $database The name of the DB the document lives in
* @param string $documentId The ID of the document
* @param bool $getRevisionList Get the revision list too?
* @param bool $getRevisionInfo Get the revision info too?
*
* @return stdClass
*/
public function getDocument($database, $documentId,
$getRevisionList = false, $getRevisionInfo = false)
{
$qryParams = array();
if ($getRevisionList) {
$qryParams['revs'] = 'true';
}
if ($getRevisionInfo) {
$qryParams['revs_info'] = 'true';
}
return $this->_getDocument($database, $documentId, $qryParams);
}
/**
* Get a specific revision of a document
*
* You can optionally get the revision list as well.
*
* From what I can tell you cannot get a doc by revision AND ask for the revision info.
* Couch doesn't get pissed... it just doesn't include the _revs_info. But it does
* seem to return the revision list if you ask for it
*
* @param string $database The DB name
* @param string $documentId The ID of the document
* @param string $rev Which revision to get
* @param bool $getRevisionList Fetch the revision list too?
*
* @return stdClass The document
*/
public function getDocumentRevision($database, $documentId, $rev, $getRevisionList = false)
{
$qryParams = array('rev' => $rev);
if ($getRevisionList) {
$qryParams['revs'] = 'true';
}
return $this->_getDocument($database, $documentId, $qryParams);
}
/**
* Get a list of known revisions for a document
*
* @param string $database The DB name
* @param string $documentId The ID of the document
*
* @return stdClass The document
*/
public function getDocumentRevisionList($database, $documentId)
{
$resp = $this->_getDocument($database, $documentId, array('revs' => 'true'));
return $resp->_revs;
}
/**
* Get the revision infomrtaion of a document
*
* @param string $database The DB name
* @param string $documentId The ID of the document
*
* @return stdClass The document
*/
public function getDocumentRevisionInfo($database, $documentId)
{
$resp = $this->_getDocument($database, $documentId, array('revs_info' => 'true'));
return $resp->_revs_info;
}
/**
* PUT a document.
*
* You can use this to "create" or "update" a document. If you're creating a new doc
* then the server assigned revision will be assigned to $doc->_rev
*
* @param string $database The database to put the document in to
* @param Duckk_CouchDB_Document $doc The document to PUT
* @param bool $batchOK Whether or not you want to allow batch processing on Couch
* This should be set to FALSE for all critical data
*
* @return stdClass The response from CouchDB
*/
public function putDocument($database, Duckk_CouchDB_Document &$doc, $batchOK = false)
{
if (! $doc->hasRevision()) {
unset($doc->_rev);
}
if (! $doc->hasAttachments()) {
unset($doc->_attachments);
}
$uri = Duckk_CouchDB_Util::makeDatabaseURI($database)
. $doc->_id
. (($batchOK) ? '?batch=ok' : '');
$json = json_encode($doc);
$resp = $this->connection->put($uri, $json, 'application/json');
if (isset($resp->error)) {
require_once 'Duckk/CouchDB/Exception/UpdateConflict.php';
throw new Duckk_CouchDB_Exception_UpdateConflict($uri, $resp);
}
$doc->_rev = $resp->rev;
return $resp;
}
/**
* POST a new Document
*
* The CouchDB docs pretty much say you shouldn't use this and should use PUt instead.
* http://wiki.apache.org/couchdb/HTTP_Document_API
*
* If you want to create a new Document and are too lazy to try generate a unique
* yourself, then this will work. So, more or less it's like PUT but soley for creating
* a NEW document AND CouchDB will assign an ID to the document for you.
*
* The class will add the rev and id to the document you passed in
*
* @param string $database The DB to post the document to
* @param Duckk_CouchDB_Document $doc The document to post
*
* @return stdClass
*/
public function postDocument($database, Duckk_CouchDB_Document &$doc)
{
$uri = Duckk_CouchDB_Util::makeDatabaseURI($database);
$json = json_encode($doc);
$resp = $this->connection->post($uri, $json, 'application/json');
// TODO: check document ID collission
$doc->_rev = $resp->rev;
$doc->_id = $resp->id;
return $resp;
}
/**
* Delete a document
*
* You can either pass in a db name with the document id, or the db name with an
* instance of Duckk_CouchDB_Document.
*
* If you dont pass in a Duckk_CouchDB_Document for $documentID then you have to
* pass in the $documentRevision as the 3rd param
*
* @param string $database The name of the DB the document is in
* @param string|Duckk_CouchDB_Document $documentID The document's ID or the document itself
* @param string $documentRevision The rev of the document to delete
*
* @return stdClass
*/
public function deleteDocument($database, $documentID, $documentRevision = null)
{
$uri = null;
if ($documentID instanceof Duckk_CouchDB_Document) {
$uri = Duckk_CouchDB_Util::makeDatabaseURI($database)
. $documentID->_id
. "?rev={$documentID->_rev}";
} else {
$uri = Duckk_CouchDB_Util::makeDatabaseURI($database)
. $documentID
. "?rev={$documentRevision}";
}
$resp = $this->connection->delete($uri);
if (isset($resp->error)) {
require_once 'Duckk/CouchDB/Exception/UpdateConflict.php';
throw new Duckk_CouchDB_Exception_UpdateConflict($uri, $resp);
}
return $resp;
}
public function putAttachment($database, $documentID,
$attachmentContentType, $attachmentData,
$documentRevision = null)
{
$uri = Duckk_CouchDB_Util::makeDatabaseURI($database)
. $documentID
. (($documentRevision) ? "?rev={$documentRevision}" : '');
$resp = $this->connection->put($uri, $json, $attachmentContentType);
}
}
?>