DiaRelationSchema.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Classes to create relation schema in Dia format.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin\Plugins\Schema\Dia;
  10. use PhpMyAdmin\Plugins\Schema\Dia\TableStatsDia;
  11. use PhpMyAdmin\Plugins\Schema\Eps\TableStatsEps;
  12. use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
  13. use PhpMyAdmin\Plugins\Schema\Pdf\TableStatsPdf;
  14. use PhpMyAdmin\Plugins\Schema\Svg\TableStatsSvg;
  15. use PhpMyAdmin\Relation;
  16. /**
  17. * Dia Relation Schema Class
  18. *
  19. * Purpose of this class is to generate the Dia XML Document
  20. * which is used for representing the database diagrams in Dia IDE
  21. * This class uses Database Table and Reference Objects of Dia and with
  22. * the combination of these objects actually helps in preparing Dia XML.
  23. *
  24. * Dia XML is generated by using XMLWriter php extension and this class
  25. * inherits ExportRelationSchema class has common functionality added
  26. * to this class
  27. *
  28. * @package PhpMyAdmin
  29. * @name Dia_Relation_Schema
  30. */
  31. class DiaRelationSchema extends ExportRelationSchema
  32. {
  33. /**
  34. * @var TableStatsDia[]|TableStatsEps[]|TableStatsPdf[]|TableStatsSvg[]
  35. */
  36. private $_tables = [];
  37. /** @var RelationStatsDia[] Relations */
  38. private $_relations = [];
  39. private $_topMargin = 2.8222000598907471;
  40. private $_bottomMargin = 2.8222000598907471;
  41. private $_leftMargin = 2.8222000598907471;
  42. private $_rightMargin = 2.8222000598907471;
  43. public static $objectId = 0;
  44. /**
  45. * The "PhpMyAdmin\Plugins\Schema\Dia\DiaRelationSchema" constructor
  46. *
  47. * Upon instantiation This outputs the Dia XML document
  48. * that user can download
  49. *
  50. * @param string $db database name
  51. *
  52. * @see Dia,TableStatsDia,RelationStatsDia
  53. */
  54. public function __construct($db)
  55. {
  56. parent::__construct($db, new Dia());
  57. $this->setShowColor(isset($_REQUEST['dia_show_color']));
  58. $this->setShowKeys(isset($_REQUEST['dia_show_keys']));
  59. $this->setOrientation($_REQUEST['dia_orientation']);
  60. $this->setPaper($_REQUEST['dia_paper']);
  61. $this->diagram->startDiaDoc(
  62. $this->paper,
  63. $this->_topMargin,
  64. $this->_bottomMargin,
  65. $this->_leftMargin,
  66. $this->_rightMargin,
  67. $this->orientation
  68. );
  69. $alltables = $this->getTablesFromRequest();
  70. foreach ($alltables as $table) {
  71. if (! isset($this->_tables[$table])) {
  72. $this->_tables[$table] = new TableStatsDia(
  73. $this->diagram,
  74. $this->db,
  75. $table,
  76. $this->pageNumber,
  77. $this->showKeys,
  78. $this->offline
  79. );
  80. }
  81. }
  82. $seen_a_relation = false;
  83. foreach ($alltables as $one_table) {
  84. $exist_rel = $this->relation->getForeigners($this->db, $one_table, '', 'both');
  85. if (! $exist_rel) {
  86. continue;
  87. }
  88. $seen_a_relation = true;
  89. foreach ($exist_rel as $master_field => $rel) {
  90. /* put the foreign table on the schema only if selected
  91. * by the user
  92. * (do not use array_search() because we would have to
  93. * to do a === false and this is not PHP3 compatible)
  94. */
  95. if ($master_field != 'foreign_keys_data') {
  96. if (in_array($rel['foreign_table'], $alltables)) {
  97. $this->_addRelation(
  98. $one_table,
  99. $master_field,
  100. $rel['foreign_table'],
  101. $rel['foreign_field'],
  102. $this->showKeys
  103. );
  104. }
  105. continue;
  106. }
  107. foreach ($rel as $one_key) {
  108. if (! in_array($one_key['ref_table_name'], $alltables)) {
  109. continue;
  110. }
  111. foreach ($one_key['index_list'] as $index => $one_field) {
  112. $this->_addRelation(
  113. $one_table,
  114. $one_field,
  115. $one_key['ref_table_name'],
  116. $one_key['ref_index_list'][$index],
  117. $this->showKeys
  118. );
  119. }
  120. }
  121. }
  122. }
  123. $this->_drawTables();
  124. if ($seen_a_relation) {
  125. $this->_drawRelations();
  126. }
  127. $this->diagram->endDiaDoc();
  128. }
  129. /**
  130. * Output Dia Document for download
  131. *
  132. * @return void
  133. * @access public
  134. */
  135. public function showOutput()
  136. {
  137. $this->diagram->showOutput($this->getFileName('.dia'));
  138. }
  139. /**
  140. * Defines relation objects
  141. *
  142. * @param string $masterTable The master table name
  143. * @param string $masterField The relation field in the master table
  144. * @param string $foreignTable The foreign table name
  145. * @param string $foreignField The relation field in the foreign table
  146. * @param bool $showKeys Whether to display ONLY keys or not
  147. *
  148. * @return void
  149. *
  150. * @access private
  151. * @see TableStatsDia::__construct(),RelationStatsDia::__construct()
  152. */
  153. private function _addRelation(
  154. $masterTable,
  155. $masterField,
  156. $foreignTable,
  157. $foreignField,
  158. $showKeys
  159. ) {
  160. if (! isset($this->_tables[$masterTable])) {
  161. $this->_tables[$masterTable] = new TableStatsDia(
  162. $this->diagram,
  163. $this->db,
  164. $masterTable,
  165. $this->pageNumber,
  166. $showKeys
  167. );
  168. }
  169. if (! isset($this->_tables[$foreignTable])) {
  170. $this->_tables[$foreignTable] = new TableStatsDia(
  171. $this->diagram,
  172. $this->db,
  173. $foreignTable,
  174. $this->pageNumber,
  175. $showKeys
  176. );
  177. }
  178. $this->_relations[] = new RelationStatsDia(
  179. $this->diagram,
  180. $this->_tables[$masterTable],
  181. $masterField,
  182. $this->_tables[$foreignTable],
  183. $foreignField
  184. );
  185. }
  186. /**
  187. * Draws relation references
  188. *
  189. * connects master table's master field to
  190. * foreign table's foreign field using Dia object
  191. * type Database - Reference
  192. *
  193. * @return void
  194. *
  195. * @access private
  196. * @see RelationStatsDia::relationDraw()
  197. */
  198. private function _drawRelations()
  199. {
  200. foreach ($this->_relations as $relation) {
  201. $relation->relationDraw($this->showColor);
  202. }
  203. }
  204. /**
  205. * Draws tables
  206. *
  207. * Tables are generated using Dia object type Database - Table
  208. * primary fields are underlined and bold in tables
  209. *
  210. * @return void
  211. *
  212. * @access private
  213. * @see TableStatsDia::tableDraw()
  214. */
  215. private function _drawTables()
  216. {
  217. foreach ($this->_tables as $table) {
  218. $table->tableDraw($this->showColor);
  219. }
  220. }
  221. }