/**
  * Called before each test method.
  */
 public function setUp()
 {
     $this->ignoreAccess = elgg_set_ignore_access(false);
     $this->cache = _elgg_get_metadata_cache();
     $this->obj1 = new \ElggObject();
     $this->obj1->save();
     $this->guid1 = $this->obj1->guid;
     $this->obj2 = new \ElggObject();
     $this->obj2->save();
     $this->guid2 = $this->obj2->guid;
 }
示例#2
0
function elgg_solr_get_entity_guids(array $options = array())
{
    global $CONFIG;
    $defaults = array('types' => ELGG_ENTITIES_ANY_VALUE, 'subtypes' => ELGG_ENTITIES_ANY_VALUE, 'type_subtype_pairs' => ELGG_ENTITIES_ANY_VALUE, 'guids' => ELGG_ENTITIES_ANY_VALUE, 'owner_guids' => ELGG_ENTITIES_ANY_VALUE, 'container_guids' => ELGG_ENTITIES_ANY_VALUE, 'site_guids' => $CONFIG->site_guid, 'modified_time_lower' => ELGG_ENTITIES_ANY_VALUE, 'modified_time_upper' => ELGG_ENTITIES_ANY_VALUE, 'created_time_lower' => ELGG_ENTITIES_ANY_VALUE, 'created_time_upper' => ELGG_ENTITIES_ANY_VALUE, 'reverse_order_by' => false, 'order_by' => 'e.time_created desc', 'group_by' => ELGG_ENTITIES_ANY_VALUE, 'limit' => 10, 'offset' => 0, 'count' => false, 'selects' => array(), 'wheres' => array(), 'joins' => array(), 'callback' => false, '__ElggBatch' => null);
    $options = array_merge($defaults, $options);
    // can't use helper function with type_subtype_pair because
    // it's already an array...just need to merge it
    if (isset($options['type_subtype_pair'])) {
        if (isset($options['type_subtype_pairs'])) {
            $options['type_subtype_pairs'] = array_merge($options['type_subtype_pairs'], $options['type_subtype_pair']);
        } else {
            $options['type_subtype_pairs'] = $options['type_subtype_pair'];
        }
    }
    $singulars = array('type', 'subtype', 'guid', 'owner_guid', 'container_guid', 'site_guid');
    $options = _elgg_normalize_plural_options_array($options, $singulars);
    // evaluate where clauses
    if (!is_array($options['wheres'])) {
        $options['wheres'] = array($options['wheres']);
    }
    $wheres = $options['wheres'];
    $wheres[] = _elgg_get_entity_type_subtype_where_sql('e', $options['types'], $options['subtypes'], $options['type_subtype_pairs']);
    $wheres[] = _elgg_get_guid_based_where_sql('e.guid', $options['guids']);
    $wheres[] = _elgg_get_guid_based_where_sql('e.owner_guid', $options['owner_guids']);
    $wheres[] = _elgg_get_guid_based_where_sql('e.container_guid', $options['container_guids']);
    $wheres[] = _elgg_get_guid_based_where_sql('e.site_guid', $options['site_guids']);
    $wheres[] = _elgg_get_entity_time_where_sql('e', $options['created_time_upper'], $options['created_time_lower'], $options['modified_time_upper'], $options['modified_time_lower']);
    // see if any functions failed
    // remove empty strings on successful functions
    foreach ($wheres as $i => $where) {
        if ($where === false) {
            return false;
        } elseif (empty($where)) {
            unset($wheres[$i]);
        }
    }
    // remove identical where clauses
    $wheres = array_unique($wheres);
    // evaluate join clauses
    if (!is_array($options['joins'])) {
        $options['joins'] = array($options['joins']);
    }
    // remove identical join clauses
    $joins = array_unique($options['joins']);
    foreach ($joins as $i => $join) {
        if ($join === false) {
            return false;
        } elseif (empty($join)) {
            unset($joins[$i]);
        }
    }
    // evalutate selects
    if ($options['selects']) {
        $selects = '';
        foreach ($options['selects'] as $select) {
            $selects .= ", {$select}";
        }
    } else {
        $selects = '';
    }
    if (!$options['count']) {
        $distinct = '';
        if ($options['require_distinct']) {
            $distinct = ' DISTINCT';
        }
        $query = "SELECT{$distinct} e.guid{$selects} FROM {$CONFIG->dbprefix}entities e ";
    } else {
        $query = "SELECT count(DISTINCT e.guid) as total FROM {$CONFIG->dbprefix}entities e ";
    }
    // add joins
    foreach ($joins as $j) {
        $query .= " {$j} ";
    }
    // add wheres
    $query .= ' WHERE ';
    foreach ($wheres as $w) {
        $query .= " {$w} AND ";
    }
    // Add access controls
    $query .= _elgg_get_access_where_sql();
    // reverse order by
    if ($options['reverse_order_by']) {
        $options['order_by'] = _elgg_sql_reverse_order_by_clause($options['order_by']);
    }
    if (!$options['count']) {
        if ($options['group_by']) {
            $query .= " GROUP BY {$options['group_by']}";
        }
        if ($options['order_by']) {
            $query .= " ORDER BY {$options['order_by']}";
        }
        if ($options['limit']) {
            $limit = sanitise_int($options['limit'], false);
            $offset = sanitise_int($options['offset'], false);
            $query .= " LIMIT {$offset}, {$limit}";
        }
        if ($options['callback'] === 'entity_row_to_elggstar') {
            $dt = _elgg_fetch_entities_from_sql($query, $options['__ElggBatch']);
        } else {
            $dt = get_data($query, $options['callback']);
        }
        if ($dt) {
            // populate entity and metadata caches
            $guids = array();
            foreach ($dt as $item) {
                // A custom callback could result in items that aren't ElggEntity's, so check for them
                if ($item instanceof ElggEntity) {
                    _elgg_cache_entity($item);
                    // plugins usually have only settings
                    if (!$item instanceof ElggPlugin) {
                        $guids[] = $item->guid;
                    }
                }
            }
            // @todo Without this, recursive delete fails. See #4568
            reset($dt);
            if ($guids) {
                _elgg_get_metadata_cache()->populateFromEntities($guids);
            }
        }
        return $dt;
    } else {
        $total = get_data_row($query);
        return (int) $total->total;
    }
}
/**
 * Invalidate the metadata cache based on options passed to various *_metadata functions
 *
 * @param string $action  Action performed on metadata. "delete", "disable", or "enable"
 * @param array  $options Options passed to elgg_(delete|disable|enable)_metadata
 * @return void
 * @access private
 * @todo not used
 */
