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 */
45  $batch = elgg_get_entities($this->getOptions([
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 }
if(! $entity instanceof \ElggEntity) if(! $entity->canComment()) $comment
Definition: save.php:42
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
delete(bool $recursive=true, ?bool $persistent=null)
Deletes the entity.
Entity table database service.
Definition: EntityTable.php:24
Database abstraction query builder.
Class to extend for asynchronous upgrades, i.e.
Result of a single BatchUpgrade run.
Definition: Result.php:10
getVersion()
Version of the upgrade.This tells the date when the upgrade was added. It consists of eight digits an...
getOptions(array $options=[])
Get the options to fetch orphaned comments.
countItems()
The total number of items to process during the upgrade.If unknown, Batch::UNKNOWN_COUNT should be re...
needsIncrementOffset()
Should the run() method receive an offset representing all processed items?If true,...
run(Result $result, $offset)
Runs upgrade on a single batch of items.If countItems() returns Batch::UNKNOWN_COUNT,...
shouldBeSkipped()
Should this upgrade be skipped?If true, the upgrade will not be performed and cannot be accessed late...
const ELGG_VALUE_STRING
Definition: constants.php:112
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:518
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
$defaults
Generic entity header upload helper.
Definition: header.php:6
if(empty($count)) $offset
Definition: pagination.php:26
$qb
Definition: queue.php:12