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';
244 foreach ($joins as $i => $join) {
245 if ($join ===
false) {
247 } elseif (empty($join)) {
255 $options[
'metastring_case_sensitive']);
257 if ($metastring_clauses) {
258 $wheres = array_merge($wheres, $metastring_clauses[
'wheres']);
259 $joins = array_merge($joins, $metastring_clauses[
'joins']);
264 $distinct =
$options[
'distinct'] ?
"DISTINCT " :
"";
267 $selects = array_unique($selects);
271 foreach ($selects as $select) {
272 $select_str .=
", $select";
276 $query =
"SELECT $distinct n_table.*{$select_str} FROM {$db_prefix}$type n_table";
279 $query =
"SELECT count($distinct e.guid) as calculation FROM {$db_prefix}$type n_table";
281 $query =
"SELECT {$options['metastring_calculation']}(v.string) as calculation FROM {$db_prefix}$type n_table";
285 $joins = array_unique($joins);
288 foreach ($joins as $j) {
295 foreach ($wheres as $w) {
296 $query .=
" $w AND ";
310 $query .=
" GROUP BY {$options['group_by']}";
315 $query .=
" ORDER BY {$options['order_by']}, n_table.id";
321 $query .=
" LIMIT $offset, $limit";
326 if (
$options[
'preload_owners'] && is_array($dt) && count($dt) > 1) {
353 $pairs = null, $ids = null, $case_sensitive =
false) {
355 if ((!$names && $names !== 0)
356 && (!$values && $values !== 0)
358 && (!$pairs && $pairs !== 0)) {
368 $binary = ($case_sensitive) ?
' BINARY ' :
'';
379 if ($names !== null) {
380 if (!is_array($names)) {
381 $names = array($names);
384 $sanitised_names = array();
385 foreach ($names as
$name) {
393 if ($names_str = implode(
',', $sanitised_names)) {
394 $return[
'joins'][] =
"JOIN {$db_prefix}metastrings msn on $table.name_id = msn.id";
395 $names_where =
"(msn.string IN ($names_str))";
401 if ($values !== null) {
402 if (!is_array($values)) {
403 $values = array($values);
406 $sanitised_values = array();
407 foreach ($values as
$value) {
415 if ($values_str = implode(
',', $sanitised_values)) {
416 $return[
'joins'][] =
"JOIN {$db_prefix}metastrings msv on $table.value_id = msv.id";
417 $values_where =
"({$binary}msv.string IN ($values_str))";
422 if (!is_array($ids)) {
426 $ids_str = implode(
',', $ids);
429 $wheres[] =
"n_table.id IN ($ids_str)";
433 if ($names_where && $values_where) {
434 $wheres[] =
"($names_where AND $values_where)";
435 } elseif ($names_where) {
436 $wheres[] = $names_where;
437 } elseif ($values_where) {
438 $wheres[] = $values_where;
443 if ($where = implode(
' AND ', $wheres)) {
444 $return[
'wheres'][] =
"($where)";
467 $prefixes = array(
'metadata_',
'annotation_',
'annotations_');
471 'names' =>
'metastring_names',
472 'values' =>
'metastring_values',
473 'case_sensitive' =>
'metastring_case_sensitive',
474 'owner_guids' =>
'metastring_owner_guids',
475 'created_time_lower' =>
'metastring_created_time_lower',
476 'created_time_upper' =>
'metastring_created_time_upper',
477 'calculation' =>
'metastring_calculation',
478 'ids' =>
'metastring_ids',
481 foreach ($prefixes as $prefix) {
482 $singulars = array(
"{$prefix}name",
"{$prefix}value",
"{$prefix}owner_guid",
"{$prefix}id");
485 foreach ($map as $specific => $normalized) {
486 $key = $prefix . $specific;
519 $type =
'annotation';
520 $table =
"{$db_prefix}annotations";
524 $table =
"{$db_prefix}metadata";
568 if (!$options || !is_array($options)) {
572 $batch = new \ElggBatch(
'_elgg_get_metastring_based_objects', $options, $callback, 50, $inc_offset);
573 return $batch->callbackResult;
591 'metastring_type' =>
$type,
592 'metastring_id' =>
$id,
597 if ($obj && count($obj) == 1) {
620 $type =
'annotation';
637 if (
$type ==
'metadata') {
638 static $metabyname_memcache;
640 $metabyname_memcache = new \ElggMemcache(
'metabyname_memcache');
643 if ($metabyname_memcache) {
645 $metabyname_memcache->delete(
"{$obj->entity_guid}:{$obj->name_id}");
649 if ($obj->canEdit()) {
652 if (
$type ==
"annotation") {
660 return (
bool)
delete_data(
"DELETE FROM $table WHERE id = $id");
678 $valid_types = array(
'metadata',
'annotation');
679 if (!in_array(
$type, $valid_types)) {
685 $n_table = (
$type ==
'annotation') ?
'annotations' :
$type;
687 $singulars = array(
"{$type}_name",
"{$type}_value",
688 "{$type}_name_value_pair",
"{$type}_owner_guid");
693 $options[
"{$type}_name_value_pairs_operator"],
$options[
"{$type}_case_sensitive"],
700 } elseif (!isset(
$options[
'wheres'])) {
709 } elseif (!isset(
$options[
'joins'])) {
715 if ($clauses[
'orders']) {
716 $order_by_metadata = implode(
", ", $clauses[
'orders']);
718 $options[
'order_by'] =
"$order_by_metadata, {$options['order_by']}";
720 $options[
'order_by'] =
"$order_by_metadata, e.time_created DESC";
740 $value[] = $CONFIG->path .
'engine/tests/ElggCoreMetastringsTest.php';
745 $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.
elgg_extract($key, array $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
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.
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