Elgg  Version 5.1
RemoveOrphanedThreadedComments.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Upgrades;
4 
8 
10 
14  public function getVersion(): int {
15  return 2023011701;
16  }
17 
21  public function shouldBeSkipped(): bool {
22  return empty($this->countItems());
23  }
24 
28  public function needsIncrementOffset(): bool {
29  return false;
30  }
31 
35  public function countItems(): int {
36  return elgg_count_entities($this->getOptions());
37  }
38 
42  public function run(Result $result, $offset): Result {
43  /* @var $batch \ElggBatch */
45  'offset' => $offset,
46  ]));
47  /* @var $comment \ElggComment */
48  foreach ($batch as $comment) {
49  if ($comment->delete()) {
50  $result->addSuccesses();
51  continue;
52  }
53 
54  $result->addFailures();
55  }
56 
57  return $result;
58  }
59 
68  protected function getOptions(array $options = []): array {
69  $defaults = [
70  'type' => 'object',
71  'subtype' => 'comment',
72  'limit' => 100,
73  'batch' => true,
74  'batch_inc_offset' => $this->needsIncrementOffset(),
75  'batch_size' => 50,
76  'metadata_name_value_pairs' => [
77  'name' => 'parent_guid',
78  'value' => 0,
79  'operand' => '>'
80  ],
81  'wheres' => [
82  function (QueryBuilder $qb, $main_alias) {
83  $sub = $qb->subquery('entities');
84  $sub->select('guid')
85  ->where($qb->compare('type', '=', 'object', ELGG_VALUE_STRING))
86  ->andWhere($qb->compare('subtype', '=', 'comment', ELGG_VALUE_STRING));
87 
88  $md = $qb->joinMetadataTable($main_alias, 'guid', 'parent_guid');
89  return $qb->compare("{$md}.value", 'NOT IN', $sub->getSQL());
90  }
91  ],
92  ];
93 
94  return array_merge($defaults, $options);
95  }
96 }
$defaults
Generic entity header upload helper.
Definition: header.php:6
addFailures(int $num=1)
Increment failure count.
Definition: Result.php:53
if(empty($count)) $offset
Definition: pagination.php:26
Database abstraction query builder.
addSuccesses(int $num=1)
Set an item (or items) as successfully upgraded.
Definition: Result.php:73
$options
Elgg admin footer.
Definition: footer.php:6
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:504
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:515
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
Result of a single BatchUpgrade run.
Definition: Result.php:10
if(!$entity instanceof\ElggEntity) if(!$entity->canComment()) $comment
Definition: save.php:42
subquery($table, $alias=null)
Creates a new SelectQueryBuilder for join/where subqueries using the DB connection of the primary Que...
const ELGG_VALUE_STRING
Definition: constants.php:112
joinMetadataTable($from_alias= '', $from_column= 'guid', $name=null, $join_type= 'inner', $joined_alias=null)
Join metadata table from alias and return joined table alias.
getOptions(array $options=[])
Get the options to fetch orphaned comments.
Class to extend for asynchronous upgrades, i.e.
$qb
Definition: queue.php:11