Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
bot_update_v2
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 3
72
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 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 add_bots
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
42
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\v33x;
15
16class bot_update_v2 extends \phpbb\db\migration\migration
17{
18    public static function depends_on()
19    {
20        return ['\phpbb\db\migration\data\v33x\v334'];
21    }
22
23    public function update_data()
24    {
25        return [
26            ['custom', [[$this, 'add_bots']]],
27        ];
28    }
29
30    public function add_bots()
31    {
32        $bots = [
33            'Ahrefs [Bot]' => 'AhrefsBot/',
34            'Amazon [Bot]' => 'Amazonbot/',
35            'Semrush [Bot]' => 'SemrushBot/',
36        ];
37
38        $group_row = [];
39
40        foreach ($bots as $bot_name => $bot_agent)
41        {
42            $bot_name_clean = utf8_clean_string($bot_name);
43
44            $sql = 'SELECT user_id
45                FROM ' . $this->table_prefix . 'users
46                WHERE ' . $this->db->sql_build_array('SELECT', ['username_clean' => $bot_name_clean]);
47            $result = $this->db->sql_query($sql);
48            $bot_exists = (bool) $this->db->sql_fetchfield('user_id');
49            $this->db->sql_freeresult($result);
50
51            if ($bot_exists)
52            {
53                continue;
54            }
55
56            if (!count($group_row))
57            {
58                $sql = 'SELECT group_id, group_colour
59                    FROM ' . $this->table_prefix . 'groups
60                    WHERE ' . $this->db->sql_build_array('SELECT', ['group_name' => 'BOTS']);
61                $result = $this->db->sql_query($sql);
62                $group_row = $this->db->sql_fetchrow($result);
63                $this->db->sql_freeresult($result);
64
65                // Default fallback, should never get here
66                if (!count($group_row))
67                {
68                    $group_row['group_id'] = 6;
69                    $group_row['group_colour'] = '9E8DA7';
70                }
71            }
72
73            if (!function_exists('user_add'))
74            {
75                include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
76            }
77
78            $user_row = [
79                'user_type'                => USER_IGNORE,
80                'group_id'                => $group_row['group_id'],
81                'username'                => $bot_name,
82                'user_regdate'            => time(),
83                'user_password'            => '',
84                'user_colour'            => $group_row['group_colour'],
85                'user_email'            => '',
86                'user_lang'                => $this->config['default_lang'],
87                'user_style'            => $this->config['default_style'],
88                'user_timezone'            => 0,
89                'user_dateformat'        => $this->config['default_dateformat'],
90                'user_allow_massemail'    => 0,
91            ];
92
93            $user_id = user_add($user_row);
94            $sql = 'INSERT INTO ' . $this->table_prefix . 'bots ' . $this->db->sql_build_array('INSERT', [
95                'bot_active'    => 1,
96                'bot_name'        => $bot_name,
97                'user_id'        => (int) $user_id,
98                'bot_agent'        => $bot_agent,
99                'bot_ip'        => '',
100            ]);
101            $this->db->sql_query($sql);
102        }
103    }
104}