Update website
This commit is contained in:
parent
4413528994
commit
1d90fbf296
6865 changed files with 1091082 additions and 0 deletions
187
admin/phpMyAdmin/libraries/classes/Plugins/Schema/Dia/Dia.php
Normal file
187
admin/phpMyAdmin/libraries/classes/Plugins/Schema/Dia/Dia.php
Normal file
|
@ -0,0 +1,187 @@
|
|||
<?php
|
||||
/**
|
||||
* Classes to create relation schema in Dia format.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Dia;
|
||||
|
||||
use PhpMyAdmin\Core;
|
||||
use PhpMyAdmin\ResponseRenderer;
|
||||
use XMLWriter;
|
||||
|
||||
use function ob_end_clean;
|
||||
use function ob_get_clean;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* This Class inherits the XMLwriter class and
|
||||
* helps in developing structure of DIA Schema Export
|
||||
*
|
||||
* @see https://www.php.net/manual/en/book.xmlwriter.php
|
||||
*/
|
||||
class Dia extends XMLWriter
|
||||
{
|
||||
/**
|
||||
* Upon instantiation This starts writing the Dia XML document
|
||||
*
|
||||
* @see XMLWriter::openMemory()
|
||||
* @see XMLWriter::setIndent()
|
||||
* @see XMLWriter::startDocument()
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->openMemory();
|
||||
/*
|
||||
* Set indenting using three spaces,
|
||||
* so output is formatted
|
||||
*/
|
||||
$this->setIndent(true);
|
||||
$this->setIndentString(' ');
|
||||
/*
|
||||
* Create the XML document
|
||||
*/
|
||||
$this->startDocument('1.0', 'UTF-8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts Dia Document
|
||||
*
|
||||
* dia document starts by first initializing dia:diagram tag
|
||||
* then dia:diagramdata contains all the attributes that needed
|
||||
* to define the document, then finally a Layer starts which
|
||||
* holds all the objects.
|
||||
*
|
||||
* @see XMLWriter::startElement()
|
||||
* @see XMLWriter::writeAttribute()
|
||||
* @see XMLWriter::writeRaw()
|
||||
*
|
||||
* @param string $paper the size of the paper/document
|
||||
* @param float $topMargin top margin of the paper/document in cm
|
||||
* @param float $bottomMargin bottom margin of the paper/document in cm
|
||||
* @param float $leftMargin left margin of the paper/document in cm
|
||||
* @param float $rightMargin right margin of the paper/document in cm
|
||||
* @param string $orientation orientation of the document, portrait or landscape
|
||||
*/
|
||||
public function startDiaDoc(
|
||||
$paper,
|
||||
$topMargin,
|
||||
$bottomMargin,
|
||||
$leftMargin,
|
||||
$rightMargin,
|
||||
$orientation
|
||||
): void {
|
||||
$isPortrait = 'false';
|
||||
|
||||
if ($orientation === 'P') {
|
||||
$isPortrait = 'true';
|
||||
}
|
||||
|
||||
$this->startElement('dia:diagram');
|
||||
$this->writeAttribute('xmlns:dia', 'http://www.lysator.liu.se/~alla/dia/');
|
||||
$this->startElement('dia:diagramdata');
|
||||
$this->writeRaw(
|
||||
'<dia:attribute name="background">
|
||||
<dia:color val="#ffffff"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="pagebreak">
|
||||
<dia:color val="#000099"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="paper">
|
||||
<dia:composite type="paper">
|
||||
<dia:attribute name="name">
|
||||
<dia:string>#' . $paper . '#</dia:string>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="tmargin">
|
||||
<dia:real val="' . $topMargin . '"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="bmargin">
|
||||
<dia:real val="' . $bottomMargin . '"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="lmargin">
|
||||
<dia:real val="' . $leftMargin . '"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="rmargin">
|
||||
<dia:real val="' . $rightMargin . '"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="is_portrait">
|
||||
<dia:boolean val="' . $isPortrait . '"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="scaling">
|
||||
<dia:real val="1"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="fitto">
|
||||
<dia:boolean val="false"/>
|
||||
</dia:attribute>
|
||||
</dia:composite>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="grid">
|
||||
<dia:composite type="grid">
|
||||
<dia:attribute name="width_x">
|
||||
<dia:real val="1"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="width_y">
|
||||
<dia:real val="1"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="visible_x">
|
||||
<dia:int val="1"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="visible_y">
|
||||
<dia:int val="1"/>
|
||||
</dia:attribute>
|
||||
<dia:composite type="color"/>
|
||||
</dia:composite>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="color">
|
||||
<dia:color val="#d8e5e5"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="guides">
|
||||
<dia:composite type="guides">
|
||||
<dia:attribute name="hguides"/>
|
||||
<dia:attribute name="vguides"/>
|
||||
</dia:composite>
|
||||
</dia:attribute>'
|
||||
);
|
||||
$this->endElement();
|
||||
$this->startElement('dia:layer');
|
||||
$this->writeAttribute('name', 'Background');
|
||||
$this->writeAttribute('visible', 'true');
|
||||
$this->writeAttribute('active', 'true');
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends Dia Document
|
||||
*
|
||||
* @see XMLWriter::endElement()
|
||||
* @see XMLWriter::endDocument()
|
||||
*/
|
||||
public function endDiaDoc(): void
|
||||
{
|
||||
$this->endElement();
|
||||
$this->endDocument();
|
||||
}
|
||||
|
||||
/**
|
||||
* Output Dia Document for download
|
||||
*
|
||||
* @see XMLWriter::flush()
|
||||
*
|
||||
* @param string $fileName name of the dia document
|
||||
*/
|
||||
public function showOutput($fileName): void
|
||||
{
|
||||
if (ob_get_clean()) {
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
$output = $this->flush();
|
||||
ResponseRenderer::getInstance()->disable();
|
||||
Core::downloadHeader(
|
||||
$fileName,
|
||||
'application/x-dia-diagram',
|
||||
strlen($output)
|
||||
);
|
||||
print $output;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,236 @@
|
|||
<?php
|
||||
/**
|
||||
* Classes to create relation schema in Dia format.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Dia;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
|
||||
|
||||
use function in_array;
|
||||
|
||||
/**
|
||||
* Dia Relation Schema Class
|
||||
*
|
||||
* Purpose of this class is to generate the Dia XML Document
|
||||
* which is used for representing the database diagrams in Dia IDE
|
||||
* This class uses Database Table and Reference Objects of Dia and with
|
||||
* the combination of these objects actually helps in preparing Dia XML.
|
||||
*
|
||||
* Dia XML is generated by using XMLWriter php extension and this class
|
||||
* inherits ExportRelationSchema class has common functionality added
|
||||
* to this class
|
||||
*
|
||||
* @property Dia $diagram
|
||||
*/
|
||||
class DiaRelationSchema extends ExportRelationSchema
|
||||
{
|
||||
/** @var TableStatsDia[] */
|
||||
private $tables = [];
|
||||
|
||||
/** @var RelationStatsDia[] Relations */
|
||||
private $relations = [];
|
||||
|
||||
/** @var float */
|
||||
private $topMargin = 2.8222000598907471;
|
||||
|
||||
/** @var float */
|
||||
private $bottomMargin = 2.8222000598907471;
|
||||
|
||||
/** @var float */
|
||||
private $leftMargin = 2.8222000598907471;
|
||||
|
||||
/** @var float */
|
||||
private $rightMargin = 2.8222000598907471;
|
||||
|
||||
/** @var int */
|
||||
public static $objectId = 0;
|
||||
|
||||
/**
|
||||
* Upon instantiation This outputs the Dia XML document
|
||||
* that user can download
|
||||
*
|
||||
* @see Dia
|
||||
* @see TableStatsDia
|
||||
* @see RelationStatsDia
|
||||
*
|
||||
* @param string $db database name
|
||||
*/
|
||||
public function __construct($db)
|
||||
{
|
||||
parent::__construct($db, new Dia());
|
||||
|
||||
$this->setShowColor(isset($_REQUEST['dia_show_color']));
|
||||
$this->setShowKeys(isset($_REQUEST['dia_show_keys']));
|
||||
$this->setOrientation((string) $_REQUEST['dia_orientation']);
|
||||
$this->setPaper((string) $_REQUEST['dia_paper']);
|
||||
|
||||
$this->diagram->startDiaDoc(
|
||||
$this->paper,
|
||||
$this->topMargin,
|
||||
$this->bottomMargin,
|
||||
$this->leftMargin,
|
||||
$this->rightMargin,
|
||||
$this->orientation
|
||||
);
|
||||
|
||||
$alltables = $this->getTablesFromRequest();
|
||||
|
||||
foreach ($alltables as $table) {
|
||||
if (isset($this->tables[$table])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->tables[$table] = new TableStatsDia(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$table,
|
||||
$this->pageNumber,
|
||||
$this->showKeys,
|
||||
$this->offline
|
||||
);
|
||||
}
|
||||
|
||||
$seen_a_relation = false;
|
||||
foreach ($alltables as $one_table) {
|
||||
$exist_rel = $this->relation->getForeigners($this->db, $one_table, '', 'both');
|
||||
if (! $exist_rel) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$seen_a_relation = true;
|
||||
foreach ($exist_rel as $master_field => $rel) {
|
||||
/* put the foreign table on the schema only if selected
|
||||
* by the user
|
||||
* (do not use array_search() because we would have to
|
||||
* to do a === false and this is not PHP3 compatible)
|
||||
*/
|
||||
if ($master_field !== 'foreign_keys_data') {
|
||||
if (in_array($rel['foreign_table'], $alltables)) {
|
||||
$this->addRelation(
|
||||
$one_table,
|
||||
$master_field,
|
||||
$rel['foreign_table'],
|
||||
$rel['foreign_field'],
|
||||
$this->showKeys
|
||||
);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($rel as $one_key) {
|
||||
if (! in_array($one_key['ref_table_name'], $alltables)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($one_key['index_list'] as $index => $one_field) {
|
||||
$this->addRelation(
|
||||
$one_table,
|
||||
$one_field,
|
||||
$one_key['ref_table_name'],
|
||||
$one_key['ref_index_list'][$index],
|
||||
$this->showKeys
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->drawTables();
|
||||
|
||||
if ($seen_a_relation) {
|
||||
$this->drawRelations();
|
||||
}
|
||||
|
||||
$this->diagram->endDiaDoc();
|
||||
}
|
||||
|
||||
/**
|
||||
* Output Dia Document for download
|
||||
*/
|
||||
public function showOutput(): void
|
||||
{
|
||||
$this->diagram->showOutput($this->getFileName('.dia'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines relation objects
|
||||
*
|
||||
* @see TableStatsDia::__construct(),RelationStatsDia::__construct()
|
||||
*
|
||||
* @param string $masterTable The master table name
|
||||
* @param string $masterField The relation field in the master table
|
||||
* @param string $foreignTable The foreign table name
|
||||
* @param string $foreignField The relation field in the foreign table
|
||||
* @param bool $showKeys Whether to display ONLY keys or not
|
||||
*/
|
||||
private function addRelation(
|
||||
$masterTable,
|
||||
$masterField,
|
||||
$foreignTable,
|
||||
$foreignField,
|
||||
$showKeys
|
||||
): void {
|
||||
if (! isset($this->tables[$masterTable])) {
|
||||
$this->tables[$masterTable] = new TableStatsDia(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$masterTable,
|
||||
$this->pageNumber,
|
||||
$showKeys
|
||||
);
|
||||
}
|
||||
|
||||
if (! isset($this->tables[$foreignTable])) {
|
||||
$this->tables[$foreignTable] = new TableStatsDia(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$foreignTable,
|
||||
$this->pageNumber,
|
||||
$showKeys
|
||||
);
|
||||
}
|
||||
|
||||
$this->relations[] = new RelationStatsDia(
|
||||
$this->diagram,
|
||||
$this->tables[$masterTable],
|
||||
$masterField,
|
||||
$this->tables[$foreignTable],
|
||||
$foreignField
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws relation references
|
||||
*
|
||||
* connects master table's master field to
|
||||
* foreign table's foreign field using Dia object
|
||||
* type Database - Reference
|
||||
*
|
||||
* @see RelationStatsDia::relationDraw()
|
||||
*/
|
||||
private function drawRelations(): void
|
||||
{
|
||||
foreach ($this->relations as $relation) {
|
||||
$relation->relationDraw($this->showColor);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws tables
|
||||
*
|
||||
* Tables are generated using Dia object type Database - Table
|
||||
* primary fields are underlined and bold in tables
|
||||
*
|
||||
* @see TableStatsDia::tableDraw()
|
||||
*/
|
||||
private function drawTables(): void
|
||||
{
|
||||
foreach ($this->tables as $table) {
|
||||
$table->tableDraw($this->showColor);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,237 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains PhpMyAdmin\Plugins\Schema\Dia\RelationStatsDia class
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Dia;
|
||||
|
||||
use function array_search;
|
||||
use function shuffle;
|
||||
|
||||
/**
|
||||
* Relation preferences/statistics
|
||||
*
|
||||
* This class fetches the table master and foreign fields positions
|
||||
* and helps in generating the Table references and then connects
|
||||
* master table's master field to foreign table's foreign key
|
||||
* in dia XML document.
|
||||
*/
|
||||
class RelationStatsDia
|
||||
{
|
||||
/** @var Dia */
|
||||
protected $diagram;
|
||||
|
||||
/** @var mixed */
|
||||
public $srcConnPointsRight;
|
||||
|
||||
/** @var mixed */
|
||||
public $srcConnPointsLeft;
|
||||
|
||||
/** @var mixed */
|
||||
public $destConnPointsRight;
|
||||
|
||||
/** @var mixed */
|
||||
public $destConnPointsLeft;
|
||||
|
||||
/** @var int */
|
||||
public $masterTableId;
|
||||
|
||||
/** @var int */
|
||||
public $foreignTableId;
|
||||
|
||||
/** @var mixed */
|
||||
public $masterTablePos;
|
||||
|
||||
/** @var mixed */
|
||||
public $foreignTablePos;
|
||||
|
||||
/** @var string */
|
||||
public $referenceColor = '#000000';
|
||||
|
||||
/**
|
||||
* @see Relation_Stats_Dia::getXy
|
||||
*
|
||||
* @param Dia $diagram The DIA diagram
|
||||
* @param TableStatsDia $master_table The master table name
|
||||
* @param string $master_field The relation field in the master table
|
||||
* @param TableStatsDia $foreign_table The foreign table name
|
||||
* @param string $foreign_field The relation field in the foreign table
|
||||
*/
|
||||
public function __construct(
|
||||
$diagram,
|
||||
$master_table,
|
||||
$master_field,
|
||||
$foreign_table,
|
||||
$foreign_field
|
||||
) {
|
||||
$this->diagram = $diagram;
|
||||
$src_pos = $this->getXy($master_table, $master_field);
|
||||
$dest_pos = $this->getXy($foreign_table, $foreign_field);
|
||||
$this->srcConnPointsLeft = $src_pos[0];
|
||||
$this->srcConnPointsRight = $src_pos[1];
|
||||
$this->destConnPointsLeft = $dest_pos[0];
|
||||
$this->destConnPointsRight = $dest_pos[1];
|
||||
$this->masterTablePos = $src_pos[2];
|
||||
$this->foreignTablePos = $dest_pos[2];
|
||||
$this->masterTableId = $master_table->tableId;
|
||||
$this->foreignTableId = $foreign_table->tableId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Each Table object have connection points
|
||||
* which is used to connect to other objects in Dia
|
||||
* we detect the position of key in fields and
|
||||
* then determines its left and right connection
|
||||
* points.
|
||||
*
|
||||
* @param TableStatsDia $table The current table name
|
||||
* @param string $column The relation column name
|
||||
*
|
||||
* @return array Table right,left connection points and key position
|
||||
*/
|
||||
private function getXy($table, $column)
|
||||
{
|
||||
$pos = array_search($column, $table->fields);
|
||||
// left, right, position
|
||||
$value = 12;
|
||||
if ($pos != 0) {
|
||||
return [
|
||||
$pos + $value + $pos,
|
||||
$pos + $value + $pos + 1,
|
||||
$pos,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
$pos + $value,
|
||||
$pos + $value + 1,
|
||||
$pos,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws relation references
|
||||
*
|
||||
* connects master table's master field to foreign table's
|
||||
* foreign field using Dia object type Database - Reference
|
||||
* Dia object is used to generate the XML of Dia Document.
|
||||
* Database reference Object and their attributes are involved
|
||||
* in the combination of displaying Database - reference on Dia Document.
|
||||
*
|
||||
* @see PDF
|
||||
*
|
||||
* @param bool $showColor Whether to use one color per relation or not
|
||||
* if showColor is true then an array of $listOfColors
|
||||
* will be used to choose the random colors for
|
||||
* references lines. we can change/add more colors to
|
||||
* this
|
||||
*
|
||||
* @return bool|void
|
||||
*/
|
||||
public function relationDraw($showColor)
|
||||
{
|
||||
++DiaRelationSchema::$objectId;
|
||||
/*
|
||||
* if source connection points and destination connection
|
||||
* points are same then return it false and don't draw that
|
||||
* relation
|
||||
*/
|
||||
if ($this->srcConnPointsRight == $this->destConnPointsRight) {
|
||||
if ($this->srcConnPointsLeft == $this->destConnPointsLeft) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ($showColor) {
|
||||
$listOfColors = [
|
||||
'FF0000',
|
||||
'000099',
|
||||
'00FF00',
|
||||
];
|
||||
shuffle($listOfColors);
|
||||
$this->referenceColor = '#' . $listOfColors[0] . '';
|
||||
} else {
|
||||
$this->referenceColor = '#000000';
|
||||
}
|
||||
|
||||
$this->diagram->writeRaw(
|
||||
'<dia:object type="Database - Reference" version="0" id="'
|
||||
. DiaRelationSchema::$objectId . '">
|
||||
<dia:attribute name="obj_pos">
|
||||
<dia:point val="3.27,18.9198"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="obj_bb">
|
||||
<dia:rectangle val="2.27,8.7175;17.7679,18.9198"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="meta">
|
||||
<dia:composite type="dict"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="orth_points">
|
||||
<dia:point val="3.27,18.9198"/>
|
||||
<dia:point val="2.27,18.9198"/>
|
||||
<dia:point val="2.27,14.1286"/>
|
||||
<dia:point val="17.7679,14.1286"/>
|
||||
<dia:point val="17.7679,9.3375"/>
|
||||
<dia:point val="16.7679,9.3375"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="orth_orient">
|
||||
<dia:enum val="0"/>
|
||||
<dia:enum val="1"/>
|
||||
<dia:enum val="0"/>
|
||||
<dia:enum val="1"/>
|
||||
<dia:enum val="0"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="orth_autoroute">
|
||||
<dia:boolean val="true"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="text_colour">
|
||||
<dia:color val="#000000"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="line_colour">
|
||||
<dia:color val="' . $this->referenceColor . '"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="line_width">
|
||||
<dia:real val="0.10000000000000001"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="line_style">
|
||||
<dia:enum val="0"/>
|
||||
<dia:real val="1"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="corner_radius">
|
||||
<dia:real val="0"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="end_arrow">
|
||||
<dia:enum val="22"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="end_arrow_length">
|
||||
<dia:real val="0.5"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="end_arrow_width">
|
||||
<dia:real val="0.5"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="start_point_desc">
|
||||
<dia:string>#1#</dia:string>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="end_point_desc">
|
||||
<dia:string>#n#</dia:string>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="normal_font">
|
||||
<dia:font family="monospace" style="0" name="Courier"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="normal_font_height">
|
||||
<dia:real val="0.59999999999999998"/>
|
||||
</dia:attribute>
|
||||
<dia:connections>
|
||||
<dia:connection handle="0" to="'
|
||||
. $this->masterTableId . '" connection="'
|
||||
. $this->srcConnPointsRight . '"/>
|
||||
<dia:connection handle="1" to="'
|
||||
. $this->foreignTableId . '" connection="'
|
||||
. $this->destConnPointsRight . '"/>
|
||||
</dia:connections>
|
||||
</dia:object>'
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,223 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains PhpMyAdmin\Plugins\Schema\Dia\TableStatsDia class
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Dia;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
|
||||
use PhpMyAdmin\Plugins\Schema\TableStats;
|
||||
|
||||
use function __;
|
||||
use function in_array;
|
||||
use function shuffle;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Table preferences/statistics
|
||||
*
|
||||
* This class preserves the table co-ordinates,fields
|
||||
* and helps in drawing/generating the Tables in dia XML document.
|
||||
*
|
||||
* @property Dia $diagram
|
||||
*/
|
||||
class TableStatsDia extends TableStats
|
||||
{
|
||||
/** @var int */
|
||||
public $tableId;
|
||||
|
||||
/** @var string */
|
||||
public $tableColor = '#000000';
|
||||
|
||||
/**
|
||||
* @param Dia $diagram The current dia document
|
||||
* @param string $db The database name
|
||||
* @param string $tableName The table name
|
||||
* @param int $pageNumber The current page number (from the
|
||||
* $cfg['Servers'][$i]['table_coords'] table)
|
||||
* @param bool $showKeys Whether to display ONLY keys or not
|
||||
* @param bool $offline Whether the coordinates are sent from the browser
|
||||
*/
|
||||
public function __construct(
|
||||
$diagram,
|
||||
$db,
|
||||
$tableName,
|
||||
$pageNumber,
|
||||
$showKeys = false,
|
||||
$offline = false
|
||||
) {
|
||||
parent::__construct($diagram, $db, $pageNumber, $tableName, $showKeys, false, $offline);
|
||||
|
||||
/**
|
||||
* Every object in Dia document needs an ID to identify
|
||||
* so, we used a static variable to keep the things unique
|
||||
*/
|
||||
$this->tableId = ++DiaRelationSchema::$objectId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays an error when the table cannot be found.
|
||||
*/
|
||||
protected function showMissingTableError(): void
|
||||
{
|
||||
ExportRelationSchema::dieSchema(
|
||||
$this->pageNumber,
|
||||
'DIA',
|
||||
sprintf(__('The %s table doesn\'t exist!'), $this->tableName)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do draw the table
|
||||
*
|
||||
* Tables are generated using object type Database - Table
|
||||
* primary fields are underlined in tables. Dia object
|
||||
* is used to generate the XML of Dia Document. Database Table
|
||||
* Object and their attributes are involved in the combination
|
||||
* of displaying Database - Table on Dia Document.
|
||||
*
|
||||
* @see Dia
|
||||
*
|
||||
* @param bool $showColor Whether to show color for tables text or not
|
||||
* if showColor is true then an array of $listOfColors
|
||||
* will be used to choose the random colors for tables
|
||||
* text we can change/add more colors to this array
|
||||
*/
|
||||
public function tableDraw($showColor): void
|
||||
{
|
||||
if ($showColor) {
|
||||
$listOfColors = [
|
||||
'FF0000',
|
||||
'000099',
|
||||
'00FF00',
|
||||
];
|
||||
shuffle($listOfColors);
|
||||
$this->tableColor = '#' . $listOfColors[0] . '';
|
||||
} else {
|
||||
$this->tableColor = '#000000';
|
||||
}
|
||||
|
||||
$factor = 0.1;
|
||||
|
||||
$this->diagram->startElement('dia:object');
|
||||
$this->diagram->writeAttribute('type', 'Database - Table');
|
||||
$this->diagram->writeAttribute('version', '0');
|
||||
$this->diagram->writeAttribute('id', '' . $this->tableId . '');
|
||||
$this->diagram->writeRaw(
|
||||
'<dia:attribute name="obj_pos">
|
||||
<dia:point val="'
|
||||
. ($this->x * $factor) . ',' . ($this->y * $factor) . '"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="obj_bb">
|
||||
<dia:rectangle val="'
|
||||
. ($this->x * $factor) . ',' . ($this->y * $factor) . ';9.97,9.2"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="meta">
|
||||
<dia:composite type="dict"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="elem_corner">
|
||||
<dia:point val="'
|
||||
. ($this->x * $factor) . ',' . ($this->y * $factor) . '"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="elem_width">
|
||||
<dia:real val="5.9199999999999999"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="elem_height">
|
||||
<dia:real val="3.5"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="text_colour">
|
||||
<dia:color val="' . $this->tableColor . '"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="line_colour">
|
||||
<dia:color val="#000000"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="fill_colour">
|
||||
<dia:color val="#ffffff"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="line_width">
|
||||
<dia:real val="0.10000000000000001"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="name">
|
||||
<dia:string>#' . $this->tableName . '#</dia:string>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="comment">
|
||||
<dia:string>##</dia:string>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="visible_comment">
|
||||
<dia:boolean val="false"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="tagging_comment">
|
||||
<dia:boolean val="false"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="underline_primary_key">
|
||||
<dia:boolean val="true"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="bold_primary_keys">
|
||||
<dia:boolean val="true"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="normal_font">
|
||||
<dia:font family="monospace" style="0" name="Courier"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="name_font">
|
||||
<dia:font family="sans" style="80" name="Helvetica-Bold"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="comment_font">
|
||||
<dia:font family="sans" style="0" name="Helvetica"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="normal_font_height">
|
||||
<dia:real val="0.80000000000000004"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="name_font_height">
|
||||
<dia:real val="0.69999999999999996"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="comment_font_height">
|
||||
<dia:real val="0.69999999999999996"/>
|
||||
</dia:attribute>'
|
||||
);
|
||||
|
||||
$this->diagram->startElement('dia:attribute');
|
||||
$this->diagram->writeAttribute('name', 'attributes');
|
||||
|
||||
foreach ($this->fields as $field) {
|
||||
$this->diagram->writeRaw(
|
||||
'<dia:composite type="table_attribute">
|
||||
<dia:attribute name="name">
|
||||
<dia:string>#' . $field . '#</dia:string>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="type">
|
||||
<dia:string>##</dia:string>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="comment">
|
||||
<dia:string>##</dia:string>
|
||||
</dia:attribute>'
|
||||
);
|
||||
unset($pm);
|
||||
$pm = 'false';
|
||||
if (in_array($field, $this->primary)) {
|
||||
$pm = 'true';
|
||||
}
|
||||
|
||||
if ($field == $this->displayfield) {
|
||||
$pm = 'false';
|
||||
}
|
||||
|
||||
$this->diagram->writeRaw(
|
||||
'<dia:attribute name="primary_key">
|
||||
<dia:boolean val="' . $pm . '"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="nullable">
|
||||
<dia:boolean val="false"/>
|
||||
</dia:attribute>
|
||||
<dia:attribute name="unique">
|
||||
<dia:boolean val="' . $pm . '"/>
|
||||
</dia:attribute>
|
||||
</dia:composite>'
|
||||
);
|
||||
}
|
||||
|
||||
$this->diagram->endElement();
|
||||
$this->diagram->endElement();
|
||||
}
|
||||
}
|
257
admin/phpMyAdmin/libraries/classes/Plugins/Schema/Eps/Eps.php
Normal file
257
admin/phpMyAdmin/libraries/classes/Plugins/Schema/Eps/Eps.php
Normal file
|
@ -0,0 +1,257 @@
|
|||
<?php
|
||||
/**
|
||||
* Classes to create relation schema in EPS format.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Eps;
|
||||
|
||||
use PhpMyAdmin\Core;
|
||||
use PhpMyAdmin\ResponseRenderer;
|
||||
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* This Class is EPS Library and
|
||||
* helps in developing structure of EPS Schema Export
|
||||
*
|
||||
* @see https://www.php.net/manual/en/book.xmlwriter.php
|
||||
*/
|
||||
class Eps
|
||||
{
|
||||
/** @var string */
|
||||
public $font = 'Arial';
|
||||
|
||||
/** @var int */
|
||||
public $fontSize = 12;
|
||||
|
||||
/** @var string */
|
||||
public $stringCommands;
|
||||
|
||||
/**
|
||||
* Upon instantiation This starts writing the EPS Document.
|
||||
* %!PS-Adobe-3.0 EPSF-3.0 This is the MUST first comment to include
|
||||
* it shows/tells that the Post Script document is purely under
|
||||
* Document Structuring Convention [DSC] and is Compliant
|
||||
* Encapsulated Post Script Document
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->stringCommands = '';
|
||||
$this->stringCommands .= "%!PS-Adobe-3.0 EPSF-3.0 \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Set document title
|
||||
*
|
||||
* @param string $value sets the title text
|
||||
*/
|
||||
public function setTitle($value): void
|
||||
{
|
||||
$this->stringCommands .= '%%Title: ' . $value . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Set document author
|
||||
*
|
||||
* @param string $value sets the author
|
||||
*/
|
||||
public function setAuthor($value): void
|
||||
{
|
||||
$this->stringCommands .= '%%Creator: ' . $value . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Set document creation date
|
||||
*
|
||||
* @param string $value sets the date
|
||||
*/
|
||||
public function setDate($value): void
|
||||
{
|
||||
$this->stringCommands .= '%%CreationDate: ' . $value . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Set document orientation
|
||||
*
|
||||
* @param string $orientation sets the orientation
|
||||
*/
|
||||
public function setOrientation($orientation): void
|
||||
{
|
||||
$this->stringCommands .= "%%PageOrder: Ascend \n";
|
||||
if ($orientation === 'L') {
|
||||
$orientation = 'Landscape';
|
||||
$this->stringCommands .= '%%Orientation: ' . $orientation . "\n";
|
||||
} else {
|
||||
$orientation = 'Portrait';
|
||||
$this->stringCommands .= '%%Orientation: ' . $orientation . "\n";
|
||||
}
|
||||
|
||||
$this->stringCommands .= "%%EndComments \n";
|
||||
$this->stringCommands .= "%%Pages 1 \n";
|
||||
$this->stringCommands .= "%%BoundingBox: 72 150 144 170 \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the font and size
|
||||
*
|
||||
* font can be set whenever needed in EPS
|
||||
*
|
||||
* @param string $value sets the font name e.g Arial
|
||||
* @param int $size sets the size of the font e.g 10
|
||||
*/
|
||||
public function setFont(string $value, int $size): void
|
||||
{
|
||||
$this->font = $value;
|
||||
$this->fontSize = $size;
|
||||
$this->stringCommands .= '/' . $value . " findfont % Get the basic font\n";
|
||||
$this->stringCommands .= ''
|
||||
. $size . ' scalefont % Scale the font to ' . $size . " points\n";
|
||||
$this->stringCommands .= "setfont % Make it the current font\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the font
|
||||
*
|
||||
* @return string return the font name e.g Arial
|
||||
*/
|
||||
public function getFont()
|
||||
{
|
||||
return $this->font;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the font Size
|
||||
*
|
||||
* @return int return the size of the font e.g 10
|
||||
*/
|
||||
public function getFontSize(): int
|
||||
{
|
||||
return $this->fontSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the line
|
||||
*
|
||||
* drawing the lines from x,y source to x,y destination and set the
|
||||
* width of the line. lines helps in showing relationships of tables
|
||||
*
|
||||
* @param int $x_from The x_from attribute defines the start
|
||||
* left position of the element
|
||||
* @param int $y_from The y_from attribute defines the start
|
||||
* right position of the element
|
||||
* @param int $x_to The x_to attribute defines the end
|
||||
* left position of the element
|
||||
* @param int $y_to The y_to attribute defines the end
|
||||
* right position of the element
|
||||
* @param int $lineWidth Sets the width of the line e.g 2
|
||||
*/
|
||||
public function line(
|
||||
$x_from = 0,
|
||||
$y_from = 0,
|
||||
$x_to = 0,
|
||||
$y_to = 0,
|
||||
$lineWidth = 0
|
||||
): void {
|
||||
$this->stringCommands .= $lineWidth . " setlinewidth \n";
|
||||
$this->stringCommands .= $x_from . ' ' . $y_from . " moveto \n";
|
||||
$this->stringCommands .= $x_to . ' ' . $y_to . " lineto \n";
|
||||
$this->stringCommands .= "stroke \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the rectangle
|
||||
*
|
||||
* drawing the rectangle from x,y source to x,y destination and set the
|
||||
* width of the line. rectangles drawn around the text shown of fields
|
||||
*
|
||||
* @param int $x_from The x_from attribute defines the start
|
||||
* left position of the element
|
||||
* @param int $y_from The y_from attribute defines the start
|
||||
* right position of the element
|
||||
* @param int $x_to The x_to attribute defines the end
|
||||
* left position of the element
|
||||
* @param int $y_to The y_to attribute defines the end
|
||||
* right position of the element
|
||||
* @param int $lineWidth Sets the width of the line e.g 2
|
||||
*/
|
||||
public function rect($x_from, $y_from, $x_to, $y_to, $lineWidth): void
|
||||
{
|
||||
$this->stringCommands .= $lineWidth . " setlinewidth \n";
|
||||
$this->stringCommands .= "newpath \n";
|
||||
$this->stringCommands .= $x_from . ' ' . $y_from . " moveto \n";
|
||||
$this->stringCommands .= '0 ' . $y_to . " rlineto \n";
|
||||
$this->stringCommands .= $x_to . " 0 rlineto \n";
|
||||
$this->stringCommands .= '0 -' . $y_to . " rlineto \n";
|
||||
$this->stringCommands .= "closepath \n";
|
||||
$this->stringCommands .= "stroke \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current point
|
||||
*
|
||||
* The moveto operator takes two numbers off the stack and treats
|
||||
* them as x and y coordinates to which to move. The coordinates
|
||||
* specified become the current point.
|
||||
*
|
||||
* @param int $x The x attribute defines the left position of the element
|
||||
* @param int $y The y attribute defines the right position of the element
|
||||
*/
|
||||
public function moveTo($x, $y): void
|
||||
{
|
||||
$this->stringCommands .= $x . ' ' . $y . " moveto \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Output/Display the text
|
||||
*
|
||||
* @param string $text The string to be displayed
|
||||
*/
|
||||
public function show($text): void
|
||||
{
|
||||
$this->stringCommands .= '(' . $text . ") show \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the text at specified co-ordinates
|
||||
*
|
||||
* @param string $text String to be displayed
|
||||
* @param int $x X attribute defines the left position of the element
|
||||
* @param int $y Y attribute defines the right position of the element
|
||||
*/
|
||||
public function showXY($text, $x, $y): void
|
||||
{
|
||||
$this->moveTo($x, $y);
|
||||
$this->show($text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends EPS Document
|
||||
*/
|
||||
public function endEpsDoc(): void
|
||||
{
|
||||
$this->stringCommands .= "showpage \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Output EPS Document for download
|
||||
*
|
||||
* @param string $fileName name of the eps document
|
||||
*/
|
||||
public function showOutput($fileName): void
|
||||
{
|
||||
// if(ob_get_clean()){
|
||||
//ob_end_clean();
|
||||
//}
|
||||
$output = $this->stringCommands;
|
||||
ResponseRenderer::getInstance()
|
||||
->disable();
|
||||
Core::downloadHeader(
|
||||
$fileName,
|
||||
'image/x-eps',
|
||||
strlen($output)
|
||||
);
|
||||
print $output;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,248 @@
|
|||
<?php
|
||||
/**
|
||||
* Classes to create relation schema in EPS format.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Eps;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
|
||||
use PhpMyAdmin\Version;
|
||||
|
||||
use function __;
|
||||
use function date;
|
||||
use function in_array;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* EPS Relation Schema Class
|
||||
*
|
||||
* Purpose of this class is to generate the EPS Document
|
||||
* which is used for representing the database diagrams.
|
||||
* This class uses post script commands and with
|
||||
* the combination of these commands actually helps in preparing EPS Document.
|
||||
*
|
||||
* This class inherits ExportRelationSchema class has common functionality added
|
||||
* to this class
|
||||
*
|
||||
* @property Eps $diagram
|
||||
*/
|
||||
class EpsRelationSchema extends ExportRelationSchema
|
||||
{
|
||||
/** @var TableStatsEps[] */
|
||||
private $tables = [];
|
||||
|
||||
/** @var RelationStatsEps[] Relations */
|
||||
private $relations = [];
|
||||
|
||||
/** @var int */
|
||||
private $tablewidth = 0;
|
||||
|
||||
/**
|
||||
* Upon instantiation This starts writing the EPS document
|
||||
* user will be prompted for download as .eps extension
|
||||
*
|
||||
* @see Eps
|
||||
*
|
||||
* @param string $db database name
|
||||
*/
|
||||
public function __construct($db)
|
||||
{
|
||||
parent::__construct($db, new Eps());
|
||||
|
||||
$this->setShowColor(isset($_REQUEST['eps_show_color']));
|
||||
$this->setShowKeys(isset($_REQUEST['eps_show_keys']));
|
||||
$this->setTableDimension(isset($_REQUEST['eps_show_table_dimension']));
|
||||
$this->setAllTablesSameWidth(isset($_REQUEST['eps_all_tables_same_width']));
|
||||
$this->setOrientation((string) $_REQUEST['eps_orientation']);
|
||||
|
||||
$this->diagram->setTitle(
|
||||
sprintf(
|
||||
__('Schema of the %s database - Page %s'),
|
||||
$this->db,
|
||||
$this->pageNumber
|
||||
)
|
||||
);
|
||||
$this->diagram->setAuthor('phpMyAdmin ' . Version::VERSION);
|
||||
$this->diagram->setDate(date('j F Y, g:i a'));
|
||||
$this->diagram->setOrientation($this->orientation);
|
||||
$this->diagram->setFont('Verdana', 10);
|
||||
|
||||
$alltables = $this->getTablesFromRequest();
|
||||
|
||||
foreach ($alltables as $table) {
|
||||
if (! isset($this->tables[$table])) {
|
||||
$this->tables[$table] = new TableStatsEps(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$table,
|
||||
$this->diagram->getFont(),
|
||||
$this->diagram->getFontSize(),
|
||||
$this->pageNumber,
|
||||
$this->tablewidth,
|
||||
$this->showKeys,
|
||||
$this->tableDimension,
|
||||
$this->offline
|
||||
);
|
||||
}
|
||||
|
||||
if (! $this->sameWide) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->tables[$table]->width = $this->tablewidth;
|
||||
}
|
||||
|
||||
$seen_a_relation = false;
|
||||
foreach ($alltables as $one_table) {
|
||||
$exist_rel = $this->relation->getForeigners($this->db, $one_table, '', 'both');
|
||||
if (! $exist_rel) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$seen_a_relation = true;
|
||||
foreach ($exist_rel as $master_field => $rel) {
|
||||
/* put the foreign table on the schema only if selected
|
||||
* by the user
|
||||
* (do not use array_search() because we would have to
|
||||
* to do a === false and this is not PHP3 compatible)
|
||||
*/
|
||||
if ($master_field !== 'foreign_keys_data') {
|
||||
if (in_array($rel['foreign_table'], $alltables)) {
|
||||
$this->addRelation(
|
||||
$one_table,
|
||||
$this->diagram->getFont(),
|
||||
$this->diagram->getFontSize(),
|
||||
$master_field,
|
||||
$rel['foreign_table'],
|
||||
$rel['foreign_field'],
|
||||
$this->tableDimension
|
||||
);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($rel as $one_key) {
|
||||
if (! in_array($one_key['ref_table_name'], $alltables)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($one_key['index_list'] as $index => $one_field) {
|
||||
$this->addRelation(
|
||||
$one_table,
|
||||
$this->diagram->getFont(),
|
||||
$this->diagram->getFontSize(),
|
||||
$one_field,
|
||||
$one_key['ref_table_name'],
|
||||
$one_key['ref_index_list'][$index],
|
||||
$this->tableDimension
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($seen_a_relation) {
|
||||
$this->drawRelations();
|
||||
}
|
||||
|
||||
$this->drawTables();
|
||||
$this->diagram->endEpsDoc();
|
||||
}
|
||||
|
||||
/**
|
||||
* Output Eps Document for download
|
||||
*/
|
||||
public function showOutput(): void
|
||||
{
|
||||
$this->diagram->showOutput($this->getFileName('.eps'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines relation objects
|
||||
*
|
||||
* @see _setMinMax
|
||||
* @see TableStatsEps::__construct()
|
||||
* @see PhpMyAdmin\Plugins\Schema\Eps\RelationStatsEps::__construct()
|
||||
*
|
||||
* @param string $masterTable The master table name
|
||||
* @param string $font The font
|
||||
* @param int $fontSize The font size
|
||||
* @param string $masterField The relation field in the master table
|
||||
* @param string $foreignTable The foreign table name
|
||||
* @param string $foreignField The relation field in the foreign table
|
||||
* @param bool $tableDimension Whether to display table position or not
|
||||
*/
|
||||
private function addRelation(
|
||||
$masterTable,
|
||||
$font,
|
||||
$fontSize,
|
||||
$masterField,
|
||||
$foreignTable,
|
||||
$foreignField,
|
||||
$tableDimension
|
||||
): void {
|
||||
if (! isset($this->tables[$masterTable])) {
|
||||
$this->tables[$masterTable] = new TableStatsEps(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$masterTable,
|
||||
$font,
|
||||
$fontSize,
|
||||
$this->pageNumber,
|
||||
$this->tablewidth,
|
||||
false,
|
||||
$tableDimension
|
||||
);
|
||||
}
|
||||
|
||||
if (! isset($this->tables[$foreignTable])) {
|
||||
$this->tables[$foreignTable] = new TableStatsEps(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$foreignTable,
|
||||
$font,
|
||||
$fontSize,
|
||||
$this->pageNumber,
|
||||
$this->tablewidth,
|
||||
false,
|
||||
$tableDimension
|
||||
);
|
||||
}
|
||||
|
||||
$this->relations[] = new RelationStatsEps(
|
||||
$this->diagram,
|
||||
$this->tables[$masterTable],
|
||||
$masterField,
|
||||
$this->tables[$foreignTable],
|
||||
$foreignField
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws relation arrows and lines connects master table's master field to
|
||||
* foreign table's foreign field
|
||||
*
|
||||
* @see RelationStatsEps::relationDraw()
|
||||
*/
|
||||
private function drawRelations(): void
|
||||
{
|
||||
foreach ($this->relations as $relation) {
|
||||
$relation->relationDraw();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws tables
|
||||
*
|
||||
* @see TableStatsEps::Table_Stats_tableDraw()
|
||||
*/
|
||||
private function drawTables(): void
|
||||
{
|
||||
foreach ($this->tables as $table) {
|
||||
$table->tableDraw($this->showColor);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains PhpMyAdmin\Plugins\Schema\Eps\RelationStatsEps class
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Eps;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\RelationStats;
|
||||
|
||||
use function sqrt;
|
||||
|
||||
/**
|
||||
* Relation preferences/statistics
|
||||
*
|
||||
* This class fetches the table master and foreign fields positions
|
||||
* and helps in generating the Table references and then connects
|
||||
* master table's master field to foreign table's foreign key
|
||||
* in EPS document.
|
||||
*
|
||||
* @see Eps
|
||||
*/
|
||||
class RelationStatsEps extends RelationStats
|
||||
{
|
||||
/**
|
||||
* @param Eps $diagram The EPS diagram
|
||||
* @param string $master_table The master table name
|
||||
* @param string $master_field The relation field in the master table
|
||||
* @param string $foreign_table The foreign table name
|
||||
* @param string $foreign_field The relation field in the foreign table
|
||||
*/
|
||||
public function __construct(
|
||||
$diagram,
|
||||
$master_table,
|
||||
$master_field,
|
||||
$foreign_table,
|
||||
$foreign_field
|
||||
) {
|
||||
$this->wTick = 10;
|
||||
parent::__construct($diagram, $master_table, $master_field, $foreign_table, $foreign_field);
|
||||
$this->ySrc += 10;
|
||||
$this->yDest += 10;
|
||||
}
|
||||
|
||||
/**
|
||||
* draws relation links and arrows
|
||||
* shows foreign key relations
|
||||
*
|
||||
* @see Eps
|
||||
*/
|
||||
public function relationDraw(): void
|
||||
{
|
||||
// draw a line like -- to foreign field
|
||||
$this->diagram->line($this->xSrc, $this->ySrc, $this->xSrc + $this->srcDir * $this->wTick, $this->ySrc, 1);
|
||||
// draw a line like -- to master field
|
||||
$this->diagram->line($this->xDest + $this->destDir * $this->wTick, $this->yDest, $this->xDest, $this->yDest, 1);
|
||||
// draw a line that connects to master field line and foreign field line
|
||||
$this->diagram->line(
|
||||
$this->xSrc + $this->srcDir * $this->wTick,
|
||||
$this->ySrc,
|
||||
$this->xDest + $this->destDir * $this->wTick,
|
||||
$this->yDest,
|
||||
1
|
||||
);
|
||||
$root2 = 2 * sqrt(2);
|
||||
$this->diagram->line(
|
||||
$this->xSrc + $this->srcDir * $this->wTick * 0.75,
|
||||
$this->ySrc,
|
||||
$this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
|
||||
$this->ySrc + $this->wTick / $root2,
|
||||
1
|
||||
);
|
||||
$this->diagram->line(
|
||||
$this->xSrc + $this->srcDir * $this->wTick * 0.75,
|
||||
$this->ySrc,
|
||||
$this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
|
||||
$this->ySrc - $this->wTick / $root2,
|
||||
1
|
||||
);
|
||||
$this->diagram->line(
|
||||
$this->xDest + $this->destDir * $this->wTick / 2,
|
||||
$this->yDest,
|
||||
$this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
|
||||
$this->yDest + $this->wTick / $root2,
|
||||
1
|
||||
);
|
||||
$this->diagram->line(
|
||||
$this->xDest + $this->destDir * $this->wTick / 2,
|
||||
$this->yDest,
|
||||
$this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
|
||||
$this->yDest - $this->wTick / $root2,
|
||||
1
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,148 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains PhpMyAdmin\Plugins\Schema\Eps\TableStatsEps class
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Eps;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
|
||||
use PhpMyAdmin\Plugins\Schema\TableStats;
|
||||
|
||||
use function __;
|
||||
use function count;
|
||||
use function max;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Table preferences/statistics
|
||||
*
|
||||
* This class preserves the table co-ordinates,fields
|
||||
* and helps in drawing/generating the Tables in EPS.
|
||||
*
|
||||
* @see Eps
|
||||
*
|
||||
* @property Eps $diagram
|
||||
*/
|
||||
class TableStatsEps extends TableStats
|
||||
{
|
||||
/** @var int */
|
||||
public $height;
|
||||
|
||||
/** @var int */
|
||||
public $currentCell = 0;
|
||||
|
||||
/**
|
||||
* @see Eps
|
||||
* @see TableStatsEps::setWidthTable
|
||||
* @see TableStatsEps::setHeightTable
|
||||
*
|
||||
* @param Eps $diagram The EPS diagram
|
||||
* @param string $db The database name
|
||||
* @param string $tableName The table name
|
||||
* @param string $font The font name
|
||||
* @param int $fontSize The font size
|
||||
* @param int $pageNumber Page number
|
||||
* @param int $same_wide_width The max width among tables
|
||||
* @param bool $showKeys Whether to display keys or not
|
||||
* @param bool $tableDimension Whether to display table position or not
|
||||
* @param bool $offline Whether the coordinates are sent
|
||||
* from the browser
|
||||
*/
|
||||
public function __construct(
|
||||
$diagram,
|
||||
$db,
|
||||
$tableName,
|
||||
$font,
|
||||
$fontSize,
|
||||
$pageNumber,
|
||||
&$same_wide_width,
|
||||
$showKeys = false,
|
||||
$tableDimension = false,
|
||||
$offline = false
|
||||
) {
|
||||
parent::__construct($diagram, $db, $pageNumber, $tableName, $showKeys, $tableDimension, $offline);
|
||||
|
||||
// height and width
|
||||
$this->setHeightTable($fontSize);
|
||||
// setWidth must me after setHeight, because title
|
||||
// can include table height which changes table width
|
||||
$this->setWidthTable($font, $fontSize);
|
||||
if ($same_wide_width >= $this->width) {
|
||||
return;
|
||||
}
|
||||
|
||||
$same_wide_width = $this->width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays an error when the table cannot be found.
|
||||
*/
|
||||
protected function showMissingTableError(): void
|
||||
{
|
||||
ExportRelationSchema::dieSchema(
|
||||
$this->pageNumber,
|
||||
'EPS',
|
||||
sprintf(__('The %s table doesn\'t exist!'), $this->tableName)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the width of the table
|
||||
*
|
||||
* @see Eps
|
||||
*
|
||||
* @param string $font The font name
|
||||
* @param int $fontSize The font size
|
||||
*/
|
||||
private function setWidthTable($font, $fontSize): void
|
||||
{
|
||||
foreach ($this->fields as $field) {
|
||||
$this->width = max(
|
||||
$this->width,
|
||||
$this->font->getStringWidth($field, $font, (int) $fontSize)
|
||||
);
|
||||
}
|
||||
|
||||
$this->width += $this->font->getStringWidth(' ', $font, (int) $fontSize);
|
||||
/*
|
||||
* it is unknown what value must be added, because
|
||||
* table title is affected by the table width value
|
||||
*/
|
||||
while ($this->width < $this->font->getStringWidth($this->getTitle(), $font, (int) $fontSize)) {
|
||||
$this->width += 7;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the height of the table
|
||||
*
|
||||
* @param int $fontSize The font size
|
||||
*/
|
||||
private function setHeightTable($fontSize): void
|
||||
{
|
||||
$this->heightCell = $fontSize + 4;
|
||||
$this->height = (count($this->fields) + 1) * $this->heightCell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the table
|
||||
*
|
||||
* @see Eps
|
||||
* @see Eps::line
|
||||
* @see Eps::rect
|
||||
*
|
||||
* @param bool $showColor Whether to display color
|
||||
*/
|
||||
public function tableDraw($showColor): void
|
||||
{
|
||||
$this->diagram->rect($this->x, $this->y + 12, $this->width, $this->heightCell, 1);
|
||||
$this->diagram->showXY($this->getTitle(), $this->x + 5, $this->y + 14);
|
||||
foreach ($this->fields as $field) {
|
||||
$this->currentCell += $this->heightCell;
|
||||
$this->diagram->rect($this->x, $this->y + 12 + $this->currentCell, $this->width, $this->heightCell, 1);
|
||||
$this->diagram->showXY($field, $this->x + 5, $this->y + 14 + $this->currentCell);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,296 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains PhpMyAdmin\Plugins\Schema\ExportRelationSchema class which is
|
||||
* inherited by all schema classes.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema;
|
||||
|
||||
use PhpMyAdmin\ConfigStorage\Relation;
|
||||
use PhpMyAdmin\Url;
|
||||
use PhpMyAdmin\Util;
|
||||
|
||||
use function __;
|
||||
use function htmlspecialchars;
|
||||
use function rawurldecode;
|
||||
|
||||
/**
|
||||
* This class is inherited by all schema classes
|
||||
* It contains those methods which are common in them
|
||||
* it works like factory pattern
|
||||
*/
|
||||
class ExportRelationSchema
|
||||
{
|
||||
/** @var string */
|
||||
protected $db;
|
||||
|
||||
/** @var Dia\Dia|Eps\Eps|Pdf\Pdf|Svg\Svg|null */
|
||||
protected $diagram;
|
||||
|
||||
/** @var bool */
|
||||
protected $showColor = false;
|
||||
|
||||
/** @var bool */
|
||||
protected $tableDimension = false;
|
||||
|
||||
/** @var bool */
|
||||
protected $sameWide = false;
|
||||
|
||||
/** @var bool */
|
||||
protected $showKeys = false;
|
||||
|
||||
/** @var string */
|
||||
protected $orientation = 'L';
|
||||
|
||||
/** @var string */
|
||||
protected $paper = 'A4';
|
||||
|
||||
/** @var int */
|
||||
protected $pageNumber = 0;
|
||||
|
||||
/** @var bool */
|
||||
protected $offline = false;
|
||||
|
||||
/** @var Relation */
|
||||
protected $relation;
|
||||
|
||||
/**
|
||||
* @param string $db database name
|
||||
* @param Pdf\Pdf|Svg\Svg|Eps\Eps|Dia\Dia|null $diagram schema diagram
|
||||
*/
|
||||
public function __construct($db, $diagram)
|
||||
{
|
||||
global $dbi;
|
||||
|
||||
$this->db = $db;
|
||||
$this->diagram = $diagram;
|
||||
$this->setPageNumber((int) $_REQUEST['page_number']);
|
||||
$this->setOffline(isset($_REQUEST['offline_export']));
|
||||
$this->relation = new Relation($dbi);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Page Number
|
||||
*
|
||||
* @param int $value Page Number of the document to be created
|
||||
*/
|
||||
public function setPageNumber(int $value): void
|
||||
{
|
||||
$this->pageNumber = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the schema page number
|
||||
*
|
||||
* @return int schema page number
|
||||
*/
|
||||
public function getPageNumber()
|
||||
{
|
||||
return $this->pageNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets showColor
|
||||
*
|
||||
* @param bool $value whether to show colors
|
||||
*/
|
||||
public function setShowColor(bool $value): void
|
||||
{
|
||||
$this->showColor = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether to show colors
|
||||
*/
|
||||
public function isShowColor(): bool
|
||||
{
|
||||
return $this->showColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Table Dimension
|
||||
*
|
||||
* @param bool $value show table co-ordinates or not
|
||||
*/
|
||||
public function setTableDimension(bool $value): void
|
||||
{
|
||||
$this->tableDimension = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether to show table dimensions
|
||||
*/
|
||||
public function isTableDimension(): bool
|
||||
{
|
||||
return $this->tableDimension;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set same width of All Tables
|
||||
*
|
||||
* @param bool $value set same width of all tables or not
|
||||
*/
|
||||
public function setAllTablesSameWidth(bool $value): void
|
||||
{
|
||||
$this->sameWide = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether to use same width for all tables or not
|
||||
*/
|
||||
public function isAllTableSameWidth(): bool
|
||||
{
|
||||
return $this->sameWide;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Show only keys
|
||||
*
|
||||
* @param bool $value show only keys or not
|
||||
*/
|
||||
public function setShowKeys(bool $value): void
|
||||
{
|
||||
$this->showKeys = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether to show keys
|
||||
*/
|
||||
public function isShowKeys(): bool
|
||||
{
|
||||
return $this->showKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Orientation
|
||||
*
|
||||
* @param string $value Orientation will be portrait or landscape
|
||||
*/
|
||||
public function setOrientation(string $value): void
|
||||
{
|
||||
$this->orientation = $value === 'P' ? 'P' : 'L';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns orientation
|
||||
*
|
||||
* @return string orientation
|
||||
*/
|
||||
public function getOrientation()
|
||||
{
|
||||
return $this->orientation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set type of paper
|
||||
*
|
||||
* @param string $value paper type can be A4 etc
|
||||
*/
|
||||
public function setPaper(string $value): void
|
||||
{
|
||||
$this->paper = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the paper size
|
||||
*
|
||||
* @return string paper size
|
||||
*/
|
||||
public function getPaper()
|
||||
{
|
||||
return $this->paper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether the document is generated from client side DB
|
||||
*
|
||||
* @param bool $value offline or not
|
||||
*/
|
||||
public function setOffline(bool $value): void
|
||||
{
|
||||
$this->offline = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the client side database is used
|
||||
*/
|
||||
public function isOffline(): bool
|
||||
{
|
||||
return $this->offline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the table names from the request
|
||||
*
|
||||
* @return string[] an array of table names
|
||||
*/
|
||||
protected function getTablesFromRequest(): array
|
||||
{
|
||||
$tables = [];
|
||||
if (isset($_POST['t_tbl'])) {
|
||||
foreach ($_POST['t_tbl'] as $table) {
|
||||
$tables[] = rawurldecode($table);
|
||||
}
|
||||
}
|
||||
|
||||
return $tables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file name
|
||||
*
|
||||
* @param string $extension file extension
|
||||
*
|
||||
* @return string file name
|
||||
*/
|
||||
protected function getFileName($extension): string
|
||||
{
|
||||
global $dbi;
|
||||
|
||||
$pdfFeature = $this->relation->getRelationParameters()->pdfFeature;
|
||||
|
||||
$filename = $this->db . $extension;
|
||||
// Get the name of this page to use as filename
|
||||
if ($this->pageNumber != -1 && ! $this->offline && $pdfFeature !== null) {
|
||||
$_name_sql = 'SELECT page_descr FROM '
|
||||
. Util::backquote($pdfFeature->database) . '.'
|
||||
. Util::backquote($pdfFeature->pdfPages)
|
||||
. ' WHERE page_nr = ' . $this->pageNumber;
|
||||
$_name_rs = $dbi->queryAsControlUser($_name_sql);
|
||||
$_name_row = $_name_rs->fetchRow();
|
||||
$filename = $_name_row[0] . $extension;
|
||||
}
|
||||
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays an error message
|
||||
*
|
||||
* @param int $pageNumber ID of the chosen page
|
||||
* @param string $type Schema Type
|
||||
* @param string $error_message The error message
|
||||
*/
|
||||
public static function dieSchema($pageNumber, $type = '', $error_message = ''): void
|
||||
{
|
||||
echo '<p><strong>' , __('SCHEMA ERROR: ') , $type , '</strong></p>' , "\n";
|
||||
if (! empty($error_message)) {
|
||||
$error_message = htmlspecialchars($error_message);
|
||||
}
|
||||
|
||||
echo '<p>' , "\n";
|
||||
echo ' ' , $error_message , "\n";
|
||||
echo '</p>' , "\n";
|
||||
echo '<a href="';
|
||||
echo Url::getFromRoute('/database/designer', [
|
||||
'db' => $GLOBALS['db'],
|
||||
'server' => $GLOBALS['server'],
|
||||
'page' => $pageNumber,
|
||||
]);
|
||||
echo '">' . __('Back') . '</a>';
|
||||
echo "\n";
|
||||
exit;
|
||||
}
|
||||
}
|
435
admin/phpMyAdmin/libraries/classes/Plugins/Schema/Pdf/Pdf.php
Normal file
435
admin/phpMyAdmin/libraries/classes/Plugins/Schema/Pdf/Pdf.php
Normal file
|
@ -0,0 +1,435 @@
|
|||
<?php
|
||||
/**
|
||||
* PDF schema handling
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Pdf;
|
||||
|
||||
use PhpMyAdmin\ConfigStorage\Relation;
|
||||
use PhpMyAdmin\Pdf as PdfLib;
|
||||
use PhpMyAdmin\Util;
|
||||
|
||||
use function __;
|
||||
use function count;
|
||||
use function getcwd;
|
||||
use function max;
|
||||
use function mb_ord;
|
||||
use function str_replace;
|
||||
use function strlen;
|
||||
use function ucfirst;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
/**
|
||||
* block attempts to directly run this script
|
||||
*/
|
||||
if (getcwd() == __DIR__) {
|
||||
die('Attack stopped');
|
||||
}
|
||||
|
||||
// phpcs:enable
|
||||
|
||||
/**
|
||||
* Extends the "TCPDF" class and helps
|
||||
* in developing the structure of PDF Schema Export
|
||||
*
|
||||
* @see TCPDF
|
||||
*/
|
||||
class Pdf extends PdfLib
|
||||
{
|
||||
/** @var int|float */
|
||||
public $xMin = 0;
|
||||
|
||||
/** @var int|float */
|
||||
public $yMin = 0;
|
||||
|
||||
/** @var int|float */
|
||||
public $leftMargin = 10;
|
||||
|
||||
/** @var int|float */
|
||||
public $topMargin = 10;
|
||||
|
||||
/** @var int|float */
|
||||
public $scale = 1;
|
||||
|
||||
/** @var array */
|
||||
public $customLinks = [];
|
||||
|
||||
/** @var array */
|
||||
public $widths = [];
|
||||
|
||||
/** @var float */
|
||||
public $cMargin = 0;
|
||||
|
||||
/** @var string */
|
||||
private $ff = PdfLib::PMA_PDF_FONT;
|
||||
|
||||
/** @var bool */
|
||||
private $offline = false;
|
||||
|
||||
/** @var int */
|
||||
private $pageNumber;
|
||||
|
||||
/** @var bool */
|
||||
private $withDoc;
|
||||
|
||||
/** @var string */
|
||||
private $db;
|
||||
|
||||
/** @var Relation */
|
||||
private $relation;
|
||||
|
||||
/**
|
||||
* Constructs PDF for schema export.
|
||||
*
|
||||
* @param string $orientation page orientation
|
||||
* @param string $unit unit
|
||||
* @param string $paper the format used for pages
|
||||
* @param int $pageNumber schema page number that is being exported
|
||||
* @param bool $withDoc with document dictionary
|
||||
* @param string $db the database name
|
||||
*/
|
||||
public function __construct(
|
||||
$orientation,
|
||||
$unit,
|
||||
$paper,
|
||||
$pageNumber,
|
||||
$withDoc,
|
||||
$db
|
||||
) {
|
||||
global $dbi;
|
||||
|
||||
parent::__construct($orientation, $unit, $paper);
|
||||
$this->pageNumber = $pageNumber;
|
||||
$this->withDoc = $withDoc;
|
||||
$this->db = $db;
|
||||
$this->relation = new Relation($dbi);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value for margins
|
||||
*
|
||||
* @param float $c_margin margin
|
||||
*/
|
||||
public function setCMargin($c_margin): void
|
||||
{
|
||||
$this->cMargin = $c_margin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the scaling factor, defines minimum coordinates and margins
|
||||
*
|
||||
* @param float|int $scale The scaling factor
|
||||
* @param float|int $xMin The minimum X coordinate
|
||||
* @param float|int $yMin The minimum Y coordinate
|
||||
* @param float|int $leftMargin The left margin
|
||||
* @param float|int $topMargin The top margin
|
||||
*/
|
||||
public function setScale(
|
||||
$scale = 1,
|
||||
$xMin = 0,
|
||||
$yMin = 0,
|
||||
$leftMargin = -1,
|
||||
$topMargin = -1
|
||||
): void {
|
||||
$this->scale = $scale;
|
||||
$this->xMin = $xMin;
|
||||
$this->yMin = $yMin;
|
||||
if ($this->leftMargin != -1) {
|
||||
$this->leftMargin = $leftMargin;
|
||||
}
|
||||
|
||||
if ($this->topMargin == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->topMargin = $topMargin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs a scaled cell
|
||||
*
|
||||
* @see TCPDF::Cell()
|
||||
*
|
||||
* @param float|int $w The cell width
|
||||
* @param float|int $h The cell height
|
||||
* @param string $txt The text to output
|
||||
* @param mixed $border Whether to add borders or not
|
||||
* @param int $ln Where to put the cursor once the output is done
|
||||
* @param string $align Align mode
|
||||
* @param bool $fill Whether to fill the cell with a color or not
|
||||
* @param string $link Link
|
||||
*/
|
||||
public function cellScale(
|
||||
$w,
|
||||
$h = 0,
|
||||
$txt = '',
|
||||
$border = 0,
|
||||
$ln = 0,
|
||||
$align = '',
|
||||
bool $fill = false,
|
||||
$link = ''
|
||||
): void {
|
||||
$h /= $this->scale;
|
||||
$w /= $this->scale;
|
||||
$this->Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws a scaled line
|
||||
*
|
||||
* @see TCPDF::Line()
|
||||
*
|
||||
* @param float $x1 The horizontal position of the starting point
|
||||
* @param float $y1 The vertical position of the starting point
|
||||
* @param float $x2 The horizontal position of the ending point
|
||||
* @param float $y2 The vertical position of the ending point
|
||||
*/
|
||||
public function lineScale($x1, $y1, $x2, $y2): void
|
||||
{
|
||||
$x1 = ($x1 - $this->xMin) / $this->scale + $this->leftMargin;
|
||||
$y1 = ($y1 - $this->yMin) / $this->scale + $this->topMargin;
|
||||
$x2 = ($x2 - $this->xMin) / $this->scale + $this->leftMargin;
|
||||
$y2 = ($y2 - $this->yMin) / $this->scale + $this->topMargin;
|
||||
$this->Line($x1, $y1, $x2, $y2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets x and y scaled positions
|
||||
*
|
||||
* @see TCPDF::setXY()
|
||||
*
|
||||
* @param float $x The x position
|
||||
* @param float $y The y position
|
||||
*/
|
||||
public function setXyScale($x, $y): void
|
||||
{
|
||||
$x = ($x - $this->xMin) / $this->scale + $this->leftMargin;
|
||||
$y = ($y - $this->yMin) / $this->scale + $this->topMargin;
|
||||
$this->setXY($x, $y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the X scaled positions
|
||||
*
|
||||
* @see TCPDF::setX()
|
||||
*
|
||||
* @param float $x The x position
|
||||
*/
|
||||
public function setXScale($x): void
|
||||
{
|
||||
$x = ($x - $this->xMin) / $this->scale + $this->leftMargin;
|
||||
$this->setX($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the scaled font size
|
||||
*
|
||||
* @see TCPDF::setFontSize()
|
||||
*
|
||||
* @param float $size The font size (in points)
|
||||
*/
|
||||
public function setFontSizeScale($size): void
|
||||
{
|
||||
// Set font size in points
|
||||
$size /= $this->scale;
|
||||
$this->setFontSize($size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the scaled line width
|
||||
*
|
||||
* @see TCPDF::setLineWidth()
|
||||
*
|
||||
* @param float $width The line width
|
||||
*/
|
||||
public function setLineWidthScale($width): void
|
||||
{
|
||||
$width /= $this->scale;
|
||||
$this->setLineWidth($width);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to render the page header.
|
||||
*
|
||||
* @see TCPDF::Header()
|
||||
*/
|
||||
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
|
||||
public function Header(): void
|
||||
{
|
||||
global $dbi;
|
||||
|
||||
// We only show this if we find something in the new pdf_pages table
|
||||
|
||||
// This function must be named "Header" to work with the TCPDF library
|
||||
if (! $this->withDoc) {
|
||||
return;
|
||||
}
|
||||
|
||||
$pdfFeature = $this->relation->getRelationParameters()->pdfFeature;
|
||||
if ($this->offline || $this->pageNumber == -1 || $pdfFeature === null) {
|
||||
$pg_name = __('PDF export page');
|
||||
} else {
|
||||
$test_query = 'SELECT * FROM '
|
||||
. Util::backquote($pdfFeature->database) . '.'
|
||||
. Util::backquote($pdfFeature->pdfPages)
|
||||
. ' WHERE db_name = \'' . $dbi->escapeString($this->db)
|
||||
. '\' AND page_nr = \'' . $this->pageNumber . '\'';
|
||||
$test_rs = $dbi->queryAsControlUser($test_query);
|
||||
$pageDesc = (string) $test_rs->fetchValue('page_descr');
|
||||
|
||||
$pg_name = ucfirst($pageDesc);
|
||||
}
|
||||
|
||||
$this->setFont($this->ff, 'B', 14);
|
||||
$this->Cell(0, 6, $pg_name, 'B', 1, 'C');
|
||||
$this->setFont($this->ff, '');
|
||||
$this->Ln();
|
||||
}
|
||||
|
||||
/**
|
||||
* This function must be named "Footer" to work with the TCPDF library
|
||||
*
|
||||
* @see PDF::Footer()
|
||||
*/
|
||||
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
|
||||
public function Footer(): void
|
||||
{
|
||||
if (! $this->withDoc) {
|
||||
return;
|
||||
}
|
||||
|
||||
parent::Footer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets widths
|
||||
*
|
||||
* @param array $w array of widths
|
||||
*/
|
||||
public function setWidths(array $w): void
|
||||
{
|
||||
// column widths
|
||||
$this->widths = $w;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates table row.
|
||||
*
|
||||
* @param array $data Data for table
|
||||
* @param array $links Links for table cells
|
||||
*/
|
||||
public function row(array $data, array $links): void
|
||||
{
|
||||
// line height
|
||||
$nb = 0;
|
||||
$data_cnt = count($data);
|
||||
for ($i = 0; $i < $data_cnt; $i++) {
|
||||
$nb = max($nb, $this->numLines($this->widths[$i], $data[$i]));
|
||||
}
|
||||
|
||||
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
|
||||
$il = $this->FontSize;
|
||||
$h = ($il + 1) * $nb;
|
||||
// page break if necessary
|
||||
$this->checkPageBreak($h);
|
||||
// draw the cells
|
||||
$data_cnt = count($data);
|
||||
for ($i = 0; $i < $data_cnt; $i++) {
|
||||
$w = $this->widths[$i];
|
||||
// save current position
|
||||
$x = $this->GetX();
|
||||
$y = $this->GetY();
|
||||
// draw the border
|
||||
$this->Rect($x, $y, $w, $h);
|
||||
if (isset($links[$i])) {
|
||||
$this->Link($x, $y, $w, $h, $links[$i]);
|
||||
}
|
||||
|
||||
// print text
|
||||
$this->MultiCell($w, $il + 1, $data[$i], 0, 'L');
|
||||
// go to right side
|
||||
$this->setXY($x + $w, $y);
|
||||
}
|
||||
|
||||
// go to line
|
||||
$this->Ln($h);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute number of lines used by a multicell of width w
|
||||
*
|
||||
* @param int $w width
|
||||
* @param string $txt text
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function numLines($w, $txt)
|
||||
{
|
||||
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
|
||||
$cw = &$this->CurrentFont['cw'];
|
||||
if ($w == 0) {
|
||||
$w = $this->w - $this->rMargin - $this->x;
|
||||
}
|
||||
|
||||
// phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
|
||||
$wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
|
||||
$s = str_replace("\r", '', $txt);
|
||||
$nb = strlen($s);
|
||||
if ($nb > 0 && $s[$nb - 1] == "\n") {
|
||||
$nb--;
|
||||
}
|
||||
|
||||
$sep = -1;
|
||||
$i = 0;
|
||||
$j = 0;
|
||||
$l = 0;
|
||||
$nl = 1;
|
||||
while ($i < $nb) {
|
||||
$c = $s[$i];
|
||||
if ($c == "\n") {
|
||||
$i++;
|
||||
$sep = -1;
|
||||
$j = $i;
|
||||
$l = 0;
|
||||
$nl++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($c === ' ') {
|
||||
$sep = $i;
|
||||
}
|
||||
|
||||
$l += $cw[mb_ord($c)] ?? 0;
|
||||
if ($l > $wmax) {
|
||||
if ($sep == -1) {
|
||||
if ($i == $j) {
|
||||
$i++;
|
||||
}
|
||||
} else {
|
||||
$i = $sep + 1;
|
||||
}
|
||||
|
||||
$sep = -1;
|
||||
$j = $i;
|
||||
$l = 0;
|
||||
$nl++;
|
||||
} else {
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
return $nl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether the document is generated from client side DB
|
||||
*
|
||||
* @param bool $value whether offline
|
||||
*/
|
||||
public function setOffline($value): void
|
||||
{
|
||||
$this->offline = $value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,781 @@
|
|||
<?php
|
||||
/**
|
||||
* PDF schema handling
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Pdf;
|
||||
|
||||
use PhpMyAdmin\Pdf as PdfLib;
|
||||
use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
|
||||
use PhpMyAdmin\Transformations;
|
||||
use PhpMyAdmin\Util;
|
||||
|
||||
use function __;
|
||||
use function ceil;
|
||||
use function getcwd;
|
||||
use function in_array;
|
||||
use function intval;
|
||||
use function max;
|
||||
use function min;
|
||||
use function rsort;
|
||||
use function sort;
|
||||
use function sprintf;
|
||||
use function str_replace;
|
||||
use function strtotime;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
/**
|
||||
* block attempts to directly run this script
|
||||
*/
|
||||
if (getcwd() == __DIR__) {
|
||||
die('Attack stopped');
|
||||
}
|
||||
|
||||
// phpcs:enable
|
||||
|
||||
/**
|
||||
* Pdf Relation Schema Class
|
||||
*
|
||||
* Purpose of this class is to generate the PDF Document. PDF is widely
|
||||
* used format for documenting text,fonts,images and 3d vector graphics.
|
||||
*
|
||||
* This class inherits ExportRelationSchema class has common functionality added
|
||||
* to this class
|
||||
*
|
||||
* @property Pdf $diagram
|
||||
*/
|
||||
class PdfRelationSchema extends ExportRelationSchema
|
||||
{
|
||||
/** @var bool */
|
||||
private $showGrid = false;
|
||||
|
||||
/** @var bool */
|
||||
private $withDoc = false;
|
||||
|
||||
/** @var string */
|
||||
private $tableOrder = '';
|
||||
|
||||
/** @var TableStatsPdf[] */
|
||||
private $tables = [];
|
||||
|
||||
/** @var string */
|
||||
private $ff = PdfLib::PMA_PDF_FONT;
|
||||
|
||||
/** @var int|float */
|
||||
private $xMax = 0;
|
||||
|
||||
/** @var int|float */
|
||||
private $yMax = 0;
|
||||
|
||||
/** @var float|int */
|
||||
private $scale;
|
||||
|
||||
/** @var int|float */
|
||||
private $xMin = 100000;
|
||||
|
||||
/** @var int|float */
|
||||
private $yMin = 100000;
|
||||
|
||||
/** @var int */
|
||||
private $topMargin = 10;
|
||||
|
||||
/** @var int */
|
||||
private $bottomMargin = 10;
|
||||
|
||||
/** @var int */
|
||||
private $leftMargin = 10;
|
||||
|
||||
/** @var int */
|
||||
private $rightMargin = 10;
|
||||
|
||||
/** @var int */
|
||||
private $tablewidth = 0;
|
||||
|
||||
/** @var RelationStatsPdf[] */
|
||||
protected $relations = [];
|
||||
|
||||
/** @var Transformations */
|
||||
private $transformations;
|
||||
|
||||
/**
|
||||
* @see Schema\Pdf
|
||||
*
|
||||
* @param string $db database name
|
||||
*/
|
||||
public function __construct($db)
|
||||
{
|
||||
$this->transformations = new Transformations();
|
||||
|
||||
$this->setShowGrid(isset($_REQUEST['pdf_show_grid']));
|
||||
$this->setShowColor(isset($_REQUEST['pdf_show_color']));
|
||||
$this->setShowKeys(isset($_REQUEST['pdf_show_keys']));
|
||||
$this->setTableDimension(isset($_REQUEST['pdf_show_table_dimension']));
|
||||
$this->setAllTablesSameWidth(isset($_REQUEST['pdf_all_tables_same_width']));
|
||||
$this->setWithDataDictionary(isset($_REQUEST['pdf_with_doc']));
|
||||
$this->setTableOrder($_REQUEST['pdf_table_order']);
|
||||
$this->setOrientation((string) $_REQUEST['pdf_orientation']);
|
||||
$this->setPaper((string) $_REQUEST['pdf_paper']);
|
||||
|
||||
// Initializes a new document
|
||||
parent::__construct(
|
||||
$db,
|
||||
new Pdf(
|
||||
$this->orientation,
|
||||
'mm',
|
||||
$this->paper,
|
||||
$this->pageNumber,
|
||||
$this->withDoc,
|
||||
$db
|
||||
)
|
||||
);
|
||||
$this->diagram->setTitle(
|
||||
sprintf(
|
||||
__('Schema of the %s database'),
|
||||
$this->db
|
||||
)
|
||||
);
|
||||
$this->diagram->setCMargin(0);
|
||||
$this->diagram->Open();
|
||||
$this->diagram->setAutoPageBreak(true);
|
||||
$this->diagram->setOffline($this->offline);
|
||||
|
||||
$alltables = $this->getTablesFromRequest();
|
||||
if ($this->getTableOrder() === 'name_asc') {
|
||||
sort($alltables);
|
||||
} elseif ($this->getTableOrder() === 'name_desc') {
|
||||
rsort($alltables);
|
||||
}
|
||||
|
||||
if ($this->withDoc) {
|
||||
$this->diagram->setAutoPageBreak(true, 15);
|
||||
$this->diagram->setCMargin(1);
|
||||
$this->dataDictionaryDoc($alltables);
|
||||
$this->diagram->setAutoPageBreak(true);
|
||||
$this->diagram->setCMargin(0);
|
||||
}
|
||||
|
||||
$this->diagram->AddPage();
|
||||
|
||||
if ($this->withDoc) {
|
||||
$this->diagram->setLink($this->diagram->customLinks['RT']['-'], -1);
|
||||
$this->diagram->Bookmark(__('Relational schema'));
|
||||
$this->diagram->setAlias('{00}', $this->diagram->PageNo());
|
||||
$this->topMargin = 28;
|
||||
$this->bottomMargin = 28;
|
||||
}
|
||||
|
||||
/* snip */
|
||||
foreach ($alltables as $table) {
|
||||
if (! isset($this->tables[$table])) {
|
||||
$this->tables[$table] = new TableStatsPdf(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$table,
|
||||
null,
|
||||
$this->pageNumber,
|
||||
$this->tablewidth,
|
||||
$this->showKeys,
|
||||
$this->tableDimension,
|
||||
$this->offline
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->sameWide) {
|
||||
$this->tables[$table]->width = $this->tablewidth;
|
||||
}
|
||||
|
||||
$this->setMinMax($this->tables[$table]);
|
||||
}
|
||||
|
||||
// Defines the scale factor
|
||||
$innerWidth = $this->diagram->getPageWidth() - $this->rightMargin
|
||||
- $this->leftMargin;
|
||||
$innerHeight = $this->diagram->getPageHeight() - $this->topMargin
|
||||
- $this->bottomMargin;
|
||||
$this->scale = ceil(
|
||||
max(
|
||||
($this->xMax - $this->xMin) / $innerWidth,
|
||||
($this->yMax - $this->yMin) / $innerHeight
|
||||
) * 100
|
||||
) / 100;
|
||||
|
||||
$this->diagram->setScale($this->scale, $this->xMin, $this->yMin, $this->leftMargin, $this->topMargin);
|
||||
// Builds and save the PDF document
|
||||
$this->diagram->setLineWidthScale(0.1);
|
||||
|
||||
if ($this->showGrid) {
|
||||
$this->diagram->setFontSize(10);
|
||||
$this->strokeGrid();
|
||||
}
|
||||
|
||||
$this->diagram->setFontSizeScale(14);
|
||||
// previous logic was checking master tables and foreign tables
|
||||
// but I think that looping on every table of the pdf page as a master
|
||||
// and finding its foreigns is OK (then we can support innodb)
|
||||
$seen_a_relation = false;
|
||||
foreach ($alltables as $one_table) {
|
||||
$exist_rel = $this->relation->getForeigners($this->db, $one_table, '', 'both');
|
||||
if (! $exist_rel) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$seen_a_relation = true;
|
||||
foreach ($exist_rel as $master_field => $rel) {
|
||||
// put the foreign table on the schema only if selected
|
||||
// by the user
|
||||
// (do not use array_search() because we would have to
|
||||
// to do a === false and this is not PHP3 compatible)
|
||||
if ($master_field !== 'foreign_keys_data') {
|
||||
if (in_array($rel['foreign_table'], $alltables)) {
|
||||
$this->addRelation($one_table, $master_field, $rel['foreign_table'], $rel['foreign_field']);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($rel as $one_key) {
|
||||
if (! in_array($one_key['ref_table_name'], $alltables)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($one_key['index_list'] as $index => $one_field) {
|
||||
$this->addRelation(
|
||||
$one_table,
|
||||
$one_field,
|
||||
$one_key['ref_table_name'],
|
||||
$one_key['ref_index_list'][$index]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($seen_a_relation) {
|
||||
$this->drawRelations();
|
||||
}
|
||||
|
||||
$this->drawTables();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Show Grid
|
||||
*
|
||||
* @param bool $value show grid of the document or not
|
||||
*/
|
||||
public function setShowGrid($value): void
|
||||
{
|
||||
$this->showGrid = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether to show grid
|
||||
*/
|
||||
public function isShowGrid(): bool
|
||||
{
|
||||
return $this->showGrid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Data Dictionary
|
||||
*
|
||||
* @param bool $value show selected database data dictionary or not
|
||||
*/
|
||||
public function setWithDataDictionary($value): void
|
||||
{
|
||||
$this->withDoc = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether to show selected database data dictionary or not
|
||||
*/
|
||||
public function isWithDataDictionary(): bool
|
||||
{
|
||||
return $this->withDoc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the order of the table in data dictionary
|
||||
*
|
||||
* @param string $value table order
|
||||
*/
|
||||
public function setTableOrder($value): void
|
||||
{
|
||||
$this->tableOrder = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the order of the table in data dictionary
|
||||
*
|
||||
* @return string table order
|
||||
*/
|
||||
public function getTableOrder()
|
||||
{
|
||||
return $this->tableOrder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output Pdf Document for download
|
||||
*/
|
||||
public function showOutput(): void
|
||||
{
|
||||
$this->diagram->download($this->getFileName('.pdf'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets X and Y minimum and maximum for a table cell
|
||||
*
|
||||
* @param TableStatsPdf $table The table name of which sets XY co-ordinates
|
||||
*/
|
||||
private function setMinMax($table): void
|
||||
{
|
||||
$this->xMax = max($this->xMax, $table->x + $table->width);
|
||||
$this->yMax = max($this->yMax, $table->y + $table->height);
|
||||
$this->xMin = min($this->xMin, $table->x);
|
||||
$this->yMin = min($this->yMin, $table->y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines relation objects
|
||||
*
|
||||
* @see setMinMax
|
||||
*
|
||||
* @param string $masterTable The master table name
|
||||
* @param string $masterField The relation field in the master table
|
||||
* @param string $foreignTable The foreign table name
|
||||
* @param string $foreignField The relation field in the foreign table
|
||||
*/
|
||||
private function addRelation(
|
||||
$masterTable,
|
||||
$masterField,
|
||||
$foreignTable,
|
||||
$foreignField
|
||||
): void {
|
||||
if (! isset($this->tables[$masterTable])) {
|
||||
$this->tables[$masterTable] = new TableStatsPdf(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$masterTable,
|
||||
null,
|
||||
$this->pageNumber,
|
||||
$this->tablewidth,
|
||||
$this->showKeys,
|
||||
$this->tableDimension
|
||||
);
|
||||
$this->setMinMax($this->tables[$masterTable]);
|
||||
}
|
||||
|
||||
if (! isset($this->tables[$foreignTable])) {
|
||||
$this->tables[$foreignTable] = new TableStatsPdf(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$foreignTable,
|
||||
null,
|
||||
$this->pageNumber,
|
||||
$this->tablewidth,
|
||||
$this->showKeys,
|
||||
$this->tableDimension
|
||||
);
|
||||
$this->setMinMax($this->tables[$foreignTable]);
|
||||
}
|
||||
|
||||
$this->relations[] = new RelationStatsPdf(
|
||||
$this->diagram,
|
||||
$this->tables[$masterTable],
|
||||
$masterField,
|
||||
$this->tables[$foreignTable],
|
||||
$foreignField
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws the grid
|
||||
*
|
||||
* @see PMA_Schema_PDF
|
||||
*/
|
||||
private function strokeGrid(): void
|
||||
{
|
||||
$gridSize = 10;
|
||||
$labelHeight = 4;
|
||||
$labelWidth = 5;
|
||||
if ($this->withDoc) {
|
||||
$topSpace = 6;
|
||||
$bottomSpace = 15;
|
||||
} else {
|
||||
$topSpace = 0;
|
||||
$bottomSpace = 0;
|
||||
}
|
||||
|
||||
$this->diagram->setMargins(0, 0);
|
||||
$this->diagram->setDrawColor(200, 200, 200);
|
||||
// Draws horizontal lines
|
||||
$innerHeight = $this->diagram->getPageHeight() - $topSpace - $bottomSpace;
|
||||
for ($l = 0, $size = intval($innerHeight / $gridSize); $l <= $size; $l++) {
|
||||
$this->diagram->line(
|
||||
0,
|
||||
$l * $gridSize + $topSpace,
|
||||
$this->diagram->getPageWidth(),
|
||||
$l * $gridSize + $topSpace
|
||||
);
|
||||
// Avoid duplicates
|
||||
if ($l <= 0 || $l > intval(($innerHeight - $labelHeight) / $gridSize)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->diagram->setXY(0, $l * $gridSize + $topSpace);
|
||||
$label = (string) sprintf(
|
||||
'%.0f',
|
||||
($l * $gridSize + $topSpace - $this->topMargin)
|
||||
* $this->scale + $this->yMin
|
||||
);
|
||||
$this->diagram->Cell($labelWidth, $labelHeight, ' ' . $label);
|
||||
}
|
||||
|
||||
// Draws vertical lines
|
||||
for ($j = 0, $size = intval($this->diagram->getPageWidth() / $gridSize); $j <= $size; $j++) {
|
||||
$this->diagram->line(
|
||||
$j * $gridSize,
|
||||
$topSpace,
|
||||
$j * $gridSize,
|
||||
$this->diagram->getPageHeight() - $bottomSpace
|
||||
);
|
||||
$this->diagram->setXY($j * $gridSize, $topSpace);
|
||||
$label = (string) sprintf('%.0f', ($j * $gridSize - $this->leftMargin) * $this->scale + $this->xMin);
|
||||
$this->diagram->Cell($labelWidth, $labelHeight, $label);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws relation arrows
|
||||
*
|
||||
* @see Relation_Stats_Pdf::relationdraw()
|
||||
*/
|
||||
private function drawRelations(): void
|
||||
{
|
||||
$i = 0;
|
||||
foreach ($this->relations as $relation) {
|
||||
$relation->relationDraw($this->showColor, $i);
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws tables
|
||||
*
|
||||
* @see TableStatsPdf::tableDraw()
|
||||
*/
|
||||
private function drawTables(): void
|
||||
{
|
||||
foreach ($this->tables as $table) {
|
||||
$table->tableDraw(null, $this->withDoc, $this->showColor);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates data dictionary pages.
|
||||
*
|
||||
* @param array $alltables Tables to document.
|
||||
*/
|
||||
public function dataDictionaryDoc(array $alltables): void
|
||||
{
|
||||
global $dbi;
|
||||
|
||||
// TOC
|
||||
$this->diagram->AddPage($this->orientation);
|
||||
$this->diagram->Cell(0, 9, __('Table of contents'), 1, 0, 'C');
|
||||
$this->diagram->Ln(15);
|
||||
$i = 1;
|
||||
foreach ($alltables as $table) {
|
||||
$this->diagram->customLinks['doc'][$table]['-'] = $this->diagram->AddLink();
|
||||
$this->diagram->setX(10);
|
||||
// $this->diagram->Ln(1);
|
||||
$this->diagram->Cell(
|
||||
0,
|
||||
6,
|
||||
__('Page number:') . ' {' . sprintf('%02d', $i) . '}',
|
||||
0,
|
||||
0,
|
||||
'R',
|
||||
false,
|
||||
$this->diagram->customLinks['doc'][$table]['-']
|
||||
);
|
||||
$this->diagram->setX(10);
|
||||
$this->diagram->Cell(
|
||||
0,
|
||||
6,
|
||||
$i . ' ' . $table,
|
||||
0,
|
||||
1,
|
||||
'L',
|
||||
false,
|
||||
$this->diagram->customLinks['doc'][$table]['-']
|
||||
);
|
||||
// $this->diagram->Ln(1);
|
||||
$fields = $dbi->getColumns($this->db, $table);
|
||||
foreach ($fields as $row) {
|
||||
$this->diagram->setX(20);
|
||||
$field_name = $row['Field'];
|
||||
$this->diagram->customLinks['doc'][$table][$field_name] = $this->diagram->AddLink();
|
||||
}
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
$this->diagram->customLinks['RT']['-'] = $this->diagram->AddLink();
|
||||
$this->diagram->setX(10);
|
||||
$this->diagram->Cell(
|
||||
0,
|
||||
6,
|
||||
__('Page number:') . ' {00}',
|
||||
0,
|
||||
0,
|
||||
'R',
|
||||
false,
|
||||
$this->diagram->customLinks['RT']['-']
|
||||
);
|
||||
$this->diagram->setX(10);
|
||||
$this->diagram->Cell(
|
||||
0,
|
||||
6,
|
||||
$i . ' ' . __('Relational schema'),
|
||||
0,
|
||||
1,
|
||||
'L',
|
||||
false,
|
||||
$this->diagram->customLinks['RT']['-']
|
||||
);
|
||||
$z = 0;
|
||||
foreach ($alltables as $table) {
|
||||
$z++;
|
||||
$this->diagram->setAutoPageBreak(true, 15);
|
||||
$this->diagram->AddPage($this->orientation);
|
||||
$this->diagram->Bookmark($table);
|
||||
$this->diagram->setAlias(
|
||||
'{' . sprintf('%02d', $z) . '}',
|
||||
$this->diagram->PageNo()
|
||||
);
|
||||
$this->diagram->customLinks['RT'][$table]['-'] = $this->diagram->AddLink();
|
||||
$this->diagram->setLink($this->diagram->customLinks['doc'][$table]['-'], -1);
|
||||
$this->diagram->setFont($this->ff, 'B', 18);
|
||||
$this->diagram->Cell(
|
||||
0,
|
||||
8,
|
||||
$z . ' ' . $table,
|
||||
1,
|
||||
1,
|
||||
'C',
|
||||
false,
|
||||
$this->diagram->customLinks['RT'][$table]['-']
|
||||
);
|
||||
$this->diagram->setFont($this->ff, '', 8);
|
||||
$this->diagram->Ln();
|
||||
|
||||
$relationParameters = $this->relation->getRelationParameters();
|
||||
$comments = $this->relation->getComments($this->db, $table);
|
||||
if ($relationParameters->browserTransformationFeature !== null) {
|
||||
$mime_map = $this->transformations->getMime($this->db, $table, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets table information
|
||||
*/
|
||||
$showtable = $dbi->getTable($this->db, $table)
|
||||
->getStatusInfo();
|
||||
$show_comment = $showtable['Comment'] ?? '';
|
||||
$create_time = isset($showtable['Create_time'])
|
||||
? Util::localisedDate(
|
||||
strtotime($showtable['Create_time'])
|
||||
)
|
||||
: '';
|
||||
$update_time = isset($showtable['Update_time'])
|
||||
? Util::localisedDate(
|
||||
strtotime($showtable['Update_time'])
|
||||
)
|
||||
: '';
|
||||
$check_time = isset($showtable['Check_time'])
|
||||
? Util::localisedDate(
|
||||
strtotime($showtable['Check_time'])
|
||||
)
|
||||
: '';
|
||||
|
||||
/**
|
||||
* Gets fields properties
|
||||
*/
|
||||
$columns = $dbi->getColumns($this->db, $table);
|
||||
|
||||
// Find which tables are related with the current one and write it in
|
||||
// an array
|
||||
$res_rel = $this->relation->getForeigners($this->db, $table);
|
||||
|
||||
/**
|
||||
* Displays the comments of the table if MySQL >= 3.23
|
||||
*/
|
||||
|
||||
$break = false;
|
||||
if (! empty($show_comment)) {
|
||||
$this->diagram->Cell(
|
||||
0,
|
||||
3,
|
||||
__('Table comments:') . ' ' . $show_comment,
|
||||
0,
|
||||
1
|
||||
);
|
||||
$break = true;
|
||||
}
|
||||
|
||||
if (! empty($create_time)) {
|
||||
$this->diagram->Cell(
|
||||
0,
|
||||
3,
|
||||
__('Creation:') . ' ' . $create_time,
|
||||
0,
|
||||
1
|
||||
);
|
||||
$break = true;
|
||||
}
|
||||
|
||||
if (! empty($update_time)) {
|
||||
$this->diagram->Cell(
|
||||
0,
|
||||
3,
|
||||
__('Last update:') . ' ' . $update_time,
|
||||
0,
|
||||
1
|
||||
);
|
||||
$break = true;
|
||||
}
|
||||
|
||||
if (! empty($check_time)) {
|
||||
$this->diagram->Cell(
|
||||
0,
|
||||
3,
|
||||
__('Last check:') . ' ' . $check_time,
|
||||
0,
|
||||
1
|
||||
);
|
||||
$break = true;
|
||||
}
|
||||
|
||||
if ($break == true) {
|
||||
$this->diagram->Cell(0, 3, '', 0, 1);
|
||||
$this->diagram->Ln();
|
||||
}
|
||||
|
||||
$this->diagram->setFont($this->ff, 'B');
|
||||
if (isset($this->orientation) && $this->orientation === 'L') {
|
||||
$this->diagram->Cell(25, 8, __('Column'), 1, 0, 'C');
|
||||
$this->diagram->Cell(20, 8, __('Type'), 1, 0, 'C');
|
||||
$this->diagram->Cell(20, 8, __('Attributes'), 1, 0, 'C');
|
||||
$this->diagram->Cell(10, 8, __('Null'), 1, 0, 'C');
|
||||
$this->diagram->Cell(20, 8, __('Default'), 1, 0, 'C');
|
||||
$this->diagram->Cell(25, 8, __('Extra'), 1, 0, 'C');
|
||||
$this->diagram->Cell(45, 8, __('Links to'), 1, 0, 'C');
|
||||
|
||||
if ($this->paper === 'A4') {
|
||||
$comments_width = 67;
|
||||
} else {
|
||||
// this is really intended for 'letter'
|
||||
/**
|
||||
* @todo find optimal width for all formats
|
||||
*/
|
||||
$comments_width = 50;
|
||||
}
|
||||
|
||||
$this->diagram->Cell($comments_width, 8, __('Comments'), 1, 0, 'C');
|
||||
$this->diagram->Cell(45, 8, 'MIME', 1, 1, 'C');
|
||||
$this->diagram->setWidths(
|
||||
[
|
||||
25,
|
||||
20,
|
||||
20,
|
||||
10,
|
||||
20,
|
||||
25,
|
||||
45,
|
||||
$comments_width,
|
||||
45,
|
||||
]
|
||||
);
|
||||
} else {
|
||||
$this->diagram->Cell(20, 8, __('Column'), 1, 0, 'C');
|
||||
$this->diagram->Cell(20, 8, __('Type'), 1, 0, 'C');
|
||||
$this->diagram->Cell(20, 8, __('Attributes'), 1, 0, 'C');
|
||||
$this->diagram->Cell(10, 8, __('Null'), 1, 0, 'C');
|
||||
$this->diagram->Cell(15, 8, __('Default'), 1, 0, 'C');
|
||||
$this->diagram->Cell(15, 8, __('Extra'), 1, 0, 'C');
|
||||
$this->diagram->Cell(30, 8, __('Links to'), 1, 0, 'C');
|
||||
$this->diagram->Cell(30, 8, __('Comments'), 1, 0, 'C');
|
||||
$this->diagram->Cell(30, 8, 'MIME', 1, 1, 'C');
|
||||
$this->diagram->setWidths([20, 20, 20, 10, 15, 15, 30, 30, 30]);
|
||||
}
|
||||
|
||||
$this->diagram->setFont($this->ff, '');
|
||||
|
||||
foreach ($columns as $row) {
|
||||
$extracted_columnspec = Util::extractColumnSpec($row['Type']);
|
||||
$type = $extracted_columnspec['print_type'];
|
||||
$attribute = $extracted_columnspec['attribute'];
|
||||
if (! isset($row['Default'])) {
|
||||
if ($row['Null'] != '' && $row['Null'] !== 'NO') {
|
||||
$row['Default'] = 'NULL';
|
||||
}
|
||||
}
|
||||
|
||||
$field_name = $row['Field'];
|
||||
// $this->diagram->Ln();
|
||||
$this->diagram->customLinks['RT'][$table][$field_name] = $this->diagram->AddLink();
|
||||
$this->diagram->Bookmark($field_name, 1, -1);
|
||||
$this->diagram->setLink($this->diagram->customLinks['doc'][$table][$field_name], -1);
|
||||
$foreigner = $this->relation->searchColumnInForeigners($res_rel, $field_name);
|
||||
|
||||
$linksTo = '';
|
||||
if ($foreigner) {
|
||||
$linksTo = '-> ';
|
||||
if ($foreigner['foreign_db'] != $this->db) {
|
||||
$linksTo .= $foreigner['foreign_db'] . '.';
|
||||
}
|
||||
|
||||
$linksTo .= $foreigner['foreign_table']
|
||||
. '.' . $foreigner['foreign_field'];
|
||||
|
||||
if (isset($foreigner['on_update'])) { // not set for internal
|
||||
$linksTo .= "\n" . 'ON UPDATE ' . $foreigner['on_update'];
|
||||
$linksTo .= "\n" . 'ON DELETE ' . $foreigner['on_delete'];
|
||||
}
|
||||
}
|
||||
|
||||
$diagram_row = [
|
||||
$field_name,
|
||||
$type,
|
||||
$attribute,
|
||||
$row['Null'] == '' || $row['Null'] === 'NO'
|
||||
? __('No')
|
||||
: __('Yes'),
|
||||
$row['Default'] ?? '',
|
||||
$row['Extra'],
|
||||
$linksTo,
|
||||
$comments[$field_name] ?? '',
|
||||
isset($mime_map, $mime_map[$field_name])
|
||||
? str_replace('_', '/', $mime_map[$field_name]['mimetype'])
|
||||
: '',
|
||||
];
|
||||
$links = [];
|
||||
$links[0] = $this->diagram->customLinks['RT'][$table][$field_name];
|
||||
if (
|
||||
$foreigner
|
||||
&& isset(
|
||||
$this->diagram->customLinks['doc'][$foreigner['foreign_table']][$foreigner['foreign_field']]
|
||||
)
|
||||
) {
|
||||
$foreignTable = $this->diagram->customLinks['doc'][$foreigner['foreign_table']];
|
||||
$links[6] = $foreignTable[$foreigner['foreign_field']];
|
||||
}
|
||||
|
||||
$this->diagram->row($diagram_row, $links);
|
||||
}
|
||||
|
||||
$this->diagram->setFont($this->ff, '', 14);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,146 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains PhpMyAdmin\Plugins\Schema\Pdf\RelationStatsPdf class
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Pdf;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\RelationStats;
|
||||
|
||||
use function sqrt;
|
||||
|
||||
/**
|
||||
* Relation preferences/statistics
|
||||
*
|
||||
* This class fetches the table master and foreign fields positions
|
||||
* and helps in generating the Table references and then connects
|
||||
* master table's master field to foreign table's foreign key
|
||||
* in PDF document.
|
||||
*
|
||||
* @see Schema\Pdf::setDrawColor
|
||||
* @see Schema\Pdf::setLineWidthScale
|
||||
* @see Pdf::lineScale
|
||||
*/
|
||||
class RelationStatsPdf extends RelationStats
|
||||
{
|
||||
/**
|
||||
* @param Pdf $diagram The PDF diagram
|
||||
* @param string $master_table The master table name
|
||||
* @param string $master_field The relation field in the master table
|
||||
* @param string $foreign_table The foreign table name
|
||||
* @param string $foreign_field The relation field in the foreign table
|
||||
*/
|
||||
public function __construct(
|
||||
$diagram,
|
||||
$master_table,
|
||||
$master_field,
|
||||
$foreign_table,
|
||||
$foreign_field
|
||||
) {
|
||||
$this->wTick = 5;
|
||||
parent::__construct($diagram, $master_table, $master_field, $foreign_table, $foreign_field);
|
||||
}
|
||||
|
||||
/**
|
||||
* draws relation links and arrows shows foreign key relations
|
||||
*
|
||||
* @see Pdf
|
||||
*
|
||||
* @param bool $showColor Whether to use one color per relation or not
|
||||
* @param int $i The id of the link to draw
|
||||
*/
|
||||
public function relationDraw($showColor, $i): void
|
||||
{
|
||||
if ($showColor) {
|
||||
$d = $i % 6;
|
||||
$j = ($i - $d) / 6;
|
||||
$j %= 4;
|
||||
$j++;
|
||||
$case = [
|
||||
[
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
],
|
||||
[
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
],
|
||||
[
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
],
|
||||
[
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
],
|
||||
[
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
],
|
||||
[
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
],
|
||||
];
|
||||
[$a, $b, $c] = $case[$d];
|
||||
$e = 1 - ($j - 1) / 6;
|
||||
$this->diagram->setDrawColor($a * 255 * $e, $b * 255 * $e, $c * 255 * $e);
|
||||
} else {
|
||||
$this->diagram->setDrawColor(0);
|
||||
}
|
||||
|
||||
$this->diagram->setLineWidthScale(0.2);
|
||||
$this->diagram->lineScale($this->xSrc, $this->ySrc, $this->xSrc + $this->srcDir * $this->wTick, $this->ySrc);
|
||||
$this->diagram->lineScale(
|
||||
$this->xDest + $this->destDir * $this->wTick,
|
||||
$this->yDest,
|
||||
$this->xDest,
|
||||
$this->yDest
|
||||
);
|
||||
$this->diagram->setLineWidthScale(0.1);
|
||||
$this->diagram->lineScale(
|
||||
$this->xSrc + $this->srcDir * $this->wTick,
|
||||
$this->ySrc,
|
||||
$this->xDest + $this->destDir * $this->wTick,
|
||||
$this->yDest
|
||||
);
|
||||
/*
|
||||
* Draws arrows ->
|
||||
*/
|
||||
$root2 = 2 * sqrt(2);
|
||||
$this->diagram->lineScale(
|
||||
$this->xSrc + $this->srcDir * $this->wTick * 0.75,
|
||||
$this->ySrc,
|
||||
$this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
|
||||
$this->ySrc + $this->wTick / $root2
|
||||
);
|
||||
$this->diagram->lineScale(
|
||||
$this->xSrc + $this->srcDir * $this->wTick * 0.75,
|
||||
$this->ySrc,
|
||||
$this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
|
||||
$this->ySrc - $this->wTick / $root2
|
||||
);
|
||||
|
||||
$this->diagram->lineScale(
|
||||
$this->xDest + $this->destDir * $this->wTick / 2,
|
||||
$this->yDest,
|
||||
$this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
|
||||
$this->yDest + $this->wTick / $root2
|
||||
);
|
||||
$this->diagram->lineScale(
|
||||
$this->xDest + $this->destDir * $this->wTick / 2,
|
||||
$this->yDest,
|
||||
$this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
|
||||
$this->yDest - $this->wTick / $root2
|
||||
);
|
||||
$this->diagram->setDrawColor(0);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,214 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains PhpMyAdmin\Plugins\Schema\Pdf\TableStatsPdf class
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Pdf;
|
||||
|
||||
use PhpMyAdmin\Pdf as PdfLib;
|
||||
use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
|
||||
use PhpMyAdmin\Plugins\Schema\TableStats;
|
||||
|
||||
use function __;
|
||||
use function count;
|
||||
use function in_array;
|
||||
use function max;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Table preferences/statistics
|
||||
*
|
||||
* This class preserves the table co-ordinates,fields
|
||||
* and helps in drawing/generating the Tables in PDF document.
|
||||
*
|
||||
* @see Schema\Pdf
|
||||
*
|
||||
* @property Pdf $diagram
|
||||
*/
|
||||
class TableStatsPdf extends TableStats
|
||||
{
|
||||
/** @var int */
|
||||
public $height;
|
||||
|
||||
/** @var string */
|
||||
private $ff = PdfLib::PMA_PDF_FONT;
|
||||
|
||||
/**
|
||||
* @see PMA_Schema_PDF
|
||||
* @see TableStatsPdf::setWidthTable
|
||||
* @see PhpMyAdmin\Plugins\Schema\Pdf\TableStatsPdf::setHeightTable
|
||||
*
|
||||
* @param Pdf $diagram The PDF diagram
|
||||
* @param string $db The database name
|
||||
* @param string $tableName The table name
|
||||
* @param int $fontSize The font size
|
||||
* @param int $pageNumber The current page number (from the
|
||||
* $cfg['Servers'][$i]['table_coords'] table)
|
||||
* @param int $sameWideWidth The max. width among tables
|
||||
* @param bool $showKeys Whether to display keys or not
|
||||
* @param bool $tableDimension Whether to display table position or not
|
||||
* @param bool $offline Whether the coordinates are sent
|
||||
* from the browser
|
||||
*/
|
||||
public function __construct(
|
||||
$diagram,
|
||||
$db,
|
||||
$tableName,
|
||||
$fontSize,
|
||||
$pageNumber,
|
||||
&$sameWideWidth,
|
||||
$showKeys = false,
|
||||
$tableDimension = false,
|
||||
$offline = false
|
||||
) {
|
||||
parent::__construct($diagram, $db, $pageNumber, $tableName, $showKeys, $tableDimension, $offline);
|
||||
|
||||
$this->heightCell = 6;
|
||||
$this->setHeight();
|
||||
/*
|
||||
* setWidth must me after setHeight, because title
|
||||
* can include table height which changes table width
|
||||
*/
|
||||
$this->setWidth($fontSize);
|
||||
if ($sameWideWidth >= $this->width) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sameWideWidth = $this->width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays an error when the table cannot be found.
|
||||
*/
|
||||
protected function showMissingTableError(): void
|
||||
{
|
||||
ExportRelationSchema::dieSchema(
|
||||
$this->pageNumber,
|
||||
'PDF',
|
||||
sprintf(__('The %s table doesn\'t exist!'), $this->tableName)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns title of the current table,
|
||||
* title can have the dimensions of the table
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getTitle()
|
||||
{
|
||||
$ret = '';
|
||||
if ($this->tableDimension) {
|
||||
$ret = sprintf('%.0fx%0.f', $this->width, $this->height);
|
||||
}
|
||||
|
||||
return $ret . ' ' . $this->tableName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the width of the table
|
||||
*
|
||||
* @see PMA_Schema_PDF
|
||||
*
|
||||
* @param int $fontSize The font size
|
||||
*/
|
||||
private function setWidth($fontSize): void
|
||||
{
|
||||
foreach ($this->fields as $field) {
|
||||
$this->width = max($this->width, $this->diagram->GetStringWidth($field));
|
||||
}
|
||||
|
||||
$this->width += $this->diagram->GetStringWidth(' ');
|
||||
$this->diagram->setFont($this->ff, 'B', $fontSize);
|
||||
/*
|
||||
* it is unknown what value must be added, because
|
||||
* table title is affected by the table width value
|
||||
*/
|
||||
while ($this->width < $this->diagram->GetStringWidth($this->getTitle())) {
|
||||
$this->width += 5;
|
||||
}
|
||||
|
||||
$this->diagram->setFont($this->ff, '', $fontSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the height of the table
|
||||
*/
|
||||
private function setHeight(): void
|
||||
{
|
||||
$this->height = (count($this->fields) + 1) * $this->heightCell;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do draw the table
|
||||
*
|
||||
* @see Schema\Pdf
|
||||
*
|
||||
* @param int|null $fontSize The font size or null to use the default value
|
||||
* @param bool $withDoc Whether to include links to documentation
|
||||
* @param bool $setColor Whether to display color
|
||||
*/
|
||||
public function tableDraw(?int $fontSize, bool $withDoc, bool $setColor = false): void
|
||||
{
|
||||
$this->diagram->setXyScale($this->x, $this->y);
|
||||
$this->diagram->setFont($this->ff, 'B', $fontSize);
|
||||
if ($setColor) {
|
||||
$this->diagram->setTextColor(200);
|
||||
$this->diagram->setFillColor(0, 0, 128);
|
||||
}
|
||||
|
||||
if ($withDoc) {
|
||||
$this->diagram->setLink($this->diagram->customLinks['RT'][$this->tableName]['-'], -1);
|
||||
} else {
|
||||
$this->diagram->customLinks['doc'][$this->tableName]['-'] = '';
|
||||
}
|
||||
|
||||
$this->diagram->cellScale(
|
||||
$this->width,
|
||||
$this->heightCell,
|
||||
$this->getTitle(),
|
||||
1,
|
||||
1,
|
||||
'C',
|
||||
$setColor,
|
||||
$this->diagram->customLinks['doc'][$this->tableName]['-']
|
||||
);
|
||||
$this->diagram->setXScale($this->x);
|
||||
$this->diagram->setFont($this->ff, '', $fontSize);
|
||||
$this->diagram->setTextColor(0);
|
||||
$this->diagram->setFillColor(255);
|
||||
|
||||
foreach ($this->fields as $field) {
|
||||
if ($setColor) {
|
||||
if (in_array($field, $this->primary)) {
|
||||
$this->diagram->setFillColor(215, 121, 123);
|
||||
}
|
||||
|
||||
if ($field == $this->displayfield) {
|
||||
$this->diagram->setFillColor(142, 159, 224);
|
||||
}
|
||||
}
|
||||
|
||||
if ($withDoc) {
|
||||
$this->diagram->setLink($this->diagram->customLinks['RT'][$this->tableName][$field], -1);
|
||||
} else {
|
||||
$this->diagram->customLinks['doc'][$this->tableName][$field] = '';
|
||||
}
|
||||
|
||||
$this->diagram->cellScale(
|
||||
$this->width,
|
||||
$this->heightCell,
|
||||
' ' . $field,
|
||||
1,
|
||||
1,
|
||||
'L',
|
||||
$setColor,
|
||||
$this->diagram->customLinks['doc'][$this->tableName][$field]
|
||||
);
|
||||
$this->diagram->setXScale($this->x);
|
||||
$this->diagram->setFillColor(255);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains abstract class to hold relation preferences/statistics
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema;
|
||||
|
||||
use function abs;
|
||||
use function array_search;
|
||||
use function min;
|
||||
|
||||
/**
|
||||
* Relations preferences/statistics
|
||||
*
|
||||
* This class fetches the table master and foreign fields positions
|
||||
* and helps in generating the Table references and then connects
|
||||
* master table's master field to foreign table's foreign key.
|
||||
*
|
||||
* @abstract
|
||||
*/
|
||||
abstract class RelationStats
|
||||
{
|
||||
/** @var object */
|
||||
protected $diagram;
|
||||
|
||||
/** @var mixed */
|
||||
public $xSrc;
|
||||
|
||||
/** @var mixed */
|
||||
public $ySrc;
|
||||
|
||||
/** @var int */
|
||||
public $srcDir;
|
||||
|
||||
/** @var int */
|
||||
public $destDir;
|
||||
|
||||
/** @var mixed */
|
||||
public $xDest;
|
||||
|
||||
/** @var mixed */
|
||||
public $yDest;
|
||||
|
||||
/** @var int */
|
||||
public $wTick = 0;
|
||||
|
||||
/**
|
||||
* @param object $diagram The diagram
|
||||
* @param string $master_table The master table name
|
||||
* @param string $master_field The relation field in the master table
|
||||
* @param string $foreign_table The foreign table name
|
||||
* @param string $foreign_field The relation field in the foreign table
|
||||
*/
|
||||
public function __construct(
|
||||
$diagram,
|
||||
$master_table,
|
||||
$master_field,
|
||||
$foreign_table,
|
||||
$foreign_field
|
||||
) {
|
||||
$this->diagram = $diagram;
|
||||
|
||||
$src_pos = $this->getXy($master_table, $master_field);
|
||||
$dest_pos = $this->getXy($foreign_table, $foreign_field);
|
||||
/*
|
||||
* [0] is x-left
|
||||
* [1] is x-right
|
||||
* [2] is y
|
||||
*/
|
||||
$src_left = $src_pos[0] - $this->wTick;
|
||||
$src_right = $src_pos[1] + $this->wTick;
|
||||
$dest_left = $dest_pos[0] - $this->wTick;
|
||||
$dest_right = $dest_pos[1] + $this->wTick;
|
||||
|
||||
$d1 = abs($src_left - $dest_left);
|
||||
$d2 = abs($src_right - $dest_left);
|
||||
$d3 = abs($src_left - $dest_right);
|
||||
$d4 = abs($src_right - $dest_right);
|
||||
$d = min($d1, $d2, $d3, $d4);
|
||||
|
||||
if ($d == $d1) {
|
||||
$this->xSrc = $src_pos[0];
|
||||
$this->srcDir = -1;
|
||||
$this->xDest = $dest_pos[0];
|
||||
$this->destDir = -1;
|
||||
} elseif ($d == $d2) {
|
||||
$this->xSrc = $src_pos[1];
|
||||
$this->srcDir = 1;
|
||||
$this->xDest = $dest_pos[0];
|
||||
$this->destDir = -1;
|
||||
} elseif ($d == $d3) {
|
||||
$this->xSrc = $src_pos[0];
|
||||
$this->srcDir = -1;
|
||||
$this->xDest = $dest_pos[1];
|
||||
$this->destDir = 1;
|
||||
} else {
|
||||
$this->xSrc = $src_pos[1];
|
||||
$this->srcDir = 1;
|
||||
$this->xDest = $dest_pos[1];
|
||||
$this->destDir = 1;
|
||||
}
|
||||
|
||||
$this->ySrc = $src_pos[2];
|
||||
$this->yDest = $dest_pos[2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets arrows coordinates
|
||||
*
|
||||
* @param TableStats $table The table
|
||||
* @param string $column The relation column name
|
||||
*
|
||||
* @return array Arrows coordinates
|
||||
*/
|
||||
private function getXy($table, $column)
|
||||
{
|
||||
$pos = array_search($column, $table->fields);
|
||||
|
||||
// x_left, x_right, y
|
||||
return [
|
||||
$table->x,
|
||||
$table->x + $table->width,
|
||||
$table->y + ($pos + 1.5) * $table->heightCell,
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
/**
|
||||
* Dia schema export code
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\Dia\DiaRelationSchema;
|
||||
use PhpMyAdmin\Plugins\SchemaPlugin;
|
||||
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
|
||||
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
|
||||
use PhpMyAdmin\Properties\Options\Items\SelectPropertyItem;
|
||||
use PhpMyAdmin\Properties\Plugins\SchemaPluginProperties;
|
||||
|
||||
use function __;
|
||||
|
||||
/**
|
||||
* Handles the schema export for the Dia format
|
||||
*/
|
||||
class SchemaDia extends SchemaPlugin
|
||||
{
|
||||
/**
|
||||
* @psalm-return non-empty-lowercase-string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return 'dia';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the schema export Dia properties
|
||||
*/
|
||||
protected function setProperties(): SchemaPluginProperties
|
||||
{
|
||||
$schemaPluginProperties = new SchemaPluginProperties();
|
||||
$schemaPluginProperties->setText('Dia');
|
||||
$schemaPluginProperties->setExtension('dia');
|
||||
$schemaPluginProperties->setMimeType('application/dia');
|
||||
|
||||
// create the root group that will be the options field for
|
||||
// $schemaPluginProperties
|
||||
// this will be shown as "Format specific options"
|
||||
$exportSpecificOptions = new OptionsPropertyRootGroup('Format Specific Options');
|
||||
|
||||
// specific options main group
|
||||
$specificOptions = new OptionsPropertyMainGroup('general_opts');
|
||||
// add options common to all plugins
|
||||
$this->addCommonOptions($specificOptions);
|
||||
|
||||
$leaf = new SelectPropertyItem(
|
||||
'orientation',
|
||||
__('Orientation')
|
||||
);
|
||||
$leaf->setValues(
|
||||
[
|
||||
'L' => __('Landscape'),
|
||||
'P' => __('Portrait'),
|
||||
]
|
||||
);
|
||||
$specificOptions->addProperty($leaf);
|
||||
|
||||
$leaf = new SelectPropertyItem(
|
||||
'paper',
|
||||
__('Paper size')
|
||||
);
|
||||
$leaf->setValues($this->getPaperSizeArray());
|
||||
$specificOptions->addProperty($leaf);
|
||||
|
||||
// add the main group to the root group
|
||||
$exportSpecificOptions->addProperty($specificOptions);
|
||||
|
||||
// set the options for the schema export plugin property item
|
||||
$schemaPluginProperties->setOptions($exportSpecificOptions);
|
||||
|
||||
return $schemaPluginProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports the schema into DIA format.
|
||||
*
|
||||
* @param string $db database name
|
||||
*/
|
||||
public function exportSchema($db): bool
|
||||
{
|
||||
$export = new DiaRelationSchema($db);
|
||||
$export->showOutput();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
/**
|
||||
* PDF schema export code
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\Eps\EpsRelationSchema;
|
||||
use PhpMyAdmin\Plugins\SchemaPlugin;
|
||||
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
|
||||
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
|
||||
use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
|
||||
use PhpMyAdmin\Properties\Options\Items\SelectPropertyItem;
|
||||
use PhpMyAdmin\Properties\Plugins\SchemaPluginProperties;
|
||||
|
||||
use function __;
|
||||
|
||||
/**
|
||||
* Handles the schema export for the EPS format
|
||||
*/
|
||||
class SchemaEps extends SchemaPlugin
|
||||
{
|
||||
/**
|
||||
* @psalm-return non-empty-lowercase-string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return 'eps';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the schema export EPS properties
|
||||
*/
|
||||
protected function setProperties(): SchemaPluginProperties
|
||||
{
|
||||
$schemaPluginProperties = new SchemaPluginProperties();
|
||||
$schemaPluginProperties->setText('EPS');
|
||||
$schemaPluginProperties->setExtension('eps');
|
||||
$schemaPluginProperties->setMimeType('application/eps');
|
||||
|
||||
// create the root group that will be the options field for
|
||||
// $schemaPluginProperties
|
||||
// this will be shown as "Format specific options"
|
||||
$exportSpecificOptions = new OptionsPropertyRootGroup('Format Specific Options');
|
||||
|
||||
// specific options main group
|
||||
$specificOptions = new OptionsPropertyMainGroup('general_opts');
|
||||
// add options common to all plugins
|
||||
$this->addCommonOptions($specificOptions);
|
||||
|
||||
// create leaf items and add them to the group
|
||||
$leaf = new BoolPropertyItem(
|
||||
'all_tables_same_width',
|
||||
__('Same width for all tables')
|
||||
);
|
||||
$specificOptions->addProperty($leaf);
|
||||
|
||||
$leaf = new SelectPropertyItem(
|
||||
'orientation',
|
||||
__('Orientation')
|
||||
);
|
||||
$leaf->setValues(
|
||||
[
|
||||
'L' => __('Landscape'),
|
||||
'P' => __('Portrait'),
|
||||
]
|
||||
);
|
||||
$specificOptions->addProperty($leaf);
|
||||
|
||||
// add the main group to the root group
|
||||
$exportSpecificOptions->addProperty($specificOptions);
|
||||
|
||||
// set the options for the schema export plugin property item
|
||||
$schemaPluginProperties->setOptions($exportSpecificOptions);
|
||||
|
||||
return $schemaPluginProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports the schema into EPS format.
|
||||
*
|
||||
* @param string $db database name
|
||||
*/
|
||||
public function exportSchema($db): bool
|
||||
{
|
||||
$export = new EpsRelationSchema($db);
|
||||
$export->showOutput();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
132
admin/phpMyAdmin/libraries/classes/Plugins/Schema/SchemaPdf.php
Normal file
132
admin/phpMyAdmin/libraries/classes/Plugins/Schema/SchemaPdf.php
Normal file
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
/**
|
||||
* PDF schema export code
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\Pdf\PdfRelationSchema;
|
||||
use PhpMyAdmin\Plugins\SchemaPlugin;
|
||||
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
|
||||
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
|
||||
use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
|
||||
use PhpMyAdmin\Properties\Options\Items\SelectPropertyItem;
|
||||
use PhpMyAdmin\Properties\Plugins\SchemaPluginProperties;
|
||||
use TCPDF;
|
||||
|
||||
use function __;
|
||||
use function class_exists;
|
||||
|
||||
/**
|
||||
* Handles the schema export for the PDF format
|
||||
*/
|
||||
class SchemaPdf extends SchemaPlugin
|
||||
{
|
||||
/**
|
||||
* @psalm-return non-empty-lowercase-string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return 'pdf';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the schema export PDF properties
|
||||
*/
|
||||
protected function setProperties(): SchemaPluginProperties
|
||||
{
|
||||
$schemaPluginProperties = new SchemaPluginProperties();
|
||||
$schemaPluginProperties->setText('PDF');
|
||||
$schemaPluginProperties->setExtension('pdf');
|
||||
$schemaPluginProperties->setMimeType('application/pdf');
|
||||
|
||||
// create the root group that will be the options field for
|
||||
// $schemaPluginProperties
|
||||
// this will be shown as "Format specific options"
|
||||
$exportSpecificOptions = new OptionsPropertyRootGroup('Format Specific Options');
|
||||
|
||||
// specific options main group
|
||||
$specificOptions = new OptionsPropertyMainGroup('general_opts');
|
||||
// add options common to all plugins
|
||||
$this->addCommonOptions($specificOptions);
|
||||
|
||||
// create leaf items and add them to the group
|
||||
$leaf = new BoolPropertyItem(
|
||||
'all_tables_same_width',
|
||||
__('Same width for all tables')
|
||||
);
|
||||
$specificOptions->addProperty($leaf);
|
||||
|
||||
$leaf = new SelectPropertyItem(
|
||||
'orientation',
|
||||
__('Orientation')
|
||||
);
|
||||
$leaf->setValues(
|
||||
[
|
||||
'L' => __('Landscape'),
|
||||
'P' => __('Portrait'),
|
||||
]
|
||||
);
|
||||
$specificOptions->addProperty($leaf);
|
||||
|
||||
$leaf = new SelectPropertyItem(
|
||||
'paper',
|
||||
__('Paper size')
|
||||
);
|
||||
$leaf->setValues($this->getPaperSizeArray());
|
||||
$specificOptions->addProperty($leaf);
|
||||
|
||||
$leaf = new BoolPropertyItem(
|
||||
'show_grid',
|
||||
__('Show grid')
|
||||
);
|
||||
$specificOptions->addProperty($leaf);
|
||||
|
||||
$leaf = new BoolPropertyItem(
|
||||
'with_doc',
|
||||
__('Data dictionary')
|
||||
);
|
||||
$specificOptions->addProperty($leaf);
|
||||
|
||||
$leaf = new SelectPropertyItem(
|
||||
'table_order',
|
||||
__('Order of the tables')
|
||||
);
|
||||
$leaf->setValues(
|
||||
[
|
||||
'' => __('None'),
|
||||
'name_asc' => __('Name (Ascending)'),
|
||||
'name_desc' => __('Name (Descending)'),
|
||||
]
|
||||
);
|
||||
$specificOptions->addProperty($leaf);
|
||||
|
||||
// add the main group to the root group
|
||||
$exportSpecificOptions->addProperty($specificOptions);
|
||||
|
||||
// set the options for the schema export plugin property item
|
||||
$schemaPluginProperties->setOptions($exportSpecificOptions);
|
||||
|
||||
return $schemaPluginProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports the schema into PDF format.
|
||||
*
|
||||
* @param string $db database name
|
||||
*/
|
||||
public function exportSchema($db): bool
|
||||
{
|
||||
$export = new PdfRelationSchema($db);
|
||||
$export->showOutput();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function isAvailable(): bool
|
||||
{
|
||||
return class_exists(TCPDF::class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
/**
|
||||
* PDF schema export code
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\Svg\SvgRelationSchema;
|
||||
use PhpMyAdmin\Plugins\SchemaPlugin;
|
||||
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
|
||||
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
|
||||
use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
|
||||
use PhpMyAdmin\Properties\Plugins\SchemaPluginProperties;
|
||||
|
||||
use function __;
|
||||
|
||||
/**
|
||||
* Handles the schema export for the SVG format
|
||||
*/
|
||||
class SchemaSvg extends SchemaPlugin
|
||||
{
|
||||
/**
|
||||
* @psalm-return non-empty-lowercase-string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return 'svg';
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the schema export SVG properties
|
||||
*/
|
||||
protected function setProperties(): SchemaPluginProperties
|
||||
{
|
||||
$schemaPluginProperties = new SchemaPluginProperties();
|
||||
$schemaPluginProperties->setText('SVG');
|
||||
$schemaPluginProperties->setExtension('svg');
|
||||
$schemaPluginProperties->setMimeType('application/svg');
|
||||
|
||||
// create the root group that will be the options field for
|
||||
// $schemaPluginProperties
|
||||
// this will be shown as "Format specific options"
|
||||
$exportSpecificOptions = new OptionsPropertyRootGroup('Format Specific Options');
|
||||
|
||||
// specific options main group
|
||||
$specificOptions = new OptionsPropertyMainGroup('general_opts');
|
||||
// add options common to all plugins
|
||||
$this->addCommonOptions($specificOptions);
|
||||
|
||||
// create leaf items and add them to the group
|
||||
$leaf = new BoolPropertyItem(
|
||||
'all_tables_same_width',
|
||||
__('Same width for all tables')
|
||||
);
|
||||
$specificOptions->addProperty($leaf);
|
||||
|
||||
// add the main group to the root group
|
||||
$exportSpecificOptions->addProperty($specificOptions);
|
||||
|
||||
// set the options for the schema export plugin property item
|
||||
$schemaPluginProperties->setOptions($exportSpecificOptions);
|
||||
|
||||
return $schemaPluginProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports the schema into SVG format.
|
||||
*
|
||||
* @param string $db database name
|
||||
*/
|
||||
public function exportSchema($db): bool
|
||||
{
|
||||
$export = new SvgRelationSchema($db);
|
||||
$export->showOutput();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains PhpMyAdmin\Plugins\Schema\Svg\RelationStatsSvg class
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Svg;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\RelationStats;
|
||||
|
||||
use function shuffle;
|
||||
use function sqrt;
|
||||
|
||||
/**
|
||||
* Relation preferences/statistics
|
||||
*
|
||||
* This class fetches the table master and foreign fields positions
|
||||
* and helps in generating the Table references and then connects
|
||||
* master table's master field to foreign table's foreign key
|
||||
* in SVG XML document.
|
||||
*
|
||||
* @see Svg::printElementLine
|
||||
*/
|
||||
class RelationStatsSvg extends RelationStats
|
||||
{
|
||||
/**
|
||||
* @param Svg $diagram The SVG diagram
|
||||
* @param string $master_table The master table name
|
||||
* @param string $master_field The relation field in the master table
|
||||
* @param string $foreign_table The foreign table name
|
||||
* @param string $foreign_field The relation field in the foreign table
|
||||
*/
|
||||
public function __construct(
|
||||
$diagram,
|
||||
$master_table,
|
||||
$master_field,
|
||||
$foreign_table,
|
||||
$foreign_field
|
||||
) {
|
||||
$this->wTick = 10;
|
||||
parent::__construct($diagram, $master_table, $master_field, $foreign_table, $foreign_field);
|
||||
}
|
||||
|
||||
/**
|
||||
* draws relation links and arrows shows foreign key relations
|
||||
*
|
||||
* @see PMA_SVG
|
||||
*
|
||||
* @param bool $showColor Whether to use one color per relation or not
|
||||
*/
|
||||
public function relationDraw($showColor): void
|
||||
{
|
||||
if ($showColor) {
|
||||
$listOfColors = [
|
||||
'#c00',
|
||||
'#bbb',
|
||||
'#333',
|
||||
'#cb0',
|
||||
'#0b0',
|
||||
'#0bf',
|
||||
'#b0b',
|
||||
];
|
||||
shuffle($listOfColors);
|
||||
$color = $listOfColors[0];
|
||||
} else {
|
||||
$color = '#333';
|
||||
}
|
||||
|
||||
$this->diagram->printElementLine(
|
||||
'line',
|
||||
$this->xSrc,
|
||||
$this->ySrc,
|
||||
$this->xSrc + $this->srcDir * $this->wTick,
|
||||
$this->ySrc,
|
||||
'stroke:' . $color . ';stroke-width:1;'
|
||||
);
|
||||
$this->diagram->printElementLine(
|
||||
'line',
|
||||
$this->xDest + $this->destDir * $this->wTick,
|
||||
$this->yDest,
|
||||
$this->xDest,
|
||||
$this->yDest,
|
||||
'stroke:' . $color . ';stroke-width:1;'
|
||||
);
|
||||
$this->diagram->printElementLine(
|
||||
'line',
|
||||
$this->xSrc + $this->srcDir * $this->wTick,
|
||||
$this->ySrc,
|
||||
$this->xDest + $this->destDir * $this->wTick,
|
||||
$this->yDest,
|
||||
'stroke:' . $color . ';stroke-width:1;'
|
||||
);
|
||||
$root2 = 2 * sqrt(2);
|
||||
$this->diagram->printElementLine(
|
||||
'line',
|
||||
$this->xSrc + $this->srcDir * $this->wTick * 0.75,
|
||||
$this->ySrc,
|
||||
$this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
|
||||
$this->ySrc + $this->wTick / $root2,
|
||||
'stroke:' . $color . ';stroke-width:2;'
|
||||
);
|
||||
$this->diagram->printElementLine(
|
||||
'line',
|
||||
$this->xSrc + $this->srcDir * $this->wTick * 0.75,
|
||||
$this->ySrc,
|
||||
$this->xSrc + $this->srcDir * (0.75 - 1 / $root2) * $this->wTick,
|
||||
$this->ySrc - $this->wTick / $root2,
|
||||
'stroke:' . $color . ';stroke-width:2;'
|
||||
);
|
||||
$this->diagram->printElementLine(
|
||||
'line',
|
||||
$this->xDest + $this->destDir * $this->wTick / 2,
|
||||
$this->yDest,
|
||||
$this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
|
||||
$this->yDest + $this->wTick / $root2,
|
||||
'stroke:' . $color . ';stroke-width:2;'
|
||||
);
|
||||
$this->diagram->printElementLine(
|
||||
'line',
|
||||
$this->xDest + $this->destDir * $this->wTick / 2,
|
||||
$this->yDest,
|
||||
$this->xDest + $this->destDir * (0.5 + 1 / $root2) * $this->wTick,
|
||||
$this->yDest - $this->wTick / $root2,
|
||||
'stroke:' . $color . ';stroke-width:2;'
|
||||
);
|
||||
}
|
||||
}
|
277
admin/phpMyAdmin/libraries/classes/Plugins/Schema/Svg/Svg.php
Normal file
277
admin/phpMyAdmin/libraries/classes/Plugins/Schema/Svg/Svg.php
Normal file
|
@ -0,0 +1,277 @@
|
|||
<?php
|
||||
/**
|
||||
* Classes to create relation schema in SVG format.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Svg;
|
||||
|
||||
use PhpMyAdmin\Core;
|
||||
use PhpMyAdmin\ResponseRenderer;
|
||||
use XMLWriter;
|
||||
|
||||
use function intval;
|
||||
use function is_int;
|
||||
use function sprintf;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* This Class inherits the XMLwriter class and
|
||||
* helps in developing structure of SVG Schema Export
|
||||
*
|
||||
* @see https://www.php.net/manual/en/book.xmlwriter.php
|
||||
*/
|
||||
class Svg extends XMLWriter
|
||||
{
|
||||
/** @var string */
|
||||
public $title = '';
|
||||
|
||||
/** @var string */
|
||||
public $author = 'phpMyAdmin';
|
||||
|
||||
/** @var string */
|
||||
public $font = 'Arial';
|
||||
|
||||
/** @var int */
|
||||
public $fontSize = 12;
|
||||
|
||||
/**
|
||||
* Upon instantiation This starts writing the RelationStatsSvg XML document
|
||||
*
|
||||
* @see XMLWriter::openMemory()
|
||||
* @see XMLWriter::setIndent()
|
||||
* @see XMLWriter::startDocument()
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->openMemory();
|
||||
/*
|
||||
* Set indenting using three spaces,
|
||||
* so output is formatted
|
||||
*/
|
||||
|
||||
$this->setIndent(true);
|
||||
$this->setIndentString(' ');
|
||||
/*
|
||||
* Create the XML document
|
||||
*/
|
||||
|
||||
$this->startDocument('1.0', 'UTF-8');
|
||||
$this->startDtd('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
|
||||
$this->endDtd();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set document title
|
||||
*
|
||||
* @param string $value sets the title text
|
||||
*/
|
||||
public function setTitle($value): void
|
||||
{
|
||||
$this->title = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set document author
|
||||
*
|
||||
* @param string $value sets the author
|
||||
*/
|
||||
public function setAuthor($value): void
|
||||
{
|
||||
$this->author = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set document font
|
||||
*
|
||||
* @param string $value sets the font e.g Arial, Sans-serif etc
|
||||
*/
|
||||
public function setFont(string $value): void
|
||||
{
|
||||
$this->font = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get document font
|
||||
*
|
||||
* @return string returns the font name
|
||||
*/
|
||||
public function getFont(): string
|
||||
{
|
||||
return $this->font;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set document font size
|
||||
*
|
||||
* @param int $value sets the font size in pixels
|
||||
*/
|
||||
public function setFontSize(int $value): void
|
||||
{
|
||||
$this->fontSize = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get document font size
|
||||
*
|
||||
* @return int returns the font size
|
||||
*/
|
||||
public function getFontSize(): int
|
||||
{
|
||||
return $this->fontSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts RelationStatsSvg Document
|
||||
*
|
||||
* svg document starts by first initializing svg tag
|
||||
* which contains all the attributes and namespace that needed
|
||||
* to define the svg document
|
||||
*
|
||||
* @see XMLWriter::startElement()
|
||||
* @see XMLWriter::writeAttribute()
|
||||
*
|
||||
* @param int $width total width of the RelationStatsSvg document
|
||||
* @param int $height total height of the RelationStatsSvg document
|
||||
* @param int $x min-x of the view box
|
||||
* @param int $y min-y of the view box
|
||||
*/
|
||||
public function startSvgDoc($width, $height, $x = 0, $y = 0): void
|
||||
{
|
||||
$this->startElement('svg');
|
||||
|
||||
if (! is_int($width)) {
|
||||
$width = intval($width);
|
||||
}
|
||||
|
||||
if (! is_int($height)) {
|
||||
$height = intval($height);
|
||||
}
|
||||
|
||||
if ($x != 0 || $y != 0) {
|
||||
$this->writeAttribute('viewBox', sprintf('%d %d %d %d', $x, $y, $width, $height));
|
||||
}
|
||||
|
||||
$this->writeAttribute('width', ($width - $x) . 'px');
|
||||
$this->writeAttribute('height', ($height - $y) . 'px');
|
||||
$this->writeAttribute('xmlns', 'http://www.w3.org/2000/svg');
|
||||
$this->writeAttribute('version', '1.1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends RelationStatsSvg Document
|
||||
*
|
||||
* @see XMLWriter::endElement()
|
||||
* @see XMLWriter::endDocument()
|
||||
*/
|
||||
public function endSvgDoc(): void
|
||||
{
|
||||
$this->endElement();
|
||||
$this->endDocument();
|
||||
}
|
||||
|
||||
/**
|
||||
* output RelationStatsSvg Document
|
||||
*
|
||||
* svg document prompted to the user for download
|
||||
* RelationStatsSvg document saved in .svg extension and can be
|
||||
* easily changeable by using any svg IDE
|
||||
*
|
||||
* @see XMLWriter::startElement()
|
||||
* @see XMLWriter::writeAttribute()
|
||||
*
|
||||
* @param string $fileName file name
|
||||
*/
|
||||
public function showOutput($fileName): void
|
||||
{
|
||||
//ob_get_clean();
|
||||
$output = $this->flush();
|
||||
ResponseRenderer::getInstance()->disable();
|
||||
Core::downloadHeader(
|
||||
$fileName,
|
||||
'image/svg+xml',
|
||||
strlen($output)
|
||||
);
|
||||
print $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws RelationStatsSvg elements
|
||||
*
|
||||
* SVG has some predefined shape elements like rectangle & text
|
||||
* and other elements who have x,y co-ordinates are drawn.
|
||||
* specify their width and height and can give styles too.
|
||||
*
|
||||
* @see XMLWriter::startElement()
|
||||
* @see XMLWriter::writeAttribute()
|
||||
* @see XMLWriter::text()
|
||||
* @see XMLWriter::endElement()
|
||||
*
|
||||
* @param string $name RelationStatsSvg element name
|
||||
* @param int $x The x attr defines the left position of the element
|
||||
* (e.g. x="0" places the element 0 pixels from the
|
||||
* left of the browser window)
|
||||
* @param int $y The y attribute defines the top position of the
|
||||
* element (e.g. y="0" places the element 0 pixels
|
||||
* from the top of the browser window)
|
||||
* @param int|string $width The width attribute defines the width the element
|
||||
* @param int|string $height The height attribute defines the height the element
|
||||
* @param string|null $text The text attribute defines the text the element
|
||||
* @param string $styles The style attribute defines the style the element
|
||||
* styles can be defined like CSS styles
|
||||
*/
|
||||
public function printElement(
|
||||
$name,
|
||||
$x,
|
||||
$y,
|
||||
$width = '',
|
||||
$height = '',
|
||||
?string $text = '',
|
||||
$styles = ''
|
||||
): void {
|
||||
$this->startElement($name);
|
||||
$this->writeAttribute('width', (string) $width);
|
||||
$this->writeAttribute('height', (string) $height);
|
||||
$this->writeAttribute('x', (string) $x);
|
||||
$this->writeAttribute('y', (string) $y);
|
||||
$this->writeAttribute('style', (string) $styles);
|
||||
if (isset($text)) {
|
||||
$this->writeAttribute('font-family', (string) $this->font);
|
||||
$this->writeAttribute('font-size', $this->fontSize . 'px');
|
||||
$this->text($text);
|
||||
}
|
||||
|
||||
$this->endElement();
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws RelationStatsSvg Line element
|
||||
*
|
||||
* RelationStatsSvg line element is drawn for connecting the tables.
|
||||
* arrows are also drawn by specify its start and ending
|
||||
* co-ordinates
|
||||
*
|
||||
* @see XMLWriter::startElement()
|
||||
* @see XMLWriter::writeAttribute()
|
||||
* @see XMLWriter::endElement()
|
||||
*
|
||||
* @param string $name RelationStatsSvg element name i.e line
|
||||
* @param int $x1 Defines the start of the line on the x-axis
|
||||
* @param int $y1 Defines the start of the line on the y-axis
|
||||
* @param int $x2 Defines the end of the line on the x-axis
|
||||
* @param int $y2 Defines the end of the line on the y-axis
|
||||
* @param string $styles The style attribute defines the style the element
|
||||
* styles can be defined like CSS styles
|
||||
*/
|
||||
public function printElementLine($name, $x1, $y1, $x2, $y2, $styles): void
|
||||
{
|
||||
$this->startElement($name);
|
||||
$this->writeAttribute('x1', (string) $x1);
|
||||
$this->writeAttribute('y1', (string) $y1);
|
||||
$this->writeAttribute('x2', (string) $x2);
|
||||
$this->writeAttribute('y2', (string) $y2);
|
||||
$this->writeAttribute('style', (string) $styles);
|
||||
$this->endElement();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,286 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains PhpMyAdmin\Plugins\Schema\Svg\RelationStatsSvg class
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Svg;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\Dia\TableStatsDia;
|
||||
use PhpMyAdmin\Plugins\Schema\Eps\TableStatsEps;
|
||||
use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
|
||||
use PhpMyAdmin\Plugins\Schema\Pdf\TableStatsPdf;
|
||||
use PhpMyAdmin\Version;
|
||||
|
||||
use function __;
|
||||
use function in_array;
|
||||
use function max;
|
||||
use function min;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* RelationStatsSvg Relation Schema Class
|
||||
*
|
||||
* Purpose of this class is to generate the SVG XML Document because
|
||||
* SVG defines the graphics in XML format which is used for representing
|
||||
* the database diagrams as vector image. This class actually helps
|
||||
* in preparing SVG XML format.
|
||||
*
|
||||
* SVG XML is generated by using XMLWriter php extension and this class
|
||||
* inherits ExportRelationSchema class has common functionality added
|
||||
* to this class
|
||||
*
|
||||
* @property Svg $diagram
|
||||
*/
|
||||
class SvgRelationSchema extends ExportRelationSchema
|
||||
{
|
||||
/** @var TableStatsDia[]|TableStatsEps[]|TableStatsPdf[]|TableStatsSvg[] */
|
||||
private $tables = [];
|
||||
|
||||
/** @var RelationStatsSvg[] Relations */
|
||||
private $relations = [];
|
||||
|
||||
/** @var int|float */
|
||||
private $xMax = 0;
|
||||
|
||||
/** @var int|float */
|
||||
private $yMax = 0;
|
||||
|
||||
/** @var int|float */
|
||||
private $xMin = 100000;
|
||||
|
||||
/** @var int|float */
|
||||
private $yMin = 100000;
|
||||
|
||||
/** @var int */
|
||||
private $tablewidth = 0;
|
||||
|
||||
/**
|
||||
* Upon instantiation This starts writing the SVG XML document
|
||||
* user will be prompted for download as .svg extension
|
||||
*
|
||||
* @see PMA_SVG
|
||||
*
|
||||
* @param string $db database name
|
||||
*/
|
||||
public function __construct($db)
|
||||
{
|
||||
parent::__construct($db, new Svg());
|
||||
|
||||
$this->setShowColor(isset($_REQUEST['svg_show_color']));
|
||||
$this->setShowKeys(isset($_REQUEST['svg_show_keys']));
|
||||
$this->setTableDimension(isset($_REQUEST['svg_show_table_dimension']));
|
||||
$this->setAllTablesSameWidth(isset($_REQUEST['svg_all_tables_same_width']));
|
||||
|
||||
$this->diagram->setTitle(
|
||||
sprintf(
|
||||
__('Schema of the %s database - Page %s'),
|
||||
$this->db,
|
||||
$this->pageNumber
|
||||
)
|
||||
);
|
||||
$this->diagram->setAuthor('phpMyAdmin ' . Version::VERSION);
|
||||
$this->diagram->setFont('Arial');
|
||||
$this->diagram->setFontSize(16);
|
||||
|
||||
$alltables = $this->getTablesFromRequest();
|
||||
|
||||
foreach ($alltables as $table) {
|
||||
if (! isset($this->tables[$table])) {
|
||||
$this->tables[$table] = new TableStatsSvg(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$table,
|
||||
$this->diagram->getFont(),
|
||||
$this->diagram->getFontSize(),
|
||||
$this->pageNumber,
|
||||
$this->tablewidth,
|
||||
$this->showKeys,
|
||||
$this->tableDimension,
|
||||
$this->offline
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->sameWide) {
|
||||
$this->tables[$table]->width = &$this->tablewidth;
|
||||
}
|
||||
|
||||
$this->setMinMax($this->tables[$table]);
|
||||
}
|
||||
|
||||
$border = 15;
|
||||
$this->diagram->startSvgDoc(
|
||||
$this->xMax + $border,
|
||||
$this->yMax + $border,
|
||||
$this->xMin - $border,
|
||||
$this->yMin - $border
|
||||
);
|
||||
|
||||
$seen_a_relation = false;
|
||||
foreach ($alltables as $one_table) {
|
||||
$exist_rel = $this->relation->getForeigners($this->db, $one_table, '', 'both');
|
||||
if (! $exist_rel) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$seen_a_relation = true;
|
||||
foreach ($exist_rel as $master_field => $rel) {
|
||||
/* put the foreign table on the schema only if selected
|
||||
* by the user
|
||||
* (do not use array_search() because we would have to
|
||||
* to do a === false and this is not PHP3 compatible)
|
||||
*/
|
||||
if ($master_field !== 'foreign_keys_data') {
|
||||
if (in_array($rel['foreign_table'], $alltables)) {
|
||||
$this->addRelation(
|
||||
$one_table,
|
||||
$this->diagram->getFont(),
|
||||
$this->diagram->getFontSize(),
|
||||
$master_field,
|
||||
$rel['foreign_table'],
|
||||
$rel['foreign_field'],
|
||||
$this->tableDimension
|
||||
);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($rel as $one_key) {
|
||||
if (! in_array($one_key['ref_table_name'], $alltables)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($one_key['index_list'] as $index => $one_field) {
|
||||
$this->addRelation(
|
||||
$one_table,
|
||||
$this->diagram->getFont(),
|
||||
$this->diagram->getFontSize(),
|
||||
$one_field,
|
||||
$one_key['ref_table_name'],
|
||||
$one_key['ref_index_list'][$index],
|
||||
$this->tableDimension
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($seen_a_relation) {
|
||||
$this->drawRelations();
|
||||
}
|
||||
|
||||
$this->drawTables();
|
||||
$this->diagram->endSvgDoc();
|
||||
}
|
||||
|
||||
/**
|
||||
* Output RelationStatsSvg Document for download
|
||||
*/
|
||||
public function showOutput(): void
|
||||
{
|
||||
$this->diagram->showOutput($this->getFileName('.svg'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets X and Y minimum and maximum for a table cell
|
||||
*
|
||||
* @param TableStatsSvg $table The table
|
||||
*/
|
||||
private function setMinMax($table): void
|
||||
{
|
||||
$this->xMax = max($this->xMax, $table->x + $table->width);
|
||||
$this->yMax = max($this->yMax, $table->y + $table->height);
|
||||
$this->xMin = min($this->xMin, $table->x);
|
||||
$this->yMin = min($this->yMin, $table->y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines relation objects
|
||||
*
|
||||
* @see setMinMax,TableStatsSvg::__construct(),
|
||||
* PhpMyAdmin\Plugins\Schema\Svg\RelationStatsSvg::__construct()
|
||||
*
|
||||
* @param string $masterTable The master table name
|
||||
* @param string $font The font face
|
||||
* @param int $fontSize Font size
|
||||
* @param string $masterField The relation field in the master table
|
||||
* @param string $foreignTable The foreign table name
|
||||
* @param string $foreignField The relation field in the foreign table
|
||||
* @param bool $tableDimension Whether to display table position or not
|
||||
*/
|
||||
private function addRelation(
|
||||
$masterTable,
|
||||
$font,
|
||||
$fontSize,
|
||||
$masterField,
|
||||
$foreignTable,
|
||||
$foreignField,
|
||||
$tableDimension
|
||||
): void {
|
||||
if (! isset($this->tables[$masterTable])) {
|
||||
$this->tables[$masterTable] = new TableStatsSvg(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$masterTable,
|
||||
$font,
|
||||
$fontSize,
|
||||
$this->pageNumber,
|
||||
$this->tablewidth,
|
||||
false,
|
||||
$tableDimension
|
||||
);
|
||||
$this->setMinMax($this->tables[$masterTable]);
|
||||
}
|
||||
|
||||
if (! isset($this->tables[$foreignTable])) {
|
||||
$this->tables[$foreignTable] = new TableStatsSvg(
|
||||
$this->diagram,
|
||||
$this->db,
|
||||
$foreignTable,
|
||||
$font,
|
||||
$fontSize,
|
||||
$this->pageNumber,
|
||||
$this->tablewidth,
|
||||
false,
|
||||
$tableDimension
|
||||
);
|
||||
$this->setMinMax($this->tables[$foreignTable]);
|
||||
}
|
||||
|
||||
$this->relations[] = new RelationStatsSvg(
|
||||
$this->diagram,
|
||||
$this->tables[$masterTable],
|
||||
$masterField,
|
||||
$this->tables[$foreignTable],
|
||||
$foreignField
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws relation arrows and lines
|
||||
* connects master table's master field to
|
||||
* foreign table's foreign field
|
||||
*
|
||||
* @see Relation_Stats_Svg::relationDraw()
|
||||
*/
|
||||
private function drawRelations(): void
|
||||
{
|
||||
foreach ($this->relations as $relation) {
|
||||
$relation->relationDraw($this->showColor);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws tables
|
||||
*
|
||||
* @see TableStatsSvg::Table_Stats_tableDraw()
|
||||
*/
|
||||
private function drawTables(): void
|
||||
{
|
||||
foreach ($this->tables as $table) {
|
||||
$table->tableDraw($this->showColor);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,190 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains PhpMyAdmin\Plugins\Schema\Svg\TableStatsSvg class
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema\Svg;
|
||||
|
||||
use PhpMyAdmin\Plugins\Schema\ExportRelationSchema;
|
||||
use PhpMyAdmin\Plugins\Schema\TableStats;
|
||||
|
||||
use function __;
|
||||
use function count;
|
||||
use function in_array;
|
||||
use function max;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Table preferences/statistics
|
||||
*
|
||||
* This class preserves the table co-ordinates,fields
|
||||
* and helps in drawing/generating the Tables in SVG XML document.
|
||||
*
|
||||
* @see Svg
|
||||
*
|
||||
* @property Svg $diagram
|
||||
*/
|
||||
class TableStatsSvg extends TableStats
|
||||
{
|
||||
/** @var int */
|
||||
public $height;
|
||||
|
||||
/** @var int */
|
||||
public $currentCell = 0;
|
||||
|
||||
/**
|
||||
* @see Svg
|
||||
* @see TableStatsSvg::setWidthTable
|
||||
* @see TableStatsSvg::setHeightTable
|
||||
*
|
||||
* @param Svg $diagram The current SVG image document
|
||||
* @param string $db The database name
|
||||
* @param string $tableName The table name
|
||||
* @param string $font Font face
|
||||
* @param int $fontSize The font size
|
||||
* @param int $pageNumber Page number
|
||||
* @param int $same_wide_width The max. width among tables
|
||||
* @param bool $showKeys Whether to display keys or not
|
||||
* @param bool $tableDimension Whether to display table position or not
|
||||
* @param bool $offline Whether the coordinates are sent
|
||||
*/
|
||||
public function __construct(
|
||||
$diagram,
|
||||
$db,
|
||||
$tableName,
|
||||
$font,
|
||||
$fontSize,
|
||||
$pageNumber,
|
||||
&$same_wide_width,
|
||||
$showKeys = false,
|
||||
$tableDimension = false,
|
||||
$offline = false
|
||||
) {
|
||||
parent::__construct($diagram, $db, $pageNumber, $tableName, $showKeys, $tableDimension, $offline);
|
||||
|
||||
// height and width
|
||||
$this->setHeightTable($fontSize);
|
||||
// setWidth must me after setHeight, because title
|
||||
// can include table height which changes table width
|
||||
$this->setWidthTable($font, $fontSize);
|
||||
if ($same_wide_width >= $this->width) {
|
||||
return;
|
||||
}
|
||||
|
||||
$same_wide_width = $this->width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays an error when the table cannot be found.
|
||||
*/
|
||||
protected function showMissingTableError(): void
|
||||
{
|
||||
ExportRelationSchema::dieSchema(
|
||||
$this->pageNumber,
|
||||
'SVG',
|
||||
sprintf(__('The %s table doesn\'t exist!'), $this->tableName)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the width of the table
|
||||
*
|
||||
* @see PMA_SVG
|
||||
*
|
||||
* @param string $font The font size
|
||||
* @param int $fontSize The font size
|
||||
*/
|
||||
private function setWidthTable($font, $fontSize): void
|
||||
{
|
||||
foreach ($this->fields as $field) {
|
||||
$this->width = max(
|
||||
$this->width,
|
||||
$this->font->getStringWidth($field, $font, $fontSize)
|
||||
);
|
||||
}
|
||||
|
||||
$this->width += $this->font->getStringWidth(' ', $font, $fontSize);
|
||||
|
||||
/*
|
||||
* it is unknown what value must be added, because
|
||||
* table title is affected by the table width value
|
||||
*/
|
||||
while ($this->width < $this->font->getStringWidth($this->getTitle(), $font, $fontSize)) {
|
||||
$this->width += 7;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the height of the table
|
||||
*
|
||||
* @param int $fontSize font size
|
||||
*/
|
||||
private function setHeightTable($fontSize): void
|
||||
{
|
||||
$this->heightCell = $fontSize + 4;
|
||||
$this->height = (count($this->fields) + 1) * $this->heightCell;
|
||||
}
|
||||
|
||||
/**
|
||||
* draw the table
|
||||
*
|
||||
* @see Svg::printElement
|
||||
*
|
||||
* @param bool $showColor Whether to display color
|
||||
*/
|
||||
public function tableDraw($showColor): void
|
||||
{
|
||||
$this->diagram->printElement(
|
||||
'rect',
|
||||
$this->x,
|
||||
$this->y,
|
||||
$this->width,
|
||||
$this->heightCell,
|
||||
null,
|
||||
'fill:#007;stroke:black;'
|
||||
);
|
||||
$this->diagram->printElement(
|
||||
'text',
|
||||
$this->x + 5,
|
||||
$this->y + 14,
|
||||
$this->width,
|
||||
$this->heightCell,
|
||||
$this->getTitle(),
|
||||
'fill:#fff;'
|
||||
);
|
||||
foreach ($this->fields as $field) {
|
||||
$this->currentCell += $this->heightCell;
|
||||
$fillColor = 'none';
|
||||
if ($showColor) {
|
||||
if (in_array($field, $this->primary)) {
|
||||
$fillColor = '#aea';
|
||||
}
|
||||
|
||||
if ($field == $this->displayfield) {
|
||||
$fillColor = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
$this->diagram->printElement(
|
||||
'rect',
|
||||
$this->x,
|
||||
$this->y + $this->currentCell,
|
||||
$this->width,
|
||||
$this->heightCell,
|
||||
null,
|
||||
'fill:' . $fillColor . ';stroke:black;'
|
||||
);
|
||||
$this->diagram->printElement(
|
||||
'text',
|
||||
$this->x + 5,
|
||||
$this->y + 14 + $this->currentCell,
|
||||
$this->width,
|
||||
$this->heightCell,
|
||||
$field,
|
||||
'fill:black;'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
225
admin/phpMyAdmin/libraries/classes/Plugins/Schema/TableStats.php
Normal file
225
admin/phpMyAdmin/libraries/classes/Plugins/Schema/TableStats.php
Normal file
|
@ -0,0 +1,225 @@
|
|||
<?php
|
||||
/**
|
||||
* Contains abstract class to hold table preferences/statistics
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Plugins\Schema;
|
||||
|
||||
use PhpMyAdmin\ConfigStorage\Relation;
|
||||
use PhpMyAdmin\Font;
|
||||
use PhpMyAdmin\Index;
|
||||
use PhpMyAdmin\Util;
|
||||
|
||||
use function array_flip;
|
||||
use function array_keys;
|
||||
use function array_merge;
|
||||
use function is_array;
|
||||
use function rawurldecode;
|
||||
use function sprintf;
|
||||
|
||||
/**
|
||||
* Table preferences/statistics
|
||||
*
|
||||
* This class preserves the table co-ordinates,fields
|
||||
* and helps in drawing/generating the tables.
|
||||
*
|
||||
* @abstract
|
||||
*/
|
||||
abstract class TableStats
|
||||
{
|
||||
/** @var Dia\Dia|Eps\Eps|Pdf\Pdf|Svg\Svg */
|
||||
protected $diagram;
|
||||
|
||||
/** @var string */
|
||||
protected $db;
|
||||
|
||||
/** @var int */
|
||||
protected $pageNumber;
|
||||
|
||||
/** @var string */
|
||||
protected $tableName;
|
||||
|
||||
/** @var bool */
|
||||
protected $showKeys;
|
||||
|
||||
/** @var bool */
|
||||
protected $tableDimension;
|
||||
|
||||
/** @var mixed */
|
||||
public $displayfield;
|
||||
|
||||
/** @var array */
|
||||
public $fields = [];
|
||||
|
||||
/** @var array */
|
||||
public $primary = [];
|
||||
|
||||
/** @var int|float */
|
||||
public $x = 0;
|
||||
|
||||
/** @var int|float */
|
||||
public $y = 0;
|
||||
|
||||
/** @var int */
|
||||
public $width = 0;
|
||||
|
||||
/** @var int */
|
||||
public $heightCell = 0;
|
||||
|
||||
/** @var bool */
|
||||
protected $offline;
|
||||
|
||||
/** @var Relation */
|
||||
protected $relation;
|
||||
|
||||
/** @var Font */
|
||||
protected $font;
|
||||
|
||||
/**
|
||||
* @param Pdf\Pdf|Svg\Svg|Eps\Eps|Dia\Dia $diagram schema diagram
|
||||
* @param string $db current db name
|
||||
* @param int $pageNumber current page number (from the
|
||||
* $cfg['Servers'][$i]['table_coords'] table)
|
||||
* @param string $tableName table name
|
||||
* @param bool $showKeys whether to display keys or not
|
||||
* @param bool $tableDimension whether to display table position or not
|
||||
* @param bool $offline whether the coordinates are sent from the browser
|
||||
*/
|
||||
public function __construct(
|
||||
$diagram,
|
||||
$db,
|
||||
$pageNumber,
|
||||
$tableName,
|
||||
$showKeys,
|
||||
$tableDimension,
|
||||
$offline
|
||||
) {
|
||||
global $dbi;
|
||||
|
||||
$this->diagram = $diagram;
|
||||
$this->db = $db;
|
||||
$this->pageNumber = $pageNumber;
|
||||
$this->tableName = $tableName;
|
||||
|
||||
$this->showKeys = $showKeys;
|
||||
$this->tableDimension = $tableDimension;
|
||||
|
||||
$this->offline = $offline;
|
||||
|
||||
$this->relation = new Relation($dbi);
|
||||
$this->font = new Font();
|
||||
|
||||
// checks whether the table exists
|
||||
// and loads fields
|
||||
$this->validateTableAndLoadFields();
|
||||
// load table coordinates
|
||||
$this->loadCoordinates();
|
||||
// loads display field
|
||||
$this->loadDisplayField();
|
||||
// loads primary keys
|
||||
$this->loadPrimaryKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate whether the table exists.
|
||||
*/
|
||||
protected function validateTableAndLoadFields(): void
|
||||
{
|
||||
global $dbi;
|
||||
|
||||
$sql = 'DESCRIBE ' . Util::backquote($this->tableName);
|
||||
$result = $dbi->tryQuery($sql);
|
||||
if (! $result || ! $result->numRows()) {
|
||||
$this->showMissingTableError();
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($this->showKeys) {
|
||||
$indexes = Index::getFromTable($this->tableName, $this->db);
|
||||
$all_columns = [];
|
||||
foreach ($indexes as $index) {
|
||||
$all_columns = array_merge(
|
||||
$all_columns,
|
||||
array_flip(array_keys($index->getColumns()))
|
||||
);
|
||||
}
|
||||
|
||||
$this->fields = array_keys($all_columns);
|
||||
} else {
|
||||
$this->fields = $result->fetchAllColumn();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays an error when the table cannot be found.
|
||||
*
|
||||
* @abstract
|
||||
*/
|
||||
abstract protected function showMissingTableError(): void;
|
||||
|
||||
/**
|
||||
* Loads coordinates of a table
|
||||
*/
|
||||
protected function loadCoordinates(): void
|
||||
{
|
||||
if (! isset($_POST['t_h']) || ! is_array($_POST['t_h'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (array_keys($_POST['t_h']) as $key) {
|
||||
$db = rawurldecode($_POST['t_db'][$key]);
|
||||
$tbl = rawurldecode($_POST['t_tbl'][$key]);
|
||||
if ($this->db . '.' . $this->tableName === $db . '.' . $tbl) {
|
||||
$this->x = (float) $_POST['t_x'][$key];
|
||||
$this->y = (float) $_POST['t_y'][$key];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the table's display field
|
||||
*/
|
||||
protected function loadDisplayField(): void
|
||||
{
|
||||
$this->displayfield = $this->relation->getDisplayField($this->db, $this->tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the PRIMARY key.
|
||||
*/
|
||||
protected function loadPrimaryKey(): void
|
||||
{
|
||||
global $dbi;
|
||||
|
||||
$result = $dbi->query('SHOW INDEX FROM ' . Util::backquote($this->tableName) . ';');
|
||||
if ($result->numRows() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
while ($row = $result->fetchAssoc()) {
|
||||
if ($row['Key_name'] !== 'PRIMARY') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->primary[] = $row['Column_name'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns title of the current table,
|
||||
* title can have the dimensions/co-ordinates of the table
|
||||
*
|
||||
* @return string title of the current table
|
||||
*/
|
||||
protected function getTitle()
|
||||
{
|
||||
return ($this->tableDimension
|
||||
? sprintf('%.0fx%0.f', $this->width, $this->heightCell)
|
||||
: ''
|
||||
)
|
||||
. ' ' . $this->tableName;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue