Elgg  Version 5.1
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 }
$defaults
Generic entity header upload helper.
Definition: header.php:6
static table($table, $alias=null)
{}
Definition: Update.php:13
getEntityGUIDOptions(array $options=[])
Get options for entity guid selection.
if(empty($count)) $offset
Definition: pagination.php:26
const ELGG_VALUE_GUID
Definition: constants.php:113
Database abstraction query builder.
$delete
const ELGG_VALUE_ID
Definition: constants.php:114
addSuccesses(int $num=1)
Set an item (or items) as successfully upgraded.
Definition: Result.php:73
$options
Elgg admin footer.
Definition: footer.php:6
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:504
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
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:112
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
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.
Class to extend for asynchronous upgrades, i.e.
$qb
Definition: queue.php:11
$methods
Definition: subscribe.php:8
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13