Translated using Weblate (Vietnamese)
[phpmyadmin.git] / src / Export / Options.php
blob548b1c21cecd9532bc62e2dea0d0fd2419372e15
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Export;
7 use PhpMyAdmin\Config;
8 use PhpMyAdmin\ConfigStorage\Relation;
9 use PhpMyAdmin\Core;
10 use PhpMyAdmin\DatabaseInterface;
11 use PhpMyAdmin\Encoding;
12 use PhpMyAdmin\Plugins;
13 use PhpMyAdmin\Plugins\ExportPlugin;
14 use PhpMyAdmin\Query\Utilities;
15 use PhpMyAdmin\Table\Table;
16 use PhpMyAdmin\Util;
18 use function explode;
19 use function function_exists;
20 use function in_array;
21 use function is_array;
22 use function is_string;
23 use function str_contains;
24 use function urldecode;
26 final class Options
28 public function __construct(private Relation $relation, private TemplateModel $templateModel)
32 /**
33 * Outputs appropriate checked statement for checkbox.
35 * @param string $str option name
37 private function checkboxCheck(string $str): bool
39 $config = Config::getInstance();
41 return isset($config->settings['Export'][$str])
42 && $config->settings['Export'][$str];
45 /**
46 * Prints Html For Export Selection Options
48 * @param string $tmpSelect Tmp selected method of export
50 * @return array<int, array{name: string, is_selected: bool}>
52 public function getDatabasesForSelectOptions(string $tmpSelect = ''): array
54 /** @var array|string|null $dbSelect */
55 $dbSelect = $_POST['db_select'] ?? null;
57 // Check if the selected databases are defined in $_POST
58 // (from clicking Back button on /export page)
59 if (is_string($dbSelect)) {
60 $dbSelect = urldecode($dbSelect);
61 $dbSelect = explode(',', $dbSelect);
64 $databases = [];
65 foreach (DatabaseInterface::getInstance()->getDatabaseList() as $currentDb) {
66 if (Utilities::isSystemSchema($currentDb, true)) {
67 continue;
70 $isSelected = false;
71 if (is_array($dbSelect)) {
72 if (in_array($currentDb, $dbSelect)) {
73 $isSelected = true;
75 } elseif ($tmpSelect !== '') {
76 if (str_contains(' ' . $tmpSelect, '|' . $currentDb . '|')) {
77 $isSelected = true;
79 } else {
80 $isSelected = true;
83 $databases[] = ['name' => $currentDb, 'is_selected' => $isSelected];
86 return $databases;
89 /**
90 * @param string $exportType export type: server|database|table
91 * @param string $db selected DB
92 * @param string $table selected table
93 * @param string $sqlQuery SQL query
94 * @param int|string $numTables number of tables
95 * @param int|string $unlimNumRows unlimited number of rows
96 * @param ExportPlugin[] $exportList
98 * @return array<string, mixed>
100 public function getOptions(
101 string $exportType,
102 string $db,
103 string $table,
104 string $sqlQuery,
105 int|string $numTables,
106 int|string $unlimNumRows,
107 array $exportList,
108 ): array {
109 $exportTemplatesFeature = $this->relation->getRelationParameters()->exportTemplatesFeature;
111 $templates = [];
113 $config = Config::getInstance();
114 if ($exportTemplatesFeature !== null) {
115 $templates = $this->templateModel->getAll(
116 $exportTemplatesFeature->database,
117 $exportTemplatesFeature->exportTemplates,
118 $config->selectedServer['user'],
119 $exportType,
122 $templates = is_array($templates) ? $templates : [];
125 $default = isset($_GET['what']) ? (string) $_GET['what'] : Plugins::getDefault('Export', 'format');
126 $dropdown = Plugins::getChoice($exportList, $default);
127 $tableObject = new Table($table, $db, DatabaseInterface::getInstance());
128 $rows = [];
130 if ($table !== '' && $numTables === 0 && ! $tableObject->isMerge() && $exportType !== 'raw') {
131 $rows = [
132 'allrows' => $_POST['allrows'] ?? null,
133 'limit_to' => $_POST['limit_to'] ?? null,
134 'limit_from' => $_POST['limit_from'] ?? null,
135 'unlim_num_rows' => $unlimNumRows,
136 'number_of_rows' => $tableObject->countRecords(),
140 $hasAliases = isset($_SESSION['tmpval']['aliases']) && ! Core::emptyRecursive($_SESSION['tmpval']['aliases']);
141 $aliases = $_SESSION['tmpval']['aliases'] ?? [];
142 unset($_SESSION['tmpval']['aliases']);
143 $filenameTemplate = $this->getFileNameTemplate($exportType, $_POST['filename_template'] ?? null);
144 $isEncodingSupported = Encoding::isSupported();
145 $selectedCompression = $_POST['compression'] ?? $config->settings['Export']['compression'] ?? 'none';
147 if (
148 isset($config->settings['Export']['as_separate_files']) && $config->settings['Export']['as_separate_files']
150 $selectedCompression = 'zip';
153 $hiddenInputs = [
154 'db' => $db,
155 'table' => $table,
156 'export_type' => $exportType,
157 'export_method' => $_POST['export_method'] ?? $config->settings['Export']['method'] ?? 'quick',
158 'template_id' => $_POST['template_id'] ?? '',
161 if (! empty($GLOBALS['single_table'])) {
162 $hiddenInputs['single_table'] = true;
165 if ($sqlQuery !== '') {
166 $hiddenInputs['sql_query'] = $sqlQuery;
169 return [
170 'export_type' => $exportType,
171 'db' => $db,
172 'table' => $table,
173 'templates' => [
174 'is_enabled' => $exportTemplatesFeature !== null,
175 'templates' => $templates,
176 'selected' => $_POST['template_id'] ?? null,
178 'sql_query' => $sqlQuery,
179 'hidden_inputs' => $hiddenInputs,
180 'export_method' => $_POST['quick_or_custom'] ?? $config->settings['Export']['method'] ?? '',
181 'plugins_choice' => $dropdown,
182 'options' => Plugins::getOptions('Export', $exportList),
183 'can_convert_kanji' => Encoding::canConvertKanji(),
184 'exec_time_limit' => $config->settings['ExecTimeLimit'],
185 'rows' => $rows,
186 'has_save_dir' => ! empty($config->settings['SaveDir']),
187 'save_dir' => Util::userDir($config->settings['SaveDir'] ?? ''),
188 'export_is_checked' => $this->checkboxCheck('quick_export_onserver'),
189 'export_overwrite_is_checked' => $this->checkboxCheck('quick_export_onserver_overwrite'),
190 'has_aliases' => $hasAliases,
191 'aliases' => $aliases,
192 'is_checked_lock_tables' => $this->checkboxCheck('lock_tables'),
193 'is_checked_asfile' => $this->checkboxCheck('asfile'),
194 'is_checked_as_separate_files' => $this->checkboxCheck('as_separate_files'),
195 'is_checked_export' => $this->checkboxCheck('onserver'),
196 'is_checked_export_overwrite' => $this->checkboxCheck('onserver_overwrite'),
197 'is_checked_remember_file_template' => $this->checkboxCheck('remember_file_template'),
198 'repopulate' => isset($_POST['repopulate']),
199 'lock_tables' => isset($_POST['lock_tables']),
200 'is_encoding_supported' => $isEncodingSupported,
201 'encodings' => $isEncodingSupported ? Encoding::listEncodings() : [],
202 'export_charset' => $config->settings['Export']['charset'],
203 'export_asfile' => $config->settings['Export']['asfile'],
204 'has_zip' => $config->settings['ZipDump'] && function_exists('gzcompress'),
205 'has_gzip' => $config->settings['GZipDump'] && function_exists('gzencode'),
206 'selected_compression' => $selectedCompression,
207 'filename_template' => $filenameTemplate,
211 private function getFileNameTemplate(string $exportType, string|null $filename = null): string
213 if ($filename !== null) {
214 return $filename;
217 $config = Config::getInstance();
218 if ($exportType === 'database') {
219 return (string) $config->getUserValue(
220 'pma_db_filename_template',
221 $config->settings['Export']['file_template_database'],
225 if ($exportType === 'table') {
226 return (string) $config->getUserValue(
227 'pma_table_filename_template',
228 $config->settings['Export']['file_template_table'],
232 return (string) $config->getUserValue(
233 'pma_server_filename_template',
234 $config->settings['Export']['file_template_server'],