Elgg  Version 3.0
MetadataCache.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Cache;
3 
5 use Elgg\Values;
6 use ElggCache;
7 use ElggMetadata;
9 
16 
20  protected $cache;
21 
27  public function __construct(ElggCache $cache) {
28  $this->cache = $cache;
29  }
30 
43  public function inject($entity_guid, array $values = []) {
44  $metadata = [];
45  foreach ($values as $key => $value) {
46  if ($value instanceof ElggMetadata) {
47  $md = $value;
48  } else {
49  $md = new ElggMetadata();
50  $md->name = $key;
51  $md->value = $value;
52  $md->entity_guid = $entity_guid;
53  }
54 
55  $metadata[] = $md->toObject();
56  }
57 
58  $this->cache->save($entity_guid, $metadata);
59  }
60 
67  public function getAll($entity_guid) {
69  if (empty($metadata)) {
70  return [];
71  }
72 
73  $metadata_values = [];
74 
75  foreach ($metadata as $md) {
76  $metadata_values[$md->name][] = $md->value;
77  }
78 
79  return array_map(function($values) {
80  return count($values) > 1 ? $values : $values[0];
81  }, $metadata_values);
82  }
83 
97  public function getSingle($entity_guid, $name) {
99  if (empty($metadata)) {
100  return null;
101  }
102 
103  $values = [];
104 
105  foreach ($metadata as $md) {
106  if ($md->name !== $name) {
107  continue;
108  }
109 
110  $values[] = $md->value;
111  }
112 
113  if (empty($values)) {
114  return null;
115  }
116 
117  return count($values) > 1 ? $values : $values[0];
118 
119  }
120 
134  public function getSingleId($entity_guid, $name) {
136  if (empty($metadata)) {
137  return null;
138  }
139 
140  $ids = [];
141 
142  foreach ($metadata as $md) {
143  if ($md->name !== $name) {
144  continue;
145  }
146 
147  $ids[] = $md->id;
148  }
149 
150  if (empty($ids)) {
151  return null;
152  }
153 
154  return count($ids) > 1 ? $ids : $ids[0];
155  }
156 
164  public function clear($entity_guid) {
165  $this->invalidateByOptions([
166  'guid' => $entity_guid,
167  ]);
168  }
169 
177  public function isLoaded($entity_guid) {
178  return $this->cache->load($entity_guid) !== null;
179  }
180 
186  public function clearAll() {
187  $this->invalidateByOptions([]);
188  }
189 
197  public function getEntityMetadata($entity_guid) {
198  $entity_guid = (int) $entity_guid;
199  $metadata = $this->cache->load($entity_guid);
200  if ($metadata === null) {
202  }
203 
204  return $metadata;
205  }
206 
214  public function invalidateByOptions(array $options) {
215  if (empty($options['guid'])) {
216  _elgg_services()->sessionCache->clear();
217  _elgg_services()->dataCache->clear();
218  } else {
219  _elgg_services()->entityTable->invalidateCache($options['guid']);
220  }
221  }
222 
229  public function populateFromEntities(...$guids) {
230  try {
232  } catch (\DataFormatException $e) {
233  return null;
234  }
235 
236  if (empty($guids)) {
237  return null;
238  }
239 
240  $version = (int) _elgg_config()->version;
241  if (!empty($version) && ($version < 2016110900)) {
242  // can't use this during upgrade from 2.x to 3.0
243  return null;
244  }
245 
246  $cached_values = [];
247 
248  foreach ($guids as $i => $guid) {
249  $value = $this->cache->load($guid);
250  if ($value !== null) {
251  $cached_values[$guid] = $value;
252  unset($guids[$i]);
253  }
254  }
255 
256  if (empty($guids)) {
257  return $cached_values;
258  }
259 
260  $data = _elgg_services()->metadataTable->getRowsForGuids($guids);
261 
262  $values = [];
263 
264  foreach ($data as $i => $row) {
265  $row->value = ($row->value_type === 'text') ? $row->value : (int) $row->value;
266  $values[$row->entity_guid][] = $row;
267  }
268 
269  // store always for every guid, even if there is no metadata
270  foreach ($guids as $guid) {
271  $metadata = elgg_extract($guid, $values, []);
272 
273  $this->cache->save($guid, $metadata);
274  $cached_values[$guid] = $metadata;
275  }
276 
277  return $cached_values;
278  }
279 
290  public function filterMetadataHeavyEntities(array $guids, $limit = 1024000) {
291 
292  $guids = _elgg_services()->metadataTable->getAll([
293  'guids' => $guids,
294  'limit' => 0,
295  'callback' => function($e) {
296  return (int) $e->entity_guid;
297  },
298  'selects' => ['SUM(LENGTH(n_table.value)) AS bytes'],
299  'order_by' => [
300  new OrderByClause('n_table.entity_guid'),
301  new OrderByClause('n_table.time_created'),
302  ],
303  'group_by' => [
304  new GroupByClause('n_table.entity_guid'),
305  ],
306  'having' => [
307  "bytes < $limit",
308  ]
309  ]);
310 
311  return $guids ? : [];
312  }
313 }
static normalizeGuids(...$args)
Flatten an array of data into an array of GUIDs.
Definition: Values.php:139
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
invalidateByOptions(array $options)
Invalidate based on options passed to the global *_metadata functions.
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:57
clear($entity_guid)
Forget about all metadata for an entity.
$guid
Removes an admin notice.
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
$entity_guid
Definition: save.php:9
$metadata
Outputs object metadata $vars[&#39;metadata&#39;] Metadata/menu $vars[&#39;show_entity_menu&#39;] Show the entity m...
Definition: metadata.php:10
clearAll()
Clear entire cache.
$limit
Definition: userpicker.php:52
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:1131
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:55
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
_elgg_config()
Get the Elgg config service.
__construct(ElggCache $cache)
Constructor.
$value
Definition: debugging.php:7
populateFromEntities(...$guids)
Populate the cache from a set of entities.
In memory cache of known metadata values stored by entity.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
$version
Definition: version.php:14