Elgg  Version master
NotificationsPrefix.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Upgrades;
4 
13 
20 
24  public function getVersion(): int {
25  return 2021022401;
26  }
27 
31  public function needsIncrementOffset(): bool {
32  return false;
33  }
34 
38  public function shouldBeSkipped(): bool {
39  $methods = _elgg_services()->notifications->getMethods();
40 
41  return empty($methods) || empty($this->countItems());
42  }
43 
47  public function countItems(): int {
49  }
50 
54  public function run(Result $result, $offset): Result {
55  $relationship_prefix = SubscriptionsService::RELATIONSHIP_PREFIX;
56  $methods = _elgg_services()->notifications->getMethods();
57 
59  'offset' => $offset,
60  ]));
61 
62  foreach ($methods as $method) {
64 
65  // exclude already migrated relationships
66  $exists = $select->subquery(RelationshipsTable::TABLE_NAME, 'r2');
67  $exists->select('1')
68  ->where($select->compare("{$select->getTableAlias()}.guid_one", '=', "{$exists->getTableAlias()}.guid_one"))
69  ->andWhere($select->compare("{$select->getTableAlias()}.guid_two", '=', "{$exists->getTableAlias()}.guid_two"))
70  ->andWhere($select->compare("{$exists->getTableAlias()}.relationship", '=', "{$relationship_prefix}:{$method}", ELGG_VALUE_STRING));
71 
72  // get old relationships
73  $select->select('id')
74  ->where($select->compare("{$select->getTableAlias()}.relationship", '=', "{$relationship_prefix}{$method}", ELGG_VALUE_STRING))
75  ->andWhere($select->compare("{$select->getTableAlias()}.guid_one", 'in', $guids, ELGG_VALUE_GUID))
76  ->andWhere("NOT EXISTS ({$exists->getSQL()})");
77 
78  $ids = _elgg_services()->db->getData($select, function($row) {
79  return (int) $row->id;
80  });
81  if (!empty($ids)) {
82  // update old relationships to new relationship
84  $update->set('relationship', $update->param("{$relationship_prefix}:{$method}", ELGG_VALUE_STRING))
85  ->where($update->compare('relationship', '=', "{$relationship_prefix}{$method}", ELGG_VALUE_STRING))
86  ->andWhere($update->compare('id', 'in', $ids, ELGG_VALUE_ID));
87 
88  _elgg_services()->db->updateData($update);
89  }
90 
91  // delete old relationships that couldn't be migrated because of key constraints
93  $delete->where($delete->compare('guid_one', 'in', $guids, ELGG_VALUE_GUID))
94  ->andWhere($delete->compare('relationship', '=', "{$relationship_prefix}{$method}", ELGG_VALUE_STRING));
95 
96  _elgg_services()->db->deleteData($delete);
97  }
98 
99  $result->addSuccesses(count($guids));
100 
101  return $result;
102  }
103 
111  protected function getEntityGUIDOptions(array $options = []): array {
112  $methods = _elgg_services()->notifications->getMethods();
113 
114  $defaults = [
115  'limit' => 100,
116  'callback' => function($row) {
117  return (int) $row->guid;
118  },
119  'wheres' => [
120  function(QueryBuilder $qb, $main_alias) use ($methods) {
121  $rel = $qb->joinRelationshipTable($main_alias, 'guid', null, true);
122 
123  $old_relationships = [];
124  foreach ($methods as $method) {
125  $old_relationships[] = SubscriptionsService::RELATIONSHIP_PREFIX . $method;
126  }
127 
128  return $qb->compare("{$rel}.relationship", 'in', $old_relationships, ELGG_VALUE_STRING);
129  },
130  ],
131  ];
132 
133  return array_merge($defaults, $options);
134  }
135 }
$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:505
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:516
Result of a single BatchUpgrade run.
Definition: Result.php:10
joinRelationshipTable(string $from_alias= '', string $from_column= 'guid', $name=null, bool $inverse=false,?string $join_type= 'inner', string $joined_alias=null)
Join relationship table from alias and return joined table alias.
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.
Class to extend for asynchronous upgrades, i.e.
$qb
Definition: queue.php:12
$methods
Definition: subscribe.php:8
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13