| Total Complexity | 102 |
| Total Lines | 530 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ExportAdmin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExportAdmin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class ExportAdmin extends AbstractAdmin |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The databases to dump |
||
| 14 | * |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $databases; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The tables to dump |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $tables; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The dump options |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $options; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The queries generated by the dump |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $queries = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Get data for export |
||
| 42 | * |
||
| 43 | * @param string $database The database name |
||
| 44 | * @param string $table |
||
| 45 | * |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | public function getExportOptions(string $database, string $table = '') |
||
| 49 | { |
||
| 50 | // From dump.inc.php |
||
| 51 | $db_style = ['', 'USE', 'DROP+CREATE', 'CREATE']; |
||
| 52 | $table_style = ['', 'DROP+CREATE', 'CREATE']; |
||
| 53 | $data_style = ['', 'TRUNCATE+INSERT', 'INSERT']; |
||
| 54 | if ($this->driver->jush() == 'sql') { //! use insertOrUpdate() in all drivers |
||
| 55 | $data_style[] = 'INSERT+UPDATE'; |
||
| 56 | } |
||
| 57 | // \parse_str($_COOKIE['adminer_export'], $row); |
||
| 58 | // if(!$row) { |
||
| 59 | $row = [ |
||
| 60 | 'output' => 'text', |
||
| 61 | 'format' => 'sql', |
||
| 62 | 'db_style' => ($database != '' ? '' : 'CREATE'), |
||
| 63 | 'table_style' => 'DROP+CREATE', |
||
| 64 | 'data_style' => 'INSERT', |
||
| 65 | ]; |
||
| 66 | // } |
||
| 67 | // if(!isset($row['events'])) { // backwards compatibility |
||
| 68 | $row['routines'] = $row['events'] = ($table == ''); |
||
| 69 | $row['triggers'] = $row['table_style']; |
||
| 70 | // } |
||
| 71 | $options = [ |
||
| 72 | 'output' => [ |
||
| 73 | 'label' => $this->trans->lang('Output'), |
||
| 74 | 'options' => $this->util->dumpOutput(), |
||
| 75 | 'value' => $row['output'], |
||
| 76 | ], |
||
| 77 | 'format' => [ |
||
| 78 | 'label' => $this->trans->lang('Format'), |
||
| 79 | 'options' => $this->util->dumpFormat(), |
||
| 80 | 'value' => $row['format'], |
||
| 81 | ], |
||
| 82 | 'table_style' => [ |
||
| 83 | 'label' => $this->trans->lang('Tables'), |
||
| 84 | 'options' => $table_style, |
||
| 85 | 'value' => $row['table_style'], |
||
| 86 | ], |
||
| 87 | 'auto_increment' => [ |
||
| 88 | 'label' => $this->trans->lang('Auto Increment'), |
||
| 89 | 'value' => 1, |
||
| 90 | 'checked' => $row['autoIncrement'] ?? false, |
||
| 91 | ], |
||
| 92 | 'data_style' => [ |
||
| 93 | 'label' => $this->trans->lang('Data'), |
||
| 94 | 'options' => $data_style, |
||
| 95 | 'value' => $row['data_style'], |
||
| 96 | ], |
||
| 97 | ]; |
||
| 98 | if ($this->driver->jush() !== 'sqlite') { |
||
| 99 | $options['db_style'] = [ |
||
| 100 | 'label' => $this->trans->lang('Database'), |
||
| 101 | 'options' => $db_style, |
||
| 102 | 'value' => $row['db_style'], |
||
| 103 | ]; |
||
| 104 | if ($this->driver->support('routine')) { |
||
| 105 | $options['routines'] = [ |
||
| 106 | 'label' => $this->trans->lang('Routines'), |
||
| 107 | 'value' => 1, |
||
| 108 | 'checked' => $row['routines'], |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | if ($this->driver->support('event')) { |
||
| 112 | $options['events'] = [ |
||
| 113 | 'label' => $this->trans->lang('Events'), |
||
| 114 | 'value' => 1, |
||
| 115 | 'checked' => $row['events'], |
||
| 116 | ]; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | if ($this->driver->support('trigger')) { |
||
| 120 | $options['triggers'] = [ |
||
| 121 | 'label' => $this->trans->lang('Triggers'), |
||
| 122 | 'value' => 1, |
||
| 123 | 'checked' => $row['triggers'], |
||
| 124 | ]; |
||
| 125 | } |
||
| 126 | |||
| 127 | $results = [ |
||
| 128 | 'options' => $options, |
||
| 129 | 'prefixes' => [], |
||
| 130 | ]; |
||
| 131 | if (($database)) { |
||
| 132 | $tables = [ |
||
| 133 | 'headers' => [$this->trans->lang('Tables'), $this->trans->lang('Data')], |
||
| 134 | 'details' => [], |
||
| 135 | ]; |
||
| 136 | $tables_list = $this->driver->tables(); |
||
| 137 | foreach ($tables_list as $name => $type) { |
||
| 138 | $prefix = \preg_replace('~_.*~', '', $name); |
||
| 139 | //! % may be part of table name |
||
| 140 | // $checked = ($TABLE == "" || $TABLE == (\substr($TABLE, -1) == "%" ? "$prefix%" : $name)); |
||
| 141 | // $results['prefixes'][$prefix]++; |
||
| 142 | |||
| 143 | $tables['details'][] = \compact('prefix', 'name', 'type'/*, 'checked'*/); |
||
| 144 | } |
||
| 145 | $results['tables'] = $tables; |
||
| 146 | } else { |
||
| 147 | $databases = [ |
||
| 148 | 'headers' => [$this->trans->lang('Database'), $this->trans->lang('Data')], |
||
| 149 | 'details' => [], |
||
| 150 | ]; |
||
| 151 | $databases_list = $this->driver->databases(false) ?? []; |
||
| 152 | foreach ($databases_list as $name) { |
||
| 153 | if (!$this->driver->isInformationSchema($name)) { |
||
| 154 | $prefix = \preg_replace('~_.*~', '', $name); |
||
| 155 | // $results['prefixes'][$prefix]++; |
||
| 156 | |||
| 157 | $databases['details'][] = \compact('prefix', 'name'); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | $results['databases'] = $databases; |
||
| 161 | } |
||
| 162 | |||
| 163 | $results['options'] = $options; |
||
| 164 | $results['labels'] = [ |
||
| 165 | 'export' => $this->trans->lang('Export'), |
||
| 166 | ]; |
||
| 167 | return $results; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Dump routines and events in the connected database |
||
| 172 | * |
||
| 173 | * @param string $database The database name |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | protected function dumpRoutinesAndEvents(string $database) |
||
| 178 | { |
||
| 179 | // From dump.inc.php |
||
| 180 | $style = $this->options["db_style"]; |
||
| 181 | $queries = []; |
||
| 182 | |||
| 183 | if ($this->options["routines"]) { |
||
| 184 | $sql = "SHOW FUNCTION STATUS WHERE Db = " . $this->driver->quote($database); |
||
| 185 | foreach ($this->driver->rows($sql) as $row) { |
||
| 186 | $sql = "SHOW CREATE FUNCTION " . $this->driver->escapeId($row["Name"]); |
||
| 187 | $create = $this->admin->removeDefiner($this->driver->result($sql, 2)); |
||
| 188 | $queries[] = $this->driver->setUtf8mb4($create); |
||
| 189 | if ($style != 'DROP+CREATE') { |
||
| 190 | $queries[] = "DROP FUNCTION IF EXISTS " . $this->driver->escapeId($row["Name"]) . ";;"; |
||
| 191 | } |
||
| 192 | $queries[] = "$create;;\n"; |
||
| 193 | } |
||
| 194 | $sql = "SHOW PROCEDURE STATUS WHERE Db = " . $this->driver->quote($database); |
||
| 195 | foreach ($this->driver->rows($sql) as $row) { |
||
| 196 | $sql = "SHOW CREATE PROCEDURE " . $this->driver->escapeId($row["Name"]); |
||
| 197 | $create = $this->admin->removeDefiner($this->driver->result($sql, 2)); |
||
| 198 | $queries[] = $this->driver->setUtf8mb4($create); |
||
| 199 | if ($style != 'DROP+CREATE') { |
||
| 200 | $queries[] = "DROP PROCEDURE IF EXISTS " . $this->driver->escapeId($row["Name"]) . ";;"; |
||
| 201 | } |
||
| 202 | $queries[] = "$create;;\n"; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | if ($this->options["events"]) { |
||
| 207 | foreach ($this->driver->rows("SHOW EVENTS") as $row) { |
||
| 208 | $sql = "SHOW CREATE EVENT " . $this->driver->escapeId($row["Name"]); |
||
| 209 | $create = $this->admin->removeDefiner($this->driver->result($sql, 3)); |
||
| 210 | $queries[] = $this->driver->setUtf8mb4($create); |
||
| 211 | if ($style != 'DROP+CREATE') { |
||
| 212 | $queries[] = "DROP EVENT IF EXISTS " . $this->driver->escapeId($row["Name"]) . ";;"; |
||
| 213 | } |
||
| 214 | $queries[] = "$create;;\n"; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | if (\count($queries) > 0) { |
||
| 219 | $this->queries[] = "DELIMITER ;;\n"; |
||
| 220 | foreach ($queries as $query) { |
||
| 221 | $this->queries[] = $query; |
||
| 222 | } |
||
| 223 | $this->queries[] = "DELIMITER ;;\n"; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Print CSV row |
||
| 229 | * |
||
| 230 | * @param array $row |
||
| 231 | * |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | protected function dumpCsv(array $row) |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Convert a value to string |
||
| 249 | * |
||
| 250 | * @param mixed $val |
||
| 251 | * @param object $field |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | protected function convertToString($val, $field) |
||
| 256 | { |
||
| 257 | // From functions.inc.php |
||
| 258 | if ($val === null) { |
||
| 259 | return "NULL"; |
||
| 260 | } |
||
| 261 | return $this->driver->unconvertField($field, \preg_match($this->driver->numberRegex(), $field->type) && |
||
| 262 | !\preg_match('~\[~', $field->fullType) && \is_numeric($val) ? |
||
| 263 | $val : $this->driver->quote(($val === false ? 0 : $val))); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Export table structure |
||
| 268 | * |
||
| 269 | * @param string $table |
||
| 270 | * @param string $style |
||
| 271 | * @param int $is_view 0 table, 1 view, 2 temporary view table |
||
| 272 | * |
||
| 273 | * @return null prints data |
||
| 274 | */ |
||
| 275 | protected function dumpTable(string $table, string $style, int $is_view = 0) |
||
| 276 | { |
||
| 277 | // From adminer.inc.php |
||
| 278 | if ($this->options['format'] != "sql") { |
||
| 279 | $this->queries[] = "\xef\xbb\xbf"; // UTF-8 byte order mark |
||
| 280 | if ($style) { |
||
| 281 | $this->dumpCsv(\array_keys($this->driver->fields($table))); |
||
| 282 | } |
||
| 283 | return; |
||
| 284 | } |
||
| 285 | |||
| 286 | if ($is_view == 2) { |
||
| 287 | $fields = []; |
||
| 288 | foreach ($this->driver->fields($table) as $name => $field) { |
||
| 289 | $fields[] = $this->driver->escapeId($name) . ' ' . $field->fullType; |
||
| 290 | } |
||
| 291 | $create = "CREATE TABLE " . $this->driver->table($table) . " (" . \implode(", ", $fields) . ")"; |
||
| 292 | } else { |
||
| 293 | $create = $this->driver->sqlForCreateTable($table, $this->options['auto_increment'], $style); |
||
| 294 | } |
||
| 295 | $this->driver->setUtf8mb4($create); |
||
| 296 | if ($style && $create) { |
||
| 297 | if ($style == "DROP+CREATE" || $is_view == 1) { |
||
| 298 | $this->queries[] = "DROP " . ($is_view == 2 ? "VIEW" : "TABLE") . |
||
| 299 | " IF EXISTS " . $this->driver->table($table) . ';'; |
||
| 300 | } |
||
| 301 | if ($is_view == 1) { |
||
| 302 | $create = $this->admin->removeDefiner($create); |
||
| 303 | } |
||
| 304 | $this->queries[] = $create . ';'; |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | /** Export table data |
||
| 309 | * |
||
| 310 | * @param string |
||
| 311 | * @param string |
||
| 312 | * @param string |
||
| 313 | * |
||
| 314 | * @return null prints data |
||
| 315 | */ |
||
| 316 | protected function dumpData($table, $style, $query) |
||
| 381 | } |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Dump tables and views in the connected database |
||
| 387 | * |
||
| 388 | * @param string $database The database name |
||
| 389 | * |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | protected function dumpTablesAndViews(string $database) |
||
| 456 | } |
||
| 457 | |||
| 458 | // if($ext == "tar") { |
||
| 459 | // $this->queries[] = pack("x512"); |
||
| 460 | // } |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Export databases |
||
| 465 | * |
||
| 466 | * @param array $databases The databases to dump |
||
| 467 | * @param array $tables The tables to dump |
||
| 468 | * @param array $options The export options |
||
| 469 | * |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | public function exportDatabases(array $databases, array $tables, array $options) |
||
| 543 |