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
16class notification_options_reconvert extends \phpbb\db\migration\migration
17{
18    public static function depends_on()
19    {
20        return array('\phpbb\db\migration\data\v310\notifications_schema_fix');
21    }
22
23    public function update_data()
24    {
25        return array(
26            array('custom', array(array($this, 'purge_notifications'))),
27            array('custom', array(array($this, 'convert_notifications'))),
28        );
29    }
30
31    public function purge_notifications()
32    {
33        $sql = 'DELETE FROM ' . $this->table_prefix . 'user_notifications';
34        $this->sql_query($sql);
35    }
36
37    public function convert_notifications($start)
38    {
39        $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'user_notifications');
40
41        return $this->perform_conversion($insert_buffer, $start);
42    }
43
44    /**
45    * Perform the conversion (separate for testability)
46    *
47    * @param \phpbb\db\sql_insert_buffer        $insert_buffer
48    * @param int            $start        Start of staggering step
49    * @return        mixed        int start of the next step, null if the end was reached
50    */
51    public function perform_conversion(\phpbb\db\sql_insert_buffer $insert_buffer, $start)
52    {
53        $limit = 250;
54        $converted_users = 0;
55        $start = $start ?: 0;
56
57        $sql = 'SELECT user_id, user_notify_type, user_notify_pm
58            FROM ' . $this->table_prefix . 'users
59            ORDER BY user_id';
60        $result = $this->db->sql_query_limit($sql, $limit, $start);
61
62        while ($row = $this->db->sql_fetchrow($result))
63        {
64            $converted_users++;
65            $notification_methods = array();
66
67            // In-board notification
68            $notification_methods[] = '';
69
70            if ($row['user_notify_type'] == NOTIFY_EMAIL || $row['user_notify_type'] == NOTIFY_BOTH)
71            {
72                $notification_methods[] = 'email';
73            }
74
75            if ($row['user_notify_type'] == NOTIFY_IM || $row['user_notify_type'] == NOTIFY_BOTH)
76            {
77                $notification_methods[] = 'jabber';
78            }
79
80            // Notifications for posts
81            foreach (array('post', 'topic') as $item_type)
82            {
83                $this->add_method_rows(
84                    $insert_buffer,
85                    $item_type,
86                    0,
87                    $row['user_id'],
88                    $notification_methods
89                );
90            }
91
92            if ($row['user_notify_pm'])
93            {
94                // Notifications for private messages
95                // User either gets all methods or no method
96                $this->add_method_rows(
97                    $insert_buffer,
98                    'pm',
99                    0,
100                    $row['user_id'],
101                    $notification_methods
102                );
103            }
104        }
105        $this->db->sql_freeresult($result);
106
107        $insert_buffer->flush();
108
109        if ($converted_users < $limit)
110        {
111            // No more users left, we are done...
112            return;
113        }
114
115        return $start + $limit;
116    }
117
118    /**
119    * Insert method rows to DB
120    *
121    * @param \phpbb\db\sql_insert_buffer $insert_buffer
122    * @param string $item_type
123    * @param int $item_id
124    * @param int $user_id
125    * @param array $methods
126    */
127    protected function add_method_rows(\phpbb\db\sql_insert_buffer $insert_buffer, $item_type, $item_id, $user_id, array $methods)
128    {
129        $row_base = array(
130            'item_type'        => $item_type,
131            'item_id'        => (int) $item_id,
132            'user_id'        => (int) $user_id,
133            'notify'        => 1
134        );
135
136        foreach ($methods as $method)
137        {
138            $row_base['method'] = $method;
139            $insert_buffer->insert($row_base);
140        }
141    }
142}