124 $this->
table = $this->db->prefix .
'entities';
162 $sql =
"SELECT * FROM {$this->db->prefix}entities 163 WHERE guid = :guid AND $access";
166 ':guid' => (int)
$guid,
169 return $this->db->getDataRow($sql, null,
$params);
180 $sql =
"INSERT INTO {$this->db->prefix}entities 181 (type, subtype, owner_guid, site_guid, container_guid, 182 access_id, time_created, time_updated, last_action) 184 (:type, :subtype_id, :owner_guid, :site_guid, :container_guid, 185 :access_id, :time_created, :time_updated, :last_action)";
187 return $this->db->insertData($sql, [
188 ':type' => $row->type,
189 ':subtype_id' => $row->subtype_id,
190 ':owner_guid' => $row->owner_guid,
191 ':site_guid' => $row->site_guid,
192 ':container_guid' => $row->container_guid,
193 ':access_id' => $row->access_id,
194 ':time_created' => $row->time_created,
195 ':time_updated' => $row->time_updated,
196 ':last_action' => $row->last_action,
209 UPDATE {$this->db->prefix}entities 210 SET owner_guid = :owner_guid, 211 access_id = :access_id, 212 container_guid = :container_guid, 213 time_created = :time_created, 214 time_updated = :time_updated 219 ':owner_guid' => $row->owner_guid,
220 ':access_id' => $row->access_id,
221 ':container_guid' => $row->container_guid,
222 ':time_created' => $row->time_created,
223 ':time_updated' => $row->time_updated,
227 return $this->db->updateData($sql,
false,
$params);
251 if (!isset(
$row->guid) || !isset(
$row->subtype)) {
255 $class_name = $this->subtype_table->getClassFromId(
$row->subtype);
256 if ($class_name && !class_exists($class_name)) {
257 $this->logger->error(
"Class '$class_name' was not found, missing plugin?");
269 if (isset($map[
$row->type])) {
270 $class_name = $map[
$row->type];
308 $this->entity_cache->set(
$entity);
343 if ($type &&
$row->type != $type) {
370 $ia = $this->session->setIgnoreAccess(
true);
375 $this->session->setIgnoreAccess($ia);
513 'site_guids' => $this->config->get(
'site_guid'),
520 'reverse_order_by' =>
false,
521 'order_by' =>
'e.time_created desc',
523 'limit' => $this->config->get(
'default_limit'),
526 'selects' => array(),
530 'preload_owners' =>
false,
531 'preload_containers' =>
false,
532 'callback' =>
'entity_row_to_elggstar',
536 'batch_inc_offset' =>
true,
540 '__ElggBatch' => null,
545 if ($options[
'batch'] && !$options[
'count']) {
546 $batch_size = $options[
'batch_size'];
547 $batch_inc_offset = $options[
'batch_inc_offset'];
550 unset($options[
'batch'], $options[
'batch_size'], $options[
'batch_inc_offset']);
552 return new \ElggBatch([$this,
'getEntities'], $options, null, $batch_size, $batch_inc_offset);
557 if (isset($options[
'type_subtype_pair'])) {
558 if (isset($options[
'type_subtype_pairs'])) {
559 $options[
'type_subtype_pairs'] = array_merge($options[
'type_subtype_pairs'],
560 $options[
'type_subtype_pair']);
562 $options[
'type_subtype_pairs'] = $options[
'type_subtype_pair'];
566 $singulars = array(
'type',
'subtype',
'guid',
'owner_guid',
'container_guid',
'site_guid');
572 if (!is_array($options[
'wheres'])) {
573 $options[
'wheres'] = array($options[
'wheres']);
576 $wheres = $options[
'wheres'];
579 $options[
'subtypes'], $options[
'type_subtype_pairs']);
587 $options[
'created_time_lower'], $options[
'modified_time_upper'], $options[
'modified_time_lower']);
591 foreach ($wheres as $i => $where) {
592 if ($where ===
false) {
594 } elseif (empty($where)) {
600 $wheres = array_unique($wheres);
603 if (!is_array($options[
'joins'])) {
604 $options[
'joins'] = array($options[
'joins']);
608 $joins = array_unique($options[
'joins']);
610 foreach ($joins as $i => $join) {
611 if ($join ===
false) {
613 } elseif (empty($join)) {
619 if ($options[
'selects']) {
621 foreach ($options[
'selects'] as
$select) {
622 $selects .=
", $select";
628 if (!$options[
'count']) {
629 $distinct = $options[
'distinct'] ?
"DISTINCT" :
"";
630 $query =
"SELECT $distinct e.*{$selects} FROM {$this->db->prefix}entities e ";
633 $count_expr = $options[
'distinct'] ?
"DISTINCT e.guid" :
"*";
634 $query =
"SELECT COUNT($count_expr) as total FROM {$this->db->prefix}entities e ";
638 foreach ($joins as $j) {
645 foreach ($wheres as $w) {
646 $query .=
" $w AND ";
653 if ($options[
'reverse_order_by']) {
657 if ($options[
'count']) {
658 $total = $this->db->getDataRow($query);
659 return (
int) $total->total;
662 if ($options[
'group_by']) {
663 $query .=
" GROUP BY {$options['group_by']}";
666 if ($options[
'order_by']) {
667 $query .=
" ORDER BY {$options['order_by']}";
670 if ($options[
'limit']) {
673 $query .=
" LIMIT $offset, $limit";
676 if ($options[
'callback'] ===
'entity_row_to_elggstar') {
677 $results = $this->
fetchFromSql($query, $options[
'__ElggBatch']);
679 $results = $this->db->getData($query, $options[
'callback']);
689 foreach ($results as
$item) {
692 $this->entity_cache->set($item);
704 $this->metadata_cache->populateFromEntities(
$guids);
707 if (count($results) > 1) {
708 $props_to_preload = [];
709 if ($options[
'preload_owners']) {
710 $props_to_preload[] =
'owner_guid';
712 if ($options[
'preload_containers']) {
713 $props_to_preload[] =
'container_guid';
715 if ($props_to_preload) {
719 _elgg_services()->entityPreloader->preload($results, $props_to_preload);
736 if (!is_array($options[
'types'])
737 || count($options[
'types']) !== 1
738 || !empty($options[
'selects'])
739 || !empty($options[
'wheres'])
740 || !empty($options[
'joins'])
741 || $options[
'callback'] !==
'entity_row_to_elggstar' 742 || $options[
'count']) {
749 'object' =>
'ElggObject',
750 'user' =>
'ElggUser',
751 'group' =>
'ElggGroup',
752 'site' =>
'ElggSite',
756 $type = reset($options[
'types']);
757 if (empty($join_types[$type])) {
763 if (!is_callable([$join_types[$type],
'getExternalAttributes'])) {
770 $options[
'selects'][] =
"st.$col";
774 $options[
'joins'][] =
"JOIN {$this->db->prefix}{$type}s_entity st ON (e.guid = st.guid)";
790 $plugin_subtype = $this->subtype_table->getId(
'object',
'plugin');
795 $types_to_optimize = array(
797 'user' =>
'password',
802 $rows = $this->db->getData($sql);
805 $lookup_types = array();
807 $guid_to_key = array();
811 &&
$rows[0]->subtype == $plugin_subtype) {
815 $types_to_optimize = array();
820 if (empty(
$row->guid) || empty(
$row->type)) {
834 if (isset($types_to_optimize[
$row->type])) {
836 if (isset(
$row->{$types_to_optimize[
$row->type]})) {
841 $lookup_types[
$row->type][] =
$row->guid;
842 $guid_to_key[
$row->guid] = $i;
847 foreach ($lookup_types as $type =>
$guids) {
848 $set =
"(" . implode(
',',
$guids) .
")";
849 $sql =
"SELECT * FROM {$this->db->prefix}{$type}s_entity WHERE guid IN $set";
850 $secondary_rows = $this->db->getData($sql);
851 if ($secondary_rows) {
852 foreach ($secondary_rows as $secondary_row) {
853 $key = $guid_to_key[$secondary_row->guid];
873 $batch->reportIncompleteEntity(
$row);
895 $this->logger->warn(
"Cannot set subtypes without type.");
905 $valid_types = $this->config->get(
'entity_types');
909 if (!is_array($pairs)) {
910 if (!is_array($types)) {
911 $types = array($types);
919 $valid_types_count = count($types);
920 $valid_subtypes_count = 0;
928 foreach ($types as $type) {
929 if (!in_array($type, $valid_types)) {
930 $valid_types_count--;
931 unset($types[array_search($type, $types)]);
934 $valid_subtypes_count += count(
$subtypes);
939 if (!$valid_types_count) {
945 foreach ($types as $type) {
946 $subtype_ids = array();
953 } elseif (!$subtype) {
961 $subtype_ids[] = $subtype_id;
963 $valid_subtypes_count--;
964 $this->logger->notice(
"Type-subtype '$type:$subtype' does not exist!");
971 if ($valid_subtypes_count <= 0) {
976 if (is_array($subtype_ids) && count($subtype_ids)) {
977 $subtype_ids_str = implode(
',', $subtype_ids);
978 $wheres[] =
"({$table}.type = '$type' AND {$table}.subtype IN ($subtype_ids_str))";
980 $wheres[] =
"({$table}.type = '$type')";
985 $valid_pairs_count = count($pairs);
986 $valid_pairs_subtypes_count = 0;
991 foreach ($pairs as $paired_type => $paired_subtypes) {
992 if (!in_array($paired_type, $valid_types)) {
993 $valid_pairs_count--;
994 unset($pairs[array_search($paired_type, $pairs)]);
996 if ($paired_subtypes && !is_array($paired_subtypes)) {
997 $pairs[$paired_type] = array($paired_subtypes);
999 $valid_pairs_subtypes_count += count($paired_subtypes);
1003 if ($valid_pairs_count <= 0) {
1006 foreach ($pairs as $paired_type => $paired_subtypes) {
1009 if (is_array($paired_subtypes)) {
1010 $paired_subtype_ids = array();
1011 foreach ($paired_subtypes as $paired_subtype) {
1017 $valid_pairs_subtypes_count--;
1018 $this->logger->notice(
"Type-subtype '$paired_type:$paired_subtype' does not exist!");
1025 if ($valid_pairs_subtypes_count <= 0) {
1030 if ($paired_subtype_ids_str = implode(
',', $paired_subtype_ids)) {
1031 $wheres[] =
"({$table}.type = '$paired_type'" 1032 .
" AND {$table}.subtype IN ($paired_subtype_ids_str))";
1035 $wheres[] =
"({$table}.type = '$paired_type')";
1041 if (is_array($wheres) && count($wheres)) {
1042 $where = implode(
' OR ', $wheres);
1071 $guids_sanitized = array();
1080 $guids_sanitized[] =
$guid;
1084 $guid_str = implode(
',', $guids_sanitized);
1087 if ($guid_str !==
false && $guid_str !==
'') {
1088 $where =
"($column IN ($guid_str))";
1108 $time_created_lower = null, $time_updated_upper = null, $time_updated_lower = null) {
1113 if ($time_created_upper && $time_created_upper ==
sanitise_int($time_created_upper)) {
1114 $wheres[] =
"{$table}.time_created <= $time_created_upper";
1117 if ($time_created_lower && $time_created_lower ==
sanitise_int($time_created_lower)) {
1118 $wheres[] =
"{$table}.time_created >= $time_created_lower";
1121 if ($time_updated_upper && $time_updated_upper ==
sanitise_int($time_updated_upper)) {
1122 $wheres[] =
"{$table}.time_updated <= $time_updated_upper";
1125 if ($time_updated_lower && $time_updated_lower ==
sanitise_int($time_updated_lower)) {
1126 $wheres[] =
"{$table}.time_updated >= $time_updated_lower";
1129 if (is_array($wheres) && count($wheres) > 0) {
1130 $where_str = implode(
' AND ', $wheres);
1131 return "($where_str)";
1171 'attribute_name_value_pairs_operator' =>
'AND',
1176 $singulars = array(
'type',
'attribute_name_value_pair');
1185 } elseif (!isset(
$options[
'wheres'])) {
1194 } elseif (!isset(
$options[
'joins'])) {
1223 if (is_array($type)) {
1228 if (!in_array($type, array(
'group',
'object',
'site',
'user'))) {
1233 $type_table =
"{$this->db->prefix}{$type}s_entity";
1237 'wheres' => array(),
1245 if (!is_array(
$options[
'attribute_name_value_pairs'])) {
1246 throw new InvalidArgumentException(
"attribute_name_value_pairs must be an array for elgg_get_entities_from_attributes()");
1252 $pairs =
$options[
'attribute_name_value_pairs'];
1253 if (isset($pairs[
'name']) || isset($pairs[
'value'])) {
1254 $pairs = array($pairs);
1257 $pair_wheres = array();
1258 foreach ($pairs as
$index => $pair) {
1260 if (!isset($pair[
'name']) || !isset($pair[
'value'])) {
1264 if (isset($pair[
'operand'])) {
1270 if (is_numeric($pair[
'value'])) {
1272 }
else if (is_array($pair[
'value'])) {
1273 $values_array = array();
1274 foreach ($pair[
'value'] as $pair_value) {
1275 if (is_numeric($pair_value)) {
1283 if ($values_array) {
1284 $value =
'(' . implode(
', ', $values_array) .
')';
1294 if (isset($pair[
'case_sensitive'])) {
1295 $pair_binary = ($pair[
'case_sensitive']) ?
'BINARY ' :
'';
1298 $pair_wheres[] =
"({$pair_binary}type_table.$name $operand $value)";
1301 if ($where = implode(
" {$options['attribute_name_value_pairs_operator']} ", $pair_wheres)) {
1302 $return[
'wheres'][] =
"($where)";
1304 $return[
'joins'][] =
"JOIN $type_table type_table ON e.guid = type_table.guid";
1329 $site_guid = (int) $site_guid;
1330 if ($site_guid == 0) {
1331 $site_guid = $this->config->get(
'site_guid');
1337 $where[] =
"type='$type'";
1343 foreach (
$subtype as $typekey => $subtypearray) {
1344 foreach ($subtypearray as $subtypeval) {
1346 if (!empty($subtypeval)) {
1347 if (!$subtypeval = (
int)
get_subtype_id($typekey, $subtypeval)) {
1353 if (!empty($tempwhere)) {
1354 $tempwhere .=
" or ";
1356 $tempwhere .=
"(type = '{$typekey}' and subtype = {$subtypeval})";
1360 if (!empty($tempwhere)) {
1361 $where[] =
"({$tempwhere})";
1368 $where[] =
"subtype=$subtype_id";
1378 $where[] =
"container_guid in (" . implode(
",",
$container_guid) .
")";
1381 $where[] =
"container_guid = {$container_guid}";
1385 if ($site_guid > 0) {
1386 $where[] =
"site_guid = {$site_guid}";
1391 $sql =
"SELECT DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(time_created)) AS yearmonth 1392 FROM {$this->db->prefix}entities where ";
1394 foreach ($where as $w) {
1398 $sql .=
"1=1 ORDER BY $order_by";
1399 if (
$result = $this->db->getData($sql)) {
1400 $endresult = array();
1402 $endresult[] = $res->yearmonth;
1427 UPDATE {$this->db->prefix}entities 1428 SET last_action = :last_action 1433 ':last_action' => (int)
$posted,
1434 ':guid' => (
int) $entity->guid,
1439 $entity->last_action =
$posted;
1459 return $this->session->getLoggedInUser();
1463 $ia = $this->session->setIgnoreAccess(
true);
1468 $this->session->setIgnoreAccess($ia);
1474 $message = $this->translator->translate(
'UserFetchFailureException', array(
$guid));
1495 if (!$this->events->trigger(
'disable',
$entity->type,
$entity)) {
1500 UPDATE {$this->table}entities 1502 WHERE owner_guid = :owner_guid 1503 OR container_guid = :owner_guid";
1512 if ($this->db->updateData($query,
true,
$params)) {
insertRow(stdClass $row)
Adds a new row to the entity table.
enable($guid, $recursive=true)
Enable an entity.
_elgg_invalidate_cache_for_entity($entity_guid)
Invalidate entity cache.
getRow($guid, $user_guid=null)
Returns a database row from the entities table.
if($guid==elgg_get_logged_in_user_guid()) $name
get_subtype_id($type, $subtype)
Return the id for a given subtype.
const ELGG_ENTITIES_NO_VALUE
__construct(Conf $config, Database $db, EntityCache $entity_cache, MetadataCache $metadata_cache, SubtypeTable $subtype_table, EventsService $events, ElggSession $session, Translator $translator, Logger $logger)
Constructor.
getCurrentTime($modifier= '')
Get the (cloned) time.
$guid
Removes an admin notice.
_elgg_get_memcache($namespace= 'default')
Get a namespaced ElggMemcache object (if memcache is available) or a null cache.
storeInPersistedCache(\ElggSharedMemoryCache $cache, $last_action=0)
Cache the entity in a persisted cache.
autoJoinTables(array $options)
Decorate getEntities() options in order to auto-join secondary tables where it's safe to do so...
updateLastAction(ElggEntity $entity, $posted=null)
Update the last_action column in the entities table for $guid.
_elgg_sql_reverse_order_by_clause($order_by)
Reverses the ordering in an ORDER BY clause.
sanitize_string($string)
Sanitizes a string for use in a query.
getDates($type= '', $subtype= '', $container_guid=0, $site_guid=0, $order_by= 'time_created')
Returns a list of months in which entities were updated or created.
_elgg_invalidate_memcache_for_entity($entity_guid)
Invalidate an entity in memcache.
elgg_get_ignore_access()
Get current ignore access setting.
getEntityTimeWhereSql($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.
rowToElggStar($row)
Create an Elgg* object from a given entity row.
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
getGuidBasedWhereSql($column, $guids)
Returns SQL where clause for owner and containers.
Volatile cache for entities.
getEntityTypeSubtypeWhereSql($table, $types=[], $subtypes=[], $pairs=[])
Returns SQL where clause for type and subtype on main entity table.
Exception indicating a user could not be looked up for a permissions check.
sanitise_string($string)
Alias of sanitize_string.
const ELGG_ENTITIES_ANY_VALUE
updateRow($guid, stdClass $row)
Update entity table row.
disableEntities($owner_guid)
Disables all entities owned and contained by a user (or another entity)
getEntityAttributeWhereSql(array $options=array())
Get the join and where clauses for working with entity attributes.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
access_get_show_hidden_status()
Return current status of showing disabled entities.
exists($guid)
Does an entity exist?
getEntities(array $options=array())
Returns an array of entities with optional filtering.
fetchFromSql($sql,\ElggBatch $batch=null)
Return entities from an SQL query generated by elgg_get_entities.
access_show_hidden_entities($show_hidden)
Show or hide disabled entities.
getFromCache($guid)
Get an entity from the in-memory or memcache caches.
has_access_to_entity($entity, $user=null)
Can a user access an entity.
sanitise_int($int, $signed=true)
Alias of sanitize_int.
_elgg_get_entity_attribute_where_sql(array $options=array())
Get the join and where clauses for working with entity attributes.
getEntitiesFromAttributes(array $options=array())
Gets entities based upon attributes in secondary tables.
trait TimeUsing
Adds methods for setting the current time (for testing)
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
_elgg_normalize_plural_options_array($options, $singulars)
Normalise the singular keys in an options array to plural keys.
$user_guid
Avatar remove action.
_elgg_get_access_where_sql(array $options=array())
Returns the SQL where clause for enforcing read access to data.
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
get_entity($guid)
Loads and returns an entity object from a guid.
getUserForPermissionsCheck($guid=0)
Get a user by GUID even if the entity is hidden or disabled.