Passed
Push — master ( b546ed...d78264 )
by Felipe
09:34 queued 05:48
created

AdminTrait::confirmEditAutovacuum()   C

Complexity

Conditions 12
Paths 259

Size

Total Lines 96
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 68
dl 0
loc 96
c 0
b 0
f 0
rs 5.0148
cc 12
nc 259
nop 2

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