Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.48% covered (warning)
81.48%
44 / 54
16.67% covered (danger)
16.67%
1 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
notification_options_reconvert
81.48% covered (warning)
81.48%
44 / 54
16.67% covered (danger)
16.67%
1 / 6
17.63
0.00% covered (danger)
0.00%
0 / 1
 depends_on
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update_data
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 purge_notifications
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 convert_notifications
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 perform_conversion
97.22% covered (success)
97.22%
35 / 36
0.00% covered (danger)
0.00%
0 / 1
10
 add_method_rows
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\db\migration\data\v310;
15
16use phpbb\messenger\method\messenger_interface;
17
18class notification_options_reconvert extends \phpbb\db\migration\migration
19{
20    public static function depends_on()
21    {
22        return array('\phpbb\db\migration\data\v310\notifications_schema_fix');
23    }
24
25    public function update_data()
26    {
27        return array(
28            array('custom', array(array($this, 'purge_notifications'))),
29            array('custom', array(array($this, 'convert_notifications'))),
30        );
31    }
32
33    public function purge_notifications()
34    {
35        $sql = 'DELETE FROM ' . $this->table_prefix . 'user_notifications';
36        $this->sql_query($sql);
37    }
38
39    public function convert_notifications($start)
40    {
41        $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'user_notifications');
42
43        return $this->perform_conversion($insert_buffer, $start);
44    }
45
46    /**
47    * Perform the conversion (separate for testability)
48    *
49    * @param \phpbb\db\sql_insert_buffer        $insert_buffer
50    * @param int            $start        Start of staggering step
51    * @return        mixed        int start of the next step, null if the end was reached
52    */
53    public function perform_conversion(\phpbb\db\sql_insert_buffer $insert_buffer, $start)
54    {
55        $limit = 250;
56        $converted_users = 0;
57        $start = $start ?: 0;
58
59        $sql = 'SELECT user_id, user_notify_type, user_notify_pm
60            FROM ' . $this->table_prefix . 'users
61            ORDER BY user_id';
62        $result = $this->db->sql_query_limit($sql, $limit, $start);
63
64        while ($row = $this->db->sql_fetchrow($result))
65        {
66            $converted_users++;
67            $notification_methods = array();
68
69            // In-board notification
70            $notification_methods[] = '';
71
72            if ($row['user_notify_type'] == messenger_interface::NOTIFY_EMAIL || $row['user_notify_type'] == messenger_interface::NOTIFY_BOTH)
73            {
74                $notification_methods[] = 'email';
75            }
76
77            if ($row['user_notify_type'] == messenger_interface::NOTIFY_IM || $row['user_notify_type'] == messenger_interface::NOTIFY_BOTH)
78            {
79                $notification_methods[] = 'jabber';
80            }
81
82            // Notifications for posts
83            foreach (array('post', 'topic') as $item_type)
84            {
85                $this->add_method_rows(
86                    $insert_buffer,
87                    $item_type,
88                    0,
89                    $row['user_id'],
90                    $notification_methods
91                );
92            }
93
94            if ($row['user_notify_pm'])
95            {
96                // Notifications for private messages
97                // User either gets all methods or no method
98                $this->add_method_rows(
99                    $insert_buffer,
100                    'pm',
101                    0,
102                    $row['user_id'],
103                    $notification_methods
104                );
105            }
106        }
107        $this->db->sql_freeresult($result);
108
109        $insert_buffer->flush();
110
111        if ($converted_users < $limit)
112        {
113            // No more users left, we are done...
114            return;
115        }
116
117        return $start + $limit;
118    }
119
120    /**
121    * Insert method rows to DB
122    *
123    * @param \phpbb\db\sql_insert_buffer $insert_buffer
124    * @param string $item_type
125    * @param int $item_id
126    * @param int $user_id
127    * @param array $methods
128    */
129    protected function add_method_rows(\phpbb\db\sql_insert_buffer $insert_buffer, $item_type, $item_id, $user_id, array $methods)
130    {
131        $row_base = array(
132            'item_type'        => $item_type,
133            'item_id'        => (int) $item_id,
134            'user_id'        => (int) $user_id,
135            'notify'        => 1
136        );
137
138        foreach ($methods as $method)
139        {
140            $row_base['method'] = $method;
141            $insert_buffer->insert($row_base);
142        }
143    }
144}