fluent-community
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Database
/
Concerns
/
MaintainsDatabase.php
MaintainsDatabase.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.98, at vendor/wpfluent/framework/src/WPFluent/Database/Concerns/MaintainsDatabase.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentCommunity\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 | $result = static::db()->get_row( |
| 40 | 'ANALYZE TABLE ' . static::table($table) |
| 41 | ); |
| 42 | |
| 43 | if ($result->Msg_text === 'OK') { |
| 44 | $result->status = true; |
| 45 | } else { |
| 46 | $result->status = false; |
| 47 | } |
| 48 | |
| 49 | return $result; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Repair or rebuild the table based on the storage engine. |
| 55 | * |
| 56 | * @param string $table |
| 57 | * @return stdClass |
| 58 | */ |
| 59 | public static function repair($table) |
| 60 | { |
| 61 | if (!static::hasTable($table)) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | if ($engine = static::getEngine($table) === 'InnoDB') { |
| 66 | $result = static::repairInnoDB(static::table($table)); |
| 67 | } elseif ($engine === 'MyISAM') { |
| 68 | $result = static::repairMyISAM(static::table($table)); |
| 69 | } else { |
| 70 | $result = new \stdClass(); |
| 71 | $result->status = false; |
| 72 | $result->Msg_text = 'Unsupported table engine for repair'; |
| 73 | } |
| 74 | |
| 75 | return $result; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Repair the InnoDB table. |
| 80 | * |
| 81 | * @param string $table |
| 82 | * @return stdClass |
| 83 | */ |
| 84 | public static function repairInnoDB($table) |
| 85 | { |
| 86 | $result = new \stdClass; |
| 87 | |
| 88 | $db = static::db(); |
| 89 | |
| 90 | $db->query('ANALYZE TABLE ' . $table); |
| 91 | |
| 92 | $optimizes = $db->get_row('OPTIMIZE TABLE ' . $table); |
| 93 | |
| 94 | if ( |
| 95 | $optimizes->Msg_text === 'OK' || |
| 96 | $optimizes->Msg_text === 'Table is already up to date' || |
| 97 | str_contains($optimizes->Msg_text, 'recreate + analyze') |
| 98 | ) { |
| 99 | $result->status = true; |
| 100 | $result->Msg_text = 'Table repaired successfully (recreate + analyze)'; |
| 101 | } else { |
| 102 | $result->status = false; |
| 103 | $result->Msg_text = $optimizes->Msg_text; |
| 104 | } |
| 105 | |
| 106 | return $result; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Repair the MYISAM table. |
| 111 | * |
| 112 | * @param string $table |
| 113 | * @return stdClass |
| 114 | */ |
| 115 | public static function repairMyISAM($table) |
| 116 | { |
| 117 | $result = new \stdClass; |
| 118 | |
| 119 | $repaired = static::db()->get_row('REPAIR TABLE ' . $table); |
| 120 | |
| 121 | if ($repaired->Msg_text === 'OK') { |
| 122 | $result->status = true; |
| 123 | $result->Msg_text = 'Table repaired successfully'; |
| 124 | } else { |
| 125 | $result->status = false; |
| 126 | $result->Msg_text = $repaired->Msg_text; |
| 127 | } |
| 128 | |
| 129 | return $result; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Optimize the table. |
| 134 | * |
| 135 | * @param string $table |
| 136 | * @return stdClass |
| 137 | */ |
| 138 | public static function optimize($table) |
| 139 | { |
| 140 | if (!static::hasTable($table)) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $result = static::db()->get_row( |
| 145 | 'OPTIMIZE TABLE ' . static::table($table) |
| 146 | ); |
| 147 | |
| 148 | if ($result->Msg_text === 'OK') { |
| 149 | $result->status = true; |
| 150 | } elseif (str_contains($result->Msg_text, 'analyze instead')) { |
| 151 | $result->status = true; |
| 152 | } else { |
| 153 | $result->status = false; |
| 154 | } |
| 155 | |
| 156 | return $result; |
| 157 | } |
| 158 | } |
| 159 |