XoopsModules25x /
tdmdownloads
| 1 | <?php declare(strict_types=1); |
||||
| 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 (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 $renameTables; |
||||
| 27 | |||||
| 28 | /** |
||||
| 29 | * Migrate constructor. |
||||
| 30 | * @throws \RuntimeException |
||||
| 31 | * @throws \InvalidArgumentException |
||||
| 32 | */ |
||||
| 33 | public function __construct() |
||||
| 34 | { |
||||
| 35 | $class = __NAMESPACE__ . '\\' . 'Configurator'; |
||||
| 36 | |||||
| 37 | if (!\class_exists($class)) { |
||||
| 38 | throw new \RuntimeException("Class '$class' not found"); |
||||
| 39 | } |
||||
| 40 | |||||
| 41 | $configurator = new $class(); |
||||
| 42 | |||||
| 43 | $this->renameTables = $configurator->renameTables; |
||||
| 44 | |||||
| 45 | $moduleDirName = \basename(\dirname(\dirname(__DIR__))); |
||||
| 46 | |||||
| 47 | parent::__construct($moduleDirName); |
||||
| 48 | } |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * change table prefix if needed |
||||
| 52 | */ |
||||
| 53 | private function changePrefix() |
||||
|
0 ignored issues
–
show
|
|||||
| 54 | { |
||||
| 55 | foreach ($this->renameTables as $oldName => $newName) { |
||||
| 56 | if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) { |
||||
| 57 | $this->tableHandler->renameTable($oldName, $newName); |
||||
| 58 | } |
||||
| 59 | } |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * Change integer IPv4 column to varchar IPv6 capable |
||||
| 64 | * |
||||
| 65 | * @param string $tableName table to convert |
||||
| 66 | * @param string $columnName column with IP address |
||||
| 67 | */ |
||||
| 68 | private function convertIPAddresses($tableName, $columnName) |
||||
|
0 ignored issues
–
show
|
|||||
| 69 | { |
||||
| 70 | if ($this->tableHandler->useTable($tableName)) { |
||||
| 71 | $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName); |
||||
| 72 | |||||
| 73 | if (false !== \mb_strpos($attributes, ' int(')) { |
||||
|
0 ignored issues
–
show
It seems like
$attributes can also be of type boolean; however, parameter $haystack of mb_strpos() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 74 | if (false === \mb_strpos($attributes, 'unsigned')) { |
||||
| 75 | $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL DEFAULT '0' "); |
||||
| 76 | |||||
| 77 | $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false); |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45) NOT NULL DEFAULT '' "); |
||||
| 81 | |||||
| 82 | $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false); |
||||
| 83 | } |
||||
| 84 | } |
||||
| 85 | } |
||||
| 86 | |||||
| 87 | /** |
||||
| 88 | * Move do* columns from newbb_posts to newbb_posts_text table |
||||
| 89 | */ |
||||
| 90 | private function moveDoColumns() |
||||
|
0 ignored issues
–
show
|
|||||
| 91 | { |
||||
| 92 | $tableName = 'newbb_posts_text'; |
||||
| 93 | |||||
| 94 | $srcTableName = 'newbb_posts'; |
||||
| 95 | |||||
| 96 | if ($this->tableHandler->useTable($tableName) |
||||
| 97 | && $this->tableHandler->useTable($srcTableName)) { |
||||
| 98 | $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml'); |
||||
| 99 | |||||
| 100 | if (false === $attributes) { |
||||
| 101 | $this->synchronizeTable($tableName); |
||||
| 102 | |||||
| 103 | $updateTable = $GLOBALS['xoopsDB']->prefix($tableName); |
||||
| 104 | |||||
| 105 | $joinTable = $GLOBALS['xoopsDB']->prefix($srcTableName); |
||||
| 106 | |||||
| 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 | |||||
| 109 | $this->tableHandler->addToQueue($sql); |
||||
| 110 | } |
||||
| 111 | } |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | /** |
||||
| 115 | * Perform any upfront actions before synchronizing the schema |
||||
| 116 | * |
||||
| 117 | * Some typical uses include |
||||
| 118 | * table and column renames |
||||
| 119 | * data conversions |
||||
| 120 | */ |
||||
| 121 | protected function preSyncActions() |
||||
| 122 | { |
||||
| 123 | /* |
||||
| 124 | // change 'bb' table prefix to 'newbb' |
||||
| 125 | $this->changePrefix(); |
||||
| 126 | // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point |
||||
| 127 | $this->moveDoColumns(); |
||||
| 128 | // Convert IP address columns from int to readable varchar(45) for IPv6 |
||||
| 129 | $this->convertIPAddresses('newbb_posts', 'poster_ip'); |
||||
| 130 | $this->convertIPAddresses('newbb_report', 'reporter_ip'); |
||||
| 131 | */ |
||||
| 132 | } |
||||
| 133 | } |
||||
| 134 |
This check looks for private methods that have been defined, but are not used inside the class.