This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 326
/
db.c
1581 lines (1284 loc) · 45.5 KB
/
db.c
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2009-2014 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <php.h>
#include <zend_exceptions.h>
#include <ext/standard/md5.h>
#include <ext/standard/php_smart_str.h>
#include "php_mongo.h"
#include "db.h"
#include "collection.h"
#include "cursor.h"
#include "command_cursor.h"
#include "cursor_shared.h"
#include "gridfs/gridfs.h"
#include "types/code.h"
#include "types/db_ref.h"
#include "mcon/manager.h"
#include "api/wire_version.h"
#ifndef zend_parse_parameters_none
#define zend_parse_parameters_none() \
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "")
#endif
extern zend_class_entry *mongo_ce_MongoClient, *mongo_ce_Collection;
extern zend_class_entry *mongo_ce_Cursor, *mongo_ce_GridFS, *mongo_ce_Id;
extern zend_class_entry *mongo_ce_Code, *mongo_ce_Exception;
extern zend_class_entry *mongo_ce_CursorException, *mongo_ce_Int64;
extern zend_class_entry *mongo_ce_ConnectionException, *mongo_ce_ResultException;
extern zend_object_handlers mongo_default_handlers;
zend_class_entry *mongo_ce_DB = NULL;
static void clear_exception(zval* return_value TSRMLS_DC);
static int php_mongo_command_supports_rp(zval *cmd)
{
HashPosition pos;
char *str;
uint str_len;
long type;
ulong idx;
if (!cmd || Z_TYPE_P(cmd) != IS_ARRAY) {
return 0;
}
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(cmd), &pos);
type = zend_hash_get_current_key_ex(Z_ARRVAL_P(cmd), &str, &str_len, &idx, 0, &pos);
if (type != HASH_KEY_IS_STRING) {
return 0;
}
/* Commands in MongoDB are case-sensitive */
if (str_len == 6) {
if (strcmp(str, "count") == 0 || strcmp(str, "group") == 0) {
return 1;
}
return 0;
}
if (str_len == 8) {
if (strcmp(str, "dbStats") == 0 || strcmp(str, "geoNear") == 0 || strcmp(str, "geoWalk") == 0) {
return 1;
}
return 0;
}
if (str_len == 9) {
if (strcmp(str, "distinct") == 0) {
return 1;
}
return 0;
}
if (str_len == 10) {
if (strcmp(str, "aggregate") == 0 || strcmp(str, "collStats") == 0 || strcmp(str, "geoSearch") == 0) {
return 1;
}
if (strcmp(str, "mapreduce") == 0 || strcmp(str, "mapReduce") == 0) {
zval **value = NULL;
if (zend_hash_find(Z_ARRVAL_P(cmd), "out", 4, (void **)&value) == SUCCESS) {
if (Z_TYPE_PP(value) == IS_ARRAY) {
if (zend_hash_exists(Z_ARRVAL_PP(value), "inline", 7)) {
return 1;
}
}
}
}
return 0;
}
if (str_len == 23) {
if (strcmp(str, "parallelCollectionScan") == 0) {
return 1;
}
return 0;
}
return 0;
}
int php_mongo_db_is_valid_dbname(char *dbname, int dbname_len TSRMLS_DC)
{
if (dbname_len == 0) {
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name cannot be empty");
return 0;
}
if (dbname_len >= 64) {
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name cannot exceed 63 characters: %s", dbname);
return 0;
}
if (memchr(dbname, '\0', dbname_len) != NULL) {
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name cannot contain null bytes: %s\\0...", dbname);
return 0;
}
/* We allow the special case "$external" as database name (PHP-1431) */
if (strcmp("$external", dbname) == 0) {
return 1;
}
if (
memchr(dbname, ' ', dbname_len) != 0 || memchr(dbname, '.', dbname_len) != 0 || memchr(dbname, '\\', dbname_len) != 0 ||
memchr(dbname, '/', dbname_len) != 0 || memchr(dbname, '$', dbname_len) != 0
) {
zend_throw_exception_ex(mongo_ce_Exception, 2 TSRMLS_CC, "Database name contains invalid characters: %s", dbname);
return 0;
}
return 1;
}
void php_mongo_db_construct(zval *z_client, zval *zlink, char *name, int name_len TSRMLS_DC)
{
mongo_db *db;
mongoclient *link;
if (!php_mongo_db_is_valid_dbname(name, name_len TSRMLS_CC)) {
return;
}
db = (mongo_db*)zend_object_store_get_object(z_client TSRMLS_CC);
db->link = zlink;
zval_add_ref(&db->link);
link = (mongoclient*)zend_object_store_get_object(zlink TSRMLS_CC);
if (!(link->servers)) {
zend_throw_exception(mongo_ce_Exception, "The MongoDB object has not been correctly initialized by its constructor", 0 TSRMLS_CC);
return;
}
if (link->servers->options.default_w != -1) {
zend_update_property_long(mongo_ce_DB, z_client, "w", strlen("w"), link->servers->options.default_w TSRMLS_CC);
} else if (link->servers->options.default_wstring != NULL) {
zend_update_property_string(mongo_ce_DB, z_client, "w", strlen("w"), link->servers->options.default_wstring TSRMLS_CC);
}
if (link->servers->options.default_wtimeout != -1) {
zend_update_property_long(mongo_ce_DB, z_client, "wtimeout", strlen("wtimeout"), link->servers->options.default_wtimeout TSRMLS_CC);
}
mongo_read_preference_copy(&link->servers->read_pref, &db->read_pref);
MAKE_STD_ZVAL(db->name);
ZVAL_STRING(db->name, name, 1);
}
/* {{{ proto void MongoDB::__construct(MongoClient client, string name)
Constructs a new MongoDB instance */
PHP_METHOD(MongoDB, __construct)
{
zval *zlink;
char *name;
int name_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &zlink, mongo_ce_MongoClient, &name, &name_len) == FAILURE) {
zval *object = getThis();
ZVAL_NULL(object);
return;
}
php_mongo_db_construct(getThis(), zlink, name, name_len TSRMLS_CC);
}
/* }}} */
PHP_METHOD(MongoDB, __toString)
{
mongo_db *db = (mongo_db*)zend_object_store_get_object(getThis() TSRMLS_CC);
MONGO_CHECK_INITIALIZED_STRING(db->name, MongoDB);
RETURN_ZVAL(db->name, 1, 0);
}
/* Selects a collection and returns it as zval. If the return value is no, an
* Exception is set. This only happens if the passed in DB was invalid. */
zval *php_mongo_db_selectcollection(zval *z_client, char *collection, int collection_len TSRMLS_DC)
{
zval *z_collection;
zval *return_value;
mongo_db *db;
db = (mongo_db*)zend_object_store_get_object(z_client TSRMLS_CC);
if (!(db->name)) {
zend_throw_exception(mongo_ce_Exception, "The MongoDB object has not been correctly initialized by its constructor", 0 TSRMLS_CC);
return NULL;
}
MAKE_STD_ZVAL(z_collection);
ZVAL_STRINGL(z_collection, collection, collection_len, 1);
MAKE_STD_ZVAL(return_value);
object_init_ex(return_value, mongo_ce_Collection);
php_mongo_collection_construct(return_value, z_client, collection, collection_len TSRMLS_CC);
if (EG(exception)) {
zval_ptr_dtor(&return_value);
return_value = NULL;
}
zval_ptr_dtor(&z_collection);
return return_value;
}
/* {{{ proto MongoCollection MongoDB::selectCollection(string name)
Returns the "name" collection from the database */
PHP_METHOD(MongoDB, selectCollection)
{
char *name;
int name_len;
zval *collection;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
return;
}
collection = php_mongo_db_selectcollection(getThis(), name, name_len TSRMLS_CC);
if (collection) {
/* Only copy the zval into return_value if it worked. If collection is
* NULL here, an exception is set */
RETURN_ZVAL(collection, 0, 1);
}
}
/* }}} */
PHP_METHOD(MongoDB, getGridFS)
{
zval temp;
zval *arg1 = 0, *arg2 = 0;
/* arg2 is deprecated */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zz", &arg1, &arg2) == FAILURE) {
return;
}
if (arg2) {
php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "The 'chunks' argument is deprecated and ignored");
}
object_init_ex(return_value, mongo_ce_GridFS);
if (!arg1) {
MONGO_METHOD1(MongoGridFS, __construct, &temp, return_value, getThis());
} else {
MONGO_METHOD2(MongoGridFS, __construct, &temp, return_value, getThis(), arg1);
}
}
PHP_METHOD(MongoDB, getSlaveOkay)
{
mongo_db *db;
PHP_MONGO_GET_DB(getThis());
RETURN_BOOL(db->read_pref.type != MONGO_RP_PRIMARY);
}
PHP_METHOD(MongoDB, setSlaveOkay)
{
zend_bool slave_okay = 1;
mongo_db *db;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &slave_okay) == FAILURE) {
return;
}
PHP_MONGO_GET_DB(getThis());
RETVAL_BOOL(db->read_pref.type != MONGO_RP_PRIMARY);
db->read_pref.type = slave_okay ? MONGO_RP_SECONDARY_PREFERRED : MONGO_RP_PRIMARY;
}
PHP_METHOD(MongoDB, getReadPreference)
{
mongo_db *db;
PHP_MONGO_GET_DB(getThis());
array_init(return_value);
add_assoc_string(return_value, "type", mongo_read_preference_type_to_name(db->read_pref.type), 1);
php_mongo_add_tagsets(return_value, &db->read_pref);
}
/* {{{ MongoDB::setReadPreference(string read_preference [, array tags ])
* Sets a read preference to be used for all read queries.*/
PHP_METHOD(MongoDB, setReadPreference)
{
char *read_preference;
int read_preference_len;
mongo_db *db;
HashTable *tags = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|h", &read_preference, &read_preference_len, &tags) == FAILURE) {
return;
}
PHP_MONGO_GET_DB(getThis());
if (php_mongo_set_readpreference(&db->read_pref, read_preference, tags TSRMLS_CC)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ array MongoDB::getWriteConcern()
* Get the MongoDB write concern, which will be inherited by constructed
* MongoCollection objects. */
PHP_METHOD(MongoDB, getWriteConcern)
{
zval *write_concern, *wtimeout;
if (zend_parse_parameters_none()) {
return;
}
write_concern = zend_read_property(mongo_ce_DB, getThis(), "w", strlen("w"), 0 TSRMLS_CC);
wtimeout = zend_read_property(mongo_ce_DB, getThis(), "wtimeout", strlen("wtimeout"), 0 TSRMLS_CC);
Z_ADDREF_P(write_concern);
Z_ADDREF_P(wtimeout);
array_init(return_value);
add_assoc_zval(return_value, "w", write_concern);
add_assoc_zval(return_value, "wtimeout", wtimeout);
}
/* }}} */
/* {{{ bool MongoDB::setWriteConcern(mixed w [, int wtimeout])
* Set the MongoDB write concern, which will be inherited by constructed
* MongoCollection objects. */
PHP_METHOD(MongoDB, setWriteConcern)
{
zval *write_concern;
long wtimeout;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &write_concern, &wtimeout) == FAILURE) {
return;
}
if (Z_TYPE_P(write_concern) == IS_LONG) {
zend_update_property_long(mongo_ce_DB, getThis(), "w", strlen("w"), Z_LVAL_P(write_concern) TSRMLS_CC);
} else if (Z_TYPE_P(write_concern) == IS_STRING) {
zend_update_property_stringl(mongo_ce_DB, getThis(), "w", strlen("w"), Z_STRVAL_P(write_concern), Z_STRLEN_P(write_concern) TSRMLS_CC);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects parameter 1 to be an string or integer, %s given", zend_get_type_by_const(Z_TYPE_P(write_concern)));
RETURN_FALSE;
}
if (ZEND_NUM_ARGS() > 1) {
zend_update_property_long(mongo_ce_DB, getThis(), "wtimeout", strlen("wtimeout"), wtimeout TSRMLS_CC);
}
RETURN_TRUE;
}
/* }}} */
static void php_mongo_db_profiling_level(INTERNAL_FUNCTION_PARAMETERS, int get)
{
long level;
zval *cmd, *cmd_return;
zval **ok;
mongo_db *db;
if (get) {
if (zend_parse_parameters_none() == FAILURE) {
return;
}
level = -1;
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &level) == FAILURE) {
return;
}
}
PHP_MONGO_GET_DB(getThis());
MAKE_STD_ZVAL(cmd);
array_init(cmd);
add_assoc_long(cmd, "profile", level);
cmd_return = php_mongo_runcommand(db->link, &db->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), cmd, NULL, 0, NULL TSRMLS_CC);
zval_ptr_dtor(&cmd);
if (!cmd_return) {
return;
}
if (
zend_hash_find(HASH_P(cmd_return), "ok", 3, (void**)&ok) == SUCCESS &&
((Z_TYPE_PP(ok) == IS_BOOL && Z_BVAL_PP(ok)) || Z_DVAL_PP(ok) == 1)
) {
zend_hash_find(HASH_P(cmd_return), "was", 4, (void**)&ok);
RETVAL_ZVAL(*ok, 1, 0);
} else {
RETVAL_NULL();
}
zval_ptr_dtor(&cmd_return);
}
/* {{{ proto array MongoDB::getProfilingLevel()
Gets this database's profiling level. */
PHP_METHOD(MongoDB, getProfilingLevel)
{
php_mongo_db_profiling_level(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ proto array MongoDB::setProfilingLevel(int level)
Sets this database's profiling level. */
PHP_METHOD(MongoDB, setProfilingLevel)
{
php_mongo_db_profiling_level(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
PHP_METHOD(MongoDB, drop)
{
zval *cmd, *retval;
mongo_db *db;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
PHP_MONGO_GET_DB(getThis());
MAKE_STD_ZVAL(cmd);
array_init(cmd);
add_assoc_long(cmd, "dropDatabase", 1);
retval = php_mongo_runcommand(db->link, &db->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), cmd, NULL, 0, NULL TSRMLS_CC);
zval_ptr_dtor(&cmd);
if (retval) {
RETURN_ZVAL(retval, 0, 1);
}
}
PHP_METHOD(MongoDB, repair)
{
zend_bool cloned=0, original=0;
zval *cmd, *retval;
mongo_db *db;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &cloned, &original) == FAILURE) {
return;
}
PHP_MONGO_GET_DB(getThis());
MAKE_STD_ZVAL(cmd);
array_init(cmd);
add_assoc_long(cmd, "repairDatabase", 1);
add_assoc_bool(cmd, "preserveClonedFilesOnFailure", cloned);
add_assoc_bool(cmd, "backupOriginalFiles", original);
retval = php_mongo_runcommand(db->link, &db->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), cmd, NULL, 0, NULL TSRMLS_CC);
zval_ptr_dtor(&cmd);
if (retval) {
RETVAL_ZVAL(retval, 0, 1);
}
}
PHP_METHOD(MongoDB, createCollection)
{
zval *cmd = NULL, *temp, *options = NULL;
char *collection;
int collection_len;
zend_bool capped = 0;
long size = 0, max = 0;
mongo_db *db;
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "s|bll", &collection, &collection_len, &capped, &size, &max) == SUCCESS) {
MAKE_STD_ZVAL(cmd);
array_init(cmd);
add_assoc_stringl(cmd, "create", collection, collection_len, 1);
if (size) {
add_assoc_long(cmd, "size", size);
}
if (capped) {
php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "This method now accepts arguments as an options array instead of the three optional arguments for capped, size and max elements");
add_assoc_bool(cmd, "capped", 1);
if (max) {
add_assoc_long(cmd, "max", max);
}
}
} else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a", &collection, &collection_len, &options) == SUCCESS) {
zval *tmp_copy;
/* We create a new array here, instead of just tagging "create" =>
* <name> at the end of the array. This is because MongoDB wants the
* name of the command as first element in the array. */
MAKE_STD_ZVAL(cmd);
array_init(cmd);
add_assoc_stringl(cmd, "create", collection, collection_len, 1);
if (options) {
zend_hash_merge(Z_ARRVAL_P(cmd), Z_ARRVAL_P(options), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0);
}
} else {
return;
}
PHP_MONGO_GET_DB(getThis());
temp = php_mongo_runcommand(db->link, &db->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), cmd, options, 0, NULL TSRMLS_CC);
zval_ptr_dtor(&cmd);
if (temp) {
zval_ptr_dtor(&temp);
}
if (!EG(exception)) {
zval *zcollection;
/* get the collection we just created */
zcollection = php_mongo_db_selectcollection(getThis(), collection, collection_len TSRMLS_CC);
if (zcollection) {
/* Only copy the zval into return_value if it worked. If
* zcollection is NULL here, an exception is set */
RETURN_ZVAL(zcollection, 0, 1);
}
}
}
/* {{{ proto MongoCollection MongoDB::dropCollection(string|MongoCollection collection)
Drops a collection and returns the database's response */
PHP_METHOD(MongoDB, dropCollection)
{
zval *collection;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &collection) == FAILURE) {
return;
}
if (Z_TYPE_P(collection) == IS_STRING) {
collection = php_mongo_db_selectcollection(getThis(), Z_STRVAL_P(collection), Z_STRLEN_P(collection) TSRMLS_CC);
if (!collection) {
/* An exception is set in this case */
return;
}
} else if (Z_TYPE_P(collection) == IS_OBJECT && Z_OBJCE_P(collection) == mongo_ce_Collection) {
zval_add_ref(&collection);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "expects parameter 1 to be an string or MongoCollection");
return;
}
php_mongocollection_drop(collection, return_value TSRMLS_CC);
zval_ptr_dtor(&collection);
}
/* }}} */
static void mongo_db_list_collections_command(zval *this_ptr, zval *options, int return_type, zval *return_value TSRMLS_DC)
{
zend_bool include_system_collections = 0;
zval *z_cmd, *list, **collections;
mongo_db *db;
mongo_connection *connection;
zval *cursor_env, *retval;
zval *tmp_iterator, *exception;
mongo_cursor *cmd_cursor;
MAKE_STD_ZVAL(z_cmd);
array_init(z_cmd);
add_assoc_long(z_cmd, "listCollections", 1);
if (options) {
zval *temp, **include_system_collections_pp;
/* "includeSystemCollections" should not be included in the command document */
if (zend_hash_find(HASH_P(options), ZEND_STRS("includeSystemCollections"), (void**)&include_system_collections_pp) == SUCCESS) {
convert_to_boolean(*include_system_collections_pp);
include_system_collections = Z_BVAL_PP(include_system_collections_pp);
zend_hash_del(HASH_P(options), "includeSystemCollections", strlen("includeSystemCollections") + 1);
}
zend_hash_merge(HASH_P(z_cmd), HASH_P(options), (copy_ctor_func_t) zval_add_ref, &temp, sizeof(zval*), 1);
}
/* Ensure that the command's cursor option, if set, is valid. If this fails,
* EG(exception) is set. */
if (!php_mongo_validate_cursor_on_command(z_cmd TSRMLS_CC)) {
zval_ptr_dtor(&z_cmd);
return;
}
PHP_MONGO_GET_DB(getThis());
retval = php_mongo_runcommand(db->link, &db->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), z_cmd, NULL, 0, &connection TSRMLS_CC);
zval_ptr_dtor(&z_cmd);
if (!retval) {
return;
}
if (php_mongo_trigger_error_on_command_failure(connection, retval TSRMLS_CC) == FAILURE) {
RETURN_ZVAL(retval, 0, 1);
}
/* list to return */
MAKE_STD_ZVAL(list);
array_init(list);
/* Handle inline command response from server >= 2.7.5 and < 2.8.0-RC3. */
if (zend_hash_find(Z_ARRVAL_P(retval), "collections", strlen("collections") + 1, (void **)&collections) == SUCCESS) {
HashPosition pointer;
zval **collection_doc;
for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(collections), &pointer);
zend_hash_get_current_data_ex(Z_ARRVAL_PP(collections), (void**)&collection_doc, &pointer) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_PP(collections), &pointer)
) {
zval *c;
zval **collection_name;
char *system;
if (zend_hash_find(Z_ARRVAL_PP(collection_doc), "name", 5, (void**)&collection_name) == FAILURE) {
continue;
}
/* check that this isn't a system ns */
system = strstr(Z_STRVAL_PP(collection_name), "system.");
if (
(!include_system_collections && (system == Z_STRVAL_PP(collection_name)))
) {
continue;
}
switch (return_type) {
case MONGO_COLLECTION_RETURN_TYPE_NAME:
add_next_index_string(list, Z_STRVAL_PP(collection_name), 1);
break;
case MONGO_COLLECTION_RETURN_TYPE_OBJECT:
c = php_mongo_db_selectcollection(this_ptr, Z_STRVAL_PP(collection_name), Z_STRLEN_PP(collection_name) TSRMLS_CC);
add_next_index_zval(list, c);
break;
case MONGO_COLLECTION_RETURN_TYPE_INFO_ARRAY:
Z_ADDREF_P(*collection_doc);
add_assoc_zval(list, Z_STRVAL_PP(collection_name), *collection_doc);
break;
}
}
zval_ptr_dtor(&retval);
RETURN_ZVAL(list, 0, 1);
}
MAKE_STD_ZVAL(tmp_iterator);
php_mongo_commandcursor_instantiate(tmp_iterator TSRMLS_CC);
cmd_cursor = (mongo_command_cursor*) zend_object_store_get_object(tmp_iterator TSRMLS_CC);
/* We need to parse the initial result, and find the "cursor" element in the result */
if (php_mongo_get_cursor_info_envelope(retval, &cursor_env TSRMLS_CC) == FAILURE) {
exception = php_mongo_cursor_throw(mongo_ce_CursorException, cmd_cursor->connection, 30 TSRMLS_CC, "the command cursor did not return a correctly structured response");
zend_update_property(mongo_ce_CursorException, exception, "doc", strlen("doc"), retval TSRMLS_CC);
zval_ptr_dtor(&tmp_iterator);
return;
}
php_mongo_command_cursor_init_from_document(db->link, cmd_cursor, connection->hash, cursor_env TSRMLS_CC);
/* TODO: Refactor to use MongoCommandCursor::rewind() once it is extracted
* into its own C function (see PHP-1362) */
php_mongocommandcursor_fetch_batch_if_first_is_empty(cmd_cursor TSRMLS_CC);
cmd_cursor->started_iterating = 1;
for (
php_mongocommandcursor_load_current_element(cmd_cursor TSRMLS_CC);
php_mongocommandcursor_is_valid(cmd_cursor);
php_mongocommandcursor_advance(cmd_cursor TSRMLS_CC)
) {
zval **collection_name;
zval **collection_doc = &cmd_cursor->current;
char *system;
if (zend_hash_find(Z_ARRVAL_PP(collection_doc), "name", 5, (void**)&collection_name) == FAILURE) {
continue;
}
/* check that this isn't a system ns */
system = strstr(Z_STRVAL_PP(collection_name), "system.");
if (
(!include_system_collections && (system == Z_STRVAL_PP(collection_name)))
) {
continue;
}
switch (return_type) {
case MONGO_COLLECTION_RETURN_TYPE_NAME:
add_next_index_string(list, Z_STRVAL_PP(collection_name), 1);
break;
case MONGO_COLLECTION_RETURN_TYPE_OBJECT: {
zval *c = php_mongo_db_selectcollection(this_ptr, Z_STRVAL_PP(collection_name), Z_STRLEN_PP(collection_name) TSRMLS_CC);
add_next_index_zval(list, c);
break;
}
case MONGO_COLLECTION_RETURN_TYPE_INFO_ARRAY:
Z_ADDREF_P(*collection_doc);
add_next_index_zval(list, *collection_doc);
break;
}
}
zval_ptr_dtor(&retval);
zval_ptr_dtor(&tmp_iterator);
RETURN_ZVAL(list, 0, 1);
}
static void mongo_db_list_collections_legacy(zval *this_ptr, zval *options, int return_type, zval *return_value TSRMLS_DC)
{
zend_bool include_system_collections = 0;
zval *z_system_collection, *z_cursor, *list, *filter = NULL;
mongo_cursor *cursor;
mongo_collection *collection;
if (options) {
zval **include_system_collections_pp, **filter_pp;
if (zend_hash_find(HASH_P(options), ZEND_STRS("includeSystemCollections"), (void**)&include_system_collections_pp) == SUCCESS) {
convert_to_boolean(*include_system_collections_pp);
include_system_collections = Z_BVAL_PP(include_system_collections_pp);
}
if (zend_hash_find(HASH_P(options), ZEND_STRS("filter"), (void**)&filter_pp) == SUCCESS) {
if (Z_TYPE_PP(filter_pp) != IS_ARRAY && Z_TYPE_PP(filter_pp) != IS_OBJECT) {
zend_throw_exception_ex(mongo_ce_Exception, 26 TSRMLS_CC, "Expected filter to be array or object, %s given", zend_get_type_by_const(Z_TYPE_PP(filter_pp)));
RETURN_NULL();
}
filter = *filter_pp;
}
}
if (filter) {
zval **name_pp;
if (zend_hash_find(HASH_P(filter), ZEND_STRS("name"), (void**)&name_pp) == SUCCESS) {
mongo_db *db;
int prefixed_name_len;
char *prefixed_name;
if (Z_TYPE_PP(name_pp) != IS_STRING) {
zend_throw_exception_ex(mongo_ce_Exception, 27 TSRMLS_CC, "Filter \"name\" must be a string for MongoDB <2.8, %s given", zend_get_type_by_const(Z_TYPE_PP(name_pp)));
RETURN_NULL();
}
db = (mongo_db*)zend_object_store_get_object(getThis() TSRMLS_CC);
if (!(db->name)) {
zend_throw_exception(mongo_ce_Exception, "The MongoDB object has not been correctly initialized by its constructor", 0 TSRMLS_CC);
RETURN_NULL();
}
prefixed_name_len = spprintf(&prefixed_name, 0, "%s.%s", Z_STRVAL_P(db->name), Z_STRVAL_PP(name_pp));
add_assoc_stringl(filter, "name", prefixed_name, prefixed_name_len, 1);
efree(prefixed_name);
}
}
/* select db.system.namespaces collection */
z_system_collection = php_mongo_db_selectcollection(this_ptr, "system.namespaces", strlen("system.namespaces") TSRMLS_CC);
if (!z_system_collection) {
/* An exception is set in this case */
return;
}
/* list to return */
MAKE_STD_ZVAL(list);
array_init(list);
/* do find */
MAKE_STD_ZVAL(z_cursor);
object_init_ex(z_cursor, mongo_ce_Cursor);
cursor = (mongo_cursor*)zend_object_store_get_object(z_cursor TSRMLS_CC);
collection = (mongo_collection*)zend_object_store_get_object(z_system_collection TSRMLS_CC);
php_mongo_collection_find(cursor, collection, filter, NULL TSRMLS_CC);
php_mongo_runquery(cursor TSRMLS_CC);
if (EG(exception)) {
zval_ptr_dtor(&z_cursor);
zval_ptr_dtor(&z_system_collection);
zval_ptr_dtor(&list);
RETURN_NULL();
}
/* populate list */
php_mongocursor_load_current_element(cursor TSRMLS_CC);
if (php_mongo_handle_error(cursor TSRMLS_CC)) {
zval_ptr_dtor(&z_cursor);
zval_ptr_dtor(&z_system_collection);
zval_ptr_dtor(&list);
RETURN_NULL();
}
while (php_mongocursor_is_valid(cursor)) {
zval *c;
zval **collection_name;
char *name, *first_dot, *system;
/* check that the ns is valid and not an index (contains $) */
if (
zend_hash_find(HASH_P(cursor->current), "name", 5, (void**)&collection_name) == FAILURE ||
(
Z_TYPE_PP(collection_name) == IS_STRING &&
strchr(Z_STRVAL_PP(collection_name), '$')
)
) {
php_mongocursor_advance(cursor TSRMLS_CC);
continue;
}
/* check that this isn't a system ns */
first_dot = strchr(Z_STRVAL_PP(collection_name), '.');
system = strstr(Z_STRVAL_PP(collection_name), ".system.");
if (
(!include_system_collections && (system && first_dot == system)) ||
(name = strchr(Z_STRVAL_PP(collection_name), '.')) == 0)
{
php_mongocursor_advance(cursor TSRMLS_CC);
continue;
}
/* take a substring after the first "." */
name++;
/* "foo." was allowed in earlier versions */
if (*name == '\0') {
php_mongocursor_advance(cursor TSRMLS_CC);
continue;
}
switch (return_type) {
case MONGO_COLLECTION_RETURN_TYPE_NAME:
add_next_index_string(list, name, 1);
break;
case MONGO_COLLECTION_RETURN_TYPE_OBJECT:
c = php_mongo_db_selectcollection(this_ptr, name, strlen(name) TSRMLS_CC);
/* No need to test for c here, as this was already covered in
* system_collection above */
add_next_index_zval(list, c);
break;
case MONGO_COLLECTION_RETURN_TYPE_INFO_ARRAY:
Z_ADDREF_P(cursor->current);
add_next_index_zval(list, cursor->current);
/* Replace the collection's namespace with the trimmed name */
add_assoc_string(cursor->current, "name", name, 1);
break;
}
php_mongocursor_advance(cursor TSRMLS_CC);
}
zval_ptr_dtor(&z_cursor);
zval_ptr_dtor(&z_system_collection);
RETURN_ZVAL(list, 0, 1);
}
/* Return types:
* 0: Collection name
* 1: MongoCollection object
* 2: Array containing collection name and options
*/
static void php_mongo_enumerate_collections(INTERNAL_FUNCTION_PARAMETERS, int return_type)
{
zval *options = NULL;
mongo_connection *connection;
mongo_db *db;
mongoclient *link;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &options) == FAILURE) {
return;
}
PHP_MONGO_GET_DB(getThis());
PHP_MONGO_GET_LINK(db->link);
if ((connection = php_mongo_collection_get_server(link, MONGO_CON_FLAG_WRITE TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
/* Convert non-hash parameter to "includeSystemCollections" option */
if (options && Z_TYPE_P(options) != IS_ARRAY && Z_TYPE_P(options) != IS_OBJECT) {
zend_bool include_system_collections;
convert_to_boolean(options);
include_system_collections = Z_BVAL_P(options);
array_init(options);
add_assoc_bool(options, "includeSystemCollections", include_system_collections);
}
if (php_mongo_api_connection_min_server_version(connection, 2, 7, 5)) {
mongo_db_list_collections_command(getThis(), options, return_type, return_value TSRMLS_CC);
} else {
mongo_db_list_collections_legacy(getThis(), options, return_type, return_value TSRMLS_CC);
}
}
PHP_METHOD(MongoDB, listCollections)
{
php_mongo_enumerate_collections(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
PHP_METHOD(MongoDB, getCollectionInfo)
{
php_mongo_enumerate_collections(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
}
PHP_METHOD(MongoDB, getCollectionNames)
{
php_mongo_enumerate_collections(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
PHP_METHOD(MongoDB, createDBRef)
{
char *collection;
int collection_len;
zval *obj, *retval = NULL;
mongo_db *db;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &collection, &collection_len, &obj) == FAILURE) {
return;
}
PHP_MONGO_GET_DB(getThis());
if (
(obj = php_mongo_dbref_resolve_id(obj TSRMLS_CC)) &&
(retval = php_mongo_dbref_create(obj, collection, NULL TSRMLS_CC))
) {
RETURN_ZVAL(retval, 0, 1);
}
RETURN_NULL();
}
PHP_METHOD(MongoDB, getDBRef)
{
zval *ref;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &ref) == FAILURE) {
return;
}
MUST_BE_ARRAY_OR_OBJECT(1, ref);