Initial commit

This commit is contained in:
2022-11-21 09:47:28 +01:00
commit 76cec83d26
11652 changed files with 1980467 additions and 0 deletions

View File

@ -0,0 +1,229 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PMA\TableChartController
*
* @package PMA
*/
namespace PMA\libraries\controllers\table;
use PMA\libraries\controllers\TableController;
use PMA\libraries\Message;
use PMA\libraries\Template;
use PMA\libraries\Util;
/**
* Handles table related logic
*
* @package PhpMyAdmin
*/
class TableChartController extends TableController
{
/**
* @var string $sql_query
*/
protected $sql_query;
/**
* @var string $url_query
*/
protected $url_query;
/**
* @var array $cfg
*/
protected $cfg;
/**
* Constructor
*
* @param string $sql_query Query
* @param string $url_query Query URL
* @param array $cfg Configuration
*/
public function __construct($sql_query, $url_query, $cfg)
{
parent::__construct();
$this->sql_query = $sql_query;
$this->url_query = $url_query;
$this->cfg = $cfg;
}
/**
* Execute the query and return the result
*
* @return void
*/
public function indexAction()
{
if (isset($_REQUEST['ajax_request'])
&& isset($_REQUEST['pos'])
&& isset($_REQUEST['session_max_rows'])
) {
$this->ajaxAction();
return;
}
// Throw error if no sql query is set
if (!isset($this->sql_query) || $this->sql_query == '') {
$this->response->setRequestStatus(false);
$this->response->addHTML(
Message::error(__('No SQL query was set to fetch data.'))
);
return;
}
$this->response->getHeader()->getScripts()->addFiles(
array(
'chart.js',
'tbl_chart.js',
'jqplot/jquery.jqplot.js',
'jqplot/plugins/jqplot.barRenderer.js',
'jqplot/plugins/jqplot.canvasAxisLabelRenderer.js',
'jqplot/plugins/jqplot.canvasTextRenderer.js',
'jqplot/plugins/jqplot.categoryAxisRenderer.js',
'jqplot/plugins/jqplot.dateAxisRenderer.js',
'jqplot/plugins/jqplot.pointLabels.js',
'jqplot/plugins/jqplot.pieRenderer.js',
'jqplot/plugins/jqplot.highlighter.js'
)
);
/**
* Extract values for common work
* @todo Extract common files
*/
$db = &$this->db;
$table = &$this->table;
$url_params = array();
/**
* Runs common work
*/
if (mb_strlen($this->table)) {
$url_params['goto'] = Util::getScriptNameForOption(
$this->cfg['DefaultTabTable'], 'table'
);
$url_params['back'] = 'tbl_sql.php';
include 'libraries/tbl_common.inc.php';
include 'libraries/tbl_info.inc.php';
} elseif (mb_strlen($this->db)) {
$url_params['goto'] = Util::getScriptNameForOption(
$this->cfg['DefaultTabDatabase'], 'database'
);
$url_params['back'] = 'sql.php';
include 'libraries/db_common.inc.php';
} else {
$url_params['goto'] = Util::getScriptNameForOption(
$this->cfg['DefaultTabServer'], 'server'
);
$url_params['back'] = 'sql.php';
include 'libraries/server_common.inc.php';
}
$data = array();
$result = $this->dbi->tryQuery($this->sql_query);
$fields_meta = $this->dbi->getFieldsMeta($result);
while ($row = $this->dbi->fetchAssoc($result)) {
$data[] = $row;
}
$keys = array_keys($data[0]);
$numeric_types = array('int', 'real');
$numeric_column_count = 0;
foreach ($keys as $idx => $key) {
if (in_array($fields_meta[$idx]->type, $numeric_types)) {
$numeric_column_count++;
}
}
if ($numeric_column_count == 0) {
$this->response->setRequestStatus(false);
$this->response->addJSON(
'message',
__('No numeric columns present in the table to plot.')
);
return;
}
$url_params['db'] = $this->db;
$url_params['reload'] = 1;
/**
* Displays the page
*/
$this->response->addHTML(
Template::get('table/chart/tbl_chart')->render(
array(
'url_query' => $this->url_query,
'url_params' => $url_params,
'keys' => $keys,
'fields_meta' => $fields_meta,
'numeric_types' => $numeric_types,
'numeric_column_count' => $numeric_column_count,
'sql_query' => $this->sql_query
)
)
);
}
/**
* Handle ajax request
*
* @return void
*/
public function ajaxAction()
{
/**
* Extract values for common work
* @todo Extract common files
*/
$db = &$this->db;
$table = &$this->table;
$tableLength = mb_strlen($this->table);
$dbLength = mb_strlen($this->db);
if ($tableLength && $dbLength) {
include './libraries/tbl_common.inc.php';
}
$sql_with_limit = sprintf(
'SELECT * FROM(%s) AS `temp_res` LIMIT %s, %s',
$this->sql_query,
$_REQUEST['pos'],
$_REQUEST['session_max_rows']
);
$data = array();
$result = $this->dbi->tryQuery($sql_with_limit);
while ($row = $this->dbi->fetchAssoc($result)) {
$data[] = $row;
}
if (empty($data)) {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', __('No data to display'));
return;
}
$sanitized_data = array();
foreach ($data as $data_row_number => $data_row) {
$tmp_row = array();
foreach ($data_row as $data_column => $data_value) {
$tmp_row[htmlspecialchars($data_column)] = htmlspecialchars(
$data_value
);
}
$sanitized_data[] = $tmp_row;
}
$this->response->setRequestStatus(true);
$this->response->addJSON('message', null);
$this->response->addJSON('chartData', json_encode($sanitized_data));
}
}

