123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace Elgg\Cache;
- class MetadataCache {
-
- protected $values = array();
-
- protected $session;
-
- public function __construct(\ElggSession $session) {
- $this->session = $session;
- }
-
- public function inject($entity_guid, array $values) {
- $this->values[$this->getAccessKey()][$entity_guid] = $values;
- }
-
- public function getSingle($entity_guid, $name) {
- $access_key = $this->getAccessKey();
- if (isset($this->values[$access_key][$entity_guid])
- && array_key_exists($name, $this->values[$access_key][$entity_guid])) {
- return $this->values[$access_key][$entity_guid][$name];
- } else {
- return null;
- }
- }
-
- public function clear($entity_guid) {
- foreach (array_keys($this->values) as $access_key) {
- unset($this->values[$access_key][$entity_guid]);
- }
- }
-
- public function isLoaded($entity_guid) {
- $access_key = $this->getAccessKey();
- if (empty($this->values[$access_key])) {
- return false;
- }
- return array_key_exists($entity_guid, $this->values[$access_key]);
- }
-
- public function clearAll() {
- $this->values = array();
- }
-
- public function invalidateByOptions(array $options) {
- if (empty($options['guid'])) {
- $this->clearAll();
- } else {
- $this->clear($options['guid']);
- }
- }
-
- public function populateFromEntities($guids) {
- if (empty($guids)) {
- return;
- }
- $access_key = $this->getAccessKey();
- if (!is_array($guids)) {
- $guids = array($guids);
- }
- $guids = array_unique($guids);
-
-
- $db_prefix = _elgg_services()->db->getTablePrefix();
- $options = array(
- 'guids' => $guids,
- 'limit' => 0,
- 'callback' => false,
- 'distinct' => false,
- 'joins' => array(
- "JOIN {$db_prefix}metastrings v ON n_table.value_id = v.id",
- "JOIN {$db_prefix}metastrings n ON n_table.name_id = n.id",
- ),
- 'selects' => array('n.string AS name', 'v.string AS value'),
- 'order_by' => 'n_table.entity_guid, n_table.time_created ASC, n_table.id ASC',
-
- 'wheres' => array(_elgg_get_access_where_sql(array(
- 'table_alias' => 'n_table',
- 'guid_column' => 'entity_guid',
- ))),
- );
- $data = _elgg_services()->metadataTable->getAll($options);
-
- foreach ($guids as $guid) {
- $this->values[$access_key][$guid] = null;
- }
-
- $last_guid = null;
- $metadata = array();
- $last_row_idx = count($data) - 1;
- foreach ($data as $i => $row) {
- $name = $row->name;
- $value = ($row->value_type === 'text') ? $row->value : (int) $row->value;
- $guid = $row->entity_guid;
- if ($guid !== $last_guid) {
- if ($last_guid) {
- $this->values[$access_key][$last_guid] = $metadata;
- }
- $metadata = array();
- }
- if (isset($metadata[$name])) {
- $metadata[$name] = (array) $metadata[$name];
- $metadata[$name][] = $value;
- } else {
- $metadata[$name] = $value;
- }
- if (($i == $last_row_idx)) {
- $this->values[$access_key][$guid] = $metadata;
- }
- $last_guid = $guid;
- }
- }
-
- public function filterMetadataHeavyEntities(array $guids, $limit = 1024000) {
- $db_prefix = _elgg_services()->config->get('dbprefix');
- $options = array(
- 'guids' => $guids,
- 'limit' => 0,
- 'callback' => false,
- 'joins' => "JOIN {$db_prefix}metastrings v ON n_table.value_id = v.id",
- 'selects' => array('SUM(LENGTH(v.string)) AS bytes'),
- 'order_by' => 'n_table.entity_guid, n_table.time_created ASC',
- 'group_by' => 'n_table.entity_guid',
- );
- $data = _elgg_services()->metadataTable->getAll($options);
-
- foreach ($data as $row) {
- if ($row->bytes > $limit || $row->bytes < 0) {
- array_splice($guids, array_search($row->entity_guid, $guids), 1);
- }
- }
- return $guids;
- }
-
- protected function getAccessKey() {
- if ($this->session->getIgnoreAccess()) {
- return "ignored";
- }
- return (string)$this->session->getLoggedInUserGuid();
- }
- }
|