Elgg  Version 1.11
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 
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 
586 function elgg_list_entities(array $options = array(), $getter = 'elgg_get_entities',
587  $viewer = 'elgg_view_entity_list') {
588 
589  global $autofeed;
590  $autofeed = true;
591 
592  $offset_key = isset($options['offset_key']) ? $options['offset_key'] : 'offset';
593 
594  $defaults = array(
595  'offset' => (int) max(get_input($offset_key, 0), 0),
596  'limit' => (int) max(get_input('limit', elgg_get_config('default_limit')), 0),
597  'full_view' => false,
598  'list_type_toggle' => false,
599  'pagination' => true,
600  'no_results' => '',
601  );
602 
603  $options = array_merge($defaults, $options);
604 
605  // backward compatibility
606  if (isset($options['view_type_toggle'])) {
607  elgg_deprecated_notice("Option 'view_type_toggle' deprecated by 'list_type_toggle' in elgg_list* functions", 1.9);
608  $options['list_type_toggle'] = $options['view_type_toggle'];
609  }
610 
611  $options['count'] = true;
612  $count = call_user_func($getter, $options);
613 
614  if ($count > 0) {
615  $options['count'] = false;
616  $entities = call_user_func($getter, $options);
617  } else {
618  $entities = array();
619  }
620 
621  $options['count'] = $count;
622 
623  return call_user_func($viewer, $entities, $options);
624 }
625 
658 function elgg_get_entities_from_attributes(array $options = array()) {
659  return _elgg_services()->entityTable->getEntitiesFromAttributes($options);
660 }
661 
671  return _elgg_services()->entityTable->getEntityAttributeWhereSql($options);
672 }
673 
691 function get_entity_dates($type = '', $subtype = '', $container_guid = 0, $site_guid = 0,
692  $order_by = 'time_created') {
693  return _elgg_services()->entityTable->getDates(
694  $type, $subtype, $container_guid, $site_guid, $order_by);
695 }
696 
712  global $CONFIG;
713 
714  $type = strtolower($type);
715  if (!in_array($type, $CONFIG->entity_types)) {
716  return false;
717  }
718 
719  if (!isset($CONFIG->registered_entities)) {
720  $CONFIG->registered_entities = array();
721  }
722 
723  if (!isset($CONFIG->registered_entities[$type])) {
724  $CONFIG->registered_entities[$type] = array();
725  }
726 
727  if ($subtype) {
728  $CONFIG->registered_entities[$type][] = $subtype;
729  }
730 
731  return true;
732 }
733 
747  global $CONFIG;
748 
749  $type = strtolower($type);
750  if (!in_array($type, $CONFIG->entity_types)) {
751  return false;
752  }
753 
754  if (!isset($CONFIG->registered_entities)) {
755  return false;
756  }
757 
758  if (!isset($CONFIG->registered_entities[$type])) {
759  return false;
760  }
761 
762  if ($subtype) {
763  if (in_array($subtype, $CONFIG->registered_entities[$type])) {
764  $key = array_search($subtype, $CONFIG->registered_entities[$type]);
765  unset($CONFIG->registered_entities[$type][$key]);
766  } else {
767  return false;
768  }
769  } else {
770  unset($CONFIG->registered_entities[$type]);
771  }
772 
773  return true;
774 }
775 
785  global $CONFIG;
786 
787  if (!isset($CONFIG->registered_entities)) {
788  return false;
789  }
790  if ($type) {
791  $type = strtolower($type);
792  }
793  if (!empty($type) && empty($CONFIG->registered_entities[$type])) {
794  return false;
795  }
796 
797  if (empty($type)) {
798  return $CONFIG->registered_entities;
799  }
800 
801  return $CONFIG->registered_entities[$type];
802 }
803 
813  global $CONFIG;
814 
815  if (!isset($CONFIG->registered_entities)) {
816  return false;
817  }
818 
819  $type = strtolower($type);
820 
821  // @todo registering a subtype implicitly registers the type.
822  // see #2684
823  if (!isset($CONFIG->registered_entities[$type])) {
824  return false;
825  }
826 
827  if ($subtype && !in_array($subtype, $CONFIG->registered_entities[$type])) {
828  return false;
829  }
830  return true;
831 }
832 
851 function elgg_list_registered_entities(array $options = array()) {
852  global $autofeed;
853  $autofeed = true;
854 
855  $defaults = array(
856  'full_view' => false,
857  'allowed_types' => true,
858  'list_type_toggle' => false,
859  'pagination' => true,
860  'offset' => 0,
861  'types' => array(),
862  'type_subtype_pairs' => array(),
863  );
864 
865  $options = array_merge($defaults, $options);
866 
867  // backward compatibility
868  if (isset($options['view_type_toggle'])) {
869  elgg_deprecated_notice("Option 'view_type_toggle' deprecated by 'list_type_toggle' in elgg_list* functions", 1.9);
870  $options['list_type_toggle'] = $options['view_type_toggle'];
871  }
872 
873  $types = get_registered_entity_types();
874 
875  foreach ($types as $type => $subtype_array) {
876  if (in_array($type, $options['allowed_types']) || $options['allowed_types'] === true) {
877  // you must explicitly register types to show up in here and in search for objects
878  if ($type == 'object') {
879  if (is_array($subtype_array) && count($subtype_array)) {
880  $options['type_subtype_pairs'][$type] = $subtype_array;
881  }
882  } else {
883  if (is_array($subtype_array) && count($subtype_array)) {
884  $options['type_subtype_pairs'][$type] = $subtype_array;
885  } else {
886  $options['type_subtype_pairs'][$type] = ELGG_ENTITIES_ANY_VALUE;
887  }
888  }
889  }
890  }
891 
892  if (!empty($options['type_subtype_pairs'])) {
893  $count = elgg_get_entities(array_merge(array('count' => true), $options));
894  if ($count > 0) {
895  $entities = elgg_get_entities($options);
896  } else {
897  $entities = array();
898  }
899  } else {
900  $count = 0;
901  $entities = array();
902  }
903 
904  $options['count'] = $count;
905  return elgg_view_entity_list($entities, $options);
906 }
907 
922 function elgg_instanceof($entity, $type = null, $subtype = null, $class = null) {
924 
925  if ($type) {
926  /* @var \ElggEntity $entity */
927  $return = $return && ($entity->getType() == $type);
928  }
929 
930  if ($subtype) {
931  $return = $return && ($entity->getSubtype() == $subtype);
932  }
933 
934  if ($class) {
935  $return = $return && ($entity instanceof $class);
936  }
937 
938  return $return;
939 }
940 
954  return _elgg_services()->entityTable->updateLastAction($guid, $posted);
955 }
956 
967 function _elgg_entities_test($hook, $type, $value) {
968  global $CONFIG;
969  $value[] = $CONFIG->path . 'engine/tests/ElggEntityTest.php';
970  $value[] = $CONFIG->path . 'engine/tests/ElggCoreAttributeLoaderTest.php';
971  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesTest.php';
972  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromAnnotationsTest.php';
973  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromMetadataTest.php';
974  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromPrivateSettingsTest.php';
975  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromRelationshipTest.php';
976  $value[] = $CONFIG->path . 'engine/tests/ElggCoreGetEntitiesFromAttributesTest.php';
977  $value[] = $CONFIG->path . 'engine/tests/ElggEntityPreloaderIntegrationTest.php';
978  return $value;
979 }
980 
989  elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_entities_test');
990 }
991 
992 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
993  $events->registerHandler('init', 'system', '_elgg_entities_init');
994 };
_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:658
$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:535
update_entity_last_action($guid, $posted=null)
Update the last_action column in the entities table for $guid.
Definition: entities.php:953
_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: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:691
$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:746
$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:703
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:812
get_registered_entity_types($type=null)
Returns registered entity types and subtypes.
Definition: entities.php:784
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:922
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:1967
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)
Definition: elgglib.php:775
_elgg_entities_test($hook, $type, $value)
Runs unit tests for the entity objects.
Definition: entities.php:967
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
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:586
elgg_list_registered_entities(array $options=array())
Returns a viewable list of entities based on the registered types.
Definition: entities.php:851
if(!$limit=(int) elgg_extract('limit', $vars, elgg_get_config('default_limit'))) $offset_key
Definition: pagination.php:31
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
if(elgg_in_context('widget')) $count
Definition: pagination.php:20
$row
_elgg_get_entity_attribute_where_sql(array $options=array())
Get the join and where clauses for working with entity attributes.
Definition: entities.php:670
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:711
$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:984
$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
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:988
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