Elgg  Version master
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 }
addFailures(int $num=1)
Increment failure count.
Definition: Result.php:53
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
Migrate the old access collection notification preferences to the new logic The old settings are from...
compare(string $x, string $comparison, $y=null, string $type=null, bool $case_sensitive=null)
Build value comparison clause.
$user
Definition: ban.php:7
ElggMetadata.
Result of a single BatchUpgrade run.
Definition: Result.php:10
elgg_get_metadata(array $options=[])
Fetch metadata or perform a calculation on them.
Definition: metadata.php:32
const ELGG_VALUE_STRING
Definition: constants.php:112
$metadata
Output annotation metadata.
Definition: metadata.php:9
Class to extend for asynchronous upgrades, i.e.
$qb
Definition: queue.php:12