2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 require_once 'Export_Relation_Schema.class.php';
11 * This Class inherits the XMLwriter class and
12 * helps in developing structure of SVG Schema Export
15 * @see http://php.net/manual/en/book.xmlwriter.php
17 class PMA_SVG
extends XMLWriter
25 * The "PMA_SVG" constructor
27 * Upon instantiation This starts writing the Svg XML document
30 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
32 function __construct()
36 * Set indenting using three spaces,
37 * so output is formatted
40 $this->setIndent(true);
41 $this->setIndentString(' ');
43 * Create the XML document
46 $this->startDocument('1.0', 'UTF-8');
48 'svg', '-//W3C//DTD SVG 1.1//EN',
49 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'
57 * @param string $value sets the title text
62 function setTitle($value)
64 $this->title
= $value;
70 * @param string $value sets the author
75 function setAuthor($value)
77 $this->author
= $value;
83 * @param string $value sets the font e.g Arial, Sans-serif etc
88 function setFont($value)
96 * @return string returns the font name
105 * Set document font size
107 * @param string $value sets the font size in pixels
112 function setFontSize($value)
114 $this->fontSize
= $value;
118 * Get document font size
120 * @return string returns the font size
123 function getFontSize()
125 return $this->fontSize
;
129 * Starts Svg Document
131 * svg document starts by first initializing svg tag
132 * which contains all the attributes and namespace that needed
133 * to define the svg document
135 * @param integer $width total width of the Svg document
136 * @param integer $height total height of the Svg document
141 * @see XMLWriter::startElement(),XMLWriter::writeAttribute()
143 function startSvgDoc($width,$height)
145 $this->startElement('svg');
146 $this->writeAttribute('width', $width);
147 $this->writeAttribute('height', $height);
148 $this->writeAttribute('xmlns', 'http://www.w3.org/2000/svg');
149 $this->writeAttribute('version', '1.1');
157 * @see XMLWriter::endElement(),XMLWriter::endDocument()
162 $this->endDocument();
166 * output Svg Document
168 * svg document prompted to the user for download
169 * Svg document saved in .svg extension and can be
170 * easily changeable by using any svg IDE
172 * @param string $fileName file name
176 * @see XMLWriter::startElement(),XMLWriter::writeAttribute()
178 function showOutput($fileName)
181 $output = $this->flush();
182 PMA_download_header($fileName . '.svg', 'image/svg+xml', strlen($output));
189 * SVG has some predefined shape elements like rectangle & text
190 * and other elements who have x,y co-ordinates are drawn.
191 * specify their width and height and can give styles too.
193 * @param string $name Svg element name
194 * @param integer $x The x attr defines the left position of the element
195 * (e.g. x="0" places the element 0 pixels from the left of the browser window)
196 * @param integer $y The y attribute defines the top position of the element
197 * (e.g. y="0" places the element 0 pixels from the top of the browser window)
198 * @param integer $width The width attribute defines the width the element
199 * @param integer $height The height attribute defines the height the element
200 * @param string $text The text attribute defines the text the element
201 * @param string $styles The style attribute defines the style the element
202 * styles can be defined like CSS styles
207 * @see XMLWriter::startElement(), XMLWriter::writeAttribute(),
208 * XMLWriter::text(), XMLWriter::endElement()
210 function printElement($name, $x, $y, $width = '', $height = '', $text = '', $styles = '')
212 $this->startElement($name);
213 $this->writeAttribute('width', $width);
214 $this->writeAttribute('height', $height);
215 $this->writeAttribute('x', $x);
216 $this->writeAttribute('y', $y);
217 $this->writeAttribute('style', $styles);
219 $this->writeAttribute('font-family', $this->font
);
220 $this->writeAttribute('font-size', $this->fontSize
);
227 * Draws Svg Line element
229 * Svg line element is drawn for connecting the tables.
230 * arrows are also drawn by specify its start and ending
233 * @param string $name Svg element name i.e line
234 * @param integer $x1 Defines the start of the line on the x-axis
235 * @param integer $y1 Defines the start of the line on the y-axis
236 * @param integer $x2 Defines the end of the line on the x-axis
237 * @param integer $y2 Defines the end of the line on the y-axis
238 * @param string $styles The style attribute defines the style the element
239 * styles can be defined like CSS styles
244 * @see XMLWriter::startElement(), XMLWriter::writeAttribute(),
245 * XMLWriter::endElement()
247 function printElementLine($name,$x1,$y1,$x2,$y2,$styles)
249 $this->startElement($name);
250 $this->writeAttribute('x1', $x1);
251 $this->writeAttribute('y1', $y1);
252 $this->writeAttribute('x2', $x2);
253 $this->writeAttribute('y2', $y2);
254 $this->writeAttribute('style', $styles);
259 * get width of string/text
261 * Svg text element width is calcualted depending on font name
262 * and font size. It is very important to know the width of text
263 * because rectangle is drawn around it.
265 * This is a bit hardcore method. I didn't found any other than this.
267 * @param string $text string that width will be calculated
268 * @param integer $font name of the font like Arial,sans-serif etc
269 * @param integer $fontSize size of font
271 * @return integer width of the text
274 function getStringWidth($text,$font,$fontSize)
277 * Start by counting the width, giving each character a modifying value
280 $count = $count +
((strlen($text) - strlen(str_replace(array("i", "j", "l"), "", $text))) * 0.23);//ijl
281 $count = $count +
((strlen($text) - strlen(str_replace(array("f"), "", $text))) * 0.27);//f
282 $count = $count +
((strlen($text) - strlen(str_replace(array("t", "I"), "", $text))) * 0.28);//tI
283 $count = $count +
((strlen($text) - strlen(str_replace(array("r"), "", $text))) * 0.34);//r
284 $count = $count +
((strlen($text) - strlen(str_replace(array("1"), "", $text))) * 0.49);//1
285 $count = $count +
((strlen($text) - strlen(str_replace(array("c", "k", "s", "v", "x", "y", "z", "J"), "", $text))) * 0.5);//cksvxyzJ
286 $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
287 $count = $count +
((strlen($text) - strlen(str_replace(array("F", "T", "Z"), "", $text))) * 0.61);//FTZ
288 $count = $count +
((strlen($text) - strlen(str_replace(array("A", "B", "E", "K", "P", "S", "V", "X", "Y"), "", $text))) * 0.67);//ABEKPSVXY
289 $count = $count +
((strlen($text) - strlen(str_replace(array("w", "C", "D", "H", "N", "R", "U"), "", $text))) * 0.73);//wCDHNRU
290 $count = $count +
((strlen($text) - strlen(str_replace(array("G", "O", "Q"), "", $text))) * 0.78);//GOQ
291 $count = $count +
((strlen($text) - strlen(str_replace(array("m", "M"), "", $text))) * 0.84);//mM
292 $count = $count +
((strlen($text) - strlen(str_replace("W", "", $text))) * .95);//W
293 $count = $count +
((strlen($text) - strlen(str_replace(" ", "", $text))) * .28);//" "
294 $text = str_replace(" ", "", $text);//remove the " "'s
295 $count = $count +
(strlen(preg_replace("/[a-z0-9]/i", "", $text)) * 0.3); //all other chrs
298 $font = strtolower($font);
301 * no modifier for arial and sans-serif
307 * .92 modifer for time, serif, brushscriptstd, and californian fb
311 case 'brushscriptstd':
312 case 'californian fb':
316 * 1.23 modifier for broadway
322 $textWidth = $count*$fontSize;
323 return ceil($textWidth*$modifier);
328 * Table preferences/statistics
330 * This class preserves the table co-ordinates,fields
331 * and helps in drawing/generating the Tables in SVG XML document.
343 private $_showInfo = false;
347 public $fields = array();
348 public $heightCell = 0;
349 public $currentCell = 0;
351 public $primary = array();
354 * The "Table_Stats" constructor
356 * @param string $tableName The table name
357 * @param string $font Font face
358 * @param integer $fontSize The font size
359 * @param integer $pageNumber Page number
360 * @param integer &$same_wide_width The max. with among tables
361 * @param boolean $showKeys Whether to display keys or not
362 * @param boolean $showInfo Whether to display table position or not
364 * @global object The current SVG image document
365 * @global integer The current page number (from the
366 * $cfg['Servers'][$i]['table_coords'] table)
367 * @global array The relations settings
368 * @global string The current db name
372 * @see PMA_SVG, Table_Stats::Table_Stats_setWidth,
373 * Table_Stats::Table_Stats_setHeight
375 function __construct($tableName, $font, $fontSize, $pageNumber,
376 &$same_wide_width, $showKeys = false, $showInfo = false)
378 global $svg, $cfgRelation, $db;
380 $this->_tableName
= $tableName;
381 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
382 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
383 if (! $result ||
! PMA_DBI_num_rows($result)) {
387 sprintf(__('The %s table doesn\'t exist!'), $tableName)
393 * check to see if it will load all fields or only the foreign keys
397 $indexes = PMA_Index
::getFromTable($this->_tableName
, $db);
398 $all_columns = array();
399 foreach ($indexes as $index) {
400 $all_columns = array_merge(
402 array_flip(array_keys($index->getColumns()))
405 $this->fields
= array_keys($all_columns);
407 while ($row = PMA_DBI_fetch_row($result)) {
408 $this->fields
[] = $row[0];
412 $this->_showInfo
= $showInfo;
415 $this->_setHeightTable($fontSize);
417 // setWidth must me after setHeight, because title
418 // can include table height which changes table width
419 $this->_setWidthTable($font, $fontSize);
420 if ($same_wide_width < $this->width
) {
421 $same_wide_width = $this->width
;
425 $sql = 'SELECT x, y FROM '
426 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
427 . PMA_backquote($cfgRelation['table_coords'])
428 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
429 . ' AND table_name = \'' . PMA_sqlAddSlashes($tableName) . '\''
430 . ' AND pdf_page_number = ' . $pageNumber;
431 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
433 if (!$result ||
!PMA_DBI_num_rows($result)) {
438 __('Please configure the coordinates for table %s'),
443 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
444 $this->x
= (double) $this->x
;
445 $this->y
= (double) $this->y
;
447 $this->displayfield
= PMA_getDisplayField($db, $tableName);
449 $result = PMA_DBI_query(
450 'SHOW INDEX FROM ' . PMA_backquote($tableName) . ';',
454 if (PMA_DBI_num_rows($result) > 0) {
455 while ($row = PMA_DBI_fetch_assoc($result)) {
456 if ($row['Key_name'] == 'PRIMARY') {
457 $this->primary
[] = $row['Column_name'];
464 * Returns title of the current table,
465 * title can have the dimensions/co-ordinates of the table
469 private function _getTitle()
471 return ($this->_showInfo
472 ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->heightCell
)
474 ) . ' ' . $this->_tableName
;
478 * Sets the width of the table
480 * @param string $font The font size
481 * @param integer $fontSize The font size
483 * @global object The current SVG image document
490 private function _setWidthTable($font,$fontSize)
494 foreach ($this->fields
as $field) {
497 $svg->getStringWidth($field, $font, $fontSize)
500 $this->width +
= $svg->getStringWidth(' ', $font, $fontSize);
503 * it is unknown what value must be added, because
504 * table title is affected by the tabe width value
506 while ($this->width
< $svg->getStringWidth($this->_getTitle(), $font, $fontSize)) {
512 * Sets the height of the table
514 * @param integer $fontSize font size
519 function _setHeightTable($fontSize)
521 $this->heightCell
= $fontSize +
4;
522 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
528 * @param boolean $showColor Whether to display color
530 * @global object The current SVG image document
535 * @see PMA_SVG,PMA_SVG::printElement
537 public function tableDraw($showColor)
540 //echo $this->_tableName.'<br />';
542 'rect', $this->x
, $this->y
, $this->width
,
543 $this->heightCell
, null, 'fill:red;stroke:black;'
546 'text', $this->x +
5, $this->y+
14, $this->width
, $this->heightCell
,
547 $this->_getTitle(), 'fill:none;stroke:black;'
549 foreach ($this->fields
as $field) {
550 $this->currentCell +
= $this->heightCell
;
553 if (in_array($field, $this->primary
)) {
556 if ($field == $this->displayfield
) {
561 'rect', $this->x
, $this->y +
$this->currentCell
, $this->width
,
562 $this->heightCell
, null, 'fill:'.$showColor.';stroke:black;'
565 'text', $this->x +
5, $this->y +
14 +
$this->currentCell
,
566 $this->width
, $this->heightCell
, $field, 'fill:none;stroke:black;'
574 * Relation preferences/statistics
576 * This class fetches the table master and foreign fields positions
577 * and helps in generating the Table references and then connects
578 * master table's master field to foreign table's foreign key
579 * in SVG XML document.
581 * @name Relation_Stats
582 * @see PMA_SVG::printElementLine
592 public $xDest, $yDest;
596 * The "Relation_Stats" constructor
598 * @param string $master_table The master table name
599 * @param string $master_field The relation field in the master table
600 * @param string $foreign_table The foreign table name
601 * @param string $foreign_field The relation field in the foreign table
605 * @see Relation_Stats::_getXy
607 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
609 $src_pos = $this->_getXy($master_table, $master_field);
610 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
616 $src_left = $src_pos[0] - $this->wTick
;
617 $src_right = $src_pos[1] +
$this->wTick
;
618 $dest_left = $dest_pos[0] - $this->wTick
;
619 $dest_right = $dest_pos[1] +
$this->wTick
;
621 $d1 = abs($src_left - $dest_left);
622 $d2 = abs($src_right - $dest_left);
623 $d3 = abs($src_left - $dest_right);
624 $d4 = abs($src_right - $dest_right);
625 $d = min($d1, $d2, $d3, $d4);
628 $this->xSrc
= $src_pos[0];
630 $this->xDest
= $dest_pos[0];
632 } elseif ($d == $d2) {
633 $this->xSrc
= $src_pos[1];
635 $this->xDest
= $dest_pos[0];
637 } elseif ($d == $d3) {
638 $this->xSrc
= $src_pos[0];
640 $this->xDest
= $dest_pos[1];
643 $this->xSrc
= $src_pos[1];
645 $this->xDest
= $dest_pos[1];
648 $this->ySrc
= $src_pos[2];
649 $this->yDest
= $dest_pos[2];
653 * Gets arrows coordinates
655 * @param string $table The current table name
656 * @param string $column The relation column name
658 * @return array Arrows coordinates
661 function _getXy($table, $column)
663 $pos = array_search($column, $table->fields
);
664 // x_left, x_right, y
667 $table->x +
$table->width
,
668 $table->y +
($pos +
1.5) * $table->heightCell
673 * draws relation links and arrows shows foreign key relations
675 * @param boolean $changeColor Whether to use one color per relation or not
677 * @global object The current SVG image document
684 public function relationDraw($changeColor)
689 $listOfColors = array(
698 shuffle($listOfColors);
699 $color = $listOfColors[0];
704 $svg->printElementLine(
705 'line', $this->xSrc
, $this->ySrc
,
706 $this->xSrc +
$this->srcDir
* $this->wTick
, $this->ySrc
,
707 'fill:' . $color . ';stroke:black;stroke-width:2;'
709 $svg->printElementLine(
710 'line', $this->xDest +
$this->destDir
* $this->wTick
,
711 $this->yDest
, $this->xDest
, $this->yDest
,
712 'fill:' . $color . ';stroke:black;stroke-width:2;'
714 $svg->printElementLine(
715 'line', $this->xSrc +
$this->srcDir
* $this->wTick
, $this->ySrc
,
716 $this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
,
717 'fill:' . $color . ';stroke:' . $color . ';stroke-width:1;'
719 $root2 = 2 * sqrt(2);
720 $svg->printElementLine(
721 'line', $this->xSrc +
$this->srcDir
* $this->wTick
* 0.75, $this->ySrc
,
722 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
723 $this->ySrc +
$this->wTick
/ $root2,
724 'fill:' . $color . ';stroke:black;stroke-width:2;'
726 $svg->printElementLine(
727 'line', $this->xSrc +
$this->srcDir
* $this->wTick
* 0.75, $this->ySrc
,
728 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
729 $this->ySrc
- $this->wTick
/ $root2,
730 'fill:' . $color . ';stroke:black;stroke-width:2;'
732 $svg->printElementLine(
733 'line', $this->xDest +
$this->destDir
* $this->wTick
/ 2, $this->yDest
,
734 $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
735 $this->yDest +
$this->wTick
/ $root2,
736 'fill:' . $color . ';stroke:black;stroke-width:2;'
738 $svg->printElementLine(
739 'line', $this->xDest +
$this->destDir
* $this->wTick
/ 2, $this->yDest
,
740 $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
741 $this->yDest
- $this->wTick
/ $root2,
742 'fill:' . $color . ';stroke:black;stroke-width:2;'
747 * end of the "Relation_Stats" class
751 * Svg Relation Schema Class
753 * Purpose of this class is to generate the SVG XML Document because
754 * SVG defines the graphics in XML format which is used for representing
755 * the database diagrams as vector image. This class actually helps
756 * in preparing SVG XML format.
758 * SVG XML is generated by using XMLWriter php extension and this class
759 * inherits Export_Relation_Schema class has common functionality added
762 * @name Svg_Relation_Schema
764 class PMA_Svg_Relation_Schema
extends PMA_Export_Relation_Schema
767 private $tables = array();
768 private $_relations = array();
772 private $_xMin = 100000;
773 private $_yMin = 100000;
774 private $t_marg = 10;
775 private $b_marg = 10;
776 private $l_marg = 10;
777 private $r_marg = 10;
778 private $_tablewidth;
781 * The "PMA_Svg_Relation_Schema" constructor
783 * Upon instantiation This starts writing the SVG XML document
784 * user will be prompted for download as .svg extension
789 function __construct()
793 $this->setPageNumber($_POST['pdf_page_number']);
794 $this->setShowColor(isset($_POST['show_color']));
795 $this->setShowKeys(isset($_POST['show_keys']));
796 $this->setTableDimension(isset($_POST['show_table_dimension']));
797 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
798 $this->setExportType($_POST['export_type']);
800 $svg = new PMA_SVG();
803 __('Schema of the %s database - Page %s'),
808 $svg->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
809 $svg->setFont('Arial');
810 $svg->setFontSize('16px');
811 $svg->startSvgDoc('1000px', '1000px');
812 $alltables = $this->getAllTables($db, $this->pageNumber
);
814 foreach ($alltables AS $table) {
815 if (! isset($this->tables
[$table])) {
816 $this->tables
[$table] = new Table_Stats(
817 $table, $svg->getFont(), $svg->getFontSize(), $this->pageNumber
,
818 $this->_tablewidth
, $this->showKeys
, $this->tableDimension
822 if ($this->sameWide
) {
823 $this->tables
[$table]->width
= $this->_tablewidth
;
825 $this->_setMinMax($this->tables
[$table]);
827 $seen_a_relation = false;
828 foreach ($alltables as $one_table) {
829 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
831 $seen_a_relation = true;
832 foreach ($exist_rel as $master_field => $rel) {
833 /* put the foreign table on the schema only if selected
835 * (do not use array_search() because we would have to
836 * to do a === false and this is not PHP3 compatible)
838 if (in_array($rel['foreign_table'], $alltables)) {
840 $one_table, $svg->getFont(), $svg->getFontSize(),
841 $master_field, $rel['foreign_table'],
842 $rel['foreign_field'], $this->tableDimension
848 if ($seen_a_relation) {
849 $this->_drawRelations($this->showColor
);
852 $this->_drawTables($this->showColor
);
854 $svg->showOutput($db.'-'.$this->pageNumber
);
859 * Sets X and Y minimum and maximum for a table cell
861 * @param string $table The table name
866 private function _setMinMax($table)
868 $this->_xMax
= max($this->_xMax
, $table->x +
$table->width
);
869 $this->_yMax
= max($this->_yMax
, $table->y +
$table->height
);
870 $this->_xMin
= min($this->_xMin
, $table->x
);
871 $this->_yMin
= min($this->_yMin
, $table->y
);
875 * Defines relation objects
877 * @param string $masterTable The master table name
878 * @param string $font The font face
879 * @param int $fontSize Font size
880 * @param string $masterField The relation field in the master table
881 * @param string $foreignTable The foreign table name
882 * @param string $foreignField The relation field in the foreign table
883 * @param boolean $showInfo Whether to display table position or not
888 * @see _setMinMax,Table_Stats::__construct(),Relation_Stats::__construct()
890 private function _addRelation($masterTable,$font,$fontSize, $masterField,
891 $foreignTable, $foreignField, $showInfo)
893 if (! isset($this->tables
[$masterTable])) {
894 $this->tables
[$masterTable] = new Table_Stats(
895 $masterTable, $font, $fontSize, $this->pageNumber
,
896 $this->_tablewidth
, false, $showInfo
898 $this->_setMinMax($this->tables
[$masterTable]);
900 if (! isset($this->tables
[$foreignTable])) {
901 $this->tables
[$foreignTable] = new Table_Stats(
902 $foreignTable, $font, $fontSize, $this->pageNumber
,
903 $this->_tablewidth
, false, $showInfo
905 $this->_setMinMax($this->tables
[$foreignTable]);
907 $this->_relations
[] = new Relation_Stats(
908 $this->tables
[$masterTable], $masterField,
909 $this->tables
[$foreignTable], $foreignField
914 * Draws relation arrows and lines
915 * connects master table's master field to
916 * foreign table's forein field
918 * @param boolean $changeColor Whether to use one color per relation or not
923 * @see Relation_Stats::relationDraw()
925 private function _drawRelations($changeColor)
927 foreach ($this->_relations
as $relation) {
928 $relation->relationDraw($changeColor);
935 * @param boolean $changeColor Whether to show color for primary fields or not
940 * @see Table_Stats::Table_Stats_tableDraw()
942 private function _drawTables($changeColor)
944 foreach ($this->tables
as $table) {
945 $table->tableDraw($changeColor);