Maintenance.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Table;
  4. use PhpMyAdmin\DatabaseInterface;
  5. use PhpMyAdmin\Index;
  6. use PhpMyAdmin\Util;
  7. use function implode;
  8. use function sprintf;
  9. final class Maintenance
  10. {
  11. /** @var DatabaseInterface */
  12. private $dbi;
  13. public function __construct(DatabaseInterface $dbi)
  14. {
  15. $this->dbi = $dbi;
  16. }
  17. /**
  18. * @param string[] $tables
  19. *
  20. * @return array
  21. */
  22. public function getAnalyzeTableRows(string $db, array $tables): array
  23. {
  24. $backQuotedTables = Util::backquote($tables);
  25. $query = 'ANALYZE TABLE ' . implode(', ', $backQuotedTables) . ';';
  26. $this->dbi->selectDb($db);
  27. $result = $this->dbi->fetchResult($query);
  28. $rows = [];
  29. foreach ($result as $row) {
  30. $rows[$row['Table']][] = $row;
  31. }
  32. return [$rows, $query];
  33. }
  34. /**
  35. * @param string[] $tables
  36. *
  37. * @return array
  38. */
  39. public function getCheckTableRows(string $db, array $tables): array
  40. {
  41. $backQuotedTables = Util::backquote($tables);
  42. $query = 'CHECK TABLE ' . implode(', ', $backQuotedTables) . ';';
  43. $this->dbi->selectDb($db);
  44. $result = $this->dbi->fetchResult($query);
  45. $rows = [];
  46. foreach ($result as $row) {
  47. $rows[$row['Table']][] = $row;
  48. }
  49. return [$rows, $query];
  50. }
  51. /**
  52. * @param string[] $tables
  53. *
  54. * @return array
  55. */
  56. public function getChecksumTableRows(string $db, array $tables): array
  57. {
  58. $backQuotedTables = Util::backquote($tables);
  59. $query = 'CHECKSUM TABLE ' . implode(', ', $backQuotedTables) . ';';
  60. $this->dbi->selectDb($db);
  61. $rows = $this->dbi->fetchResult($query);
  62. $warnings = $this->dbi->getWarnings();
  63. return [$rows, $query, $warnings];
  64. }
  65. /** @param string[] $tables */
  66. public function getIndexesProblems(string $db, array $tables): string
  67. {
  68. $indexesProblems = '';
  69. foreach ($tables as $table) {
  70. $check = Index::findDuplicates($table, $db);
  71. if (empty($check)) {
  72. continue;
  73. }
  74. $indexesProblems .= sprintf(__('Problems with indexes of table `%s`'), $table);
  75. $indexesProblems .= $check;
  76. }
  77. return $indexesProblems;
  78. }
  79. /**
  80. * @param string[] $tables
  81. *
  82. * @return array
  83. */
  84. public function getOptimizeTableRows(string $db, array $tables): array
  85. {
  86. $backQuotedTables = Util::backquote($tables);
  87. $query = 'OPTIMIZE TABLE ' . implode(', ', $backQuotedTables) . ';';
  88. $this->dbi->selectDb($db);
  89. $result = $this->dbi->fetchResult($query);
  90. $rows = [];
  91. foreach ($result as $row) {
  92. $rows[$row['Table']][] = $row;
  93. }
  94. return [$rows, $query];
  95. }
  96. /**
  97. * @param string[] $tables
  98. *
  99. * @return array
  100. */
  101. public function getRepairTableRows(string $db, array $tables): array
  102. {
  103. $backQuotedTables = Util::backquote($tables);
  104. $query = 'REPAIR TABLE ' . implode(', ', $backQuotedTables) . ';';
  105. $this->dbi->selectDb($db);
  106. $result = $this->dbi->fetchResult($query);
  107. $rows = [];
  108. foreach ($result as $row) {
  109. $rows[$row['Table']][] = $row;
  110. }
  111. return [$rows, $query];
  112. }
  113. }