-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcouchDocument.php
More file actions
396 lines (363 loc) · 10.7 KB
/
couchDocument.php
File metadata and controls
396 lines (363 loc) · 10.7 KB
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
<?PHP
/*
Copyright (C) 2009 Mickael Bailly
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class couchDocument {
/**
* @var stdClass object internal data
*/
protected $__couch_data = NULL;
/**
*class constructor
*
* @param couchClient $client couchClient connection object
*
*/
function __construct(couchClient $client) {
$this->__couch_data = new stdClass();
$this->__couch_data->client = $client;
$this->__couch_data->fields = new stdClass();
$this->__couch_data->autocommit = true;
}
/**
* load a CouchDB document from the CouchDB server
*
* @param string $id CouchDB document ID
* @return couchDocument $this
*/
public function load ( $id ) {
if ( !strlen($id) ) throw new InvalidArgumentException("No id given");
$this->__couch_data->fields = $this->__couch_data->client->getDoc($id);
return $this;
}
/**
* Set the auto-commit mode (default true)
*
* If set to false, you should explicitely call the record() method :
* <code>
* $couchdoc->setAutocommit(false);
* $couchdoc->somefield = "foo";
* $couchdoc->someotherfield = "bar";
* $couchdoc->record();
* </code>
*
* @param boolean $commit turn on or off the autocommit feature
* @return couchDocument $this
*/
public function setAutocommit($commit) {
$this->__couch_data->autocommit = (boolean)true;
return $this;
}
/**
* get current auto-commit state (on or off)
*
* @return boolean true if auto-commit is enabled
*/
public function getAutocommit() {
return $this->__couch_data->autocommit;
}
/**
* load a CouchDB document from a PHP object
*
* note that this method clones the object given in argument
*
* @param object $doc CouchDB document (should have $doc->_id , $doc->_rev, ...)
* @return couchDocument $this
*/
public function loadFromObject($doc) {
$this->__couch_data->fields = clone $doc;
return $this;
}
/**
* load a document in a couchDocument object and return it
*
* @static
* @param couchClient $client couchClient instance
* @param string $id id of the document to load
* @return couchDocument couch document loaded with data of document $id
*/
public static function getInstance(couchClient $client,$id) {
$back = new couchDocument($client);
return $back->load($id);
}
/**
* returns all defined keys in this document
*
* @return array list of keys available in this document
*/
public function getKeys ( ) {
return array_keys(get_object_vars($this->__couch_data->fields));
}
/**
* returns all fields (key => values) of this document
*
* @return object all fields of the document
*/
public function getFields () {
return clone $this->__couch_data->fields;
}
/**
* returns document URI
*
* example : couch.server.com:5984/mydb/thisdoc
*
* @return string document URI
*/
public function getUri() {
return $this->__couch_data->client->getDatabaseUri().'/'.$this->id();
}
/**
* returns document id (or null)
*
* @return string document id
*/
public function id() {
return $this->get('_id');
}
/**
* returns value of field $key
*
* @param string $key field name
* @return mixed field value (or null)
*/
public function get ( $key ) {
//echo "get for $key\n";
$key = (string)$key;
if (!strlen($key) ) throw new InvalidArgumentException("No key given");
return property_exists( $this->__couch_data->fields,$key ) ? $this->__couch_data->fields->$key : NULL;
}
/**
* PHP magic method : getter
*
* @see get()
*/
public function __get ( $key ) {
return $this->get($key);
}
/**
* set one field to a value
*
* does not update the database
*
* @param string $key field name
* @param mixed $value field value
* @return boolean TRUE
*/
protected function setOne ($key, $value ) {
$key = (string)$key;
if ( !strlen($key) ) throw new InvalidArgumentException("property name can't be empty");
if ( $key == '_rev' ) throw new InvalidArgumentException("Can't set _rev field");
if ( $key == '_id' AND $this->get('_id') ) throw new InvalidArgumentException("Can't set _id field because it's already set");
if ( substr($key,0,1) == '_' AND !in_array($key,couchClient::$allowed_underscored_properties) )
throw new InvalidArgumentException("Property $key can't begin with an underscore");
//echo "setting $key to ".print_r($value,TRUE)."<BR>\n";
$this->__couch_data->fields->$key = $value;
return TRUE;
}
/**
* record the object to the database
*
*
*/
public function record() {
foreach ( couchClient::$underscored_properties_to_remove_on_storage as $key ) {
if ( property_exists($this->__couch_data->fields,$key) ) {
unset( $this->__couch_data->fields->$key );
}
}
$response = $this->__couch_data->client->storeDoc($this->__couch_data->fields);
$this->__couch_data->fields->_id = $response->id;
$this->__couch_data->fields->_rev = $response->rev;
}
/**
* set document fields
*
* this method store the object in the database !
*
* there is 2 ways to use it. Set one field :
* <code>
* $this->set('some_field','some value');
* </code>
*
* or set multiple fields in one go :
* <code>
* $this->set( array('some_field'=>'some value','some_other_field'=>'another value') );
* </code>
*
* @param string|array $key
* @param mixed $value
*
*/
public function set ( $key , $value = NULL ) {
if ( func_num_args() == 1 ) {
if ( !is_array($key) AND !is_object($key) ) throw new InvalidArgumentException("When second argument is null, first argument should ba an array or an object");
foreach ( $key as $one_key => $one_value ) {
$this->setOne($one_key,$one_value);
}
} else {
$this->setOne($key,$value);
}
if ( $this->__couch_data->autocommit ) {
$this->record();
}
return TRUE;
}
/**
* PHP automagic setter
*
* modify a document property and store to the Server
*
* @link http://php.net/__set
*
* @param string $key name of the property to set
* @param mixed $value property value
*/
public function __set( $key , $value ) {
return $this->set($key,$value);
}
/**
* PHP automagic isset'er
*
*
* @link http://php.net/__isset
*
* @param string $key name of the property to test
*/
public function __isset($key) {
return property_exists($this->__couch_data->fields,$key);
}
/**
* deletes a document property
*
* @param string $key the key to remove
* @return boolean whether the removal process ran successfully
*/
public function remove($key) {
$key = (string)$key;
if ( !strlen($key) ) throw new InvalidArgumentException("Can't remove a key without name");
if ( $key == '_id' OR $key == '_rev' ) return FALSE;
if ( isset($this->$key) ) {
unset($this->__couch_data->fields->$key);
$this->record();
}
return TRUE;
}
/**
* PHP automagic unset'er
*
* @see remove()
* @param string $key the property to delete
* @return boolean whether the removal process ran successfully
*/
public function __unset($key) {
return $this->remove($key);
}
/**
* Replicates document on another couchDB database
*
* @param string $url the database to replicate to ( eg. "http://localhost:5984/foo" or "foo" )
* @param boolean $create_target if set to true, target database will be created if needed
* @return boolean tell if document replication succeded
*/
public function replicateTo($url, $create_target = false) {
echo "replicateTo : ".$this->_id.", $url\n";
if ( !isset($this->_id) ) {
throw new InvalidArgumentException("Can't replicate a document without id");
}
if ( !class_exists("couchReplicator") ) {
return false;
}
$r = new couchReplicator($this->__couch_data->client);
if ( $create_target ) {
$r->create_target();
}
try {
$res = $r ->doc_ids( array( $this->_id ) )
->to($url);
} catch ( Exception $e ) {
return false;
}
print_r($res);
return true;
}
/**
* Replicates document on another couchDB database
*
* @param string $id id of the document to replicate
* @param string $url the database to replicate from ( eg. "http://localhost:5984/foo" or "foo" )
* @param boolean $create_target if set to true, target database will be created if needed
* @return boolean tell if document replication succeded
*/
public function replicateFrom($id, $url, $create_target = false) {
echo "replicateFrom : $id, $url\n";
if ( !class_exists("couchReplicator") ) {
return false;
}
$r = new couchReplicator($this->__couch_data->client);
if ( $create_target ) {
$r->create_target();
}
$r->doc_ids( array( $id ) )->from($url);
$this->load($id);
return true;
}
/**
* Attach a file to a document
*
*
* @param string $file the attachment file (local storage)
* @param string $content_type the attachment content-type (defaults to 'application/octet-stream')
* @param string $filename the attachment filename. If not specified, the basename of "$file" will be used
* @return object CouchDB attachment storage response
*/
public function storeAttachment($file, $content_type = 'application/octet-stream',$filename = null) {
$back = $this->__couch_data->client->storeAttachment($this,$file,$content_type,$filename);
$this->load($this->_id);
return $back;
}
/**
* Attach data as a document attachment
*
*
* @param string $data the attachment contents
* @param string $filename the attachment filename.
* @param string $content_type the attachment content-type (defaults to 'application/octet-stream')
* @return object CouchDB attachment storage response
*/
public function storeAsAttachment ($data,$filename,$content_type = 'application/octet-stream') {
$back = $this->__couch_data->client->storeAsAttachment($this,$data,$filename,$content_type);
$this->load($this->_id);
return $back;
}
/**
* Deletes an attachment
*
* @param string $attachment_name name of the document attachment
* @return object CouchDB attachment removal response
*/
public function deleteAttachment ($attachment_name) {
$back = $this->__couch_data->client->deleteAttachment( $this , $attachment_name );
$this->load($this->_id);
return $back;
}
/**
* returns the URI of a document attachment
*
* @param string $attachment_name the name of the attachment (relative to the document)
* @return string the attachment URI
*/
public function getAttachmentUri ($attachment_name ) {
return $this->getUri().'/'.$attachment_name;
}
}