Elgg  Version 6.2
RemoveDeletedEntitiesHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Entity;
4 
7 
12 
20  public function __invoke(\Elgg\Event $event): void {
21  $retention = (int) elgg_get_config('trash_retention');
22  if ($retention < 1) {
23  return;
24  }
25 
27  /* @var $entities \ElggBatch */
28  $entities = elgg_get_entities([
29  'limit' => false,
30  'batch' => true,
31  'batch_inc_offset' => false,
32  'wheres' => [
33  function(QueryBuilder $qb, $main_alias) {
34  // only deleted items
35  return $qb->compare("{$main_alias}.deleted", '=', 'yes', ELGG_VALUE_STRING);
36  },
37  function(QueryBuilder $qb, $main_alias) use ($retention) {
38  // past the retention period
39  return $qb->compare("{$main_alias}.time_deleted", '<', \Elgg\Values::normalizeTimestamp("-{$retention} days"), ELGG_VALUE_TIMESTAMP);
40  },
41  function(QueryBuilder $qb, $main_alias) {
42  // get only the root deleted items (not the related/sub items)
43  // the related items will be deleted with the root item
44  $sub = $qb->subquery(RelationshipsTable::TABLE_NAME);
45  $sub->select('guid_one')
46  ->where($qb->compare('relationship', '=', 'deleted_with', ELGG_VALUE_STRING));
47 
48  return $qb->compare("{$main_alias}.guid", 'not in', $sub->getSQL());
49  }
50  ],
51  'sort_by' => [
52  'property' => 'time_deleted',
53  'direction' => 'ASC',
54  ],
55  ]);
56 
57  $starttime = microtime(true);
58 
59  /* @var $entity \ElggEntity */
60  foreach ($entities as $entity) {
61  if ((microtime(true) - $starttime) > 300) {
62  // limit the cleanup to 5 minutes
63  break;
64  }
65 
66  if (!$entity->delete(true, true)) {
67  $entities->reportFailure();
68  }
69  }
70  });
71  }
72 }
$entity
Definition: reset.php:8
Database abstraction query builder.
Relationships table database service.
Cleanup deleted entities from the database.
__invoke(\Elgg\Event $event)
After a grace period remove deleted entities from the database.
Models an event passed to event handlers.
Definition: Event.php:11
static normalizeTimestamp($time)
Returns timestamp value of the time representation.
Definition: Values.php:63
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
const ELGG_VALUE_STRING
Definition: constants.php:112
const ELGG_IGNORE_ACCESS
elgg_call() flags
Definition: constants.php:121
const ELGG_SHOW_DISABLED_ENTITIES
Definition: constants.php:123
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
const ELGG_SHOW_DELETED_ENTITIES
Definition: constants.php:127
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags.
Definition: elgglib.php:306
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
$retention
Show a notice about the retention period of trashed items.
Definition: notice.php:6
$qb
Definition: queue.php:12