function search_group_by_title($group)
 {
     global $CONFIG, $GROUP_TITLE_TO_GUID_MAP_CACHE;
     $group = sanitise_string($group);
     // Caching
     if (isset($GROUP_TITLE_TO_GUID_MAP_CACHE[$group]) && retrieve_cached_entity($GROUP_TITLE_TO_GUID_MAP_CACHE[$group])) {
         return retrieve_cached_entity($GROUP_TITLE_TO_GUID_MAP_CACHE[$group]);
     }
     $guid = get_data("SELECT guid from {$CONFIG->dbprefix}groups_entity where name='{$group}'");
     if ($guid) {
         $GROUP_TITLE_TO_GUID_MAP_CACHE[$group] = $guid[0]->guid;
     } else {
         $guid = false;
     }
     if ($guid) {
         return $guid[0]->guid;
     } else {
         return false;
     }
 }
Exemple #2
0
/**
 * As retrieve_cached_entity, but returns the result as a stdClass
 * (compatible with load functions that expect a database row.)
 *
 * @param int $guid The guid
 *
 * @return mixed
 * @todo unused
 * @access private
 */
function retrieve_cached_entity_row($guid)
{
    $obj = retrieve_cached_entity($guid);
    if ($obj) {
        $tmp = new stdClass();
        foreach ($obj as $k => $v) {
            $tmp->{$k} = $v;
        }
        return $tmp;
    }
    return false;
}
/**
 * Get user by session code
 *
 * @param string $code The session code
 * @return ElggUser|false Depending on success
 */
function get_user_by_code($code)
{
    global $CONFIG, $CODE_TO_GUID_MAP_CACHE;
    $code = sanitise_string($code);
    $access = get_access_sql_suffix('e');
    // Caching
    if (isset($CODE_TO_GUID_MAP_CACHE[$code]) && retrieve_cached_entity($CODE_TO_GUID_MAP_CACHE[$code])) {
        return retrieve_cached_entity($CODE_TO_GUID_MAP_CACHE[$code]);
    }
    $row = get_data_row("SELECT e.* from {$CONFIG->dbprefix}users_entity u join {$CONFIG->dbprefix}entities e on e.guid=u.guid where u.code='{$code}' and {$access}");
    if ($row) {
        $CODE_TO_GUID_MAP_CACHE[$code] = $row->guid;
        return new ElggUser($row);
    }
    return false;
}
Exemple #4
0
/**
 * Prefetch entities that will be displayed in the river.
 *
 * @param ElggRiverItem[] $river_items
 * @access private
 */
function _elgg_prefetch_river_entities(array $river_items)
{
    // prefetch objects and subjects
    $guids = array();
    foreach ($river_items as $item) {
        if ($item->subject_guid && !retrieve_cached_entity($item->subject_guid)) {
            $guids[$item->subject_guid] = true;
        }
        if ($item->object_guid && !retrieve_cached_entity($item->object_guid)) {
            $guids[$item->object_guid] = true;
        }
    }
    if ($guids) {
        // avoid creating oversized query
        // @todo how to better handle this?
        $guids = array_slice($guids, 0, 300, true);
        // return value unneeded, just priming cache
        elgg_get_entities(array('guids' => array_keys($guids), 'limit' => 0));
    }
    // prefetch object containers
    $guids = array();
    foreach ($river_items as $item) {
        $object = $item->getObjectEntity();
        if ($object->container_guid && !retrieve_cached_entity($object->container_guid)) {
            $guids[$object->container_guid] = true;
        }
    }
    if ($guids) {
        $guids = array_slice($guids, 0, 300, true);
        elgg_get_entities(array('guids' => array_keys($guids), 'limit' => 0));
    }
}
Exemple #5
0
/**
 * Get user by session code
 *
 * @param string $code The session code
 *
 * @return ElggUser|false Depending on success
 */
function get_user_by_code($code)
{
    global $CONFIG, $CODE_TO_GUID_MAP_CACHE;
    $code = sanitise_string($code);
    $access = get_access_sql_suffix('e');
    // Caching
    if (isset($CODE_TO_GUID_MAP_CACHE[$code]) && retrieve_cached_entity($CODE_TO_GUID_MAP_CACHE[$code])) {
        return retrieve_cached_entity($CODE_TO_GUID_MAP_CACHE[$code]);
    }
    $query = "SELECT e.* from {$CONFIG->dbprefix}users_entity u\n\t\tjoin {$CONFIG->dbprefix}entities e on e.guid=u.guid\n\t\twhere u.code='{$code}' and {$access}";
    $entity = get_data_row($query, 'entity_row_to_elggstar');
    if ($entity) {
        $CODE_TO_GUID_MAP_CACHE[$code] = $entity->guid;
    }
    return $entity;
}
Exemple #6
0
/**
 * Return entities from an SQL query generated by elgg_get_entities.
 *
 * @param string $sql
 * @return ElggEntity[]
 *
 * @access private
 * @throws LogicException
 */
function _elgg_fetch_entities_from_sql($sql)
{
    static $plugin_subtype;
    if (null === $plugin_subtype) {
        $plugin_subtype = get_subtype_id('object', 'plugin');
    }
    // Keys are types, values are columns that, if present, suggest that the secondary
    // table is already JOINed
    $types_to_optimize = array('object' => 'title', 'user' => 'password', 'group' => 'name');
    $rows = get_data($sql);
    // guids to look up in each type
    $lookup_types = array();
    // maps GUIDs to the $rows key
    $guid_to_key = array();
    if (isset($rows[0]->type, $rows[0]->subtype) && $rows[0]->type === 'object' && $rows[0]->subtype == $plugin_subtype) {
        // Likely the entire resultset is plugins, which have already been optimized
        // to JOIN the secondary table. In this case we allow retrieving from cache,
        // but abandon the extra queries.
        $types_to_optimize = array();
    }
    // First pass: use cache where possible, gather GUIDs that we're optimizing
    foreach ($rows as $i => $row) {
        if (empty($row->guid) || empty($row->type)) {
            throw new LogicException('Entity row missing guid or type');
        }
        if ($entity = retrieve_cached_entity($row->guid)) {
            $rows[$i] = $entity;
            continue;
        }
        if (isset($types_to_optimize[$row->type])) {
            // check if row already looks JOINed.
            if (isset($row->{$types_to_optimize[$row->type]})) {
                // Row probably already contains JOINed secondary table. Don't make another query just
                // to pull data that's already there
                continue;
            }
            $lookup_types[$row->type][] = $row->guid;
            $guid_to_key[$row->guid] = $i;
        }
    }
    // Do secondary queries and merge rows
    if ($lookup_types) {
        $dbprefix = elgg_get_config('dbprefix');
        foreach ($lookup_types as $type => $guids) {
            $set = "(" . implode(',', $guids) . ")";
            $sql = "SELECT * FROM {$dbprefix}{$type}s_entity WHERE guid IN {$set}";
            $secondary_rows = get_data($sql);
            if ($secondary_rows) {
                foreach ($secondary_rows as $secondary_row) {
                    $key = $guid_to_key[$secondary_row->guid];
                    // cast to arrays to merge then cast back
                    $rows[$key] = (object) array_merge((array) $rows[$key], (array) $secondary_row);
                }
            }
        }
    }
    // Second pass to finish conversion
    foreach ($rows as $i => $row) {
        if ($row instanceof ElggEntity) {
            continue;
        } else {
            try {
                $rows[$i] = entity_row_to_elggstar($row);
            } catch (IncompleteEntityException $e) {
                // don't let incomplete entities throw fatal errors
                unset($rows[$i]);
            }
        }
    }
    return $rows;
}