diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2021-09-10 21:52:48 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2021-09-10 21:52:48 +0300 |
commit | 38fda677bdfbfbe4314b2853b852a67598b7f2f2 (patch) | |
tree | 7a3ee17b6e7311b9add61411a3ea35233122e074 | |
parent | 94e714a49ba306461d2935906e6fc1a7b8f4addb (diff) |
Extract Processes class from ProcessesController
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
-rw-r--r-- | libraries/classes/Controllers/Server/Status/ProcessesController.php | 185 | ||||
-rw-r--r-- | libraries/classes/Server/Status/Processes.php | 194 | ||||
-rw-r--r-- | libraries/services.php | 4 | ||||
-rw-r--r-- | libraries/services_controllers.php | 1 | ||||
-rw-r--r-- | psalm-baseline.xml | 44 | ||||
-rw-r--r-- | test/classes/Controllers/Server/Status/ProcessesControllerTest.php | 17 |
6 files changed, 245 insertions, 200 deletions
diff --git a/libraries/classes/Controllers/Server/Status/ProcessesController.php b/libraries/classes/Controllers/Server/Status/ProcessesController.php index 7008ccbe18..00e1fb1a60 100644 --- a/libraries/classes/Controllers/Server/Status/ProcessesController.php +++ b/libraries/classes/Controllers/Server/Status/ProcessesController.php @@ -5,36 +5,34 @@ declare(strict_types=1); namespace PhpMyAdmin\Controllers\Server\Status; use PhpMyAdmin\DatabaseInterface; -use PhpMyAdmin\Html\Generator; use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Message; use PhpMyAdmin\ResponseRenderer; use PhpMyAdmin\Server\Status\Data; +use PhpMyAdmin\Server\Status\Processes; use PhpMyAdmin\Template; use PhpMyAdmin\Url; -use PhpMyAdmin\Util; use function __; -use function array_keys; -use function count; -use function mb_strtolower; -use function strlen; -use function ucfirst; class ProcessesController extends AbstractController { /** @var DatabaseInterface */ private $dbi; + /** @var Processes */ + private $processes; + /** * @param ResponseRenderer $response * @param Data $data * @param DatabaseInterface $dbi */ - public function __construct($response, Template $template, $data, $dbi) + public function __construct($response, Template $template, $data, $dbi, Processes $processes) { parent::__construct($response, $template, $data); $this->dbi = $dbi; + $this->processes = $processes; } public function index(): void @@ -69,12 +67,12 @@ class ProcessesController extends AbstractController 'sort_order' => $params['sort_order'] ?? '', ]; - $serverProcessList = $this->getList($params); + $listHtml = $this->template->render('server/status/processes/list', $this->processes->getList($params)); $this->render('server/status/processes/index', [ 'url_params' => $urlParams, 'is_checked' => $isChecked, - 'server_process_list' => $serverProcessList, + 'server_process_list' => $listHtml, ]); } @@ -95,7 +93,7 @@ class ProcessesController extends AbstractController return; } - $this->response->addHTML($this->getList($params)); + $this->render('server/status/processes/list', $this->processes->getList($params)); } /** @@ -129,169 +127,4 @@ class ProcessesController extends AbstractController $this->response->addJSON(['message' => $message]); } - - /** - * @param array $params Request parameters - */ - private function getList(array $params): string - { - $urlParams = []; - - $showFullSql = ! empty($params['full']); - if ($showFullSql) { - $urlParams['full'] = ''; - } else { - $urlParams['full'] = 1; - } - - $sqlQuery = $showFullSql - ? 'SHOW FULL PROCESSLIST' - : 'SHOW PROCESSLIST'; - if ( - (! empty($params['order_by_field']) - && ! empty($params['sort_order'])) - || ! empty($params['showExecuting']) - ) { - $urlParams['order_by_field'] = $params['order_by_field']; - $urlParams['sort_order'] = $params['sort_order']; - $urlParams['showExecuting'] = $params['showExecuting']; - $sqlQuery = 'SELECT * FROM `INFORMATION_SCHEMA`.`PROCESSLIST` '; - } - - if (! empty($params['showExecuting'])) { - $sqlQuery .= ' WHERE state != "" '; - } - - if (! empty($params['order_by_field']) && ! empty($params['sort_order'])) { - $sqlQuery .= ' ORDER BY ' - . Util::backquote($params['order_by_field']) - . ' ' . $params['sort_order']; - } - - $result = $this->dbi->query($sqlQuery); - $rows = []; - while ($process = $this->dbi->fetchAssoc($result)) { - // Array keys need to modify due to the way it has used - // to display column values - if ( - (! empty($params['order_by_field']) && ! empty($params['sort_order'])) - || ! empty($params['showExecuting']) - ) { - foreach (array_keys($process) as $key) { - $newKey = ucfirst(mb_strtolower($key)); - if ($newKey === $key) { - continue; - } - - $process[$newKey] = $process[$key]; - unset($process[$key]); - } - } - - $rows[] = [ - 'id' => $process['Id'], - 'user' => $process['User'], - 'host' => $process['Host'], - 'db' => ! isset($process['db']) || strlen($process['db']) === 0 ? '' : $process['db'], - 'command' => $process['Command'], - 'time' => $process['Time'], - 'state' => ! empty($process['State']) ? $process['State'] : '---', - 'progress' => ! empty($process['Progress']) ? $process['Progress'] : '---', - 'info' => ! empty($process['Info']) ? Generator::formatSql( - $process['Info'], - ! $showFullSql - ) : '---', - ]; - } - - return $this->template->render('server/status/processes/list', [ - 'columns' => $this->getSortableColumnsForProcessList($showFullSql, $params), - 'rows' => $rows, - 'refresh_params' => $urlParams, - ]); - } - - private function getSortableColumnsForProcessList(bool $showFullSql, array $params): array - { - // This array contains display name and real column name of each - // sortable column in the table - $sortableColumns = [ - [ - 'column_name' => __('ID'), - 'order_by_field' => 'Id', - ], - [ - 'column_name' => __('User'), - 'order_by_field' => 'User', - ], - [ - 'column_name' => __('Host'), - 'order_by_field' => 'Host', - ], - [ - 'column_name' => __('Database'), - 'order_by_field' => 'db', - ], - [ - 'column_name' => __('Command'), - 'order_by_field' => 'Command', - ], - [ - 'column_name' => __('Time'), - 'order_by_field' => 'Time', - ], - [ - 'column_name' => __('Status'), - 'order_by_field' => 'State', - ], - [ - 'column_name' => __('Progress'), - 'order_by_field' => 'Progress', - ], - [ - 'column_name' => __('SQL query'), - 'order_by_field' => 'Info', - ], - ]; - - $sortableColCount = count($sortableColumns); - - $columns = []; - foreach ($sortableColumns as $columnKey => $column) { - $is_sorted = ! empty($params['order_by_field']) - && ! empty($params['sort_order']) - && ($params['order_by_field'] == $column['order_by_field']); - - $column['sort_order'] = 'ASC'; - if ($is_sorted && $params['sort_order'] === 'ASC') { - $column['sort_order'] = 'DESC'; - } - - if (isset($params['showExecuting'])) { - $column['showExecuting'] = 'on'; - } - - $columns[$columnKey] = [ - 'name' => $column['column_name'], - 'params' => $column, - 'is_sorted' => $is_sorted, - 'sort_order' => $column['sort_order'], - 'has_full_query' => false, - 'is_full' => false, - ]; - - if (0 !== --$sortableColCount) { - continue; - } - - $columns[$columnKey]['has_full_query'] = true; - if (! $showFullSql) { - continue; - } - - $columns[$columnKey]['is_full'] = true; - } - - return $columns; - } } diff --git a/libraries/classes/Server/Status/Processes.php b/libraries/classes/Server/Status/Processes.php new file mode 100644 index 0000000000..8298a89a50 --- /dev/null +++ b/libraries/classes/Server/Status/Processes.php @@ -0,0 +1,194 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Server\Status; + +use PhpMyAdmin\DatabaseInterface; +use PhpMyAdmin\Html\Generator; +use PhpMyAdmin\Util; + +use function __; +use function array_keys; +use function count; +use function mb_strtolower; +use function strlen; +use function ucfirst; + +final class Processes +{ + /** @var DatabaseInterface */ + private $dbi; + + public function __construct(DatabaseInterface $dbi) + { + $this->dbi = $dbi; + } + + /** + * @param array $params Request parameters + * + * @return array<string, array|string> + */ + public function getList(array $params): array + { + $urlParams = []; + + $showFullSql = ! empty($params['full']); + if ($showFullSql) { + $urlParams['full'] = ''; + } else { + $urlParams['full'] = 1; + } + + $sqlQuery = $showFullSql + ? 'SHOW FULL PROCESSLIST' + : 'SHOW PROCESSLIST'; + if ( + (! empty($params['order_by_field']) + && ! empty($params['sort_order'])) + || ! empty($params['showExecuting']) + ) { + $urlParams['order_by_field'] = $params['order_by_field']; + $urlParams['sort_order'] = $params['sort_order']; + $urlParams['showExecuting'] = $params['showExecuting']; + $sqlQuery = 'SELECT * FROM `INFORMATION_SCHEMA`.`PROCESSLIST` '; + } + + if (! empty($params['showExecuting'])) { + $sqlQuery .= ' WHERE state != "" '; + } + + if (! empty($params['order_by_field']) && ! empty($params['sort_order'])) { + $sqlQuery .= ' ORDER BY ' + . Util::backquote($params['order_by_field']) + . ' ' . $params['sort_order']; + } + + $result = $this->dbi->query($sqlQuery); + $rows = []; + while ($process = $this->dbi->fetchAssoc($result)) { + // Array keys need to modify due to the way it has used + // to display column values + if ( + (! empty($params['order_by_field']) && ! empty($params['sort_order'])) + || ! empty($params['showExecuting']) + ) { + foreach (array_keys($process) as $key) { + $newKey = ucfirst(mb_strtolower($key)); + if ($newKey === $key) { + continue; + } + + $process[$newKey] = $process[$key]; + unset($process[$key]); + } + } + + $rows[] = [ + 'id' => $process['Id'], + 'user' => $process['User'], + 'host' => $process['Host'], + 'db' => ! isset($process['db']) || strlen($process['db']) === 0 ? '' : $process['db'], + 'command' => $process['Command'], + 'time' => $process['Time'], + 'state' => ! empty($process['State']) ? $process['State'] : '---', + 'progress' => ! empty($process['Progress']) ? $process['Progress'] : '---', + 'info' => ! empty($process['Info']) ? Generator::formatSql( + $process['Info'], + ! $showFullSql + ) : '---', + ]; + } + + return [ + 'columns' => $this->getSortableColumnsForProcessList($showFullSql, $params), + 'rows' => $rows, + 'refresh_params' => $urlParams, + ]; + } + + private function getSortableColumnsForProcessList(bool $showFullSql, array $params): array + { + // This array contains display name and real column name of each + // sortable column in the table + $sortableColumns = [ + [ + 'column_name' => __('ID'), + 'order_by_field' => 'Id', + ], + [ + 'column_name' => __('User'), + 'order_by_field' => 'User', + ], + [ + 'column_name' => __('Host'), + 'order_by_field' => 'Host', + ], + [ + 'column_name' => __('Database'), + 'order_by_field' => 'db', + ], + [ + 'column_name' => __('Command'), + 'order_by_field' => 'Command', + ], + [ + 'column_name' => __('Time'), + 'order_by_field' => 'Time', + ], + [ + 'column_name' => __('Status'), + 'order_by_field' => 'State', + ], + [ + 'column_name' => __('Progress'), + 'order_by_field' => 'Progress', + ], + [ + 'column_name' => __('SQL query'), + 'order_by_field' => 'Info', + ], + ]; + + $sortableColCount = count($sortableColumns); + + $columns = []; + foreach ($sortableColumns as $columnKey => $column) { + $is_sorted = ! empty($params['order_by_field']) + && ! empty($params['sort_order']) + && ($params['order_by_field'] == $column['order_by_field']); + + $column['sort_order'] = 'ASC'; + if ($is_sorted && $params['sort_order'] === 'ASC') { + $column['sort_order'] = 'DESC'; + } + + if (isset($params['showExecuting'])) { + $column['showExecuting'] = 'on'; + } + + $columns[$columnKey] = [ + 'name' => $column['column_name'], + 'params' => $column, + 'is_sorted' => $is_sorted, + 'sort_order' => $column['sort_order'], + 'has_full_query' => false, + 'is_full' => false, + ]; + + if (0 !== --$sortableColCount) { + continue; + } + + $columns[$columnKey]['has_full_query'] = true; + if (! $showFullSql) { + continue; + } + + $columns[$columnKey]['is_full'] = true; + } + + return $columns; + } +} diff --git a/libraries/services.php b/libraries/services.php index 4e518daf3a..ddc6a7cc3e 100644 --- a/libraries/services.php +++ b/libraries/services.php @@ -193,6 +193,10 @@ return [ 'class' => PhpMyAdmin\Server\Status\Monitor::class, 'arguments' => ['@dbi'], ], + 'status_processes' => [ + 'class' => PhpMyAdmin\Server\Status\Processes::class, + 'arguments' => ['@dbi'], + ], 'table_maintenance' => [ 'class' => PhpMyAdmin\Table\Maintenance::class, 'arguments' => ['$dbi' => '@dbi'], diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index d58196e5ea..4b6bfee1e8 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -885,6 +885,7 @@ return [ '$template' => '@template', '$data' => '@status_data', '$dbi' => '@dbi', + '$processes' => '@status_processes', ], ], Server\Status\QueriesController::class => [ diff --git a/psalm-baseline.xml b/psalm-baseline.xml index fb41c248d1..5dc7408881 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2785,28 +2785,6 @@ <code>$params['varValue']</code> </MixedArgument> </file> - <file src="libraries/classes/Controllers/Server/Status/ProcessesController.php"> - <MixedArgument occurrences="4"> - <code>$params['order_by_field']</code> - <code>$process['Info']</code> - <code>$process['db']</code> - <code>$result</code> - </MixedArgument> - <MixedArgumentTypeCoercion occurrences="1"> - <code>$key</code> - </MixedArgumentTypeCoercion> - <MixedAssignment occurrences="5"> - <code>$process[$newKey]</code> - <code>$result</code> - <code>$urlParams['order_by_field']</code> - <code>$urlParams['showExecuting']</code> - <code>$urlParams['sort_order']</code> - </MixedAssignment> - <MixedOperand occurrences="2"> - <code>$params['sort_order']</code> - <code>Util::backquote($params['order_by_field'])</code> - </MixedOperand> - </file> <file src="libraries/classes/Controllers/Server/Status/QueriesController.php"> <MixedArgumentTypeCoercion occurrences="1"> <code>$key</code> @@ -15083,6 +15061,28 @@ <code>$row['#']</code> </MixedOperand> </file> + <file src="libraries/classes/Server/Status/Processes.php"> + <MixedArgument occurrences="4"> + <code>$params['order_by_field']</code> + <code>$process['Info']</code> + <code>$process['db']</code> + <code>$result</code> + </MixedArgument> + <MixedArgumentTypeCoercion occurrences="1"> + <code>$key</code> + </MixedArgumentTypeCoercion> + <MixedAssignment occurrences="5"> + <code>$process[$newKey]</code> + <code>$result</code> + <code>$urlParams['order_by_field']</code> + <code>$urlParams['showExecuting']</code> + <code>$urlParams['sort_order']</code> + </MixedAssignment> + <MixedOperand occurrences="2"> + <code>$params['sort_order']</code> + <code>Util::backquote($params['order_by_field'])</code> + </MixedOperand> + </file> <file src="libraries/classes/Server/SysInfo/WindowsNt.php"> <MixedArrayAccess occurrences="6"> <code>$buffer[0]['FreePhysicalMemory']</code> diff --git a/test/classes/Controllers/Server/Status/ProcessesControllerTest.php b/test/classes/Controllers/Server/Status/ProcessesControllerTest.php index cae09c768f..af8585c206 100644 --- a/test/classes/Controllers/Server/Status/ProcessesControllerTest.php +++ b/test/classes/Controllers/Server/Status/ProcessesControllerTest.php @@ -6,6 +6,7 @@ namespace PhpMyAdmin\Tests\Controllers\Server\Status; use PhpMyAdmin\Controllers\Server\Status\ProcessesController; use PhpMyAdmin\Server\Status\Data; +use PhpMyAdmin\Server\Status\Processes; use PhpMyAdmin\Template; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\ResponseRenderer; @@ -43,7 +44,13 @@ class ProcessesControllerTest extends AbstractTestCase { $response = new ResponseRenderer(); - $controller = new ProcessesController($response, new Template(), $this->data, $GLOBALS['dbi']); + $controller = new ProcessesController( + $response, + new Template(), + $this->data, + $GLOBALS['dbi'], + new Processes($GLOBALS['dbi']) + ); $this->dummyDbi->addSelectDb('mysql'); $controller->index(); @@ -161,7 +168,13 @@ class ProcessesControllerTest extends AbstractTestCase $response = new ResponseRenderer(); $response->setAjax(true); - $controller = new ProcessesController($response, new Template(), $this->data, $GLOBALS['dbi']); + $controller = new ProcessesController( + $response, + new Template(), + $this->data, + $GLOBALS['dbi'], + new Processes($GLOBALS['dbi']) + ); $_POST['full'] = '1'; $_POST['order_by_field'] = 'process'; |