PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/public/phpmyadmin/libraries/plugins/schema/dia/Dia_Relation_Schema.class.php

https://gitlab.com/qbarbosa/klindev
PHP | 381 lines | 218 code | 21 blank | 142 comment | 14 complexity | 53052ecadbc5b4e062f470d9960bc898 MD5 | raw file
  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. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. require_once 'libraries/plugins/schema/Export_Relation_Schema.class.php';
  12. require_once 'libraries/plugins/schema/dia/RelationStatsDia.class.php';
  13. require_once 'libraries/plugins/schema/dia/TableStatsDia.class.php';
  14. /**
  15. * This Class inherits the XMLwriter class and
  16. * helps in developing structure of DIA Schema Export
  17. *
  18. * @package PhpMyAdmin
  19. * @access public
  20. * @see http://php.net/manual/en/book.xmlwriter.php
  21. */
  22. class PMA_DIA extends XMLWriter
  23. {
  24. /**
  25. * The "PMA_DIA" constructor
  26. *
  27. * Upon instantiation This starts writing the Dia XML document
  28. *
  29. * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
  30. */
  31. public function __construct()
  32. {
  33. $this->openMemory();
  34. /*
  35. * Set indenting using three spaces,
  36. * so output is formatted
  37. */
  38. $this->setIndent(true);
  39. $this->setIndentString(' ');
  40. /*
  41. * Create the XML document
  42. */
  43. $this->startDocument('1.0', 'UTF-8');
  44. }
  45. /**
  46. * Starts Dia Document
  47. *
  48. * dia document starts by first initializing dia:diagram tag
  49. * then dia:diagramdata contains all the attributes that needed
  50. * to define the document, then finally a Layer starts which
  51. * holds all the objects.
  52. *
  53. * @param string $paper the size of the paper/document
  54. * @param float $topMargin top margin of the paper/document in cm
  55. * @param float $bottomMargin bottom margin of the paper/document in cm
  56. * @param float $leftMargin left margin of the paper/document in cm
  57. * @param float $rightMargin right margin of the paper/document in cm
  58. * @param string $orientation orientation of the document, portrait or landscape
  59. *
  60. * @return void
  61. *
  62. * @access public
  63. * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),
  64. * XMLWriter::writeRaw()
  65. */
  66. public function startDiaDoc($paper, $topMargin, $bottomMargin, $leftMargin,
  67. $rightMargin, $orientation
  68. ) {
  69. if ($orientation == 'P') {
  70. $isPortrait = 'true';
  71. } else {
  72. $isPortrait = 'false';
  73. }
  74. $this->startElement('dia:diagram');
  75. $this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/');
  76. $this->startElement('dia:diagramdata');
  77. $this->writeRaw(
  78. '<dia:attribute name="background">
  79. <dia:color val="#ffffff"/>
  80. </dia:attribute>
  81. <dia:attribute name="pagebreak">
  82. <dia:color val="#000099"/>
  83. </dia:attribute>
  84. <dia:attribute name="paper">
  85. <dia:composite type="paper">
  86. <dia:attribute name="name">
  87. <dia:string>#' . $paper . '#</dia:string>
  88. </dia:attribute>
  89. <dia:attribute name="tmargin">
  90. <dia:real val="' . $topMargin . '"/>
  91. </dia:attribute>
  92. <dia:attribute name="bmargin">
  93. <dia:real val="' . $bottomMargin . '"/>
  94. </dia:attribute>
  95. <dia:attribute name="lmargin">
  96. <dia:real val="' . $leftMargin . '"/>
  97. </dia:attribute>
  98. <dia:attribute name="rmargin">
  99. <dia:real val="' . $rightMargin . '"/>
  100. </dia:attribute>
  101. <dia:attribute name="is_portrait">
  102. <dia:boolean val="' . $isPortrait . '"/>
  103. </dia:attribute>
  104. <dia:attribute name="scaling">
  105. <dia:real val="1"/>
  106. </dia:attribute>
  107. <dia:attribute name="fitto">
  108. <dia:boolean val="false"/>
  109. </dia:attribute>
  110. </dia:composite>
  111. </dia:attribute>
  112. <dia:attribute name="grid">
  113. <dia:composite type="grid">
  114. <dia:attribute name="width_x">
  115. <dia:real val="1"/>
  116. </dia:attribute>
  117. <dia:attribute name="width_y">
  118. <dia:real val="1"/>
  119. </dia:attribute>
  120. <dia:attribute name="visible_x">
  121. <dia:int val="1"/>
  122. </dia:attribute>
  123. <dia:attribute name="visible_y">
  124. <dia:int val="1"/>
  125. </dia:attribute>
  126. <dia:composite type="color"/>
  127. </dia:composite>
  128. </dia:attribute>
  129. <dia:attribute name="color">
  130. <dia:color val="#d8e5e5"/>
  131. </dia:attribute>
  132. <dia:attribute name="guides">
  133. <dia:composite type="guides">
  134. <dia:attribute name="hguides"/>
  135. <dia:attribute name="vguides"/>
  136. </dia:composite>
  137. </dia:attribute>'
  138. );
  139. $this->endElement();
  140. $this->startElement('dia:layer');
  141. $this->writeAttribute('name', 'Background');
  142. $this->writeAttribute('visible', 'true');
  143. $this->writeAttribute('active', 'true');
  144. }
  145. /**
  146. * Ends Dia Document
  147. *
  148. * @return void
  149. * @access public
  150. * @see XMLWriter::endElement(),XMLWriter::endDocument()
  151. */
  152. public function endDiaDoc()
  153. {
  154. $this->endElement();
  155. $this->endDocument();
  156. }
  157. /**
  158. * Output Dia Document for download
  159. *
  160. * @param string $fileName name of the dia document
  161. *
  162. * @return void
  163. * @access public
  164. * @see XMLWriter::flush()
  165. */
  166. public function showOutput($fileName)
  167. {
  168. if (ob_get_clean()) {
  169. ob_end_clean();
  170. }
  171. $output = $this->flush();
  172. PMA_Response::getInstance()->disable();
  173. PMA_downloadHeader(
  174. $fileName,
  175. 'application/x-dia-diagram',
  176. /*overload*/mb_strlen($output)
  177. );
  178. print $output;
  179. }
  180. }
  181. /**
  182. * Dia Relation Schema Class
  183. *
  184. * Purpose of this class is to generate the Dia XML Document
  185. * which is used for representing the database diagrams in Dia IDE
  186. * This class uses Database Table and Reference Objects of Dia and with
  187. * the combination of these objects actually helps in preparing Dia XML.
  188. *
  189. * Dia XML is generated by using XMLWriter php extension and this class
  190. * inherits Export_Relation_Schema class has common functionality added
  191. * to this class
  192. *
  193. * @package PhpMyAdmin
  194. * @name Dia_Relation_Schema
  195. */
  196. class PMA_Dia_Relation_Schema extends PMA_Export_Relation_Schema
  197. {
  198. /**
  199. * @var Table_Stats_Dia[]|Table_Stats_Eps[]|Table_Stats_Pdf[]|Table_Stats_Svg[]
  200. */
  201. private $_tables = array();
  202. /** @var Relation_Stats_Dia[] Relations */
  203. private $_relations = array();
  204. private $_topMargin = 2.8222000598907471;
  205. private $_bottomMargin = 2.8222000598907471;
  206. private $_leftMargin = 2.8222000598907471;
  207. private $_rightMargin = 2.8222000598907471;
  208. public static $objectId = 0;
  209. /**
  210. * The "PMA_Dia_Relation_Schema" constructor
  211. *
  212. * Upon instantiation This outputs the Dia XML document
  213. * that user can download
  214. *
  215. * @param string $db database name
  216. *
  217. * @see PMA_DIA,Table_Stats_Dia,Relation_Stats_Dia
  218. */
  219. public function __construct($db)
  220. {
  221. parent::__construct($db, new PMA_DIA());
  222. $this->setShowColor(isset($_REQUEST['dia_show_color']));
  223. $this->setShowKeys(isset($_REQUEST['dia_show_keys']));
  224. $this->setOrientation($_REQUEST['dia_orientation']);
  225. $this->setPaper($_REQUEST['dia_paper']);
  226. $this->diagram->startDiaDoc(
  227. $this->paper, $this->_topMargin, $this->_bottomMargin,
  228. $this->_leftMargin, $this->_rightMargin, $this->orientation
  229. );
  230. $alltables = $this->getTablesFromRequest();
  231. foreach ($alltables as $table) {
  232. if (! isset($this->tables[$table])) {
  233. $this->_tables[$table] = new Table_Stats_Dia(
  234. $this->diagram, $this->db, $table, $this->pageNumber,
  235. $this->showKeys, $this->offline
  236. );
  237. }
  238. }
  239. $seen_a_relation = false;
  240. foreach ($alltables as $one_table) {
  241. $exist_rel = PMA_getForeigners($this->db, $one_table, '', 'both');
  242. if (!$exist_rel) {
  243. continue;
  244. }
  245. $seen_a_relation = true;
  246. foreach ($exist_rel as $master_field => $rel) {
  247. /* put the foreign table on the schema only if selected
  248. * by the user
  249. * (do not use array_search() because we would have to
  250. * to do a === false and this is not PHP3 compatible)
  251. */
  252. if ($master_field != 'foreign_keys_data') {
  253. if (in_array($rel['foreign_table'], $alltables)) {
  254. $this->_addRelation(
  255. $one_table, $master_field, $rel['foreign_table'],
  256. $rel['foreign_field'], $this->showKeys
  257. );
  258. }
  259. continue;
  260. }
  261. foreach ($rel as $one_key) {
  262. if (!in_array($one_key['ref_table_name'], $alltables)) {
  263. continue;
  264. }
  265. foreach ($one_key['index_list'] as $index => $one_field) {
  266. $this->_addRelation(
  267. $one_table, $one_field, $one_key['ref_table_name'],
  268. $one_key['ref_index_list'][$index], $this->showKeys
  269. );
  270. }
  271. }
  272. }
  273. }
  274. $this->_drawTables();
  275. if ($seen_a_relation) {
  276. $this->_drawRelations();
  277. }
  278. $this->diagram->endDiaDoc();
  279. }
  280. /**
  281. * Output Dia Document for download
  282. *
  283. * @return void
  284. * @access public
  285. */
  286. public function showOutput()
  287. {
  288. $this->diagram->showOutput($this->getFileName('.dia'));
  289. }
  290. /**
  291. * Defines relation objects
  292. *
  293. * @param string $masterTable The master table name
  294. * @param string $masterField The relation field in the master table
  295. * @param string $foreignTable The foreign table name
  296. * @param string $foreignField The relation field in the foreign table
  297. * @param bool $showKeys Whether to display ONLY keys or not
  298. *
  299. * @return void
  300. *
  301. * @access private
  302. * @see Table_Stats_Dia::__construct(),Relation_Stats_Dia::__construct()
  303. */
  304. private function _addRelation($masterTable, $masterField, $foreignTable,
  305. $foreignField, $showKeys
  306. ) {
  307. if (! isset($this->_tables[$masterTable])) {
  308. $this->_tables[$masterTable] = new Table_Stats_Dia(
  309. $this->diagram, $this->db, $masterTable, $this->pageNumber, $showKeys
  310. );
  311. }
  312. if (! isset($this->_tables[$foreignTable])) {
  313. $this->_tables[$foreignTable] = new Table_Stats_Dia(
  314. $this->diagram, $this->db, $foreignTable, $this->pageNumber, $showKeys
  315. );
  316. }
  317. $this->_relations[] = new Relation_Stats_Dia(
  318. $this->diagram,
  319. $this->_tables[$masterTable],
  320. $masterField,
  321. $this->_tables[$foreignTable],
  322. $foreignField
  323. );
  324. }
  325. /**
  326. * Draws relation references
  327. *
  328. * connects master table's master field to
  329. * foreign table's foreign field using Dia object
  330. * type Database - Reference
  331. *
  332. * @return void
  333. *
  334. * @access private
  335. * @see Relation_Stats_Dia::relationDraw()
  336. */
  337. private function _drawRelations()
  338. {
  339. foreach ($this->_relations as $relation) {
  340. $relation->relationDraw($this->showColor);
  341. }
  342. }
  343. /**
  344. * Draws tables
  345. *
  346. * Tables are generated using Dia object type Database - Table
  347. * primary fields are underlined and bold in tables
  348. *
  349. * @return void
  350. *
  351. * @access private
  352. * @see Table_Stats_Dia::tableDraw()
  353. */
  354. private function _drawTables()
  355. {
  356. foreach ($this->_tables as $table) {
  357. $table->tableDraw($this->showColor);
  358. }
  359. }
  360. }