2 /* vim: set expandtab sw=4 ts=4 sts=4: */
7 if (! defined('PHPMYADMIN')) {
11 require_once 'Export_Relation_Schema.class.php';
14 * This Class inherits the XMLwriter class and
15 * helps in developing structure of DIA Schema Export
18 * @see http://php.net/manual/en/book.xmlwriter.php
20 class PMA_DIA
extends XMLWriter
28 * The "PMA_DIA" constructor
30 * Upon instantiation This starts writing the Dia XML document
33 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
35 function __construct()
39 * Set indenting using three spaces,
40 * so output is formatted
43 $this->setIndent(true);
44 $this->setIndentString(' ');
46 * Create the XML document
49 $this->startDocument('1.0', 'UTF-8');
55 * dia document starts by first initializing dia:diagram tag
56 * then dia:diagramdata contains all the attributes that needed
57 * to define the document, then finally a Layer starts which
58 * holds all the objects.
60 * @param string $paper the size of the paper/document
61 * @param float $topMargin top margin of the paper/document in cm
62 * @param float $bottomMargin bottom margin of the paper/document in cm
63 * @param float $leftMargin left margin of the paper/document in cm
64 * @param float $rightMargin right margin of the paper/document in cm
65 * @param string $portrait document will be portrait or landscape
70 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),
71 * XMLWriter::writeRaw()
73 function startDiaDoc($paper, $topMargin, $bottomMargin, $leftMargin,
74 $rightMargin, $portrait
76 if ($portrait == 'P') {
81 $this->startElement('dia:diagram');
82 $this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/');
83 $this->startElement('dia:diagramdata');
85 '<dia:attribute name="background">
86 <dia:color val="#ffffff"/>
88 <dia:attribute name="pagebreak">
89 <dia:color val="#000099"/>
91 <dia:attribute name="paper">
92 <dia:composite type="paper">
93 <dia:attribute name="name">
94 <dia:string>#' . $paper . '#</dia:string>
96 <dia:attribute name="tmargin">
97 <dia:real val="' . $topMargin . '"/>
99 <dia:attribute name="bmargin">
100 <dia:real val="' . $bottomMargin . '"/>
102 <dia:attribute name="lmargin">
103 <dia:real val="' . $leftMargin . '"/>
105 <dia:attribute name="rmargin">
106 <dia:real val="' . $rightMargin . '"/>
108 <dia:attribute name="is_portrait">
109 <dia:boolean val="' . $isPortrait . '"/>
111 <dia:attribute name="scaling">
114 <dia:attribute name="fitto">
115 <dia:boolean val="false"/>
119 <dia:attribute name="grid">
120 <dia:composite type="grid">
121 <dia:attribute name="width_x">
124 <dia:attribute name="width_y">
127 <dia:attribute name="visible_x">
130 <dia:attribute name="visible_y">
133 <dia:composite type="color"/>
136 <dia:attribute name="color">
137 <dia:color val="#d8e5e5"/>
139 <dia:attribute name="guides">
140 <dia:composite type="guides">
141 <dia:attribute name="hguides"/>
142 <dia:attribute name="vguides"/>
147 $this->startElement('dia:layer');
148 $this->writeAttribute('name', 'Background');
149 $this->writeAttribute('visible', 'true');
150 $this->writeAttribute('active', 'true');
158 * @see XMLWriter::endElement(),XMLWriter::endDocument()
163 $this->endDocument();
167 * Output Dia Document for download
169 * @param string $fileName name of the dia document
173 * @see XMLWriter::flush()
175 function showOutput($fileName)
177 if (ob_get_clean()) {
180 $output = $this->flush();
181 PMA_Response
::getInstance()->disable();
183 $fileName . '.dia', 'application/x-dia-diagram', strlen($output)
190 * Table preferences/statistics
192 * This class preserves the table co-ordinates,fields
193 * and helps in drawing/generating the Tables in dia XML document.
204 public $fields = array();
206 public $primary = array();
211 * The "Table_Stats" constructor
213 * @param string $tableName The table name
214 * @param integer $pageNumber The current page number (from the
215 * $cfg['Servers'][$i]['table_coords'] table)
216 * @param boolean $showKeys Whether to display ONLY keys or not
220 * @global object The current dia document
221 * @global array The relations settings
222 * @global string The current db name
226 function __construct($tableName, $pageNumber, $showKeys = false)
228 global $dia, $cfgRelation, $db;
230 $this->tableName
= $tableName;
231 $sql = 'DESCRIBE ' . PMA_Util
::backquote($tableName);
232 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
233 if (!$result ||
!PMA_DBI_num_rows($result)) {
236 sprintf(__('The %s table doesn\'t exist!'), $tableName)
241 * check to see if it will load all fields or only the foreign keys
244 $indexes = PMA_Index
::getFromTable($this->tableName
, $db);
245 $all_columns = array();
246 foreach ($indexes as $index) {
247 $all_columns = array_merge(
249 array_flip(array_keys($index->getColumns()))
252 $this->fields
= array_keys($all_columns);
254 while ($row = PMA_DBI_fetch_row($result)) {
255 $this->fields
[] = $row[0];
259 $sql = 'SELECT x, y FROM '
260 . PMA_Util
::backquote($GLOBALS['cfgRelation']['db']) . '.'
261 . PMA_Util
::backquote($cfgRelation['table_coords'])
262 . ' WHERE db_name = \'' . PMA_Util
::sqlAddSlashes($db) . '\''
263 . ' AND table_name = \''
264 . PMA_Util
::sqlAddSlashes($tableName) . '\''
265 . ' AND pdf_page_number = ' . $pageNumber;
266 $result = PMA_queryAsControlUser($sql, false, PMA_DBI_QUERY_STORE
);
267 if (! $result ||
! PMA_DBI_num_rows($result)) {
272 __('Please configure the coordinates for table %s'),
277 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
278 $this->x
= (double) $this->x
;
279 $this->y
= (double) $this->y
;
283 $this->displayfield
= PMA_getDisplayField($db, $tableName);
287 $result = PMA_DBI_query(
288 'SHOW INDEX FROM ' . PMA_Util
::backquote($tableName) . ';',
292 if (PMA_DBI_num_rows($result) > 0) {
293 while ($row = PMA_DBI_fetch_assoc($result)) {
294 if ($row['Key_name'] == 'PRIMARY') {
295 $this->primary
[] = $row['Column_name'];
300 * Every object in Dia document needs an ID to identify
301 * so, we used a static variable to keep the things unique
303 PMA_Dia_Relation_Schema
::$objectId +
= 1;
304 $this->tableId
= PMA_Dia_Relation_Schema
::$objectId;
310 * Tables are generated using object type Database - Table
311 * primary fields are underlined in tables. Dia object
312 * is used to generate the XML of Dia Document. Database Table
313 * Object and their attributes are involved in the combination
314 * of displaing Database - Table on Dia Document.
316 * @param boolean $changeColor Whether to show color for tables text or not
317 * if changeColor is true then an array of $listOfColors will be used to choose
318 * the random colors for tables text we can change/add more colors to this array
322 * @global object The current Dia document
327 public function tableDraw($changeColor)
332 $listOfColors = array(
337 shuffle($listOfColors);
338 $this->tableColor
= '#' . $listOfColors[0] . '';
340 $this->tableColor
= '#000000';
345 $dia->startElement('dia:object');
346 $dia->writeAttribute('type', 'Database - Table');
347 $dia->writeAttribute('version', '0');
348 $dia->writeAttribute('id', '' . $this->tableId
. '');
350 '<dia:attribute name="obj_pos">
352 . ($this->x
* $factor) . ',' . ($this->y
* $factor) . '"/>
354 <dia:attribute name="obj_bb">
355 <dia:rectangle val="'
356 .($this->x
* $factor) . ',' . ($this->y
* $factor) . ';9.97,9.2"/>
358 <dia:attribute name="meta">
359 <dia:composite type="dict"/>
361 <dia:attribute name="elem_corner">
363 . ($this->x
* $factor) . ',' . ($this->y
* $factor) . '"/>
365 <dia:attribute name="elem_width">
366 <dia:real val="5.9199999999999999"/>
368 <dia:attribute name="elem_height">
369 <dia:real val="3.5"/>
371 <dia:attribute name="text_colour">
372 <dia:color val="' . $this->tableColor
. '"/>
374 <dia:attribute name="line_colour">
375 <dia:color val="#000000"/>
377 <dia:attribute name="fill_colour">
378 <dia:color val="#ffffff"/>
380 <dia:attribute name="line_width">
381 <dia:real val="0.10000000000000001"/>
383 <dia:attribute name="name">
384 <dia:string>#' . $this->tableName
. '#</dia:string>
386 <dia:attribute name="comment">
387 <dia:string>##</dia:string>
389 <dia:attribute name="visible_comment">
390 <dia:boolean val="false"/>
392 <dia:attribute name="tagging_comment">
393 <dia:boolean val="false"/>
395 <dia:attribute name="underline_primary_key">
396 <dia:boolean val="true"/>
398 <dia:attribute name="bold_primary_keys">
399 <dia:boolean val="true"/>
401 <dia:attribute name="normal_font">
402 <dia:font family="monospace" style="0" name="Courier"/>
404 <dia:attribute name="name_font">
405 <dia:font family="sans" style="80" name="Helvetica-Bold"/>
407 <dia:attribute name="comment_font">
408 <dia:font family="sans" style="0" name="Helvetica"/>
410 <dia:attribute name="normal_font_height">
411 <dia:real val="0.80000000000000004"/>
413 <dia:attribute name="name_font_height">
414 <dia:real val="0.69999999999999996"/>
416 <dia:attribute name="comment_font_height">
417 <dia:real val="0.69999999999999996"/>
421 $dia->startElement('dia:attribute');
422 $dia->writeAttribute('name', 'attributes');
424 foreach ($this->fields
as $field) {
426 '<dia:composite type="table_attribute">
427 <dia:attribute name="name">
428 <dia:string>#' . $field . '#</dia:string>
430 <dia:attribute name="type">
431 <dia:string>##</dia:string>
433 <dia:attribute name="comment">
434 <dia:string>##</dia:string>
439 if (in_array($field, $this->primary
)) {
442 if ($field == $this->displayfield
) {
446 '<dia:attribute name="primary_key">
447 <dia:boolean val="' . $pm . '"/>
449 <dia:attribute name="nullable">
450 <dia:boolean val="false"/>
452 <dia:attribute name="unique">
453 <dia:boolean val="' . $pm . '"/>
464 * Relation preferences/statistics
466 * This class fetches the table master and foreign fields positions
467 * and helps in generating the Table references and then connects
468 * master table's master field to foreign table's foreign key
469 * in dia XML document.
471 * @name Relation_Stats
479 public $srcConnPointsRight;
480 public $srcConnPointsLeft;
481 public $destConnPointsRight;
482 public $destConnPointsLeft;
483 public $masterTableId;
484 public $foreignTableId;
485 public $masterTablePos;
486 public $foreignTablePos;
487 public $referenceColor;
490 * The "Relation_Stats" constructor
492 * @param string $master_table The master table name
493 * @param string $master_field The relation field in the master table
494 * @param string $foreign_table The foreign table name
495 * @param string $foreign_field The relation field in the foreign table
499 * @see Relation_Stats::_getXy
501 function __construct($master_table, $master_field, $foreign_table,
504 $src_pos = $this->_getXy($master_table, $master_field);
505 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
506 $this->srcConnPointsLeft
= $src_pos[0];
507 $this->srcConnPointsRight
= $src_pos[1];
508 $this->destConnPointsLeft
= $dest_pos[0];
509 $this->destConnPointsRight
= $dest_pos[1];
510 $this->masterTablePos
= $src_pos[2];
511 $this->foreignTablePos
= $dest_pos[2];
512 $this->masterTableId
= $master_table->tableId
;
513 $this->foreignTableId
= $foreign_table->tableId
;
517 * Each Table object have connection points
518 * which is used to connect to other objects in Dia
519 * we detect the position of key in fields and
520 * then determines its left and right connection
523 * @param string $table The current table name
524 * @param string $column The relation column name
526 * @return array Table right,left connection points and key position
530 private function _getXy($table, $column)
532 $pos = array_search($column, $table->fields
);
533 // left, right, position
536 return array($pos +
$value +
$pos, $pos +
$value +
$pos +
1, $pos);
538 return array($pos +
$value , $pos +
$value +
1, $pos);
542 * Draws relation references
544 * connects master table's master field to foreign table's
545 * forein field using Dia object type Database - Reference
546 * Dia object is used to generate the XML of Dia Document.
547 * Database reference Object and their attributes are involved
548 * in the combination of displaing Database - reference on Dia Document.
550 * @param boolean $changeColor Whether to use one color per relation or not
551 * if changeColor is true then an array of $listOfColors will be used to choose
552 * the random colors for references lines. we can change/add more colors to this
556 * @global object The current Dia document
561 public function relationDraw($changeColor)
565 PMA_Dia_Relation_Schema
::$objectId +
= 1;
567 * if source connection points and destination connection
568 * points are same then return it false and don't draw that
571 if ( $this->srcConnPointsRight
== $this->destConnPointsRight
) {
572 if ( $this->srcConnPointsLeft
== $this->destConnPointsLeft
) {
578 $listOfColors = array(
583 shuffle($listOfColors);
584 $this->referenceColor
= '#' . $listOfColors[0] . '';
586 $this->referenceColor
= '#000000';
590 '<dia:object type="Database - Reference" version="0" id="'
591 . PMA_Dia_Relation_Schema
::$objectId . '">
592 <dia:attribute name="obj_pos">
593 <dia:point val="3.27,18.9198"/>
595 <dia:attribute name="obj_bb">
596 <dia:rectangle val="2.27,8.7175;17.7679,18.9198"/>
598 <dia:attribute name="meta">
599 <dia:composite type="dict"/>
601 <dia:attribute name="orth_points">
602 <dia:point val="3.27,18.9198"/>
603 <dia:point val="2.27,18.9198"/>
604 <dia:point val="2.27,14.1286"/>
605 <dia:point val="17.7679,14.1286"/>
606 <dia:point val="17.7679,9.3375"/>
607 <dia:point val="16.7679,9.3375"/>
609 <dia:attribute name="orth_orient">
616 <dia:attribute name="orth_autoroute">
617 <dia:boolean val="true"/>
619 <dia:attribute name="text_colour">
620 <dia:color val="#000000"/>
622 <dia:attribute name="line_colour">
623 <dia:color val="' . $this->referenceColor
. '"/>
625 <dia:attribute name="line_width">
626 <dia:real val="0.10000000000000001"/>
628 <dia:attribute name="line_style">
632 <dia:attribute name="corner_radius">
635 <dia:attribute name="end_arrow">
638 <dia:attribute name="end_arrow_length">
639 <dia:real val="0.5"/>
641 <dia:attribute name="end_arrow_width">
642 <dia:real val="0.5"/>
644 <dia:attribute name="start_point_desc">
645 <dia:string>#1#</dia:string>
647 <dia:attribute name="end_point_desc">
648 <dia:string>#n#</dia:string>
650 <dia:attribute name="normal_font">
651 <dia:font family="monospace" style="0" name="Courier"/>
653 <dia:attribute name="normal_font_height">
654 <dia:real val="0.59999999999999998"/>
657 <dia:connection handle="0" to="'
658 . $this->masterTableId
. '" connection="'
659 . $this->srcConnPointsRight
. '"/>
660 <dia:connection handle="1" to="'
661 . $this->foreignTableId
. '" connection="'
662 . $this->destConnPointsRight
. '"/>
670 * Dia Relation Schema Class
672 * Purpose of this class is to generate the Dia XML Document
673 * which is used for representing the database diagrams in Dia IDE
674 * This class uses Database Table and Reference Objects of Dia and with
675 * the combination of these objects actually helps in preparing Dia XML.
677 * Dia XML is generated by using XMLWriter php extension and this class
678 * inherits Export_Relation_Schema class has common functionality added
681 * @name Dia_Relation_Schema
683 class PMA_Dia_Relation_Schema
extends PMA_Export_Relation_Schema
688 private $_tables = array();
689 private $_relations = array();
690 private $_topMargin = 2.8222000598907471;
691 private $_bottomMargin = 2.8222000598907471;
692 private $_leftMargin = 2.8222000598907471;
693 private $_rightMargin = 2.8222000598907471;
694 public static $objectId = 0;
697 * The "PMA_Dia_Relation_Schema" constructor
699 * Upon instantiation This outputs the Dia XML document
700 * that user can download
703 * @see PMA_DIA,Table_Stats,Relation_Stats
705 function __construct()
709 $this->setPageNumber($_POST['pdf_page_number']);
710 $this->setShowGrid(isset($_POST['show_grid']));
711 $this->setShowColor($_POST['show_color']);
712 $this->setShowKeys(isset($_POST['show_keys']));
713 $this->setOrientation(isset($_POST['orientation']));
714 $this->setPaper($_POST['paper']);
715 $this->setExportType($_POST['export_type']);
717 $dia = new PMA_DIA();
719 $this->paper
, $this->_topMargin
, $this->_bottomMargin
,
720 $this->_leftMargin
, $this->_rightMargin
, $this->orientation
722 $alltables = $this->getAllTables($db, $this->pageNumber
);
723 foreach ($alltables as $table) {
724 if (! isset($this->tables
[$table])) {
725 $this->_tables
[$table] = new Table_Stats(
726 $table, $this->pageNumber
, $this->showKeys
731 $seen_a_relation = false;
732 foreach ($alltables as $one_table) {
733 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
735 $seen_a_relation = true;
736 foreach ($exist_rel as $master_field => $rel) {
737 /* put the foreign table on the schema only if selected
739 * (do not use array_search() because we would have to
740 * to do a === false and this is not PHP3 compatible)
742 if (in_array($rel['foreign_table'], $alltables)) {
744 $one_table, $master_field, $rel['foreign_table'],
745 $rel['foreign_field'], $this->showKeys
751 $this->_drawTables($this->showColor
);
753 if ($seen_a_relation) {
754 $this->_drawRelations($this->showColor
);
757 $dia->showOutput($db . '-' . $this->pageNumber
);
762 * Defines relation objects
764 * @param string $masterTable The master table name
765 * @param string $masterField The relation field in the master table
766 * @param string $foreignTable The foreign table name
767 * @param string $foreignField The relation field in the foreign table
768 * @param bool $showKeys Whether to display ONLY keys or not
773 * @see Table_Stats::__construct(),Relation_Stats::__construct()
775 private function _addRelation($masterTable, $masterField, $foreignTable,
776 $foreignField, $showKeys
778 if (! isset($this->_tables
[$masterTable])) {
779 $this->_tables
[$masterTable] = new Table_Stats(
780 $masterTable, $this->pageNumber
, $showKeys
783 if (! isset($this->_tables
[$foreignTable])) {
784 $this->_tables
[$foreignTable] = new Table_Stats(
785 $foreignTable, $this->pageNumber
, $showKeys
788 $this->_relations
[] = new Relation_Stats(
789 $this->_tables
[$masterTable], $masterField,
790 $this->_tables
[$foreignTable], $foreignField
795 * Draws relation references
797 * connects master table's master field to
798 * foreign table's forein field using Dia object
799 * type Database - Reference
801 * @param boolean $changeColor Whether to use one color per relation or not
806 * @see Relation_Stats::relationDraw()
808 private function _drawRelations($changeColor)
810 foreach ($this->_relations
as $relation) {
811 $relation->relationDraw($changeColor);
818 * Tables are generated using Dia object type Database - Table
819 * primary fields are underlined and bold in tables
821 * @param boolean $changeColor Whether to show color for tables text or not
826 * @see Table_Stats::tableDraw()
828 private function _drawTables($changeColor)
830 foreach ($this->_tables
as $table) {
831 $table->tableDraw($changeColor);