SvgRelationSchema.php 8.8 KB

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