2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Classes to create relation schema in SVG format.
8 if (! defined('PHPMYADMIN')) {
12 require_once 'libraries/plugins/schema/Export_Relation_Schema.class.php';
13 require_once 'libraries/plugins/schema/svg/RelationStatsSvg.class.php';
14 require_once 'libraries/plugins/schema/svg/TableStatsSvg.class.php';
15 require_once 'libraries/Font.class.php';
18 * This Class inherits the XMLwriter class and
19 * helps in developing structure of SVG Schema Export
23 * @see http://php.net/manual/en/book.xmlwriter.php
25 class PMA_SVG
extends XMLWriter
33 * The "PMA_SVG" constructor
35 * Upon instantiation This starts writing the Svg XML document
37 * @see XMLWriter::openMemory(),XMLWriter::setIndent(),XMLWriter::startDocument()
39 function __construct()
43 * Set indenting using three spaces,
44 * so output is formatted
47 $this->setIndent(true);
48 $this->setIndentString(' ');
50 * Create the XML document
53 $this->startDocument('1.0', 'UTF-8');
55 'svg', '-//W3C//DTD SVG 1.1//EN',
56 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'
64 * @param string $value sets the title text
69 function setTitle($value)
71 $this->title
= $value;
77 * @param string $value sets the author
82 function setAuthor($value)
84 $this->author
= $value;
90 * @param string $value sets the font e.g Arial, Sans-serif etc
95 function setFont($value)
103 * @return string returns the font name
112 * Set document font size
114 * @param string $value sets the font size in pixels
119 function setFontSize($value)
121 $this->fontSize
= $value;
125 * Get document font size
127 * @return string returns the font size
130 function getFontSize()
132 return $this->fontSize
;
136 * Starts Svg Document
138 * svg document starts by first initializing svg tag
139 * which contains all the attributes and namespace that needed
140 * to define the svg document
142 * @param integer $width total width of the Svg document
143 * @param integer $height total height of the Svg document
148 * @see XMLWriter::startElement(),XMLWriter::writeAttribute()
150 function startSvgDoc($width,$height)
152 $this->startElement('svg');
153 $this->writeAttribute('width', $width);
154 $this->writeAttribute('height', $height);
155 $this->writeAttribute('xmlns', 'http://www.w3.org/2000/svg');
156 $this->writeAttribute('version', '1.1');
164 * @see XMLWriter::endElement(),XMLWriter::endDocument()
169 $this->endDocument();
173 * output Svg Document
175 * svg document prompted to the user for download
176 * Svg document saved in .svg extension and can be
177 * easily changeable by using any svg IDE
179 * @param string $fileName file name
183 * @see XMLWriter::startElement(),XMLWriter::writeAttribute()
185 function showOutput($fileName)
188 $output = $this->flush();
189 PMA_Response
::getInstance()->disable();
193 /*overload*/mb_strlen($output)
201 * SVG has some predefined shape elements like rectangle & text
202 * and other elements who have x,y co-ordinates are drawn.
203 * specify their width and height and can give styles too.
205 * @param string $name Svg element name
206 * @param int $x The x attr defines the left position of the element
207 * (e.g. x="0" places the element 0 pixels from the left of the browser window)
208 * @param integer $y The y attribute defines the top position of the
209 * element (e.g. y="0" places the element 0 pixels from the top of the browser
211 * @param int|string $width The width attribute defines the width the element
212 * @param int|string $height The height attribute defines the height the element
213 * @param string $text The text attribute defines the text the element
214 * @param string $styles The style attribute defines the style the element
215 * styles can be defined like CSS styles
220 * @see XMLWriter::startElement(), XMLWriter::writeAttribute(),
221 * XMLWriter::text(), XMLWriter::endElement()
223 function printElement($name, $x, $y, $width = '', $height = '',
224 $text = '', $styles = ''
226 $this->startElement($name);
227 $this->writeAttribute('width', $width);
228 $this->writeAttribute('height', $height);
229 $this->writeAttribute('x', $x);
230 $this->writeAttribute('y', $y);
231 $this->writeAttribute('style', $styles);
233 $this->writeAttribute('font-family', $this->font
);
234 $this->writeAttribute('font-size', $this->fontSize
);
241 * Draws Svg Line element
243 * Svg line element is drawn for connecting the tables.
244 * arrows are also drawn by specify its start and ending
247 * @param string $name Svg element name i.e line
248 * @param integer $x1 Defines the start of the line on the x-axis
249 * @param integer $y1 Defines the start of the line on the y-axis
250 * @param integer $x2 Defines the end of the line on the x-axis
251 * @param integer $y2 Defines the end of the line on the y-axis
252 * @param string $styles The style attribute defines the style the element
253 * styles can be defined like CSS styles
258 * @see XMLWriter::startElement(), XMLWriter::writeAttribute(),
259 * XMLWriter::endElement()
261 function printElementLine($name,$x1,$y1,$x2,$y2,$styles)
263 $this->startElement($name);
264 $this->writeAttribute('x1', $x1);
265 $this->writeAttribute('y1', $y1);
266 $this->writeAttribute('x2', $x2);
267 $this->writeAttribute('y2', $y2);
268 $this->writeAttribute('style', $styles);
274 * Svg Relation Schema Class
276 * Purpose of this class is to generate the SVG XML Document because
277 * SVG defines the graphics in XML format which is used for representing
278 * the database diagrams as vector image. This class actually helps
279 * in preparing SVG XML format.
281 * SVG XML is generated by using XMLWriter php extension and this class
282 * inherits Export_Relation_Schema class has common functionality added
285 * @package PhpMyAdmin
286 * @name Svg_Relation_Schema
288 class PMA_Svg_Relation_Schema
extends PMA_Export_Relation_Schema
291 private $_tables = array();
292 private $_relations = array();
295 private $_xMin = 100000;
296 private $_yMin = 100000;
297 private $_tablewidth;
300 * The "PMA_Svg_Relation_Schema" constructor
302 * Upon instantiation This starts writing the SVG XML document
303 * user will be prompted for download as .svg extension
307 function __construct()
309 parent
::__construct();
313 $this->setShowColor(isset($_REQUEST['svg_show_color']));
314 $this->setShowKeys(isset($_REQUEST['svg_show_keys']));
315 $this->setTableDimension(isset($_REQUEST['svg_show_table_dimension']));
316 $this->setAllTablesSameWidth(isset($_REQUEST['svg_all_tables_same_width']));
318 $svg = new PMA_SVG();
321 __('Schema of the %s database - Page %s'),
326 $svg->SetAuthor('phpMyAdmin ' . PMA_VERSION
);
327 $svg->setFont('Arial');
328 $svg->setFontSize('16px');
329 $svg->startSvgDoc('1000px', '1000px');
331 $alltables = $this->getTablesFromRequest();
333 foreach ($alltables as $table) {
334 if (! isset($this->_tables
[$table])) {
335 $this->_tables
[$table] = new Table_Stats_Svg(
336 $table, $svg->getFont(), $svg->getFontSize(), $this->pageNumber
,
337 $this->_tablewidth
, $this->showKeys
, $this->tableDimension
,
342 if ($this->sameWide
) {
343 $this->_tables
[$table]->width
= $this->_tablewidth
;
345 $this->_setMinMax($this->_tables
[$table]);
347 $seen_a_relation = false;
348 foreach ($alltables as $one_table) {
349 $exist_rel = PMA_getForeigners($GLOBALS['db'], $one_table, '', 'both');
354 $seen_a_relation = true;
355 foreach ($exist_rel as $master_field => $rel) {
356 /* put the foreign table on the schema only if selected
358 * (do not use array_search() because we would have to
359 * to do a === false and this is not PHP3 compatible)
361 if ($master_field != 'foreign_keys_data') {
362 if (in_array($rel['foreign_table'], $alltables)) {
364 $one_table, $svg->getFont(), $svg->getFontSize(),
365 $master_field, $rel['foreign_table'],
366 $rel['foreign_field'], $this->tableDimension
372 foreach ($rel as $one_key) {
373 if (!in_array($one_key['ref_table_name'], $alltables)) {
377 foreach ($one_key['index_list']
378 as $index => $one_field
381 $one_table, $svg->getFont(),
383 $one_field, $one_key['ref_table_name'],
384 $one_key['ref_index_list'][$index],
385 $this->tableDimension
391 if ($seen_a_relation) {
392 $this->_drawRelations();
395 $this->_drawTables();
400 * Output Svg Document for download
405 function showOutput()
408 $svg->showOutput($this->getFileName('.svg'));
413 * Sets X and Y minimum and maximum for a table cell
415 * @param string $table The table name
420 private function _setMinMax($table)
422 $this->_xMax
= max($this->_xMax
, $table->x +
$table->width
);
423 $this->_yMax
= max($this->_yMax
, $table->y +
$table->height
);
424 $this->_xMin
= min($this->_xMin
, $table->x
);
425 $this->_yMin
= min($this->_yMin
, $table->y
);
429 * Defines relation objects
431 * @param string $masterTable The master table name
432 * @param string $font The font face
433 * @param int $fontSize Font size
434 * @param string $masterField The relation field in the master table
435 * @param string $foreignTable The foreign table name
436 * @param string $foreignField The relation field in the foreign table
437 * @param boolean $tableDimension Whether to display table position or not
442 * @see _setMinMax,Table_Stats_Svg::__construct(),
443 * Relation_Stats_Svg::__construct()
445 private function _addRelation(
446 $masterTable,$font,$fontSize, $masterField,
447 $foreignTable, $foreignField, $tableDimension
449 if (! isset($this->_tables
[$masterTable])) {
450 $this->_tables
[$masterTable] = new Table_Stats_Svg(
451 $masterTable, $font, $fontSize, $this->pageNumber
,
452 $this->_tablewidth
, false, $tableDimension
454 $this->_setMinMax($this->_tables
[$masterTable]);
456 if (! isset($this->_tables
[$foreignTable])) {
457 $this->_tables
[$foreignTable] = new Table_Stats_Svg(
458 $foreignTable, $font, $fontSize, $this->pageNumber
,
459 $this->_tablewidth
, false, $tableDimension
461 $this->_setMinMax($this->_tables
[$foreignTable]);
463 $this->_relations
[] = new Relation_Stats_Svg(
464 $this->_tables
[$masterTable], $masterField,
465 $this->_tables
[$foreignTable], $foreignField
470 * Draws relation arrows and lines
471 * connects master table's master field to
472 * foreign table's foreign field
477 * @see Relation_Stats_Svg::relationDraw()
479 private function _drawRelations()
481 foreach ($this->_relations
as $relation) {
482 $relation->relationDraw($this->showColor
);
492 * @see Table_Stats_Svg::Table_Stats_tableDraw()
494 private function _drawTables()
496 foreach ($this->_tables
as $table) {
497 $table->tableDraw($this->showColor
);