Elgg  Version 1.10
entities.php
Go to the documentation of this file.
1 <?php
16 $ENTITY_CACHE = array();
17 
26 
37 
39  $ENTITY_CACHE_DISABLED_GUIDS[$guid] = true;
40 }
41 
51 
52  unset($ENTITY_CACHE_DISABLED_GUIDS[$guid]);
53 }
54 
65 
66  $guid = (int)$guid;
67 
68  if (isset($ENTITY_CACHE[$guid])) {
69  unset($ENTITY_CACHE[$guid]);
70 
71  // Purge separate metadata cache. Original idea was to do in entity destructor, but that would
72  // have caused a bunch of unnecessary purges at every shutdown. Doing it this way we have no way
73  // to know that the expunged entity will be GCed (might be another reference living), but that's
74  // OK; the metadata will reload if necessary.
75  _elgg_services()->metadataCache->clear($guid);
76  }
77 }
78 
94 
95  // Don't cache non-plugin entities while access control is off, otherwise they could be
96  // exposed to users who shouldn't see them when control is re-enabled.
97  if (!($entity instanceof \ElggPlugin) && elgg_get_ignore_access()) {
98  return;
99  }
100 
101  $guid = $entity->getGUID();
102  if (isset($ENTITY_CACHE_DISABLED_GUIDS[$guid])) {
103  return;
104  }
105 
106  // Don't store too many or we'll have memory problems
107  // @todo Pick a less arbitrary limit
108  if (count($ENTITY_CACHE) > 256) {
109  _elgg_invalidate_cache_for_entity(array_rand($ENTITY_CACHE));
110  }
111 
112  $ENTITY_CACHE[$guid] = $entity;
113 }
114 
127 
128  if (isset($ENTITY_CACHE[$guid])) {
129  if ($ENTITY_CACHE[$guid]->isFullyLoaded()) {
130  return $ENTITY_CACHE[$guid];
131  }
132  }
133 
134  return false;
135 }
136 
158  return _elgg_services()->subtypeTable->getId($type, $subtype);
159 }
160 
169 function get_subtype_from_id($subtype_id) {
170  return _elgg_services()->subtypeTable->getSubtype($subtype_id);
171 }
172 
183  return _elgg_services()->subtypeTable->retrieveFromCache($type, $subtype);
184 }
185 
192  return _elgg_services()->subtypeTable->populateCache();
193 }
194 
211  return _elgg_services()->subtypeTable->getClass($type, $subtype);
212 }
213 
224 function get_subtype_class_from_id($subtype_id) {
225  return _elgg_services()->subtypeTable->getClassFromId($subtype_id);
226 }
227 
248 function add_subtype($type, $subtype, $class = "") {
249  return _elgg_services()->subtypeTable->add($type, $subtype, $class);
250 }
251 
267  return _elgg_services()->subtypeTable->remove($type, $subtype);
268 }
269 
280  return _elgg_services()->subtypeTable->update($type, $subtype, $class);
281 }
282 
299 function can_write_to_container($user_guid = 0, $container_guid = 0, $type = 'all', $subtype = 'all') {
300  $container_guid = (int)$container_guid;
301  if (!$container_guid) {
302  $container_guid = elgg_get_page_owner_guid();
303  }
304 
305  $container = get_entity($container_guid);
306 
307  $user_guid = (int)$user_guid;
308  if ($user_guid == 0) {
311  } else {
313  if (!$user) {
314  return false;
315  }
316  }
317 
318  $return = false;
319  if ($container) {
320  // If the user can edit the container, they can also write to it
321  if ($container->canEdit($user_guid)) {
322  $return = true;
323  }
324  }
325 
326  // See if anyone else has anything to say
328  'container_permissions_check',
329  $type,
330  array(
331  'container' => $container,
332  'user' => $user,
333  'subtype' => $subtype
334  ),
335  $return);
336 }
337 
353  return _elgg_services()->entityTable->getRow($guid);
354 }
355 
372  return _elgg_services()->entityTable->rowToElggStar($row);
373 }
374 
382 function get_entity($guid) {
383  return _elgg_services()->entityTable->get($guid);
384 }
385 
400  return _elgg_services()->entityTable->exists($guid);
401 }
402 
412 function elgg_enable_entity($guid, $recursive = true) {
413  return _elgg_services()->entityTable->enable($guid, $recursive);
414 }
415 
490 function elgg_get_entities(array $options = array()) {
491  return _elgg_services()->entityTable->getEntities($options);
492 }
493 
504 function _elgg_fetch_entities_from_sql($sql, \ElggBatch $batch = null) {
505  return _elgg_services()->entityTable->fetchFromSql($sql, $batch);
506 }
507 
521  return _elgg_services()->entityTable->getEntityTypeSubtypeWhereSql($table, $types, $subtypes, $pairs);
522 }
523 
536  return _elgg_services()->entityTable->getGuidBasedWhereSql($column, $guids);
537 }
538 
553 function _elgg_get_entity_time_where_sql($table, $time_created_upper = null,
554  $time_created_lower = null, $time_updated_upper = null, $time_updated_lower = null) {
555  return _elgg_services()->entityTable->getEntityTimeWhereSql($table,
556  $time_created_upper, $time_created_lower, $time_updated_upper, $time_updated_lower);
557 }
558 
585 function elgg_list_entities(array $options = array(), $getter = 'elgg_get_entities',
586  $viewer = 'elgg_view_entity_list') {
587 
588  global $autofeed;
589  $autofeed = true;
590 
591  $offset_key = isset($options['offset_key']) ? $options['offset_key'] : 'offset';
592 
593  $defaults = array(
594  'offset' => (int) max(get_input($offset_key, 0), 0),
595  'limit' => (int) max(get_input('limit', elgg_get_config('default_limit')), 0),
596  'full_view' => false,
597  'list_type_toggle' => false,
598  'pagination' => true,
599  'no_results' => '',
600  );
601 
602  $options = array_merge($defaults, $options);
603 
604  // backward compatibility
605  if (isset($options['view_type_toggle'])) {
606  elgg_deprecated_notice("Option 'view_type_toggle' deprecated by 'list_type_toggle' in elgg_list* functions", 1.9);
607  $options['list_type_toggle'] = $options['view_type_toggle'];
608  }
609 
610  $options['count'] = true;
611  $count = call_user_func($getter, $options);
612 
613  if ($count > 0) {
614  $options['count'] = false;
615  $entities = call_user_func($getter, $options);
616  } else {
617  $entities = array();
618  }
619 
620  $options['count'] = $count;
621 
622  return call_user_func($viewer, $entities, $options);
623 }
624 
657 function elgg_get_entities_from_attributes(array $options = array()) {
658  return _elgg_services()->entityTable->getEntitiesFromAttributes($options);
659 }
660 
670  return _elgg_services()->entityTable->getEntityAttributeWhereSql($options);
671 }
672 
690 function get_entity_dates($type = '', $subtype = '', $container_guid = 0, $site_guid = 0,
691  $order_by = 'time_created') {
692  return _elgg_services()->entityTable->getDates(
693  $type, $subtype, $container_guid, $site_guid, $order_by);
694 }
695 
711  global $CONFIG;
712 
713  $type = strtolower($type);
714  if (!in_array($type, $CONFIG->entity_types)) {
715  return false;
716  }
717 
718  if (!isset($CONFIG->registered_entities)) {
719  $CONFIG->registered_entities = array();
720  }
721 
722  if (!isset($CONFIG->registered_entities[$type])) {
723  $CONFIG->registered_entities[$type] = array();
724  }
725 
726  if ($subtype) {
727  $CONFIG->registered_entities[$type][] = $subtype;
728  }
729 
730  return true;
731 }
732 
746  global $CONFIG;
747 
748  $type = strtolower($type);
749  if (!in_array($type, $CONFIG->entity_types)) {
750  return false;
751  }
752 
753  if (!isset($CONFIG->registered_entities)) {
754  return false;
755  }
756 
757  if (!isset($CONFIG->registered_entities[$type])) {
758  return false;
759  }
760 
761  if ($subtype) {
762  if (in_array($subtype, $CONFIG->registered_entities[$type])) {
763  $key = array_search($subtype, $CONFIG->registered_entities[$type]);
764  unset($CONFIG->registered_entities[$type][$key]);
765  } else {
766  return false;
767  }
768  } else {
769  unset($CONFIG->registered_entities[$type]);
770  }
771 
772  return true;
773 }
774 
784  global $CONFIG;
785 
786  if (!isset($CONFIG->registered_entities)) {
787  return false;
788  }
789  if ($type) {
790  $type = strtolower($type);
791  }
792  if (!empty($type) && empty($CONFIG->registered_entities[$type])) {
793  return false;
794  }
795 
796  if (empty($type)) {
797  return $CONFIG->registered_entities;
798  }
799 
800  return $CONFIG->registered_entities[$type];
801 }
802 
812  global $CONFIG;
813 
814  if (!isset($CONFIG->registered_entities)) {
815  return false;
816  }
817 
818  $type = strtolower($type);
819 
820  // @todo registering a subtype implicitly registers the type.
821  // see #2684
822  if (!isset($CONFIG->registered_entities[$type])) {
823  return false;
824  }
825 
826  if ($subtype && !in_array($subtype, $CONFIG->registered_entities[$type])) {
827  return false;
828  }
829  return true;
830 }
831 
850 function elgg_list_registered_entities(array $options = array()) {
851  global $autofeed;
852  $autofeed = true;
853 
854  $defaults = array(
855  'full_view' => false,
856  'allowed_types' => true,
857  'list_type_toggle' => false,
858  'pagination' => true,
859  'offset' => 0,
860  'types' => array(),
861  'type_subtype_pairs' => array(),
862  );
863 
864  $options = array_merge($defaults, $options);
865 
866  // backward compatibility
867  if (isset($options['view_type_toggle'])) {
868  elgg_deprecated_notice("Option 'view_type_toggle' deprecated by 'list_type_toggle' in elgg_list* functions", 1.9);
869  $options['list_type_toggle'] = $options['view_type_toggle'];
870  }
871 
872  $types = get_registered_entity_types();
873 
874  foreach ($types as $type => $subtype_array) {
875  if (in_array($type, $options['allowed_types']) || $options['allowed_types'] === true) {
876  // you must explicitly register types to show up in here and in search for objects
877  if ($type == 'object') {
878  if (is_array($subtype_array) && count($subtype_array)) {
879  $options['type_subtype_pairs'][$type] = $subtype_array;
880  }
881  } else {
882  if (is_array($subtype_array) && count($subtype_array)) {
883  $options['type_subtype_pairs'][$type] = $subtype_array;
884  } else {
885  $options['type_subtype_pairs'][$type] = ELGG_ENTITIES_ANY_VALUE;
886  }
887  }
888  }
889  }
890 
891  if (!empty($options['type_subtype_pairs'])) {
892  $count = elgg_get_entities(array_merge(array('count' => true), $options));
893  if ($count > 0) {
894  $entities = elgg_get_entities($options);
895  } else {
896  $entities = array();
897  }
898  } else {
899  $count = 0;
900  $entities = array();
901  }
902 
903  $options['count'] = $count;
904  return elgg_view_entity_list($entities, $options);
905 }
906 
921 function elgg_instanceof($entity, $type = null, $subtype = null, $class = null) {
923 
924  if ($type) {
925  /* @var \ElggEntity $entity */
926  $return = $return && ($entity->getType() == $type);
927  }
928 
929  if ($subtype) {
930  $return = $return && ($entity->getSubtype() == $subtype);
931  }
932 
933  if ($class) {
934  $return = $return && ($entity instanceof $class);
935  }
936 
937  return $return;
938 }
939 
953  return _elgg_services()->entityTable->updateLastAction($guid, $posted);
954 }
955 
966 function _elgg_entities_test($hook, $type, $value) {
967  global $CONFIG;
968  $value[] = $CONFIG->path . 'engine/tests/ElggEntityTest.php';
969  $value[] = $CONFIG->path . 'engine/tests/ElggCoreAttributeLoaderTest.php';
970  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesTest.php';
971  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromAnnotationsTest.php';
972  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromMetadataTest.php';
973  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromPrivateSettingsTest.php';
974  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromRelationshipTest.php';
975  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromAttributesTest.php';
976  $value[] = $CONFIG->path . 'engine/tests/ElggOwnerPreloaderIntegrationTest.php';
977  return $value;
978 }
979 
988  elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_entities_test');
989 }
990 
991 elgg_register_event_handler('init', 'system', '_elgg_entities_init');
_elgg_disable_caching_for_entity($guid)
Remove this entity from the entity cache and make sure it is not re-added.
Definition: entities.php:35
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
_elgg_populate_subtype_cache()
Fetch all suptypes from DB to local cache.
Definition: entities.php:191
_elgg_retrieve_cached_subtype($type, $subtype)
Retrieve subtype from the cache.
Definition: entities.php:182
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
_elgg_invalidate_cache_for_entity($guid)
Invalidate this class&#39;s entry in the cache.
Definition: entities.php:63
elgg_get_entities_from_attributes(array $options=array())
Gets entities based upon attributes in secondary tables.
Definition: entities.php:657
$table
Definition: cron.php:28
_elgg_retrieve_cached_entity($guid)
Retrieve a entity from the cache.
Definition: entities.php:125
get_subtype_id($type, $subtype)
Return the id for a given subtype.
Definition: entities.php:157
elgg_enable_entity($guid, $recursive=true)
Enable an entity.
Definition: entities.php:412
_elgg_get_guid_based_where_sql($column, $guids)
Returns SQL where clause for owner and containers.
Definition: entities.php:535
update_entity_last_action($guid, $posted=null)
Update the last_action column in the entities table for $guid.
Definition: entities.php:952
_elgg_get_entity_time_where_sql($table, $time_created_upper=null, $time_created_lower=null, $time_updated_upper=null, $time_updated_lower=null)
Returns SQL where clause for entity time limits.
Definition: entities.php:553
get_subtype_from_id($subtype_id)
Gets the denormalized string for a given subtype ID.
Definition: entities.php:169
_elgg_cache_entity(\ElggEntity $entity)
Cache an entity.
Definition: entities.php:92
$value
Definition: longtext.php:29
get_entity_dates($type= '', $subtype= '', $container_guid=0, $site_guid=0, $order_by= 'time_created')
Returns a list of months in which entities were updated or created.
Definition: entities.php:690
$column
Definition: add.php:13
elgg_unregister_entity_type($type, $subtype=null)
Unregisters an entity type and subtype as a public-facing type.
Definition: entities.php:745
if(!$autoload_available) _elgg_services()
Definition: autoloader.php:20
$return
Definition: opendd.php:15
getGUID()
Returns the guid.
elgg_entity_exists($guid)
Does an entity exist?
Definition: entities.php:399
$guid
Removes an admin notice.
_elgg_enable_caching_for_entity($guid)
Allow this entity to be stored in the entity cache.
Definition: entities.php:49
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Register a callback as a plugin hook handler.
Definition: elgglib.php:737
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
get_subtype_class_from_id($subtype_id)
Returns the class name for a subtype id.
Definition: entities.php:224
elgg_get_ignore_access()
Get current ignore access setting.
Definition: access.php:54
entity_row_to_elggstar($row)
Create an Elgg* object from a given entity row.
Definition: entities.php:371
$options
Definition: index.php:14
get_subtype_class($type, $subtype)
Return the class name for a registered type and subtype.
Definition: entities.php:210
is_registered_entity_type($type, $subtype=null)
Returns if the entity type and subtype have been registered with elgg_register_entity_type().
Definition: entities.php:811
get_registered_entity_types($type=null)
Returns registered entity types and subtypes.
Definition: entities.php:783
global $ENTITY_CACHE_DISABLED_GUIDS
GUIDs of entities banned from the entity cache (during this request)
Definition: entities.php:24
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:921
add_subtype($type, $subtype, $class="")
Register with a certain type and subtype to be loaded as a specific class.
Definition: entities.php:248
if(!$limit=(int) elgg_extract('limit', $vars, elgg_get_config('default_limit'))) $count
Definition: pagination.php:26
$key
Definition: summary.php:34
get_user($guid)
Get a user object from a GUID.
Definition: users.php:87
global $CONFIG
can_write_to_container($user_guid=0, $container_guid=0, $type= 'all', $subtype= 'all')
Determine if a given user can write to an entity container.
Definition: entities.php:299
$user
Definition: ban.php:13
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2059
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:490
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Trigger a Plugin Hook and run all handler callbacks registered to that hook:type. ...
Definition: elgglib.php:809
_elgg_entities_test($hook, $type, $value)
Runs unit tests for the entity objects.
Definition: entities.php:966
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1055
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$offset_key
Definition: pagination.php:27
$type
Definition: add.php:8
get_entity_as_row($guid)
Returns a database row from the entities table.
Definition: entities.php:352
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Register a callback as an Elgg event handler.
Definition: elgglib.php:553
update_subtype($type, $subtype, $class= '')
Update a registered type, subtype, and class name.
Definition: entities.php:279
$posted
Definition: comment.php:69
$guids
elgg_list_entities(array $options=array(), $getter= 'elgg_get_entities', $viewer= 'elgg_view_entity_list')
Returns a string of rendered entities.
Definition: entities.php:585
elgg_list_registered_entities(array $options=array())
Returns a viewable list of entities based on the registered types.
Definition: entities.php:850
remove_subtype($type, $subtype)
Removes a registered type, subtype, and classname.
Definition: entities.php:266
$subtypes
_elgg_get_entity_type_subtype_where_sql($table, $types, $subtypes, $pairs)
Returns SQL where clause for type and subtype on main entity table.
Definition: entities.php:520
$row
_elgg_get_entity_attribute_where_sql(array $options=array())
Get the join and where clauses for working with entity attributes.
Definition: entities.php:669
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:32
elgg_register_entity_type($type, $subtype=null)
Registers an entity type and subtype as a public-facing entity that should be shown in search and by ...
Definition: entities.php:710
$user_guid
Avatar remove action.
Definition: remove.php:6
$defaults
Definition: access.php:19
$entity
Definition: delete.php:10
elgg_view_entity_list($entities, $vars=array(), $offset=0, $limit=null, $full_view=true, $list_type_toggle=true, $pagination=true)
Returns a rendered list of entities with pagination.
Definition: views.php:967
$subtype
Definition: river.php:12
global $ENTITY_CACHE
Cache entities in memory once loaded.
Definition: entities.php:15
_elgg_fetch_entities_from_sql($sql,\ElggBatch $batch=null)
Return entities from an SQL query generated by elgg_get_entities.
Definition: entities.php:504
$container
Definition: access.php:30
elgg ElggEntity
Definition: ElggEntity.js:16
elgg_get_page_owner_guid($guid=0)
Gets the guid of the entity that owns the current page.
Definition: pageowner.php:18
_elgg_entities_init()
Entities init function; establishes the default entity page handler.
Definition: entities.php:987
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:42
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:382