Passed
Pull Request — develop (#262)
by Felipe
04:12
created

AdminTrait::adminActions()   C

Complexity

Conditions 17
Paths 57

Size

Total Lines 75
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 17
eloc 51
c 2
b 1
f 0
nc 57
nop 2
dl 0
loc 75
rs 5.2166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// declare(strict_types=1);
4
5
/**
6
 * PHPPgAdmin vv6.0.0-RC8-16-g13de173f
7
 *
8
 */
0 ignored issues
show
Coding Style introduced by
Additional blank lines found at end of doc comment
Loading history...
9
10
namespace PHPPgAdmin\Traits;
11
12
use PHPPgAdmin\Decorators\Decorator;
13
14
/**
15
 * Common trait for admin features.
16
 */
17
trait AdminTrait
18
{
19
    /**
20
     * Show confirmation of cluster.
21
     *
22
     * @param mixed $type
23
     */
24
    public function confirmCluster($type): void
25
    {
26
        $this->script = ('database' === $type) ? 'database' : 'tables';
27
28
        $script = $this->script;
29
30
        if (('table' === $type) && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
31
            $this->doDefault($this->lang['strspecifytabletocluster']);
32
33
            return;
34
        }
35
36
        if (isset($_REQUEST['ma'])) {
37
            $this->printTrail('schema');
38
            $this->printTitle($this->lang['strclusterindex'], 'pg.index.cluster');
39
40
            echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
41
42
            foreach ($_REQUEST['ma'] as $v) {
43
                $a = \unserialize(\htmlspecialchars_decode($v, \ENT_QUOTES));
44
                echo '<p>', \sprintf($this->lang['strconfclustertable'], $this->misc->printVal($a['table'])), '</p>' . \PHP_EOL;
45
                echo '<input type="hidden" name="table[]" value="', \htmlspecialchars($a['table']), '" />' . \PHP_EOL;
46
            } //  END if multi cluster
47
        } else {
48
            $this->printTrail($type);
49
            $this->printTitle($this->lang['strclusterindex'], 'pg.index.cluster');
50
51
            echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
52
53
            if ('table' === $type) {
54
                echo '<p>', \sprintf($this->lang['strconfclustertable'], $this->misc->printVal($_REQUEST['object'])), '</p>' . \PHP_EOL;
55
                echo '<input type="hidden" name="table" value="', \htmlspecialchars($_REQUEST['object']), '" />' . \PHP_EOL;
56
            } else {
57
                echo '<p>', \sprintf($this->lang['strconfclusterdatabase'], $this->misc->printVal($_REQUEST['object'])), '</p>' . \PHP_EOL;
58
                echo '<input type="hidden" name="table" value="" />' . \PHP_EOL;
59
            }
60
        }
61
        echo '<input type="hidden" name="action" value="cluster" />' . \PHP_EOL;
62
63
        echo $this->misc->form;
64
65
        echo "<input type=\"submit\" name=\"cluster\" value=\"{$this->lang['strcluster']}\" />\n"; //TODO
66
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />" . \PHP_EOL;
67
        echo "</form>\n"; //  END single cluster
68
    }
69
70
    /**
71
     * Show confirmation of reindex.
72
     *
73
     * @param mixed $type
74
     */
75
    public function confirmReindex($type): void
76
    {
77
        $this->script = ('database' === $type) ? 'database' : 'tables';
78
        $script       = $this->script;
79
        $this->misc   = $this->misc;
80
        $data         = $this->misc->getDatabaseAccessor();
81
82
        if (('table' === $type) && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
83
            $this->doDefault($this->lang['strspecifytabletoreindex']);
84
85
            return;
86
        }
87
88
        if (isset($_REQUEST['ma'])) {
89
            $this->printTrail('schema');
90
            $this->printTitle($this->lang['strreindex'], 'pg.reindex');
91
92
            echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
93
94
            foreach ($_REQUEST['ma'] as $v) {
95
                $a = \unserialize(\htmlspecialchars_decode($v, \ENT_QUOTES));
96
                echo '<p>', \sprintf($this->lang['strconfreindextable'], $this->misc->printVal($a['table'])), '</p>' . \PHP_EOL;
97
                echo '<input type="hidden" name="table[]" value="', \htmlspecialchars($a['table']), '" />' . \PHP_EOL;
98
            } //  END if multi reindex
99
        } else {
100
            $this->printTrail($type);
101
            $this->printTitle($this->lang['strreindex'], 'pg.reindex');
102
103
            echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
104
105
            if ('table' === $type) {
106
                echo '<p>', \sprintf($this->lang['strconfreindextable'], $this->misc->printVal($_REQUEST['object'])), '</p>' . \PHP_EOL;
107
                echo '<input type="hidden" name="table" value="', \htmlspecialchars($_REQUEST['object']), '" />' . \PHP_EOL;
108
            } else {
109
                echo '<p>', \sprintf($this->lang['strconfreindexdatabase'], $this->misc->printVal($_REQUEST['object'])), '</p>' . \PHP_EOL;
110
                echo '<input type="hidden" name="table" value="" />' . \PHP_EOL;
111
            }
112
        }
113
        echo '<input type="hidden" name="action" value="reindex" />' . \PHP_EOL;
114
115
        if ($data->hasForceReindex()) {
116
            echo "<p><input type=\"checkbox\" id=\"reindex_force\" name=\"reindex_force\" /><label for=\"reindex_force\">{$this->lang['strforce']}</label></p>" . \PHP_EOL;
117
        }
118
119
        echo $this->misc->form;
120
121
        echo "<input type=\"submit\" name=\"reindex\" value=\"{$this->lang['strreindex']}\" />\n"; //TODO
122
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />" . \PHP_EOL;
123
        echo "</form>\n"; //  END single reindex
124
    }
125
126
    /**
127
     * Show confirmation of analyze.
128
     *
129
     * @param mixed $type
130
     */
131
    public function confirmAnalyze($type): void
132
    {
133
        $this->script = ('database' === $type) ? 'database' : 'tables';
134
135
        $script = $this->script;
136
137
        if (('table' === $type) && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
138
            $this->doDefault($this->lang['strspecifytabletoanalyze']);
139
140
            return;
141
        }
142
143
        if (isset($_REQUEST['ma'])) {
144
            $this->printTrail('schema');
145
            $this->printTitle($this->lang['stranalyze'], 'pg.analyze');
146
147
            echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
148
149
            foreach ($_REQUEST['ma'] as $v) {
150
                $a = \unserialize(\htmlspecialchars_decode($v, \ENT_QUOTES));
151
                //\Kint::dump($a);
152
                echo '<p>', \sprintf($this->lang['strconfanalyzetable'], $this->misc->printVal($a['table'])), '</p>' . \PHP_EOL;
153
                echo '<input type="hidden" name="table[]" value="', \htmlspecialchars($a['table']), '" />' . \PHP_EOL;
154
            } //  END if multi analyze
155
        } else {
156
            $this->printTrail($type);
157
            $this->printTitle($this->lang['stranalyze'], 'pg.analyze');
158
159
            echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
160
161
            if ('table' === $type) {
162
                echo '<p>', \sprintf($this->lang['strconfanalyzetable'], $this->misc->printVal($_REQUEST['object'])), '</p>' . \PHP_EOL;
163
                echo '<input type="hidden" name="table" value="', \htmlspecialchars($_REQUEST['object']), '" />' . \PHP_EOL;
164
            } else {
165
                echo '<p>', \sprintf($this->lang['strconfanalyzedatabase'], $this->misc->printVal($_REQUEST['object'])), '</p>' . \PHP_EOL;
166
                echo '<input type="hidden" name="table" value="" />' . \PHP_EOL;
167
            }
168
        }
169
        echo '<input type="hidden" name="action" value="analyze" />' . \PHP_EOL;
170
        echo $this->misc->form;
171
172
        echo "<input type=\"submit\" name=\"analyze\" value=\"{$this->lang['stranalyze']}\" />\n"; //TODO
173
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />" . \PHP_EOL;
174
        echo "</form>\n"; //  END single analyze
175
    }
176
177
    /**
178
     * Show confirmation of vacuum.
179
     *
180
     * @param mixed $type
181
     */
182
    public function confirmVacuum($type): void
183
    {
184
        $script = ('database' === $type) ? 'database' : 'tables';
185
186
        if (('table' === $type) && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
187
            $this->doDefault($this->lang['strspecifytabletovacuum']);
188
189
            return;
190
        }
191
192
        if (isset($_REQUEST['ma'])) {
193
            $this->printTrail('schema');
194
            $this->printTitle($this->lang['strvacuum'], 'pg.vacuum');
195
196
            echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
197
198
            foreach ($_REQUEST['ma'] as $v) {
199
                $a = \unserialize(\htmlspecialchars_decode($v, \ENT_QUOTES));
200
                echo '<p>', \sprintf($this->lang['strconfvacuumtable'], $this->misc->printVal($a['table'])), '</p>' . \PHP_EOL;
201
                echo '<input type="hidden" name="table[]" value="', \htmlspecialchars($a['table']), '" />' . \PHP_EOL;
202
            }
203
        } else {
204
            // END if multi vacuum
205
            $this->printTrail($type);
206
            $this->printTitle($this->lang['strvacuum'], 'pg.vacuum');
207
208
            echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
209
210
            if ('table' === $type) {
211
                echo '<p>', \sprintf($this->lang['strconfvacuumtable'], $this->misc->printVal($_REQUEST['object'])), '</p>' . \PHP_EOL;
212
                echo '<input type="hidden" name="table" value="', \htmlspecialchars($_REQUEST['object']), '" />' . \PHP_EOL;
213
            } else {
214
                echo '<p>', \sprintf($this->lang['strconfvacuumdatabase'], $this->misc->printVal($_REQUEST['object'])), '</p>' . \PHP_EOL;
215
                echo '<input type="hidden" name="table" value="" />' . \PHP_EOL;
216
            }
217
        }
218
        echo '<input type="hidden" name="action" value="vacuum" />' . \PHP_EOL;
219
        echo $this->misc->form;
220
        echo "<p><input type=\"checkbox\" id=\"vacuum_full\" name=\"vacuum_full\" /> <label for=\"vacuum_full\">{$this->lang['strfull']}</label></p>" . \PHP_EOL;
221
        echo "<p><input type=\"checkbox\" id=\"vacuum_analyze\" name=\"vacuum_analyze\" /> <label for=\"vacuum_analyze\">{$this->lang['stranalyze']}</label></p>" . \PHP_EOL;
222
        echo "<p><input type=\"checkbox\" id=\"vacuum_freeze\" name=\"vacuum_freeze\" /> <label for=\"vacuum_freeze\">{$this->lang['strfreeze']}</label></p>" . \PHP_EOL;
223
        echo "<input type=\"submit\" name=\"vacuum\" value=\"{$this->lang['strvacuum']}\" />" . \PHP_EOL;
224
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />" . \PHP_EOL;
225
        echo "</form>\n"; //  END single vacuum
226
    }
227
228
    /**
229
     * Add or Edit autovacuum params.
230
     *
231
     * @param mixed $type
232
     * @param mixed $msg
233
     */
234
    public function confirmEditAutovacuum($type, $msg = ''): void
235
    {
236
        $data = $this->misc->getDatabaseAccessor();
237
238
        if (empty($_REQUEST['table'])) {
239
            $this->doAdmin($type, $this->lang['strspecifyeditvacuumtable']);
240
241
            return;
242
        }
243
244
        $script = ('database' === $type) ? 'database' : 'tables';
245
246
        $this->printTrail($type);
247
        $this->printTitle(\sprintf($this->lang['streditvacuumtable'], $this->misc->printVal($_REQUEST['table'])));
248
        $this->printMsg(\sprintf($msg, $this->misc->printVal($_REQUEST['table'])));
249
250
        if (empty($_REQUEST['table'])) {
251
            $this->doAdmin($type, $this->lang['strspecifyeditvacuumtable']);
252
253
            return;
254
        }
255
256
        $old_val  = $data->getTableAutovacuum($_REQUEST['table']);
257
        $defaults = $data->getAutovacuum();
258
        $old_val  = $old_val->fields;
259
260
        if (isset($old_val['autovacuum_enabled']) && ('off' === $old_val['autovacuum_enabled'])) {
261
            $enabled  = '';
262
            $disabled = 'checked="checked"';
263
        } else {
264
            $enabled  = 'checked="checked"';
265
            $disabled = '';
266
        }
267
268
        if (!isset($old_val['autovacuum_vacuum_threshold'])) {
269
            $old_val['autovacuum_vacuum_threshold'] = '';
270
        }
271
272
        if (!isset($old_val['autovacuum_vacuum_scale_factor'])) {
273
            $old_val['autovacuum_vacuum_scale_factor'] = '';
274
        }
275
276
        if (!isset($old_val['autovacuum_analyze_threshold'])) {
277
            $old_val['autovacuum_analyze_threshold'] = '';
278
        }
279
280
        if (!isset($old_val['autovacuum_analyze_scale_factor'])) {
281
            $old_val['autovacuum_analyze_scale_factor'] = '';
282
        }
283
284
        if (!isset($old_val['autovacuum_vacuum_cost_delay'])) {
285
            $old_val['autovacuum_vacuum_cost_delay'] = '';
286
        }
287
288
        if (!isset($old_val['autovacuum_vacuum_cost_limit'])) {
289
            $old_val['autovacuum_vacuum_cost_limit'] = '';
290
        }
291
292
        echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
293
        echo $this->misc->form;
294
        echo '<input type="hidden" name="action" value="editautovac" />' . \PHP_EOL;
295
        echo '<input type="hidden" name="table" value="', \htmlspecialchars($_REQUEST['table']), '" />' . \PHP_EOL;
296
297
        echo "<br />\n<br />\n<table>" . \PHP_EOL;
298
        echo "\t<tr><td>&nbsp;</td>" . \PHP_EOL;
299
        echo "<th class=\"data\">{$this->lang['strnewvalues']}</th><th class=\"data\">{$this->lang['strdefaultvalues']}</th></tr>" . \PHP_EOL;
300
        echo "\t<tr><th class=\"data left\">{$this->lang['strenable']}</th>" . \PHP_EOL;
301
        echo '<td class="data1">' . \PHP_EOL;
302
        echo "<label for=\"on\">on</label><input type=\"radio\" name=\"autovacuum_enabled\" id=\"on\" value=\"on\" {$enabled} />" . \PHP_EOL;
303
        echo "<label for=\"off\">off</label><input type=\"radio\" name=\"autovacuum_enabled\" id=\"off\" value=\"off\" {$disabled} /></td>" . \PHP_EOL;
304
        echo "<th class=\"data left\">{$defaults['autovacuum']}</th></tr>" . \PHP_EOL;
305
        echo "\t<tr><th class=\"data left\">{$this->lang['strvacuumbasethreshold']}</th>" . \PHP_EOL;
306
        echo "<td class=\"data1\"><input type=\"text\" name=\"autovacuum_vacuum_threshold\" value=\"{$old_val['autovacuum_vacuum_threshold']}\" /></td>" . \PHP_EOL;
307
        echo "<th class=\"data left\">{$defaults['autovacuum_vacuum_threshold']}</th></tr>" . \PHP_EOL;
308
        echo "\t<tr><th class=\"data left\">{$this->lang['strvacuumscalefactor']}</th>" . \PHP_EOL;
309
        echo "<td class=\"data1\"><input type=\"text\" name=\"autovacuum_vacuum_scale_factor\" value=\"{$old_val['autovacuum_vacuum_scale_factor']}\" /></td>" . \PHP_EOL;
310
        echo "<th class=\"data left\">{$defaults['autovacuum_vacuum_scale_factor']}</th></tr>" . \PHP_EOL;
311
        echo "\t<tr><th class=\"data left\">{$this->lang['stranalybasethreshold']}</th>" . \PHP_EOL;
312
        echo "<td class=\"data1\"><input type=\"text\" name=\"autovacuum_analyze_threshold\" value=\"{$old_val['autovacuum_analyze_threshold']}\" /></td>" . \PHP_EOL;
313
        echo "<th class=\"data left\">{$defaults['autovacuum_analyze_threshold']}</th></tr>" . \PHP_EOL;
314
        echo "\t<tr><th class=\"data left\">{$this->lang['stranalyzescalefactor']}</th>" . \PHP_EOL;
315
        echo "<td class=\"data1\"><input type=\"text\" name=\"autovacuum_analyze_scale_factor\" value=\"{$old_val['autovacuum_analyze_scale_factor']}\" /></td>" . \PHP_EOL;
316
        echo "<th class=\"data left\">{$defaults['autovacuum_analyze_scale_factor']}</th></tr>" . \PHP_EOL;
317
        echo "\t<tr><th class=\"data left\">{$this->lang['strvacuumcostdelay']}</th>" . \PHP_EOL;
318
        echo "<td class=\"data1\"><input type=\"text\" name=\"autovacuum_vacuum_cost_delay\" value=\"{$old_val['autovacuum_vacuum_cost_delay']}\" /></td>" . \PHP_EOL;
319
        echo "<th class=\"data left\">{$defaults['autovacuum_vacuum_cost_delay']}</th></tr>" . \PHP_EOL;
320
        echo "\t<tr><th class=\"data left\">{$this->lang['strvacuumcostlimit']}</th>" . \PHP_EOL;
321
        echo "<td class=\"datat1\"><input type=\"text\" name=\"autovacuum_vacuum_cost_limit\" value=\"{$old_val['autovacuum_vacuum_cost_limit']}\" /></td>" . \PHP_EOL;
322
        echo "<th class=\"data left\">{$defaults['autovacuum_vacuum_cost_limit']}</th></tr>" . \PHP_EOL;
323
        echo '</table>' . \PHP_EOL;
324
        echo '<br />';
325
        echo '<br />';
326
        echo "<input type=\"submit\" name=\"save\" value=\"{$this->lang['strsave']}\" />" . \PHP_EOL;
327
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>" . \PHP_EOL;
328
329
        echo '</form>' . \PHP_EOL;
330
    }
331
332
    /**
333
     * Confirm drop autovacuum params for a table.
334
     *
335
     * @param mixed $type
336
     */
337
    public function confirmDropAutovacuum($type): void
338
    {
339
        $script = ('database' === $type) ? 'database' : 'tables';
340
341
        if (empty($_REQUEST['table'])) {
342
            $this->doAdmin($type, $this->lang['strspecifydelvacuumtable']);
343
344
            return;
345
        }
346
347
        $this->printTrail($type);
348
        $this->printTabs($type, 'admin');
349
350
        \printf(
351
            "<p>{$this->lang['strdelvacuumtable']}</p>\n",
352
            $this->misc->printVal("\"{$_GET['schema']}\".\"{$_GET['table']}\"")
353
        );
354
355
        echo "<form style=\"float: left\" action=\"{$script}\" method=\"post\">" . \PHP_EOL;
356
        echo '<input type="hidden" name="action" value="delautovac" />' . \PHP_EOL;
357
        echo $this->misc->form;
358
        echo '<input type="hidden" name="table" value="', \htmlspecialchars($_REQUEST['table']), '" />' . \PHP_EOL;
359
        echo '<input type="hidden" name="rel" value="', \htmlspecialchars(\serialize([$_REQUEST['schema'], $_REQUEST['table']])), '" />' . \PHP_EOL;
360
        echo "<input type=\"submit\" name=\"yes\" value=\"{$this->lang['stryes']}\" />" . \PHP_EOL;
361
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />" . \PHP_EOL;
362
        echo '</form>' . \PHP_EOL;
363
    }
364
365
    /**
366
     * perform cluster.
367
     *
368
     * @param mixed $type
369
     */
370
    public function doCluster($type): void
371
    {
372
        $data = $this->misc->getDatabaseAccessor();
373
374
        if (('table' === $type) && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
375
            $this->doDefault($this->lang['strspecifytabletocluster']);
376
377
            return;
378
        }
379
380
        $msg = '';
381
        //If multi table cluster
382
        if ('table' === $type) {
383
            // cluster one or more table
384
            if (\is_array($_REQUEST['table'])) {
385
                foreach ($_REQUEST['table'] as $o) {
386
                    list($status, $sql) = $data->clusterIndex($o);
387
                    $msg .= \sprintf('%s<br />', $sql);
388
389
                    if (0 === $status) {
390
                        $msg .= \sprintf('%s: %s<br />', \htmlentities($o, \ENT_QUOTES, 'UTF-8'), $this->lang['strclusteredgood']);
391
                    } else {
392
                        $this->doDefault(\sprintf('%s %s%s: %s<br />', $type, $msg, \htmlentities($o, \ENT_QUOTES, 'UTF-8'), $this->lang['strclusteredbad']));
393
394
                        return;
395
                    }
396
                }
397
                // Everything went fine, back to the Default page....
398
                $this->doDefault($msg);
399
            } else {
400
                list($status, $sql) = $data->clusterIndex($_REQUEST['object']);
401
                $msg .= \sprintf('%s<br />', $sql);
402
403
                if (0 === $status) {
404
                    $this->doAdmin($type, $msg . $this->lang['strclusteredgood']);
405
                } else {
406
                    $this->doAdmin($type, $msg . $this->lang['strclusteredbad']);
407
                }
408
            }
409
        } else {
410
            // Cluster all tables in database
411
            list($status, $sql) = $data->clusterIndex();
412
            $msg .= \sprintf('%s<br />', $sql);
413
414
            if (0 === $status) {
415
                $this->doAdmin($type, $msg . $this->lang['strclusteredgood']);
416
            } else {
417
                $this->doAdmin($type, $msg . $this->lang['strclusteredbad']);
418
            }
419
        }
420
    }
421
422
    /**
423
     * perform reindex.
424
     *
425
     * @param mixed $type
426
     */
427
    public function doReindex($type): void
428
    {
429
        $this->misc = $this->misc;
430
        $data       = $this->misc->getDatabaseAccessor();
431
432
        if (('table' === $type) && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
433
            $this->doDefault($this->lang['strspecifytabletoreindex']);
434
435
            return;
436
        }
437
438
        //If multi table reindex
439
        if (('table' === $type) && \is_array($_REQUEST['table'])) {
440
            $msg = '';
441
442
            foreach ($_REQUEST['table'] as $o) {
443
                $status = $data->reindex(\mb_strtoupper($type), $o, isset($_REQUEST['reindex_force']));
444
445
                if (0 === $status) {
446
                    $msg .= \sprintf('%s: %s<br />', \htmlentities($o, \ENT_QUOTES, 'UTF-8'), $this->lang['strreindexgood']);
447
                } else {
448
                    $this->doDefault(\sprintf('%s %s%s: %s<br />', $type, $msg, \htmlentities($o, \ENT_QUOTES, 'UTF-8'), $this->lang['strreindexbad']));
449
450
                    return;
451
                }
452
            }
453
            // Everything went fine, back to the Default page....
454
            $this->misc->setReloadBrowser(true);
455
            $this->doDefault($msg);
456
        } else {
457
            $status = $data->reindex(\mb_strtoupper($type), $_REQUEST['object'], isset($_REQUEST['reindex_force']));
458
459
            if (0 === $status) {
460
                $this->misc->setReloadBrowser(true);
461
                $this->doAdmin($type, $this->lang['strreindexgood']);
462
            } else {
463
                $this->doAdmin($type, $this->lang['strreindexbad']);
464
            }
465
        }
466
    }
467
468
    /**
469
     * perform analyze.
470
     *
471
     * @param mixed $type
472
     */
473
    public function doAnalyze($type): void
474
    {
475
        $data = $this->misc->getDatabaseAccessor();
476
477
        if (('table' === $type) && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
478
            $this->doDefault($this->lang['strspecifytabletoanalyze']);
479
480
            return;
481
        }
482
483
        //If multi table analyze
484
        if (('table' === $type) && \is_array($_REQUEST['table'])) {
485
            $msg = '';
486
487
            foreach ($_REQUEST['table'] as $o) {
488
                $status = $data->analyzeDB($o);
489
490
                if (0 === $status) {
491
                    $msg .= \sprintf('%s: %s<br />', \htmlentities($o, \ENT_QUOTES, 'UTF-8'), $this->lang['stranalyzegood']);
492
                } else {
493
                    $this->doDefault(\sprintf('%s %s%s: %s<br />', $type, $msg, \htmlentities($o, \ENT_QUOTES, 'UTF-8'), $this->lang['stranalyzebad']));
494
495
                    return;
496
                }
497
            }
498
            // Everything went fine, back to the Default page....
499
            $this->misc->setReloadBrowser(true);
500
            $this->doDefault($msg);
501
        } else {
502
            //we must pass table here. When empty, analyze the whole db
503
            $status = $data->analyzeDB($_REQUEST['table']);
504
505
            if (0 === $status) {
506
                $this->misc->setReloadBrowser(true);
507
                $this->doAdmin($type, $this->lang['stranalyzegood']);
508
            } else {
509
                $this->doAdmin($type, $this->lang['stranalyzebad']);
510
            }
511
        }
512
    }
513
514
    /**
515
     * perform actual vacuum.
516
     *
517
     * @param mixed $type
518
     */
519
    public function doVacuum($type)
520
    {
521
        $data = $this->misc->getDatabaseAccessor();
522
523
        if (('table' === $type) && empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
524
            $this->doDefault($this->lang['strspecifytabletovacuum']);
525
526
            return;
527
        }
528
529
        //If multi drop
530
        if (\is_array($_REQUEST['table'])) {
531
            $msg = '';
532
533
            foreach ($_REQUEST['table'] as $t) {
534
                list($status, $sql) = $data->vacuumDB($t, isset($_REQUEST['vacuum_analyze']), isset($_REQUEST['vacuum_full']), isset($_REQUEST['vacuum_freeze']));
535
536
                if (0 !== $status) {
537
                    return $this->doDefault(\sprintf('%s %s%s: %s<br />', $type, $msg, \htmlentities($t, \ENT_QUOTES, 'UTF-8'), $this->lang['strvacuumbad']));
538
                }
539
                $msg .= \sprintf('%s%s %s: %s<br />', $sql, \PHP_EOL, \htmlentities($t, \ENT_QUOTES, 'UTF-8'), $this->lang['strvacuumgood']);
540
            }
541
            // Everything went fine, back to the Default page....
542
            $this->misc->setReloadBrowser(true);
543
544
            return $this->doDefault($msg);
545
        }
546
        //we must pass table here. When empty, vacuum the whole db
547
        list($status, $sql) = $data->vacuumDB($_REQUEST['table'], isset($_REQUEST['vacuum_analyze']), isset($_REQUEST['vacuum_full']), isset($_REQUEST['vacuum_freeze']));
548
549
        if (0 === $status) {
550
            $this->misc->setReloadBrowser(true);
551
552
            return $this->doAdmin($type, \sprintf('%s%s%s', $sql, \PHP_EOL, $this->lang['strvacuumgood']));
553
        }
554
555
        return $this->doAdmin($type, $this->lang['strvacuumbad']);
556
    }
557
558
    /**
559
     * persist changes in autovacuum settings.
560
     *
561
     * @param mixed $type
562
     * @param mixed $confirm
563
     * @param mixed $msg
564
     */
565
    public function doEditAutovacuum($type): void
566
    {
567
        $data = $this->misc->getDatabaseAccessor();
568
569
        if (empty($_REQUEST['table'])) {
570
            $this->doAdmin($type, $this->lang['strspecifyeditvacuumtable']);
571
572
            return;
573
        }
574
575
        $status = $data->saveAutovacuum(
576
            $_REQUEST['table'],
577
            $_POST['autovacuum_enabled'],
578
            $_POST['autovacuum_vacuum_threshold'],
579
            $_POST['autovacuum_vacuum_scale_factor'],
580
            $_POST['autovacuum_analyze_threshold'],
581
            $_POST['autovacuum_analyze_scale_factor'],
582
            $_POST['autovacuum_vacuum_cost_delay'],
583
            $_POST['autovacuum_vacuum_cost_limit']
584
        );
585
586
        if (0 === $status) {
587
            $this->doAdmin($type, \sprintf($this->lang['strsetvacuumtablesaved'], $_REQUEST['table']));
588
        } else {
589
            $this->confirmEditAutovacuum($type, $this->lang['strsetvacuumtablefail']);
590
        }
591
    }
592
593
    /**
594
     * drop autovacuum settings.
595
     *
596
     * @param mixed $type
597
     */
598
    public function doDropAutovacuum($type): void
599
    {
600
        $data = $this->misc->getDatabaseAccessor();
601
602
        if (empty($_REQUEST['table'])) {
603
            $this->doAdmin($type, $this->lang['strspecifydelvacuumtable']);
604
605
            return;
606
        }
607
608
        $status = $data->dropAutovacuum($_POST['table']);
609
610
        if (0 === $status) {
611
            $this->doAdmin($type, \sprintf($this->lang['strvacuumtablereset'], $this->misc->printVal($_POST['table'])));
612
        } else {
613
            $this->doAdmin($type, \sprintf($this->lang['strdelvacuumtablefail'], $this->misc->printVal($_POST['table'])));
614
        }
615
    }
616
617
    /**
618
     * Database/table administration and tuning tasks.
619
     *
620
     * Release: admin
621
     *
622
     * @param mixed $type
623
     * @param mixed $msg
624
     */
625
    public function doAdmin($type, $msg = ''): void
626
    {
627
        $this->script = ('database' === $type) ? 'database' : 'tables';
628
629
        $script = $this->script;
630
631
        $data = $this->misc->getDatabaseAccessor();
632
633
        $this->printTrail($type);
634
        $this->printTabs($type, 'admin');
635
        $this->printMsg($msg);
636
637
        if ('database' === $type) {
638
            \printf("<p>{$this->lang['stradminondatabase']}</p>\n", $this->misc->printVal($_REQUEST['object']));
639
        } else {
640
            \printf("<p>{$this->lang['stradminontable']}</p>\n", $this->misc->printVal($_REQUEST['object']));
641
        }
642
643
        echo '<table style="width: 50%">' . \PHP_EOL;
644
        echo '<tr>' . \PHP_EOL;
645
        echo '<th class="data">';
646
        $this->misc->printHelp($this->lang['strvacuum'], 'pg.admin.vacuum') . '</th>' . \PHP_EOL;
647
        echo '</th>';
648
        echo '<th class="data">';
649
        $this->misc->printHelp($this->lang['stranalyze'], 'pg.admin.analyze');
650
        echo '</th>';
651
652
        $table_hidden_inputs = ('table' === $type) ?
653
        \sprintf('<input type="hidden" name="table" value="%s" />%s<input type="hidden" name="subject" value="table" />', \htmlspecialchars($_REQUEST['object']), \PHP_EOL, \PHP_EOL) : '';
654
655
        list($recluster_help, $reclusterconf) = $this->_getReclusterConf($data, $type, $table_hidden_inputs);
656
657
        echo $recluster_help;
658
659
        echo '<th class="data">';
660
        $this->misc->printHelp($this->lang['strreindex'], 'pg.index.reindex');
661
        echo '</th>';
662
        echo '</tr>';
663
664
        // Vacuum
665
        echo '<tr class="row1">' . \PHP_EOL;
666
        echo '<td style="text-align: center; vertical-align: bottom">' . \PHP_EOL;
667
        echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
668
669
        echo '<p><input type="hidden" name="action" value="confirm_vacuum" />' . \PHP_EOL;
670
        echo $this->misc->form;
671
        echo $table_hidden_inputs;
672
        echo "<input type=\"submit\" value=\"{$this->lang['strvacuum']}\" /></p>" . \PHP_EOL;
673
        echo '</form>' . \PHP_EOL;
674
        echo '</td>' . \PHP_EOL;
675
676
        // Analyze
677
        echo '<td style="text-align: center; vertical-align: bottom">' . \PHP_EOL;
678
        echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
679
        echo '<p><input type="hidden" name="action" value="confirm_analyze" />' . \PHP_EOL;
680
        echo $this->misc->form;
681
        echo $table_hidden_inputs;
682
        echo "<input type=\"submit\" value=\"{$this->lang['stranalyze']}\" /></p>" . \PHP_EOL;
683
        echo '</form>' . \PHP_EOL;
684
        echo '</td>' . \PHP_EOL;
685
686
        // Cluster
687
        echo $reclusterconf;
688
689
        // Reindex
690
        echo '<td style="text-align: center; vertical-align: bottom">' . \PHP_EOL;
691
        echo '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
692
        echo '<p><input type="hidden" name="action" value="confirm_reindex" />' . \PHP_EOL;
693
        echo $this->misc->form;
694
        echo $table_hidden_inputs;
695
        echo "<input type=\"submit\" value=\"{$this->lang['strreindex']}\" /></p>" . \PHP_EOL;
696
        echo '</form>' . \PHP_EOL;
697
        echo '</td>' . \PHP_EOL;
698
        echo '</tr>' . \PHP_EOL;
699
        echo '</table>' . \PHP_EOL;
700
701
        // Autovacuum
702
        $this->_printAutoVacuumConf($data, $type);
703
    }
704
705
    public function adminActions($action, $type)
706
    {
707
        if ('database' === $type) {
708
            $_REQUEST['object'] = $_REQUEST['database'];
709
        } else {
710
            // $_REQUEST['table'] is no set if we are in the schema page
711
            $_REQUEST['object'] = ($_REQUEST['table'] ?? '');
712
713
            if (\is_array($_REQUEST['object'])) {
714
                return false;
715
            }
716
        }
717
718
        if (isset($_POST['cancel'])) {
719
            $action = 'admin';
720
        }
721
722
        switch ($action) {
723
            case 'admin':
724
                $this->doAdmin($type);
725
726
                break;
727
            case 'confirm_cluster':
728
                $this->confirmCluster($type);
729
730
                break;
731
            case 'confirm_reindex':
732
                $this->confirmReindex($type);
733
734
                break;
735
            case 'confirm_analyze':
736
                $this->confirmAnalyze($type);
737
738
                break;
739
            case 'confirm_vacuum':
740
                $this->confirmVacuum($type);
741
742
                break;
743
            case 'confeditautovac':
744
                $this->confirmEditAutovacuum($type);
745
746
                break;
747
            case 'confdelautovac':
748
                $this->confirmDropAutovacuum($type);
749
750
                break;
751
            case 'cluster':
752
                $this->doCluster($type);
753
754
                break;
755
            case 'reindex':
756
                $this->doReindex($type);
757
758
                break;
759
            case 'analyze':
760
                $this->doAnalyze($type);
761
762
                break;
763
            case 'vacuum':
764
                $this->doVacuum($type);
765
766
                break;
767
            case 'editautovac':
768
                $this->doEditAutovacuum($type);
769
770
                break;
771
            case 'delautovac':
772
                $this->doDropAutovacuum($type);
773
774
                break;
775
            default:
776
                return false;
777
        }
778
779
        return true;
780
    }
781
782
    abstract public function doDefault($msg = '');
783
784
    abstract public function printTrail($trail = [], $do_print = true);
785
786
    abstract public function printTitle($title, $help = null, $do_print = true);
787
788
    abstract public function printMsg($msg, $do_print = true);
789
790
    abstract public function printTabs($tabs, $activetab, $do_print = true);
791
792
    abstract public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = '', $pre_fn = null);
793
794
    private function _getReclusterConf($data, $type, $table_hidden_inputs)
795
    {
796
        if (!$data->hasRecluster()) {
797
            return ['', ''];
798
        }
799
        $script         = $this->script;
800
        $recluster_help = \sprintf('<th class="data">%s</th>', $this->misc->printHelp($this->lang['strclusterindex'], 'pg.index.cluster', false));
801
802
        $disabled      = '';
803
        $reclusterconf = '<td style="text-align: center; vertical-align: bottom">' . \PHP_EOL;
804
        $reclusterconf .= '<form action="' . \SUBFOLDER . "/src/views/{$script}\" method=\"post\">" . \PHP_EOL;
805
        $reclusterconf .= $this->misc->form;
806
        $reclusterconf .= $table_hidden_inputs;
807
808
        if ('table' === $type && !$data->alreadyClustered($_REQUEST['object'])) {
809
            $disabled = 'disabled="disabled" ';
810
            $reclusterconf .= "{$this->lang['strnoclusteravailable']}<br />";
811
        }
812
        $reclusterconf .= '<p><input type="hidden" name="action" value="confirm_cluster" />' . \PHP_EOL;
813
        $reclusterconf .= "<input type=\"submit\" value=\"{$this->lang['strclusterindex']}\" {$disabled}/></p>" . \PHP_EOL;
814
        $reclusterconf .= '</form>' . \PHP_EOL;
815
        $reclusterconf .= '</td>' . \PHP_EOL;
816
817
        return [$recluster_help, $reclusterconf];
818
    }
819
820
    private function _printAutoVacuumConf($data, $type): void
821
    {
822
        if (!$data->hasAutovacuum()) {
823
            return;
824
        }
825
        $script = $this->script;
826
        // get defaults values for autovacuum
827
        $defaults = $data->getAutovacuum();
828
        // Fetch the autovacuum properties from the database or table if != ''
829
        if ('table' === $type) {
830
            $autovac = $data->getTableAutovacuum($_REQUEST['table']);
831
        } else {
832
            $autovac = $data->getTableAutovacuum();
833
        }
834
835
        echo "<br /><br /><h2>{$this->lang['strvacuumpertable']}</h2>";
836
        echo '<p>' . (('on' === $defaults['autovacuum']) ? $this->lang['strturnedon'] : $this->lang['strturnedoff']) . '</p>';
837
        echo "<p class=\"message\">{$this->lang['strnotdefaultinred']}</p>";
838
839
        $enlight = static function ($f, $p) {
840
            if (isset($f[$p[0]]) && ($f[$p[0]] !== $p[1])) {
841
                return '<span style="color:#F33;font-weight:bold">' . \htmlspecialchars($f[$p[0]]) . '</span>';
842
            }
843
844
            return \htmlspecialchars($p[1]);
845
        };
846
847
        $columns = [
848
            'namespace'                       => [
849
                'title' => $this->lang['strschema'],
850
                'field' => Decorator::field('nspname'),
851
                'url'   => \SUBFOLDER . "/redirect/schema?{$this->misc->href}&amp;",
852
                'vars'  => ['schema' => 'nspname'],
853
            ],
854
            'relname'                         => [
855
                'title' => $this->lang['strtable'],
856
                'field' => Decorator::field('relname'),
857
                'url'   => \SUBFOLDER . "/redirect/table?{$this->misc->href}&amp;",
858
                'vars'  => ['table' => 'relname', 'schema' => 'nspname'],
859
            ],
860
            'autovacuum_enabled'              => [
861
                'title' => $this->lang['strenabled'],
862
                'field' => Decorator::callback($enlight, ['autovacuum_enabled', $defaults['autovacuum']]),
863
                'type'  => 'verbatim',
864
            ],
865
            'autovacuum_vacuum_threshold'     => [
866
                'title' => $this->lang['strvacuumbasethreshold'],
867
                'field' => Decorator::callback($enlight, ['autovacuum_vacuum_threshold', $defaults['autovacuum_vacuum_threshold']]),
868
                'type'  => 'verbatim',
869
            ],
870
            'autovacuum_vacuum_scale_factor'  => [
871
                'title' => $this->lang['strvacuumscalefactor'],
872
                'field' => Decorator::callback($enlight, ['autovacuum_vacuum_scale_factor', $defaults['autovacuum_vacuum_scale_factor']]),
873
                'type'  => 'verbatim',
874
            ],
875
            'autovacuum_analyze_threshold'    => [
876
                'title' => $this->lang['stranalybasethreshold'],
877
                'field' => Decorator::callback($enlight, ['autovacuum_analyze_threshold', $defaults['autovacuum_analyze_threshold']]),
878
                'type'  => 'verbatim',
879
            ],
880
            'autovacuum_analyze_scale_factor' => [
881
                'title' => $this->lang['stranalyzescalefactor'],
882
                'field' => Decorator::callback($enlight, ['autovacuum_analyze_scale_factor', $defaults['autovacuum_analyze_scale_factor']]),
883
                'type'  => 'verbatim',
884
            ],
885
            'autovacuum_vacuum_cost_delay'    => [
886
                'title' => $this->lang['strvacuumcostdelay'],
887
                'field' => Decorator::concat(Decorator::callback($enlight, ['autovacuum_vacuum_cost_delay', $defaults['autovacuum_vacuum_cost_delay']]), 'ms'),
888
                'type'  => 'verbatim',
889
            ],
890
            'autovacuum_vacuum_cost_limit'    => [
891
                'title' => $this->lang['strvacuumcostlimit'],
892
                'field' => Decorator::callback($enlight, ['autovacuum_vacuum_cost_limit', $defaults['autovacuum_vacuum_cost_limit']]),
893
                'type'  => 'verbatim',
894
            ],
895
        ];
896
897
        // Maybe we need to check permissions here?
898
        $columns['actions'] = ['title' => $this->lang['stractions']];
899
900
        $actions = [
901
            'edit'   => [
902
                'content' => $this->lang['stredit'],
903
                'attr'    => [
904
                    'href' => [
905
                        'url'     => $script,
906
                        'urlvars' => [
907
                            'subject' => $type,
908
                            'action'  => 'confeditautovac',
909
                            'schema'  => Decorator::field('nspname'),
910
                            'table'   => Decorator::field('relname'),
911
                        ],
912
                    ],
913
                ],
914
            ],
915
            'delete' => [
916
                'content' => $this->lang['strdelete'],
917
                'attr'    => [
918
                    'href' => [
919
                        'url'     => $script,
920
                        'urlvars' => [
921
                            'subject' => $type,
922
                            'action'  => 'confdelautovac',
923
                            'schema'  => Decorator::field('nspname'),
924
                            'table'   => Decorator::field('relname'),
925
                        ],
926
                    ],
927
                ],
928
            ],
929
        ];
930
931
        if ('table' === $type) {
932
            unset($actions['edit']['vars']['schema'],
933
                $actions['delete']['vars']['schema'],
934
                $columns['namespace'],
935
                $columns['relname']
936
            );
937
        }
938
939
        echo $this->printTable($autovac, $columns, $actions, 'admin-admin', $this->lang['strnovacuumconf']);
940
941
        if (('table' === $type) && (0 === $autovac->recordCount())) {
942
            echo '<br />';
943
944
            echo '<a href="' . \SUBFOLDER . "/src/views/tables?action=confeditautovac&amp;{$this->misc->href}&amp;table=";
945
            echo \htmlspecialchars($_REQUEST['table']);
946
            echo "\">{$this->lang['straddvacuumtable']}</a>";
947
        }
948
    }
949
}
950