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"))
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 }
$owner
Definition: upload.php:7
$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...
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
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
getUpgrade()
Returns the related upgrade entity.
Definition: Batch.php:94
if(empty($guid)) $upgrade
Definition: upgrade.php:11
$entity
Definition: reset.php:8
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
Subscribe all content owners to their own content.
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:518
Result of a single BatchUpgrade run.
Definition: Result.php:10
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:12
$methods
Definition: subscribe.php:8