28 $this->CONFIG = $CONFIG;
55 return _elgg_services()->db->getDataRow(
"SELECT * from {$this->CONFIG->dbprefix}entities where guid=$guid and $access");
74 if (!(
$row instanceof \stdClass)) {
78 if ((!isset(
$row->guid)) || (!isset(
$row->subtype))) {
85 static $newentity_cache;
87 $newentity_cache = new \ElggMemcache(
'new_entity_cache');
89 if ($newentity_cache) {
90 $new_entity = $newentity_cache->load(
$row->guid);
98 if ($classname !=
"") {
99 if (class_exists($classname)) {
100 $new_entity =
new $classname(
$row);
103 $msg = $classname .
" is not a " .
'\ElggEntity' .
".";
104 throw new \ClassException($msg);
107 error_log(
"Class '" . $classname .
"' was not found, missing plugin?");
113 switch (
$row->type) {
115 $new_entity = new \ElggObject(
$row);
121 $new_entity = new \ElggGroup(
$row);
124 $new_entity = new \ElggSite(
$row);
127 $msg =
"Entity type " .
$row->type .
" is not supported.";
128 throw new \InstallationException($msg);
133 if (($newentity_cache) && ($new_entity)) {
134 $newentity_cache->save($new_entity->guid, $new_entity);
175 return $entities ? $entities[0] :
false;
195 $query =
"SELECT count(*) as total FROM {$this->CONFIG->dbprefix}entities WHERE guid = $guid";
317 'site_guids' => $this->CONFIG->site_guid,
324 'reverse_order_by' =>
false,
325 'order_by' =>
'e.time_created desc',
330 'selects' => array(),
334 'preload_owners' =>
false,
335 'preload_containers' =>
false,
336 'callback' =>
'entity_row_to_elggstar',
340 '__ElggBatch' => null,
347 if (isset($options[
'type_subtype_pair'])) {
348 if (isset($options[
'type_subtype_pairs'])) {
349 $options[
'type_subtype_pairs'] = array_merge($options[
'type_subtype_pairs'],
350 $options[
'type_subtype_pair']);
352 $options[
'type_subtype_pairs'] = $options[
'type_subtype_pair'];
356 $singulars = array(
'type',
'subtype',
'guid',
'owner_guid',
'container_guid',
'site_guid');
362 if (!is_array($options[
'wheres'])) {
363 $options[
'wheres'] = array($options[
'wheres']);
366 $wheres = $options[
'wheres'];
369 $options[
'subtypes'], $options[
'type_subtype_pairs']);
377 $options[
'created_time_lower'], $options[
'modified_time_upper'], $options[
'modified_time_lower']);
381 foreach ($wheres as $i => $where) {
382 if ($where ===
false) {
384 } elseif (empty($where)) {
390 $wheres = array_unique($wheres);
393 if (!is_array($options[
'joins'])) {
394 $options[
'joins'] = array($options[
'joins']);
398 $joins = array_unique($options[
'joins']);
400 foreach ($joins as $i => $join) {
401 if ($join ===
false) {
403 } elseif (empty($join)) {
409 if ($options[
'selects']) {
411 foreach ($options[
'selects'] as $select) {
412 $selects .=
", $select";
418 if (!$options[
'count']) {
419 $distinct = $options[
'distinct'] ?
"DISTINCT" :
"";
420 $query =
"SELECT $distinct e.*{$selects} FROM {$this->CONFIG->dbprefix}entities e ";
423 $count_expr = $options[
'distinct'] ?
"DISTINCT e.guid" :
"*";
424 $query =
"SELECT COUNT($count_expr) as total FROM {$this->CONFIG->dbprefix}entities e ";
428 foreach ($joins as $j) {
435 foreach ($wheres as $w) {
436 $query .=
" $w AND ";
443 if ($options[
'reverse_order_by']) {
447 if ($options[
'count']) {
449 return (
int)$total->total;
452 if ($options[
'group_by']) {
453 $query .=
" GROUP BY {$options['group_by']}";
456 if ($options[
'order_by']) {
457 $query .=
" ORDER BY {$options['order_by']}";
460 if ($options[
'limit']) {
463 $query .=
" LIMIT $offset, $limit";
466 if ($options[
'callback'] ===
'entity_row_to_elggstar') {
469 $results =
_elgg_services()->db->getData($query, $options[
'callback']);
479 foreach ($results as
$item) {
497 if (count($results) > 1) {
498 $props_to_preload = [];
499 if ($options[
'preload_owners']) {
500 $props_to_preload[] =
'owner_guid';
502 if ($options[
'preload_containers']) {
503 $props_to_preload[] =
'container_guid';
505 if ($props_to_preload) {
509 _elgg_services()->entityPreloader->preload($results, $props_to_preload);
526 if (!is_array($options[
'types'])
527 || count($options[
'types']) !== 1
528 || !empty($options[
'selects'])
529 || !empty($options[
'wheres'])
530 || !empty($options[
'joins'])
531 || $options[
'callback'] !==
'entity_row_to_elggstar' 532 || $options[
'count']) {
539 'object' =>
'ElggObject',
540 'user' =>
'ElggUser',
541 'group' =>
'ElggGroup',
542 'site' =>
'ElggSite',
546 $type = reset($options[
'types']);
547 if (empty($join_types[$type])) {
553 if (!is_callable([$join_types[$type],
'getExternalAttributes'])) {
560 $options[
'selects'][] =
"st.$col";
564 $options[
'joins'][] =
"JOIN {$this->CONFIG->dbprefix}{$type}s_entity st ON (e.guid = st.guid)";
580 static $plugin_subtype;
581 if (null === $plugin_subtype) {
588 $types_to_optimize = array(
590 'user' =>
'password',
598 $lookup_types = array();
600 $guid_to_key = array();
604 &&
$rows[0]->subtype == $plugin_subtype) {
608 $types_to_optimize = array();
613 if (empty(
$row->guid) || empty(
$row->type)) {
614 throw new \LogicException(
'Entity row missing guid or type');
622 if (isset($types_to_optimize[
$row->type])) {
624 if (isset(
$row->{$types_to_optimize[
$row->type]})) {
629 $lookup_types[
$row->type][] =
$row->guid;
630 $guid_to_key[
$row->guid] = $i;
637 foreach ($lookup_types as $type =>
$guids) {
638 $set =
"(" . implode(
',',
$guids) .
")";
639 $sql =
"SELECT * FROM {$dbprefix}{$type}s_entity WHERE guid IN $set";
641 if ($secondary_rows) {
642 foreach ($secondary_rows as $secondary_row) {
643 $key = $guid_to_key[$secondary_row->guid];
663 $batch->reportIncompleteEntity(
$row);
685 _elgg_services()->logger->warn(
"Cannot set subtypes without type.");
699 if (!is_array($pairs)) {
700 if (!is_array($types)) {
701 $types = array($types);
709 $valid_types_count = count($types);
710 $valid_subtypes_count = 0;
718 foreach ($types as $type) {
719 if (!in_array($type, $valid_types)) {
720 $valid_types_count--;
721 unset($types[array_search($type, $types)]);
724 $valid_subtypes_count += count(
$subtypes);
729 if (!$valid_types_count) {
735 foreach ($types as $type) {
736 $subtype_ids = array();
743 } elseif (!$subtype) {
751 $subtype_ids[] = $subtype_id;
753 $valid_subtypes_count--;
754 _elgg_services()->logger->notice(
"Type-subtype '$type:$subtype' does not exist!");
761 if ($valid_subtypes_count <= 0) {
766 if (is_array($subtype_ids) && count($subtype_ids)) {
767 $subtype_ids_str = implode(
',', $subtype_ids);
768 $wheres[] =
"({$table}.type = '$type' AND {$table}.subtype IN ($subtype_ids_str))";
770 $wheres[] =
"({$table}.type = '$type')";
775 $valid_pairs_count = count($pairs);
776 $valid_pairs_subtypes_count = 0;
781 foreach ($pairs as $paired_type => $paired_subtypes) {
782 if (!in_array($paired_type, $valid_types)) {
783 $valid_pairs_count--;
784 unset($pairs[array_search($paired_type, $pairs)]);
786 if ($paired_subtypes && !is_array($paired_subtypes)) {
787 $pairs[$paired_type] = array($paired_subtypes);
789 $valid_pairs_subtypes_count += count($paired_subtypes);
793 if ($valid_pairs_count <= 0) {
796 foreach ($pairs as $paired_type => $paired_subtypes) {
799 if (is_array($paired_subtypes)) {
800 $paired_subtype_ids = array();
801 foreach ($paired_subtypes as $paired_subtype) {
803 || ($paired_subtype_id =
get_subtype_id($paired_type, $paired_subtype))) {
808 $valid_pairs_subtypes_count--;
809 _elgg_services()->logger->notice(
"Type-subtype '$paired_type:$paired_subtype' does not exist!");
816 if ($valid_pairs_subtypes_count <= 0) {
821 if ($paired_subtype_ids_str = implode(
',', $paired_subtype_ids)) {
822 $wheres[] =
"({$table}.type = '$paired_type'" 823 .
" AND {$table}.subtype IN ($paired_subtype_ids_str))";
826 $wheres[] =
"({$table}.type = '$paired_type')";
832 if (is_array($wheres) && count($wheres)) {
833 $where = implode(
' OR ', $wheres);
862 $guids_sanitized = array();
871 $guids_sanitized[] =
$guid;
875 $guid_str = implode(
',', $guids_sanitized);
878 if ($guid_str !==
false && $guid_str !==
'') {
879 $where =
"($column IN ($guid_str))";
899 $time_created_lower = null, $time_updated_upper = null, $time_updated_lower = null) {
904 if ($time_created_upper && $time_created_upper ==
sanitise_int($time_created_upper)) {
905 $wheres[] =
"{$table}.time_created <= $time_created_upper";
908 if ($time_created_lower && $time_created_lower ==
sanitise_int($time_created_lower)) {
909 $wheres[] =
"{$table}.time_created >= $time_created_lower";
912 if ($time_updated_upper && $time_updated_upper ==
sanitise_int($time_updated_upper)) {
913 $wheres[] =
"{$table}.time_updated <= $time_updated_upper";
916 if ($time_updated_lower && $time_updated_lower ==
sanitise_int($time_updated_lower)) {
917 $wheres[] =
"{$table}.time_updated >= $time_updated_lower";
920 if (is_array($wheres) && count($wheres) > 0) {
921 $where_str = implode(
' AND ', $wheres);
922 return "($where_str)";
962 'attribute_name_value_pairs_operator' =>
'AND',
967 $singulars = array(
'type',
'attribute_name_value_pair');
976 } elseif (!isset(
$options[
'wheres'])) {
985 } elseif (!isset(
$options[
'joins'])) {
1005 throw new \InvalidArgumentException(
"The entity type must be defined for elgg_get_entities_from_attributes()");
1009 throw new \InvalidArgumentException(
"Only one type can be passed to elgg_get_entities_from_attributes()");
1014 if (is_array($type)) {
1019 if (!in_array($type, array(
'group',
'object',
'site',
'user'))) {
1020 throw new \InvalidArgumentException(
"Invalid type '$type' passed to elgg_get_entities_from_attributes()");
1024 $type_table =
"{$this->CONFIG->dbprefix}{$type}s_entity";
1028 'wheres' => array(),
1036 if (!is_array(
$options[
'attribute_name_value_pairs'])) {
1037 throw new \InvalidArgumentException(
"attribute_name_value_pairs must be an array for elgg_get_entities_from_attributes()");
1043 $pairs =
$options[
'attribute_name_value_pairs'];
1044 if (isset($pairs[
'name']) || isset($pairs[
'value'])) {
1045 $pairs = array($pairs);
1048 $pair_wheres = array();
1049 foreach ($pairs as $index => $pair) {
1051 if (!isset($pair[
'name']) || !isset($pair[
'value'])) {
1055 if (isset($pair[
'operand'])) {
1061 if (is_numeric($pair[
'value'])) {
1063 }
else if (is_array($pair[
'value'])) {
1064 $values_array = array();
1065 foreach ($pair[
'value'] as $pair_value) {
1066 if (is_numeric($pair_value)) {
1074 if ($values_array) {
1075 $value =
'(' . implode(
', ', $values_array) .
')';
1086 if (isset($pair[
'case_sensitive'])) {
1087 $pair_binary = ($pair[
'case_sensitive']) ?
'BINARY ' :
'';
1090 $pair_wheres[] =
"({$pair_binary}type_table.$name $operand $value)";
1093 if ($where = implode(
" {$options['attribute_name_value_pairs_operator']} ", $pair_wheres)) {
1094 $return[
'wheres'][] =
"($where)";
1096 $return[
'joins'][] =
"JOIN $type_table type_table ON e.guid = type_table.guid";
1120 $order_by =
'time_created') {
1124 $site_guid = (int) $site_guid;
1125 if ($site_guid == 0) {
1126 $site_guid = $this->CONFIG->site_guid;
1132 $where[] =
"type='$type'";
1138 foreach (
$subtype as $typekey => $subtypearray) {
1139 foreach ($subtypearray as $subtypeval) {
1141 if (!empty($subtypeval)) {
1142 if (!$subtypeval = (
int)
get_subtype_id($typekey, $subtypeval)) {
1148 if (!empty($tempwhere)) {
1149 $tempwhere .=
" or ";
1151 $tempwhere .=
"(type = '{$typekey}' and subtype = {$subtypeval})";
1155 if (!empty($tempwhere)) {
1156 $where[] =
"({$tempwhere})";
1163 $where[] =
"subtype=$subtype_id";
1173 $where[] =
"container_guid in (" . implode(
",",
$container_guid) .
")";
1176 $where[] =
"container_guid = {$container_guid}";
1180 if ($site_guid > 0) {
1181 $where[] =
"site_guid = {$site_guid}";
1186 $sql =
"SELECT DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(time_created)) AS yearmonth 1187 FROM {$this->CONFIG->dbprefix}entities where ";
1189 foreach ($where as $w) {
1193 $sql .=
"1=1 ORDER BY $order_by";
1195 $endresult = array();
1197 $endresult[] = $res->yearmonth;
1227 $query =
"UPDATE {$this->CONFIG->dbprefix}entities SET last_action = {$posted} WHERE guid = {$guid}";
enable($guid, $recursive=true)
Enable an entity.
if($guid==elgg_get_logged_in_user_guid()) $name
_elgg_retrieve_cached_entity($guid)
Retrieve a entity from the cache.
get_subtype_id($type, $subtype)
Return the id for a given subtype.
const ELGG_ENTITIES_NO_VALUE
_elgg_get_guid_based_where_sql($column, $guids)
Returns SQL where clause for owner and containers.
_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.
_elgg_cache_entity(\ElggEntity $entity)
Cache an entity.
$guid
Removes an admin notice.
autoJoinTables(array $options)
Decorate getEntities() options in order to auto-join secondary tables where it's safe to do so...
_elgg_sql_reverse_order_by_clause($order_by)
Reverses the ordering in an ORDER BY clause.
sanitize_string($string)
Sanitize a string for database use.
getRow($guid)
Returns a database row from the entities table.
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.
get_subtype_class_from_id($subtype_id)
Returns the class name for a subtype id.
entity_row_to_elggstar($row)
Create an Elgg* object from a given entity row.
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.
updateLastAction($guid, $posted=null)
Update the last_action column in the entities table for $guid.
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.
sanitise_string($string)
Wrapper function for alternate English spelling (.
const ELGG_ENTITIES_ANY_VALUE
elgg global
Pointer to the global context.
getEntityAttributeWhereSql(array $options=array())
Get the join and where clauses for working with entity attributes.
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.
_elgg_get_entity_type_subtype_where_sql($table, $types, $subtypes, $pairs)
Returns SQL where clause for type and subtype on main entity table.
sanitise_int($int, $signed=true)
Sanitizes an integer for database use.
_elgg_get_entity_attribute_where_sql(array $options=array())
Get the join and where clauses for working with entity attributes.
is_memcache_available()
Return true if memcache is available and configured.
getEntitiesFromAttributes(array $options=array())
Gets entities based upon attributes in secondary tables.
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.
sanitize_int($int, $signed=true)
Sanitizes an integer for database use.
getEntityTypeSubtypeWhereSql($table, $types, $subtypes, $pairs)
Returns SQL where clause for type and subtype on main entity table.
_elgg_get_access_where_sql(array $options=array())
Returns the SQL where clause for enforcing read access to data.
_elgg_fetch_entities_from_sql($sql,\ElggBatch $batch=null)
Return entities from an SQL query generated by elgg_get_entities.
get_entity($guid)
Loads and returns an entity object from a guid.
__construct()
Constructor.