Migrate::changePrefix()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 3
nop 0
dl 0
loc 22
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Newbb\Common;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
use XoopsModules\Newbb;
16
17
/**
18
 * Class Migrate synchronize existing tables with target schema
19
 *
20
 * @category  Migrate
21
 * @package   Newbb
22
 * @author    Richard Griffith <[email protected]>
23
 * @copyright 2016 XOOPS Project (https://xoops.org)
24
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
25
 * @link      https://xoops.org
26
 */
27
class Migrate extends \Xmf\Database\Migrate
28
{
29
    private $renameTables;
30
31
    /**
32
     * Migrate constructor.
33
     * @param \XoopsModules\Newbb\Common\Configurator $configurator
34
     */
35
    public function __construct(Newbb\Common\Configurator $configurator)
36
    {
37
        $this->renameTables = $configurator->renameTables;
38
39
        $moduleDirName = \basename(\dirname(\dirname(__DIR__)));
40
        parent::__construct($moduleDirName);
41
    }
42
43
    /**
44
     * change table prefix if needed
45
     */
46
    private function changePrefix()
47
    {
48
        foreach ($this->renameTables as $oldName => $newName) {
49
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
50
                $this->tableHandler->renameTable($oldName, $newName);
51
            } else {
52
                //                Newbb\Helper::getInstance()->getModule()->setErrors('Could not migrate table: '. $oldName . '! The table ' .$newName . ' already exist!');
53
                Newbb\Helper::getInstance()->getModule()->setErrors(
54
                    '<span style="font-weight: bold; color: #ff0000;">'
55
                    . 'Could not migrate table: '
56
                    . '</span>'
57
                    . $oldName
58
                    . '<span style="font-weight: bold; color: #ff0000;">'
59
                    . ' The table '
60
                    . '</span>'
61
                    . $newName
62
                    . '<span style="font-weight: bold; color: #ff0000;">'
63
                    . ' already exist!'
64
                    . '</span>'
65
                );
66
67
                \trigger_error('Could not migrate table: ' . $oldName . '! The table ' . $newName . ' already exist!');
68
            }
69
        }
70
    }
71
72
    /**
73
     * Change integer IPv4 column to varchar IPv6 capable
74
     *
75
     * @param string $tableName  table to convert
76
     * @param string $columnName column with IP address
77
     */
78
    private function convertIPAddresses($tableName, $columnName)
79
    {
80
        if ($this->tableHandler->useTable($tableName)) {
81
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
82
            if (false !== mb_strpos($attributes, ' int(')) {
83
                if (false === mb_strpos($attributes, 'unsigned')) {
84
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
85
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
86
                }
87
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
88
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
89
            }
90
        }
91
    }
92
93
    /**
94
     * Move do* columns from newbb_posts to newbb_posts_text table
95
     */
96
    private function moveDoColumns()
97
    {
98
        $tableName    = 'newbb_posts_text';
99
        $srcTableName = 'newbb_posts';
100
        if ($this->tableHandler->useTable($tableName)
101
            && $this->tableHandler->useTable($srcTableName)) {
102
            $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
103
            if (false === $attributes) {
104
                $this->synchronizeTable($tableName);
105
                $updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
106
                $joinTable   = $GLOBALS['xoopsDB']->prefix($srcTableName);
107
                $sql         = "UPDATE `$updateTable` t1 INNER JOIN `$joinTable` t2 ON t1.post_id = t2.post_id \n" . "SET t1.dohtml = t2.dohtml,  t1.dosmiley = t2.dosmiley, t1.doxcode = t2.doxcode\n" . '  , t1.doimage = t2.doimage, t1.dobr = t2.dobr';
108
                $this->tableHandler->addToQueue($sql);
109
            }
110
        }
111
    }
112
113
    /**
114
     * Perform any upfront actions before synchronizing the schema
115
     *
116
     * Some typical uses include
117
     *   table and column renames
118
     *   data conversions
119
     */
120
    protected function preSyncActions()
121
    {
122
        // change 'bb' table prefix to 'newbb'
123
        $this->changePrefix();
124
        // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
125
        $this->moveDoColumns();
126
        // Convert IP address columns from int to readable varchar(45) for IPv6
127
        $this->convertIPAddresses('newbb_posts', 'poster_ip');
128
        $this->convertIPAddresses('newbb_report', 'reporter_ip');
129
        $this->convertIPAddresses('newbb_online', 'online_ip');
130
        $this->convertIPAddresses('newbb_moderates', 'ip');
131
    }
132
}
133