public function get($key, callable $callback = null, $default = null) { $value = $this->memcache->load($key); if (!isset($value)) { $value = $default; } if (is_callable($callback)) { return call_user_func($callback, $value); } return $value; }
/** * Create an Elgg* object from a given entity row. * * Handles loading all tables into the correct class. * * @param \stdClass $row The row of the entry in the entities table. * * @return \ElggEntity|false * @see get_entity_as_row() * @see add_subtype() * @see get_entity() * @access private * * @throws \ClassException|\InstallationException */ function rowToElggStar($row) { if (!$row instanceof \stdClass) { return $row; } if (!isset($row->guid) || !isset($row->subtype)) { return $row; } $new_entity = false; // Create a memcache cache if we can static $newentity_cache; if (!$newentity_cache && is_memcache_available()) { $newentity_cache = new \ElggMemcache('new_entity_cache'); } if ($newentity_cache) { $new_entity = $newentity_cache->load($row->guid); } if ($new_entity) { return $new_entity; } // load class for entity if one is registered $classname = get_subtype_class_from_id($row->subtype); if ($classname != "") { if (class_exists($classname)) { $new_entity = new $classname($row); if (!$new_entity instanceof \ElggEntity) { $msg = $classname . " is not a " . '\\ElggEntity' . "."; throw new \ClassException($msg); } } else { error_log("Class '" . $classname . "' was not found, missing plugin?"); } } if (!$new_entity) { //@todo Make this into a function switch ($row->type) { case 'object': $new_entity = new \ElggObject($row); break; case 'user': $new_entity = new \ElggUser($row); break; case 'group': $new_entity = new \ElggGroup($row); break; case 'site': $new_entity = new \ElggSite($row); break; default: $msg = "Entity type " . $row->type . " is not supported."; throw new \InstallationException($msg); } } // Cache entity if we have a cache available if ($newentity_cache && $new_entity) { $newentity_cache->save($new_entity->guid, $new_entity); } return $new_entity; }
/** * Create a standard object from a given entity row. * * @param stdClass $row The row of the entry in the entities table. * * @return ElggEntity|false * @link http://docs.elgg.org/DataModel/Entities * @see get_entity_as_row() * @see add_subtype() * @see get_entity() * @access private * * @throws ClassException|InstallationException */ function elasticsearch_entity_row_to_std($row) { if (!$row instanceof stdClass) { return $row; } if (!isset($row->guid) || !isset($row->subtype)) { return $row; } $new_entity = false; // Create a memcache cache if we can static $newentity_cache; if (!$newentity_cache && is_memcache_available()) { $newentity_cache = new ElggMemcache('new_entity_cache'); } if ($newentity_cache) { $new_entity = $newentity_cache->load($row->guid); } if ($new_entity) { return $new_entity; } try { // load class for entity if one is registered $classname = get_subtype_class_from_id($row->subtype); if ($classname != "") { if (class_exists($classname)) { $new_entity = new $classname($row); if (!$new_entity instanceof ElggEntity) { $msg = elgg_echo('ClassException:ClassnameNotClass', array($classname, 'ElggEntity')); throw new ClassException($msg); } } } if (!$new_entity) { switch ($row->type) { case 'object': $new_entity = new ElggObject($row); break; case 'user': $new_entity = new ElggUser($row); break; case 'group': $new_entity = new ElggGroup($row); break; case 'site': $new_entity = new ElggSite($row); break; default: $msg = elgg_echo('InstallationException:TypeNotSupported', array($row->type)); throw new InstallationException($msg); } } } catch (IncompleteEntityException $e) { return false; } return $new_entity; }
/** * Gets the metastring identifier for a value. * * Elgg normalizes the names and values of annotations and metadata. This function * provides the identifier used as the index in the metastrings table. Plugin * developers should only use this if denormalizing names/values for performance * reasons (to avoid multiple joins on the metastrings table). * * @param string $string The value * @param bool $case_sensitive Should the retrieval be case sensitive? * If not, there may be more than one result * * @return int|array metastring id or array of ids * @since 1.9.0 */ function elgg_get_metastring_id($string, $case_sensitive = true) { global $CONFIG, $METASTRINGS_CACHE; // caching doesn't work for case insensitive requests if ($case_sensitive) { $result = array_search($string, $METASTRINGS_CACHE, true); if ($result !== false) { return $result; } // Experimental memcache $msfc = null; static $metastrings_memcache; if (!$metastrings_memcache && is_memcache_available()) { $metastrings_memcache = new ElggMemcache('metastrings_memcache'); } if ($metastrings_memcache) { $msfc = $metastrings_memcache->load($string); } if ($msfc) { return $msfc; } } $escaped_string = sanitise_string($string); if ($case_sensitive) { $query = "SELECT * FROM {$CONFIG->dbprefix}metastrings WHERE string = BINARY '{$escaped_string}' LIMIT 1"; } else { $query = "SELECT * FROM {$CONFIG->dbprefix}metastrings WHERE string = '{$escaped_string}'"; } $id = false; $results = get_data($query); if (is_array($results)) { if (!$case_sensitive) { $ids = array(); foreach ($results as $result) { $ids[] = $result->id; } // return immediately because we don't want to cache case insensitive results return $ids; } else { if (isset($results[0])) { $id = $results[0]->id; } } } if (!$id) { $id = _elgg_add_metastring($string); } $METASTRINGS_CACHE[$id] = $string; if ($metastrings_memcache) { $metastrings_memcache->save($string, $id); } return $id; }
/** * Return the meta string id for a given tag, or false. * * @param string $string The value (whatever that is) to be stored * @param bool $case_sensitive Do we want to make the query case sensitive? * @return mixed Integer tag or false. */ function get_metastring_id($string, $case_sensitive = true) { global $CONFIG, $METASTRINGS_CACHE, $METASTRINGS_DEADNAME_CACHE; $string = sanitise_string($string); $result = array_search($string, $METASTRINGS_CACHE); if ($result !== false) { if (isset($CONFIG->debug) && $CONFIG->debug) { error_log("** Returning id for string:{$string} from cache."); } return $result; } // See if we have previously looked for this and found nothing if (in_array($string, $METASTRINGS_DEADNAME_CACHE)) { return false; } // Experimental memcache $msfc = null; static $metastrings_memcache; if (!$metastrings_memcache && is_memcache_available()) { $metastrings_memcache = new ElggMemcache('metastrings_memcache'); } if ($metastrings_memcache) { $msfc = $metastrings_memcache->load($string); } if ($msfc) { return $msfc; } // Case sensitive $cs = ""; if ($case_sensitive) { $cs = " BINARY "; } $row = get_data_row("SELECT * from {$CONFIG->dbprefix}metastrings where string={$cs}'{$string}' limit 1"); if ($row) { $METASTRINGS_CACHE[$row->id] = $row->string; // Cache it // Attempt to memcache it if memcache is available if ($metastrings_memcache) { $metastrings_memcache->save($row->string, $row->id); } if (isset($CONFIG->debug) && $CONFIG->debug) { error_log("** Cacheing string '{$row->string}'"); } return $row->id; } else { $METASTRINGS_DEADNAME_CACHE[$string] = $string; } return false; }
function subsite_manager_get_plugin_order() { if (is_memcache_available()) { $memcache = new ElggMemcache('subsite_manager'); } if (isset($memcache)) { if ($plugin_order = $memcache->load('plugin_order')) { return $plugin_order; } } $db_prefix = get_config('dbprefix'); $priority = elgg_namespace_plugin_private_setting('internal', 'priority'); $options = array('type' => 'object', 'subtype' => 'plugin', 'limit' => ELGG_ENTITIES_NO_VALUE, 'selects' => array('plugin_oe.*'), 'joins' => array("JOIN {$db_prefix}private_settings ps on ps.entity_guid = e.guid", "JOIN {$db_prefix}objects_entity plugin_oe on plugin_oe.guid = e.guid"), 'wheres' => array("ps.name = '{$priority}'"), 'order_by' => "CAST(ps.value as unsigned), e.guid", 'site_guids' => array(1)); $plugins = elgg_get_entities_from_relationship($options); $plugin_order = array(); foreach ($plugins as $i => $plugin) { $plugin_order[$plugin->title] = $i; } if (isset($memcache)) { $memcache->save('plugin_order', $plugin_order); } return $plugin_order; }
/** * Create an Elgg* object from a given entity row. * * Handles loading all tables into the correct class. * * @param stdClass $row The row of the entry in the entities table. * * @return ElggEntity|false * @link http://docs.elgg.org/DataModel/Entities * @see get_entity_as_row() * @see add_subtype() * @see get_entity() * @access private * * @throws ClassException|InstallationException */ function entity_row_to_elggstar($row) { if (!$row instanceof stdClass) { return $row; } if (!isset($row->guid) || !isset($row->subtype)) { return $row; } $new_entity = false; // Create a memcache cache if we can static $newentity_cache; if (!$newentity_cache && is_memcache_available()) { $newentity_cache = new ElggMemcache('new_entity_cache'); } if ($newentity_cache) { $new_entity = $newentity_cache->load($row->guid); } if ($new_entity) { return $new_entity; } // load class for entity if one is registered $classname = get_subtype_class_from_id($row->subtype); if ($classname != "") { if (class_exists($classname)) { $new_entity = new $classname($row); if (!$new_entity instanceof ElggEntity) { $msg = elgg_echo('ClassException:ClassnameNotClass', array($classname, 'ElggEntity')); throw new ClassException($msg); } } else { error_log(elgg_echo('ClassNotFoundException:MissingClass', array($classname))); } } if (!$new_entity) { //@todo Make this into a function switch ($row->type) { case 'object': $new_entity = new ElggObject($row); break; case 'user': $new_entity = new ElggUser($row); break; case 'group': $new_entity = new ElggGroup($row); break; case 'site': $new_entity = new ElggSite($row); break; default: $msg = elgg_echo('InstallationException:TypeNotSupported', array($row->type)); throw new InstallationException($msg); } } // Cache entity if we have a cache available if ($newentity_cache && $new_entity) { $newentity_cache->save($new_entity->guid, $new_entity); } return $new_entity; }
/** * Get the value of a datalist element. * * @internal Datalists are stored in the datalist table. * * @tip Use datalists to store information common to a full installation. * * @param string $name The name of the datalist * @return string|null|false String if value exists, null if doesn't, false on error * @access private */ function datalist_get($name) { global $CONFIG, $DATALIST_CACHE; $name = trim($name); // cannot store anything longer than 255 characters in db, so catch here if (elgg_strlen($name) > 255) { elgg_log("The name length for configuration variables cannot be greater than 255", "ERROR"); return false; } $name = sanitise_string($name); if (isset($DATALIST_CACHE[$name])) { return $DATALIST_CACHE[$name]; } // If memcache enabled then cache value in memcache $value = null; static $datalist_memcache; if (!$datalist_memcache && is_memcache_available()) { $datalist_memcache = new ElggMemcache('datalist_memcache'); } if ($datalist_memcache) { $value = $datalist_memcache->load($name); } if ($value) { return $value; } // [Marcus Povey 20090217 : Now retrieving all datalist values on first // load as this saves about 9 queries per page] // This also causes OOM problems when the datalists table is large // @todo make a list of datalists that we want to get in one grab $result = get_data("SELECT * from {$CONFIG->dbprefix}datalists"); if ($result) { foreach ($result as $row) { $DATALIST_CACHE[$row->name] = $row->value; // Cache it if memcache is available if ($datalist_memcache) { $datalist_memcache->save($row->name, $row->value); } } if (isset($DATALIST_CACHE[$name])) { return $DATALIST_CACHE[$name]; } } return null; }
<?php $site = elgg_get_site_entity(); $key = "css_splash_" . $site->guid; if (is_memcache_available()) { $cache = new ElggMemcache("rijkshuisstijl"); $css = $cache->load($key); if ($css) { echo $css; return; } } $parser = new Less_Parser(); $parser->parseFile(RIJKSHUISSTIJL_LESS . "/splash.less", "/mod/rijkshuisstijl/src/assets/"); $colors = elgg_get_plugin_setting("colors", "rijkshuisstijl"); if ($colors) { $colors = unserialize($colors); foreach ($colors as $number => $value) { $parser->parse("@belastingdienst--{$number}: {$value};"); } } $css = $parser->getCss(); echo $css; if (is_memcache_available()) { $cache->save($key, $css, 0); }
/** * Get the value of a datalist element. * * Plugin authors should use elgg_get_config() and pass null for the site GUID. * * @internal Datalists are stored in the datalist table. * * @tip Use datalists to store information common to a full installation. * * @param string $name The name of the datalist * @return string|null|false String if value exists, null if doesn't, false on error * @access private */ function datalist_get($name) { global $CONFIG, $DATALIST_CACHE; $name = trim($name); // cannot store anything longer than 255 characters in db, so catch here if (elgg_strlen($name) > 255) { elgg_log("The name length for configuration variables cannot be greater than 255", "ERROR"); return false; } if (isset($DATALIST_CACHE[$name])) { return $DATALIST_CACHE[$name]; } // If memcache enabled then cache value in memcache $value = null; static $datalist_memcache = null; if (!$datalist_memcache && is_memcache_available()) { $datalist_memcache = new ElggMemcache('datalist_memcache'); } if ($datalist_memcache) { $value = $datalist_memcache->load($name); } // @todo cannot cache 0 or false? if ($value) { return $value; } // not in cache and not in memcache so check database $escaped_name = sanitize_string($name); $result = get_data_row("SELECT * FROM {$CONFIG->dbprefix}datalists WHERE name = '{$escaped_name}'"); if ($result) { $DATALIST_CACHE[$result->name] = $result->value; // Cache it if memcache is available if ($datalist_memcache) { $datalist_memcache->save($result->name, $result->value); } return $result->value; } return null; }
/** * Get the value of a particular piece of data in the datalist * * @param string $name The name of the datalist * @return string|false Depending on success */ function datalist_get($name) { global $CONFIG, $DATALIST_CACHE; // We need this, because sometimes datalists are received before the database is created if (!is_db_installed()) { return false; } $name = sanitise_string($name); if (isset($DATALIST_CACHE[$name])) { return $DATALIST_CACHE[$name]; } // If memcache enabled then cache value in memcache $value = null; static $datalist_memcache; if (!$datalist_memcache && is_memcache_available()) { $datalist_memcache = new ElggMemcache('datalist_memcache'); } if ($datalist_memcache) { $value = $datalist_memcache->load($name); } if ($value) { return $value; } // [Marcus Povey 20090217 : Now retrieving all datalist values on first load as this saves about 9 queries per page] $result = get_data("SELECT * from {$CONFIG->dbprefix}datalists"); if ($result) { foreach ($result as $row) { $DATALIST_CACHE[$row->name] = $row->value; // Cache it if memcache is available if ($datalist_memcache) { $datalist_memcache->save($name, $row->value); } } if (isset($DATALIST_CACHE[$name])) { return $DATALIST_CACHE[$name]; } } /*if ($row = get_data_row("SELECT value from {$CONFIG->dbprefix}datalists where name = '{$name}' limit 1")) { $DATALIST_CACHE[$name] = $row->value; // Cache it if memcache is available if ($datalist_memcache) $datalist_memcache->save($name, $row->value); return $row->value; }*/ return false; }
/** * Return the metadata values that match your query. * * @param string $meta_name * @return mixed either a value, an array of ElggMetadata or false. */ function get_metadata_byname($entity_guid, $meta_name) { global $CONFIG; $meta_name = get_metastring_id($meta_name); if (empty($meta_name)) { return false; } $entity_guid = (int) $entity_guid; $access = get_access_sql_suffix("e"); $md_access = get_access_sql_suffix("m"); // If memcache is available then cache this (cache only by name for now since this is the most common query) $meta = null; static $metabyname_memcache; if (!$metabyname_memcache && is_memcache_available()) { $metabyname_memcache = new ElggMemcache('metabyname_memcache'); } if ($metabyname_memcache) { $meta = $metabyname_memcache->load("{$entity_guid}:{$meta_name}"); } if ($meta) { return $meta; } $result = get_data("SELECT m.*, n.string as name, v.string as value from {$CONFIG->dbprefix}metadata m JOIN {$CONFIG->dbprefix}entities e ON e.guid = m.entity_guid JOIN {$CONFIG->dbprefix}metastrings v on m.value_id = v.id JOIN {$CONFIG->dbprefix}metastrings n on m.name_id = n.id where m.entity_guid={$entity_guid} and m.name_id='{$meta_name}' and {$access} and {$md_access}", "row_to_elggmetadata"); if (!$result) { return false; } // Cache if memcache available if ($metabyname_memcache) { if (count($result) == 1) { $r = $result[0]; } else { $r = $result; } $metabyname_memcache->setDefaultExpiry(3600); // This is a bit of a hack - we shorten the expiry on object metadata so that it'll be gone in an hour. This means that deletions and more importantly updates will filter through eventually. $metabyname_memcache->save("{$entity_guid}:{$meta_name}", $r); } if (count($result) == 1) { return $result[0]; } return $result; }
/** * Return an array of all private settings. * * @param int $entity_guid The entity GUID * * @return array|false * @see set_private_setting() * @see get_private_settings() * @see remove_private_setting() * @see remove_all_private_settings() * @link http://docs.elgg.org/DataModel/Entities/PrivateSettings */ function get_all_private_settings($entity_guid) { global $PRIVATE_SETTINGS_CACHE; static $private_setting_memcache; $dbprefix = elgg_get_config("dbprefix"); $entity_guid = (int) $entity_guid; // check if you have access to the entity if (!elgg_get_ignore_access() && !get_entity_as_row($entity_guid)) { return false; } // first check localy if (isset($PRIVATE_SETTINGS_CACHE[$entity_guid])) { return $PRIVATE_SETTINGS_CACHE[$entity_guid]; } if (!isset($private_setting_memcache) && is_memcache_available()) { $private_setting_memcache = new ElggMemcache("private_settings"); } if ($private_setting_memcache) { if ($settings = $private_setting_memcache->load($entity_guid)) { // cache localy $PRIVATE_SETTINGS_CACHE[$entity_guid] = $settings; if (!empty($settings)) { return $settings; } else { return false; } } } $query = "SELECT *"; $query .= " FROM {$dbprefix}private_settings"; $query .= " WHERE entity_guid = {$entity_guid}"; $settings = array(); if ($result = get_data($query)) { foreach ($result as $r) { $settings[$r->name] = $r->value; } } if ($private_setting_memcache) { $private_setting_memcache->save($entity_guid, $settings); } if (!empty($settings)) { // cache localy $PRIVATE_SETTINGS_CACHE[$entity_guid] = $settings; return $settings; } return false; }
/** * Return the meta string id for a given tag, or false. * * @param string $string The value to store * @param bool $case_sensitive Do we want to make the query case sensitive? * If not there may be more than one result * * @return int|array|false meta string id, array of ids or false if none found * @deprecated 1.9 Use elgg_get_metastring_id() */ function get_metastring_id($string, $case_sensitive = TRUE) { elgg_deprecated_notice(__FUNCTION__ . ' is deprecated. Use elgg_get_metastring_id()', 1.9); global $CONFIG, $METASTRINGS_CACHE, $METASTRINGS_DEADNAME_CACHE; $string = sanitise_string($string); // caching doesn't work for case insensitive searches if ($case_sensitive) { $result = array_search($string, $METASTRINGS_CACHE, true); if ($result !== false) { return $result; } // See if we have previously looked for this and found nothing if (in_array($string, $METASTRINGS_DEADNAME_CACHE, true)) { return false; } // Experimental memcache $msfc = null; static $metastrings_memcache; if (!$metastrings_memcache && is_memcache_available()) { $metastrings_memcache = new \ElggMemcache('metastrings_memcache'); } if ($metastrings_memcache) { $msfc = $metastrings_memcache->load($string); } if ($msfc) { return $msfc; } } // Case sensitive if ($case_sensitive) { $query = "SELECT * from {$CONFIG->dbprefix}metastrings where string= BINARY '{$string}' limit 1"; } else { $query = "SELECT * from {$CONFIG->dbprefix}metastrings where string = '{$string}'"; } $row = FALSE; $metaStrings = get_data($query); if (is_array($metaStrings)) { if (sizeof($metaStrings) > 1) { $ids = array(); foreach ($metaStrings as $metaString) { $ids[] = $metaString->id; } return $ids; } else { if (isset($metaStrings[0])) { $row = $metaStrings[0]; } } } if ($row) { $METASTRINGS_CACHE[$row->id] = $row->string; // Cache it // Attempt to memcache it if memcache is available if ($metastrings_memcache) { $metastrings_memcache->save($row->string, $row->id); } return $row->id; } else { $METASTRINGS_DEADNAME_CACHE[$string] = $string; } return false; }
function entity_view_counter_is_counted(ElggEntity $entity) { if (php_sapi_name() === 'cli') { return true; } if (!entity_view_counter_is_configured_entity_type($entity->getType(), $entity->getSubtype())) { return true; } if (isset($_SERVER["HTTP_USER_AGENT"]) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER["HTTP_USER_AGENT"])) { return true; } $user = elgg_get_logged_in_user_entity(); if ($user && $user->getGUID() == $entity->getOwnerGUID()) { return true; } if (is_memcache_available()) { $cache = new ElggMemcache('entity_view_counter'); $key = "view_" . session_id() . "_" . $entity->guid; if ($cache->load($key)) { return true; } } if (entity_view_counter_ignore_ip()) { return true; } return false; }
/** * Loads and returns an entity object from a guid. * * @param int $guid The GUID of the entity * * @return ElggEntity The correct Elgg or custom object based upon entity type and subtype * @link http://docs.elgg.org/DataModel/Entities */ function get_entity($guid) { static $newentity_cache; $new_entity = false; if (!is_numeric($guid)) { return FALSE; } if (!$newentity_cache && is_memcache_available()) { $newentity_cache = new ElggMemcache('new_entity_cache'); } if ($newentity_cache) { $new_entity = $newentity_cache->load($guid); } if ($new_entity) { return $new_entity; } return entity_row_to_elggstar(get_entity_as_row($guid)); }
/** * Loads and returns an entity object from a guid. * * @param int $guid The GUID of the entity * * @return ElggEntity The correct Elgg or custom object based upon entity type and subtype * @link http://docs.elgg.org/DataModel/Entities */ function get_entity($guid) { static $newentity_cache; $new_entity = false; // We could also use: if (!(int) $guid) { return FALSE }, // but that evaluates to a false positive for $guid = TRUE. // This is a bit slower, but more thorough. if (!is_numeric($guid) || $guid === 0 || $guid === '0') { return FALSE; } if (!$newentity_cache && is_memcache_available()) { $newentity_cache = new ElggMemcache('new_entity_cache'); } if ($newentity_cache) { $new_entity = $newentity_cache->load($guid); } if ($new_entity) { return $new_entity; } return entity_row_to_elggstar(get_entity_as_row($guid)); }