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 }
static table(string $table)
Returns a QueryBuilder for updating data in a given table.
Definition: Update.php:17
$defaults
Generic entity header upload helper.
Definition: header.php:6
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
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
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:518
static fromTable(string $table)
Returns a QueryBuilder for deleting data from a given table.
Definition: Delete.php:17
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.
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
const ELGG_VALUE_STRING
Definition: constants.php:112
static fromTable(string $table, string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Definition: Select.php:18
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
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