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 {
48  return elgg_count_entities($this->getEntityGUIDOptions());
49  }
50 
54  public function run(Result $result, $offset): Result {
55  $relationship_prefix = SubscriptionsService::RELATIONSHIP_PREFIX;
56  $methods = _elgg_services()->notifications->getMethods();
57 
58  $guids = elgg_get_entities($this->getEntityGUIDOptions([
59  'offset' => $offset,
60  ]));
61 
62  foreach ($methods as $method) {
63  $select = Select::fromTable(RelationshipsTable::TABLE_NAME, 'r1');
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
83  $update = Update::table(RelationshipsTable::TABLE_NAME);
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
92  $delete = Delete::fromTable(RelationshipsTable::TABLE_NAME);
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 }
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
$delete
Query builder for updating data in the database.
Definition: Delete.php:8
Database abstraction query builder.
Relationships table database service.
Query builder for fetching data from the database.
Definition: Select.php:8
Query builder for updating data in the database.
Definition: Update.php:8
Class to extend for asynchronous upgrades, i.e.
Result of a single BatchUpgrade run.
Definition: Result.php:10
Migrate the notification subscription relationship to a new naming convention.
needsIncrementOffset()
Should the run() method receive an offset representing all processed items?If true,...
getEntityGUIDOptions(array $options=[])
Get options for entity guid selection.
getVersion()
Version of the upgrade.This tells the date when the upgrade was added. It consists of eight digits an...
shouldBeSkipped()
Should this upgrade be skipped?If true, the upgrade will not be performed and cannot be accessed late...
countItems()
The total number of items to process during the upgrade.If unknown, Batch::UNKNOWN_COUNT should be re...
run(Result $result, $offset)
Runs upgrade on a single batch of items.If countItems() returns Batch::UNKNOWN_COUNT,...
const ELGG_VALUE_STRING
Definition: constants.php:112
const ELGG_VALUE_ID
Definition: constants.php:114
const ELGG_VALUE_GUID
Definition: constants.php:113
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:518
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
$defaults
Generic entity header upload helper.
Definition: header.php:6
if(empty($count)) $offset
Definition: pagination.php:26
$qb
Definition: queue.php:12
$methods
Definition: subscribe.php:8