View File

@ -0,0 +1,213 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PMA\TableIndexesController
*
* @package PMA
*/
namespace PMA\libraries\controllers\table;
use PMA\libraries\controllers\TableController;
use PMA\libraries\Message;
use PMA\libraries\Template;
use PMA\libraries\gis\GISVisualization;
require_once 'libraries/common.inc.php';
require_once 'libraries/db_common.inc.php';
/**
* Class TableGisVisualizationController
*
* @package PMA\libraries\controllers\table
*/
class TableGisVisualizationController extends TableController
{
/**
* @var array $url_params
*/
protected $url_params;
/**
* @var string $sql_query
*/
protected $sql_query;
/**
* @var array $visualizationSettings
*/
protected $visualizationSettings;
/**
* @var \PMA\libraries\gis\GISVisualization $visualization
*/
protected $visualization;
/**
* Constructor
*
* @param string $sql_query SQL query for retrieving GIS data
* @param array $url_params array of URL parameters
* @param string $goto goto script
* @param string $back back script
* @param array $visualizationSettings visualization settings
*/
public function __construct(
$sql_query,
$url_params,
$goto,
$back,
$visualizationSettings
) {
parent::__construct();
$this->sql_query = $sql_query;
$this->url_params = $url_params;
$this->url_params['goto'] = $goto;
$this->url_params['back'] = $back;
$this->visualizationSettings = $visualizationSettings;
}
/**
* Save to file
*
* @return void
*/
public function saveToFileAction()
{
$this->response->disable();
$file_name = $this->visualizationSettings['spatialColumn'];
$save_format = $_REQUEST['fileFormat'];
$this->visualization->toFile($file_name, $save_format);
}
/**
* Index
*
* @return void
*/
public function indexAction()
{
// Throw error if no sql query is set
if (! isset($this->sql_query) || $this->sql_query == '') {
$this->response->setRequestStatus(false);
$this->response->addHTML(
Message::error(__('No SQL query was set to fetch data.'))
);
return;
}
// Execute the query and return the result
$result = $this->dbi->tryQuery($this->sql_query);
// Get the meta data of results
$meta = $this->dbi->getFieldsMeta($result);
// Find the candidate fields for label column and spatial column
$labelCandidates = array();
$spatialCandidates = array();
foreach ($meta as $column_meta) {
if ($column_meta->type == 'geometry') {
$spatialCandidates[] = $column_meta->name;
} else {
$labelCandidates[] = $column_meta->name;
}
}
// Get settings if any posted
if (PMA_isValid($_REQUEST['visualizationSettings'], 'array')) {
$this->visualizationSettings = $_REQUEST['visualizationSettings'];
}
if (!isset($this->visualizationSettings['labelColumn'])
&& isset($labelCandidates[0])
) {
$this->visualizationSettings['labelColumn'] = '';
}
// If spatial column is not set, use first geometric column as spatial column
if (! isset($this->visualizationSettings['spatialColumn'])) {
$this->visualizationSettings['spatialColumn'] = $spatialCandidates[0];
}
// Convert geometric columns from bytes to text.
$pos = isset($_REQUEST['pos']) ? $_REQUEST['pos']
: $_SESSION['tmpval']['pos'];
if (isset($_REQUEST['session_max_rows'])) {
$rows = $_REQUEST['session_max_rows'];
} else {
if ($_SESSION['tmpval']['max_rows'] != 'all') {
$rows = $_SESSION['tmpval']['max_rows'];
} else {
$rows = $GLOBALS['cfg']['MaxRows'];
}
}
$this->visualization = GISVisualization::get(
$this->sql_query,
$this->visualizationSettings,
$rows,
$pos
);
if (isset($_REQUEST['saveToFile'])) {
$this->saveToFileAction();
return;
}
$this->response->getHeader()->getScripts()->addFiles(
array(
'openlayers/OpenLayers.js',
'jquery/jquery.svg.js',
'tbl_gis_visualization.js',
)
);
// If all the rows contain SRID, use OpenStreetMaps on the initial loading.
if (! isset($_REQUEST['displayVisualization'])) {
if ($this->visualization->hasSrid()) {
$this->visualizationSettings['choice'] = 'useBaseLayer';
} else {
unset($this->visualizationSettings['choice']);
}
}
$this->visualization->setUserSpecifiedSettings($this->visualizationSettings);
if ($this->visualizationSettings != null) {
foreach ($this->visualization->getSettings() as $setting => $val) {
if (! isset($this->visualizationSettings[$setting])) {
$this->visualizationSettings[$setting] = $val;
}
}
}
/**
* Displays the page
*/
$this->url_params['sql_query'] = $this->sql_query;
$downloadUrl = 'tbl_gis_visualization.php' . PMA_URL_getCommon(
array_merge(
$this->url_params,
array(
'saveToFile' => true,
'session_max_rows' => $rows,
'pos' => $pos
)
)
);
$html = Template::get('table/gis_visualization/gis_visualization')->render(
array(
'url_params' => $this->url_params,
'downloadUrl' => $downloadUrl,
'labelCandidates' => $labelCandidates,
'spatialCandidates' => $spatialCandidates,
'visualizationSettings' => $this->visualizationSettings,
'sql_query' => $this->sql_query,
'visualization' => $this->visualization->toImage('svg'),
'drawOl' => $this->visualization->asOl()
)
);
$this->response->addHTML($html);
}
}

