2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 include_once("Export_Relation_Schema.class.php");
11 * This Class inherits the XMLwriter class and
12 * helps in developing structure of DIA Schema Export
18 * @see http://php.net/manual/en/book.xmlwriter.php
21 class PMA_DIA
extends XMLWriter
29 * The "PMA_DIA" constructor
31 * Upon instantiation This starts writing the Dia XML document
34 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
36 function __construct()
40 * Set indenting using three spaces,
41 * so output is formatted
44 $this->setIndent(TRUE);
45 $this->setIndentString(' ');
47 * Create the XML document
50 $this->startDocument('1.0','UTF-8');
56 * dia document starts by first initializing dia:diagram tag
57 * then dia:diagramdata contains all the attributes that needed
58 * to define the document, then finally a Layer starts which
59 * holds all the objects.
61 * @param string paper The size of the paper/document
62 * @param float topMargin top margin of the paper/document in cm
63 * @param float bottomMargin bottom margin of the paper/document in cm
64 * @param float leftMargin left margin of the paper/document in cm
65 * @param float rightMargin right margin of the paper/document in cm
66 * @param string portrait document will be portrait or landscape
69 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),XMLWriter::writeRaw()
71 function startDiaDoc($paper,$topMargin,$bottomMargin,$leftMargin,$rightMargin,$portrait)
78 $this->startElement('dia:diagram');
79 $this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/');
80 $this->startElement('dia:diagramdata');
82 '<dia:attribute name="background">
83 <dia:color val="#ffffff"/>
85 <dia:attribute name="pagebreak">
86 <dia:color val="#000099"/>
88 <dia:attribute name="paper">
89 <dia:composite type="paper">
90 <dia:attribute name="name">
91 <dia:string>#'.$paper.'#</dia:string>
93 <dia:attribute name="tmargin">
94 <dia:real val="'.$topMargin.'"/>
96 <dia:attribute name="bmargin">
97 <dia:real val="'.$bottomMargin.'"/>
99 <dia:attribute name="lmargin">
100 <dia:real val="'.$leftMargin.'"/>
102 <dia:attribute name="rmargin">
103 <dia:real val="'.$rightMargin.'"/>
105 <dia:attribute name="is_portrait">
106 <dia:boolean val="'.$isPortrait.'"/>
108 <dia:attribute name="scaling">
111 <dia:attribute name="fitto">
112 <dia:boolean val="false"/>
116 <dia:attribute name="grid">
117 <dia:composite type="grid">
118 <dia:attribute name="width_x">
121 <dia:attribute name="width_y">
124 <dia:attribute name="visible_x">
127 <dia:attribute name="visible_y">
130 <dia:composite type="color"/>
133 <dia:attribute name="color">
134 <dia:color val="#d8e5e5"/>
136 <dia:attribute name="guides">
137 <dia:composite type="guides">
138 <dia:attribute name="hguides"/>
139 <dia:attribute name="vguides"/>
143 $this->startElement('dia:layer');
144 $this->writeAttribute('name', 'Background');
145 $this->writeAttribute('visible', 'true');
146 $this->writeAttribute('active', 'true');
155 * @see XMLWriter::endElement(),XMLWriter::endDocument()
160 $this->endDocument();
164 * Output Dia Document for download
166 * @param string fileName name of the dia document
169 * @see XMLWriter::flush()
171 function showOutput($fileName)
176 header('Content-type: application/x-dia-diagram');
177 header('Content-Disposition: attachment; filename="'.$fileName.'.dia"');
178 $output = $this->flush();
184 * Table preferences/statistics
186 * This class preserves the table co-ordinates,fields
187 * and helps in drawing/generating the Tables in dia XML document.
200 public $fields = array();
202 public $primary = array();
207 * The "Table_Stats" constructor
209 * @param string table_name The table name
210 * @param integer pageNumber The current page number (from the
211 * $cfg['Servers'][$i]['table_coords'] table)
212 * @param boolean showKeys Whether to display ONLY keys or not
214 * @global object The current dia document
215 * @global array The relations settings
216 * @global string The current db name
219 function __construct($tableName, $pageNumber, $showKeys = false)
221 global $dia, $cfgRelation, $db;
223 $this->tableName
= $tableName;
224 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
225 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
226 if (!$result ||
!PMA_DBI_num_rows($result)) {
227 $dia->dieSchema($pageNumber,"DIA",sprintf(__('The %s table doesn\'t exist!'), $tableName));
231 * check to see if it will load all fields or only the foreign keys
234 $indexes = PMA_Index
::getFromTable($this->tableName
, $db);
235 $all_columns = array();
236 foreach ($indexes as $index) {
237 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
239 $this->fields
= array_keys($all_columns);
241 while ($row = PMA_DBI_fetch_row($result)) {
242 $this->fields
[] = $row[0];
246 $sql = 'SELECT x, y FROM '
247 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
248 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
249 . ' AND table_name = \'' . PMA_sqlAddslashes($tableName) . '\''
250 . ' AND pdf_page_number = ' . $pageNumber;
251 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
252 if (!$result ||
!PMA_DBI_num_rows($result)) {
253 $dia->dieSchema($pageNumber,"DIA",sprintf(__('Please configure the coordinates for table %s'), $tableName));
255 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
256 $this->x
= (double) $this->x
;
257 $this->y
= (double) $this->y
;
261 $this->displayfield
= PMA_getDisplayField($db, $tableName);
265 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE
);
266 if (PMA_DBI_num_rows($result) > 0) {
267 while ($row = PMA_DBI_fetch_assoc($result)) {
268 if ($row['Key_name'] == 'PRIMARY') {
269 $this->primary
[] = $row['Column_name'];
274 * Every object in Dia document needs an ID to identify
275 * so, we used a static variable to keep the things unique
277 PMA_Dia_Relation_Schema
::$objectId +
= 1;
278 $this->tableId
= PMA_Dia_Relation_Schema
::$objectId;
284 * Tables are generated using object type Database - Table
285 * primary fields are underlined in tables. Dia object
286 * is used to generate the XML of Dia Document. Database Table
287 * Object and their attributes are involved in the combination
288 * of displaing Database - Table on Dia Document.
290 * @param boolean changeColor Whether to show color for tables text or not
291 if changeColor is true then an array of $listOfColors
292 will be used to choose the random colors for tables text
293 we can change/add more colors to this array
295 * @global object The current Dia document
299 public function tableDraw($changeColor)
304 $listOfColors = array(
309 shuffle($listOfColors);
310 $this->tableColor
= '#'.$listOfColors[0].'';
312 $this->tableColor
= '#000000';
317 $dia->startElement('dia:object');
318 $dia->writeAttribute('type', 'Database - Table');
319 $dia->writeAttribute('version', '0');
320 $dia->writeAttribute('id', ''.$this->tableId
.'');
322 '<dia:attribute name="obj_pos">
323 <dia:point val="'.($this->x
* $factor).','.($this->y
* $factor).'"/>
325 <dia:attribute name="obj_bb">
326 <dia:rectangle val="'.($this->x
* $factor).','.($this->y
* $factor).';9.97,9.2"/>
328 <dia:attribute name="meta">
329 <dia:composite type="dict"/>
331 <dia:attribute name="elem_corner">
332 <dia:point val="'.($this->x
* $factor).','.($this->y
* $factor).'"/>
334 <dia:attribute name="elem_width">
335 <dia:real val="5.9199999999999999"/>
337 <dia:attribute name="elem_height">
338 <dia:real val="3.5"/>
340 <dia:attribute name="text_colour">
341 <dia:color val="'.$this->tableColor
.'"/>
343 <dia:attribute name="line_colour">
344 <dia:color val="#000000"/>
346 <dia:attribute name="fill_colour">
347 <dia:color val="#ffffff"/>
349 <dia:attribute name="line_width">
350 <dia:real val="0.10000000000000001"/>
352 <dia:attribute name="name">
353 <dia:string>#'.$this->tableName
.'#</dia:string>
355 <dia:attribute name="comment">
356 <dia:string>##</dia:string>
358 <dia:attribute name="visible_comment">
359 <dia:boolean val="false"/>
361 <dia:attribute name="tagging_comment">
362 <dia:boolean val="false"/>
364 <dia:attribute name="underline_primary_key">
365 <dia:boolean val="true"/>
367 <dia:attribute name="bold_primary_keys">
368 <dia:boolean val="true"/>
370 <dia:attribute name="normal_font">
371 <dia:font family="monospace" style="0" name="Courier"/>
373 <dia:attribute name="name_font">
374 <dia:font family="sans" style="80" name="Helvetica-Bold"/>
376 <dia:attribute name="comment_font">
377 <dia:font family="sans" style="0" name="Helvetica"/>
379 <dia:attribute name="normal_font_height">
380 <dia:real val="0.80000000000000004"/>
382 <dia:attribute name="name_font_height">
383 <dia:real val="0.69999999999999996"/>
385 <dia:attribute name="comment_font_height">
386 <dia:real val="0.69999999999999996"/>
390 $dia->startElement('dia:attribute');
391 $dia->writeAttribute('name', 'attributes');
393 foreach ($this->fields
as $field) {
395 '<dia:composite type="table_attribute">
396 <dia:attribute name="name">
397 <dia:string>#'.$field.'#</dia:string>
399 <dia:attribute name="type">
400 <dia:string>##</dia:string>
402 <dia:attribute name="comment">
403 <dia:string>##</dia:string>
408 if (in_array($field, $this->primary
)) {
411 if ($field == $this->displayfield
) {
415 '<dia:attribute name="primary_key">
416 <dia:boolean val="'.$pm.'"/>
418 <dia:attribute name="nullable">
419 <dia:boolean val="false"/>
421 <dia:attribute name="unique">
422 <dia:boolean val="'.$pm.'"/>
433 * Relation preferences/statistics
435 * This class fetches the table master and foreign fields positions
436 * and helps in generating the Table references and then connects
437 * master table's master field to foreign table's foreign key
438 * in dia XML document.
440 * @name Relation_Stats
450 public $srcConnPointsRight;
451 public $srcConnPointsLeft;
452 public $destConnPointsRight;
453 public $destConnPointsLeft;
454 public $masterTableId;
455 public $foreignTableId;
456 public $masterTablePos;
457 public $foreignTablePos;
458 public $referenceColor;
461 * The "Relation_Stats" constructor
463 * @param string master_table The master table name
464 * @param string master_field The relation field in the master table
465 * @param string foreign_table The foreign table name
466 * @param string foreigh_field The relation field in the foreign table
468 * @see Relation_Stats::_getXy
470 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
472 $src_pos = $this->_getXy($master_table, $master_field);
473 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
474 $this->srcConnPointsLeft
= $src_pos[0];
475 $this->srcConnPointsRight
= $src_pos[1];
476 $this->destConnPointsLeft
= $dest_pos[0];
477 $this->destConnPointsRight
= $dest_pos[1];
478 $this->masterTablePos
= $src_pos[2];
479 $this->foreignTablePos
= $dest_pos[2];
480 $this->masterTableId
= $master_table->tableId
;
481 $this->foreignTableId
= $foreign_table->tableId
;
485 * Each Table object have connection points
486 * which is used to connect to other objects in Dia
487 * we detect the position of key in fields and
488 * then determines its left and right connection
491 * @param string table The current table name
492 * @param string column The relation column name
493 * @return array Table right,left connection points and key position
496 private function _getXy($table, $column)
498 $pos = array_search($column, $table->fields
);
499 // left, right, position
503 return array($pos +
$value +
$pos, $pos +
$value +
$pos +
1, $pos);
505 return array($pos +
$value , $pos +
$value +
1, $pos);
509 * Draws relation references
511 * connects master table's master field to foreign table's
512 * forein field using Dia object type Database - Reference
513 * Dia object is used to generate the XML of Dia Document.
514 * Database reference Object and their attributes are involved
515 * in the combination of displaing Database - reference on Dia Document.
517 * @param boolean changeColor Whether to use one color per relation or not
518 if changeColor is true then an array of $listOfColors
519 will be used to choose the random colors for references
520 lines. we can change/add more colors to this array
522 * @global object The current Dia document
526 public function relationDraw($changeColor)
530 PMA_Dia_Relation_Schema
::$objectId +
= 1;
532 * if source connection points and destination connection
533 * points are same then return it false and don't draw that
536 if ( $this->srcConnPointsRight
== $this->destConnPointsRight
){
537 if ( $this->srcConnPointsLeft
== $this->destConnPointsLeft
){
543 $listOfColors = array(
548 shuffle($listOfColors);
549 $this->referenceColor
= '#'.$listOfColors[0].'';
551 $this->referenceColor
= '#000000';
555 '<dia:object type="Database - Reference" version="0" id="'.PMA_Dia_Relation_Schema
::$objectId.'">
556 <dia:attribute name="obj_pos">
557 <dia:point val="3.27,18.9198"/>
559 <dia:attribute name="obj_bb">
560 <dia:rectangle val="2.27,8.7175;17.7679,18.9198"/>
562 <dia:attribute name="meta">
563 <dia:composite type="dict"/>
565 <dia:attribute name="orth_points">
566 <dia:point val="3.27,18.9198"/>
567 <dia:point val="2.27,18.9198"/>
568 <dia:point val="2.27,14.1286"/>
569 <dia:point val="17.7679,14.1286"/>
570 <dia:point val="17.7679,9.3375"/>
571 <dia:point val="16.7679,9.3375"/>
573 <dia:attribute name="orth_orient">
580 <dia:attribute name="orth_autoroute">
581 <dia:boolean val="true"/>
583 <dia:attribute name="text_colour">
584 <dia:color val="#000000"/>
586 <dia:attribute name="line_colour">
587 <dia:color val="'.$this->referenceColor
.'"/>
589 <dia:attribute name="line_width">
590 <dia:real val="0.10000000000000001"/>
592 <dia:attribute name="line_style">
596 <dia:attribute name="corner_radius">
599 <dia:attribute name="end_arrow">
602 <dia:attribute name="end_arrow_length">
603 <dia:real val="0.5"/>
605 <dia:attribute name="end_arrow_width">
606 <dia:real val="0.5"/>
608 <dia:attribute name="start_point_desc">
609 <dia:string>#1#</dia:string>
611 <dia:attribute name="end_point_desc">
612 <dia:string>#n#</dia:string>
614 <dia:attribute name="normal_font">
615 <dia:font family="monospace" style="0" name="Courier"/>
617 <dia:attribute name="normal_font_height">
618 <dia:real val="0.59999999999999998"/>
621 <dia:connection handle="0" to="'.$this->masterTableId
.'" connection="'.$this->srcConnPointsRight
.'"/>
622 <dia:connection handle="1" to="'.$this->foreignTableId
.'" connection="'.$this->destConnPointsRight
.'"/>
630 * Dia Relation Schema Class
632 * Purpose of this class is to generate the Dia XML Document
633 * which is used for representing the database diagrams in Dia IDE
634 * This class uses Database Table and Reference Objects of Dia and with
635 * the combination of these objects actually helps in preparing Dia XML.
637 * Dia XML is generated by using XMLWriter php extension and this class
638 * inherits Export_Relation_Schema class has common functionality added
641 * @name Dia_Relation_Schema
645 class PMA_Dia_Relation_Schema
extends PMA_Export_Relation_Schema
650 private $_tables = array();
651 private $_relations = array();
652 private $_topMargin = 2.8222000598907471;
653 private $_bottomMargin = 2.8222000598907471;
654 private $_leftMargin = 2.8222000598907471;
655 private $_rightMargin = 2.8222000598907471;
656 public static $objectId = 0;
659 * The "PMA_Dia_Relation_Schema" constructor
661 * Upon instantiation This outputs the Dia XML document
662 * that user can download
665 * @see PMA_DIA,Table_Stats,Relation_Stats
667 function __construct()
671 $this->setPageNumber($_POST['pdf_page_number']);
672 $this->setShowGrid(isset($_POST['show_grid']));
673 $this->setShowColor($_POST['show_color']);
674 $this->setShowKeys(isset($_POST['show_keys']));
675 $this->setOrientation(isset($_POST['orientation']));
676 $this->setPaper($_POST['paper']);
677 $this->setExportType($_POST['export_type']);
679 $dia = new PMA_DIA();
680 $dia->startDiaDoc($this->paper
,$this->_topMargin
,$this->_bottomMargin
,$this->_leftMargin
,$this->_rightMargin
,$this->orientation
);
681 $alltables = $this->getAllTables($db,$this->pageNumber
);
682 foreach ($alltables as $table) {
683 if (!isset($this->tables
[$table])) {
684 $this->tables
[$table] = new Table_Stats($table, $this->pageNumber
, $this->showKeys
);
688 $seen_a_relation = false;
689 foreach ($alltables as $one_table) {
690 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
692 $seen_a_relation = true;
693 foreach ($exist_rel as $master_field => $rel) {
694 /* put the foreign table on the schema only if selected
696 * (do not use array_search() because we would have to
697 * to do a === FALSE and this is not PHP3 compatible)
699 if (in_array($rel['foreign_table'], $alltables)) {
700 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'],$this->showKeys
);
705 $this->_drawTables($this->showColor
);
707 if ($seen_a_relation) {
708 $this->_drawRelations($this->showColor
);
711 $dia->showOutput($db.'-'.$this->pageNumber
);
716 * Defines relation objects
718 * @param string masterTable The master table name
719 * @param string masterField The relation field in the master table
720 * @param string foreignTable The foreign table name
721 * @param string foreignField The relation field in the foreign table
724 * @see Table_Stats::__construct(),Relation_Stats::__construct()
726 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showKeys)
728 if (!isset($this->tables
[$masterTable])) {
729 $this->tables
[$masterTable] = new Table_Stats($masterTable, $this->pageNumber
, $showKeys);
731 if (!isset($this->tables
[$foreignTable])) {
732 $this->tables
[$foreignTable] = new Table_Stats($foreignTable, $this->pageNumber
, $showKeys);
734 $this->_relations
[] = new Relation_Stats($this->tables
[$masterTable], $masterField, $this->tables
[$foreignTable], $foreignField);
738 * Draws relation references
740 * connects master table's master field to
741 * foreign table's forein field using Dia object
742 * type Database - Reference
744 * @param boolean changeColor Whether to use one color per relation or not
747 * @see Relation_Stats::relationDraw()
749 private function _drawRelations($changeColor)
751 foreach ($this->_relations
as $relation) {
752 $relation->relationDraw($changeColor);
759 * Tables are generated using Dia object type Database - Table
760 * primary fields are underlined and bold in tables
762 * @param boolean changeColor Whether to show color for tables text or not
765 * @see Table_Stats::tableDraw()
767 private function _drawTables($changeColor)
769 foreach ($this->tables
as $table) {
770 $table->tableDraw($changeColor);