79 switch (
$options[
'metastring_type']) {
82 $callback =
'row_to_elggmetadata';
87 $type =
'annotations';
88 $callback =
'row_to_elggannotation';
117 'metastring_case_sensitive' =>
true,
129 'order_by' =>
'n_table.time_created ASC, n_table.id ASC',
133 'selects' => array(),
138 'preload_owners' =>
false,
139 'callback' => $callback,
149 if (isset(
$options[
'type_subtype_pair'])) {
150 if (isset(
$options[
'type_subtype_pairs'])) {
151 $options[
'type_subtype_pairs'] = array_merge(
$options[
'type_subtype_pairs'],
159 'type',
'subtype',
'type_subtype_pair',
160 'guid',
'owner_guid',
'container_guid',
'site_guid',
161 'metastring_name',
'metastring_value',
162 'metastring_owner_guid',
'metastring_id',
163 'select',
'where',
'join' 175 if (!is_array(
$options[
'wheres'])) {
195 $options[
'metastring_created_time_lower'], null, null);
198 $options[
'metastring_owner_guids']);
202 foreach ($wheres as $i => $where) {
203 if ($where ===
false) {
205 } elseif (empty($where)) {
211 $wheres = array_unique($wheres);
219 $joins[] =
"JOIN {$db_prefix}entities e ON n_table.entity_guid = e.guid";
222 if (!is_array(
$options[
'selects'])) {
232 $custom_callback = (
$options[
'callback'] ==
'row_to_elggmetadata' 233 ||
$options[
'callback'] ==
'row_to_elggannotation');
234 $is_calculation =
$options[
'metastring_calculation'] ?
true :
false;
236 if ($custom_callback || $is_calculation) {
237 $joins[] =
"JOIN {$db_prefix}metastrings n on n_table.name_id = n.id";
238 $joins[] =
"JOIN {$db_prefix}metastrings v on n_table.value_id = v.id";
240 $selects[] =
'n.string as name';
241 $selects[] =
'v.string as value';
245 $joins = array_merge($joins,
$options[
'joins']);
250 $options[
'metastring_case_sensitive']);
252 if ($metastring_clauses) {
253 $wheres = array_merge($wheres, $metastring_clauses[
'wheres']);
254 $joins = array_merge($joins, $metastring_clauses[
'joins']);
257 'table_alias' =>
'n_table',
258 'guid_column' =>
'entity_guid',
262 $distinct =
$options[
'distinct'] ?
"DISTINCT " :
"";
265 $selects = array_unique($selects);
269 foreach ($selects as $select) {
270 $select_str .=
", $select";
274 $query =
"SELECT $distinct n_table.*{$select_str} FROM {$db_prefix}$type n_table";
277 $query =
"SELECT count($distinct e.guid) as calculation FROM {$db_prefix}$type n_table";
279 $query =
"SELECT {$options['metastring_calculation']}(v.string) as calculation FROM {$db_prefix}$type n_table";
282 foreach ($joins as $i => $join) {
283 if ($join ===
false) {
285 } elseif (empty($join)) {
291 $joins = array_unique($joins);
294 foreach ($joins as $j) {
301 foreach ($wheres as $w) {
302 $query .=
" $w AND ";
316 $query .=
" GROUP BY {$options['group_by']}";
321 $query .=
" ORDER BY {$options['order_by']}, n_table.id";
327 $query .=
" LIMIT $offset, $limit";
332 if (
$options[
'preload_owners'] && is_array($dt) && count($dt) > 1) {
359 $pairs = null, $ids = null, $case_sensitive =
false) {
361 if ((!$names && $names !== 0)
362 && (!$values && $values !== 0)
364 && (!$pairs && $pairs !== 0)) {
374 $binary = ($case_sensitive) ?
' BINARY ' :
'';
385 if ($names !== null) {
386 if (!is_array($names)) {
387 $names = array($names);
390 $sanitised_names = array();
391 foreach ($names as
$name) {
399 if ($names_str = implode(
',', $sanitised_names)) {
400 $return[
'joins'][] =
"JOIN {$db_prefix}metastrings msn on $table.name_id = msn.id";
401 $names_where =
"(msn.string IN ($names_str))";
407 if ($values !== null) {
408 if (!is_array($values)) {
409 $values = array($values);
412 $sanitised_values = array();
413 foreach ($values as
$value) {
421 if ($values_str = implode(
',', $sanitised_values)) {
422 $return[
'joins'][] =
"JOIN {$db_prefix}metastrings msv on $table.value_id = msv.id";
423 $values_where =
"({$binary}msv.string IN ($values_str))";
428 if (!is_array($ids)) {
432 $ids_str = implode(
',', $ids);
435 $wheres[] =
"n_table.id IN ($ids_str)";
439 if ($names_where && $values_where) {
440 $wheres[] =
"($names_where AND $values_where)";
441 } elseif ($names_where) {
442 $wheres[] = $names_where;
443 } elseif ($values_where) {
444 $wheres[] = $values_where;
449 'guid_column' =>
'entity_guid',
452 if ($where = implode(
' AND ', $wheres)) {
453 $return[
'wheres'][] =
"($where)";
476 $prefixes = array(
'metadata_',
'annotation_',
'annotations_');
480 'names' =>
'metastring_names',
481 'values' =>
'metastring_values',
482 'case_sensitive' =>
'metastring_case_sensitive',
483 'owner_guids' =>
'metastring_owner_guids',
484 'created_time_lower' =>
'metastring_created_time_lower',
485 'created_time_upper' =>
'metastring_created_time_upper',
486 'calculation' =>
'metastring_calculation',
487 'ids' =>
'metastring_ids',
490 foreach ($prefixes as $prefix) {
491 $singulars = array(
"{$prefix}name",
"{$prefix}value",
"{$prefix}owner_guid",
"{$prefix}id");
494 foreach ($map as $specific => $normalized) {
495 $key = $prefix . $specific;
528 $type =
'annotation';
529 $table =
"{$db_prefix}annotations";
533 $table =
"{$db_prefix}metadata";
577 if (!$options || !is_array($options)) {
581 $batch = new \ElggBatch(
'_elgg_get_metastring_based_objects', $options, $callback, 50, $inc_offset);
582 return $batch->callbackResult;
600 'metastring_type' =>
$type,
601 'metastring_id' =>
$id,
606 if ($obj && count($obj) == 1) {
629 $type =
'annotation';
646 if (
$type ==
'metadata') {
647 static $metabyname_memcache;
649 $metabyname_memcache = new \ElggMemcache(
'metabyname_memcache');
652 if ($metabyname_memcache) {
654 $metabyname_memcache->delete(
"{$obj->entity_guid}:{$obj->name_id}");
658 if ($obj->canEdit()) {
661 if (
$type ==
"annotation") {
669 return (
bool)
delete_data(
"DELETE FROM $table WHERE id = $id");
687 $valid_types = array(
'metadata',
'annotation');
688 if (!in_array(
$type, $valid_types)) {
694 $n_table = (
$type ==
'annotation') ?
'annotations' :
$type;
696 $singulars = array(
"{$type}_name",
"{$type}_value",
697 "{$type}_name_value_pair",
"{$type}_owner_guid");
702 $options[
"{$type}_name_value_pairs_operator"],
$options[
"{$type}_case_sensitive"],
709 } elseif (!isset(
$options[
'wheres'])) {
718 } elseif (!isset(
$options[
'joins'])) {
724 if ($clauses[
'orders']) {
725 $order_by_metadata = implode(
", ", $clauses[
'orders']);
727 $options[
'order_by'] =
"$order_by_metadata, {$options['order_by']}";
729 $options[
'order_by'] =
"$order_by_metadata, e.time_created DESC";
749 $value[] = $CONFIG->path .
'engine/tests/ElggCoreMetastringsTest.php';
754 $hooks->registerHandler(
'unit_test',
'system',
'_elgg_metastrings_test');
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
get_data_row($query, $callback="")
Retrieve a single row from the database.
if($guid==elgg_get_logged_in_user_guid()) $name
const ELGG_ENTITIES_NO_VALUE
_elgg_get_guid_based_where_sql($column, $guids)
Returns SQL where clause for owner and containers.
get_config($name, $site_guid=0)
Gets a configuration value.
_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.
delete_data($query)
Remove a row from the database.
_elgg_sql_reverse_order_by_clause($order_by)
Reverses the ordering in an ORDER BY clause.
update_data($query)
Update a row in the database.
$enabled
Sample cli installer script.
sanitise_string($string)
Wrapper function for alternate English spelling (.
const ELGG_ENTITIES_ANY_VALUE
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
elgg global
Pointer to the global context.
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
get_data($query, $callback="")
Retrieve rows from the database.
_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.
is_memcache_available()
Return true if memcache is available and configured.
_elgg_normalize_plural_options_array($options, $singulars)
Normalise the singular keys in an options array to plural keys.
_elgg_get_access_where_sql(array $options=array())
Returns the SQL where clause for enforcing read access to data.
elgg_trigger_event($event, $object_type, $object=null)
if(!$collection_name) $id
if(!$num_display) $db_prefix