fluent-boards
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Database
/
Concerns
/
MaintainsDatabase.php
MaintainsDatabase.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 2.1.0, at vendor/wpfluent/framework/src/WPFluent/Database/Concerns/MaintainsDatabase.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentBoards\Framework\Database\Concerns; |
| 4 | |
| 5 | trait MaintainsDatabase |
| 6 | { |
| 7 | /** |
| 8 | * Check the table. |
| 9 | * |
| 10 | * @param string $table |
| 11 | * @return \stdClass |
| 12 | */ |
| 13 | public static function check($table) |
| 14 | { |
| 15 | if (static::hasTable($table)) { |
| 16 | $result = static::db()->get_row( |
| 17 | 'CHECK TABLE ' . static::table($table) |
| 18 | ); |
| 19 | |
| 20 | if ($result->Msg_text === 'OK') { |
| 21 | $result->status = true; |
| 22 | } else { |
| 23 | $result->status = false; |
| 24 | } |
| 25 | |
| 26 | return $result; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Analyze the table. |
| 32 | * |
| 33 | * @param string $table |
| 34 | * @return \stdClass |
| 35 | */ |
| 36 | public static function analyze($table) |
| 37 | { |
| 38 | if (!static::hasTable($table)) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | if (static::isSqlite()) { |
| 43 | $result = new \stdClass(); |
| 44 | $result->status = true; |
| 45 | $result->Msg_text = 'OK'; |
| 46 | return $result; |
| 47 | } |
| 48 | |
| 49 | $result = static::db()->get_row( |
| 50 | 'ANALYZE TABLE ' . static::table($table) |
| 51 | ); |
| 52 | |
| 53 | if ($result->Msg_text === 'OK') { |
| 54 | $result->status = true; |
| 55 | } else { |
| 56 | $result->status = false; |
| 57 | } |
| 58 | |
| 59 | return $result; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Repair or rebuild the table based on the storage engine. |
| 64 | * |
| 65 | * @param string $table |
| 66 | * @return \stdClass |
| 67 | */ |
| 68 | public static function repair($table) |
| 69 | { |
| 70 | if (!static::hasTable($table)) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | if (static::isSqlite()) { |
| 75 | $result = new \stdClass(); |
| 76 | $result->status = true; |
| 77 | $result->Msg_text = 'OK'; |
| 78 | return $result; |
| 79 | } |
| 80 | |
| 81 | if (($engine = static::getEngine($table)) === 'InnoDB') { |
| 82 | $result = static::repairInnoDB(static::table($table)); |
| 83 | } elseif ($engine === 'MyISAM') { |
| 84 | $result = static::repairMyISAM(static::table($table)); |
| 85 | } else { |
| 86 | $result = new \stdClass(); |
| 87 | $result->status = false; |
| 88 | $result->Msg_text = 'Unsupported table engine for repair'; |
| 89 | } |
| 90 | |
| 91 | return $result; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Repair the InnoDB table. |
| 96 | * |
| 97 | * @param string $table |
| 98 | * @return \stdClass |
| 99 | */ |
| 100 | public static function repairInnoDB($table) |
| 101 | { |
| 102 | $result = new \stdClass; |
| 103 | |
| 104 | $db = static::db(); |
| 105 | |
| 106 | $db->query('ANALYZE TABLE ' . $table); |
| 107 | |
| 108 | $optimizes = $db->get_row('OPTIMIZE TABLE ' . $table); |
| 109 | |
| 110 | if ( |
| 111 | $optimizes->Msg_text === 'OK' || |
| 112 | $optimizes->Msg_text === 'Table is already up to date' || |
| 113 | str_contains($optimizes->Msg_text, 'recreate + analyze') |
| 114 | ) { |
| 115 | $result->status = true; |
| 116 | $result->Msg_text = 'Table repaired successfully (recreate + analyze)'; |
| 117 | } else { |
| 118 | $result->status = false; |
| 119 | $result->Msg_text = $optimizes->Msg_text; |
| 120 | } |
| 121 | |
| 122 | return $result; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Repair the MYISAM table. |
| 127 | * |
| 128 | * @param string $table |
| 129 | * @return \stdClass |
| 130 | */ |
| 131 | public static function repairMyISAM($table) |
| 132 | { |
| 133 | $result = new \stdClass; |
| 134 | |
| 135 | $repaired = static::db()->get_row('REPAIR TABLE ' . $table); |
| 136 | |
| 137 | if ($repaired->Msg_text === 'OK') { |
| 138 | $result->status = true; |
| 139 | $result->Msg_text = 'Table repaired successfully'; |
| 140 | } else { |
| 141 | $result->status = false; |
| 142 | $result->Msg_text = $repaired->Msg_text; |
| 143 | } |
| 144 | |
| 145 | return $result; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Optimize the table. |
| 150 | * |
| 151 | * @param string $table |
| 152 | * @return \stdClass |
| 153 | */ |
| 154 | public static function optimize($table) |
| 155 | { |
| 156 | if (!static::hasTable($table)) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (static::isSqlite()) { |
| 161 | $result = new \stdClass(); |
| 162 | $result->status = true; |
| 163 | $result->Msg_text = 'OK'; |
| 164 | return $result; |
| 165 | } |
| 166 | |
| 167 | $result = static::db()->get_row( |
| 168 | 'OPTIMIZE TABLE ' . static::table($table) |
| 169 | ); |
| 170 | |
| 171 | if ($result->Msg_text === 'OK') { |
| 172 | $result->status = true; |
| 173 | } elseif (str_contains($result->Msg_text, 'analyze instead')) { |
| 174 | $result->status = true; |
| 175 | } else { |
| 176 | $result->status = false; |
| 177 | } |
| 178 | |
| 179 | return $result; |
| 180 | } |
| 181 | } |
| 182 |