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 SVG Schema Export
18 * @see http://php.net/manual/en/book.xmlwriter.php
20 class PMA_SVG
extends XMLWriter
28 * The "PMA_SVG" constructor
30 * Upon instantiation This starts writing the Svg 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');
51 'svg', '-//W3C//DTD SVG 1.1//EN',
52 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'
60 * @param string $value sets the title text
65 function setTitle($value)
67 $this->title
= $value;
73 * @param string $value sets the author
78 function setAuthor($value)
80 $this->author
= $value;
86 * @param string $value sets the font e.g Arial, Sans-serif etc
91 function setFont($value)
99 * @return string returns the font name
108 * Set document font size
110 * @param string $value sets the font size in pixels
115 function setFontSize($value)
117 $this->fontSize
= $value;
121 * Get document font size
123 * @return string returns the font size
126 function getFontSize()
128 return $this->fontSize
;
132 * Starts Svg Document
134 * svg document starts by first initializing svg tag
135 * which contains all the attributes and namespace that needed
136 * to define the svg document
138 * @param integer $width total width of the Svg document
139 * @param integer $height total height of the Svg document
144 * @see XMLWriter::startElement(),XMLWriter::writeAttribute()
146 function startSvgDoc($width,$height)
148 $this->startElement('svg');
149 $this->writeAttribute('width', $width);
150 $this->writeAttribute('height', $height);
151 $this->writeAttribute('xmlns', 'http://www.w3.org/2000/svg');
152 $this->writeAttribute('version', '1.1');
160 * @see XMLWriter::endElement(),XMLWriter::endDocument()
165 $this->endDocument();
169 * output Svg Document
171 * svg document prompted to the user for download
172 * Svg document saved in .svg extension and can be
173 * easily changeable by using any svg IDE
175 * @param string $fileName file name
179 * @see XMLWriter::startElement(),XMLWriter::writeAttribute()
181 function showOutput($fileName)
184 $output = $this->flush();
185 PMA_Response
::getInstance()->disable();
186 PMA_downloadHeader($fileName . '.svg', 'image/svg+xml', strlen($output));
193 * SVG has some predefined shape elements like rectangle & text
194 * and other elements who have x,y co-ordinates are drawn.
195 * specify their width and height and can give styles too.
197 * @param string $name Svg element name
198 * @param integer $x The x attr defines the left position of the element
199 * (e.g. x="0" places the element 0 pixels from the left of the browser window)
200 * @param integer $y The y attribute defines the top position of the element
201 * (e.g. y="0" places the element 0 pixels from the top of the browser window)
202 * @param integer $width The width attribute defines the width the element
203 * @param integer $height The height attribute defines the height the element
204 * @param string $text The text attribute defines the text the element
205 * @param string $styles The style attribute defines the style the element
206 * styles can be defined like CSS styles
211 * @see XMLWriter::startElement(), XMLWriter::writeAttribute(),
212 * XMLWriter::text(), XMLWriter::endElement()
214 function printElement($name, $x, $y, $width = '', $height = '', $text = '', $styles = '')
216 $this->startElement($name);
217 $this->writeAttribute('width', $width);
218 $this->writeAttribute('height', $height);
219 $this->writeAttribute('x', $x);
220 $this->writeAttribute('y', $y);
221 $this->writeAttribute('style', $styles);
223 $this->writeAttribute('font-family', $this->font
);
224 $this->writeAttribute('font-size', $this->fontSize
);
231 * Draws Svg Line element
233 * Svg line element is drawn for connecting the tables.
234 * arrows are also drawn by specify its start and ending
237 * @param string $name Svg element name i.e line
238 * @param integer $x1 Defines the start of the line on the x-axis
239 * @param integer $y1 Defines the start of the line on the y-axis
240 * @param integer $x2 Defines the end of the line on the x-axis
241 * @param integer $y2 Defines the end of the line on the y-axis
242 * @param string $styles The style attribute defines the style the element
243 * styles can be defined like CSS styles
248 * @see XMLWriter::startElement(), XMLWriter::writeAttribute(),
249 * XMLWriter::endElement()
251 function printElementLine($name,$x1,$y1,$x2,$y2,$styles)
253 $this->startElement($name);
254 $this->writeAttribute('x1', $x1);
255 $this->writeAttribute('y1', $y1);
256 $this->writeAttribute('x2', $x2);
257 $this->writeAttribute('y2', $y2);
258 $this->writeAttribute('style', $styles);
263 * get width of string/text
265 * Svg text element width is calcualted depending on font name
266 * and font size. It is very important to know the width of text
267 * because rectangle is drawn around it.
269 * This is a bit hardcore method. I didn't found any other than this.
271 * @param string $text string that width will be calculated
272 * @param integer $font name of the font like Arial,sans-serif etc
273 * @param integer $fontSize size of font
275 * @return integer width of the text
278 function getStringWidth($text,$font,$fontSize)
281 * Start by counting the width, giving each character a modifying value
284 $count = $count +
((strlen($text) - strlen(str_replace(array("i", "j", "l"), "", $text))) * 0.23);//ijl
285 $count = $count +
((strlen($text) - strlen(str_replace(array("f"), "", $text))) * 0.27);//f
286 $count = $count +
((strlen($text) - strlen(str_replace(array("t", "I"), "", $text))) * 0.28);//tI
287 $count = $count +
((strlen($text) - strlen(str_replace(array("r"), "", $text))) * 0.34);//r
288 $count = $count +
((strlen($text) - strlen(str_replace(array("1"), "", $text))) * 0.49);//1
289 $count = $count +
((strlen($text) - strlen(str_replace(array("c", "k", "s", "v", "x", "y", "z", "J"), "", $text))) * 0.5);//cksvxyzJ
290 $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
291 $count = $count +
((strlen($text) - strlen(str_replace(array("F", "T", "Z"), "", $text))) * 0.61);//FTZ
292 $count = $count +
((strlen($text) - strlen(str_replace(array("A", "B", "E", "K", "P", "S", "V", "X", "Y"), "", $text))) * 0.67);//ABEKPSVXY
293 $count = $count +
((strlen($text) - strlen(str_replace(array("w", "C", "D", "H", "N", "R", "U"), "", $text))) * 0.73);//wCDHNRU
294 $count = $count +
((strlen($text) - strlen(str_replace(array("G", "O", "Q"), "", $text))) * 0.78);//GOQ
295 $count = $count +
((strlen($text) - strlen(str_replace(array("m", "M"), "", $text))) * 0.84);//mM
296 $count = $count +
((strlen($text) - strlen(str_replace("W", "", $text))) * .95);//W
297 $count = $count +
((strlen($text) - strlen(str_replace(" ", "", $text))) * .28);//" "
298 $text = str_replace(" ", "", $text);//remove the " "'s
299 $count = $count +
(strlen(preg_replace("/[a-z0-9]/i", "", $text)) * 0.3); //all other chrs
302 $font = strtolower($font);
305 * no modifier for arial and sans-serif
311 * .92 modifer for time, serif, brushscriptstd, and californian fb
315 case 'brushscriptstd':
316 case 'californian fb':
320 * 1.23 modifier for broadway
326 $textWidth = $count*$fontSize;
327 return ceil($textWidth*$modifier);
332 * Table preferences/statistics
334 * This class preserves the table co-ordinates,fields
335 * and helps in drawing/generating the Tables in SVG XML document.
347 private $_showInfo = false;
351 public $fields = array();
352 public $heightCell = 0;
353 public $currentCell = 0;
355 public $primary = array();
358 * The "Table_Stats" constructor
360 * @param string $tableName The table name
361 * @param string $font Font face
362 * @param integer $fontSize The font size
363 * @param integer $pageNumber Page number
364 * @param integer &$same_wide_width The max. with among tables
365 * @param boolean $showKeys Whether to display keys or not
366 * @param boolean $showInfo Whether to display table position or not
368 * @global object The current SVG image document
369 * @global integer The current page number (from the
370 * $cfg['Servers'][$i]['table_coords'] table)
371 * @global array The relations settings
372 * @global string The current db name
376 * @see PMA_SVG, Table_Stats::Table_Stats_setWidth,
377 * Table_Stats::Table_Stats_setHeight
379 function __construct(
380 $tableName, $font, $fontSize, $pageNumber,
381 &$same_wide_width, $showKeys = false, $showInfo = false
383 global $svg, $cfgRelation, $db;
385 $this->_tableName
= $tableName;
386 $sql = 'DESCRIBE ' . PMA_Util
::backquote($tableName);
387 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
388 if (! $result ||
! PMA_DBI_num_rows($result)) {
392 sprintf(__('The %s table doesn\'t exist!'), $tableName)
398 * check to see if it will load all fields or only the foreign keys
402 $indexes = PMA_Index
::getFromTable($this->_tableName
, $db);
403 $all_columns = array();
404 foreach ($indexes as $index) {
405 $all_columns = array_merge(
407 array_flip(array_keys($index->getColumns()))
410 $this->fields
= array_keys($all_columns);
412 while ($row = PMA_DBI_fetch_row($result)) {
413 $this->fields
[] = $row[0];
417 $this->_showInfo
= $showInfo;
420 $this->_setHeightTable($fontSize);
422 // setWidth must me after setHeight, because title
423 // can include table height which changes table width
424 $this->_setWidthTable($font, $fontSize);
425 if ($same_wide_width < $this->width
) {
426 $same_wide_width = $this->width
;
430 $sql = 'SELECT x, y FROM '
431 . PMA_Util
::backquote($GLOBALS['cfgRelation']['db']) . '.'
432 . PMA_Util
::backquote($cfgRelation['table_coords'])
433 . ' WHERE db_name = \'' . PMA_Util
::sqlAddSlashes($db) . '\''
434 . ' AND table_name = \'' . PMA_Util
::sqlAddSlashes($tableName) . '\''
435 . ' AND pdf_page_number = ' . $pageNumber;
436 $result = PMA_queryAsControlUser($sql, false, PMA_DBI_QUERY_STORE
);
438 if (!$result ||
!PMA_DBI_num_rows($result)) {
443 __('Please configure the coordinates for table %s'),
448 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
449 $this->x
= (double) $this->x
;
450 $this->y
= (double) $this->y
;
452 $this->displayfield
= PMA_getDisplayField($db, $tableName);
454 $result = PMA_DBI_query(
455 'SHOW INDEX FROM ' . PMA_Util
::backquote($tableName) . ';',
459 if (PMA_DBI_num_rows($result) > 0) {
460 while ($row = PMA_DBI_fetch_assoc($result)) {
461 if ($row['Key_name'] == 'PRIMARY') {
462 $this->primary
[] = $row['Column_name'];
469 * Returns title of the current table,
470 * title can have the dimensions/co-ordinates of the table
474 private function _getTitle()
476 return ($this->_showInfo
477 ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->heightCell
)
479 ) . ' ' . $this->_tableName
;
483 * Sets the width of the table
485 * @param string $font The font size
486 * @param integer $fontSize The font size
488 * @global object The current SVG image document
495 private function _setWidthTable($font,$fontSize)
499 foreach ($this->fields
as $field) {
502 $svg->getStringWidth($field, $font, $fontSize)
505 $this->width +
= $svg->getStringWidth(' ', $font, $fontSize);
508 * it is unknown what value must be added, because
509 * table title is affected by the tabe width value
511 while ($this->width
< $svg->getStringWidth($this->_getTitle(), $font, $fontSize)) {
517 * Sets the height of the table
519 * @param integer $fontSize font size
524 function _setHeightTable($fontSize)
526 $this->heightCell
= $fontSize +
4;
527 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
533 * @param boolean $showColor Whether to display color
535 * @global object The current SVG image document
540 * @see PMA_SVG,PMA_SVG::printElement
542 public function tableDraw($showColor)
545 //echo $this->_tableName.'<br />';
547 'rect', $this->x
, $this->y
, $this->width
,
548 $this->heightCell
, null, 'fill:red;stroke:black;'
551 'text', $this->x +
5, $this->y+
14, $this->width
, $this->heightCell
,
552 $this->_getTitle(), 'fill:none;stroke:black;'
554 foreach ($this->fields
as $field) {
555 $this->currentCell +
= $this->heightCell
;
558 if (in_array($field, $this->primary
)) {
561 if ($field == $this->displayfield
) {
566 'rect', $this->x
, $this->y +
$this->currentCell
, $this->width
,
567 $this->heightCell
, null, 'fill:'.$showColor.';stroke:black;'
570 'text', $this->x +
5, $this->y +
14 +
$this->currentCell
,
571 $this->width
, $this->heightCell
, $field, 'fill:none;stroke:black;'
579 * Relation preferences/statistics
581 * This class fetches the table master and foreign fields positions
582 * and helps in generating the Table references and then connects
583 * master table's master field to foreign table's foreign key
584 * in SVG XML document.
586 * @name Relation_Stats
587 * @see PMA_SVG::printElementLine
597 public $xDest, $yDest;
601 * The "Relation_Stats" constructor
603 * @param string $master_table The master table name
604 * @param string $master_field The relation field in the master table
605 * @param string $foreign_table The foreign table name
606 * @param string $foreign_field The relation field in the foreign table
610 * @see Relation_Stats::_getXy
612 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
614 $src_pos = $this->_getXy($master_table, $master_field);
615 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
621 $src_left = $src_pos[0] - $this->wTick
;
622 $src_right = $src_pos[1] +
$this->wTick
;
623 $dest_left = $dest_pos[0] - $this->wTick
;
624 $dest_right = $dest_pos[1] +
$this->wTick
;
626 $d1 = abs($src_left - $dest_left);
627 $d2 = abs($src_right - $dest_left);
628 $d3 = abs($src_left - $dest_right);
629 $d4 = abs($src_right - $dest_right);
630 $d = min($d1, $d2, $d3, $d4);
633 $this->xSrc
= $src_pos[0];
635 $this->xDest
= $dest_pos[0];
637 } elseif ($d == $d2) {
638 $this->xSrc
= $src_pos[1];
640 $this->xDest
= $dest_pos[0];
642 } elseif ($d == $d3) {
643 $this->xSrc
= $src_pos[0];
645 $this->xDest
= $dest_pos[1];
648 $this->xSrc
= $src_pos[1];
650 $this->xDest
= $dest_pos[1];
653 $this->ySrc
= $src_pos[2];
654 $this->yDest
= $dest_pos[2];
658 * Gets arrows coordinates
660 * @param string $table The current table name
661 * @param string $column The relation column name
663 * @return array Arrows coordinates
666 function _getXy($table, $column)
668 $pos = array_search($column, $table->fields
);
669 // x_left, x_right, y
672 $table->x +
$table->width
,
673 $table->y +
($pos +
1.5) * $table->heightCell
678 * draws relation links and arrows shows foreign key relations
680 * @param boolean $changeColor Whether to use one color per relation or not
682 * @global object The current SVG image document
689 public function relationDraw($changeColor)
694 $listOfColors = array(
703 shuffle($listOfColors);
704 $color = $listOfColors[0];
709 $svg->printElementLine(
710 'line', $this->xSrc
, $this->ySrc
,
711 $this->xSrc +
$this->srcDir
* $this->wTick
, $this->ySrc
,
712 'fill:' . $color . ';stroke:black;stroke-width:2;'
714 $svg->printElementLine(
715 'line', $this->xDest +
$this->destDir
* $this->wTick
,
716 $this->yDest
, $this->xDest
, $this->yDest
,
717 'fill:' . $color . ';stroke:black;stroke-width:2;'
719 $svg->printElementLine(
720 'line', $this->xSrc +
$this->srcDir
* $this->wTick
, $this->ySrc
,
721 $this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
,
722 'fill:' . $color . ';stroke:' . $color . ';stroke-width:1;'
724 $root2 = 2 * sqrt(2);
725 $svg->printElementLine(
726 'line', $this->xSrc +
$this->srcDir
* $this->wTick
* 0.75, $this->ySrc
,
727 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
728 $this->ySrc +
$this->wTick
/ $root2,
729 'fill:' . $color . ';stroke:black;stroke-width:2;'
731 $svg->printElementLine(
732 'line', $this->xSrc +
$this->srcDir
* $this->wTick
* 0.75, $this->ySrc
,
733 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
734 $this->ySrc
- $this->wTick
/ $root2,
735 'fill:' . $color . ';stroke:black;stroke-width:2;'
737 $svg->printElementLine(
738 'line', $this->xDest +
$this->destDir
* $this->wTick
/ 2, $this->yDest
,
739 $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
740 $this->yDest +
$this->wTick
/ $root2,
741 'fill:' . $color . ';stroke:black;stroke-width:2;'
743 $svg->printElementLine(
744 'line', $this->xDest +
$this->destDir
* $this->wTick
/ 2, $this->yDest
,
745 $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
746 $this->yDest
- $this->wTick
/ $root2,
747 'fill:' . $color . ';stroke:black;stroke-width:2;'
752 * end of the "Relation_Stats" class
756 * Svg Relation Schema Class
758 * Purpose of this class is to generate the SVG XML Document because
759 * SVG defines the graphics in XML format which is used for representing
760 * the database diagrams as vector image. This class actually helps
761 * in preparing SVG XML format.
763 * SVG XML is generated by using XMLWriter php extension and this class
764 * inherits Export_Relation_Schema class has common functionality added
767 * @name Svg_Relation_Schema
769 class PMA_Svg_Relation_Schema
extends PMA_Export_Relation_Schema
772 private $_tables = array();
773 private $_relations = array();
776 private $_xMin = 100000;
777 private $_yMin = 100000;
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->setAllTablesSameWidth(isset($_POST['all_tables_same_width']));
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(
891 $masterTable,$font,$fontSize, $masterField,
892 $foreignTable, $foreignField, $showInfo
894 if (! isset($this->_tables
[$masterTable])) {
895 $this->_tables
[$masterTable] = new Table_Stats(
896 $masterTable, $font, $fontSize, $this->pageNumber
,
897 $this->_tablewidth
, false, $showInfo
899 $this->_setMinMax($this->_tables
[$masterTable]);
901 if (! isset($this->_tables
[$foreignTable])) {
902 $this->_tables
[$foreignTable] = new Table_Stats(
903 $foreignTable, $font, $fontSize, $this->pageNumber
,
904 $this->_tablewidth
, false, $showInfo
906 $this->_setMinMax($this->_tables
[$foreignTable]);
908 $this->_relations
[] = new Relation_Stats(
909 $this->_tables
[$masterTable], $masterField,
910 $this->_tables
[$foreignTable], $foreignField
915 * Draws relation arrows and lines
916 * connects master table's master field to
917 * foreign table's forein field
919 * @param boolean $changeColor Whether to use one color per relation or not
924 * @see Relation_Stats::relationDraw()
926 private function _drawRelations($changeColor)
928 foreach ($this->_relations
as $relation) {
929 $relation->relationDraw($changeColor);
936 * @param boolean $changeColor Whether to show color for primary fields or not
941 * @see Table_Stats::Table_Stats_tableDraw()
943 private function _drawTables($changeColor)
945 foreach ($this->_tables
as $table) {
946 $table->tableDraw($changeColor);