Elgg  Version 4.x
MetadataCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cache;
4 
5 use ElggCache;
9 use Elgg\Values;
10 use ElggMetadata;
11 
18 
22  protected $cache;
23 
29  public function __construct(ElggCache $cache) {
30  $this->cache = $cache;
31  }
32 
45  public function inject($entity_guid, array $values = []) {
46  $metadata = [];
47  foreach ($values as $key => $value) {
48  if ($value instanceof ElggMetadata) {
49  $md = $value;
50  } else {
51  $md = new ElggMetadata();
52  $md->name = $key;
53  $md->value = $value;
54  $md->entity_guid = $entity_guid;
55  }
56 
57  $metadata[] = $md->toObject();
58  }
59 
60  $this->cache->save($entity_guid, $metadata);
61  }
62 
69  public function getAll($entity_guid) {
71  if (empty($metadata)) {
72  return [];
73  }
74 
75  $metadata_values = [];
76 
77  foreach ($metadata as $md) {
78  $metadata_values[$md->name][] = $md->value;
79  }
80 
81  return array_map(function($values) {
82  return count($values) > 1 ? $values : $values[0];
83  }, $metadata_values);
84  }
85 
99  public function getSingle($entity_guid, $name) {
101  if (empty($metadata)) {
102  return null;
103  }
104 
105  $values = [];
106 
107  foreach ($metadata as $md) {
108  if ($md->name !== $name) {
109  continue;
110  }
111 
112  $values[] = $md->value;
113  }
114 
115  if (empty($values)) {
116  return null;
117  }
118 
119  return count($values) > 1 ? $values : $values[0];
120 
121  }
122 
136  public function getSingleId($entity_guid, $name) {
138  if (empty($metadata)) {
139  return null;
140  }
141 
142  $ids = [];
143 
144  foreach ($metadata as $md) {
145  if ($md->name !== $name) {
146  continue;
147  }
148 
149  $ids[] = $md->id;
150  }
151 
152  if (empty($ids)) {
153  return null;
154  }
155 
156  return count($ids) > 1 ? $ids : $ids[0];
157  }
158 
166  public function clear($entity_guid) {
167  $this->invalidateByOptions([
168  'guid' => $entity_guid,
169  ]);
170  }
171 
179  public function isLoaded($entity_guid) {
180  return $this->cache->load($entity_guid) !== null;
181  }
182 
188  public function clearAll() {
189  $this->invalidateByOptions([]);
190  }
191 
199  public function getEntityMetadata($entity_guid) {
200  $entity_guid = (int) $entity_guid;
201  $metadata = $this->cache->load($entity_guid);
202  if ($metadata === null) {
204  }
205 
206  return $metadata;
207  }
208 
216  public function invalidateByOptions(array $options) {
217  if (empty($options['guid'])) {
218  _elgg_services()->sessionCache->clear();
219  _elgg_services()->dataCache->clear();
220  } else {
221  _elgg_services()->entityTable->invalidateCache($options['guid']);
222  }
223  }
224 
231  public function populateFromEntities(...$guids) {
232  try {
234  } catch (DataFormatException $e) {
235  return null;
236  }
237 
238  if (empty($guids)) {
239  return null;
240  }
241 
242  $cached_values = [];
243 
244  foreach ($guids as $i => $guid) {
245  $value = $this->cache->load($guid);
246  if ($value !== null) {
247  $cached_values[$guid] = $value;
248  unset($guids[$i]);
249  }
250  }
251 
252  if (empty($guids)) {
253  return $cached_values;
254  }
255 
256  $data = _elgg_services()->metadataTable->getRowsForGuids($guids);
257 
258  $values = [];
259  foreach ($data as $row) {
260  $values[$row->entity_guid][] = $row;
261  }
262 
263  // store always for every guid, even if there is no metadata
264  foreach ($guids as $guid) {
265  $metadata = elgg_extract($guid, $values, []);
266 
267  $this->cache->save($guid, $metadata);
268  $cached_values[$guid] = $metadata;
269  }
270 
271  return $cached_values;
272  }
273 
284  public function filterMetadataHeavyEntities(array $guids, $limit = 1024000) {
285 
286  $guids = _elgg_services()->metadataTable->getAll([
287  'guids' => $guids,
288  'limit' => false,
289  'callback' => function($e) {
290  return (int) $e->entity_guid;
291  },
292  'selects' => ['SUM(LENGTH(n_table.value)) AS bytes'],
293  'order_by' => [
294  new OrderByClause('n_table.entity_guid'),
295  new OrderByClause('n_table.time_created'),
296  ],
297  'group_by' => [
298  new GroupByClause('n_table.entity_guid'),
299  ],
300  'having' => [
301  "bytes < $limit",
302  ]
303  ]);
304 
305  return $guids ? : [];
306  }
307 }
static normalizeGuids(...$args)
Flatten an array of data into an array of GUIDs.
Definition: Values.php:141
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
invalidateByOptions(array $options)
Invalidate based on options passed to the global *_metadata functions.
Saves user notification settings.
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:53
clear($entity_guid)
Forget about all metadata for an entity.
if($pagination &&($position== 'after'||$position== 'both')) $limit
Definition: list.php:108
Extends QueryBuilder with GROUP BY statements.
getEntityMetadata($entity_guid)
Returns loaded entity metadata.
isLoaded($entity_guid)
If true, getSingle() will return an accurate values from the DB.
getSingle($entity_guid, $name)
Get the metadata for a particular name.
$options
Elgg admin footer.
Definition: footer.php:6
$value
Definition: generic.php:51
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
clearAll()
Clear entire cache.
inject($entity_guid, array $values=[])
Set the visible metadata for an entity in the cache.
getSingleId($entity_guid, $name)
Get the metadata id for a particular name.
filterMetadataHeavyEntities(array $guids, $limit=1024000)
Filter out entities whose concatenated metadata values (INTs casted as string) exceed a threshold in ...
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:686
getAll($entity_guid)
Get all entity metadata.
Extends QueryBuilder with ORDER BY clauses.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
__construct(ElggCache $cache)
Constructor.
populateFromEntities(...$guids)
Populate the cache from a set of entities.
An exception thrown when there is a problem in the format of some data.
$metadata
Output annotation metadata.
Definition: metadata.php:9
In memory cache of known metadata values stored by entity.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:777
$guid
Reset an ElggUpgrade.
Definition: reset.php:6