/** * https://github.com/Elgg/Elgg/issues/4867 */ public function testElggGetEntityMetadataWhereSqlWithFalseValue() { $pair = array('name' => 'test', 'value' => false); $result = elgg_get_entity_metadata_where_sql('e', 'metadata', null, null, $pair); $where = preg_replace('/\\s+/', ' ', $result['wheres'][0]); $this->assertTrue(strpos($where, "msn1.string = 'test' AND BINARY msv1.string = 0") > 0); $result = elgg_get_entity_metadata_where_sql('e', 'metadata', array('test'), array(false)); $where = preg_replace('/\\s+/', ' ', $result['wheres'][0]); $this->assertTrue(strpos($where, "msn.string IN ('test')) AND ( BINARY msv.string IN ('0')")); }
function pleio_api_get_sub_wikis($group_id = 0, $parent_id = 0, $user_id = 0, $offset = 0, $search = null, $filter = 0) { $total = 0; $offset = intval($offset); $list = array(); $user = elgg_get_logged_in_user_entity(); if ($filter == 1) { $user_id = $user->guid; } if (strlen($parent_id) == 32) { $swordfish_group = $parent_id; } else { $swordfish_group = $group_id ? pleio_api_swordfish_group($group_id) : false; } $url = false; if ($swordfish_group) { return pleio_api_get_swordfish_wikis($user, $group_id, $swordfish_group, $parent_id); } else { $wheres = array(); $joins = array(); if ($group_id) { $wheres[] = sprintf("e.container_guid = %d", $group_id); } if ($parent_id) { $more = elgg_get_entity_metadata_where_sql('e', 'metadata', null, null, array("name" => "parent_guid", "value" => (int) $parent_id)); $wheres = array_merge($wheres, $more["wheres"]); $joins = array_merge($joins, $more["joins"]); } if ($user_id) { $wheres[] = sprintf("e.owner_guid = %d", $user_id); } elseif ($filter == 2) { // friends $friends = get_user_friends($user->guid, "", 999999, 0); if (sizeof($friends)) { $wheres[] = " e.owner_guid IN (" . implode(",", array_map(create_function('$user', 'return $user->guid;'), $friends)) . ") "; } else { return array("total" => 0, "list" => array(), "offset" => $offset); } } if ($search) { $search = sanitise_string($search); $wheres[] = " (o.description LIKE '%%{$search}%%' OR o.title LIKE '%%{$search}%%') "; $joins[] = sprintf("INNER JOIN %sobjects_entity o on e.guid = o.guid", get_config("dbprefix")); } $options = array('type' => 'object', 'subtypes' => $parent_id ? array('page_top', 'page') : 'page_top', 'limit' => 20, 'offset' => $offset, 'count' => true, "wheres" => $wheres, "joins" => $joins); $total = elgg_get_entities($options); if ($total) { $options["count"] = false; $options["order_by"] = "e.time_created DESC"; $data = elgg_get_entities($options); foreach ($data as $item) { $export = pleio_api_export($item, array("guid", "time_created", "owner_guid", "container_guid", "title", "site_guid")); $export["likes_count"] = pleio_api_fetch_likes($item->guid); $export["liked"] = 0; if ($export["likes_count"]) { $export["liked"] = pleio_api_fetch_likes($item->guid, 1, 0, 0, $user->guid) > 0 ? 1 : 0; } $list[] = $export; } } } return array("total" => $total, "list" => $list, "offset" => $offset); }
/** * Returns options to pass to elgg_get_entities() for metastrings operations. * * @param string $type Metastring type: annotations or metadata * @param array $options Options * * @return array * @since 1.7.0 * @access private */ function elgg_entities_get_metastrings_options($type, $options) { $valid_types = array('metadata', 'annotation'); if (!in_array($type, $valid_types)) { return FALSE; } // the options for annotations are singular (annotation_name) but the table // is plural (elgg_annotations) so rewrite for the table name. $n_table = $type == 'annotation' ? 'annotations' : $type; $singulars = array("{$type}_name", "{$type}_value", "{$type}_name_value_pair", "{$type}_owner_guid"); $options = elgg_normalise_plural_options_array($options, $singulars); $clauses = elgg_get_entity_metadata_where_sql('e', $n_table, $options["{$type}_names"], $options["{$type}_values"], $options["{$type}_name_value_pairs"], $options["{$type}_name_value_pairs_operator"], $options["{$type}_case_sensitive"], $options["order_by_{$type}"], $options["{$type}_owner_guids"]); if ($clauses) { // merge wheres to pass to get_entities() if (isset($options['wheres']) && !is_array($options['wheres'])) { $options['wheres'] = array($options['wheres']); } elseif (!isset($options['wheres'])) { $options['wheres'] = array(); } $options['wheres'] = array_merge($options['wheres'], $clauses['wheres']); // merge joins to pass to get_entities() if (isset($options['joins']) && !is_array($options['joins'])) { $options['joins'] = array($options['joins']); } elseif (!isset($options['joins'])) { $options['joins'] = array(); } $options['joins'] = array_merge($options['joins'], $clauses['joins']); if ($clauses['orders']) { $order_by_metadata = implode(", ", $clauses['orders']); if (isset($options['order_by']) && $options['order_by']) { $options['order_by'] = "{$order_by_metadata}, {$options['order_by']}"; } else { $options['order_by'] = "{$order_by_metadata}, e.time_created DESC"; } } } return $options; }
/** * Returns entities based upon metadata. Also accepts all * options available to elgg_get_entities(). Supports * the singular option shortcut. * * NB: Using metadata_names and metadata_values results in a * "names IN (...) AND values IN (...)" clause. This is subtly * differently than default multiple metadata_name_value_pairs, which use * "(name = value) AND (name = value)" clauses. * * When in doubt, use name_value_pairs. * * @see elgg_get_entities * @param array $options Array in format: * * metadata_names => NULL|ARR metadata names * * metadata_values => NULL|ARR metadata values * * metadata_name_value_pairs => NULL|ARR (name = 'name', value => 'value', 'operand' => '=', 'case_sensitive' => TRUE) entries. * Currently if multiple values are sent via an array (value => array('value1', 'value2') the pair's operand will be forced to "IN". * * metadata_name_value_pairs_operator => NULL|STR The operator to use for combining (name = value) OPERATOR (name = value); default AND * * metadata_case_sensitive => BOOL Overall Case sensitive * * @return array */ function elgg_get_entities_from_metadata(array $options = array()) { $defaults = array('metadata_names' => ELGG_ENTITIES_ANY_VALUE, 'metadata_values' => ELGG_ENTITIES_ANY_VALUE, 'metadata_name_value_pairs' => ELGG_ENTITIES_ANY_VALUE, 'metadata_name_value_pairs_operator' => 'AND', 'metadata_case_sensitive' => TRUE, 'order_by_metadata' => array()); $options = array_merge($defaults, $options); $singulars = array('metadata_name', 'metadata_value', 'metadata_name_value_pair'); $options = elgg_normalise_plural_options_array($options, $singulars); $clauses = elgg_get_entity_metadata_where_sql('e', $options['metadata_names'], $options['metadata_values'], $options['metadata_name_value_pairs'], $options['metadata_name_value_pairs_operator'], $options['metadata_case_sensitive'], $options['order_by_metadata']); if ($clauses) { // merge wheres to pass to get_entities() if (isset($options['wheres']) && !is_array($options['wheres'])) { $options['wheres'] = array($options['wheres']); } elseif (!isset($options['wheres'])) { $options['wheres'] = array(); } $options['wheres'] = array_merge($options['wheres'], $clauses['wheres']); // merge joins to pass to get_entities() if (isset($options['joins']) && !is_array($options['joins'])) { $options['joins'] = array($options['joins']); } elseif (!isset($options['joins'])) { $options['joins'] = array(); } $options['joins'] = array_merge($options['joins'], $clauses['joins']); if ($clauses['orders']) { $order_by_metadata = implode(", ", $clauses['orders']); if (isset($options['order_by']) && $options['order_by']) { $options['order_by'] = "{$order_by_metadata}, {$options['order_by']}"; } else { $options['order_by'] = "{$order_by_metadata}, e.time_created DESC"; } } } return elgg_get_entities($options); }