From fdfc24604034568bc66e93ecfa57d355da73164d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 11 Apr 2020 15:47:59 -0300 Subject: [PATCH] Remove the Rte\Export class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- libraries/classes/Rte/Events.php | 60 +++++++++- libraries/classes/Rte/Export.php | 178 ----------------------------- libraries/classes/Rte/Routines.php | 67 ++++++++++- libraries/classes/Rte/Triggers.php | 64 ++++++++++- 4 files changed, 176 insertions(+), 193 deletions(-) delete mode 100644 libraries/classes/Rte/Export.php diff --git a/libraries/classes/Rte/Events.php b/libraries/classes/Rte/Events.php index 11e9bfd983..3b6c4224d3 100644 --- a/libraries/classes/Rte/Events.php +++ b/libraries/classes/Rte/Events.php @@ -27,9 +27,6 @@ use const ENT_QUOTES; */ class Events { - /** @var Export */ - private $export; - /** @var DatabaseInterface */ private $dbi; @@ -42,7 +39,6 @@ class Events public function __construct(DatabaseInterface $dbi) { $this->dbi = $dbi; - $this->export = new Export($this->dbi); $this->template = new Template(); } @@ -104,7 +100,7 @@ class Events * Process all requests */ $this->handleEditor(); - $this->export->events(); + $this->export(); $items = $this->dbi->getEvents($db); $response = Response::getInstance(); @@ -797,4 +793,58 @@ class Events } } } + + private function export(): void + { + global $db; + + if (empty($_GET['export_item']) || empty($_GET['item_name'])) { + return; + } + + $itemName = $_GET['item_name']; + $exportData = $this->dbi->getDefinition($db, 'EVENT', $itemName); + + if (! $exportData) { + $exportData = false; + } + + $response = Response::getInstance(); + + $itemName = htmlspecialchars(Util::backquote($_GET['item_name'])); + if ($exportData !== false) { + $exportData = htmlspecialchars(trim($exportData)); + $title = sprintf(__('Export of event %s'), $itemName); + + if ($response->isAjax()) { + $response->addJSON('message', $exportData); + $response->addJSON('title', $title); + + exit; + } + + $exportData = ''; + echo "
\n" . '' . $title . "\n" + . $exportData . "
\n"; + + return; + } + + $message = sprintf( + __('Error in processing request: No event with name %1$s found in database %2$s.'), + $itemName, + htmlspecialchars(Util::backquote($db)) + ); + $message = Message::error($message); + + if ($response->isAjax()) { + $response->setRequestStatus(false); + $response->addJSON('message', $message); + + exit; + } + + $message->display(); + } } diff --git a/libraries/classes/Rte/Export.php b/libraries/classes/Rte/Export.php deleted file mode 100644 index 63e0d896c1..0000000000 --- a/libraries/classes/Rte/Export.php +++ /dev/null @@ -1,178 +0,0 @@ -dbi = $dbi; - } - - /** - * This function is called from one of the other functions in this file - * and it completes the handling of the export functionality. - * - * @param string|false $export_data The SQL query to create the requested item - * @param string $type RTE type (routine|trigger|event). - * - * @return void - */ - private function handle($export_data, string $type): void - { - global $db; - - $response = Response::getInstance(); - - $exportMessage = ''; - $noViewMessage = ''; - if ($type === 'routine') { - $exportMessage = __('Export of routine %s'); - $noViewMessage = __( - 'No routine with name %1$s found in database %2$s. ' - . 'You might be lacking the necessary privileges to view/export this routine.' - ); - } elseif ($type === 'event') { - $exportMessage = __('Export of event %s'); - } elseif ($type === 'trigger') { - $exportMessage = __('Export of trigger %s'); - } - - $item_name = htmlspecialchars(Util::backquote($_GET['item_name'])); - if ($export_data !== false) { - $export_data = htmlspecialchars(trim($export_data)); - $title = sprintf($exportMessage, $item_name); - - if ($response->isAjax()) { - $response->addJSON('message', $export_data); - $response->addJSON('title', $title); - - exit; - } - - $export_data = ''; - echo "
\n" - , '' . $title . "\n" - , $export_data - , "
\n"; - - return; - } - - $_db = htmlspecialchars(Util::backquote($db)); - $message = __('Error in processing request:') . ' ' - . sprintf($noViewMessage, $item_name, $_db); - $message = Message::error($message); - - if ($response->isAjax()) { - $response->setRequestStatus(false); - $response->addJSON('message', $message); - - exit; - } - - $message->display(); - } - - /** - * If necessary, prepares event information and passes - * it to handle() for the actual export. - * - * @return void - */ - public function events(): void - { - global $db; - - if (empty($_GET['export_item']) || empty($_GET['item_name'])) { - return; - } - - $item_name = $_GET['item_name']; - $export_data = $this->dbi->getDefinition($db, 'EVENT', $item_name); - - if (! $export_data) { - $export_data = false; - } - - $this->handle($export_data, 'event'); - } - - /** - * If necessary, prepares routine information and passes - * it to handle() for the actual export. - * - * @return void - */ - public function routines(): void - { - global $db; - - if (empty($_GET['export_item']) || empty($_GET['item_name']) || empty($_GET['item_type'])) { - return; - } - - if ($_GET['item_type'] !== 'FUNCTION' && $_GET['item_type'] !== 'PROCEDURE') { - return; - } - - $rtn_definition = $this->dbi->getDefinition($db, $_GET['item_type'], $_GET['item_name']); - $export_data = false; - - if ($rtn_definition !== null) { - $export_data = "DELIMITER $$\n" . $rtn_definition . "$$\nDELIMITER ;\n"; - } - - $this->handle($export_data, 'routine'); - } - - /** - * If necessary, prepares trigger information and passes - * it to handle() for the actual export. - * - * @return void - */ - public function triggers(): void - { - global $db, $table; - - if (empty($_GET['export_item']) || empty($_GET['item_name'])) { - return; - } - - $item_name = $_GET['item_name']; - $triggers = $this->dbi->getTriggers($db, $table, ''); - $export_data = false; - - foreach ($triggers as $trigger) { - if ($trigger['name'] === $item_name) { - $export_data = $trigger['create']; - break; - } - } - - $this->handle($export_data, 'trigger'); - } -} diff --git a/libraries/classes/Rte/Routines.php b/libraries/classes/Rte/Routines.php index 1d93b2dba7..ea0e2fe895 100644 --- a/libraries/classes/Rte/Routines.php +++ b/libraries/classes/Rte/Routines.php @@ -44,9 +44,6 @@ use const ENT_QUOTES; */ class Routines { - /** @var Export */ - private $export; - /** @var DatabaseInterface */ private $dbi; @@ -59,7 +56,6 @@ class Routines public function __construct(DatabaseInterface $dbi) { $this->dbi = $dbi; - $this->export = new Export($this->dbi); $this->template = new Template(); } @@ -109,7 +105,7 @@ class Routines */ $this->handleEditor(); $this->handleExecute(); - $this->export->routines(); + $this->export(); /** * Display a list of available routines */ @@ -1863,4 +1859,65 @@ class Routines return $errors; } + + private function export(): void + { + global $db; + + if (empty($_GET['export_item']) || empty($_GET['item_name']) || empty($_GET['item_type'])) { + return; + } + + if ($_GET['item_type'] !== 'FUNCTION' && $_GET['item_type'] !== 'PROCEDURE') { + return; + } + + $routineDefinition = $this->dbi->getDefinition($db, $_GET['item_type'], $_GET['item_name']); + $exportData = false; + + if ($routineDefinition !== null) { + $exportData = "DELIMITER $$\n" . $routineDefinition . "$$\nDELIMITER ;\n"; + } + + $response = Response::getInstance(); + + $itemName = htmlspecialchars(Util::backquote($_GET['item_name'])); + if ($exportData !== false) { + $exportData = htmlspecialchars(trim($exportData)); + $title = sprintf(__('Export of routine %s'), $itemName); + + if ($response->isAjax()) { + $response->addJSON('message', $exportData); + $response->addJSON('title', $title); + + exit; + } + + $exportData = ''; + echo "
\n" . '' . $title . "\n" + . $exportData . "
\n"; + + return; + } + + $message = sprintf( + __( + 'Error in processing request: No routine with name %1$s found in database %2$s.' + . ' You might be lacking the necessary privileges to view/export this routine.' + ), + $itemName, + htmlspecialchars(Util::backquote($db)) + ); + $message = Message::error($message); + + if ($response->isAjax()) { + $response->setRequestStatus(false); + $response->addJSON('message', $message); + + exit; + } + + $message->display(); + } } diff --git a/libraries/classes/Rte/Triggers.php b/libraries/classes/Rte/Triggers.php index 4c59f04cbb..263f919a78 100644 --- a/libraries/classes/Rte/Triggers.php +++ b/libraries/classes/Rte/Triggers.php @@ -26,9 +26,6 @@ use const ENT_QUOTES; */ class Triggers { - /** @var Export */ - private $export; - /** @var DatabaseInterface */ private $dbi; @@ -41,7 +38,6 @@ class Triggers public function __construct(DatabaseInterface $dbi) { $this->dbi = $dbi; - $this->export = new Export($this->dbi); $this->template = new Template(); } @@ -80,7 +76,7 @@ class Triggers * Process all requests */ $this->handleEditor(); - $this->export->triggers(); + $this->export(); $items = $this->dbi->getTriggers($db, $table); $response = Response::getInstance(); @@ -597,4 +593,62 @@ class Triggers } } } + + private function export(): void + { + global $db, $table; + + if (empty($_GET['export_item']) || empty($_GET['item_name'])) { + return; + } + + $itemName = $_GET['item_name']; + $triggers = $this->dbi->getTriggers($db, $table, ''); + $exportData = false; + + foreach ($triggers as $trigger) { + if ($trigger['name'] === $itemName) { + $exportData = $trigger['create']; + break; + } + } + + $response = Response::getInstance(); + + $itemName = htmlspecialchars(Util::backquote($_GET['item_name'])); + if ($exportData !== false) { + $exportData = htmlspecialchars(trim($exportData)); + $title = sprintf(__('Export of trigger %s'), $itemName); + + if ($response->isAjax()) { + $response->addJSON('message', $exportData); + $response->addJSON('title', $title); + + exit; + } + + $exportData = ''; + echo "
\n" . '' . $title . "\n" + . $exportData . "
\n"; + + return; + } + + $message = sprintf( + __('Error in processing request: No trigger with name %1$s found in database %2$s.'), + $itemName, + htmlspecialchars(Util::backquote($db)) + ); + $message = Message::error($message); + + if ($response->isAjax()) { + $response->setRequestStatus(false); + $response->addJSON('message', $message); + + exit; + } + + $message->display(); + } }