38 if ($case_sensitive) {
47 static $metastrings_memcache;
49 $metastrings_memcache =
new ElggMemcache(
'metastrings_memcache');
51 if ($metastrings_memcache) {
52 $msfc = $metastrings_memcache->load(
$string);
60 if ($case_sensitive) {
61 $query =
"SELECT * FROM {$CONFIG->dbprefix}metastrings WHERE string = BINARY '$escaped_string' LIMIT 1";
63 $query =
"SELECT * FROM {$CONFIG->dbprefix}metastrings WHERE string = '$escaped_string'";
68 if (is_array($results)) {
69 if (!$case_sensitive) {
76 }
else if (isset($results[0])) {
77 $id = $results[0]->id;
87 if ($metastrings_memcache) {
107 return insert_data(
"INSERT INTO {$CONFIG->dbprefix}metastrings (string) VALUES ('$escaped_string')");
122 SELECT * FROM {$CONFIG->dbprefix}metastrings WHERE 124 (id NOT IN (SELECT name_id FROM {$CONFIG->dbprefix}metadata)) AND 125 (id NOT IN (SELECT value_id FROM {$CONFIG->dbprefix}metadata)) AND 126 (id NOT IN (SELECT name_id FROM {$CONFIG->dbprefix}annotations)) AND 127 (id NOT IN (SELECT value_id FROM {$CONFIG->dbprefix}annotations)) 132 static $metastrings_memcache;
133 if (!$metastrings_memcache) {
134 $metastrings_memcache =
new ElggMemcache(
'metastrings_memcache');
137 foreach ($dead as $d) {
138 $metastrings_memcache->delete($d->string);
144 DELETE FROM {$CONFIG->dbprefix}metastrings WHERE 146 (id NOT IN (SELECT name_id FROM {$CONFIG->dbprefix}metadata)) AND 147 (id NOT IN (SELECT value_id FROM {$CONFIG->dbprefix}metadata)) AND 148 (id NOT IN (SELECT name_id FROM {$CONFIG->dbprefix}annotations)) AND 149 (id NOT IN (SELECT value_id FROM {$CONFIG->dbprefix}annotations)) 193 switch (
$options[
'metastring_type']) {
196 $callback =
'row_to_elggmetadata';
201 $type =
'annotations';
202 $callback =
'row_to_elggannotation';
231 'metastring_case_sensitive' =>
true,
243 'order_by' =>
'n_table.time_created asc',
247 'selects' => array(),
251 'callback' => $callback,
261 if (isset(
$options[
'type_subtype_pair'])) {
262 if (isset(
$options[
'type_subtype_pairs'])) {
263 $options[
'type_subtype_pairs'] = array_merge(
$options[
'type_subtype_pairs'],
271 'type',
'subtype',
'type_subtype_pair',
272 'guid',
'owner_guid',
'container_guid',
'site_guid',
273 'metastring_name',
'metastring_value',
274 'metastring_owner_guid',
'metastring_id',
275 'select',
'where',
'join' 287 if (!is_array(
$options[
'wheres'])) {
307 $options[
'metastring_created_time_lower'], null, null);
310 $options[
'metastring_owner_guids']);
314 foreach ($wheres as $i => $where) {
315 if ($where ===
false) {
317 } elseif (empty($where)) {
323 $wheres = array_unique($wheres);
331 $joins[] =
"JOIN {$db_prefix}entities e ON n_table.entity_guid = e.guid";
334 if (!is_array(
$options[
'selects'])) {
344 $custom_callback = (
$options[
'callback'] ==
'row_to_elggmetadata' 345 ||
$options[
'callback'] ==
'row_to_elggannotation');
346 $is_calculation =
$options[
'metastring_calculation'] ?
true :
false;
348 if ($custom_callback || $is_calculation) {
349 $joins[] =
"JOIN {$db_prefix}metastrings n on n_table.name_id = n.id";
350 $joins[] =
"JOIN {$db_prefix}metastrings v on n_table.value_id = v.id";
352 $selects[] =
'n.string as name';
353 $selects[] =
'v.string as value';
356 foreach ($joins as $i => $join) {
357 if ($join ===
false) {
359 } elseif (empty($join)) {
367 $options[
'metastring_case_sensitive']);
369 if ($metastring_clauses) {
370 $wheres = array_merge($wheres, $metastring_clauses[
'wheres']);
371 $joins = array_merge($joins, $metastring_clauses[
'joins']);
377 $selects = array_unique($selects);
381 foreach ($selects as $select) {
382 $select_str .=
", $select";
386 $query =
"SELECT DISTINCT n_table.*{$select_str} FROM {$db_prefix}$type n_table";
389 $query =
"SELECT count(DISTINCT e.guid) as calculation FROM {$db_prefix}$type n_table";
391 $query =
"SELECT {$options['metastring_calculation']}(v.string) as calculation FROM {$db_prefix}$type n_table";
395 $joins = array_unique($joins);
398 foreach ($joins as $j) {
405 foreach ($wheres as $w) {
406 $query .=
" $w AND ";
420 $query .=
" GROUP BY {$options['group_by']}";
425 $query .=
" ORDER BY {$options['order_by']}, n_table.id";
431 $query .=
" LIMIT $offset, $limit";
458 $pairs = null, $ids = null, $case_sensitive =
false) {
460 if ((!$names && $names !== 0)
461 && (!$values && $values !== 0)
463 && (!$pairs && $pairs !== 0)) {
473 $binary = ($case_sensitive) ?
' BINARY ' :
'';
484 if ($names !== null) {
485 if (!is_array($names)) {
486 $names = array($names);
489 $sanitised_names = array();
490 foreach ($names as
$name) {
498 if ($names_str = implode(
',', $sanitised_names)) {
499 $return[
'joins'][] =
"JOIN {$db_prefix}metastrings msn on $table.name_id = msn.id";
500 $names_where =
"(msn.string IN ($names_str))";
506 if ($values !== null) {
507 if (!is_array($values)) {
508 $values = array($values);
511 $sanitised_values = array();
512 foreach ($values as
$value) {
520 if ($values_str = implode(
',', $sanitised_values)) {
521 $return[
'joins'][] =
"JOIN {$db_prefix}metastrings msv on $table.value_id = msv.id";
522 $values_where =
"({$binary}msv.string IN ($values_str))";
527 if (!is_array($ids)) {
531 $ids_str = implode(
',', $ids);
534 $wheres[] =
"n_table.id IN ($ids_str)";
538 if ($names_where && $values_where) {
539 $wheres[] =
"($names_where AND $values_where)";
540 } elseif ($names_where) {
541 $wheres[] = $names_where;
542 } elseif ($values_where) {
543 $wheres[] = $values_where;
548 if ($where = implode(
' AND ', $wheres)) {
549 $return[
'wheres'][] =
"($where)";
572 $prefixes = array(
'metadata_',
'annotation_',
'annotations_');
576 'names' =>
'metastring_names',
577 'values' =>
'metastring_values',
578 'case_sensitive' =>
'metastring_case_sensitive',
579 'owner_guids' =>
'metastring_owner_guids',
580 'created_time_lower' =>
'metastring_created_time_lower',
581 'created_time_upper' =>
'metastring_created_time_upper',
582 'calculation' =>
'metastring_calculation',
583 'ids' =>
'metastring_ids',
586 foreach ($prefixes as $prefix) {
587 $singulars = array(
"{$prefix}name",
"{$prefix}value",
"{$prefix}owner_guid",
"{$prefix}id");
590 foreach ($map as $specific => $normalized) {
591 $key = $prefix . $specific;
624 $type =
'annotation';
625 $table =
"{$db_prefix}annotations";
629 $table =
"{$db_prefix}metadata";
673 if (!$options || !is_array($options)) {
677 $batch =
new ElggBatch(
'_elgg_get_metastring_based_objects', $options, $callback, 50, $inc_offset);
678 return $batch->callbackResult;
696 'metastring_type' =>
$type,
697 'metastring_id' =>
$id,
702 if ($obj && count($obj) == 1) {
725 $type =
'annotation';
742 if (
$type ==
'metadata') {
743 static $metabyname_memcache;
745 $metabyname_memcache =
new ElggMemcache(
'metabyname_memcache');
748 if ($metabyname_memcache) {
750 $metabyname_memcache->delete(
"{$obj->entity_guid}:{$obj->name_id}");
754 if ($obj->canEdit()) {
757 if (
$type ==
"annotation") {
765 return (
bool)
delete_data(
"DELETE FROM $table WHERE id = $id");
783 $valid_types = array(
'metadata',
'annotation');
784 if (!in_array(
$type, $valid_types)) {
790 $n_table = (
$type ==
'annotation') ?
'annotations' :
$type;
792 $singulars = array(
"{$type}_name",
"{$type}_value",
793 "{$type}_name_value_pair",
"{$type}_owner_guid");
798 $options[
"{$type}_name_value_pairs_operator"],
$options[
"{$type}_case_sensitive"],
805 } elseif (!isset(
$options[
'wheres'])) {
814 } elseif (!isset(
$options[
'joins'])) {
820 if ($clauses[
'orders']) {
821 $order_by_metadata = implode(
", ", $clauses[
'orders']);
823 $options[
'order_by'] =
"$order_by_metadata, {$options['order_by']}";
825 $options[
'order_by'] =
"$order_by_metadata, e.time_created DESC";
845 $value[] = $CONFIG->path .
'engine/tests/ElggCoreMetastringsTest.php';
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.
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Register a callback as a plugin hook handler.
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.
insert_data($query)
Insert a row into 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)
Sends 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)
Trigger an Elgg Event and attempt to run all handler callbacks registered to that event...
if(!$collection_name) $id
if(!$num_display) $db_prefix