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);
151 static $shared_cache;
167 if (null === $shared_cache) {
169 $shared_cache = new \ElggMemcache(
'new_entity_cache');
171 $shared_cache =
false;
182 $cached_entity = $shared_cache->load(
$guid);
184 if ($cached_entity) {
186 return $cached_entity;
220 $query =
"SELECT count(*) as total FROM {$this->CONFIG->dbprefix}entities WHERE guid = $guid";
337 'site_guids' => $this->CONFIG->site_guid,
344 'reverse_order_by' =>
false,
345 'order_by' =>
'e.time_created desc',
350 'selects' => array(),
354 'preload_owners' =>
false,
355 'callback' =>
'entity_row_to_elggstar',
359 '__ElggBatch' => null,
366 if (isset($options[
'type_subtype_pair'])) {
367 if (isset($options[
'type_subtype_pairs'])) {
368 $options[
'type_subtype_pairs'] = array_merge($options[
'type_subtype_pairs'],
369 $options[
'type_subtype_pair']);
371 $options[
'type_subtype_pairs'] = $options[
'type_subtype_pair'];
375 $singulars = array(
'type',
'subtype',
'guid',
'owner_guid',
'container_guid',
'site_guid');
379 if (!is_array($options[
'wheres'])) {
380 $options[
'wheres'] = array($options[
'wheres']);
383 $wheres = $options[
'wheres'];
386 $options[
'subtypes'], $options[
'type_subtype_pairs']);
394 $options[
'created_time_lower'], $options[
'modified_time_upper'], $options[
'modified_time_lower']);
398 foreach ($wheres as $i => $where) {
399 if ($where ===
false) {
401 } elseif (empty($where)) {
407 $wheres = array_unique($wheres);
410 if (!is_array($options[
'joins'])) {
411 $options[
'joins'] = array($options[
'joins']);
415 $joins = array_unique($options[
'joins']);
417 foreach ($joins as $i => $join) {
418 if ($join ===
false) {
420 } elseif (empty($join)) {
426 if ($options[
'selects']) {
428 foreach ($options[
'selects'] as $select) {
429 $selects .=
", $select";
435 if (!$options[
'count']) {
436 $distinct = $options[
'distinct'] ?
"DISTINCT" :
"";
437 $query =
"SELECT $distinct e.*{$selects} FROM {$this->CONFIG->dbprefix}entities e ";
440 $count_expr = $options[
'distinct'] ?
"DISTINCT e.guid" :
"*";
441 $query =
"SELECT COUNT($count_expr) as total FROM {$this->CONFIG->dbprefix}entities e ";
445 foreach ($joins as $j) {
452 foreach ($wheres as $w) {
453 $query .=
" $w AND ";
460 if ($options[
'reverse_order_by']) {
464 if (!$options[
'count']) {
465 if ($options[
'group_by']) {
466 $query .=
" GROUP BY {$options['group_by']}";
469 if ($options[
'order_by']) {
470 $query .=
" ORDER BY {$options['order_by']}";
473 if ($options[
'limit']) {
476 $query .=
" LIMIT $offset, $limit";
479 if ($options[
'callback'] ===
'entity_row_to_elggstar') {
482 $dt =
_elgg_services()->db->getData($query, $options[
'callback']);
488 foreach ($dt as
$item) {
505 if ($options[
'preload_owners'] && count($dt) > 1) {
512 return (
int)$total->total;
527 static $plugin_subtype;
528 if (null === $plugin_subtype) {
534 $types_to_optimize = array(
536 'user' =>
'password',
543 $lookup_types = array();
545 $guid_to_key = array();
549 &&
$rows[0]->subtype == $plugin_subtype) {
553 $types_to_optimize = array();
558 if (empty(
$row->guid) || empty(
$row->type)) {
559 throw new \LogicException(
'Entity row missing guid or type');
567 if (isset($types_to_optimize[
$row->type])) {
569 if (isset(
$row->{$types_to_optimize[
$row->type]})) {
574 $lookup_types[
$row->type][] =
$row->guid;
575 $guid_to_key[
$row->guid] = $i;
583 $set =
"(" . implode(
',',
$guids) .
")";
584 $sql =
"SELECT * FROM {$dbprefix}{$type}s_entity WHERE guid IN $set";
586 if ($secondary_rows) {
587 foreach ($secondary_rows as $secondary_row) {
588 $key = $guid_to_key[$secondary_row->guid];
608 $batch->reportIncompleteEntity(
$row);
630 _elgg_services()->logger->warn(
"Cannot set subtypes without type.");
644 if (!is_array($pairs)) {
645 if (!is_array($types)) {
646 $types = array($types);
654 $valid_types_count = count($types);
655 $valid_subtypes_count = 0;
663 foreach ($types as
$type) {
664 if (!in_array($type, $valid_types)) {
665 $valid_types_count--;
666 unset($types[array_search($type, $types)]);
669 $valid_subtypes_count += count(
$subtypes);
674 if (!$valid_types_count) {
680 foreach ($types as $type) {
681 $subtype_ids = array();
688 } elseif (!$subtype) {
696 $subtype_ids[] = $subtype_id;
698 $valid_subtypes_count--;
699 _elgg_services()->logger->notice(
"Type-subtype '$type:$subtype' does not exist!");
706 if ($valid_subtypes_count <= 0) {
711 if (is_array($subtype_ids) && count($subtype_ids)) {
712 $subtype_ids_str = implode(
',', $subtype_ids);
713 $wheres[] =
"({$table}.type = '$type' AND {$table}.subtype IN ($subtype_ids_str))";
715 $wheres[] =
"({$table}.type = '$type')";
720 $valid_pairs_count = count($pairs);
721 $valid_pairs_subtypes_count = 0;
726 foreach ($pairs as $paired_type => $paired_subtypes) {
727 if (!in_array($paired_type, $valid_types)) {
728 $valid_pairs_count--;
729 unset($pairs[array_search($paired_type, $pairs)]);
731 if ($paired_subtypes && !is_array($paired_subtypes)) {
732 $pairs[$paired_type] = array($paired_subtypes);
734 $valid_pairs_subtypes_count += count($paired_subtypes);
738 if ($valid_pairs_count <= 0) {
741 foreach ($pairs as $paired_type => $paired_subtypes) {
744 if (is_array($paired_subtypes)) {
745 $paired_subtype_ids = array();
746 foreach ($paired_subtypes as $paired_subtype) {
748 || ($paired_subtype_id =
get_subtype_id($paired_type, $paired_subtype))) {
753 $valid_pairs_subtypes_count--;
754 _elgg_services()->logger->notice(
"Type-subtype '$paired_type:$paired_subtype' does not exist!");
761 if ($valid_pairs_subtypes_count <= 0) {
766 if ($paired_subtype_ids_str = implode(
',', $paired_subtype_ids)) {
767 $wheres[] =
"({$table}.type = '$paired_type'" 768 .
" AND {$table}.subtype IN ($paired_subtype_ids_str))";
771 $wheres[] =
"({$table}.type = '$paired_type')";
777 if (is_array($wheres) && count($wheres)) {
778 $where = implode(
' OR ', $wheres);
807 $guids_sanitized = array();
816 $guids_sanitized[] =
$guid;
820 $guid_str = implode(
',', $guids_sanitized);
823 if ($guid_str !==
false && $guid_str !==
'') {
824 $where =
"($column IN ($guid_str))";
844 $time_created_lower = null, $time_updated_upper = null, $time_updated_lower = null) {
849 if ($time_created_upper && $time_created_upper ==
sanitise_int($time_created_upper)) {
850 $wheres[] =
"{$table}.time_created <= $time_created_upper";
853 if ($time_created_lower && $time_created_lower ==
sanitise_int($time_created_lower)) {
854 $wheres[] =
"{$table}.time_created >= $time_created_lower";
857 if ($time_updated_upper && $time_updated_upper ==
sanitise_int($time_updated_upper)) {
858 $wheres[] =
"{$table}.time_updated <= $time_updated_upper";
861 if ($time_updated_lower && $time_updated_lower ==
sanitise_int($time_updated_lower)) {
862 $wheres[] =
"{$table}.time_updated >= $time_updated_lower";
865 if (is_array($wheres) && count($wheres) > 0) {
866 $where_str = implode(
' AND ', $wheres);
867 return "($where_str)";
907 'attribute_name_value_pairs_operator' =>
'AND',
912 $singulars = array(
'type',
'attribute_name_value_pair');
921 } elseif (!isset(
$options[
'wheres'])) {
930 } elseif (!isset(
$options[
'joins'])) {
950 throw new \InvalidArgumentException(
"The entity type must be defined for elgg_get_entities_from_attributes()");
954 throw new \InvalidArgumentException(
"Only one type can be passed to elgg_get_entities_from_attributes()");
959 if (is_array(
$type)) {
964 if (!in_array(
$type, array(
'group',
'object',
'site',
'user'))) {
965 throw new \InvalidArgumentException(
"Invalid type '$type' passed to elgg_get_entities_from_attributes()");
969 $type_table =
"{$this->CONFIG->dbprefix}{$type}s_entity";
981 if (!is_array(
$options[
'attribute_name_value_pairs'])) {
982 throw new \InvalidArgumentException(
"attribute_name_value_pairs must be an array for elgg_get_entities_from_attributes()");
988 $pairs =
$options[
'attribute_name_value_pairs'];
989 if (isset($pairs[
'name']) || isset($pairs[
'value'])) {
990 $pairs = array($pairs);
993 $pair_wheres = array();
994 foreach ($pairs as $index => $pair) {
996 if (!isset($pair[
'name']) || !isset($pair[
'value'])) {
1000 if (isset($pair[
'operand'])) {
1006 if (is_numeric($pair[
'value'])) {
1008 }
else if (is_array($pair[
'value'])) {
1009 $values_array = array();
1010 foreach ($pair[
'value'] as $pair_value) {
1011 if (is_numeric($pair_value)) {
1019 if ($values_array) {
1020 $value =
'(' . implode(
', ', $values_array) .
')';
1031 if (isset($pair[
'case_sensitive'])) {
1032 $pair_binary = ($pair[
'case_sensitive']) ?
'BINARY ' :
'';
1035 $pair_wheres[] =
"({$pair_binary}type_table.$name $operand $value)";
1038 if ($where = implode(
" {$options['attribute_name_value_pairs_operator']} ", $pair_wheres)) {
1039 $return[
'wheres'][] =
"($where)";
1041 $return[
'joins'][] =
"JOIN $type_table type_table ON e.guid = type_table.guid";
1065 $order_by =
'time_created') {
1069 $site_guid = (int) $site_guid;
1070 if ($site_guid == 0) {
1071 $site_guid = $this->CONFIG->site_guid;
1077 $where[] =
"type='$type'";
1083 foreach (
$subtype as $typekey => $subtypearray) {
1084 foreach ($subtypearray as $subtypeval) {
1086 if (!empty($subtypeval)) {
1087 if (!$subtypeval = (
int)
get_subtype_id($typekey, $subtypeval)) {
1093 if (!empty($tempwhere)) {
1094 $tempwhere .=
" or ";
1096 $tempwhere .=
"(type = '{$typekey}' and subtype = {$subtypeval})";
1100 if (!empty($tempwhere)) {
1101 $where[] =
"({$tempwhere})";
1108 $where[] =
"subtype=$subtype_id";
1113 if ($container_guid !== 0) {
1114 if (is_array($container_guid)) {
1115 foreach ($container_guid as
$key => $val) {
1116 $container_guid[
$key] = (int) $val;
1118 $where[] =
"container_guid in (" . implode(
",", $container_guid) .
")";
1120 $container_guid = (int) $container_guid;
1121 $where[] =
"container_guid = {$container_guid}";
1125 if ($site_guid > 0) {
1126 $where[] =
"site_guid = {$site_guid}";
1131 $sql =
"SELECT DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(time_created)) AS yearmonth 1132 FROM {$this->CONFIG->dbprefix}entities where ";
1134 foreach ($where as $w) {
1138 $sql .=
"1=1 ORDER BY $order_by";
1140 $endresult = array();
1142 $endresult[] = $res->yearmonth;
1172 $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.
if(!$autoload_available) _elgg_services()
$guid
Removes an admin notice.
_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.
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?
get_entity_as_row($guid)
Returns a database row from the entities table.
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.