Elgg  Version 4.3
ContentOwnerSubscriptions.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Upgrades;
4 
9 
16 
20  public function getVersion(): int {
21  return 2021060401;
22  }
23 
27  public function needsIncrementOffset(): bool {
28  return false;
29  }
30 
34  public function shouldBeSkipped(): bool {
36  if (empty($methods)) {
37  return true;
38  }
39 
40  return empty($this->countItems());
41  }
42 
46  public function countItems(): int {
47  return elgg_count_entities($this->getOptions());
48  }
49 
53  public function run(Result $result, $offset): Result {
54 
55  /* @var $entities \ElggBatch */
56  $entities = elgg_get_entities($this->getOptions([
57  'offset' => $offset,
58  ]));
59 
60  $process_entity = function (\ElggEntity $entity) use (&$result) {
61  // using setMetadata because we don't want te rely on the magic setters,
62  // as they can store data in a different table (eg. widgets, plugins)
63  $entity->setMetadata('__content_owner_subscription_upgrade_migrated', time());
64 
65  $result->addSuccesses();
66  };
67 
68  /* @var $entity \ElggEntity */
69  foreach ($entities as $entity) {
70  $owner = $entity->getOwnerEntity();
71  if (!$owner instanceof \ElggUser) {
72  // how did this happen?
73  $process_entity($entity);
74  continue;
75  }
76 
77  // get user preferences
78  $content_preferences = $owner->getNotificationSettings('content_create');
79  $enabled_methods = array_keys(array_filter($content_preferences));
80  if (empty($enabled_methods)) {
81  $process_entity($entity);
82  continue;
83  }
84 
85  if ($entity->hasSubscriptions($owner->guid)) {
86  // already subscribed
87  $process_entity($entity);
88  continue;
89  }
90 
91  // add subscption
92  $entity->addSubscription($owner->guid, $enabled_methods);
93 
94  $process_entity($entity);
95  }
96 
97  return $result;
98  }
99 
108  protected function getOptions(array $options = []): array {
109  $upgrade = $this->getUpgradeEntity();
110 
111  $defaults = [
112  'created_before' => $upgrade->time_created,
113  'limit' => 100,
114  'batch' => true,
115  'batch_inc_offset' => $this->needsIncrementOffset(),
116  'batch_size' => 50,
117  'preload_owners' => true,
118  'wheres' => [
119  function (QueryBuilder $qb, $main_alias) {
120  $owner_guids = $qb->subquery('entities');
121  $owner_guids->select('guid')
122  ->andWhere($qb->compare('type', '=', 'user', ELGG_VALUE_STRING));
123 
124  return $qb->compare("{$main_alias}.owner_guid", 'in', $owner_guids->getSQL());
125  },
126  function (QueryBuilder $qb, $main_alias) {
127  $metadata = $qb->subquery('metadata', 'md');
128  $metadata->select('md.entity_guid')
129  ->where($qb->compare('md.name', '=', '__content_owner_subscription_upgrade_migrated', ELGG_VALUE_STRING));
130 
131  return $qb->compare("{$main_alias}.guid", 'NOT IN', $metadata->getSQL());
132  },
133  function (QueryBuilder $qb, $main_alias) {
134  // exclude some subtypes of objects
135  $object = $qb->merge([
136  $qb->compare("{$main_alias}.type", '=', 'object', ELGG_VALUE_STRING),
137  $qb->compare("{$main_alias}.subtype", 'NOT IN', ['widget', 'site_notification', 'messages'], ELGG_VALUE_STRING),
138  ], 'AND');
139 
140  // migrate objects and groups
141  return $qb->merge([
142  $qb->compare("{$main_alias}.type", '=', 'group', ELGG_VALUE_STRING),
143  $object,
144  ], 'OR');
145  },
146  function (QueryBuilder $qb, $main_alias) {
147  $notification_relationship = $qb->subquery('entity_relationships', 'er');
148  $notification_relationship->select('er.guid_one')
149  ->andWhere($qb->compare('er.guid_two', '=', "{$main_alias}.guid"))
151 
152  return $qb->compare("{$main_alias}.owner_guid", 'NOT IN', $notification_relationship->getSQL());
153  },
154  ],
155  ];
156 
157  return array_merge($defaults, $options);
158  }
159 
165  protected function getUpgradeEntity(): \ElggUpgrade {
166  return _elgg_services()->upgradeLocator->getUpgradeByClass(self::class);
167  }
168 }
Interface to be implement for asynchronous upgrades, i.e.
needsIncrementOffset()
Should the run() method receive an offset representing all processed items?If true, run() will receive as $offset the number of items already processed. This is useful if you are only modifying data, and need to use the $offset in a function like elgg_get_entities*() to know how many to skip over.If false, run() will receive as $offset the total number of failures. This should be used if your process deletes or moves data out of the way of the process. E.g. if you delete 50 objects on each run(), you may still use the $offset to skip objects that already failed once.bool
$owner
Definition: upload.php:7
run(Result $result, $offset)
Runs upgrade on a single batch of items.If countItems() returns Batch::UNKNOWN_COUNT, this method must call $result->markCompleted() when the upgrade is complete.Result of the batch (this must be returned) Number to skip when processingResult
$defaults
if(!$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
if(empty($guid)) $upgrade
Definition: upgrade.php:11
$entity
Definition: reset.php:8
Represents an upgrade that runs outside of the upgrade.php script.
Definition: ElggUpgrade.php:26
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
Subscribe all content owners to their own content.
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:556
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
countItems()
The total number of items to process during the upgrade.If unknown, Batch::UNKNOWN_COUNT should be re...
Result of a single BatchUpgrade run.
Definition: Result.php:10
subquery($table, $alias=null)
Creates a new SelectQueryBuilder for join/where subqueries using the DB connection of the primary Que...
shouldBeSkipped()
Should this upgrade be skipped?If true, the upgrade will not be performed and cannot be accessed late...
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
getUpgradeEntity()
Get the ElggUpgrade for this Upgrade Batch.
const ELGG_VALUE_STRING
Definition: constants.php:127
$metadata
Output annotation metadata.
Definition: metadata.php:9
getOptions(array $options=[])
Get query options.
getVersion()
Version of the upgrade.This tells the date when the upgrade was added. It consists of eight digits an...
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
elgg_get_notification_methods()
Returns registered delivery methods for notifications [ &#39;email&#39; => &#39;email&#39;, &#39;sms&#39; => &#39;sms&#39;...
$qb
Definition: queue.php:11
$methods
Definition: subscribe.php:8