QueryBuilder
2 years ago
DbInfo.php
2 years ago
ExcludedTables.php
4 years ago
OptionPreservationHandler.php
2 years ago
SearchReplace.php
2 years ago
SelectedTables.php
2 years ago
TableDto.php
5 years ago
TableService.php
2 years ago
TablesRenamer.php
2 years ago
WpDbInfo.php
2 years ago
WpOptionsInfo.php
2 years ago
iDbInfo.php
2 years ago
WpOptionsInfo.php
88 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Database; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Adapter\Database; |
| 7 | |
| 8 | class WpOptionsInfo |
| 9 | { |
| 10 | /** @var mixed|Database */ |
| 11 | private $wpdb; |
| 12 | |
| 13 | public function __construct() |
| 14 | { |
| 15 | $this->wpdb = Wpstaging::make(Database::class)->getWpdb(); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Check whether the wp_options table is missing primary key | auto increment |
| 20 | * @param string $optionTable |
| 21 | * @return bool |
| 22 | */ |
| 23 | public function isOptionTablePrimaryKeyMissing(string $optionTable): bool |
| 24 | { |
| 25 | $fInfo = $this->getFieldInfo('option_id', $optionTable); |
| 26 | |
| 27 | // Check whether the flag have primary key and auto increment flag |
| 28 | if (isset($fInfo->flags) && ($fInfo->flags & MYSQLI_PRI_KEY_FLAG) && $fInfo->flags & MYSQLI_AUTO_INCREMENT_FLAG) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | if ($this->isPrimaryKeyIsOptionName($optionTable)) { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param string $optionTable |
| 41 | * @return bool |
| 42 | */ |
| 43 | public function isPrimaryKeyIsOptionName(string $optionTable): bool |
| 44 | { |
| 45 | $fInfo = $this->getFieldInfo('option_name', $optionTable); |
| 46 | // Abort if flag has no primary key |
| 47 | if (!(isset($fInfo->flags) && $fInfo->flags & MYSQLI_PRI_KEY_FLAG)) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | // Check if the field has a composite key |
| 52 | $results = $this->wpdb->get_results("SELECT `CONSTRAINT_NAME`,`COLUMN_NAME` FROM `information_schema`.`KEY_COLUMN_USAGE` WHERE `table_name`='{$optionTable}' AND `table_schema`=DATABASE()", ARRAY_A); |
| 53 | if (empty($results) || !is_array($results)) { |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | $found = 0; |
| 58 | while ($row = array_shift($results)) { |
| 59 | if ($row['CONSTRAINT_NAME'] === 'PRIMARY' && in_array($row['COLUMN_NAME'], ['option_name', 'option_id'])) { |
| 60 | $found++; |
| 61 | } |
| 62 | |
| 63 | if ($found > 1) { |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param string $fieldName |
| 73 | * @param string $tableName |
| 74 | * @return false|object|null |
| 75 | */ |
| 76 | protected function getFieldInfo(string $fieldName, string $tableName) |
| 77 | { |
| 78 | $result = $this->wpdb->dbh->query("SELECT {$fieldName} FROM {$tableName} LIMIT 1"); |
| 79 | if (!is_object($result)) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | $fieldInfo = $result->fetch_field(); |
| 84 | $result->free_result(); |
| 85 | return $fieldInfo; |
| 86 | } |
| 87 | } |
| 88 |