View File

@ -0,0 +1,173 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PMA\TableIndexesController
*
* @package PMA
*/
namespace PMA\libraries\controllers\table;
use PMA\libraries\controllers\TableController;
use PMA\libraries\Index;
use PMA\libraries\Message;
use PMA\libraries\Template;
use PMA\libraries\Util;
/**
* Class TableIndexesController
*
* @package PMA\libraries\controllers\table
*/
class TableIndexesController extends TableController
{
/**
* @var Index $index
*/
protected $index;
/**
* Constructor
*
* @param Index $index Index
*/
public function __construct($index)
{
parent::__construct();
$this->index = $index;
}
/**
* Index
*
* @return void
*/
public function indexAction()
{
if (isset($_REQUEST['do_save_data'])) {
$this->doSaveDataAction();
return;
} // end builds the new index
$this->displayFormAction();
}
/**
* Display the form to edit/create an index
*
* @return void
*/
public function displayFormAction()
{
include_once 'libraries/tbl_info.inc.php';
$add_fields = 0;
if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
// coming already from form
if (isset($_REQUEST['index']['columns']['names'])) {
$add_fields = count($_REQUEST['index']['columns']['names'])
- $this->index->getColumnCount();
}
if (isset($_REQUEST['add_fields'])) {
$add_fields += $_REQUEST['added_fields'];
}
} elseif (isset($_REQUEST['create_index'])) {
$add_fields = $_REQUEST['added_fields'];
} // end preparing form values
// Get fields and stores their name/type
if (isset($_REQUEST['create_edit_table'])) {
$fields = json_decode($_REQUEST['columns'], true);
$index_params = array(
'Non_unique' => ($_REQUEST['index']['Index_choice'] == 'UNIQUE')
? '0' : '1',
);
$this->index->set($index_params);
$add_fields = count($fields);
} else {
$fields = $this->dbi->getTable($this->db, $this->table)
->getNameAndTypeOfTheColumns();
}
$form_params = array(
'db' => $this->db,
'table' => $this->table,
);
if (isset($_REQUEST['create_index'])) {
$form_params['create_index'] = 1;
} elseif (isset($_REQUEST['old_index'])) {
$form_params['old_index'] = $_REQUEST['old_index'];
} elseif (isset($_REQUEST['index'])) {
$form_params['old_index'] = $_REQUEST['index'];
}
$this->response->getHeader()->getScripts()->addFile('indexes.js');
$this->response->addHTML(
Template::get('table/index_form')->render(
array(
'fields' => $fields,
'index' => $this->index,
'form_params' => $form_params,
'add_fields' => $add_fields
)
)
);
}
/**
* Process the data from the edit/create index form,
* run the query to build the new index
* and moves back to "tbl_sql.php"
*
* @return void
*/
public function doSaveDataAction()
{
$error = false;
$sql_query = $this->dbi->getTable($this->db, $this->table)
->getSqlQueryForIndexCreateOrEdit($this->index, $error);
// If there is a request for SQL previewing.
if (isset($_REQUEST['preview_sql'])) {
$this->response->addJSON(
'sql_data',
Template::get('preview_sql')
->render(
array(
'query_data' => $sql_query
)
)
);
} elseif (!$error) {
$this->dbi->query($sql_query);
if ($GLOBALS['is_ajax_request'] == true) {
$message = Message::success(
__('Table %1$s has been altered successfully.')
);
$message->addParam($this->table);
$this->response->addJSON(
'message', Util::getMessage($message, $sql_query, 'success')
);
$this->response->addJSON(
'index_table',
Index::getHtmlForIndexes(
$this->table, $this->db
)
);
} else {
include 'tbl_structure.php';
}
} else {
$this->response->setRequestStatus(false);
$this->response->addJSON('message', $error);
}
}
}

