Total Complexity | 61 |
Total Lines | 589 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AlldbController 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 AlldbController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class AlldbController extends BaseController |
||
15 | { |
||
16 | use \PHPPgAdmin\Traits\ExportTrait; |
||
|
|||
17 | |||
18 | public $table_place = 'alldb-databases'; |
||
19 | |||
20 | public $controller_title = 'strdatabases'; |
||
21 | |||
22 | /** |
||
23 | * Default method to render the controller according to the action parameter. |
||
24 | */ |
||
25 | public function render() |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Show default list of databases in the server. |
||
94 | * |
||
95 | * @param mixed $msg |
||
96 | */ |
||
97 | public function doDefault($msg = ''): void |
||
98 | { |
||
99 | $this->printTrail('server'); |
||
100 | $this->printTabs('server', 'databases'); |
||
101 | $this->printMsg($msg); |
||
102 | $data = $this->misc->getDatabaseAccessor(); |
||
103 | |||
104 | $databases = $data->getDatabases(); |
||
105 | |||
106 | $this->view->setReloadBrowser(true); |
||
107 | |||
108 | $href = $this->misc->getHREF(); |
||
109 | $redirecturl = $this->container->getDestinationWithLastTab('database'); |
||
110 | |||
111 | $columns = [ |
||
112 | 'database' => [ |
||
113 | 'title' => $this->lang['strdatabase'], |
||
114 | 'field' => Decorator::field('datname'), |
||
115 | 'url' => \containerInstance()->subFolder . $redirecturl . '&', |
||
116 | 'vars' => ['database' => 'datname'], |
||
117 | ], |
||
118 | 'owner' => [ |
||
119 | 'title' => $this->lang['strowner'], |
||
120 | 'field' => Decorator::field('datowner'), |
||
121 | ], |
||
122 | 'encoding' => [ |
||
123 | 'title' => $this->lang['strencoding'], |
||
124 | 'field' => Decorator::field('datencoding'), |
||
125 | ], |
||
126 | |||
127 | 'tablespace' => [ |
||
128 | 'title' => $this->lang['strtablespace'], |
||
129 | 'field' => Decorator::field('tablespace'), |
||
130 | ], |
||
131 | 'dbsize' => [ |
||
132 | 'title' => $this->lang['strsize'], |
||
133 | 'field' => Decorator::field('dbsize'), |
||
134 | 'type' => 'prettysize', |
||
135 | ], |
||
136 | 'lc_collate' => [ |
||
137 | 'title' => $this->lang['strcollation'], |
||
138 | 'field' => Decorator::field('datcollate'), |
||
139 | ], |
||
140 | 'lc_ctype' => [ |
||
141 | 'title' => $this->lang['strctype'], |
||
142 | 'field' => Decorator::field('datctype'), |
||
143 | ], |
||
144 | 'actions' => [ |
||
145 | 'title' => $this->lang['stractions'], |
||
146 | ], |
||
147 | 'comment' => [ |
||
148 | 'title' => $this->lang['strcomment'], |
||
149 | 'field' => Decorator::field('datcomment'), |
||
150 | ], |
||
151 | ]; |
||
152 | |||
153 | $actions = [ |
||
154 | 'multiactions' => [ |
||
155 | 'keycols' => ['database' => 'datname'], |
||
156 | 'url' => 'alldb', |
||
157 | 'default' => null, |
||
158 | ], |
||
159 | 'drop' => [ |
||
160 | 'content' => $this->lang['strdrop'], |
||
161 | 'attr' => [ |
||
162 | 'href' => [ |
||
163 | 'url' => 'alldb', |
||
164 | 'urlvars' => [ |
||
165 | 'subject' => 'database', |
||
166 | 'action' => 'confirm_drop', |
||
167 | 'dropdatabase' => Decorator::field('datname'), |
||
168 | ], |
||
169 | ], |
||
170 | ], |
||
171 | 'multiaction' => 'confirm_drop', |
||
172 | ], |
||
173 | 'privileges' => [ |
||
174 | 'content' => $this->lang['strprivileges'], |
||
175 | 'attr' => [ |
||
176 | 'href' => [ |
||
177 | 'url' => 'privileges', |
||
178 | 'urlvars' => [ |
||
179 | 'subject' => 'database', |
||
180 | 'database' => Decorator::field('datname'), |
||
181 | ], |
||
182 | ], |
||
183 | ], |
||
184 | ], |
||
185 | ]; |
||
186 | |||
187 | if ($data->hasAlterDatabase()) { |
||
188 | $actions['alter'] = [ |
||
189 | 'content' => $this->lang['stralter'], |
||
190 | 'attr' => [ |
||
191 | 'href' => [ |
||
192 | 'url' => 'alldb', |
||
193 | 'urlvars' => [ |
||
194 | 'subject' => 'database', |
||
195 | 'action' => 'confirm_alter', |
||
196 | 'alterdatabase' => Decorator::field('datname'), |
||
197 | ], |
||
198 | ], |
||
199 | ], |
||
200 | ]; |
||
201 | } |
||
202 | |||
203 | if (!$data->hasTablespaces()) { |
||
204 | unset($columns['tablespace']); |
||
205 | } |
||
206 | |||
207 | if (!$data->hasServerAdminFuncs()) { |
||
208 | unset($columns['dbsize']); |
||
209 | } |
||
210 | |||
211 | if (!$data->hasDatabaseCollation()) { |
||
212 | unset($columns['lc_collate'], $columns['lc_ctype']); |
||
213 | } |
||
214 | |||
215 | if (!isset($data->privlist['database'])) { |
||
216 | unset($actions['privileges']); |
||
217 | } |
||
218 | |||
219 | echo $this->printTable($databases, $columns, $actions, $this->table_place, $this->lang['strnodatabases']); |
||
220 | |||
221 | $navlinks = [ |
||
222 | 'create' => [ |
||
223 | 'attr' => [ |
||
224 | 'href' => [ |
||
225 | 'url' => 'alldb', |
||
226 | 'urlvars' => [ |
||
227 | 'action' => 'create', |
||
228 | 'server' => $_REQUEST['server'], |
||
229 | ], |
||
230 | ], |
||
231 | ], |
||
232 | 'content' => $this->lang['strcreatedatabase'], |
||
233 | ], |
||
234 | ]; |
||
235 | $this->printNavLinks($navlinks, $this->table_place, \get_defined_vars()); |
||
236 | } |
||
237 | |||
238 | public function doTree() |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Display a form for alter and perform actual alter. |
||
259 | * |
||
260 | * @param mixed $confirm |
||
261 | */ |
||
262 | public function doAlter($confirm): void |
||
322 | } |
||
323 | } |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Show confirmation of drop and perform actual drop. |
||
328 | * |
||
329 | * @param mixed $confirm |
||
330 | */ |
||
331 | public function doDrop($confirm) |
||
399 | } |
||
400 | } |
||
401 | //END DROP |
||
402 | } |
||
403 | } |
||
404 | |||
405 | // END FUNCTION |
||
406 | |||
407 | /** |
||
408 | * Displays a screen where they can enter a new database. |
||
409 | * |
||
410 | * @param mixed $msg |
||
411 | */ |
||
412 | public function doCreate($msg = ''): void |
||
413 | { |
||
414 | $data = $this->misc->getDatabaseAccessor(); |
||
415 | |||
416 | $this->printTrail('server'); |
||
417 | $this->printTitle($this->lang['strcreatedatabase'], 'pg.database.create'); |
||
418 | $this->printMsg($msg); |
||
419 | |||
420 | $this->coalesceArr($_POST, 'formName', ''); |
||
421 | |||
422 | // Default encoding is that in language file |
||
423 | $this->coalesceArr($_POST, 'formEncoding', ''); |
||
424 | $this->coalesceArr($_POST, 'formTemplate', 'template1'); |
||
425 | |||
426 | $this->coalesceArr($_POST, 'formSpc', ''); |
||
427 | |||
428 | $this->coalesceArr($_POST, 'formComment', ''); |
||
429 | |||
430 | // Fetch a list of databases in the cluster |
||
431 | $templatedbs = $data->getDatabases(false); |
||
432 | |||
433 | $tablespaces = null; |
||
434 | // Fetch all tablespaces from the database |
||
435 | if ($data->hasTablespaces()) { |
||
436 | $tablespaces = $data->getTablespaces(); |
||
437 | } |
||
438 | |||
439 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/alldb" method="post">' . \PHP_EOL; |
||
440 | echo '<table>' . \PHP_EOL; |
||
441 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL; |
||
442 | echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
443 | \htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
444 | |||
445 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strtemplatedb']}</th>" . \PHP_EOL; |
||
446 | echo "\t\t<td class=\"data1\">" . \PHP_EOL; |
||
447 | echo "\t\t\t<select name=\"formTemplate\">" . \PHP_EOL; |
||
448 | // Always offer template0 and template1 |
||
449 | echo "\t\t\t\t<option value=\"template0\"", |
||
450 | ('template0' === $_POST['formTemplate']) ? ' selected="selected"' : '', '>template0</option>' . \PHP_EOL; |
||
451 | echo "\t\t\t\t<option value=\"template1\"", |
||
452 | ('template1' === $_POST['formTemplate']) ? ' selected="selected"' : '', '>template1</option>' . \PHP_EOL; |
||
453 | |||
454 | while (!$templatedbs->EOF) { |
||
455 | $dbname = \htmlspecialchars($templatedbs->fields['datname']); |
||
456 | |||
457 | if ('template1' !== $dbname) { |
||
458 | // filter out for $this->conf[show_system] users so we dont get duplicates |
||
459 | echo "\t\t\t\t<option value=\"{$dbname}\"", |
||
460 | ($dbname === $_POST['formTemplate']) ? ' selected="selected"' : '', ">{$dbname}</option>" . \PHP_EOL; |
||
461 | } |
||
462 | $templatedbs->moveNext(); |
||
463 | } |
||
464 | echo "\t\t\t</select>" . \PHP_EOL; |
||
465 | echo "\t\t</td>\n\t</tr>" . \PHP_EOL; |
||
466 | |||
467 | // ENCODING |
||
468 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strencoding']}</th>" . \PHP_EOL; |
||
469 | echo "\t\t<td class=\"data1\">" . \PHP_EOL; |
||
470 | echo "\t\t\t<select name=\"formEncoding\">" . \PHP_EOL; |
||
471 | echo "\t\t\t\t<option value=\"\"></option>" . \PHP_EOL; |
||
472 | |||
473 | foreach ($data->codemap as $key) { |
||
474 | echo "\t\t\t\t<option value=\"", \htmlspecialchars($key), '"', |
||
475 | ($key === $_POST['formEncoding']) ? ' selected="selected"' : '', '>', |
||
476 | $this->misc->printVal($key), '</option>' . \PHP_EOL; |
||
477 | } |
||
478 | echo "\t\t\t</select>" . \PHP_EOL; |
||
479 | echo "\t\t</td>\n\t</tr>" . \PHP_EOL; |
||
480 | |||
481 | if ($data->hasDatabaseCollation()) { |
||
482 | $this->coalesceArr($_POST, 'formCollate', ''); |
||
483 | |||
484 | $this->coalesceArr($_POST, 'formCType', ''); |
||
485 | |||
486 | // LC_COLLATE |
||
487 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcollation']}</th>" . \PHP_EOL; |
||
488 | echo "\t\t<td class=\"data1\">" . \PHP_EOL; |
||
489 | echo "\t\t\t<input name=\"formCollate\" value=\"", \htmlspecialchars($_POST['formCollate']), '" />' . \PHP_EOL; |
||
490 | echo "\t\t</td>\n\t</tr>" . \PHP_EOL; |
||
491 | |||
492 | // LC_CTYPE |
||
493 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strctype']}</th>" . \PHP_EOL; |
||
494 | echo "\t\t<td class=\"data1\">" . \PHP_EOL; |
||
495 | echo "\t\t\t<input name=\"formCType\" value=\"", \htmlspecialchars($_POST['formCType']), '" />' . \PHP_EOL; |
||
496 | echo "\t\t</td>\n\t</tr>" . \PHP_EOL; |
||
497 | } |
||
498 | |||
499 | // Tablespace (if there are any) |
||
500 | if ($data->hasTablespaces() && 0 < $tablespaces->recordCount()) { |
||
501 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strtablespace']}</th>" . \PHP_EOL; |
||
502 | echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">" . \PHP_EOL; |
||
503 | // Always offer the default (empty) option |
||
504 | echo "\t\t\t\t<option value=\"\"", |
||
505 | ('' === $_POST['formSpc']) ? ' selected="selected"' : '', '></option>' . \PHP_EOL; |
||
506 | // Display all other tablespaces |
||
507 | while (!$tablespaces->EOF) { |
||
508 | $spcname = \htmlspecialchars($tablespaces->fields['spcname'] ?? ''); |
||
509 | echo "\t\t\t\t<option value=\"{$spcname}\"", |
||
510 | ($spcname === $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>" . \PHP_EOL; |
||
511 | $tablespaces->moveNext(); |
||
512 | } |
||
513 | echo "\t\t\t</select>\n\t\t</td>\n\t</tr>" . \PHP_EOL; |
||
514 | } |
||
515 | |||
516 | // Comments (if available) |
||
517 | if ($data->hasSharedComments()) { |
||
518 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>" . \PHP_EOL; |
||
519 | echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
520 | \htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>" . \PHP_EOL; |
||
521 | } |
||
522 | |||
523 | echo '</table>' . \PHP_EOL; |
||
524 | echo '<p><input type="hidden" name="action" value="save_create" />' . \PHP_EOL; |
||
525 | echo $this->view->form; |
||
526 | echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />" . \PHP_EOL; |
||
527 | echo \sprintf('<input type="submit" name="cancel" value="%s" /></p>%s', $this->lang['strcancel'], \PHP_EOL); |
||
528 | echo '</form>' . \PHP_EOL; |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Actually creates the new view in the database. |
||
533 | */ |
||
534 | public function doSaveCreate(): void |
||
535 | { |
||
536 | $data = $this->misc->getDatabaseAccessor(); |
||
537 | |||
538 | // Default tablespace to null if it isn't set |
||
539 | $this->coalesceArr($_POST, 'formSpc', ''); |
||
540 | |||
541 | // Default comment to blank if it isn't set |
||
542 | $this->coalesceArr($_POST, 'formComment', ''); |
||
543 | |||
544 | // Default collate to blank if it isn't set |
||
545 | $this->coalesceArr($_POST, 'formCollate', ''); |
||
546 | |||
547 | // Default ctype to blank if it isn't set |
||
548 | $this->coalesceArr($_POST, 'formCType', ''); |
||
549 | |||
550 | // Check that they've given a name and a definition |
||
551 | if ('' === $_POST['formName']) { |
||
552 | $this->doCreate($this->lang['strdatabaseneedsname']); |
||
553 | } else { |
||
554 | $status = $data->createDatabase( |
||
555 | $_POST['formName'], |
||
556 | $_POST['formEncoding'], |
||
557 | $_POST['formSpc'], |
||
558 | $_POST['formComment'], |
||
559 | $_POST['formTemplate'], |
||
560 | $_POST['formCollate'], |
||
561 | $_POST['formCType'] |
||
562 | ); |
||
563 | |||
564 | if (0 === $status) { |
||
565 | $this->view->setReloadBrowser(true); |
||
566 | $this->doDefault($this->lang['strdatabasecreated']); |
||
567 | } else { |
||
568 | $this->doCreate($this->lang['strdatabasecreatedbad']); |
||
569 | } |
||
570 | } |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * Displays options for cluster download. |
||
575 | * |
||
576 | * @param mixed $msg |
||
577 | */ |
||
578 | public function doExport($msg = ''): void |
||
603 | } |
||
604 | } |
||
605 |