Passed
Push — develop ( 123d4d...dedbfe )
by Felipe
06:48
created

AdminTrait::confirmReindex()   C

Complexity

Conditions 9
Paths 14

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

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