|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * phpBB Browser Push Notifications. An extension for the phpBB Forum Software package. |
| 5 | + * |
| 6 | + * @copyright (c) 2024, phpBB Limited <https://www.phpbb.com> |
| 7 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +namespace phpbb\webpushnotifications\migrations; |
| 12 | + |
| 13 | +use phpbb\db\migration\migration; |
| 14 | + |
| 15 | +class update_user_notifications extends migration |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @inheritDoc |
| 19 | + */ |
| 20 | + public static function depends_on() |
| 21 | + { |
| 22 | + return ['\phpbb\webpushnotifications\migrations\add_webpush']; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @inheritDoc |
| 27 | + */ |
| 28 | + public function effectively_installed() |
| 29 | + { |
| 30 | + $sql = 'SELECT method |
| 31 | + FROM ' . $this->table_prefix . "user_notifications |
| 32 | + WHERE method = '" . $this->db->sql_escape('notification.method.phpbb.wpn.webpush') . "'"; |
| 33 | + $result = $this->db->sql_query($sql); |
| 34 | + $row = $this->db->sql_fetchrow($result); |
| 35 | + $this->db->sql_freeresult($result); |
| 36 | + |
| 37 | + return $row !== false; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritDoc |
| 42 | + */ |
| 43 | + public function update_data() |
| 44 | + { |
| 45 | + return [ |
| 46 | + ['custom', [[$this, 'update_notifications']]], |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Add default push notifications for users in chunks |
| 52 | + * |
| 53 | + * @param $start int Start value for the update |
| 54 | + * @return int|true Next start value or true if complete |
| 55 | + */ |
| 56 | + public function update_notifications($start) |
| 57 | + { |
| 58 | + $start = (int) $start; |
| 59 | + $limit = 500; |
| 60 | + $updated = 0; |
| 61 | + |
| 62 | + $sql_ary = []; |
| 63 | + |
| 64 | + $sql = 'SELECT user_id |
| 65 | + FROM ' . $this->table_prefix . 'users |
| 66 | + ORDER BY user_id ASC'; |
| 67 | + $result = $this->db->sql_query_limit($sql, $limit, $start); |
| 68 | + |
| 69 | + while ($row = $this->db->sql_fetchrow($result)) |
| 70 | + { |
| 71 | + $sql_ary[] = [ |
| 72 | + 'item_type' => 'notification.type.pm', |
| 73 | + 'item_id' => 0, |
| 74 | + 'user_id' => (int) $row['user_id'], |
| 75 | + 'notify' => 1, |
| 76 | + 'method' => 'notification.method.phpbb.wpn.webpush', |
| 77 | + ]; |
| 78 | + $sql_ary[] = [ |
| 79 | + 'item_type' => 'notification.type.quote', |
| 80 | + 'item_id' => 0, |
| 81 | + 'user_id' => (int) $row['user_id'], |
| 82 | + 'notify' => 1, |
| 83 | + 'method' => 'notification.method.phpbb.wpn.webpush', |
| 84 | + ]; |
| 85 | + $updated++; |
| 86 | + } |
| 87 | + $this->db->sql_freeresult($result); |
| 88 | + |
| 89 | + $this->db->sql_multi_insert($this->table_prefix . 'user_notifications', $sql_ary); |
| 90 | + |
| 91 | + return ($updated === $limit) ? $start + $limit : true; |
| 92 | + } |
| 93 | +} |
0 commit comments