View File

@ -0,0 +1,362 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PMA\TableRelationController
*
* @package PMA
*/
namespace PMA\libraries\controllers\table;
require_once 'libraries/index.lib.php';
use PMA\libraries\controllers\TableController;
use PMA\libraries\DatabaseInterface;
use PMA\libraries\Index;
use PMA\libraries\Table;
use PMA\libraries\Template;
use PMA\libraries\Util;
/**
* Handles table relation logic
*
* @package PhpMyAdmin
*/
class TableRelationController extends TableController
{
/**
* @var array $options_array
*/
protected $options_array;
/**
* @var array $cfgRelation
*/
protected $cfgRelation;
/**
* @var array $existrel
*/
protected $existrel;
/**
* @var string $disp
*/
protected $disp;
/**
* @var string $tbl_storage_engine
*/
protected $tbl_storage_engine;
/**
* @var array $existrel_foreign
*/
protected $existrel_foreign;
/**
* @var Table $udp_query
*/
protected $upd_query;
/**
* Constructor
*
* @param array $options_array Options
* @param array $cfgRelation Config relation
* @param string $tbl_storage_engine Table storage engine
* @param array $existrel Relations
* @param array $existrel_foreign External relations
* @param string $disp Display
* @param string $upd_query Update query
*/
public function __construct($options_array, $cfgRelation, $tbl_storage_engine,
$existrel, $existrel_foreign, $disp, $upd_query
) {
parent::__construct();
$this->options_array = $options_array;
$this->cfgRelation = $cfgRelation;
$this->tbl_storage_engine = $tbl_storage_engine;
$this->existrel = $existrel;
$this->existrel_foreign = $existrel_foreign;
$this->disp = $disp;
$this->upd_query = $upd_query;
}
/**
* Index
*
* @return void
*/
public function indexAction()
{
// Send table of column names to populate corresponding dropdowns depending
// on the current selection
if (isset($_REQUEST['getDropdownValues'])
&& $_REQUEST['getDropdownValues'] === 'true'
) {
// if both db and table are selected
if (isset($_REQUEST['foreignTable'])) {
$this->getDropdownValueForTableAction();
} else { // if only the db is selected
$this->getDropdownValueForDbAction();
}
return;
}
$this->response->getHeader()->getScripts()->addFiles(
array(
'tbl_relation.js',
'indexes.js'
)
);
// Gets tables information
include_once 'libraries/tbl_info.inc.php';
// updates for Internal relations
if (isset($_POST['destination_db']) && $this->cfgRelation['relwork']) {
$this->updateForInternalRelationAction();
}
// updates for foreign keys
if (isset($_POST['destination_foreign_db'])) {
$this->updateForForeignKeysAction();
}
// Updates for display field
if ($this->cfgRelation['displaywork'] && isset($_POST['display_field'])) {
$this->updateForDisplayField();
}
// If we did an update, refresh our data
if (isset($_POST['destination_db']) && $this->cfgRelation['relwork']) {
$this->existrel = PMA_getForeigners(
$this->db, $this->table, '', 'internal'
);
}
if (isset($_POST['destination_foreign_db'])
&& Util::isForeignKeySupported($this->tbl_storage_engine)
) {
$this->existrel_foreign = PMA_getForeigners(
$this->db, $this->table, '', 'foreign'
);
}
if ($this->cfgRelation['displaywork']) {
$this->disp = PMA_getDisplayField($this->db, $this->table);
}
// display secondary level tabs if necessary
$engine = $this->dbi->getTable($this->db, $this->table)
->getStatusInfo('ENGINE');
$this->response->addHTML(
Template::get('table/secondary_tabs')->render(
array(
'url_params' => array(
'db' => $GLOBALS['db'],
'table' => $GLOBALS['table']
),
'engine' => $engine
)
)
);
$this->response->addHTML('<div id="structure_content">');
/**
* Dialog
*/
// Now find out the columns of our $table
// need to use DatabaseInterface::QUERY_STORE with $this->dbi->numRows()
// in mysqli
$columns = $this->dbi->getColumns($this->db, $this->table);
// common form
$this->response->addHTML(
Template::get('table/relation/common_form')->render(
array(
'db' => $this->db,
'table' => $this->table,
'columns' => $columns,
'cfgRelation' => $this->cfgRelation,
'tbl_storage_engine' => $this->tbl_storage_engine,
'existrel' => isset($this->existrel) ? $this->existrel : array(),
'existrel_foreign' => isset($this->existrel_foreign)
? $this->existrel_foreign['foreign_keys_data'] : array(),
'options_array' => $this->options_array
)
)
);
if (Util::isForeignKeySupported($this->tbl_storage_engine)) {
$this->response->addHTML(PMA_getHtmlForDisplayIndexes());
}
$this->response->addHTML('</div>');
}
/**
* Update for display field
*
* @return void
*/
public function updateForDisplayField()
{
if ($this->upd_query->updateDisplayField(
$this->disp, $_POST['display_field'], $this->cfgRelation
)
) {
$this->response->addHTML(
Util::getMessage(
__('Display column was successfully updated.'),
'', 'success'
)
);
}
}
/**
* Update for FK
*
* @return void
*/
public function updateForForeignKeysAction()
{
$multi_edit_columns_name = isset($_REQUEST['foreign_key_fields_name'])
? $_REQUEST['foreign_key_fields_name']
: null;
// (for now, one index name only; we keep the definitions if the
// foreign db is not the same)
list($html, $preview_sql_data, $display_query, $seen_error)
= $this->upd_query->updateForeignKeys(
$_POST['destination_foreign_db'],
$multi_edit_columns_name, $_POST['destination_foreign_table'],
$_POST['destination_foreign_column'], $this->options_array,
$this->table,
isset($this->existrel_foreign)
? $this->existrel_foreign['foreign_keys_data']
: null
);
$this->response->addHTML($html);
// If there is a request for SQL previewing.
if (isset($_REQUEST['preview_sql'])) {
PMA_previewSQL($preview_sql_data);
}
if (!empty($display_query) && !$seen_error) {
$GLOBALS['display_query'] = $display_query;
$this->response->addHTML(
Util::getMessage(
__('Your SQL query has been executed successfully.'),
null, 'success'
)
);
}
}
/**
* Update for internal relation
*
* @return void
*/
public function updateForInternalRelationAction()
{
$multi_edit_columns_name = isset($_REQUEST['fields_name'])
? $_REQUEST['fields_name']
: null;
if ($this->upd_query->updateInternalRelations(
$multi_edit_columns_name,
$_POST['destination_db'],
$_POST['destination_table'],
$_POST['destination_column'],
$this->cfgRelation,
isset($this->existrel) ? $this->existrel : null
)
) {
$this->response->addHTML(
Util::getMessage(
__('Internal relations were successfully updated.'),
'', 'success'
)
);
}
}
/**
* Send table columns for foreign table dropdown
*
* @return void
*
*/
public function getDropdownValueForTableAction()
{
$foreignTable = $_REQUEST['foreignTable'];
$table_obj = $this->dbi->getTable($_REQUEST['foreignDb'], $foreignTable);
// Since views do not have keys defined on them provide the full list of
// columns
if ($table_obj->isView()) {
$columnList = $table_obj->getColumns(false, false);
} else {
$columnList = $table_obj->getIndexedColumns(false, false);
}
$columns = array();
foreach ($columnList as $column) {
$columns[] = htmlspecialchars($column);
}
$this->response->addJSON('columns', $columns);
// @todo should be: $server->db($db)->table($table)->primary()
$primary = Index::getPrimary($foreignTable, $_REQUEST['foreignDb']);
if (false === $primary) {
return;
}
$this->response->addJSON('primary', array_keys($primary->getColumns()));
}
/**
* Send database selection values for dropdown
*
* @return void
*
*/
public function getDropdownValueForDbAction()
{
$tables = array();
$foreign = isset($_REQUEST['foreign']) && $_REQUEST['foreign'] === 'true';
if ($foreign) {
$query = 'SHOW TABLE STATUS FROM '
. Util::backquote($_REQUEST['foreignDb']);
$tables_rs = $this->dbi->query(
$query,
null,
DatabaseInterface::QUERY_STORE
);
while ($row = $this->dbi->fetchArray($tables_rs)) {
if (isset($row['Engine'])
&& mb_strtoupper($row['Engine']) == $this->tbl_storage_engine
) {
$tables[] = htmlspecialchars($row['Name']);
}
}
} else {
$query = 'SHOW TABLES FROM '
. Util::backquote($_REQUEST['foreignDb']);
$tables_rs = $this->dbi->query(
$query,
null,
DatabaseInterface::QUERY_STORE
);
while ($row = $this->dbi->fetchArray($tables_rs)) {
$tables[] = htmlspecialchars($row[0]);
}
}
$this->response->addJSON('tables', $tables);
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff