/
entities.php
2335 lines (2018 loc) · 61.9 KB
/
entities.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
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
<?php
/**
* Procedural code for creating, loading, and modifying ElggEntity objects.
*
* @package Elgg.Core
* @subpackage DataModel.Entities
* @link http://docs.elgg.org/DataModel/Entities
*/
/**
* Cache entities in memory once loaded.
*
* @global array $ENTITY_CACHE
* @access private
*/
global $ENTITY_CACHE;
$ENTITY_CACHE = array();
/**
* Cache subtypes and related class names once loaded.
*
* @global array $SUBTYPE_CACHE
* @access private
*/
global $SUBTYPE_CACHE;
$SUBTYPE_CACHE = NULL;
/**
* Invalidate this class's entry in the cache.
*
* @param int $guid The entity guid
*
* @return void
* @access private
*/
function invalidate_cache_for_entity($guid) {
global $ENTITY_CACHE;
$guid = (int)$guid;
unset($ENTITY_CACHE[$guid]);
}
/**
* Cache an entity.
*
* Stores an entity in $ENTITY_CACHE;
*
* @param ElggEntity $entity Entity to cache
*
* @return void
* @see retrieve_cached_entity()
* @see invalidate_cache_for_entity()
* @access private
*/
function cache_entity(ElggEntity $entity) {
global $ENTITY_CACHE;
$ENTITY_CACHE[$entity->guid] = $entity;
}
/**
* Retrieve a entity from the cache.
*
* @param int $guid The guid
*
* @return void
* @see cache_entity()
* @see invalidate_cache_for_entity()
* @access private
*/
function retrieve_cached_entity($guid) {
global $ENTITY_CACHE;
$guid = (int)$guid;
if (isset($ENTITY_CACHE[$guid])) {
if ($ENTITY_CACHE[$guid]->isFullyLoaded()) {
return $ENTITY_CACHE[$guid];
}
}
return false;
}
/**
* As retrieve_cached_entity, but returns the result as a stdClass
* (compatible with load functions that expect a database row.)
*
* @param int $guid The guid
*
* @return mixed
* @todo unused
* @access private
*/
function retrieve_cached_entity_row($guid) {
$obj = retrieve_cached_entity($guid);
if ($obj) {
$tmp = new stdClass;
foreach ($obj as $k => $v) {
$tmp->$k = $v;
}
return $tmp;
}
return false;
}
/**
* Return the id for a given subtype.
*
* ElggEntity objects have a type and a subtype. Subtypes
* are defined upon creation and cannot be changed.
*
* Plugin authors generally don't need to use this function
* unless writing their own SQL queries. Use {@link ElggEntity::getSubtype()}
* to return the string subtype.
*
* @warning {@link ElggEntity::subtype} returns the ID. You probably want
* {@link ElggEntity::getSubtype()} instead!
*
* @internal Subtypes are stored in the entity_subtypes table. There is a foreign
* key in the entities table.
*
* @param string $type Type
* @param string $subtype Subtype
*
* @return int Subtype ID
* @link http://docs.elgg.org/DataModel/Entities/Subtypes
* @see get_subtype_from_id()
* @access private
*/
function get_subtype_id($type, $subtype) {
global $CONFIG, $SUBTYPE_CACHE;
$type = sanitise_string($type);
$subtype = sanitise_string($subtype);
if ($subtype == "") {
return FALSE;
}
// @todo use the cache before hitting database
$result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes
where type='$type' and subtype='$subtype'");
if ($result) {
if (!$SUBTYPE_CACHE) {
$SUBTYPE_CACHE = array();
}
$SUBTYPE_CACHE[$result->id] = $result;
return $result->id;
}
return FALSE;
}
/**
* Return string name for a given subtype ID.
*
* @param int $subtype_id Subtype ID
*
* @return string Subtype name
* @link http://docs.elgg.org/DataModel/Entities/Subtypes
* @see get_subtype_from_id()
* @access private
*/
function get_subtype_from_id($subtype_id) {
global $CONFIG, $SUBTYPE_CACHE;
$subtype_id = (int)$subtype_id;
if (!$subtype_id) {
return false;
}
if (isset($SUBTYPE_CACHE[$subtype_id])) {
return $SUBTYPE_CACHE[$subtype_id]->subtype;
}
$result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes where id=$subtype_id");
if ($result) {
if (!$SUBTYPE_CACHE) {
$SUBTYPE_CACHE = array();
}
$SUBTYPE_CACHE[$subtype_id] = $result;
return $result->subtype;
}
return false;
}
/**
* Return the class name for a registered type and subtype.
*
* Entities can be registered to always be loaded as a certain class
* with add_subtype() or update_subtype(). This function returns the class
* name if found and NULL if not.
*
* @param string $type The type
* @param string $subtype The subtype
*
* @return string|null a class name or null
* @see get_subtype_from_id()
* @see get_subtype_class_from_id()
* @access private
*/
function get_subtype_class($type, $subtype) {
global $CONFIG, $SUBTYPE_CACHE;
$type = sanitise_string($type);
$subtype = sanitise_string($subtype);
// @todo use the cache before going to the database
$result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes
where type='$type' and subtype='$subtype'");
if ($result) {
if (!$SUBTYPE_CACHE) {
$SUBTYPE_CACHE = array();
}
$SUBTYPE_CACHE[$result->id] = $result;
return $result->class;
}
return NULL;
}
/**
* Returns the class name for a subtype id.
*
* @param int $subtype_id The subtype id
*
* @return string|null
* @see get_subtype_class()
* @see get_subtype_from_id()
* @access private
*/
function get_subtype_class_from_id($subtype_id) {
global $CONFIG, $SUBTYPE_CACHE;
$subtype_id = (int)$subtype_id;
if (!$subtype_id) {
return false;
}
if (isset($SUBTYPE_CACHE[$subtype_id])) {
return $SUBTYPE_CACHE[$subtype_id]->class;
}
$result = get_data_row("SELECT * from {$CONFIG->dbprefix}entity_subtypes where id=$subtype_id");
if ($result) {
if (!$SUBTYPE_CACHE) {
$SUBTYPE_CACHE = array();
}
$SUBTYPE_CACHE[$subtype_id] = $result;
return $result->class;
}
return NULL;
}
/**
* Register ElggEntities with a certain type and subtype to be loaded as a specific class.
*
* By default entities are loaded as one of the 4 parent objects: site, user, object, or group.
* If you subclass any of these you can register the classname with add_subtype() so
* it will be loaded as that class automatically when retrieved from the database with
* {@link get_entity()}.
*
* @warning This function cannot be used to change the class for a type-subtype pair.
* Use update_subtype() for that.
*
* @param string $type The type you're subtyping (site, user, object, or group)
* @param string $subtype The subtype
* @param string $class Optional class name for the object
*
* @return int
* @link http://docs.elgg.org/Tutorials/Subclasses
* @link http://docs.elgg.org/DataModel/Entities
* @see update_subtype()
* @see remove_subtype()
* @see get_entity()
*/
function add_subtype($type, $subtype, $class = "") {
global $CONFIG;
$type = sanitise_string($type);
$subtype = sanitise_string($subtype);
$class = sanitise_string($class);
// Short circuit if no subtype is given
if ($subtype == "") {
return 0;
}
$id = get_subtype_id($type, $subtype);
if ($id == 0) {
return insert_data("insert into {$CONFIG->dbprefix}entity_subtypes"
. " (type, subtype, class) values ('$type','$subtype','$class')");
}
return $id;
}
/**
* Removes a registered ElggEntity type, subtype, and classname.
*
* @param string $type Type
* @param string $subtype Subtype
*
* @return bool
* @see add_subtype()
* @see update_subtype()
*/
function remove_subtype($type, $subtype) {
global $CONFIG;
$type = sanitise_string($type);
$subtype = sanitise_string($subtype);
return delete_data("DELETE FROM {$CONFIG->dbprefix}entity_subtypes"
. " WHERE type = '$type' AND subtype = '$subtype'");
}
/**
* Update a registered ElggEntity type, subtype, and classname
*
* @param string $type Type
* @param string $subtype Subtype
* @param string $class Class name to use when loading this entity
*
* @return bool
*/
function update_subtype($type, $subtype, $class = '') {
global $CONFIG;
if (!$id = get_subtype_id($type, $subtype)) {
return FALSE;
}
$type = sanitise_string($type);
$subtype = sanitise_string($subtype);
return update_data("UPDATE {$CONFIG->dbprefix}entity_subtypes
SET type = '$type', subtype = '$subtype', class = '$class'
WHERE id = $id
");
}
/**
* Update an entity in the database.
*
* There are 4 basic entity types: site, user, object, and group.
* All entities are split between two tables: the entities table and their type table.
*
* @warning Plugin authors should never call this directly. Use ->save() instead.
*
* @param int $guid The guid of the entity to update
* @param int $owner_guid The new owner guid
* @param int $access_id The new access id
* @param int $container_guid The new container guid
* @param int $time_created The time creation timestamp
*
* @return bool
* @link http://docs.elgg.org/DataModel/Entities
* @access private
*/
function update_entity($guid, $owner_guid, $access_id, $container_guid = null, $time_created = null) {
global $CONFIG, $ENTITY_CACHE;
$guid = (int)$guid;
$owner_guid = (int)$owner_guid;
$access_id = (int)$access_id;
$container_guid = (int) $container_guid;
if (is_null($container_guid)) {
$container_guid = $owner_guid;
}
$time = time();
$entity = get_entity($guid);
if ($time_created == null) {
$time_created = $entity->time_created;
} else {
$time_created = (int) $time_created;
}
if ($entity && $entity->canEdit()) {
if (elgg_trigger_event('update', $entity->type, $entity)) {
$ret = update_data("UPDATE {$CONFIG->dbprefix}entities
set owner_guid='$owner_guid', access_id='$access_id',
container_guid='$container_guid', time_created='$time_created',
time_updated='$time' WHERE guid=$guid");
if ($entity instanceof ElggObject) {
update_river_access_by_object($guid, $access_id);
}
// If memcache is available then delete this entry from the cache
static $newentity_cache;
if ((!$newentity_cache) && (is_memcache_available())) {
$newentity_cache = new ElggMemcache('new_entity_cache');
}
if ($newentity_cache) {
$newentity_cache->delete($guid);
}
// Handle cases where there was no error BUT no rows were updated!
if ($ret === false) {
return false;
}
return true;
}
}
}
/**
* Determine if a given user can write to an entity container.
*
* An entity can be a container for any other entity by setting the
* container_guid. container_guid can differ from owner_guid.
*
* A plugin hook container_permissions_check:$entity_type is emitted to allow granular
* access controls in plugins.
*
* @param int $user_guid The user guid, or 0 for logged in user
* @param int $container_guid The container, or 0 for the current page owner.
* @param string $type The type of entity we're looking to write
* @param string $subtype The subtype of the entity we're looking to write
*
* @return bool
* @link http://docs.elgg.org/DataModel/Containers
*/
function can_write_to_container($user_guid = 0, $container_guid = 0, $type = 'all', $subtype = 'all') {
$user_guid = (int)$user_guid;
$user = get_entity($user_guid);
if (!$user) {
$user = elgg_get_logged_in_user_entity();
}
$container_guid = (int)$container_guid;
if (!$container_guid) {
$container_guid = elgg_get_page_owner_guid();
}
$return = false;
if (!$container_guid) {
$return = true;
}
$container = get_entity($container_guid);
if ($container) {
// If the user can edit the container, they can also write to it
if ($container->canEdit($user_guid)) {
$return = true;
}
// If still not approved, see if the user is a member of the group
// @todo this should be moved to the groups plugin/library
if (!$return && $user && $container instanceof ElggGroup) {
if ($container->isMember($user)) {
$return = true;
}
}
}
// See if anyone else has anything to say
return elgg_trigger_plugin_hook(
'container_permissions_check',
$type,
array(
'container' => $container,
'user' => $user,
'subtype' => $subtype
),
$return);
}
/**
* Create a new entry in the entities table.
*
* Saves the base information in the entities table for the entity. Saving
* the type information is handled in the calling class method.
*
* @warning Plugin authors should never call this directly. Always use entity objects.
*
* @warning Entities must have an entry in both the entities table and their type table
* or they will throw an exception when loaded.
*
* @param string $type The type of the entity (site, user, object, group).
* @param string $subtype The subtype of the entity.
* @param int $owner_guid The GUID of the object's owner.
* @param int $access_id The access control group to create the entity with.
* @param int $site_guid The site to add this entity to. 0 for current.
* @param int $container_guid The container GUID
*
* @return int|false The new entity's GUID, or false on failure
* @throws InvalidParameterException
* @link http://docs.elgg.org/DataModel/Entities
* @access private
*/
function create_entity($type, $subtype, $owner_guid, $access_id, $site_guid = 0,
$container_guid = 0) {
global $CONFIG;
$type = sanitise_string($type);
$subtype_id = add_subtype($type, $subtype);
$owner_guid = (int)$owner_guid;
$access_id = (int)$access_id;
$time = time();
if ($site_guid == 0) {
$site_guid = $CONFIG->site_guid;
}
$site_guid = (int) $site_guid;
if ($container_guid == 0) {
$container_guid = $owner_guid;
}
$user_guid = elgg_get_logged_in_user_guid();
if (!can_write_to_container($user_guid, $owner_guid, $type, $subtype)) {
return false;
}
if ($owner_guid != $container_guid) {
if (!can_write_to_container($user_guid, $container_guid, $type, $subtype)) {
return false;
}
}
if ($type == "") {
throw new InvalidParameterException(elgg_echo('InvalidParameterException:EntityTypeNotSet'));
}
return insert_data("INSERT into {$CONFIG->dbprefix}entities
(type, subtype, owner_guid, site_guid, container_guid,
access_id, time_created, time_updated, last_action)
values
('$type',$subtype_id, $owner_guid, $site_guid, $container_guid,
$access_id, $time, $time, $time)");
}
/**
* Returns a database row from the entities table.
*
* @tip Use get_entity() to return the fully loaded entity.
*
* @warning This will only return results if a) it exists, b) you have access to it.
* see {@link get_access_sql_suffix()}.
*
* @param int $guid The GUID of the object to extract
*
* @return stdClass|false
* @link http://docs.elgg.org/DataModel/Entities
* @see entity_row_to_elggstar()
* @access private
*/
function get_entity_as_row($guid) {
global $CONFIG;
if (!$guid) {
return false;
}
$guid = (int) $guid;
$access = get_access_sql_suffix();
return get_data_row("SELECT * from {$CONFIG->dbprefix}entities where guid=$guid and $access");
}
/**
* Create an Elgg* object from a given entity row.
*
* Handles loading all tables into the correct class.
*
* @param stdClass $row The row of the entry in the entities table.
*
* @return object|false
* @link http://docs.elgg.org/DataModel/Entities
* @see get_entity_as_row()
* @see add_subtype()
* @see get_entity()
* @access private
*/
function entity_row_to_elggstar($row) {
if (!($row instanceof stdClass)) {
return $row;
}
if ((!isset($row->guid)) || (!isset($row->subtype))) {
return $row;
}
$new_entity = false;
// Create a memcache cache if we can
static $newentity_cache;
if ((!$newentity_cache) && (is_memcache_available())) {
$newentity_cache = new ElggMemcache('new_entity_cache');
}
if ($newentity_cache) {
$new_entity = $newentity_cache->load($row->guid);
}
if ($new_entity) {
return $new_entity;
}
// load class for entity if one is registered
$classname = get_subtype_class_from_id($row->subtype);
if ($classname != "") {
if (class_exists($classname)) {
$new_entity = new $classname($row);
if (!($new_entity instanceof ElggEntity)) {
$msg = elgg_echo('ClassException:ClassnameNotClass', array($classname, 'ElggEntity'));
throw new ClassException($msg);
}
} else {
error_log(elgg_echo('ClassNotFoundException:MissingClass', array($classname)));
}
}
if (!$new_entity) {
//@todo Make this into a function
switch ($row->type) {
case 'object' :
$new_entity = new ElggObject($row);
break;
case 'user' :
$new_entity = new ElggUser($row);
break;
case 'group' :
$new_entity = new ElggGroup($row);
break;
case 'site' :
$new_entity = new ElggSite($row);
break;
default:
$msg = elgg_echo('InstallationException:TypeNotSupported', array($row->type));
throw new InstallationException($msg);
}
}
// Cache entity if we have a cache available
if (($newentity_cache) && ($new_entity)) {
$newentity_cache->save($new_entity->guid, $new_entity);
}
return $new_entity;
}
/**
* Loads and returns an entity object from a guid.
*
* @param int $guid The GUID of the entity
*
* @return ElggEntity The correct Elgg or custom object based upon entity type and subtype
* @link http://docs.elgg.org/DataModel/Entities
*/
function get_entity($guid) {
static $newentity_cache;
$new_entity = false;
// We could also use: if (!(int) $guid) { return FALSE },
// but that evaluates to a false positive for $guid = TRUE.
// This is a bit slower, but more thorough.
if (!is_numeric($guid) || $guid === 0 || $guid === '0') {
return FALSE;
}
if ((!$newentity_cache) && (is_memcache_available())) {
$newentity_cache = new ElggMemcache('new_entity_cache');
}
if ($newentity_cache) {
$new_entity = $newentity_cache->load($guid);
}
if ($new_entity) {
return $new_entity;
}
return entity_row_to_elggstar(get_entity_as_row($guid));
}
/**
* Does an entity exist?
*
* This function checks for the existence of an entity independent of access
* permissions. It is useful for situations when a user cannot access an entity
* and it must be determined whether entity has been deleted or the access level
* has changed.
*
* @param int $guid The GUID of the entity
*
* @return bool
* @since 1.8.0
*/
function elgg_entity_exists($guid) {
global $CONFIG;
$guid = sanitize_int($guid);
$query = "SELECT count(*) as total FROM {$CONFIG->dbprefix}entities WHERE guid = $guid";
$result = get_data_row($query);
if ($result->total == 0) {
return false;
} else {
return true;
}
}
/**
* Returns an array of entities with optional filtering.
*
* Entities are the basic unit of storage in Elgg. This function
* provides the simplest way to get an array of entities. There
* are many options available that can be passed to filter
* what sorts of entities are returned.
*
* @tip To output formatted strings of entities, use {@link elgg_list_entities()} and
* its cousins.
*
* @tip Plural arguments can be written as singular if only specifying a
* single element. ('type' => 'object' vs 'types' => array('object')).
*
* @param array $options Array in format:
*
* types => NULL|STR entity type (type IN ('type1', 'type2')
* Joined with subtypes by AND. See below)
*
* subtypes => NULL|STR entity subtype (SQL: subtype IN ('subtype1', 'subtype2))
*
* type_subtype_pairs => NULL|ARR (array('type' => 'subtype'))
* (type = '$type' AND subtype = '$subtype') pairs
*
* guids => NULL|ARR Array of entity guids
*
* owner_guids => NULL|ARR Array of owner guids
*
* container_guids => NULL|ARR Array of container_guids
*
* site_guids => NULL (current_site)|ARR Array of site_guid
*
* order_by => NULL (time_created desc)|STR SQL order by clause
*
* reverse_order_by => BOOL Reverse the default order by clause
*
* limit => NULL (10)|INT SQL limit clause (0 means no limit)
*
* offset => NULL (0)|INT SQL offset clause
*
* created_time_lower => NULL|INT Created time lower boundary in epoch time
*
* created_time_upper => NULL|INT Created time upper boundary in epoch time
*
* modified_time_lower => NULL|INT Modified time lower boundary in epoch time
*
* modified_time_upper => NULL|INT Modified time upper boundary in epoch time
*
* count => TRUE|FALSE return a count instead of entities
*
* wheres => array() Additional where clauses to AND together
*
* joins => array() Additional joins
*
* callback => string A callback function to pass each row through
*
* @return mixed If count, int. If not count, array. false on errors.
* @since 1.7.0
* @see elgg_get_entities_from_metadata()
* @see elgg_get_entities_from_relationship()
* @see elgg_get_entities_from_access_id()
* @see elgg_get_entities_from_annotations()
* @see elgg_list_entities()
* @link http://docs.elgg.org/DataModel/Entities/Getters
*/
function elgg_get_entities(array $options = array()) {
global $CONFIG;
$defaults = array(
'types' => ELGG_ENTITIES_ANY_VALUE,
'subtypes' => ELGG_ENTITIES_ANY_VALUE,
'type_subtype_pairs' => ELGG_ENTITIES_ANY_VALUE,
'guids' => ELGG_ENTITIES_ANY_VALUE,
'owner_guids' => ELGG_ENTITIES_ANY_VALUE,
'container_guids' => ELGG_ENTITIES_ANY_VALUE,
'site_guids' => $CONFIG->site_guid,
'modified_time_lower' => ELGG_ENTITIES_ANY_VALUE,
'modified_time_upper' => ELGG_ENTITIES_ANY_VALUE,
'created_time_lower' => ELGG_ENTITIES_ANY_VALUE,
'created_time_upper' => ELGG_ENTITIES_ANY_VALUE,
'reverse_order_by' => false,
'order_by' => 'e.time_created desc',
'group_by' => ELGG_ENTITIES_ANY_VALUE,
'limit' => 10,
'offset' => 0,
'count' => FALSE,
'selects' => array(),
'wheres' => array(),
'joins' => array(),
'callback' => 'entity_row_to_elggstar',
);
$options = array_merge($defaults, $options);
// can't use helper function with type_subtype_pair because
// it's already an array...just need to merge it
if (isset($options['type_subtype_pair'])) {
if (isset($options['type_subtype_pairs'])) {
$options['type_subtype_pairs'] = array_merge($options['type_subtype_pairs'],
$options['type_subtype_pair']);
} else {
$options['type_subtype_pairs'] = $options['type_subtype_pair'];
}
}
$singulars = array('type', 'subtype', 'guid', 'owner_guid', 'container_guid', 'site_guid');
$options = elgg_normalise_plural_options_array($options, $singulars);
// evaluate where clauses
if (!is_array($options['wheres'])) {
$options['wheres'] = array($options['wheres']);
}
$wheres = $options['wheres'];
$wheres[] = elgg_get_entity_type_subtype_where_sql('e', $options['types'],
$options['subtypes'], $options['type_subtype_pairs']);
$wheres[] = elgg_get_guid_based_where_sql('e.guid', $options['guids']);
$wheres[] = elgg_get_guid_based_where_sql('e.owner_guid', $options['owner_guids']);
$wheres[] = elgg_get_guid_based_where_sql('e.container_guid', $options['container_guids']);
$wheres[] = elgg_get_guid_based_where_sql('e.site_guid', $options['site_guids']);
$wheres[] = elgg_get_entity_time_where_sql('e', $options['created_time_upper'],
$options['created_time_lower'], $options['modified_time_upper'], $options['modified_time_lower']);
// see if any functions failed
// remove empty strings on successful functions
foreach ($wheres as $i => $where) {
if ($where === FALSE) {
return FALSE;
} elseif (empty($where)) {
unset($wheres[$i]);
}
}
// remove identical where clauses
$wheres = array_unique($wheres);
// evaluate join clauses
if (!is_array($options['joins'])) {
$options['joins'] = array($options['joins']);
}
// remove identical join clauses
$joins = array_unique($options['joins']);
foreach ($joins as $i => $join) {
if ($join === FALSE) {
return FALSE;
} elseif (empty($join)) {
unset($joins[$i]);
}
}
// evalutate selects
if ($options['selects']) {
$selects = '';
foreach ($options['selects'] as $select) {
$selects .= ", $select";
}
} else {
$selects = '';
}
if (!$options['count']) {
$query = "SELECT DISTINCT e.*{$selects} FROM {$CONFIG->dbprefix}entities e ";
} else {
$query = "SELECT count(DISTINCT e.guid) as total FROM {$CONFIG->dbprefix}entities e ";
}
// add joins
foreach ($joins as $j) {
$query .= " $j ";
}
// add wheres
$query .= ' WHERE ';
foreach ($wheres as $w) {
$query .= " $w AND ";
}
// Add access controls
$query .= get_access_sql_suffix('e');
// reverse order by
if ($options['reverse_order_by']) {
$options['order_by'] = elgg_sql_reverse_order_by_clause($options['order_by']);
}
if (!$options['count']) {
if ($options['group_by'] = sanitise_string($options['group_by'])) {
$query .= " GROUP BY {$options['group_by']}";
}
if ($options['order_by'] = sanitise_string($options['order_by'])) {
$query .= " ORDER BY {$options['order_by']}";
}
if ($options['limit']) {
$limit = sanitise_int($options['limit'], false);
$offset = sanitise_int($options['offset'], false);
$query .= " LIMIT $offset, $limit";
}
$dt = get_data($query, $options['callback']);
return $dt;
} else {
$total = get_data_row($query);
return (int)$total->total;
}
}
/**
* Returns SQL where clause for type and subtype on main entity table
*
* @param string $table Entity table prefix as defined in SELECT...FROM entities $table
* @param NULL|array $types Array of types or NULL if none.
* @param NULL|array $subtypes Array of subtypes or NULL if none
* @param NULL|array $pairs Array of pairs of types and subtypes
*
* @return FALSE|string
* @since 1.7.0
* @access private
*/
function elgg_get_entity_type_subtype_where_sql($table, $types, $subtypes, $pairs) {
// subtype depends upon type.
if ($subtypes && !$types) {
elgg_log("Cannot set subtypes without type.", 'WARNING');
return FALSE;
}
// short circuit if nothing is requested
if (!$types && !$subtypes && !$pairs) {
return '';
}
// these are the only valid types for entities in elgg
$valid_types = elgg_get_config('entity_types');
// pairs override
$wheres = array();
if (!is_array($pairs)) {
if (!is_array($types)) {
$types = array($types);
}
if ($subtypes && !is_array($subtypes)) {
$subtypes = array($subtypes);
}
// decrementer for valid types. Return FALSE if no valid types
$valid_types_count = count($types);
$valid_subtypes_count = 0;
// remove invalid types to get an accurate count of
// valid types for the invalid subtype detection to use
// below.
// also grab the count of ALL subtypes on valid types to decrement later on
// and check against.
//
// yes this is duplicating a foreach on $types.
foreach ($types as $type) {
if (!in_array($type, $valid_types)) {
$valid_types_count--;
unset($types[array_search($type, $types)]);
} else {
// do the checking (and decrementing) in the subtype section.
$valid_subtypes_count += count($subtypes);
}
}
// return false if nothing is valid.
if (!$valid_types_count) {
return FALSE;
}