Elgg  Version 4.3
NotificationsPrefix.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Upgrades;
4 
12 
19 
23  public function getVersion(): int {
24  return 2021022401;
25  }
26 
30  public function needsIncrementOffset(): bool {
31  return false;
32  }
33 
37  public function shouldBeSkipped(): bool {
38  $methods = _elgg_services()->notifications->getMethods();
39 
40  return empty($methods) || empty($this->countItems());
41  }
42 
46  public function countItems(): int {
48  }
49 
53  public function run(Result $result, $offset): Result {
54  $relationship_prefix = SubscriptionsService::RELATIONSHIP_PREFIX;
55  $methods = _elgg_services()->notifications->getMethods();
56 
58  'offset' => $offset,
59  ]));
60 
61  foreach ($methods as $method) {
62  $select = Select::fromTable('entity_relationships', 'r1');
63 
64  // exclude already migrated relationships
65  $exists = $select->subquery('entity_relationships', 'r2');
66  $exists->select('1')
67  ->where($select->compare('r1.guid_one', '=', 'r2.guid_one'))
68  ->andWhere($select->compare('r1.guid_two', '=', 'r2.guid_two'))
69  ->andWhere($select->compare('r2.relationship', '=', "{$relationship_prefix}:{$method}", ELGG_VALUE_STRING));
70 
71  // get old relationships
72  $select->select('id')
73  ->where($select->compare('r1.relationship', '=', "{$relationship_prefix}{$method}", ELGG_VALUE_STRING))
74  ->andWhere($select->compare('r1.guid_one', 'in', $guids, ELGG_VALUE_GUID))
75  ->andWhere($select->compare(null, 'not exists', $exists->getSQL()));
76 
77  $ids = _elgg_services()->db->getData($select, function($row) {
78  return (int) $row->id;
79  });
80  if (!empty($ids)) {
81  // update old relationships to new relationship
82  $update = Update::table('entity_relationships');
83  $update->set('relationship', $update->param("{$relationship_prefix}:{$method}", ELGG_VALUE_STRING))
84  ->where($update->compare('relationship', '=', "{$relationship_prefix}{$method}", ELGG_VALUE_STRING))
85  ->andWhere($update->compare('id', 'in', $ids, ELGG_VALUE_ID));
86 
87  _elgg_services()->db->updateData($update);
88  }
89 
90  // delete old relationships that couldn't be migrated because of key constraints
91  $delete = Delete::fromTable('entity_relationships');
92  $delete->where($delete->compare('guid_one', 'in', $guids, ELGG_VALUE_GUID))
93  ->andWhere($delete->compare('relationship', '=', "{$relationship_prefix}{$method}", ELGG_VALUE_STRING));
94 
95  _elgg_services()->db->deleteData($delete);
96  }
97 
98  $result->addSuccesses(count($guids));
99 
100  return $result;
101  }
102 
110  protected function getEntityGUIDOptions(array $options = []): array {
111  $methods = _elgg_services()->notifications->getMethods();
112 
113  $defaults = [
114  'limit' => 100,
115  'callback' => function($row) {
116  return (int) $row->guid;
117  },
118  'wheres' => [
119  function(QueryBuilder $qb, $main_alias) use ($methods) {
120  $rel = $qb->joinRelationshipTable($main_alias, 'guid', null, true);
121 
122  $old_relationships = [];
123  foreach ($methods as $method) {
124  $old_relationships[] = SubscriptionsService::RELATIONSHIP_PREFIX . $method;
125  }
126 
127  return $qb->compare("{$rel}.relationship", 'in', $old_relationships, ELGG_VALUE_STRING);
128  },
129  ],
130  ];
131 
132  return array_merge($defaults, $options);
133  }
134 }
Interface to be implement for asynchronous upgrades, i.e.
shouldBeSkipped()
Should this upgrade be skipped?If true, the upgrade will not be performed and cannot be accessed late...
static table($table, $alias=null)
{}
Definition: Update.php:13
getEntityGUIDOptions(array $options=[])
Get options for entity guid selection.
$defaults
const ELGG_VALUE_GUID
Definition: constants.php:128
if(!$count) $offset
Definition: pagination.php:26
Database abstraction query builder.
$delete
const ELGG_VALUE_ID
Definition: constants.php:129
addSuccesses(int $num=1)
Set an item (or items) as successfully upgraded.
Definition: Result.php:73
$options
Elgg admin footer.
Definition: footer.php:6
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
getVersion()
Version of the upgrade.This tells the date when the upgrade was added. It consists of eight digits an...
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
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
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.
if(in_array('elgg-popup', $classes)) if(in_array('elgg-toggle', $classes)) $rel
Definition: url.php:155
Result of a single BatchUpgrade run.
Definition: Result.php:10
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
const ELGG_VALUE_STRING
Definition: constants.php:127
countItems()
The total number of items to process during the upgrade.If unknown, Batch::UNKNOWN_COUNT should be re...
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
Migrate the notification subscription relationship to a new naming convention.
joinRelationshipTable($from_alias= '', $from_column= 'guid', $name=null, $inverse=false, $join_type= 'inner', $joined_alias=null)
Join relationship table from alias and return joined table alias.
$qb
Definition: queue.php:11
$methods
Definition: subscribe.php:8
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13