Elgg  Version master
ContentOwnerSubscriptions.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Upgrades;
4 
10 
17 
21  public function getVersion(): int {
22  return 2021060401;
23  }
24 
28  public function needsIncrementOffset(): bool {
29  return false;
30  }
31 
35  public function shouldBeSkipped(): bool {
37  if (empty($methods)) {
38  return true;
39  }
40 
41  return empty($this->countItems());
42  }
43 
47  public function countItems(): int {
48  return elgg_count_entities($this->getOptions());
49  }
50 
54  public function run(Result $result, $offset): Result {
55 
56  /* @var $entities \ElggBatch */
57  $entities = elgg_get_entities($this->getOptions([
58  'offset' => $offset,
59  ]));
60 
61  $process_entity = function (\ElggEntity $entity) use (&$result) {
62  // using setMetadata because we don't want te rely on the magic setters,
63  // as they can store data in a different table (eg. widgets, plugins)
64  $entity->setMetadata('__content_owner_subscription_upgrade_migrated', time());
65 
66  $result->addSuccesses();
67  };
68 
69  /* @var $entity \ElggEntity */
70  foreach ($entities as $entity) {
71  $owner = $entity->getOwnerEntity();
72  if (!$owner instanceof \ElggUser) {
73  // how did this happen?
74  $process_entity($entity);
75  continue;
76  }
77 
78  // get user preferences
79  $content_preferences = $owner->getNotificationSettings('content_create');
80  $enabled_methods = array_keys(array_filter($content_preferences));
81  if (empty($enabled_methods)) {
82  $process_entity($entity);
83  continue;
84  }
85 
86  if ($entity->hasSubscriptions($owner->guid)) {
87  // already subscribed
88  $process_entity($entity);
89  continue;
90  }
91 
92  // add subscption
93  $entity->addSubscription($owner->guid, $enabled_methods);
94 
95  $process_entity($entity);
96  }
97 
98  return $result;
99  }
100 
109  protected function getOptions(array $options = []): array {
110  $upgrade = $this->getUpgrade();
111 
112  $defaults = [
113  'created_before' => $upgrade->time_created,
114  'limit' => 100,
115  'batch' => true,
116  'batch_inc_offset' => $this->needsIncrementOffset(),
117  'batch_size' => 50,
118  'preload_owners' => true,
119  'wheres' => [
120  function (QueryBuilder $qb, $main_alias) {
121  $owner_guids = $qb->subquery(EntityTable::TABLE_NAME);
122  $owner_guids->select('guid')
123  ->andWhere($qb->compare('type', '=', 'user', ELGG_VALUE_STRING));
124 
125  return $qb->compare("{$main_alias}.owner_guid", 'in', $owner_guids->getSQL());
126  },
127  function (QueryBuilder $qb, $main_alias) {
128  $metadata = $qb->subquery('metadata', 'md');
129  $metadata->select('md.entity_guid')
130  ->where($qb->compare('md.name', '=', '__content_owner_subscription_upgrade_migrated', ELGG_VALUE_STRING));
131 
132  return $qb->compare("{$main_alias}.guid", 'NOT IN', $metadata->getSQL());
133  },
134  function (QueryBuilder $qb, $main_alias) {
135  // exclude some subtypes of objects
136  $object = $qb->merge([
137  $qb->compare("{$main_alias}.type", '=', 'object', ELGG_VALUE_STRING),
138  $qb->compare("{$main_alias}.subtype", 'NOT IN', ['widget', 'site_notification', 'messages'], ELGG_VALUE_STRING),
139  ], 'AND');
140 
141  // migrate objects and groups
142  return $qb->merge([
143  $qb->compare("{$main_alias}.type", '=', 'group', ELGG_VALUE_STRING),
144  $object,
145  ], 'OR');
146  },
147  function (QueryBuilder $qb, $main_alias) {
148  $notification_relationship = $qb->subquery('entity_relationships', 'er');
149  $notification_relationship->select('er.guid_one')
150  ->andWhere($qb->compare('er.guid_two', '=', "{$main_alias}.guid"))
151  ->andWhere($qb->compare('relationship', '=', SubscriptionsService::MUTE_NOTIFICATIONS_RELATIONSHIP, ELGG_VALUE_STRING));
152 
153  return $qb->compare("{$main_alias}.owner_guid", 'NOT IN', $notification_relationship->getSQL());
154  },
155  ],
156  ];
157 
158  return array_merge($defaults, $options);
159  }
160 }
$entity
Definition: reset.php:8
if(empty($guid)) $upgrade
Definition: upgrade.php:11
$owner
Definition: upload.php:7
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
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
Subscribe all content owners to their own content.
countItems()
The total number of items to process during the upgrade.If unknown, Batch::UNKNOWN_COUNT should be re...
shouldBeSkipped()
Should this upgrade be skipped?If true, the upgrade will not be performed and cannot be accessed late...
getVersion()
Version of the upgrade.This tells the date when the upgrade was added. It consists of eight digits an...
run(Result $result, $offset)
Runs upgrade on a single batch of items.If countItems() returns Batch::UNKNOWN_COUNT,...
getOptions(array $options=[])
Get query options.
needsIncrementOffset()
Should the run() method receive an offset representing all processed items?If true,...
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
if($email instanceof \Elgg\Email) $object
Definition: body.php:24
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
elgg_get_notification_methods()
Returns registered delivery methods for notifications [ 'email' => 'email', 'sms' => 'sms',...
if(empty($count)) $offset
Definition: pagination.php:26
$qb
Definition: queue.php:12
$methods
Definition: subscribe.php:8
$metadata
Output annotation metadata.
Definition: metadata.php:9