DiaRelationSchema.php 6.9 KB

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