Maintenance.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Table;
  4. use PhpMyAdmin\DatabaseInterface;
  5. use PhpMyAdmin\Dbal\DatabaseName;
  6. use PhpMyAdmin\Dbal\TableName;
  7. use PhpMyAdmin\Dbal\Warning;
  8. use PhpMyAdmin\Index;
  9. use PhpMyAdmin\Table\Maintenance\Message;
  10. use PhpMyAdmin\Util;
  11. use function __;
  12. use function htmlspecialchars;
  13. use function implode;
  14. use function sprintf;
  15. final class Maintenance
  16. {
  17. /** @var DatabaseInterface */
  18. private $dbi;
  19. public function __construct(DatabaseInterface $dbi)
  20. {
  21. $this->dbi = $dbi;
  22. }
  23. /**
  24. * @param TableName[] $tables
  25. *
  26. * @return array<int, array<string, Message[]>|string>
  27. * @psalm-return array{array<string, Message[]>, string}
  28. */
  29. public function getAnalyzeTableRows(DatabaseName $db, array $tables): array
  30. {
  31. $backQuotedTables = [];
  32. foreach ($tables as $table) {
  33. $backQuotedTables[] = Util::backquote($table->getName());
  34. }
  35. $query = 'ANALYZE TABLE ' . implode(', ', $backQuotedTables) . ';';
  36. $this->dbi->selectDb($db);
  37. /** @var array<int, array<string, string>> $result */
  38. $result = $this->dbi->fetchResult($query);
  39. $rows = [];
  40. foreach ($result as $row) {
  41. $message = Message::fromArray($row);
  42. $rows[$message->table][] = $message;
  43. }
  44. return [$rows, $query];
  45. }
  46. /**
  47. * @param TableName[] $tables
  48. *
  49. * @return array<int, array<string, Message[]>|string>
  50. * @psalm-return array{array<string, Message[]>, string}
  51. */
  52. public function getCheckTableRows(DatabaseName $db, array $tables): array
  53. {
  54. $backQuotedTables = [];
  55. foreach ($tables as $table) {
  56. $backQuotedTables[] = Util::backquote($table->getName());
  57. }
  58. $query = 'CHECK TABLE ' . implode(', ', $backQuotedTables) . ';';
  59. $this->dbi->selectDb($db);
  60. /** @var array<int, array<string, string>> $result */
  61. $result = $this->dbi->fetchResult($query);
  62. $rows = [];
  63. foreach ($result as $row) {
  64. $message = Message::fromArray($row);
  65. $rows[$message->table][] = $message;
  66. }
  67. return [$rows, $query];
  68. }
  69. /**
  70. * @param TableName[] $tables
  71. *
  72. * @return array<int, array<string, array<int, array<string, string|null>>>|string>
  73. * @psalm-return array{array<int, array<string, string|null>>, string, Warning[]}
  74. */
  75. public function getChecksumTableRows(DatabaseName $db, array $tables): array
  76. {
  77. $backQuotedTables = [];
  78. foreach ($tables as $table) {
  79. $backQuotedTables[] = Util::backquote($table->getName());
  80. }
  81. $query = 'CHECKSUM TABLE ' . implode(', ', $backQuotedTables) . ';';
  82. $this->dbi->selectDb($db);
  83. /** @var array<int, array<string, string|null>> $rows */
  84. $rows = $this->dbi->fetchResult($query);
  85. $warnings = $this->dbi->getWarnings();
  86. return [$rows, $query, $warnings];
  87. }
  88. /**
  89. * @param TableName[] $tables
  90. */
  91. public function getIndexesProblems(DatabaseName $db, array $tables): string
  92. {
  93. $indexesProblems = '';
  94. foreach ($tables as $table) {
  95. $check = Index::findDuplicates($table->getName(), $db->getName());
  96. if (empty($check)) {
  97. continue;
  98. }
  99. $indexesProblems .= htmlspecialchars(sprintf(__('Problems with indexes of table `%s`'), $table->getName()));
  100. $indexesProblems .= $check;
  101. }
  102. return $indexesProblems;
  103. }
  104. /**
  105. * @param TableName[] $tables
  106. *
  107. * @return array<int, array<string, Message[]>|string>
  108. * @psalm-return array{array<string, Message[]>, string}
  109. */
  110. public function getOptimizeTableRows(DatabaseName $db, array $tables): array
  111. {
  112. $backQuotedTables = [];
  113. foreach ($tables as $table) {
  114. $backQuotedTables[] = Util::backquote($table->getName());
  115. }
  116. $query = 'OPTIMIZE TABLE ' . implode(', ', $backQuotedTables) . ';';
  117. $this->dbi->selectDb($db);
  118. /** @var array<int, array<string, string>> $result */
  119. $result = $this->dbi->fetchResult($query);
  120. $rows = [];
  121. foreach ($result as $row) {
  122. $message = Message::fromArray($row);
  123. $rows[$message->table][] = $message;
  124. }
  125. return [$rows, $query];
  126. }
  127. /**
  128. * @param TableName[] $tables
  129. *
  130. * @return array<int, array<string, Message[]>|string>
  131. * @psalm-return array{array<string, Message[]>, string}
  132. */
  133. public function getRepairTableRows(DatabaseName $db, array $tables): array
  134. {
  135. $backQuotedTables = [];
  136. foreach ($tables as $table) {
  137. $backQuotedTables[] = Util::backquote($table->getName());
  138. }
  139. $query = 'REPAIR TABLE ' . implode(', ', $backQuotedTables) . ';';
  140. $this->dbi->selectDb($db);
  141. /** @var array<int, array<string, string>> $result */
  142. $result = $this->dbi->fetchResult($query);
  143. $rows = [];
  144. foreach ($result as $row) {
  145. $message = Message::fromArray($row);
  146. $rows[$message->table][] = $message;
  147. }
  148. return [$rows, $query];
  149. }
  150. }