Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
profilefield_youtube_update
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 effectively_installed
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update_youtube_profile_field
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
6
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 profilefield_youtube_update extends \phpbb\db\migration\migration
17{
18    public static $youtube_url_matcher = 'https:\\/\\/(www\\.)?youtube\\.com\\/.+';
19
20    public function effectively_installed()
21    {
22        $profile_fields = $this->table_prefix . 'profile_fields';
23
24        $result = $this->db->sql_query(
25            "SELECT field_validation
26                FROM $profile_fields
27                WHERE field_name = 'phpbb_youtube'"
28        );
29
30        $row = $this->db->sql_fetchrow($result);
31        $this->db->sql_freeresult($result);
32
33        return !$row || $row['field_validation'] === self::$youtube_url_matcher;
34    }
35
36    public static function depends_on()
37    {
38        return ['\phpbb\db\migration\data\v33x\v337'];
39    }
40
41    public function update_data()
42    {
43        return [['custom', [[$this, 'update_youtube_profile_field']]]];
44    }
45
46    public function update_youtube_profile_field()
47    {
48        $profile_fields = $this->table_prefix . 'profile_fields';
49        $profile_fields_data = $this->table_prefix . 'profile_fields_data';
50
51        $field_data = [
52            'field_length'        => 40,
53            'field_minlen'        => strlen('https://youtube.com/c/') + 1,
54            'field_maxlen'        => 255,
55            'field_validation'    => self::$youtube_url_matcher,
56            'field_contact_url'    => '%s'
57        ];
58
59        $sql = 'UPDATE ' . $profile_fields . '
60            SET ' . $this->db->sql_build_array('UPDATE', $field_data) . "
61            WHERE field_name = 'phpbb_youtube'";
62        $this->db->sql_query($sql);
63
64        $yt_profile_field = 'pf_phpbb_youtube';
65        $prepend_legacy_youtube_url = $this->db->sql_concatenate(
66            "'https://youtube.com/user/'", $yt_profile_field
67        );
68        $is_not_already_youtube_url = $this->db->sql_not_like_expression(
69            $this->db->get_any_char()
70                . 'youtube.com/'
71                . $this->db->get_any_char()
72        );
73
74        // We're done if the profile field doesn't exist
75        if (!$this->db_tools->sql_column_exists($profile_fields_data, $yt_profile_field))
76        {
77            return;
78        }
79
80        $this->db->sql_query(
81            "UPDATE $profile_fields_data SET
82                $yt_profile_field = $prepend_legacy_youtube_url
83                WHERE $yt_profile_field <> ''
84                AND $yt_profile_field $is_not_already_youtube_url"
85        );
86    }
87}