Elgg  Version 1.12
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') {
301  if (!$container_guid) {
303  }
304 
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 
494 function elgg_get_entities(array $options = array()) {
495  return _elgg_services()->entityTable->getEntities($options);
496 }
497 
508 function _elgg_fetch_entities_from_sql($sql, \ElggBatch $batch = null) {
509  return _elgg_services()->entityTable->fetchFromSql($sql, $batch);
510 }
511 
525  return _elgg_services()->entityTable->getEntityTypeSubtypeWhereSql($table, $types, $subtypes, $pairs);
526 }
527 
540  return _elgg_services()->entityTable->getGuidBasedWhereSql($column, $guids);
541 }
542 
557 function _elgg_get_entity_time_where_sql($table, $time_created_upper = null,
558  $time_created_lower = null, $time_updated_upper = null, $time_updated_lower = null) {
559  return _elgg_services()->entityTable->getEntityTimeWhereSql($table,
560  $time_created_upper, $time_created_lower, $time_updated_upper, $time_updated_lower);
561 }
562 
590 function elgg_list_entities(array $options = array(), $getter = 'elgg_get_entities',
591  $viewer = 'elgg_view_entity_list') {
592 
593  global $autofeed;
594  $autofeed = true;
595 
596  $offset_key = isset($options['offset_key']) ? $options['offset_key'] : 'offset';
597 
598  $defaults = array(
599  'offset' => (int) max(get_input($offset_key, 0), 0),
600  'limit' => (int) max(get_input('limit', elgg_get_config('default_limit')), 0),
601  'full_view' => false,
602  'list_type_toggle' => false,
603  'pagination' => true,
604  'no_results' => '',
605  );
606 
607  $options = array_merge($defaults, $options);
608 
609  // backward compatibility
610  if (isset($options['view_type_toggle'])) {
611  elgg_deprecated_notice("Option 'view_type_toggle' deprecated by 'list_type_toggle' in elgg_list* functions", 1.9);
612  $options['list_type_toggle'] = $options['view_type_toggle'];
613  }
614 
615  $options['count'] = true;
616  $count = call_user_func($getter, $options);
617 
618  if ($count > 0) {
619  $options['count'] = false;
620  $entities = call_user_func($getter, $options);
621  } else {
622  $entities = array();
623  }
624 
625  $options['count'] = $count;
626 
627  return call_user_func($viewer, $entities, $options);
628 }
629 
662 function elgg_get_entities_from_attributes(array $options = array()) {
663  return _elgg_services()->entityTable->getEntitiesFromAttributes($options);
664 }
665 
675  return _elgg_services()->entityTable->getEntityAttributeWhereSql($options);
676 }
677 
695 function get_entity_dates($type = '', $subtype = '', $container_guid = 0, $site_guid = 0,
696  $order_by = 'time_created') {
697  return _elgg_services()->entityTable->getDates(
698  $type, $subtype, $container_guid, $site_guid, $order_by);
699 }
700 
716  global $CONFIG;
717 
718  $type = strtolower($type);
719  if (!in_array($type, $CONFIG->entity_types)) {
720  return false;
721  }
722 
723  if (!isset($CONFIG->registered_entities)) {
724  $CONFIG->registered_entities = array();
725  }
726 
727  if (!isset($CONFIG->registered_entities[$type])) {
728  $CONFIG->registered_entities[$type] = array();
729  }
730 
731  if ($subtype) {
732  $CONFIG->registered_entities[$type][] = $subtype;
733  }
734 
735  return true;
736 }
737 
751  global $CONFIG;
752 
753  $type = strtolower($type);
754  if (!in_array($type, $CONFIG->entity_types)) {
755  return false;
756  }
757 
758  if (!isset($CONFIG->registered_entities)) {
759  return false;
760  }
761 
762  if (!isset($CONFIG->registered_entities[$type])) {
763  return false;
764  }
765 
766  if ($subtype) {
767  if (in_array($subtype, $CONFIG->registered_entities[$type])) {
768  $key = array_search($subtype, $CONFIG->registered_entities[$type]);
769  unset($CONFIG->registered_entities[$type][$key]);
770  } else {
771  return false;
772  }
773  } else {
774  unset($CONFIG->registered_entities[$type]);
775  }
776 
777  return true;
778 }
779 
789  global $CONFIG;
790 
791  if (!isset($CONFIG->registered_entities)) {
792  return false;
793  }
794  if ($type) {
795  $type = strtolower($type);
796  }
797  if (!empty($type) && empty($CONFIG->registered_entities[$type])) {
798  return false;
799  }
800 
801  if (empty($type)) {
802  return $CONFIG->registered_entities;
803  }
804 
805  return $CONFIG->registered_entities[$type];
806 }
807 
817  global $CONFIG;
818 
819  if (!isset($CONFIG->registered_entities)) {
820  return false;
821  }
822 
823  $type = strtolower($type);
824 
825  // @todo registering a subtype implicitly registers the type.
826  // see #2684
827  if (!isset($CONFIG->registered_entities[$type])) {
828  return false;
829  }
830 
831  if ($subtype && !in_array($subtype, $CONFIG->registered_entities[$type])) {
832  return false;
833  }
834  return true;
835 }
836 
855 function elgg_list_registered_entities(array $options = array()) {
856  global $autofeed;
857  $autofeed = true;
858 
859  $defaults = array(
860  'full_view' => false,
861  'allowed_types' => true,
862  'list_type_toggle' => false,
863  'pagination' => true,
864  'offset' => 0,
865  'types' => array(),
866  'type_subtype_pairs' => array(),
867  );
868 
869  $options = array_merge($defaults, $options);
870 
871  // backward compatibility
872  if (isset($options['view_type_toggle'])) {
873  elgg_deprecated_notice("Option 'view_type_toggle' deprecated by 'list_type_toggle' in elgg_list* functions", 1.9);
874  $options['list_type_toggle'] = $options['view_type_toggle'];
875  }
876 
877  $types = get_registered_entity_types();
878 
879  foreach ($types as $type => $subtype_array) {
880  if (in_array($type, $options['allowed_types']) || $options['allowed_types'] === true) {
881  // you must explicitly register types to show up in here and in search for objects
882  if ($type == 'object') {
883  if (is_array($subtype_array) && count($subtype_array)) {
884  $options['type_subtype_pairs'][$type] = $subtype_array;
885  }
886  } else {
887  if (is_array($subtype_array) && count($subtype_array)) {
888  $options['type_subtype_pairs'][$type] = $subtype_array;
889  } else {
890  $options['type_subtype_pairs'][$type] = ELGG_ENTITIES_ANY_VALUE;
891  }
892  }
893  }
894  }
895 
896  if (!empty($options['type_subtype_pairs'])) {
897  $count = elgg_get_entities(array_merge(array('count' => true), $options));
898  if ($count > 0) {
899  $entities = elgg_get_entities($options);
900  } else {
901  $entities = array();
902  }
903  } else {
904  $count = 0;
905  $entities = array();
906  }
907 
908  $options['count'] = $count;
909  return elgg_view_entity_list($entities, $options);
910 }
911 
926 function elgg_instanceof($entity, $type = null, $subtype = null, $class = null) {
928 
929  if ($type) {
930  /* @var \ElggEntity $entity */
931  $return = $return && ($entity->getType() == $type);
932  }
933 
934  if ($subtype) {
935  $return = $return && ($entity->getSubtype() == $subtype);
936  }
937 
938  if ($class) {
939  $return = $return && ($entity instanceof $class);
940  }
941 
942  return $return;
943 }
944 
958  return _elgg_services()->entityTable->updateLastAction($guid, $posted);
959 }
960 
971 function _elgg_entities_test($hook, $type, $value) {
972  global $CONFIG;
973  $value[] = $CONFIG->path . 'engine/tests/ElggEntityTest.php';
974  $value[] = $CONFIG->path . 'engine/tests/ElggCoreAttributeLoaderTest.php';
975  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesTest.php';
976  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromAnnotationsTest.php';
977  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromMetadataTest.php';
978  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromPrivateSettingsTest.php';
979  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromRelationshipTest.php';
980  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromAttributesTest.php';
981  $value[] = $CONFIG->path . 'engine/tests/ElggEntityPreloaderIntegrationTest.php';
982  return $value;
983 }
984 
993  elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_entities_test');
994 }
995 
996 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
997  $events->registerHandler('init', 'system', '_elgg_entities_init');
998 };
_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:662
$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
$defaults
_elgg_get_guid_based_where_sql($column, $guids)
Returns SQL where clause for owner and containers.
Definition: entities.php:539
update_entity_last_action($guid, $posted=null)
Update the last_action column in the entities table for $guid.
Definition: entities.php:957
_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:557
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:26
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:695
$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:750
$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)
Definition: elgglib.php:717
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:816
get_registered_entity_types($type=null)
Returns registered entity types and subtypes.
Definition: entities.php:788
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:926
if($entity) $container
Definition: access.php:62
add_subtype($type, $subtype, $class="")
Register with a certain type and subtype to be loaded as a specific class.
Definition: entities.php:248
$key
Definition: summary.php:34
get_user($guid)
Get a user object from a GUID.
Definition: users.php:87
_elgg_services()
Definition: autoloader.php:14
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:2006
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:494
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:790
_elgg_entities_test($hook, $type, $value)
Runs unit tests for the entity objects.
Definition: entities.php:971
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1031
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
get_entity_as_row($guid)
Returns a database row from the entities table.
Definition: entities.php:352
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:590
elgg_list_registered_entities(array $options=array())
Returns a viewable list of entities based on the registered types.
Definition: entities.php:855
if(!$limit=(int) elgg_extract('limit', $vars, elgg_get_config('default_limit'))) $offset_key
Definition: pagination.php:32
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:524
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
$row
_elgg_get_entity_attribute_where_sql(array $options=array())
Get the join and where clauses for working with entity attributes.
Definition: entities.php:674
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:32
$container_guid
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:715
$user_guid
Avatar remove action.
Definition: remove.php:6
$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:970
$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:508
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:992
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