2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 include_once("Export_Relation_Schema.class.php");
11 * This Class is EPS Library and
12 * helps in developing structure of EPS Schema Export
15 * @see http://php.net/manual/en/book.xmlwriter.php
22 public $stringCommands;
25 * The "PMA_EPS" constructor
27 * Upon instantiation This starts writing the EPS Document.
28 * %!PS-Adobe-3.0 EPSF-3.0 This is the MUST first comment to include
29 * it shows/tells that the Post Script document is purely under
30 * Document Structuring Convention [DSC] and is Compliant
31 * Encapsulated Post Script Document
36 function __construct()
38 $this->stringCommands
= "";
39 $this->stringCommands
.= "%!PS-Adobe-3.0 EPSF-3.0 \n";
45 * @param string value sets the title text
49 function setTitle($value)
51 $this->stringCommands
.= '%%Title: ' . $value . "\n";
57 * @param string value sets the author
61 function setAuthor($value)
63 $this->stringCommands
.= '%%Creator: ' . $value . "\n";
67 * Set document creation date
69 * @param string value sets the date
73 function setDate($value)
75 $this->stringCommands
.= '%%CreationDate: ' . $value . "\n";
79 * Set document orientation
81 * @param string value sets the author
85 function setOrientation($value)
87 $this->stringCommands
.= "%%PageOrder: Ascend \n";
90 $this->stringCommands
.= '%%Orientation: ' . $value . "\n";
93 $this->stringCommands
.= '%%Orientation: ' . $value . "\n";
95 $this->stringCommands
.= "%%EndComments \n";
96 $this->stringCommands
.= "%%Pages 1 \n";
97 $this->stringCommands
.= "%%BoundingBox: 72 150 144 170 \n";
101 * Set the font and size
103 * font can be set whenever needed in EPS
105 * @param string value sets the font name e.g Arial
106 * @param integer value sets the size of the font e.g 10
110 function setFont($value,$size)
112 $this->font
= $value;
113 $this->fontSize
= $size;
114 $this->stringCommands
.= "/".$value." findfont % Get the basic font\n";
115 $this->stringCommands
.= "".$size." scalefont % Scale the font to $size points\n";
116 $this->stringCommands
.= "setfont % Make it the current font\n";
122 * @return string return the font name e.g Arial
133 * @return string return the size of the font e.g 10
136 function getFontSize()
138 return $this->fontSize
;
144 * drawing the lines from x,y source to x,y destination and set the
145 * width of the line. lines helps in showing relationships of tables
147 * @param integer x_from The x_from attribute defines the start
148 left position of the element
149 * @param integer y_from The y_from attribute defines the start
150 right position of the element
151 * @param integer x_to The x_to attribute defines the end
152 left position of the element
153 * @param integer y_to The y_to attribute defines the end
154 right position of the element
155 * @param integer lineWidth sets the width of the line e.g 2
159 function line($x_from=0, $y_from=0, $x_to=0, $y_to=0, $lineWidth=0)
161 $this->stringCommands
.= $lineWidth . " setlinewidth \n";
162 $this->stringCommands
.= $x_from . ' ' . $y_from . " moveto \n";
163 $this->stringCommands
.= $x_to . ' ' . $y_to . " lineto \n";
164 $this->stringCommands
.= "stroke \n";
170 * drawing the rectangle from x,y source to x,y destination and set the
171 * width of the line. rectangles drawn around the text shown of fields
173 * @param integer x_from The x_from attribute defines the start
174 left position of the element
175 * @param integer y_from The y_from attribute defines the start
176 right position of the element
177 * @param integer x_to The x_to attribute defines the end
178 left position of the element
179 * @param integer y_to The y_to attribute defines the end
180 right position of the element
181 * @param integer lineWidth sets the width of the line e.g 2
185 function rect($x_from, $y_from, $x_to, $y_to, $lineWidth)
187 $this->stringCommands
.= $lineWidth . " setlinewidth \n";
188 $this->stringCommands
.= "newpath \n";
189 $this->stringCommands
.= $x_from . " " . $y_from . " moveto \n";
190 $this->stringCommands
.= "0 " . $y_to . " rlineto \n";
191 $this->stringCommands
.= $x_to . " 0 rlineto \n";
192 $this->stringCommands
.= "0 -" . $y_to . " rlineto \n";
193 $this->stringCommands
.= "closepath \n";
194 $this->stringCommands
.= "stroke \n";
198 * Set the current point
200 * The moveto operator takes two numbers off the stack and treats
201 * them as x and y coordinates to which to move. The coordinates
202 * specified become the current point.
204 * @param integer x The x attribute defines the
205 left position of the element
206 * @param integer y The y attribute defines the
207 right position of the element
211 function moveTo($x, $y)
213 $this->stringCommands
.= $x . ' ' . $y . " moveto \n";
217 * Output/Display the text
219 * @param string text The string to be displayed
225 $this->stringCommands
.= '(' . $text . ") show \n";
229 * Output the text at specified co-ordinates
231 * @param string text The string to be displayed
232 * @param integer x The x attribute defines the
233 left position of the element
234 * @param integer y The y attribute defines the
235 right position of the element
239 function showXY($text, $x, $y)
241 $this->moveTo($x, $y);
246 * get width of string/text
248 * EPS text width is calcualted depending on font name
249 * and font size. It is very important to know the width of text
250 * because rectangle is drawn around it.
252 * This is a bit hardcore method. I didn't found any other better than this.
253 * if someone found better than this. would love to hear that method
255 * @param string text string that width will be calculated
256 * @param integer font name of the font like Arial,sans-serif etc
257 * @param integer fontSize size of font
258 * @return integer width of the text
261 function getStringWidth($text,$font,$fontSize)
264 * Start by counting the width, giving each character a modifying value
267 $count = $count +
((strlen($text) - strlen(str_replace(array("i","j","l"),"",$text)))*0.23);//ijl
268 $count = $count +
((strlen($text) - strlen(str_replace(array("f"),"",$text)))*0.27);//f
269 $count = $count +
((strlen($text) - strlen(str_replace(array("t","I"),"",$text)))*0.28);//tI
270 $count = $count +
((strlen($text) - strlen(str_replace(array("r"),"",$text)))*0.34);//r
271 $count = $count +
((strlen($text) - strlen(str_replace(array("1"),"",$text)))*0.49);//1
272 $count = $count +
((strlen($text) - strlen(str_replace(array("c","k","s","v","x","y","z","J"),"",$text)))*0.5);//cksvxyzJ
273 $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
274 $count = $count +
((strlen($text) - strlen(str_replace(array("F","T","Z"),"",$text)))*0.61);//FTZ
275 $count = $count +
((strlen($text) - strlen(str_replace(array("A","B","E","K","P","S","V","X","Y"),"",$text)))*0.67);//ABEKPSVXY
276 $count = $count +
((strlen($text) - strlen(str_replace(array("w","C","D","H","N","R","U"),"",$text)))*0.73);//wCDHNRU
277 $count = $count +
((strlen($text) - strlen(str_replace(array("G","O","Q"),"",$text)))*0.78);//GOQ
278 $count = $count +
((strlen($text) - strlen(str_replace(array("m","M"),"",$text)))*0.84);//mM
279 $count = $count +
((strlen($text) - strlen(str_replace("W","",$text)))*.95);//W
280 $count = $count +
((strlen($text) - strlen(str_replace(" ","",$text)))*.28);//" "
281 $text = str_replace(" ","",$text);//remove the " "'s
282 $count = $count +
(strlen(preg_replace("/[a-z0-9]/i","",$text))*0.3); //all other chrs
285 $font = strtolower($font);
288 * no modifier for arial and sans-serif
294 * .92 modifer for time, serif, brushscriptstd, and californian fb
298 case 'brushscriptstd':
299 case 'californian fb':
303 * 1.23 modifier for broadway
309 $textWidth = $count*$fontSize;
310 return ceil($textWidth*$modifier);
321 $this->stringCommands
.= "showpage \n";
325 * Output EPS Document for download
327 * @param string fileName name of the eps document
331 function showOutput($fileName)
333 // if(ob_get_clean()){
336 $output = $this->stringCommands
;
337 PMA_download_header($fileName . '.eps', 'image/x-eps', strlen($output));
343 * Table preferences/statistics
345 * This class preserves the table co-ordinates,fields
346 * and helps in drawing/generating the Tables in EPS.
358 private $_showInfo = false;
362 public $fields = array();
363 public $heightCell = 0;
364 public $currentCell = 0;
366 public $primary = array();
369 * The "Table_Stats" constructor
371 * @param string tableName The table name
372 * @param string font The font name
373 * @param integer fontSize The font size
374 * @param integer same_wide_width The max width among tables
375 * @param boolean showKeys Whether to display keys or not
376 * @param boolean showInfo Whether to display table position or not
377 * @global object The current eps document
378 * @global integer The current page number (from the
379 * $cfg['Servers'][$i]['table_coords'] table)
380 * @global array The relations settings
381 * @global string The current db name
383 * @see PMA_EPS, Table_Stats::Table_Stats_setWidth,
384 Table_Stats::Table_Stats_setHeight
386 function __construct($tableName, $font, $fontSize, $pageNumber, &$same_wide_width, $showKeys = false, $showInfo = false)
388 global $eps, $cfgRelation, $db;
390 $this->_tableName
= $tableName;
391 $sql = 'DESCRIBE ' . PMA_backquote($tableName);
392 $result = PMA_DBI_try_query($sql, null, PMA_DBI_QUERY_STORE
);
393 if (!$result ||
!PMA_DBI_num_rows($result)) {
394 $eps->dieSchema($pageNumber,"EPS",sprintf(__('The %s table doesn\'t exist!'), $tableName));
399 * 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($all_columns, array_flip(array_keys($index->getColumns())));
407 $this->fields
= array_keys($all_columns);
409 while ($row = PMA_DBI_fetch_row($result)) {
410 $this->fields
[] = $row[0];
414 $this->_showInfo
= $showInfo;
417 $this->_setHeightTable($fontSize);
419 // setWidth must me after setHeight, because title
420 // can include table height which changes table width
421 $this->_setWidthTable($font,$fontSize);
422 if ($same_wide_width < $this->width
) {
423 $same_wide_width = $this->width
;
427 $sql = 'SELECT x, y FROM '
428 . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_coords'])
429 . ' WHERE db_name = \'' . PMA_sqlAddSlashes($db) . '\''
430 . ' AND table_name = \'' . PMA_sqlAddSlashes($tableName) . '\''
431 . ' AND pdf_page_number = ' . $pageNumber;
432 $result = PMA_query_as_controluser($sql, false, PMA_DBI_QUERY_STORE
);
434 if (!$result ||
!PMA_DBI_num_rows($result)) {
435 $eps->dieSchema($pageNumber,"EPS",sprintf(__('Please configure the coordinates for table %s'), $tableName));
437 list($this->x
, $this->y
) = PMA_DBI_fetch_row($result);
438 $this->x
= (double) $this->x
;
439 $this->y
= (double) $this->y
;
441 $this->displayfield
= PMA_getDisplayField($db, $tableName);
443 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($tableName) . ';', null, PMA_DBI_QUERY_STORE
);
444 if (PMA_DBI_num_rows($result) > 0) {
445 while ($row = PMA_DBI_fetch_assoc($result)) {
446 if ($row['Key_name'] == 'PRIMARY') {
447 $this->primary
[] = $row['Column_name'];
454 * Returns title of the current table,
455 * title can have the dimensions/co-ordinates of the table
457 * @return string The relation/table name
460 private function _getTitle()
462 return ($this->_showInfo ?
sprintf('%.0f', $this->width
) . 'x' . sprintf('%.0f', $this->heightCell
) : '') . ' ' . $this->_tableName
;
466 * Sets the width of the table
468 * @param string font The font name
469 * @param integer fontSize The font size
470 * @global object The current eps document
475 private function _setWidthTable($font,$fontSize)
479 foreach ($this->fields
as $field) {
480 $this->width
= max($this->width
, $eps->getStringWidth($field,$font,$fontSize));
482 $this->width +
= $eps->getStringWidth(' ',$font,$fontSize);
484 * it is unknown what value must be added, because
485 * table title is affected by the tabe width value
487 while ($this->width
< $eps->getStringWidth($this->_getTitle(),$font,$fontSize)) {
493 * Sets the height of the table
495 * @param integer fontSize The font size
499 private function _setHeightTable($fontSize)
501 $this->heightCell
= $fontSize +
4;
502 $this->height
= (count($this->fields
) +
1) * $this->heightCell
;
508 * @param boolean showColor Whether to display color
509 * @global object The current eps document
512 * @see PMA_EPS,PMA_EPS::line,PMA_EPS::rect
514 public function tableDraw($showColor)
517 //echo $this->_tableName.'<br />';
518 $eps->rect($this->x
,$this->y +
12,
519 $this->width
,$this->heightCell
,
522 $eps->showXY($this->_getTitle(),$this->x +
5,$this->y +
14);
523 foreach ($this->fields
as $field) {
524 $this->currentCell +
= $this->heightCell
;
527 if (in_array($field, $this->primary
)) {
530 if ($field == $this->displayfield
) {
534 $eps->rect($this->x
,$this->y +
12 +
$this->currentCell
,
535 $this->width
, $this->heightCell
,1);
536 $eps->showXY($field, $this->x +
5, $this->y +
14 +
$this->currentCell
);
542 * Relation preferences/statistics
544 * This class fetches the table master and foreign fields positions
545 * and helps in generating the Table references and then connects
546 * master table's master field to foreign table's foreign key
549 * @name Relation_Stats
560 public $xDest, $yDest;
564 * The "Relation_Stats" constructor
566 * @param string master_table The master table name
567 * @param string master_field The relation field in the master table
568 * @param string foreign_table The foreign table name
569 * @param string foreigh_field The relation field in the foreign table
570 * @see Relation_Stats::_getXy
572 function __construct($master_table, $master_field, $foreign_table, $foreign_field)
574 $src_pos = $this->_getXy($master_table, $master_field);
575 $dest_pos = $this->_getXy($foreign_table, $foreign_field);
581 $src_left = $src_pos[0] - $this->wTick
;
582 $src_right = $src_pos[1] +
$this->wTick
;
583 $dest_left = $dest_pos[0] - $this->wTick
;
584 $dest_right = $dest_pos[1] +
$this->wTick
;
586 $d1 = abs($src_left - $dest_left);
587 $d2 = abs($src_right - $dest_left);
588 $d3 = abs($src_left - $dest_right);
589 $d4 = abs($src_right - $dest_right);
590 $d = min($d1, $d2, $d3, $d4);
593 $this->xSrc
= $src_pos[0];
595 $this->xDest
= $dest_pos[0];
597 } elseif ($d == $d2) {
598 $this->xSrc
= $src_pos[1];
600 $this->xDest
= $dest_pos[0];
602 } elseif ($d == $d3) {
603 $this->xSrc
= $src_pos[0];
605 $this->xDest
= $dest_pos[1];
608 $this->xSrc
= $src_pos[1];
610 $this->xDest
= $dest_pos[1];
613 $this->ySrc
= $src_pos[2] +
10;
614 $this->yDest
= $dest_pos[2] +
10;
618 * Gets arrows coordinates
620 * @param string table The current table name
621 * @param string column The relation column name
622 * @return array Arrows coordinates
625 private function _getXy($table, $column)
627 $pos = array_search($column, $table->fields
);
628 // x_left, x_right, y
629 return array($table->x
, $table->x +
$table->width
, $table->y +
($pos +
1.5) * $table->heightCell
);
633 * draws relation links and arrows
634 * shows foreign key relations
636 * @param boolean changeColor Whether to use one color per relation or not
637 * @global object The current EPS document
641 public function relationDraw($changeColor)
646 $listOfColors = array(
655 shuffle($listOfColors);
656 $color = $listOfColors[0];
660 // draw a line like -- to foreign field
661 $eps->line($this->xSrc
,$this->ySrc
,
662 $this->xSrc +
$this->srcDir
* $this->wTick
,$this->ySrc
,
665 // draw a line like -- to master field
666 $eps->line($this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
,
667 $this->xDest
, $this->yDest
,
670 // draw a line that connects to master field line and foreign field line
671 $eps->line($this->xSrc +
$this->srcDir
* $this->wTick
,$this->ySrc
,
672 $this->xDest +
$this->destDir
* $this->wTick
, $this->yDest
,
675 $root2 = 2 * sqrt(2);
676 $eps->line($this->xSrc +
$this->srcDir
* $this->wTick
* 0.75, $this->ySrc
,
677 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
678 $this->ySrc +
$this->wTick
/ $root2 ,
681 $eps->line($this->xSrc +
$this->srcDir
* $this->wTick
* 0.75, $this->ySrc
,
682 $this->xSrc +
$this->srcDir
* (0.75 - 1 / $root2) * $this->wTick
,
683 $this->ySrc
- $this->wTick
/ $root2 ,
686 $eps->line($this->xDest +
$this->destDir
* $this->wTick
/ 2 , $this->yDest
,
687 $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
688 $this->yDest +
$this->wTick
/ $root2 ,
690 $eps->line($this->xDest +
$this->destDir
* $this->wTick
/ 2 ,
691 $this->yDest
, $this->xDest +
$this->destDir
* (0.5 +
1 / $root2) * $this->wTick
,
692 $this->yDest
- $this->wTick
/ $root2 ,
698 * end of the "Relation_Stats" class
702 * EPS Relation Schema Class
704 * Purpose of this class is to generate the EPS Document
705 * which is used for representing the database diagrams.
706 * This class uses post script commands and with
707 * the combination of these commands actually helps in preparing EPS Document.
709 * This class inherits Export_Relation_Schema class has common functionality added
712 * @name Eps_Relation_Schema
714 class PMA_Eps_Relation_Schema
extends PMA_Export_Relation_Schema
716 private $tables = array();
717 private $_relations = array();
720 * The "PMA_EPS_Relation_Schema" constructor
722 * Upon instantiation This starts writing the EPS document
723 * user will be prompted for download as .eps extension
728 function __construct()
732 $this->setPageNumber($_POST['pdf_page_number']);
733 $this->setShowColor(isset($_POST['show_color']));
734 $this->setShowKeys(isset($_POST['show_keys']));
735 $this->setTableDimension(isset($_POST['show_table_dimension']));
736 $this->setAllTableSameWidth(isset($_POST['all_table_same_wide']));
737 $this->setOrientation($_POST['orientation']);
738 $this->setExportType($_POST['export_type']);
740 $eps = new PMA_EPS();
741 $eps->setTitle(sprintf(__('Schema of the %s database - Page %s'), $db, $this->pageNumber
));
742 $eps->setAuthor('phpMyAdmin ' . PMA_VERSION
);
743 $eps->setDate(date("j F Y, g:i a"));
744 $eps->setOrientation($this->orientation
);
745 $eps->setFont('Verdana','10');
749 $alltables = $this->getAllTables($db,$this->pageNumber
);
751 foreach ($alltables AS $table) {
752 if (! isset($this->tables
[$table])) {
753 $this->tables
[$table] = new Table_Stats($table,$eps->getFont(),$eps->getFontSize(), $this->pageNumber
, $this->_tablewidth
, $this->showKeys
, $this->tableDimension
);
756 if ($this->sameWide
) {
757 $this->tables
[$table]->width
= $this->_tablewidth
;
761 $seen_a_relation = false;
762 foreach ($alltables as $one_table) {
763 $exist_rel = PMA_getForeigners($db, $one_table, '', 'both');
765 $seen_a_relation = true;
766 foreach ($exist_rel as $master_field => $rel) {
767 /* put the foreign table on the schema only if selected
769 * (do not use array_search() because we would have to
770 * to do a === false and this is not PHP3 compatible)
772 if (in_array($rel['foreign_table'], $alltables)) {
773 $this->_addRelation($one_table,$eps->getFont(),$eps->getFontSize(), $master_field, $rel['foreign_table'], $rel['foreign_field'], $this->tableDimension
);
778 if ($seen_a_relation) {
779 $this->_drawRelations($this->showColor
);
782 $this->_drawTables($this->showColor
);
784 $eps->showOutput($db.'-'.$this->pageNumber
);
789 * Defines relation objects
791 * @param string masterTable The master table name
792 * @param string masterField The relation field in the master table
793 * @param string foreignTable The foreign table name
794 * @param string foreignField The relation field in the foreign table
795 * @param boolean showInfo Whether to display table position or not
798 * @see _setMinMax,Table_Stats::__construct(),Relation_Stats::__construct()
800 private function _addRelation($masterTable,$font,$fontSize, $masterField, $foreignTable, $foreignField, $showInfo)
802 if (! isset($this->tables
[$masterTable])) {
803 $this->tables
[$masterTable] = new Table_Stats($masterTable, $font, $fontSize, $this->pageNumber
, $this->_tablewidth
, false, $showInfo);
805 if (! isset($this->tables
[$foreignTable])) {
806 $this->tables
[$foreignTable] = new Table_Stats($foreignTable,$font,$fontSize,$this->pageNumber
, $this->_tablewidth
, false, $showInfo);
808 $this->_relations
[] = new Relation_Stats($this->tables
[$masterTable], $masterField, $this->tables
[$foreignTable], $foreignField);
812 * Draws relation arrows and lines
813 * connects master table's master field to
814 * foreign table's forein field
816 * @param boolean changeColor Whether to use one color per relation or not
819 * @see Relation_Stats::relationDraw()
821 private function _drawRelations($changeColor)
823 foreach ($this->_relations
as $relation) {
824 $relation->relationDraw($changeColor);
831 * @param boolean changeColor Whether to show color for primary fields or not
834 * @see Table_Stats::Table_Stats_tableDraw()
836 private function _drawTables($changeColor)
838 foreach ($this->tables
as $table) {
839 $table->tableDraw($changeColor);