DataDictionaryController.php 5.0 KB

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