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 SVG Schema Export
18 * @see http://php.net/manual/en/book.xmlwriter.php
21 class PMA_SVG
extends XMLWriter
29 * The "PMA_SVG" constructor
31 * Upon instantiation This starts writing the Svg 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');
51 $this->startDtd('svg','-//W3C//DTD SVG 1.1//EN','http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
58 * @param string value sets the title text
62 function setTitle($value)
64 $this->title
= $value;
70 * @param string value sets the author
74 function setAuthor($value)
76 $this->author
= $value;
82 * @param string value sets the font e.g Arial, Sans-serif etc
86 function setFont($value)
94 * @return string returns the font name
103 * Set document font size
105 * @param string value sets the font size in pixels
109 function setFontSize($value)
111 $this->fontSize
= $value;
115 * Get document font size
117 * @return string returns the font size
120 function getFontSize()
122 return $this->fontSize
;
126 * Starts Svg Document
128 * svg document starts by first initializing svg tag
129 * which contains all the attributes and namespace that needed
130 * to define the svg document
132 * @param integer width total width of the Svg document
133 * @param integer height total height of the Svg document
136 * @see XMLWriter::startElement(),XMLWriter::writeAttribute()
138 function startSvgDoc($width,$height)
140 $this->startElement('svg');
141 $this->writeAttribute('width', $width);
142 $this->writeAttribute('height', $height);
143 $this->writeAttribute('xmlns', 'http://www.w3.org/2000/svg');
144 $this->writeAttribute('version', '1.1');
152 * @see XMLWriter::endElement(),XMLWriter::endDocument()
157 $this->endDocument();
161 * output Svg Document
163 * svg document prompted to the user for download
164 * Svg document saved in .svg extension and can be
165 * easily changeable by using any svg IDE
169 * @see XMLWriter::startElement(),XMLWriter::writeAttribute()
171 function showOutput($fileName)
174 header('Content-type: image/svg+xml');
175 header('Content-Disposition: attachment; filename="'.$fileName.'.svg"');
176 $output = $this->flush();
183 * SVG has some predefined shape elements like rectangle & text
184 * and other elements who have x,y co-ordinates are drawn.
185 * specify their width and height and can give styles too.
187 * @param string name Svg element name
188 * @param integer x The x attribute defines the left position of the element
189 (e.g. x="0" places the element 0 pixels from the left of
191 * @param integer y The y attribute defines the top position of the element
192 (e.g. y="0" places the element 0 pixels from the top of
194 * @param integer width The width attribute defines the width the element
195 * @param integer height The height attribute defines the height the element
196 * @param string text The text attribute defines the text the element
197 * @param string styles The style attribute defines the style the element
198 styles can be defined like CSS styles
201 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),XMLWriter::text(),XMLWriter::endElement()
203 function printElement($name,$x,$y,$width = '',$height = '',$text = '',$styles = '')
205 $this->startElement($name);
206 $this->writeAttribute('width',$width);
207 $this->writeAttribute('height',$height);
208 $this->writeAttribute('x', $x);
209 $this->writeAttribute('y', $y);
210 $this->writeAttribute('style', $styles);
212 $this->writeAttribute('font-family', $this->font
);
213 $this->writeAttribute('font-size', $this->fontSize
);
220 * Draws Svg Line element
222 * Svg line element is drawn for connecting the tables.
223 * arrows are also drawn by specify its start and ending
226 * @param string name Svg element name i.e line
227 * @param integer x1 The x1 attribute defines the start of the line on the x-axis
228 * @param integer y1 The y1 attribute defines the start of the line on the y-axis
229 * @param integer x2 The x2 attribute defines the end of the line on the x-axis
230 * @param integer y2 The y2 attribute defines the end of the line on the y-axis
231 * @param string styles The style attribute defines the style the element
232 styles can be defined like CSS styles
235 * @see XMLWriter::startElement(),XMLWriter::writeAttribute(),XMLWriter::endElement()
237 function printElementLine($name,$x1,$y1,$x2,$y2,$styles)
239 $this->startElement($name);
240 $this->writeAttribute('x1',$x1);
241 $this->writeAttribute('y1',$y1);
242 $this->writeAttribute('x2', $x2);
243 $this->writeAttribute('y2', $y2);
244 $this->writeAttribute('style', $styles);
249 * get width of string/text
251 * Svg text element width is calcualted depending on font name
252 * and font size. It is very important to know the width of text
253 * because rectangle is drawn around it.
255 * This is a bit hardcore method. I didn't found any other than this.
257 * @param string text string that width will be calculated
258 * @param integer font name of the font like Arial,sans-serif etc
259 * @param integer fontSize size of font
260 * @return integer width of the text
263 function getStringWidth($text,$font,$fontSize)
266 * Start by counting the width, giving each character a modifying value
269 $count = $count +
((strlen($text) - strlen(str_replace(array("i","j","l"),"",$text)))*0.23);//ijl
270 $count = $count +
((strlen($text) - strlen(str_replace(array("f"),"",$text)))*0.27);//f
271 $count = $count +
((strlen($text) - strlen(str_replace(array("t","I"),"",$text)))*0.28);//tI
272 $count = $count +
((strlen($text) - strlen(str_replace(array("r"),"",$text)))*0.34);//r
273 $count = $count +
((strlen($text) - strlen(str_replace(array("1"),"",$text)))*0.49);//1
274 $count = $count +
((strlen($text) - strlen(str_replace(array("c","k","s","v","x","y","z","J"),"",$text)))*0.5);//cksvxyzJ
275 $count = $count +
((strlen($text) - strlen(str_replace(array("a","b","d","e","g","h","n","o","p","q","u","L","0","2","3","4","5","6","7","8","9"),"",$text)))*0.56);//abdeghnopquL023456789
276 $count = $count +
((strlen($text) - strlen(str_replace(array("F","T","Z"),"",$text)))*0.61);//FTZ
277 $count = $count +
((strlen($text) - strlen(str_replace(array("A","B","E","K","P","S","V","X","Y"),"",$text)))*0.67);//ABEKPSVXY
278 $count = $count +
((strlen($text) - strlen(str_replace(array("w","C","D","H","N","R","U"),"",$text)))*0.73);//wCDHNRU
279 $count = $count +
((strlen($text) - strlen(str_replace(array("G","O","Q"),"",$text)))*0.78);//GOQ
280 $count = $count +
((strlen($text) - strlen(str_replace(array("m","M"),"",$text)))*0.84);//mM
281 $count = $count +
((strlen($text) - strlen(str_replace("W","",$text)))*.95);//W
282 $count = $count +
((strlen($text) - strlen(str_replace(" ","",$text)))*.28);//" "
283 $text = str_replace(" ","",$text);//remove the " "'s
284 $count = $count +
(strlen(preg_replace("/[a-z0-9]/i","",$text))*0.3); //all other chrs
287 $font = strtolower($font);
290 * no modifier for arial and sans-serif
296 * .92 modifer for time, serif, brushscriptstd, and californian fb
300 case 'brushscriptstd':
301 case 'californian fb':
305 * 1.23 modifier for broadway
311 $textWidth = $count*$fontSize;
312 return ceil($textWidth*$modifier);
317 * Table preferences/statistics
319 * This class preserves the table co-ordinates,fields
320 * and helps in drawing/generating the Tables in SVG XML document.
334 private $_showInfo = false;
338 public $fields = array();
339 public $heightCell = 0;
340 public $currentCell = 0;
342 public $primary = array();
345 * The "Table_Stats" constructor
347 * @param string table_name The table name
348 * @param integer ff The font size
349 * @param integer samewidth The max. with among tables
350 * @param boolean show_keys Whether to display keys or not
351 * @param boolean show_info Whether to display table position or not
352 * @global object The current SVG image document
353 * @global integer The current page number (from the
354 * $cfg['Servers'][$i]['table_coords'] table)
355 * @global array The relations settings
356 * @global string The current db name
358 * @see PMA_SVG, Table_Stats::Table_Stats_setWidth,
359 Table_Stats::Table_Stats_setHeight
361 function __construct($tableName, $font, $fontSize, $pageNumber, &$same_wide_width, $showKeys = false, $showInfo = false)
363 global $svg, $cfgRelation, $db;
365 $this->_tableName
= $tableName;
366 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
367 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
368 if (!$result ||
!PMA_DBI_num_rows($result)) {
369 $svg->dieSchema($pageNumber,"SVG",sprintf(__('The %s table doesn\'t exist!'), $tableName));
374 * check to see if it will load all fields or only the foreign keys
378 $indexes = PMA_Index
::getFromTable($this->_tableName
, $db);
379 $all_columns = array();
380 foreach ($indexes as $index) {
381 $all_columns = array_merge($all_columns, array_flip(array_keys($index->getColumns())));
383 $this->fields
= array_keys($all_columns);
385 while ($row = PMA_DBI_fetch_row($result)) {
386 $this->fields
[] = $row[0];
390 $this->_showInfo
= $showInfo;
393 $this->_setHeightTable($fontSize);
395 // setWidth must me after setHeight, because title
396 // can include table height which changes table width
397 $this->_setWidthTable($font,$fontSize);
398 if ($same_wide_width < $this->width
) {
399 $same_wide_width = $this->width
;
403 $sql = 'SELECT x, y FROM '
404 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
405 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
406 . ' AND table_name = \'' . PMA_sqlAddslashes($tableName) . '\''
407 . ' AND pdf_page_number = ' . $pageNumber;
408 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
410 if (!$result ||
!PMA_DBI_num_rows($result)) {
411 $svg->dieSchema($pageNumber,"SVG",sprintf(__('Please configure the coordinates for table %s'), $tableName));
413 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
414 $this->x
= (double) $this->x
;
415 $this->y
= (double) $this->y
;
417 $this->displayfield
= PMA_getDisplayField($db, $tableName);
419 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE
);
420 if (PMA_DBI_num_rows($result) > 0) {
421 while ($row = PMA_DBI_fetch_assoc($result)) {
422 if ($row['Key_name'] == 'PRIMARY') {
423 $this->primary
[] = $row['Column_name'];
430 * Returns title of the current table,
431 * title can have the dimensions/co-ordinates of the table
435 private function _getTitle()
437 return ($this->_showInfo ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->heightCell
) : '') . ' ' . $this->_tableName
;
441 * Sets the width of the table
443 * @param string font The font size
444 * @param integer fontSize The font size
445 * @global object The current SVG image document
449 function _setWidthTable($font,$fontSize)
453 foreach ($this->fields
as $field) {
454 $this->width
= max($this->width
, $svg->getStringWidth($field,$font,$fontSize));
456 $this->width +
= $svg->getStringWidth(' ',$font,$fontSize);
458 * it is unknown what value must be added, because
459 * table title is affected by the tabe width value
461 while ($this->width
< $svg->getStringWidth($this->_getTitle(),$font,$fontSize)) {
467 * Sets the height of the table
471 function _setHeightTable($fontSize)
473 $this->heightCell
= $fontSize +
4;
474 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
480 * @param boolean showColor Whether to display color
481 * @global object The current SVG image document
483 * @see PMA_SVG,PMA_SVG::printElement
485 public function tableDraw($showColor)
488 //echo $this->_tableName.'<br />';
489 $svg->printElement('rect',$this->x
,$this->y
,
490 $this->width
,$this->heightCell
,
491 NULL,'fill:red;stroke:black;'
493 $svg->printElement('text',$this->x +
5,$this->y+
14,
494 $this->width
,$this->heightCell
,
496 'fill:none;stroke:black;'
498 foreach ($this->fields
as $field) {
499 $this->currentCell +
= $this->heightCell
;
502 if (in_array($field, $this->primary
)) {
505 if ($field == $this->displayfield
) {
509 $svg->printElement('rect', $this->x
,$this->y +
$this->currentCell
,
510 $this->width
, $this->heightCell
,
512 'fill:'.$showColor.';stroke:black;'
514 $svg->printElement('text', $this->x +
5, $this->y +
14 +
$this->currentCell
,
515 $this->width
, $this->heightCell
,
517 'fill:none;stroke:black;'
525 * Relation preferences/statistics
527 * This class fetches the table master and foreign fields positions
528 * and helps in generating the Table references and then connects
529 * master table's master field to foreign table's foreign key
530 * in SVG XML document.
532 * @name Relation_Stats
535 * @see PMA_SVG::printElementLine
545 public $xDest, $yDest;
549 * The "Relation_Stats" constructor
551 * @param string master_table The master table name
552 * @param string master_field The relation field in the master table
553 * @param string foreign_table The foreign table name
554 * @param string foreigh_field The relation field in the foreign table
555 * @see Relation_Stats::_getXy
557 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
559 $src_pos = $this->_getXy($master_table, $master_field);
560 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
566 $src_left = $src_pos[0] - $this->wTick
;
567 $src_right = $src_pos[1] +
$this->wTick
;
568 $dest_left = $dest_pos[0] - $this->wTick
;
569 $dest_right = $dest_pos[1] +
$this->wTick
;
571 $d1 = abs($src_left - $dest_left);
572 $d2 = abs($src_right - $dest_left);
573 $d3 = abs($src_left - $dest_right);
574 $d4 = abs($src_right - $dest_right);
575 $d = min($d1, $d2, $d3, $d4);
578 $this->xSrc
= $src_pos[0];
580 $this->xDest
= $dest_pos[0];
582 } elseif ($d == $d2) {
583 $this->xSrc
= $src_pos[1];
585 $this->xDest
= $dest_pos[0];
587 } elseif ($d == $d3) {
588 $this->xSrc
= $src_pos[0];
590 $this->xDest
= $dest_pos[1];
593 $this->xSrc
= $src_pos[1];
595 $this->xDest
= $dest_pos[1];
598 $this->ySrc
= $src_pos[2];
599 $this->yDest
= $dest_pos[2];
603 * Gets arrows coordinates
605 * @param string table The current table name
606 * @param string column The relation column name
607 * @return array Arrows coordinates
610 function _getXy($table, $column)
612 $pos = array_search($column, $table->fields
);
613 // x_left, x_right, y
614 return array($table->x
, $table->x +
$table->width
, $table->y +
($pos +
1.5) * $table->heightCell
);
618 * draws relation links and arrows
619 * shows foreign key relations
621 * @param boolean changeColor Whether to use one color per relation or not
622 * @global object The current SVG image document
626 public function relationDraw($changeColor)
631 $listOfColors = array(
640 shuffle($listOfColors);
641 $color = $listOfColors[0];
646 $svg->printElementLine('line',$this->xSrc
,$this->ySrc
,
647 $this->xSrc +
$this->srcDir
* $this->wTick
,$this->ySrc
,
648 'fill:'.$color.';stroke:black;stroke-width:2;'
650 $svg->printElementLine('line',$this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
,
651 $this->xDest
, $this->yDest
,
652 'fill:'.$color.';stroke:black;stroke-width:2;'
654 $svg->printElementLine('line',$this->xSrc +
$this->srcDir
* $this->wTick
,$this->ySrc
,
655 $this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
,
656 'fill:'.$color.';stroke:'.$color.';stroke-width:1;'
658 $root2 = 2 * sqrt(2);
659 $svg->printElementLine('line',$this->xSrc +
$this->srcDir
* $this->wTick
* 0.75, $this->ySrc
,
660 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
661 $this->ySrc +
$this->wTick
/ $root2 ,
662 'fill:'.$color.';stroke:black;stroke-width:2;'
664 $svg->printElementLine('line',$this->xSrc +
$this->srcDir
* $this->wTick
* 0.75, $this->ySrc
,
665 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
666 $this->ySrc
- $this->wTick
/ $root2 ,
667 'fill:'.$color.';stroke:black;stroke-width:2;'
669 $svg->printElementLine('line',$this->xDest +
$this->destDir
* $this->wTick
/ 2 , $this->yDest
,
670 $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
671 $this->yDest +
$this->wTick
/ $root2 ,
672 'fill:'.$color.';stroke:black;stroke-width:2;');
673 $svg->printElementLine('line',$this->xDest +
$this->destDir
* $this->wTick
/ 2 ,
674 $this->yDest
, $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
675 $this->yDest
- $this->wTick
/ $root2 ,
676 'fill:'.$color.';stroke:black;stroke-width:2;'
681 * end of the "Relation_Stats" class
685 * Svg Relation Schema Class
687 * Purpose of this class is to generate the SVG XML Document because
688 * SVG defines the graphics in XML format which is used for representing
689 * the database diagrams as vector image. This class actually helps
690 * in preparing SVG XML format.
692 * SVG XML is generated by using XMLWriter php extension and this class
693 * inherits Export_Relation_Schema class has common functionality added
696 * @name Svg_Relation_Schema
700 class PMA_Svg_Relation_Schema
extends PMA_Export_Relation_Schema
703 private $tables = array();
704 private $_relations = array();
708 private $_xMin = 100000;
709 private $_yMin = 100000;
710 private $t_marg = 10;
711 private $b_marg = 10;
712 private $l_marg = 10;
713 private $r_marg = 10;
714 private $_tablewidth;
717 * The "PMA_Svg_Relation_Schema" constructor
719 * Upon instantiation This starts writing the SVG XML document
720 * user will be prompted for download as .svg extension
725 function __construct()
729 $this->setPageNumber($_POST['pdf_page_number']);
730 $this->setShowColor(isset($_POST['show_color']));
731 $this->setShowKeys(isset($_POST['show_keys']));
732 $this->setTableDimension(isset($_POST['show_table_dimension']));
733 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
734 $this->setExportType($_POST['export_type']);
736 $svg = new PMA_SVG();
737 $svg->setTitle(sprintf(__('Schema of the %s database - Page %s'), $db, $this->pageNumber
));
738 $svg->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
739 $svg->setFont('Arial');
740 $svg->setFontSize('16px');
741 $svg->startSvgDoc('1000px','1000px');
742 $alltables = $this->getAllTables($db,$this->pageNumber
);
744 foreach ($alltables AS $table) {
745 if (!isset($this->tables
[$table])) {
746 $this->tables
[$table] = new Table_Stats($table,$svg->getFont(),$svg->getFontSize(), $this->pageNumber
, $this->_tablewidth
, $this->showKeys
, $this->tableDimension
);
749 if ($this->sameWide
) {
750 $this->tables
[$table]->width
= $this->_tablewidth
;
752 $this->_setMinMax($this->tables
[$table]);
754 $seen_a_relation = false;
755 foreach ($alltables as $one_table) {
756 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
758 $seen_a_relation = true;
759 foreach ($exist_rel as $master_field => $rel) {
760 /* put the foreign table on the schema only if selected
762 * (do not use array_search() because we would have to
763 * to do a === FALSE and this is not PHP3 compatible)
765 if (in_array($rel['foreign_table'], $alltables)) {
766 $this->_addRelation($one_table,$svg->getFont(),$svg->getFontSize(), $master_field, $rel['foreign_table'], $rel['foreign_field'], $this->tableDimension
);
771 if ($seen_a_relation) {
772 $this->_drawRelations($this->showColor
);
775 $this->_drawTables($this->showColor
);
777 $svg->showOutput($db.'-'.$this->pageNumber
);
782 * Sets X and Y minimum and maximum for a table cell
784 * @param string table The table name
787 private function _setMinMax($table)
789 $this->_xMax
= max($this->_xMax
, $table->x +
$table->width
);
790 $this->_yMax
= max($this->_yMax
, $table->y +
$table->height
);
791 $this->_xMin
= min($this->_xMin
, $table->x
);
792 $this->_yMin
= min($this->_yMin
, $table->y
);
796 * Defines relation objects
798 * @param string masterTable The master table name
799 * @param string masterField The relation field in the master table
800 * @param string foreignTable The foreign table name
801 * @param string foreignField The relation field in the foreign table
802 * @param boolean showInfo Whether to display table position or not
804 * @see _setMinMax,Table_Stats::__construct(),Relation_Stats::__construct()
806 private function _addRelation($masterTable,$font,$fontSize, $masterField, $foreignTable, $foreignField, $showInfo)
808 if (!isset($this->tables
[$masterTable])) {
809 $this->tables
[$masterTable] = new Table_Stats($masterTable, $font, $fontSize, $this->pageNumber
, $this->_tablewidth
, false, $showInfo);
810 $this->_setMinMax($this->tables
[$masterTable]);
812 if (!isset($this->tables
[$foreignTable])) {
813 $this->tables
[$foreignTable] = new Table_Stats($foreignTable,$font,$fontSize,$this->pageNumber
, $this->_tablewidth
, false, $showInfo);
814 $this->_setMinMax($this->tables
[$foreignTable]);
816 $this->_relations
[] = new Relation_Stats($this->tables
[$masterTable], $masterField, $this->tables
[$foreignTable], $foreignField);
820 * Draws relation arrows and lines
821 * connects master table's master field to
822 * foreign table's forein field
824 * @param boolean changeColor Whether to use one color per relation or not
826 * @see Relation_Stats::relationDraw()
828 private function _drawRelations($changeColor)
830 foreach ($this->_relations
as $relation) {
831 $relation->relationDraw($changeColor);
838 * @param boolean changeColor Whether to show color for primary fields or not
840 * @see Table_Stats::Table_Stats_tableDraw()
842 private function _drawTables($changeColor)
844 foreach ($this->tables
as $table) {
845 $table->tableDraw($changeColor);