Passed
Pull Request — master (#267)
by Felipe
07:20
created

AdminTrait::doVacuum()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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