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
15 * @see http://php.net/manual/en/book.xmlwriter.php
18 class PMA_DIA
extends XMLWriter
26 * The "PMA_DIA" constructor
28 * Upon instantiation This starts writing the Dia XML document
31 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
33 function __construct()
37 * Set indenting using three spaces,
38 * so output is formatted
41 $this->setIndent(true);
42 $this->setIndentString(' ');
44 * Create the XML document
47 $this->startDocument('1.0','UTF-8');
53 * dia document starts by first initializing dia:diagram tag
54 * then dia:diagramdata contains all the attributes that needed
55 * to define the document, then finally a Layer starts which
56 * holds all the objects.
58 * @param string paper The size of the paper/document
59 * @param float topMargin top margin of the paper/document in cm
60 * @param float bottomMargin bottom margin of the paper/document in cm
61 * @param float leftMargin left margin of the paper/document in cm
62 * @param float rightMargin right margin of the paper/document in cm
63 * @param string portrait document will be portrait or landscape
66 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),XMLWriter::writeRaw()
68 function startDiaDoc($paper,$topMargin,$bottomMargin,$leftMargin,$rightMargin,$portrait)
75 $this->startElement('dia:diagram');
76 $this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/');
77 $this->startElement('dia:diagramdata');
79 '<dia:attribute name="background">
80 <dia:color val="#ffffff"/>
82 <dia:attribute name="pagebreak">
83 <dia:color val="#000099"/>
85 <dia:attribute name="paper">
86 <dia:composite type="paper">
87 <dia:attribute name="name">
88 <dia:string>#'.$paper.'#</dia:string>
90 <dia:attribute name="tmargin">
91 <dia:real val="'.$topMargin.'"/>
93 <dia:attribute name="bmargin">
94 <dia:real val="'.$bottomMargin.'"/>
96 <dia:attribute name="lmargin">
97 <dia:real val="'.$leftMargin.'"/>
99 <dia:attribute name="rmargin">
100 <dia:real val="'.$rightMargin.'"/>
102 <dia:attribute name="is_portrait">
103 <dia:boolean val="'.$isPortrait.'"/>
105 <dia:attribute name="scaling">
108 <dia:attribute name="fitto">
109 <dia:boolean val="false"/>
113 <dia:attribute name="grid">
114 <dia:composite type="grid">
115 <dia:attribute name="width_x">
118 <dia:attribute name="width_y">
121 <dia:attribute name="visible_x">
124 <dia:attribute name="visible_y">
127 <dia:composite type="color"/>
130 <dia:attribute name="color">
131 <dia:color val="#d8e5e5"/>
133 <dia:attribute name="guides">
134 <dia:composite type="guides">
135 <dia:attribute name="hguides"/>
136 <dia:attribute name="vguides"/>
140 $this->startElement('dia:layer');
141 $this->writeAttribute('name', 'Background');
142 $this->writeAttribute('visible', 'true');
143 $this->writeAttribute('active', 'true');
152 * @see XMLWriter::endElement(),XMLWriter::endDocument()
157 $this->endDocument();
161 * Output Dia Document for download
163 * @param string fileName name of the dia document
166 * @see XMLWriter::flush()
168 function showOutput($fileName)
173 $output = $this->flush();
174 PMA_download_header($fileName . '.dia', 'application/x-dia-diagram', strlen($output));
180 * Table preferences/statistics
182 * This class preserves the table co-ordinates,fields
183 * and helps in drawing/generating the Tables in dia XML document.
194 public $fields = array();
196 public $primary = array();
201 * The "Table_Stats" constructor
203 * @param string table_name The table name
204 * @param integer pageNumber The current page number (from the
205 * $cfg['Servers'][$i]['table_coords'] table)
206 * @param boolean showKeys Whether to display ONLY keys or not
208 * @global object The current dia document
209 * @global array The relations settings
210 * @global string The current db name
213 function __construct($tableName, $pageNumber, $showKeys = false)
215 global $dia, $cfgRelation, $db;
217 $this->tableName
= $tableName;
218 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
219 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
220 if (!$result ||
!PMA_DBI_num_rows($result)) {
221 $dia->dieSchema($pageNumber,"DIA",sprintf(__('The %s table doesn\'t exist!'), $tableName));
225 * check to see if it will load all fields or only the foreign keys
228 $indexes = PMA_Index
::getFromTable($this->tableName
, $db);
229 $all_columns = array();
230 foreach ($indexes as $index) {
231 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
233 $this->fields
= array_keys($all_columns);
235 while ($row = PMA_DBI_fetch_row($result)) {
236 $this->fields
[] = $row[0];
240 $sql = 'SELECT x, y FROM '
241 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
242 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
243 . ' AND table_name = \'' . PMA_sqlAddSlashes($tableName) . '\''
244 . ' AND pdf_page_number = ' . $pageNumber;
245 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
246 if (!$result ||
!PMA_DBI_num_rows($result)) {
247 $dia->dieSchema($pageNumber,"DIA",sprintf(__('Please configure the coordinates for table %s'), $tableName));
249 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
250 $this->x
= (double) $this->x
;
251 $this->y
= (double) $this->y
;
255 $this->displayfield
= PMA_getDisplayField($db, $tableName);
259 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE
);
260 if (PMA_DBI_num_rows($result) > 0) {
261 while ($row = PMA_DBI_fetch_assoc($result)) {
262 if ($row['Key_name'] == 'PRIMARY') {
263 $this->primary
[] = $row['Column_name'];
268 * Every object in Dia document needs an ID to identify
269 * so, we used a static variable to keep the things unique
271 PMA_Dia_Relation_Schema
::$objectId +
= 1;
272 $this->tableId
= PMA_Dia_Relation_Schema
::$objectId;
278 * Tables are generated using object type Database - Table
279 * primary fields are underlined in tables. Dia object
280 * is used to generate the XML of Dia Document. Database Table
281 * Object and their attributes are involved in the combination
282 * of displaing Database - Table on Dia Document.
284 * @param boolean changeColor Whether to show color for tables text or not
285 if changeColor is true then an array of $listOfColors
286 will be used to choose the random colors for tables text
287 we can change/add more colors to this array
289 * @global object The current Dia document
293 public function tableDraw($changeColor)
298 $listOfColors = array(
303 shuffle($listOfColors);
304 $this->tableColor
= '#'.$listOfColors[0].'';
306 $this->tableColor
= '#000000';
311 $dia->startElement('dia:object');
312 $dia->writeAttribute('type', 'Database - Table');
313 $dia->writeAttribute('version', '0');
314 $dia->writeAttribute('id', ''.$this->tableId
.'');
316 '<dia:attribute name="obj_pos">
317 <dia:point val="'.($this->x
* $factor).','.($this->y
* $factor).'"/>
319 <dia:attribute name="obj_bb">
320 <dia:rectangle val="'.($this->x
* $factor).','.($this->y
* $factor).';9.97,9.2"/>
322 <dia:attribute name="meta">
323 <dia:composite type="dict"/>
325 <dia:attribute name="elem_corner">
326 <dia:point val="'.($this->x
* $factor).','.($this->y
* $factor).'"/>
328 <dia:attribute name="elem_width">
329 <dia:real val="5.9199999999999999"/>
331 <dia:attribute name="elem_height">
332 <dia:real val="3.5"/>
334 <dia:attribute name="text_colour">
335 <dia:color val="'.$this->tableColor
.'"/>
337 <dia:attribute name="line_colour">
338 <dia:color val="#000000"/>
340 <dia:attribute name="fill_colour">
341 <dia:color val="#ffffff"/>
343 <dia:attribute name="line_width">
344 <dia:real val="0.10000000000000001"/>
346 <dia:attribute name="name">
347 <dia:string>#'.$this->tableName
.'#</dia:string>
349 <dia:attribute name="comment">
350 <dia:string>##</dia:string>
352 <dia:attribute name="visible_comment">
353 <dia:boolean val="false"/>
355 <dia:attribute name="tagging_comment">
356 <dia:boolean val="false"/>
358 <dia:attribute name="underline_primary_key">
359 <dia:boolean val="true"/>
361 <dia:attribute name="bold_primary_keys">
362 <dia:boolean val="true"/>
364 <dia:attribute name="normal_font">
365 <dia:font family="monospace" style="0" name="Courier"/>
367 <dia:attribute name="name_font">
368 <dia:font family="sans" style="80" name="Helvetica-Bold"/>
370 <dia:attribute name="comment_font">
371 <dia:font family="sans" style="0" name="Helvetica"/>
373 <dia:attribute name="normal_font_height">
374 <dia:real val="0.80000000000000004"/>
376 <dia:attribute name="name_font_height">
377 <dia:real val="0.69999999999999996"/>
379 <dia:attribute name="comment_font_height">
380 <dia:real val="0.69999999999999996"/>
384 $dia->startElement('dia:attribute');
385 $dia->writeAttribute('name', 'attributes');
387 foreach ($this->fields
as $field) {
389 '<dia:composite type="table_attribute">
390 <dia:attribute name="name">
391 <dia:string>#'.$field.'#</dia:string>
393 <dia:attribute name="type">
394 <dia:string>##</dia:string>
396 <dia:attribute name="comment">
397 <dia:string>##</dia:string>
402 if (in_array($field, $this->primary
)) {
405 if ($field == $this->displayfield
) {
409 '<dia:attribute name="primary_key">
410 <dia:boolean val="'.$pm.'"/>
412 <dia:attribute name="nullable">
413 <dia:boolean val="false"/>
415 <dia:attribute name="unique">
416 <dia:boolean val="'.$pm.'"/>
427 * Relation preferences/statistics
429 * This class fetches the table master and foreign fields positions
430 * and helps in generating the Table references and then connects
431 * master table's master field to foreign table's foreign key
432 * in dia XML document.
434 * @name Relation_Stats
442 public $srcConnPointsRight;
443 public $srcConnPointsLeft;
444 public $destConnPointsRight;
445 public $destConnPointsLeft;
446 public $masterTableId;
447 public $foreignTableId;
448 public $masterTablePos;
449 public $foreignTablePos;
450 public $referenceColor;
453 * The "Relation_Stats" constructor
455 * @param string master_table The master table name
456 * @param string master_field The relation field in the master table
457 * @param string foreign_table The foreign table name
458 * @param string foreigh_field The relation field in the foreign table
460 * @see Relation_Stats::_getXy
462 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
464 $src_pos = $this->_getXy($master_table, $master_field);
465 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
466 $this->srcConnPointsLeft
= $src_pos[0];
467 $this->srcConnPointsRight
= $src_pos[1];
468 $this->destConnPointsLeft
= $dest_pos[0];
469 $this->destConnPointsRight
= $dest_pos[1];
470 $this->masterTablePos
= $src_pos[2];
471 $this->foreignTablePos
= $dest_pos[2];
472 $this->masterTableId
= $master_table->tableId
;
473 $this->foreignTableId
= $foreign_table->tableId
;
477 * Each Table object have connection points
478 * which is used to connect to other objects in Dia
479 * we detect the position of key in fields and
480 * then determines its left and right connection
483 * @param string table The current table name
484 * @param string column The relation column name
485 * @return array Table right,left connection points and key position
488 private function _getXy($table, $column)
490 $pos = array_search($column, $table->fields
);
491 // left, right, position
495 return array($pos +
$value +
$pos, $pos +
$value +
$pos +
1, $pos);
497 return array($pos +
$value , $pos +
$value +
1, $pos);
501 * Draws relation references
503 * connects master table's master field to foreign table's
504 * forein field using Dia object type Database - Reference
505 * Dia object is used to generate the XML of Dia Document.
506 * Database reference Object and their attributes are involved
507 * in the combination of displaing Database - reference on Dia Document.
509 * @param boolean changeColor Whether to use one color per relation or not
510 if changeColor is true then an array of $listOfColors
511 will be used to choose the random colors for references
512 lines. we can change/add more colors to this array
514 * @global object The current Dia document
518 public function relationDraw($changeColor)
522 PMA_Dia_Relation_Schema
::$objectId +
= 1;
524 * if source connection points and destination connection
525 * points are same then return it false and don't draw that
528 if ( $this->srcConnPointsRight
== $this->destConnPointsRight
){
529 if ( $this->srcConnPointsLeft
== $this->destConnPointsLeft
){
535 $listOfColors = array(
540 shuffle($listOfColors);
541 $this->referenceColor
= '#'.$listOfColors[0].'';
543 $this->referenceColor
= '#000000';
547 '<dia:object type="Database - Reference" version="0" id="'.PMA_Dia_Relation_Schema
::$objectId.'">
548 <dia:attribute name="obj_pos">
549 <dia:point val="3.27,18.9198"/>
551 <dia:attribute name="obj_bb">
552 <dia:rectangle val="2.27,8.7175;17.7679,18.9198"/>
554 <dia:attribute name="meta">
555 <dia:composite type="dict"/>
557 <dia:attribute name="orth_points">
558 <dia:point val="3.27,18.9198"/>
559 <dia:point val="2.27,18.9198"/>
560 <dia:point val="2.27,14.1286"/>
561 <dia:point val="17.7679,14.1286"/>
562 <dia:point val="17.7679,9.3375"/>
563 <dia:point val="16.7679,9.3375"/>
565 <dia:attribute name="orth_orient">
572 <dia:attribute name="orth_autoroute">
573 <dia:boolean val="true"/>
575 <dia:attribute name="text_colour">
576 <dia:color val="#000000"/>
578 <dia:attribute name="line_colour">
579 <dia:color val="'.$this->referenceColor
.'"/>
581 <dia:attribute name="line_width">
582 <dia:real val="0.10000000000000001"/>
584 <dia:attribute name="line_style">
588 <dia:attribute name="corner_radius">
591 <dia:attribute name="end_arrow">
594 <dia:attribute name="end_arrow_length">
595 <dia:real val="0.5"/>
597 <dia:attribute name="end_arrow_width">
598 <dia:real val="0.5"/>
600 <dia:attribute name="start_point_desc">
601 <dia:string>#1#</dia:string>
603 <dia:attribute name="end_point_desc">
604 <dia:string>#n#</dia:string>
606 <dia:attribute name="normal_font">
607 <dia:font family="monospace" style="0" name="Courier"/>
609 <dia:attribute name="normal_font_height">
610 <dia:real val="0.59999999999999998"/>
613 <dia:connection handle="0" to="'.$this->masterTableId
.'" connection="'.$this->srcConnPointsRight
.'"/>
614 <dia:connection handle="1" to="'.$this->foreignTableId
.'" connection="'.$this->destConnPointsRight
.'"/>
622 * Dia Relation Schema Class
624 * Purpose of this class is to generate the Dia XML Document
625 * which is used for representing the database diagrams in Dia IDE
626 * This class uses Database Table and Reference Objects of Dia and with
627 * the combination of these objects actually helps in preparing Dia XML.
629 * Dia XML is generated by using XMLWriter php extension and this class
630 * inherits Export_Relation_Schema class has common functionality added
633 * @name Dia_Relation_Schema
635 class PMA_Dia_Relation_Schema
extends PMA_Export_Relation_Schema
640 private $_tables = array();
641 private $_relations = array();
642 private $_topMargin = 2.8222000598907471;
643 private $_bottomMargin = 2.8222000598907471;
644 private $_leftMargin = 2.8222000598907471;
645 private $_rightMargin = 2.8222000598907471;
646 public static $objectId = 0;
649 * The "PMA_Dia_Relation_Schema" constructor
651 * Upon instantiation This outputs the Dia XML document
652 * that user can download
655 * @see PMA_DIA,Table_Stats,Relation_Stats
657 function __construct()
661 $this->setPageNumber($_POST['pdf_page_number']);
662 $this->setShowGrid(isset($_POST['show_grid']));
663 $this->setShowColor($_POST['show_color']);
664 $this->setShowKeys(isset($_POST['show_keys']));
665 $this->setOrientation(isset($_POST['orientation']));
666 $this->setPaper($_POST['paper']);
667 $this->setExportType($_POST['export_type']);
669 $dia = new PMA_DIA();
670 $dia->startDiaDoc($this->paper
,$this->_topMargin
,$this->_bottomMargin
,$this->_leftMargin
,$this->_rightMargin
,$this->orientation
);
671 $alltables = $this->getAllTables($db,$this->pageNumber
);
672 foreach ($alltables as $table) {
673 if (! isset($this->tables
[$table])) {
674 $this->tables
[$table] = new Table_Stats($table, $this->pageNumber
, $this->showKeys
);
678 $seen_a_relation = false;
679 foreach ($alltables as $one_table) {
680 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
682 $seen_a_relation = true;
683 foreach ($exist_rel as $master_field => $rel) {
684 /* put the foreign table on the schema only if selected
686 * (do not use array_search() because we would have to
687 * to do a === false and this is not PHP3 compatible)
689 if (in_array($rel['foreign_table'], $alltables)) {
690 $this->_addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field'],$this->showKeys
);
695 $this->_drawTables($this->showColor
);
697 if ($seen_a_relation) {
698 $this->_drawRelations($this->showColor
);
701 $dia->showOutput($db.'-'.$this->pageNumber
);
706 * Defines relation objects
708 * @param string masterTable The master table name
709 * @param string masterField The relation field in the master table
710 * @param string foreignTable The foreign table name
711 * @param string foreignField The relation field in the foreign table
714 * @see Table_Stats::__construct(),Relation_Stats::__construct()
716 private function _addRelation($masterTable, $masterField, $foreignTable, $foreignField, $showKeys)
718 if (! isset($this->tables
[$masterTable])) {
719 $this->tables
[$masterTable] = new Table_Stats($masterTable, $this->pageNumber
, $showKeys);
721 if (! isset($this->tables
[$foreignTable])) {
722 $this->tables
[$foreignTable] = new Table_Stats($foreignTable, $this->pageNumber
, $showKeys);
724 $this->_relations
[] = new Relation_Stats($this->tables
[$masterTable], $masterField, $this->tables
[$foreignTable], $foreignField);
728 * Draws relation references
730 * connects master table's master field to
731 * foreign table's forein field using Dia object
732 * type Database - Reference
734 * @param boolean changeColor Whether to use one color per relation or not
737 * @see Relation_Stats::relationDraw()
739 private function _drawRelations($changeColor)
741 foreach ($this->_relations
as $relation) {
742 $relation->relationDraw($changeColor);
749 * Tables are generated using Dia object type Database - Table
750 * primary fields are underlined and bold in tables
752 * @param boolean changeColor Whether to show color for tables text or not
755 * @see Table_Stats::tableDraw()
757 private function _drawTables($changeColor)
759 foreach ($this->tables
as $table) {
760 $table->tableDraw($changeColor);