Test Failed
Push — master ( 34dd7c...325a03 )
by Felipe
11:17 queued 06:14
created

AdminTrait::doDropAutovacuum()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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