Elgg  Version 4.3
MigrateACLNotificationPreferences.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Upgrades;
4 
8 
16 
20  public function getVersion(): int {
21  return 2021040801;
22  }
23 
27  public function needsIncrementOffset(): bool {
28  return false;
29  }
30 
34  public function shouldBeSkipped(): bool {
35  return empty($this->countItems());
36  }
37 
41  public function countItems(): int {
42  return elgg_get_metadata([
43  'type' => 'user',
44  'count' => true,
45  'wheres' => [
46  function(QueryBuilder $qb, $main_alias) {
47  return $qb->compare("{$main_alias}.name", 'like', 'collections_notifications_preferences_%', ELGG_VALUE_STRING);
48  },
49  ],
50  ]);
51  }
52 
56  public function run(Result $result, $offset): Result {
58  'type' => 'user',
59  'limit' => 50,
60  'offset' => $offset,
61  'wheres' => [
62  function(QueryBuilder $qb, $main_alias) {
63  return $qb->compare("{$main_alias}.name", 'like', 'collections_notifications_preferences_%', ELGG_VALUE_STRING);
64  },
65  ],
66  ]);
67 
68  $remove_md = function (\ElggMetadata $md) use (&$result) {
69  if ($md->delete()) {
70  $result->addSuccesses();
71  } else {
72  $result->addFailures();
73  }
74  };
75 
76  /* @var $md \ElggMetadata */
77  foreach ($metadata as $md) {
78  if ($md->value !== -1) {
79  // preference for an access collection which isn't migrated
80  $remove_md($md);
81  continue;
82  }
83 
84  $method = substr($md->name, strlen('collections_notifications_preferences_'));
85  $user = $md->getEntity();
86  if ($user instanceof \ElggUser) {
87  // only truthy values were saved
88  $user->setNotificationSetting($method, true, 'friends');
89  }
90 
91  $remove_md($md);
92  }
93 
94  return $result;
95  }
96 }
Interface to be implement for asynchronous upgrades, i.e.
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
addFailures(int $num=1)
Increment failure count.
Definition: Result.php:53
if(!$count) $offset
Definition: pagination.php:26
Database abstraction query builder.
getVersion()
Version of the upgrade.This tells the date when the upgrade was added. It consists of eight digits an...
addSuccesses(int $num=1)
Set an item (or items) as successfully upgraded.
Definition: Result.php:73
Migrate the old access collection notification preferences to the new logic The old settings are from...
$user
Definition: ban.php:7
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
ElggMetadata.
Result of a single BatchUpgrade run.
Definition: Result.php:10
shouldBeSkipped()
Should this upgrade be skipped?If true, the upgrade will not be performed and cannot be accessed late...
elgg_get_metadata(array $options=[])
Fetch metadata or perform a calculation on them.
Definition: metadata.php:32
const ELGG_VALUE_STRING
Definition: constants.php:127
$metadata
Output annotation metadata.
Definition: metadata.php:9
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
countItems()
The total number of items to process during the upgrade.If unknown, Batch::UNKNOWN_COUNT should be re...
$qb
Definition: queue.php:11