Elgg  Version 5.1
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->getUpgrade();
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 }
$owner
Definition: upload.php:7
$defaults
Generic entity header upload helper.
Definition: header.php:6
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
getUpgrade()
Returns the related upgrade entity.
Definition: Batch.php:100
$entity
Definition: reset.php:8
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:504
Subscribe all content owners to their own content.
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
subquery($table, $alias=null)
Creates a new SelectQueryBuilder for join/where subqueries using the DB connection of the primary Que...
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
const ELGG_VALUE_STRING
Definition: constants.php:112
$metadata
Output annotation metadata.
Definition: metadata.php:9
getOptions(array $options=[])
Get query options.
elgg_get_notification_methods()
Returns registered delivery methods for notifications [ &#39;email&#39; => &#39;email&#39;, &#39;sms&#39; => &#39;sms&#39;...
Class to extend for asynchronous upgrades, i.e.
$qb
Definition: queue.php:11
$methods
Definition: subscribe.php:8