SvgRelationSchema.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * Contains PhpMyAdmin\Plugins\Schema\Svg\RelationStatsSvg class
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Schema\Svg;
  7. use PhpMyAdmin\Plugins\Schema\Dia\TableStatsDia;
  8. use PhpMyAdmin\Plugins\Schema\Eps\TableStatsEps;
  9. use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
  10. use PhpMyAdmin\Plugins\Schema\Pdf\TableStatsPdf;
  11. use function in_array;
  12. use function max;
  13. use function min;
  14. use function sprintf;
  15. /**
  16. * RelationStatsSvg Relation Schema Class
  17. *
  18. * Purpose of this class is to generate the SVG XML Document because
  19. * SVG defines the graphics in XML format which is used for representing
  20. * the database diagrams as vector image. This class actually helps
  21. * in preparing SVG XML format.
  22. *
  23. * SVG XML is generated by using XMLWriter php extension and this class
  24. * inherits ExportRelationSchema class has common functionality added
  25. * to this class
  26. *
  27. * @name Svg_Relation_Schema
  28. */
  29. class SvgRelationSchema extends ExportRelationSchema
  30. {
  31. /** @var TableStatsDia[]|TableStatsEps[]|TableStatsPdf[]|TableStatsSvg[] */
  32. private $tables = [];
  33. /** @var RelationStatsSvg[] Relations */
  34. private $relations = [];
  35. /** @var int|float */
  36. private $xMax = 0;
  37. /** @var int|float */
  38. private $yMax = 0;
  39. /** @var int|float */
  40. private $xMin = 100000;
  41. /** @var int|float */
  42. private $yMin = 100000;
  43. /** @var int */
  44. private $tablewidth;
  45. /**
  46. * Upon instantiation This starts writing the SVG XML document
  47. * user will be prompted for download as .svg extension
  48. *
  49. * @see PMA_SVG
  50. *
  51. * @param string $db database name
  52. */
  53. public function __construct($db)
  54. {
  55. parent::__construct($db, new Svg());
  56. $this->setShowColor(isset($_REQUEST['svg_show_color']));
  57. $this->setShowKeys(isset($_REQUEST['svg_show_keys']));
  58. $this->setTableDimension(isset($_REQUEST['svg_show_table_dimension']));
  59. $this->setAllTablesSameWidth(isset($_REQUEST['svg_all_tables_same_width']));
  60. $this->diagram->setTitle(
  61. sprintf(
  62. __('Schema of the %s database - Page %s'),
  63. $this->db,
  64. $this->pageNumber
  65. )
  66. );
  67. $this->diagram->SetAuthor('phpMyAdmin ' . PMA_VERSION);
  68. $this->diagram->setFont('Arial');
  69. $this->diagram->setFontSize(16);
  70. $alltables = $this->getTablesFromRequest();
  71. foreach ($alltables as $table) {
  72. if (! isset($this->tables[$table])) {
  73. $this->tables[$table] = new TableStatsSvg(
  74. $this->diagram,
  75. $this->db,
  76. $table,
  77. $this->diagram->getFont(),
  78. $this->diagram->getFontSize(),
  79. $this->pageNumber,
  80. $this->tablewidth,
  81. $this->showKeys,
  82. $this->tableDimension,
  83. $this->offline
  84. );
  85. }
  86. if ($this->sameWide) {
  87. $this->tables[$table]->width = &$this->tablewidth;
  88. }
  89. $this->setMinMax($this->tables[$table]);
  90. }
  91. $border = 15;
  92. $this->diagram->startSvgDoc(
  93. $this->xMax + $border,
  94. $this->yMax + $border,
  95. $this->xMin - $border,
  96. $this->yMin - $border
  97. );
  98. $seen_a_relation = false;
  99. foreach ($alltables as $one_table) {
  100. $exist_rel = $this->relation->getForeigners($this->db, $one_table, '', 'both');
  101. if (! $exist_rel) {
  102. continue;
  103. }
  104. $seen_a_relation = true;
  105. foreach ($exist_rel as $master_field => $rel) {
  106. /* put the foreign table on the schema only if selected
  107. * by the user
  108. * (do not use array_search() because we would have to
  109. * to do a === false and this is not PHP3 compatible)
  110. */
  111. if ($master_field !== 'foreign_keys_data') {
  112. if (in_array($rel['foreign_table'], $alltables)) {
  113. $this->addRelation(
  114. $one_table,
  115. $this->diagram->getFont(),
  116. $this->diagram->getFontSize(),
  117. $master_field,
  118. $rel['foreign_table'],
  119. $rel['foreign_field'],
  120. $this->tableDimension
  121. );
  122. }
  123. continue;
  124. }
  125. foreach ($rel as $one_key) {
  126. if (! in_array($one_key['ref_table_name'], $alltables)) {
  127. continue;
  128. }
  129. foreach ($one_key['index_list'] as $index => $one_field) {
  130. $this->addRelation(
  131. $one_table,
  132. $this->diagram->getFont(),
  133. $this->diagram->getFontSize(),
  134. $one_field,
  135. $one_key['ref_table_name'],
  136. $one_key['ref_index_list'][$index],
  137. $this->tableDimension
  138. );
  139. }
  140. }
  141. }
  142. }
  143. if ($seen_a_relation) {
  144. $this->drawRelations();
  145. }
  146. $this->drawTables();
  147. $this->diagram->endSvgDoc();
  148. }
  149. /**
  150. * Output RelationStatsSvg Document for download
  151. *
  152. * @return void
  153. */
  154. public function showOutput()
  155. {
  156. $this->diagram->showOutput($this->getFileName('.svg'));
  157. }
  158. /**
  159. * Sets X and Y minimum and maximum for a table cell
  160. *
  161. * @param TableStatsSvg $table The table
  162. *
  163. * @return void
  164. */
  165. private function setMinMax($table)
  166. {
  167. $this->xMax = max($this->xMax, $table->x + $table->width);
  168. $this->yMax = max($this->yMax, $table->y + $table->height);
  169. $this->xMin = min($this->xMin, $table->x);
  170. $this->yMin = min($this->yMin, $table->y);
  171. }
  172. /**
  173. * Defines relation objects
  174. *
  175. * @see setMinMax,TableStatsSvg::__construct(),
  176. * PhpMyAdmin\Plugins\Schema\Svg\RelationStatsSvg::__construct()
  177. *
  178. * @param string $masterTable The master table name
  179. * @param string $font The font face
  180. * @param int $fontSize Font size
  181. * @param string $masterField The relation field in the master table
  182. * @param string $foreignTable The foreign table name
  183. * @param string $foreignField The relation field in the foreign table
  184. * @param bool $tableDimension Whether to display table position or not
  185. *
  186. * @return void
  187. */
  188. private function addRelation(
  189. $masterTable,
  190. $font,
  191. $fontSize,
  192. $masterField,
  193. $foreignTable,
  194. $foreignField,
  195. $tableDimension
  196. ) {
  197. if (! isset($this->tables[$masterTable])) {
  198. $this->tables[$masterTable] = new TableStatsSvg(
  199. $this->diagram,
  200. $this->db,
  201. $masterTable,
  202. $font,
  203. $fontSize,
  204. $this->pageNumber,
  205. $this->tablewidth,
  206. false,
  207. $tableDimension
  208. );
  209. $this->setMinMax($this->tables[$masterTable]);
  210. }
  211. if (! isset($this->tables[$foreignTable])) {
  212. $this->tables[$foreignTable] = new TableStatsSvg(
  213. $this->diagram,
  214. $this->db,
  215. $foreignTable,
  216. $font,
  217. $fontSize,
  218. $this->pageNumber,
  219. $this->tablewidth,
  220. false,
  221. $tableDimension
  222. );
  223. $this->setMinMax($this->tables[$foreignTable]);
  224. }
  225. $this->relations[] = new RelationStatsSvg(
  226. $this->diagram,
  227. $this->tables[$masterTable],
  228. $masterField,
  229. $this->tables[$foreignTable],
  230. $foreignField
  231. );
  232. }
  233. /**
  234. * Draws relation arrows and lines
  235. * connects master table's master field to
  236. * foreign table's foreign field
  237. *
  238. * @see Relation_Stats_Svg::relationDraw()
  239. *
  240. * @return void
  241. */
  242. private function drawRelations()
  243. {
  244. foreach ($this->relations as $relation) {
  245. $relation->relationDraw($this->showColor);
  246. }
  247. }
  248. /**
  249. * Draws tables
  250. *
  251. * @see TableStatsSvg::Table_Stats_tableDraw()
  252. *
  253. * @return void
  254. */
  255. private function drawTables()
  256. {
  257. foreach ($this->tables as $table) {
  258. $table->tableDraw($this->showColor);
  259. }
  260. }
  261. }