1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
use Alcaeus\MongoDbAdapter\Helper; |
17
|
|
|
use Alcaeus\MongoDbAdapter\TypeConverter; |
18
|
|
|
use MongoDB\Model\CollectionInfo; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Instances of this class are used to interact with a database. |
22
|
|
|
* @link http://www.php.net/manual/en/class.mongodb.php |
23
|
|
|
*/ |
24
|
|
|
class MongoDB |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
use Helper\ReadPreference; |
27
|
|
|
use Helper\SlaveOkay; |
28
|
|
|
use Helper\WriteConcern; |
29
|
|
|
|
30
|
|
|
const PROFILING_OFF = 0; |
31
|
|
|
const PROFILING_SLOW = 1; |
32
|
|
|
const PROFILING_ON = 2; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var MongoClient |
36
|
|
|
*/ |
37
|
|
|
protected $connection; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \MongoDB\Database |
41
|
|
|
*/ |
42
|
|
|
protected $db; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $name; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Creates a new database |
51
|
|
|
* |
52
|
|
|
* This method is not meant to be called directly. The preferred way to create an instance of MongoDB is through {@see Mongo::__get()} or {@see Mongo::selectDB()}. |
53
|
|
|
* @link http://www.php.net/manual/en/mongodb.construct.php |
54
|
|
|
* @param MongoClient $conn Database connection. |
55
|
|
|
* @param string $name Database name. |
56
|
|
|
* @throws Exception |
57
|
|
|
* @return MongoDB Returns the database. |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
public function __construct(MongoClient $conn, $name) |
60
|
|
|
{ |
61
|
|
|
$this->connection = $conn; |
62
|
|
|
$this->name = $name; |
63
|
|
|
|
64
|
|
|
$this->setReadPreferenceFromArray($conn->getReadPreference()); |
65
|
|
|
$this->setWriteConcernFromArray($conn->getWriteConcern()); |
66
|
|
|
|
67
|
|
|
$this->createDatabaseObject(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return \MongoDB\Database |
72
|
|
|
* @internal This method is not part of the ext-mongo API |
73
|
|
|
*/ |
74
|
|
|
public function getDb() |
75
|
|
|
{ |
76
|
|
|
return $this->db; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The name of this database |
81
|
|
|
* |
82
|
|
|
* @link http://www.php.net/manual/en/mongodb.--tostring.php |
83
|
|
|
* @return string Returns this database's name. |
84
|
|
|
*/ |
85
|
|
|
public function __toString() |
86
|
|
|
{ |
87
|
|
|
return $this->name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Gets a collection |
92
|
|
|
* |
93
|
|
|
* @link http://www.php.net/manual/en/mongodb.get.php |
94
|
|
|
* @param string $name The name of the collection. |
95
|
|
|
* @return MongoCollection |
96
|
|
|
*/ |
97
|
|
|
public function __get($name) |
98
|
|
|
{ |
99
|
|
|
// Handle w and wtimeout properties that replicate data stored in $readPreference |
100
|
|
|
if ($name === 'w' || $name === 'wtimeout') { |
101
|
|
|
return $this->getWriteConcern()[$name]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->selectCollection($name); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $name |
109
|
|
|
* @param mixed $value |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function __set($name, $value) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
if ($name === 'w' || $name === 'wtimeout') { |
114
|
|
|
$this->setWriteConcernFromArray([$name => $value] + $this->getWriteConcern()); |
115
|
|
|
$this->createDatabaseObject(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns information about collections in this database |
121
|
|
|
* |
122
|
|
|
* @link http://www.php.net/manual/en/mongodb.getcollectioninfo.php |
123
|
|
|
* @param array $options An array of options for listing the collections. |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
public function getCollectionInfo(array $options = []) |
127
|
|
|
{ |
128
|
|
|
// The includeSystemCollections option is no longer supported |
129
|
|
|
if (isset($options['includeSystemCollections'])) { |
130
|
|
|
unset($options['includeSystemCollections']); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$collections = $this->db->listCollections($options); |
134
|
|
|
|
135
|
|
|
$getCollectionInfo = function (CollectionInfo $collectionInfo) { |
136
|
|
|
return [ |
137
|
|
|
'name' => $collectionInfo->getName(), |
138
|
|
|
'options' => $collectionInfo->getOptions(), |
139
|
|
|
]; |
140
|
|
|
}; |
141
|
|
|
|
142
|
|
|
return array_map($getCollectionInfo, iterator_to_array($collections)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get all collections from this database |
147
|
|
|
* |
148
|
|
|
* @link http://www.php.net/manual/en/mongodb.getcollectionnames.php |
149
|
|
|
* @param array $options An array of options for listing the collections. |
150
|
|
|
* @return array Returns the names of the all the collections in the database as an array |
151
|
|
|
*/ |
152
|
|
|
public function getCollectionNames(array $options = []) |
153
|
|
|
{ |
154
|
|
|
// The includeSystemCollections option is no longer supported |
155
|
|
|
if (isset($options['includeSystemCollections'])) { |
156
|
|
|
unset($options['includeSystemCollections']); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$collections = $this->db->listCollections($options); |
160
|
|
|
|
161
|
|
|
$getCollectionName = function (CollectionInfo $collectionInfo) { |
162
|
|
|
return $collectionInfo->getName(); |
163
|
|
|
}; |
164
|
|
|
|
165
|
|
|
return array_map($getCollectionName, iterator_to_array($collections)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return MongoClient |
170
|
|
|
* @internal This method is not part of the ext-mongo API |
171
|
|
|
*/ |
172
|
|
|
public function getConnection() |
173
|
|
|
{ |
174
|
|
|
return $this->connection; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Fetches toolkit for dealing with files stored in this database |
179
|
|
|
* |
180
|
|
|
* @link http://www.php.net/manual/en/mongodb.getgridfs.php |
181
|
|
|
* @param string $prefix The prefix for the files and chunks collections. |
182
|
|
|
* @return MongoGridFS Returns a new gridfs object for this database. |
183
|
|
|
*/ |
184
|
|
|
public function getGridFS($prefix = "fs") |
185
|
|
|
{ |
186
|
|
|
return new \MongoGridFS($this, $prefix, $prefix); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Gets this database's profiling level |
191
|
|
|
* |
192
|
|
|
* @link http://www.php.net/manual/en/mongodb.getprofilinglevel.php |
193
|
|
|
* @return int Returns the profiling level. |
194
|
|
|
*/ |
195
|
|
|
public function getProfilingLevel() |
196
|
|
|
{ |
197
|
|
|
$result = $this->command(['profile' => -1]); |
198
|
|
|
|
199
|
|
|
return ($result['ok'] && isset($result['was'])) ? $result['was'] : 0; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Sets this database's profiling level |
204
|
|
|
* |
205
|
|
|
* @link http://www.php.net/manual/en/mongodb.setprofilinglevel.php |
206
|
|
|
* @param int $level Profiling level. |
207
|
|
|
* @return int Returns the previous profiling level. |
208
|
|
|
*/ |
209
|
|
|
public function setProfilingLevel($level) |
210
|
|
|
{ |
211
|
|
|
$result = $this->command(['profile' => $level]); |
212
|
|
|
|
213
|
|
|
return ($result['ok'] && isset($result['was'])) ? $result['was'] : 0; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Drops this database |
218
|
|
|
* |
219
|
|
|
* @link http://www.php.net/manual/en/mongodb.drop.php |
220
|
|
|
* @return array Returns the database response. |
221
|
|
|
*/ |
222
|
|
|
public function drop() |
223
|
|
|
{ |
224
|
|
|
return TypeConverter::convertObjectToLegacyArray($this->db->drop()); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Repairs and compacts this database |
229
|
|
|
* |
230
|
|
|
* @link http://www.php.net/manual/en/mongodb.repair.php |
231
|
|
|
* @param bool $preserve_cloned_files [optional] <p>If cloned files should be kept if the repair fails.</p> |
232
|
|
|
* @param bool $backup_original_files [optional] <p>If original files should be backed up.</p> |
233
|
|
|
* @return array <p>Returns db response.</p> |
234
|
|
|
*/ |
235
|
|
|
public function repair($preserve_cloned_files = FALSE, $backup_original_files = FALSE) |
|
|
|
|
236
|
|
|
{ |
237
|
|
|
$this->notImplemented(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Gets a collection |
242
|
|
|
* |
243
|
|
|
* @link http://www.php.net/manual/en/mongodb.selectcollection.php |
244
|
|
|
* @param string $name <b>The collection name.</b> |
245
|
|
|
* @throws Exception if the collection name is invalid. |
246
|
|
|
* @return MongoCollection Returns a new collection object. |
247
|
|
|
*/ |
248
|
|
|
public function selectCollection($name) |
249
|
|
|
{ |
250
|
|
|
return new MongoCollection($this, $name); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Creates a collection |
255
|
|
|
* |
256
|
|
|
* @link http://www.php.net/manual/en/mongodb.createcollection.php |
257
|
|
|
* @param string $name The name of the collection. |
258
|
|
|
* @param array $options |
259
|
|
|
* @return MongoCollection Returns a collection object representing the new collection. |
260
|
|
|
*/ |
261
|
|
|
public function createCollection($name, $options) |
262
|
|
|
{ |
263
|
|
|
$this->db->createCollection($name, $options); |
264
|
|
|
return $this->selectCollection($name); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Drops a collection |
269
|
|
|
* |
270
|
|
|
* @link http://www.php.net/manual/en/mongodb.dropcollection.php |
271
|
|
|
* @param MongoCollection|string $coll MongoCollection or name of collection to drop. |
272
|
|
|
* @return array Returns the database response. |
273
|
|
|
* |
274
|
|
|
* @deprecated Use MongoCollection::drop() instead. |
275
|
|
|
*/ |
276
|
|
|
public function dropCollection($coll) |
277
|
|
|
{ |
278
|
|
|
if ($coll instanceof MongoCollection) { |
279
|
|
|
$coll = $coll->getName(); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return TypeConverter::convertObjectToLegacyArray($this->db->dropCollection((string) $coll)); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Get a list of collections in this database |
287
|
|
|
* |
288
|
|
|
* @link http://www.php.net/manual/en/mongodb.listcollections.php |
289
|
|
|
* @param array $options |
290
|
|
|
* @return MongoCollection[] Returns a list of MongoCollections. |
291
|
|
|
*/ |
292
|
|
|
public function listCollections(array $options = []) |
293
|
|
|
{ |
294
|
|
|
return array_map([$this, 'selectCollection'], $this->getCollectionNames($options)); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Creates a database reference |
299
|
|
|
* |
300
|
|
|
* @link http://www.php.net/manual/en/mongodb.createdbref.php |
301
|
|
|
* @param string $collection The collection to which the database reference will point. |
302
|
|
|
* @param mixed $document_or_id |
303
|
|
|
* @return array Returns a database reference array. |
304
|
|
|
*/ |
305
|
|
View Code Duplication |
public function createDBRef($collection, $document_or_id) |
|
|
|
|
306
|
|
|
{ |
307
|
|
|
if ($document_or_id instanceof \MongoId) { |
308
|
|
|
$id = $document_or_id; |
309
|
|
|
} elseif (is_object($document_or_id)) { |
310
|
|
|
if (! isset($document_or_id->_id)) { |
311
|
|
|
return null; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
$id = $document_or_id->_id; |
315
|
|
|
} elseif (is_array($document_or_id)) { |
316
|
|
|
if (! isset($document_or_id['_id'])) { |
317
|
|
|
return null; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$id = $document_or_id['_id']; |
321
|
|
|
} else { |
322
|
|
|
$id = $document_or_id; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
return MongoDBRef::create($collection, $id, $this->name); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Fetches the document pointed to by a database reference |
331
|
|
|
* |
332
|
|
|
* @link http://www.php.net/manual/en/mongodb.getdbref.php |
333
|
|
|
* @param array $ref A database reference. |
334
|
|
|
* @return array Returns the document pointed to by the reference. |
335
|
|
|
*/ |
336
|
|
|
public function getDBRef(array $ref) |
337
|
|
|
{ |
338
|
|
|
return MongoDBRef::get($this, $ref); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Runs JavaScript code on the database server. |
343
|
|
|
* |
344
|
|
|
* @link http://www.php.net/manual/en/mongodb.execute.php |
345
|
|
|
* @param MongoCode|string $code Code to execute. |
346
|
|
|
* @param array $args [optional] Arguments to be passed to code. |
347
|
|
|
* @return array Returns the result of the evaluation. |
348
|
|
|
*/ |
349
|
|
|
public function execute($code, array $args = []) |
350
|
|
|
{ |
351
|
|
|
return $this->command(['eval' => $code, 'args' => $args]); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Execute a database command |
356
|
|
|
* |
357
|
|
|
* @link http://www.php.net/manual/en/mongodb.command.php |
358
|
|
|
* @param array $data The query to send. |
359
|
|
|
* @param array $options |
360
|
|
|
* @return array Returns database response. |
361
|
|
|
*/ |
362
|
|
|
public function command(array $data, $options = [], &$hash = null) |
|
|
|
|
363
|
|
|
{ |
364
|
|
|
try { |
365
|
|
|
$cursor = new \MongoCommandCursor($this->connection, $this->name, $data); |
366
|
|
|
$cursor->setReadPreference($this->getReadPreference()); |
|
|
|
|
367
|
|
|
|
368
|
|
|
return iterator_to_array($cursor)[0]; |
369
|
|
|
} catch (\MongoDB\Driver\Exception\RuntimeException $e) { |
|
|
|
|
370
|
|
|
return [ |
371
|
|
|
'ok' => 0, |
372
|
|
|
'errmsg' => $e->getMessage(), |
373
|
|
|
'code' => $e->getCode(), |
374
|
|
|
]; |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Check if there was an error on the most recent db operation performed |
380
|
|
|
* |
381
|
|
|
* @link http://www.php.net/manual/en/mongodb.lasterror.php |
382
|
|
|
* @return array Returns the error, if there was one. |
383
|
|
|
*/ |
384
|
|
|
public function lastError() |
385
|
|
|
{ |
386
|
|
|
return $this->command(array('getLastError' => 1)); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Checks for the last error thrown during a database operation |
391
|
|
|
* |
392
|
|
|
* @link http://www.php.net/manual/en/mongodb.preverror.php |
393
|
|
|
* @return array Returns the error and the number of operations ago it occurred. |
394
|
|
|
*/ |
395
|
|
|
public function prevError() |
396
|
|
|
{ |
397
|
|
|
return $this->command(array('getPrevError' => 1)); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Clears any flagged errors on the database |
402
|
|
|
* |
403
|
|
|
* @link http://www.php.net/manual/en/mongodb.reseterror.php |
404
|
|
|
* @return array Returns the database response. |
405
|
|
|
*/ |
406
|
|
|
public function resetError() |
407
|
|
|
{ |
408
|
|
|
return $this->command(array('resetError' => 1)); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Creates a database error |
413
|
|
|
* |
414
|
|
|
* @link http://www.php.net/manual/en/mongodb.forceerror.php |
415
|
|
|
* @return boolean Returns the database response. |
416
|
|
|
*/ |
417
|
|
|
public function forceError() |
418
|
|
|
{ |
419
|
|
|
return $this->command(array('forceerror' => 1)); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Log in to this database |
424
|
|
|
* |
425
|
|
|
* @link http://www.php.net/manual/en/mongodb.authenticate.php |
426
|
|
|
* @param string $username The username. |
427
|
|
|
* @param string $password The password (in plaintext). |
428
|
|
|
* @return array Returns database response. If the login was successful, it will return 1. |
429
|
|
|
*/ |
430
|
|
|
public function authenticate($username, $password) |
|
|
|
|
431
|
|
|
{ |
432
|
|
|
$this->notImplemented(); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* {@inheritdoc} |
437
|
|
|
*/ |
438
|
|
|
public function setReadPreference($readPreference, $tags = null) |
439
|
|
|
{ |
440
|
|
|
$result = $this->setReadPreferenceFromParameters($readPreference, $tags); |
441
|
|
|
$this->createDatabaseObject(); |
442
|
|
|
|
443
|
|
|
return $result; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* {@inheritdoc} |
448
|
|
|
*/ |
449
|
|
|
public function setWriteConcern($wstring, $wtimeout = 0) |
450
|
|
|
{ |
451
|
|
|
$result = $this->setWriteConcernFromParameters($wstring, $wtimeout); |
452
|
|
|
$this->createDatabaseObject(); |
453
|
|
|
|
454
|
|
|
return $result; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
protected function notImplemented() |
458
|
|
|
{ |
459
|
|
|
throw new \Exception('Not implemented'); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @return \MongoDB\Database |
464
|
|
|
*/ |
465
|
|
View Code Duplication |
private function createDatabaseObject() |
|
|
|
|
466
|
|
|
{ |
467
|
|
|
$options = [ |
468
|
|
|
'readPreference' => $this->readPreference, |
469
|
|
|
'writeConcern' => $this->writeConcern, |
470
|
|
|
]; |
471
|
|
|
|
472
|
|
|
if ($this->db === null) { |
473
|
|
|
$this->db = $this->connection->getClient()->selectDatabase($this->name, $options); |
474
|
|
|
} else { |
475
|
|
|
$this->db = $this->db->withOptions($options); |
476
|
|
|
} |
477
|
|
|
} |
478
|
|
|
} |
479
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.