Total Complexity | 74 |
Total Lines | 600 |
Duplicated Lines | 7.5 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
10 | class AlldbController extends BaseController |
||
11 | { |
||
12 | public $_name = 'AlldbController'; |
||
13 | public $table_place = 'alldb-databases'; |
||
14 | |||
15 | public function render() |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Show default list of databases in the server. |
||
77 | */ |
||
78 | public function doDefault($msg = '') |
||
79 | { |
||
80 | $conf = $this->conf; |
||
81 | $misc = $this->misc; |
||
82 | $lang = $this->lang; |
||
83 | |||
84 | $this->printTrail('server'); |
||
85 | $this->printTabs('server', 'databases'); |
||
86 | $this->printMsg($msg); |
||
87 | $data = $misc->getDatabaseAccessor(); |
||
88 | $databases = $data->getDatabases(); |
||
89 | |||
90 | $columns = [ |
||
91 | 'database' => [ |
||
92 | 'title' => $lang['strdatabase'], |
||
93 | 'field' => Decorator::field('datname'), |
||
94 | 'url' => SUBFOLDER."/redirect/database?{$misc->href}&", |
||
95 | 'vars' => ['database' => 'datname'], |
||
96 | ], |
||
97 | 'owner' => [ |
||
98 | 'title' => $lang['strowner'], |
||
99 | 'field' => Decorator::field('datowner'), |
||
100 | ], |
||
101 | 'encoding' => [ |
||
102 | 'title' => $lang['strencoding'], |
||
103 | 'field' => Decorator::field('datencoding'), |
||
104 | ], |
||
105 | 'lc_collate' => [ |
||
106 | 'title' => $lang['strcollation'], |
||
107 | 'field' => Decorator::field('datcollate'), |
||
108 | ], |
||
109 | 'lc_ctype' => [ |
||
110 | 'title' => $lang['strctype'], |
||
111 | 'field' => Decorator::field('datctype'), |
||
112 | ], |
||
113 | 'tablespace' => [ |
||
114 | 'title' => $lang['strtablespace'], |
||
115 | 'field' => Decorator::field('tablespace'), |
||
116 | ], |
||
117 | 'dbsize' => [ |
||
118 | 'title' => $lang['strsize'], |
||
119 | 'field' => Decorator::field('dbsize'), |
||
120 | 'type' => 'prettysize', |
||
121 | ], |
||
122 | 'actions' => [ |
||
123 | 'title' => $lang['stractions'], |
||
124 | ], |
||
125 | 'comment' => [ |
||
126 | 'title' => $lang['strcomment'], |
||
127 | 'field' => Decorator::field('datcomment'), |
||
128 | ], |
||
129 | ]; |
||
130 | |||
131 | $actions = [ |
||
132 | 'multiactions' => [ |
||
133 | 'keycols' => ['database' => 'datname'], |
||
134 | 'url' => 'alldb.php', |
||
135 | 'default' => null, |
||
136 | ], |
||
137 | 'drop' => [ |
||
138 | 'content' => $lang['strdrop'], |
||
139 | 'attr' => [ |
||
140 | 'href' => [ |
||
141 | 'url' => 'alldb.php', |
||
142 | 'urlvars' => [ |
||
143 | 'subject' => 'database', |
||
144 | 'action' => 'confirm_drop', |
||
145 | 'dropdatabase' => Decorator::field('datname'), |
||
146 | ], |
||
147 | ], |
||
148 | ], |
||
149 | 'multiaction' => 'confirm_drop', |
||
150 | ], |
||
151 | 'privileges' => [ |
||
152 | 'content' => $lang['strprivileges'], |
||
153 | 'attr' => [ |
||
154 | 'href' => [ |
||
155 | 'url' => 'privileges.php', |
||
156 | 'urlvars' => [ |
||
157 | 'subject' => 'database', |
||
158 | 'database' => Decorator::field('datname'), |
||
159 | ], |
||
160 | ], |
||
161 | ], |
||
162 | ], |
||
163 | ]; |
||
164 | if ($data->hasAlterDatabase()) { |
||
165 | $actions['alter'] = [ |
||
166 | 'content' => $lang['stralter'], |
||
167 | 'attr' => [ |
||
168 | 'href' => [ |
||
169 | 'url' => 'alldb.php', |
||
170 | 'urlvars' => [ |
||
171 | 'subject' => 'database', |
||
172 | 'action' => 'confirm_alter', |
||
173 | 'alterdatabase' => Decorator::field('datname'), |
||
174 | ], |
||
175 | ], |
||
176 | ], |
||
177 | ]; |
||
178 | } |
||
179 | |||
180 | if (!$data->hasTablespaces()) { |
||
181 | unset($columns['tablespace']); |
||
182 | } |
||
183 | |||
184 | if (!$data->hasServerAdminFuncs()) { |
||
185 | unset($columns['dbsize']); |
||
186 | } |
||
187 | |||
188 | if (!$data->hasDatabaseCollation()) { |
||
189 | unset($columns['lc_collate'], $columns['lc_ctype']); |
||
190 | } |
||
191 | |||
192 | if (!isset($data->privlist['database'])) { |
||
193 | unset($actions['privileges']); |
||
194 | } |
||
195 | |||
196 | echo $this->printTable($databases, $columns, $actions, $this->table_place, $lang['strnodatabases']); |
||
197 | |||
198 | $navlinks = [ |
||
199 | 'create' => [ |
||
200 | 'attr' => [ |
||
201 | 'href' => [ |
||
202 | 'url' => 'alldb.php', |
||
203 | 'urlvars' => [ |
||
204 | 'action' => 'create', |
||
205 | 'server' => $_REQUEST['server'], |
||
206 | ], |
||
207 | ], |
||
208 | ], |
||
209 | 'content' => $lang['strcreatedatabase'], |
||
210 | ], |
||
211 | ]; |
||
212 | $this->printNavLinks($navlinks, $this->table_place, get_defined_vars()); |
||
213 | } |
||
214 | |||
215 | View Code Duplication | public function doTree() |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * Display a form for alter and perform actual alter. |
||
242 | */ |
||
243 | public function doAlter($confirm) |
||
244 | { |
||
245 | $conf = $this->conf; |
||
246 | $misc = $this->misc; |
||
247 | $lang = $this->lang; |
||
248 | $data = $misc->getDatabaseAccessor(); |
||
249 | |||
250 | if ($confirm) { |
||
251 | $this->printTrail('database'); |
||
252 | $this->printTitle($lang['stralter'], 'pg.database.alter'); |
||
253 | |||
254 | echo '<form action="'.SUBFOLDER."/src/views/alldb.php\" method=\"post\">\n"; |
||
255 | echo "<table>\n"; |
||
256 | echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
257 | echo '<td class="data1">'; |
||
258 | echo "<input name=\"newname\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
259 | htmlspecialchars($_REQUEST['alterdatabase']), "\" /></td></tr>\n"; |
||
260 | |||
261 | if ($data->hasAlterDatabaseOwner() && $data->isSuperUser()) { |
||
262 | // Fetch all users |
||
263 | |||
264 | $rs = $data->getDatabaseOwner($_REQUEST['alterdatabase']); |
||
265 | $owner = isset($rs->fields['usename']) ? $rs->fields['usename'] : ''; |
||
266 | $users = $data->getUsers(); |
||
267 | |||
268 | echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n"; |
||
269 | echo '<td class="data1"><select name="owner">'; |
||
270 | while (!$users->EOF) { |
||
271 | $uname = $users->fields['usename']; |
||
272 | echo '<option value="', htmlspecialchars($uname), '"', |
||
273 | ($uname == $owner) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n"; |
||
274 | $users->moveNext(); |
||
275 | } |
||
276 | echo "</select></td></tr>\n"; |
||
277 | } |
||
278 | if ($data->hasSharedComments()) { |
||
279 | $rs = $data->getDatabaseComment($_REQUEST['alterdatabase']); |
||
280 | $comment = isset($rs->fields['description']) ? $rs->fields['description'] : ''; |
||
281 | echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
282 | echo '<td class="data1">'; |
||
283 | echo '<textarea rows="3" cols="32" name="dbcomment">', |
||
284 | htmlspecialchars($comment), "</textarea></td></tr>\n"; |
||
285 | } |
||
286 | echo "</table>\n"; |
||
287 | echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n"; |
||
288 | echo $misc->form; |
||
289 | echo '<input type="hidden" name="oldname" value="', |
||
290 | htmlspecialchars($_REQUEST['alterdatabase']), "\" />\n"; |
||
291 | echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n"; |
||
292 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n"; |
||
293 | echo "</form>\n"; |
||
294 | } else { |
||
295 | if (!isset($_POST['owner'])) { |
||
296 | $_POST['owner'] = ''; |
||
297 | } |
||
298 | |||
299 | if (!isset($_POST['dbcomment'])) { |
||
300 | $_POST['dbcomment'] = ''; |
||
301 | } |
||
302 | |||
303 | if ($data->alterDatabase($_POST['oldname'], $_POST['newname'], $_POST['owner'], $_POST['dbcomment']) == 0) { |
||
304 | $this->misc->setReloadBrowser(true); |
||
305 | $this->doDefault($lang['strdatabasealtered']); |
||
306 | } else { |
||
307 | $this->doDefault($lang['strdatabasealteredbad']); |
||
308 | } |
||
309 | } |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Show confirmation of drop and perform actual drop. |
||
314 | */ |
||
315 | public function doDrop($confirm) |
||
373 | } |
||
374 | } |
||
375 | } //END DROP |
||
376 | } |
||
377 | |||
378 | // END FUNCTION |
||
379 | |||
380 | /** |
||
381 | * Displays a screen where they can enter a new database. |
||
382 | */ |
||
383 | public function doCreate($msg = '') |
||
384 | { |
||
385 | $conf = $this->conf; |
||
386 | $misc = $this->misc; |
||
387 | $lang = $this->lang; |
||
388 | $data = $misc->getDatabaseAccessor(); |
||
389 | |||
390 | $this->printTrail('server'); |
||
391 | $this->printTitle($lang['strcreatedatabase'], 'pg.database.create'); |
||
392 | $this->printMsg($msg); |
||
393 | |||
394 | if (!isset($_POST['formName'])) { |
||
395 | $_POST['formName'] = ''; |
||
396 | } |
||
397 | |||
398 | // Default encoding is that in language file |
||
399 | if (!isset($_POST['formEncoding'])) { |
||
400 | $_POST['formEncoding'] = ''; |
||
401 | } |
||
402 | if (!isset($_POST['formTemplate'])) { |
||
403 | $_POST['formTemplate'] = 'template1'; |
||
404 | } |
||
405 | |||
406 | if (!isset($_POST['formSpc'])) { |
||
407 | $_POST['formSpc'] = ''; |
||
408 | } |
||
409 | |||
410 | if (!isset($_POST['formComment'])) { |
||
411 | $_POST['formComment'] = ''; |
||
412 | } |
||
413 | |||
414 | // Fetch a list of databases in the cluster |
||
415 | $templatedbs = $data->getDatabases(false); |
||
416 | |||
417 | // Fetch all tablespaces from the database |
||
418 | if ($data->hasTablespaces()) { |
||
419 | $tablespaces = $data->getTablespaces(); |
||
420 | } |
||
421 | |||
422 | echo '<form action="'.SUBFOLDER."/src/views/alldb.php\" method=\"post\">\n"; |
||
423 | echo "<table>\n"; |
||
424 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
425 | echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
426 | htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>\n"; |
||
427 | |||
428 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strtemplatedb']}</th>\n"; |
||
429 | echo "\t\t<td class=\"data1\">\n"; |
||
430 | echo "\t\t\t<select name=\"formTemplate\">\n"; |
||
431 | // Always offer template0 and template1 |
||
432 | echo "\t\t\t\t<option value=\"template0\"", |
||
433 | ($_POST['formTemplate'] == 'template0') ? ' selected="selected"' : '', ">template0</option>\n"; |
||
434 | echo "\t\t\t\t<option value=\"template1\"", |
||
435 | ($_POST['formTemplate'] == 'template1') ? ' selected="selected"' : '', ">template1</option>\n"; |
||
436 | while (!$templatedbs->EOF) { |
||
437 | $dbname = htmlspecialchars($templatedbs->fields['datname']); |
||
438 | if ($dbname != 'template1') { |
||
439 | // filter out for $conf[show_system] users so we dont get duplicates |
||
440 | echo "\t\t\t\t<option value=\"{$dbname}\"", |
||
441 | ($dbname == $_POST['formTemplate']) ? ' selected="selected"' : '', ">{$dbname}</option>\n"; |
||
442 | } |
||
443 | $templatedbs->moveNext(); |
||
444 | } |
||
445 | echo "\t\t\t</select>\n"; |
||
446 | echo "\t\t</td>\n\t</tr>\n"; |
||
447 | |||
448 | // ENCODING |
||
449 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strencoding']}</th>\n"; |
||
450 | echo "\t\t<td class=\"data1\">\n"; |
||
451 | echo "\t\t\t<select name=\"formEncoding\">\n"; |
||
452 | echo "\t\t\t\t<option value=\"\"></option>\n"; |
||
453 | while (list($key) = each($data->codemap)) { |
||
454 | echo "\t\t\t\t<option value=\"", htmlspecialchars($key), '"', |
||
455 | ($key == $_POST['formEncoding']) ? ' selected="selected"' : '', '>', |
||
456 | $misc->printVal($key), "</option>\n"; |
||
457 | } |
||
458 | echo "\t\t\t</select>\n"; |
||
459 | echo "\t\t</td>\n\t</tr>\n"; |
||
460 | |||
461 | if ($data->hasDatabaseCollation()) { |
||
462 | if (!isset($_POST['formCollate'])) { |
||
463 | $_POST['formCollate'] = ''; |
||
464 | } |
||
465 | |||
466 | if (!isset($_POST['formCType'])) { |
||
467 | $_POST['formCType'] = ''; |
||
468 | } |
||
469 | |||
470 | // LC_COLLATE |
||
471 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcollation']}</th>\n"; |
||
472 | echo "\t\t<td class=\"data1\">\n"; |
||
473 | echo "\t\t\t<input name=\"formCollate\" value=\"", htmlspecialchars($_POST['formCollate']), "\" />\n"; |
||
474 | echo "\t\t</td>\n\t</tr>\n"; |
||
475 | |||
476 | // LC_CTYPE |
||
477 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strctype']}</th>\n"; |
||
478 | echo "\t\t<td class=\"data1\">\n"; |
||
479 | echo "\t\t\t<input name=\"formCType\" value=\"", htmlspecialchars($_POST['formCType']), "\" />\n"; |
||
480 | echo "\t\t</td>\n\t</tr>\n"; |
||
481 | } |
||
482 | |||
483 | // Tablespace (if there are any) |
||
484 | View Code Duplication | if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) { |
|
485 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n"; |
||
486 | echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">\n"; |
||
487 | // Always offer the default (empty) option |
||
488 | echo "\t\t\t\t<option value=\"\"", |
||
489 | ($_POST['formSpc'] == '') ? ' selected="selected"' : '', "></option>\n"; |
||
490 | // Display all other tablespaces |
||
491 | while (!$tablespaces->EOF) { |
||
492 | $spcname = htmlspecialchars($tablespaces->fields['spcname']); |
||
493 | echo "\t\t\t\t<option value=\"{$spcname}\"", |
||
494 | ($spcname == $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>\n"; |
||
495 | $tablespaces->moveNext(); |
||
496 | } |
||
497 | echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n"; |
||
498 | } |
||
499 | |||
500 | // Comments (if available) |
||
501 | View Code Duplication | if ($data->hasSharedComments()) { |
|
502 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
503 | echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
504 | htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
505 | } |
||
506 | |||
507 | echo "</table>\n"; |
||
508 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
509 | echo $misc->form; |
||
510 | echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n"; |
||
511 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
512 | echo "</form>\n"; |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Actually creates the new view in the database. |
||
517 | */ |
||
518 | public function doSaveCreate() |
||
519 | { |
||
520 | $conf = $this->conf; |
||
521 | $misc = $this->misc; |
||
522 | $lang = $this->lang; |
||
523 | $data = $misc->getDatabaseAccessor(); |
||
524 | |||
525 | // Default tablespace to null if it isn't set |
||
526 | if (!isset($_POST['formSpc'])) { |
||
527 | $_POST['formSpc'] = null; |
||
528 | } |
||
529 | |||
530 | // Default comment to blank if it isn't set |
||
531 | if (!isset($_POST['formComment'])) { |
||
532 | $_POST['formComment'] = null; |
||
533 | } |
||
534 | |||
535 | // Default collate to blank if it isn't set |
||
536 | if (!isset($_POST['formCollate'])) { |
||
537 | $_POST['formCollate'] = null; |
||
538 | } |
||
539 | |||
540 | // Default ctype to blank if it isn't set |
||
541 | if (!isset($_POST['formCType'])) { |
||
542 | $_POST['formCType'] = null; |
||
543 | } |
||
544 | |||
545 | // Check that they've given a name and a definition |
||
546 | if ($_POST['formName'] == '') { |
||
547 | $this->doCreate($lang['strdatabaseneedsname']); |
||
548 | } else { |
||
549 | $status = $data->createDatabase($_POST['formName'], $_POST['formEncoding'], $_POST['formSpc'], |
||
550 | $_POST['formComment'], $_POST['formTemplate'], $_POST['formCollate'], $_POST['formCType']); |
||
551 | if ($status == 0) { |
||
552 | $this->misc->setReloadBrowser(true); |
||
553 | $this->doDefault($lang['strdatabasecreated']); |
||
554 | } else { |
||
555 | $this->doCreate($lang['strdatabasecreatedbad']); |
||
556 | } |
||
557 | } |
||
558 | } |
||
559 | |||
560 | /** |
||
561 | * Displays options for cluster download. |
||
562 | */ |
||
563 | public function doExport($msg = '') |
||
610 | } |
||
611 | } |
||
612 |