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";
321 'site_guids' => $this->CONFIG->site_guid,
328 'reverse_order_by' =>
false,
329 'order_by' =>
'e.time_created desc',
334 'selects' => array(),
338 'preload_owners' =>
false,
339 'preload_containers' =>
false,
340 'callback' =>
'entity_row_to_elggstar',
344 '__ElggBatch' => null,
351 if (isset($options[
'type_subtype_pair'])) {
352 if (isset($options[
'type_subtype_pairs'])) {
353 $options[
'type_subtype_pairs'] = array_merge($options[
'type_subtype_pairs'],
354 $options[
'type_subtype_pair']);
356 $options[
'type_subtype_pairs'] = $options[
'type_subtype_pair'];
360 $singulars = array(
'type',
'subtype',
'guid',
'owner_guid',
'container_guid',
'site_guid');
366 if (!is_array($options[
'wheres'])) {
367 $options[
'wheres'] = array($options[
'wheres']);
370 $wheres = $options[
'wheres'];
373 $options[
'subtypes'], $options[
'type_subtype_pairs']);
381 $options[
'created_time_lower'], $options[
'modified_time_upper'], $options[
'modified_time_lower']);
385 foreach ($wheres as $i => $where) {
386 if ($where ===
false) {
388 } elseif (empty($where)) {
394 $wheres = array_unique($wheres);
397 if (!is_array($options[
'joins'])) {
398 $options[
'joins'] = array($options[
'joins']);
402 $joins = array_unique($options[
'joins']);
404 foreach ($joins as $i => $join) {
405 if ($join ===
false) {
407 } elseif (empty($join)) {
413 if ($options[
'selects']) {
415 foreach ($options[
'selects'] as $select) {
416 $selects .=
", $select";
422 if (!$options[
'count']) {
423 $distinct = $options[
'distinct'] ?
"DISTINCT" :
"";
424 $query =
"SELECT $distinct e.*{$selects} FROM {$this->CONFIG->dbprefix}entities e ";
427 $count_expr = $options[
'distinct'] ?
"DISTINCT e.guid" :
"*";
428 $query =
"SELECT COUNT($count_expr) as total FROM {$this->CONFIG->dbprefix}entities e ";
432 foreach ($joins as $j) {
439 foreach ($wheres as $w) {
440 $query .=
" $w AND ";
447 if ($options[
'reverse_order_by']) {
451 if ($options[
'count']) {
453 return (
int)$total->total;
456 if ($options[
'group_by']) {
457 $query .=
" GROUP BY {$options['group_by']}";
460 if ($options[
'order_by']) {
461 $query .=
" ORDER BY {$options['order_by']}";
464 if ($options[
'limit']) {
467 $query .=
" LIMIT $offset, $limit";
470 if ($options[
'callback'] ===
'entity_row_to_elggstar') {
473 $results =
_elgg_services()->db->getData($query, $options[
'callback']);
483 foreach ($results as
$item) {
501 if (count($results) > 1) {
502 $props_to_preload = [];
503 if ($options[
'preload_owners']) {
504 $props_to_preload[] =
'owner_guid';
506 if ($options[
'preload_containers']) {
507 $props_to_preload[] =
'container_guid';
509 if ($props_to_preload) {
513 _elgg_services()->entityPreloader->preload($results, $props_to_preload);
530 if (!is_array($options[
'types'])
531 || count($options[
'types']) !== 1
532 || !empty($options[
'selects'])
533 || !empty($options[
'wheres'])
534 || !empty($options[
'joins'])
535 || $options[
'callback'] !==
'entity_row_to_elggstar' 536 || $options[
'count']) {
543 'object' =>
'ElggObject',
544 'user' =>
'ElggUser',
545 'group' =>
'ElggGroup',
546 'site' =>
'ElggSite',
550 $type = reset($options[
'types']);
551 if (empty($join_types[$type])) {
557 if (!is_callable([$join_types[$type],
'getExternalAttributes'])) {
564 $options[
'selects'][] =
"st.$col";
568 $options[
'joins'][] =
"JOIN {$this->CONFIG->dbprefix}{$type}s_entity st ON (e.guid = st.guid)";
584 static $plugin_subtype;
585 if (null === $plugin_subtype) {
592 $types_to_optimize = array(
594 'user' =>
'password',
602 $lookup_types = array();
604 $guid_to_key = array();
608 &&
$rows[0]->subtype == $plugin_subtype) {
612 $types_to_optimize = array();
617 if (empty(
$row->guid) || empty(
$row->type)) {
618 throw new \LogicException(
'Entity row missing guid or type');
626 if (isset($types_to_optimize[
$row->type])) {
628 if (isset(
$row->{$types_to_optimize[
$row->type]})) {
633 $lookup_types[
$row->type][] =
$row->guid;
634 $guid_to_key[
$row->guid] = $i;
641 foreach ($lookup_types as $type =>
$guids) {
642 $set =
"(" . implode(
',',
$guids) .
")";
643 $sql =
"SELECT * FROM {$dbprefix}{$type}s_entity WHERE guid IN $set";
645 if ($secondary_rows) {
646 foreach ($secondary_rows as $secondary_row) {
647 $key = $guid_to_key[$secondary_row->guid];
667 $batch->reportIncompleteEntity(
$row);
689 _elgg_services()->logger->warn(
"Cannot set subtypes without type.");
703 if (!is_array($pairs)) {
704 if (!is_array($types)) {
705 $types = array($types);
713 $valid_types_count = count($types);
714 $valid_subtypes_count = 0;
722 foreach ($types as $type) {
723 if (!in_array($type, $valid_types)) {
724 $valid_types_count--;
725 unset($types[array_search($type, $types)]);
728 $valid_subtypes_count += count(
$subtypes);
733 if (!$valid_types_count) {
739 foreach ($types as $type) {
740 $subtype_ids = array();
747 } elseif (!$subtype) {
755 $subtype_ids[] = $subtype_id;
757 $valid_subtypes_count--;
758 _elgg_services()->logger->notice(
"Type-subtype '$type:$subtype' does not exist!");
765 if ($valid_subtypes_count <= 0) {
770 if (is_array($subtype_ids) && count($subtype_ids)) {
771 $subtype_ids_str = implode(
',', $subtype_ids);
772 $wheres[] =
"({$table}.type = '$type' AND {$table}.subtype IN ($subtype_ids_str))";
774 $wheres[] =
"({$table}.type = '$type')";
779 $valid_pairs_count = count($pairs);
780 $valid_pairs_subtypes_count = 0;
785 foreach ($pairs as $paired_type => $paired_subtypes) {
786 if (!in_array($paired_type, $valid_types)) {
787 $valid_pairs_count--;
788 unset($pairs[array_search($paired_type, $pairs)]);
790 if ($paired_subtypes && !is_array($paired_subtypes)) {
791 $pairs[$paired_type] = array($paired_subtypes);
793 $valid_pairs_subtypes_count += count($paired_subtypes);
797 if ($valid_pairs_count <= 0) {
800 foreach ($pairs as $paired_type => $paired_subtypes) {
803 if (is_array($paired_subtypes)) {
804 $paired_subtype_ids = array();
805 foreach ($paired_subtypes as $paired_subtype) {
807 || ($paired_subtype_id =
get_subtype_id($paired_type, $paired_subtype))) {
812 $valid_pairs_subtypes_count--;
813 _elgg_services()->logger->notice(
"Type-subtype '$paired_type:$paired_subtype' does not exist!");
820 if ($valid_pairs_subtypes_count <= 0) {
825 if ($paired_subtype_ids_str = implode(
',', $paired_subtype_ids)) {
826 $wheres[] =
"({$table}.type = '$paired_type'" 827 .
" AND {$table}.subtype IN ($paired_subtype_ids_str))";
830 $wheres[] =
"({$table}.type = '$paired_type')";
836 if (is_array($wheres) && count($wheres)) {
837 $where = implode(
' OR ', $wheres);
866 $guids_sanitized = array();
875 $guids_sanitized[] =
$guid;
879 $guid_str = implode(
',', $guids_sanitized);
882 if ($guid_str !==
false && $guid_str !==
'') {
883 $where =
"($column IN ($guid_str))";
903 $time_created_lower = null, $time_updated_upper = null, $time_updated_lower = null) {
908 if ($time_created_upper && $time_created_upper ==
sanitise_int($time_created_upper)) {
909 $wheres[] =
"{$table}.time_created <= $time_created_upper";
912 if ($time_created_lower && $time_created_lower ==
sanitise_int($time_created_lower)) {
913 $wheres[] =
"{$table}.time_created >= $time_created_lower";
916 if ($time_updated_upper && $time_updated_upper ==
sanitise_int($time_updated_upper)) {
917 $wheres[] =
"{$table}.time_updated <= $time_updated_upper";
920 if ($time_updated_lower && $time_updated_lower ==
sanitise_int($time_updated_lower)) {
921 $wheres[] =
"{$table}.time_updated >= $time_updated_lower";
924 if (is_array($wheres) && count($wheres) > 0) {
925 $where_str = implode(
' AND ', $wheres);
926 return "($where_str)";
966 'attribute_name_value_pairs_operator' =>
'AND',
971 $singulars = array(
'type',
'attribute_name_value_pair');
980 } elseif (!isset(
$options[
'wheres'])) {
989 } elseif (!isset(
$options[
'joins'])) {
1009 throw new \InvalidArgumentException(
"The entity type must be defined for elgg_get_entities_from_attributes()");
1013 throw new \InvalidArgumentException(
"Only one type can be passed to elgg_get_entities_from_attributes()");
1018 if (is_array($type)) {
1023 if (!in_array($type, array(
'group',
'object',
'site',
'user'))) {
1024 throw new \InvalidArgumentException(
"Invalid type '$type' passed to elgg_get_entities_from_attributes()");
1028 $type_table =
"{$this->CONFIG->dbprefix}{$type}s_entity";
1032 'wheres' => array(),
1040 if (!is_array(
$options[
'attribute_name_value_pairs'])) {
1041 throw new \InvalidArgumentException(
"attribute_name_value_pairs must be an array for elgg_get_entities_from_attributes()");
1047 $pairs =
$options[
'attribute_name_value_pairs'];
1048 if (isset($pairs[
'name']) || isset($pairs[
'value'])) {
1049 $pairs = array($pairs);
1052 $pair_wheres = array();
1053 foreach ($pairs as $index => $pair) {
1055 if (!isset($pair[
'name']) || !isset($pair[
'value'])) {
1059 if (isset($pair[
'operand'])) {
1065 if (is_numeric($pair[
'value'])) {
1067 }
else if (is_array($pair[
'value'])) {
1068 $values_array = array();
1069 foreach ($pair[
'value'] as $pair_value) {
1070 if (is_numeric($pair_value)) {
1078 if ($values_array) {
1079 $value =
'(' . implode(
', ', $values_array) .
')';
1090 if (isset($pair[
'case_sensitive'])) {
1091 $pair_binary = ($pair[
'case_sensitive']) ?
'BINARY ' :
'';
1094 $pair_wheres[] =
"({$pair_binary}type_table.$name $operand $value)";
1097 if ($where = implode(
" {$options['attribute_name_value_pairs_operator']} ", $pair_wheres)) {
1098 $return[
'wheres'][] =
"($where)";
1100 $return[
'joins'][] =
"JOIN $type_table type_table ON e.guid = type_table.guid";
1124 $order_by =
'time_created') {
1128 $site_guid = (int) $site_guid;
1129 if ($site_guid == 0) {
1130 $site_guid = $this->CONFIG->site_guid;
1136 $where[] =
"type='$type'";
1142 foreach (
$subtype as $typekey => $subtypearray) {
1143 foreach ($subtypearray as $subtypeval) {
1145 if (!empty($subtypeval)) {
1146 if (!$subtypeval = (
int)
get_subtype_id($typekey, $subtypeval)) {
1152 if (!empty($tempwhere)) {
1153 $tempwhere .=
" or ";
1155 $tempwhere .=
"(type = '{$typekey}' and subtype = {$subtypeval})";
1159 if (!empty($tempwhere)) {
1160 $where[] =
"({$tempwhere})";
1167 $where[] =
"subtype=$subtype_id";
1177 $where[] =
"container_guid in (" . implode(
",",
$container_guid) .
")";
1180 $where[] =
"container_guid = {$container_guid}";
1184 if ($site_guid > 0) {
1185 $where[] =
"site_guid = {$site_guid}";
1190 $sql =
"SELECT DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(time_created)) AS yearmonth 1191 FROM {$this->CONFIG->dbprefix}entities where ";
1193 foreach ($where as $w) {
1197 $sql .=
"1=1 ORDER BY $order_by";
1199 $endresult = array();
1201 $endresult[] = $res->yearmonth;
1231 $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.