function _elgg_invalidate_metadata_cache($action, array $options)
{
    // remove as little as possible, optimizing for common cases
    $cache = _elgg_get_metadata_cache();
    if (empty($options['guid'])) {
        // safest to clear everything unless we want to make this even more complex :(
        $cache->flush();
    } else {
        if (empty($options['metadata_name'])) {
            // safest to clear the whole entity
            $cache->clear($options['guid']);
        } else {
            switch ($action) {
                case 'delete':
                    $cache->markEmpty($options['guid'], $options['metadata_name']);
                    break;
                default:
                    $cache->markUnknown($options['guid'], $options['metadata_name']);
            }
        }
    }
}
示例#4
0
 /**
  * Return the value of a piece of metadata.
  *
  * @param string $name Name
  *
  * @return mixed The value, or null if not found.
  */
 public function getMetadata($name)
 {
     $guid = $this->getGUID();
     if (!$guid) {
         if (isset($this->temp_metadata[$name])) {
             // md is returned as an array only if more than 1 entry
             if (count($this->temp_metadata[$name]) == 1) {
                 return $this->temp_metadata[$name][0];
             } else {
                 return $this->temp_metadata[$name];
             }
         } else {
             return null;
         }
     }
     // upon first cache miss, just load/cache all the metadata and retry.
     // if this works, the rest of this function may not be needed!
     $cache = _elgg_get_metadata_cache();
     if ($cache->isKnown($guid, $name)) {
         return $cache->load($guid, $name);
     } else {
         $cache->populateFromEntities(array($guid));
         // in case ignore_access was on, we have to check again...
         if ($cache->isKnown($guid, $name)) {
             return $cache->load($guid, $name);
         }
     }
     $md = elgg_get_metadata(array('guid' => $guid, 'metadata_name' => $name, 'limit' => 0));
     $value = null;
     if ($md && !is_array($md)) {
         $value = $md->value;
     } elseif (count($md) == 1) {
         $value = $md[0]->value;
     } else {
         if ($md && is_array($md)) {
             $value = metadata_array_to_values($md);
         }
     }
     $cache->save($guid, $name, $value);
     return $value;
 }
示例#5
0
 /**
  * Enable the metadata
  *
  * @return bool
  * @since 1.8
  */
 public function enable()
 {
     $success = _elgg_set_metastring_based_object_enabled_by_id($this->id, 'yes', 'metadata');
     if ($success) {
         _elgg_get_metadata_cache()->markUnknown($this->entity_guid, $this->name);
     }
     return $success;
 }