Passed
Push — master ( 290aa0...437a59 )
by Michael
05:22 queued 02:53
created

Migrate::changePrefix()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
nc 3
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Xfguestbook\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__)) . '/config/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
    private function convertIPAddresses($tableName, $columnName)
62
    {
63
        if ($this->tableHandler->useTable($tableName)) {
64
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
65
            if (false !== mb_strpos($attributes, ' int(')) {
66
                if (false === mb_strpos($attributes, 'unsigned')) {
67
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
68
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
69
                }
70
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
71
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
72
            }
73
        }
74
    }
75
76
    /**
77
     * Move do* columns from newbb_posts to newbb_posts_text table
78
     */
79
    private function moveDoColumns()
80
    {
81
        $tableName = 'newbb_posts_text';
82
        $srcTableName = 'newbb_posts';
83
        if (false !== $this->tableHandler->useTable($tableName)
84
            && false !== $this->tableHandler->useTable($srcTableName)) {
85
            $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
86
            if (false === $attributes) {
87
                $this->synchronizeTable($tableName);
88
                $updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
89
                $joinTable = $GLOBALS['xoopsDB']->prefix($srcTableName);
90
                $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';
91
                $this->tableHandler->addToQueue($sql);
92
            }
93
        }
94
    }
95
96
    /**
97
     * Perform any upfront actions before synchronizing the schema
98
     *
99
     * Some typical uses include
100
     *   table and column renames
101
     *   data conversions
102
     */
103
    protected function preSyncActions()
104
    {
105
        // change 'bb' table prefix to 'newbb'
106
        $this->changePrefix();
107
        // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
108
        $this->moveDoColumns();
109
        // Convert IP address columns from int to readable varchar(45) for IPv6
110
        $this->convertIPAddresses('newbb_posts', 'poster_ip');
111
        $this->convertIPAddresses('newbb_report', 'reporter_ip');
112
    }
113
}
114