Elgg  Version master
RemoveOrphanedThreadedComments.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Upgrades;
4 
9 
11 
15  public function getVersion(): int {
16  return 2023011701;
17  }
18 
22  public function shouldBeSkipped(): bool {
23  return empty($this->countItems());
24  }
25 
29  public function needsIncrementOffset(): bool {
30  return false;
31  }
32 
36  public function countItems(): int {
37  return elgg_count_entities($this->getOptions());
38  }
39 
43  public function run(Result $result, $offset): Result {
44  /* @var $batch \ElggBatch */
46  'offset' => $offset,
47  ]));
48  /* @var $comment \ElggComment */
49  foreach ($batch as $comment) {
50  if ($comment->delete()) {
51  $result->addSuccesses();
52  continue;
53  }
54 
55  $result->addFailures();
56  }
57 
58  return $result;
59  }
60 
69  protected function getOptions(array $options = []): array {
70  $defaults = [
71  'type' => 'object',
72  'subtype' => 'comment',
73  'limit' => 100,
74  'batch' => true,
75  'batch_inc_offset' => $this->needsIncrementOffset(),
76  'batch_size' => 50,
77  'metadata_name_value_pairs' => [
78  'name' => 'parent_guid',
79  'value' => 0,
80  'operand' => '>'
81  ],
82  'wheres' => [
83  function (QueryBuilder $qb, $main_alias) {
84  $sub = $qb->subquery(EntityTable::TABLE_NAME);
85  $sub->select('guid')
86  ->where($qb->compare('type', '=', 'object', ELGG_VALUE_STRING))
87  ->andWhere($qb->compare('subtype', '=', 'comment', ELGG_VALUE_STRING));
88 
89  $md = $qb->joinMetadataTable($main_alias, 'guid', 'parent_guid');
90  return $qb->compare("{$md}.value", 'NOT IN', $sub->getSQL());
91  }
92  ],
93  ];
94 
95  return array_merge($defaults, $options);
96  }
97 }
$defaults
Generic entity header upload helper.
Definition: header.php:6
subquery(string $table, string $alias=null)
Creates a new SelectQueryBuilder for join/where sub queries using the DB connection of the primary Qu...
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:505
compare(string $x, string $comparison, $y=null, string $type=null, bool $case_sensitive=null)
Build value comparison clause.
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:516
Result of a single BatchUpgrade run.
Definition: Result.php:10
if(!$entity instanceof\ElggEntity) if(!$entity->canComment()) $comment
Definition: save.php:42
joinMetadataTable(string $from_alias= '', string $from_column= 'guid', $name=null,?string $join_type= 'inner', string $joined_alias=null)
Join metadata table from alias and return joined table alias.
const ELGG_VALUE_STRING
Definition: constants.php:112
getOptions(array $options=[])
Get the options to fetch orphaned comments.
Class to extend for asynchronous upgrades, i.e.
$qb
Definition: queue.php:12