DataDictionaryController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Database;
  4. use PhpMyAdmin\DatabaseInterface;
  5. use PhpMyAdmin\Index;
  6. use PhpMyAdmin\Relation;
  7. use PhpMyAdmin\Response;
  8. use PhpMyAdmin\Template;
  9. use PhpMyAdmin\Transformations;
  10. use PhpMyAdmin\Util;
  11. use function is_array;
  12. use function str_replace;
  13. class DataDictionaryController extends AbstractController
  14. {
  15. /** @var Relation */
  16. private $relation;
  17. /** @var Transformations */
  18. private $transformations;
  19. /** @var DatabaseInterface */
  20. private $dbi;
  21. /**
  22. * @param Response $response
  23. * @param string $db Database name
  24. * @param Relation $relation
  25. * @param Transformations $transformations
  26. * @param DatabaseInterface $dbi
  27. */
  28. public function __construct($response, Template $template, $db, $relation, $transformations, $dbi)
  29. {
  30. parent::__construct($response, $template, $db);
  31. $this->relation = $relation;
  32. $this->transformations = $transformations;
  33. $this->dbi = $dbi;
  34. }
  35. public function index(): void
  36. {
  37. Util::checkParameters(['db'], true);
  38. $header = $this->response->getHeader();
  39. $header->enablePrintView();
  40. $cfgRelation = $this->relation->getRelationsParam();
  41. $comment = $this->relation->getDbComment($this->db);
  42. $this->dbi->selectDb($this->db);
  43. $tablesNames = $this->dbi->getTables($this->db);
  44. $tables = [];
  45. foreach ($tablesNames as $tableName) {
  46. $showComment = (string) $this->dbi->getTable(
  47. $this->db,
  48. $tableName
  49. )->getStatusInfo('TABLE_COMMENT');
  50. [, $primaryKeys] = Util::processIndexData(
  51. $this->dbi->getTableIndexes($this->db, $tableName)
  52. );
  53. [$foreigners, $hasRelation] = $this->relation->getRelationsAndStatus(
  54. ! empty($cfgRelation['relation']),
  55. $this->db,
  56. $tableName
  57. );
  58. $columnsComments = $this->relation->getComments($this->db, $tableName);
  59. $columns = $this->dbi->getColumns($this->db, $tableName);
  60. $rows = [];
  61. foreach ($columns as $row) {
  62. $extractedColumnSpec = Util::extractColumnSpec($row['Type']);
  63. $relation = '';
  64. if ($hasRelation) {
  65. $foreigner = $this->relation->searchColumnInForeigners(
  66. $foreigners,
  67. $row['Field']
  68. );
  69. if (is_array($foreigner) && isset($foreigner['foreign_table'], $foreigner['foreign_field'])) {
  70. $relation = $foreigner['foreign_table'];
  71. $relation .= ' -> ';
  72. $relation .= $foreigner['foreign_field'];
  73. }
  74. }
  75. $mime = '';
  76. if ($cfgRelation['mimework']) {
  77. $mimeMap = $this->transformations->getMime(
  78. $this->db,
  79. $tableName,
  80. true
  81. );
  82. if (is_array($mimeMap) && isset($mimeMap[$row['Field']]['mimetype'])) {
  83. $mime = str_replace(
  84. '_',
  85. '/',
  86. $mimeMap[$row['Field']]['mimetype']
  87. );
  88. }
  89. }
  90. $rows[$row['Field']] = [
  91. 'name' => $row['Field'],
  92. 'has_primary_key' => isset($primaryKeys[$row['Field']]),
  93. 'type' => $extractedColumnSpec['type'],
  94. 'print_type' => $extractedColumnSpec['print_type'],
  95. 'is_nullable' => $row['Null'] !== '' && $row['Null'] !== 'NO',
  96. 'default' => $row['Default'] ?? null,
  97. 'comment' => $columnsComments[$row['Field']] ?? '',
  98. 'mime' => $mime,
  99. 'relation' => $relation,
  100. ];
  101. }
  102. $tables[$tableName] = [
  103. 'name' => $tableName,
  104. 'comment' => $showComment,
  105. 'has_relation' => $hasRelation,
  106. 'has_mime' => $cfgRelation['mimework'],
  107. 'columns' => $rows,
  108. 'indexes' => Index::getFromTable($tableName, $this->db),
  109. ];
  110. }
  111. $this->render('database/data_dictionary/index', [
  112. 'database' => $this->db,
  113. 'comment' => $comment,
  114. 'tables' => $tables,
  115. ]);
  116. }
  117. }