Passed
Pull Request — master (#5)
by Michael
02:50
created

Migrate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Tdmdownloads\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
/**
16
 * Class Migrate synchronize existing tables with target schema
17
 *
18
 * @category  Migrate
19
 * @author    Richard Griffith <[email protected]>
20
 * @copyright 2016 XOOPS Project (https://xoops.org)
21
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22
 * @link      https://xoops.org
23
 */
24
class Migrate extends \Xmf\Database\Migrate
25
{
26
    private $renameTables;
27
28
    /**
29
     * Migrate constructor.
30
     * @throws \RuntimeException
31
     * @throws \InvalidArgumentException
32
     */
33
    public function __construct()
34
    {
35
        require_once dirname(dirname(__DIR__)) . '/include/config.php';
36
        $config             = getConfig();
37
        $this->renameTables = $config->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
            }
52
        }
53
    }
54
55
    /**
56
     * Change integer IPv4 column to varchar IPv6 capable
57
     *
58
     * @param string $tableName  table to convert
59
     * @param string $columnName column with IP address
60
     *
61
     * @return void
62
     */
63
    private function convertIPAddresses($tableName, $columnName)
64
    {
65
        if ($this->tableHandler->useTable($tableName)) {
66
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
67
            if (false !== strpos($attributes, ' int(')) {
68
                if (false === strpos($attributes, 'unsigned')) {
69
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
70
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
71
                }
72
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
73
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
74
            }
75
        }
76
    }
77
78
    /**
79
     * Move do* columns from newbb_posts to newbb_posts_text table
80
     *
81
     * @return void
82
     */
83
    private function moveDoColumns()
84
    {
85
        $tableName    = 'newbb_posts_text';
86
        $srcTableName = 'newbb_posts';
87
        if (false !== $this->tableHandler->useTable($tableName)
88
            && false !== $this->tableHandler->useTable($srcTableName)) {
89
            $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
90
            if (false === $attributes) {
91
                $this->synchronizeTable($tableName);
92
                $updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
93
                $joinTable   = $GLOBALS['xoopsDB']->prefix($srcTableName);
94
                $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';
95
                $this->tableHandler->addToQueue($sql);
96
            }
97
        }
98
    }
99
100
    /**
101
     * Perform any upfront actions before synchronizing the schema
102
     *
103
     * Some typical uses include
104
     *   table and column renames
105
     *   data conversions
106
     *
107
     * @return void
108
     */
109
    protected function preSyncActions()
110
    {
111
        // change 'bb' table prefix to 'newbb'
112
        $this->changePrefix();
113
        // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
114
        $this->moveDoColumns();
115
        // Convert IP address columns from int to readable varchar(45) for IPv6
116
        $this->convertIPAddresses('newbb_posts', 'poster_ip');
117
        $this->convertIPAddresses('newbb_report', 'reporter_ip');
118
    }
119
}
120