1
|
|
|
<?php declare(strict_types=1);
|
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Xoopsfaq\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.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
|
22
|
|
|
* @link https://xoops.org
|
23
|
|
|
*/
|
24
|
|
|
class Migrate extends \Xmf\Database\Migrate
|
25
|
|
|
{
|
26
|
|
|
private $moduleDirName;
|
27
|
|
|
private $renameColumns;
|
28
|
|
|
private $renameTables;
|
29
|
|
|
|
30
|
|
|
/**
|
31
|
|
|
* Migrate constructor.
|
32
|
|
|
* @throws \InvalidArgumentException
|
33
|
|
|
* @throws \RuntimeException
|
34
|
|
|
*/
|
35
|
|
|
public function __construct()
|
36
|
|
|
{
|
37
|
|
|
$class = __NAMESPACE__ . '\\' . 'Configurator';
|
38
|
|
|
if (!\class_exists($class)) {
|
39
|
|
|
throw new \RuntimeException("Class '$class' not found");
|
40
|
|
|
}
|
41
|
|
|
$configurator = new $class();
|
42
|
|
|
$this->renameTables = $configurator->renameTables;
|
43
|
|
|
$this->renameColumns = $configurator->renameColumns;
|
44
|
|
|
|
45
|
|
|
$this->moduleDirName = \basename(\dirname(__DIR__, 2));
|
46
|
|
|
parent::__construct($this->moduleDirName);
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
/**
|
50
|
|
|
* change table prefix if needed
|
51
|
|
|
*/
|
52
|
|
|
private function changePrefix(): void
|
53
|
|
|
{
|
54
|
|
|
// foreach ($this->renameTables as $oldName => $newName) {
|
55
|
|
|
// if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
|
56
|
|
|
// $this->tableHandler->renameTable($oldName, $newName);
|
57
|
|
|
// }
|
58
|
|
|
// }
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
/**
|
62
|
|
|
* Change integer IPv4 column to varchar IPv6 capable
|
63
|
|
|
*
|
64
|
|
|
* @param string $tableName table to convert
|
65
|
|
|
* @param string $columnName column with IP address
|
66
|
|
|
*/
|
67
|
|
|
private function convertIPAddresses(string $tableName, string $columnName): void
|
|
|
|
|
68
|
|
|
{
|
69
|
|
|
// if ($this->tableHandler->useTable($tableName)) {
|
70
|
|
|
// $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
|
71
|
|
|
// if (false !== \mb_strpos($attributes, ' int(')) {
|
72
|
|
|
// if (false === \mb_strpos($attributes, 'unsigned')) {
|
73
|
|
|
// $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL DEFAULT '0' ");
|
74
|
|
|
// $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
|
75
|
|
|
// }
|
76
|
|
|
// $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45) NOT NULL DEFAULT '' ");
|
77
|
|
|
// $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
|
78
|
|
|
// }
|
79
|
|
|
// }
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* @deprecated (just as an example here)
|
84
|
|
|
* Move do* columns from newbb_posts to newbb_posts_text table
|
85
|
|
|
*/
|
86
|
|
|
private function moveDoColumns(): void
|
87
|
|
|
{
|
88
|
|
|
// $tableName = 'newbb_posts_text';
|
89
|
|
|
// $srcTableName = 'newbb_posts';
|
90
|
|
|
// if ($this->tableHandler->useTable($tableName)
|
91
|
|
|
// && $this->tableHandler->useTable($srcTableName)) {
|
92
|
|
|
// $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
|
93
|
|
|
// if (false === $attributes) {
|
94
|
|
|
// $this->synchronizeTable($tableName);
|
95
|
|
|
// $updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
|
96
|
|
|
// $joinTable = $GLOBALS['xoopsDB']->prefix($srcTableName);
|
97
|
|
|
// $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';
|
98
|
|
|
// $this->tableHandler->addToQueue($sql);
|
99
|
|
|
// }
|
100
|
|
|
// }
|
101
|
|
|
}
|
102
|
|
|
|
103
|
|
|
/**
|
104
|
|
|
* rename table if needed
|
105
|
|
|
*/
|
106
|
|
|
private function renameTable(): void
|
107
|
|
|
{
|
108
|
|
|
foreach ($this->renameTables as $oldName => $newName) {
|
109
|
|
|
if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
|
110
|
|
|
$this->tableHandler->renameTable($oldName, $newName);
|
111
|
|
|
}
|
112
|
|
|
}
|
113
|
|
|
}
|
114
|
|
|
|
115
|
|
|
/**
|
116
|
|
|
* rename columns if needed
|
117
|
|
|
*/
|
118
|
|
|
private function renameColumns(): void
|
119
|
|
|
{
|
120
|
|
|
foreach ($this->renameColumns as $tableName) {
|
121
|
|
|
if ($this->tableHandler->useTable($tableName)) {
|
122
|
|
|
$oldName = $tableName['from'];
|
123
|
|
|
$newName = $tableName['to'];
|
124
|
|
|
$attributes = $this->tableHandler->getColumnAttributes($tableName, $oldName);
|
125
|
|
|
if (\is_string($attributes) && false !== \mb_strpos($attributes, ' int(')) {
|
126
|
|
|
$this->tableHandler->alterColumn($tableName, $oldName, $attributes, $newName);
|
127
|
|
|
}
|
128
|
|
|
}
|
129
|
|
|
}
|
130
|
|
|
}
|
131
|
|
|
|
132
|
|
|
/**
|
133
|
|
|
* Perform any upfront actions before synchronizing the schema
|
134
|
|
|
*
|
135
|
|
|
* Some typical uses include
|
136
|
|
|
* table and column renames
|
137
|
|
|
* data conversions
|
138
|
|
|
*/
|
139
|
|
|
final protected function preSyncActions(): void
|
140
|
|
|
{
|
141
|
|
|
// change 'bb' table prefix to 'newbb'
|
142
|
|
|
$this->changePrefix();
|
143
|
|
|
// columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
|
144
|
|
|
$this->moveDoColumns();
|
|
|
|
|
145
|
|
|
// Convert IP address columns from int to readable varchar(45) for IPv6
|
146
|
|
|
// $this->convertIPAddresses('newbb_posts', 'poster_ip');
|
147
|
|
|
// $this->convertIPAddresses('newbb_report', 'reporter_ip');
|
148
|
|
|
|
149
|
|
|
// rename table
|
150
|
|
|
if ($this->renameTables && \is_array($this->renameTables)) {
|
151
|
|
|
$this->renameTable();
|
152
|
|
|
}
|
153
|
|
|
// rename column
|
154
|
|
|
if ($this->renameColumns && \is_array($this->renameColumns)) {
|
155
|
|
|
$this->renameColumns();
|
156
|
|
|
}
|
157
|
|
|
}
|
158
|
|
|
}
|
159
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.