Passed
Push — develop ( c2019a...2cab77 )
by Felipe
04:50
created

DatabaseController   F

Complexity

Total Complexity 82

Size/Duplication

Total Lines 761
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 82
dl 0
loc 761
rs 1.263
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
B doTree() 0 29 1
A _highlight() 0 3 1
D render() 0 90 15
A doSignal() 0 9 2
A _printTypeOption() 0 8 2
A doDefault() 0 3 1
A doVariables() 0 23 1
A printFindForm() 0 54 4
C doFind() 0 38 11
B _translatedType() 0 33 2
A doProcesses() 0 13 2
D _printHtmlForType() 0 149 21
A doExport() 0 20 2
B currentLocks() 0 45 2
C currentProcesses() 0 117 7
C doSQL() 0 33 7
A doLocks() 0 10 1

How to fix   Complexity   

Complex Class

Complex classes like DatabaseController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DatabaseController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.48
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class DatabaseController extends BaseController
17
{
18
    use \PHPPgAdmin\Traits\AdminTrait;
19
    use \PHPPgAdmin\Traits\ExportTrait;
20
    public $table_place = 'database-variables';
21
    public $fields;
22
    public $controller_title = 'strdatabase';
23
24
    private function _highlight($string, $term)
25
    {
26
        return str_replace($term, "<b>{$term}</b>", $string);
27
    }
28
29
    /**
30
     * Default method to render the controller according to the action parameter.
31
     */
32
    public function render()
33
    {
34
        if ('tree' == $this->action) {
35
            return $this->doTree();
36
        }
37
38
        if ('refresh_locks' == $this->action) {
39
            return $this->currentLocks(true);
40
        }
41
42
        if ('refresh_processes' == $this->action) {
43
            return $this->currentProcesses(true);
44
        }
45
        $scripts = '';
46
        // normal flow
47
        if ('locks' == $this->action || 'processes' == $this->action) {
48
            $scripts .= '<script src="'.\SUBFOLDER.'/assets/js/database.js" type="text/javascript"></script>';
49
50
            $refreshTime = $this->conf['ajax_refresh'] * 1500;
51
52
            $scripts .= '<script type="text/javascript">'.PHP_EOL;
53
            $scripts .= "var Database = {\n";
54
            $scripts .= "ajax_time_refresh: {$refreshTime},\n";
55
            $scripts .= "str_start: {text:'{$this->lang['strstart']}',icon: '".$this->misc->icon('Execute')."'},\n";
56
            $scripts .= "str_stop: {text:'{$this->lang['strstop']}',icon: '".$this->misc->icon('Stop')."'},\n";
57
            $scripts .= "load_icon: '".$this->misc->icon('Loading')."',\n";
58
            $scripts .= "server:'{$_REQUEST['server']}',\n";
59
            $scripts .= "dbname:'{$_REQUEST['database']}',\n";
60
            $scripts .= "action:'refresh_{$this->action}',\n";
61
            $scripts .= "errmsg: '".str_replace("'", "\\'", $this->lang['strconnectionfail'])."'\n";
62
            $scripts .= "};\n";
63
            $scripts .= '</script>'.PHP_EOL;
64
        }
65
66
        $header_template = 'header.twig';
67
        $footer_template = 'footer.twig';
68
        // @todo convert all these methods to return text instead of print text
69
        ob_start();
70
        switch ($this->action) {
71
            case 'find':
72
                if (isset($_REQUEST['term'])) {
73
                    $this->printFindForm(false);
74
                } else {
75
                    $this->printFindForm(true);
76
                }
77
78
                break;
79
            case 'sql':
80
                $this->doSQL();
81
                $header_template = 'header_sqledit.twig';
82
                $footer_template = 'footer_sqledit.twig';
83
84
                break;
85
            case 'variables':
86
                $this->doVariables();
87
88
                break;
89
            case 'processes':
90
                $this->doProcesses();
91
92
                break;
93
            case 'locks':
94
                $this->doLocks();
95
96
                break;
97
            case 'export':
98
                $this->doExport();
99
100
                break;
101
            case 'signal':
102
                $this->doSignal();
103
104
                break;
105
            default:
106
                if (false === $this->adminActions($this->action, 'database')) {
107
                    $header_template = 'header_sqledit.twig';
108
                    $footer_template = 'footer_sqledit.twig';
109
                    $this->doSQL();
110
                }
111
112
                break;
113
        }
114
        $output = ob_get_clean();
115
116
        $this->printHeader($this->headerTitle(), $scripts, true, $header_template);
117
        $this->printBody();
118
119
        echo $output;
120
121
        $this->printFooter(true, $footer_template);
122
    }
123
124
    public function doTree($print = true)
125
    {
126
        $reqvars = $this->misc->getRequestVars('database');
127
        $tabs    = $this->misc->getNavTabs('database');
128
        $items   = $this->adjustTabsForTree($tabs);
129
130
        $attrs = [
131
            'text'   => Decorator::field('title'),
132
            'icon'   => Decorator::field('icon'),
133
            'action' => Decorator::actionurl(
134
                Decorator::field('url'),
135
                $reqvars,
136
                Decorator::field('urlvars'),
137
                [
138
                    'database' => $this->misc->getDatabase(),
139
                ]
140
            ),
141
            'branch' => Decorator::branchurl(
142
                Decorator::field('url'),
143
                $reqvars,
144
                Decorator::field('urlvars'),
145
                [
146
                    'action'   => 'tree',
147
                    'database' => $this->misc->getDatabase(),
148
                ]
149
            ),
150
        ];
151
152
        return $this->printTree($items, $attrs, 'database', $print);
153
    }
154
155
    /**
156
     * Sends a signal to a process.
157
     */
158
    public function doSignal()
159
    {
160
        $data = $this->misc->getDatabaseAccessor();
161
162
        $status = $data->sendSignal($_REQUEST['pid'], $_REQUEST['signal']);
163
        if (0 == $status) {
164
            $this->doProcesses($this->lang['strsignalsent']);
165
        } else {
166
            $this->doProcesses($this->lang['strsignalsentbad']);
167
        }
168
    }
169
170
    /**
171
     * Searches for a named database object.
172
     *
173
     * @param mixed $confirm
174
     * @param mixed $msg
175
     */
176
    public function printFindForm($confirm = true, $msg = '')
177
    {
178
        $data = $this->misc->getDatabaseAccessor();
179
180
        $this->coalesceArr($_REQUEST, 'term', '');
181
182
        $this->coalesceArr($_REQUEST, 'filter', '');
183
184
        $this->printTrail('database');
185
        $this->printTabs('database', 'find');
186
        $this->printMsg($msg);
187
188
        echo '<form action="'.\SUBFOLDER.'/src/views/database" method="post">'.PHP_EOL;
189
        echo '<p><input name="term" value="', htmlspecialchars($_REQUEST['term']),
190
            "\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" />".PHP_EOL;
191
        // Output list of filters.  This is complex due to all the 'has' and 'conf' feature possibilities
192
        echo '<select name="filter">'.PHP_EOL;
193
194
        echo $this->_printTypeOption('');
195
        echo $this->_printTypeOption('COLUMN');
196
        echo $this->_printTypeOption('CONSTRAINT');
197
        echo $this->_printTypeOption('DOMAIN');
198
        echo $this->_printTypeOption('FUNCTION');
199
        echo $this->_printTypeOption('INDEX');
200
        echo $this->_printTypeOption('RULE');
201
        echo $this->_printTypeOption('SCHEMA');
202
        echo $this->_printTypeOption('SEQUENCE');
203
        echo $this->_printTypeOption('TABLE');
204
        echo $this->_printTypeOption('TRIGGER');
205
        echo $this->_printTypeOption('VIEW');
206
207
        if ($this->conf['show_advanced']) {
208
            echo $this->_printTypeOption('AGGREGATE');
209
            echo $this->_printTypeOption('TYPE');
210
            echo $this->_printTypeOption('OPERATOR');
211
            echo $this->_printTypeOption('OPCLASS');
212
            echo $this->_printTypeOption('CONVERSION');
213
            echo $this->_printTypeOption('LANGUAGE');
214
        }
215
216
        echo '</select>'.PHP_EOL;
217
        echo "<input type=\"submit\" value=\"{$this->lang['strfind']}\" />".PHP_EOL;
218
        echo $this->misc->form;
219
        echo '<input type="hidden" name="action" value="find" /></p>'.PHP_EOL;
220
        echo '<input type="hidden" name="confirm" value="true" /></p>'.PHP_EOL;
221
        echo '</form>'.PHP_EOL;
222
223
        // Default focus
224
        $this->setFocus('forms[0].term');
225
226
        // If a search term has been specified, then perform the search
227
        // and display the results, grouped by object type
228
        if (!$confirm && '' != $_REQUEST['term']) {
229
            return $this->doFind();
230
        }
231
    }
232
233
    private function _printTypeOption($curr)
234
    {
235
        $filter     = $_REQUEST['filter'];
236
        $optionhtml = sprintf('%s<option value="%s" %s>', "\t", $curr, ($curr === $filter) ? ' selected="selected"' : '');
237
        $optionhtml .= $this->_translatedType($curr);
238
        $optionhtml .= '</option>'.PHP_EOL;
239
240
        return $optionhtml;
241
    }
242
243
    private function _translatedType($curr)
244
    {
245
        $types = [
246
            'COLUMN'           => $this->lang['strcolumns'],
247
            'CONSTRAINT'       => $this->lang['strconstraints'],
248
            'COLUMNTABLE'      => $this->lang['strcolumns'],
249
            'COLUMNVIEW'       => $this->lang['strcolumns'],
250
            'CONSTRAINTDOMAIN' => $this->lang['strconstraints'],
251
            'CONSTRAINTTABLE'  => $this->lang['strconstraints'],
252
            'DOMAIN'           => $this->lang['strdomains'],
253
            'FUNCTION'         => $this->lang['strfunctions'],
254
            'INDEX'            => $this->lang['strindexes'],
255
            'RULE'             => $this->lang['strrules'],
256
            'RULETABLE'        => $this->lang['strrules'],
257
            'RULEVIEW'         => $this->lang['strrules'],
258
            'SCHEMA'           => $this->lang['strschemas'],
259
            'SEQUENCE'         => $this->lang['strsequences'],
260
            'TABLE'            => $this->lang['strtables'],
261
            'TRIGGER'          => $this->lang['strtriggers'],
262
            'VIEW'             => $this->lang['strviews'],
263
264
            'AGGREGATE'        => $this->lang['straggregates'],
265
            'CONVERSION'       => $this->lang['strconversions'],
266
            'LANGUAGE'         => $this->lang['strlanguages'],
267
            'OPCLASS'          => $this->lang['stropclasses'],
268
            'OPERATOR'         => $this->lang['stroperators'],
269
            'TYPE'             => $this->lang['strtypes'],
270
        ];
271
        if (array_key_exists($curr, $types)) {
272
            return $types[$curr];
273
        }
274
275
        return $this->lang['strallobjects'];
276
    }
277
278
    private function _printHtmlForType($curr, $rs)
279
    {
280
        switch ($curr) {
281
            case 'SCHEMA':
282
                echo '<li><a href="'.\SUBFOLDER."/redirect/schema?{$this->misc->href}&schema=";
283
                echo $this->misc->printVal($rs->fields['name']), '">';
284
                echo $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']);
285
                echo '</a></li>'.PHP_EOL;
286
287
                break;
288
            case 'TABLE':
289
                echo '<li>';
290
                echo "<a href=\"tables?subject=schema&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
291
                echo '<a href="'.\SUBFOLDER."/redirect/table?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;table=',
292
                urlencode($rs->fields['name']), '">', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
293
294
                break;
295
            case 'VIEW':
296
                echo '<li>';
297
                echo "<a href=\"views?subject=schema&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
298
                echo '<a href="'.\SUBFOLDER."/redirect/view?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;view=',
299
                urlencode($rs->fields['name']), '">', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
300
301
                break;
302
            case 'SEQUENCE':
303
                echo '<li>';
304
                echo "<a href=\"sequences?subject=schema&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
305
                echo "<a href=\"sequences?subject=sequence&amp;action=properties&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']),
306
                '&amp;sequence=', urlencode($rs->fields['name']), '">', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
307
308
                break;
309
            case 'COLUMNTABLE':
310
                echo '<li>';
311
                echo '<a href="'.\SUBFOLDER."/redirect/schema?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
312
                echo "<a href=\"tblproperties?subject=table&{$this->misc->href}&table=", urlencode($rs->fields['relname']), '&amp;schema=', urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['relname']), '</a>.';
313
                echo "<a href=\"colproperties?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;table=',
314
                urlencode($rs->fields['relname']), '&amp;column=', urlencode($rs->fields['name']), '">',
315
                $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
316
317
                break;
318
            case 'COLUMNVIEW':
319
                echo '<li>';
320
                echo '<a href="'.\SUBFOLDER."/redirect/schema?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
321
                echo "<a href=\"viewproperties?subject=view&{$this->misc->href}&view=", urlencode($rs->fields['relname']), '&amp;schema=', urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['relname']), '</a>.';
322
                echo "<a href=\"colproperties?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;view=',
323
                urlencode($rs->fields['relname']), '&amp;column=', urlencode($rs->fields['name']), '">',
324
                $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
325
326
                break;
327
            case 'INDEX':
328
                echo '<li>';
329
                echo '<a href="'.\SUBFOLDER."/redirect/schema?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
330
                echo '<a href="'.\SUBFOLDER."/redirect/table?{$this->misc->href}&table=", urlencode($rs->fields['relname']), '&amp;schema=', urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['relname']), '</a>.';
331
                echo "<a href=\"indexes?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;table=', urlencode($rs->fields['relname']), '">', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
332
333
                break;
334
            case 'CONSTRAINTTABLE':
335
                echo '<li>';
336
                echo '<a href="'.\SUBFOLDER."/redirect/schema?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
337
                echo '<a href="'.\SUBFOLDER."/redirect/table?{$this->misc->href}&table=", urlencode($rs->fields['relname']), '&amp;schema=', urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['relname']), '</a>.';
338
                echo "<a href=\"constraints?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;table=',
339
                urlencode($rs->fields['relname']), '">', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
340
341
                break;
342
            case 'CONSTRAINTDOMAIN':
343
                echo '<li>';
344
                echo "<a href=\"domains?subject=schema&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
345
                echo "<a href=\"domains?action=properties&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;domain=', urlencode($rs->fields['relname']), '">',
346
                $this->misc->printVal($rs->fields['relname']), '.', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
347
348
                break;
349
            case 'TRIGGER':
350
                echo '<li>';
351
                echo '<a href="'.\SUBFOLDER."/redirect/schema?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
352
                echo '<a href="'.\SUBFOLDER."/redirect/table?{$this->misc->href}&table=", urlencode($rs->fields['relname']), '&amp;schema=', urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['relname']), '</a>.';
353
                echo "<a href=\"triggers?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;table=', urlencode($rs->fields['relname']), '">',
354
                $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
355
356
                break;
357
            case 'RULETABLE':
358
                echo '<li>';
359
                echo '<a href="'.\SUBFOLDER."/redirect/schema?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
360
                echo '<a href="'.\SUBFOLDER."/redirect/table?{$this->misc->href}&table=", urlencode($rs->fields['relname']), '&amp;schema=', urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['relname']), '</a>.';
361
                echo "<a href=\"rules?subject=table&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;reltype=table&amp;table=',
362
                urlencode($rs->fields['relname']), '">', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
363
364
                break;
365
            case 'RULEVIEW':
366
                echo '<li>';
367
                echo '<a href="'.\SUBFOLDER."/redirect/schema?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
368
                echo '<a href="'.\SUBFOLDER."/redirect/view?{$this->misc->href}&view=", urlencode($rs->fields['relname']), '&amp;schema=', urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['relname']), '</a>.';
369
                echo "<a href=\"rules?subject=view&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;reltype=view&amp;view=',
370
                urlencode($rs->fields['relname']), '">', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
371
372
                break;
373
            case 'FUNCTION':
374
                echo '<li>';
375
                echo "<a href=\"functions?subject=schema&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
376
                echo "<a href=\"functions?action=properties&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;function=',
377
                urlencode($rs->fields['name']), '&amp;function_oid=', urlencode($rs->fields['oid']), '">',
378
                $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
379
380
                break;
381
            case 'TYPE':
382
                echo '<li>';
383
                echo "<a href=\"types?subject=schema&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
384
                echo "<a href=\"types?action=properties&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;type=',
385
                urlencode($rs->fields['name']), '">', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
386
387
                break;
388
            case 'DOMAIN':
389
                echo '<li>';
390
                echo "<a href=\"domains?subject=schema&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
391
                echo "<a href=\"domains?action=properties&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;domain=',
392
                urlencode($rs->fields['name']), '">', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
393
394
                break;
395
            case 'OPERATOR':
396
                echo '<li>';
397
                echo "<a href=\"operators?subject=schema&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
398
                echo "<a href=\"operators?action=properties&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '&amp;operator=',
399
                urlencode($rs->fields['name']), '&amp;operator_oid=', urlencode($rs->fields['oid']), '">', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
400
401
                break;
402
            case 'CONVERSION':
403
                echo '<li>';
404
                echo "<a href=\"conversions?subject=schema&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
405
                echo "<a href=\"conversions?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']),
406
                '">', $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
407
408
                break;
409
            case 'LANGUAGE':
410
                echo "<li><a href=\"languages?{$this->misc->href}\">", $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
411
412
                break;
413
            case 'AGGREGATE':
414
                echo '<li>';
415
                echo "<a href=\"aggregates?subject=schema&{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
416
                echo "<a href=\"aggregates?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">',
417
                $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
418
419
                break;
420
            case 'OPCLASS':
421
                echo '<li>';
422
                echo '<a href="'.\SUBFOLDER."/redirect/schema?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">', $this->misc->printVal($rs->fields['schemaname']), '</a>.';
423
                echo "<a href=\"opclasses?{$this->misc->href}&schema=", urlencode($rs->fields['schemaname']), '">',
424
                $this->_highlight($this->misc->printVal($rs->fields['name']), $_REQUEST['term']), '</a></li>'.PHP_EOL;
425
426
                break;
427
        }
428
    }
429
430
    public function doFind()
431
    {
432
        $data = $this->misc->getDatabaseAccessor();
433
        $rs   = $data->findObject($_REQUEST['term'], $_REQUEST['filter']);
434
        if ($rs->RecordCount() > 0) {
435
            $curr = '';
436
            while (!$rs->EOF) {
437
                // Output a new header if the current type has changed, but not if it's just changed the rule type
438
                if ($rs->fields['type'] != $curr) {
439
                    // Short-circuit in the case of changing from table rules to view rules; table cols to view cols;
440
                    // table constraints to domain constraints
441
                    if ('RULEVIEW' == $rs->fields['type'] && 'RULETABLE' == $curr) {
442
                        $curr = $rs->fields['type'];
443
                    } elseif ('COLUMNVIEW' == $rs->fields['type'] && 'COLUMNTABLE' == $curr) {
444
                        $curr = $rs->fields['type'];
445
                    } elseif ('CONSTRAINTTABLE' == $rs->fields['type'] && 'CONSTRAINTDOMAIN' == $curr) {
446
                        $curr = $rs->fields['type'];
447
                    } else {
448
                        if ('' != $curr) {
449
                            echo '</ul>'.PHP_EOL;
450
                        }
451
452
                        $curr = $rs->fields['type'];
453
                        echo '<h3>';
454
                        echo $this->_translatedType($curr);
455
                        echo '</h3>';
456
                        echo '<ul>'.PHP_EOL;
457
                    }
458
                }
459
460
                $this->_printHtmlForType($curr, $rs);
461
                $rs->moveNext();
462
            }
463
            echo '</ul>'.PHP_EOL;
464
465
            echo '<p>', $rs->recordCount(), ' ', $this->lang['strobjects'], '</p>'.PHP_EOL;
466
        } else {
467
            echo "<p>{$this->lang['strnoobjects']}</p>".PHP_EOL;
468
        }
469
    }
470
471
    /**
472
     * Displays options for database download.
473
     *
474
     * @param mixed $msg
475
     */
476
    public function doExport($msg = '')
477
    {
478
        $this->printTrail('database');
479
        $this->printTabs('database', 'export');
480
        $this->printMsg($msg);
481
482
        $subject = 'database';
483
        $object  = $_REQUEST['database'];
484
485
        echo $this->formHeader('dbexport');
486
487
        echo $this->dataOnly(true, true);
488
489
        echo $this->structureOnly();
490
491
        echo $this->structureAndData(true);
492
493
        echo $this->displayOrDownload(!(strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && isset($_SERVER['HTTPS'])));
494
495
        echo $this->formFooter($subject, $object);
496
    }
497
498
    /**
499
     * Show the current status of all database variables.
500
     */
501
    public function doVariables()
502
    {
503
        $data = $this->misc->getDatabaseAccessor();
504
505
        // Fetch the variables from the database
506
        $variables = $data->getVariables();
507
        $this->printTrail('database');
508
        $this->printTabs('database', 'variables');
509
510
        $columns = [
511
            'variable' => [
512
                'title' => $this->lang['strname'],
513
                'field' => Decorator::field('name'),
514
            ],
515
            'value'    => [
516
                'title' => $this->lang['strsetting'],
517
                'field' => Decorator::field('setting'),
518
            ],
519
        ];
520
521
        $actions = [];
522
523
        echo $this->printTable($variables, $columns, $actions, $this->table_place, $this->lang['strnodata']);
524
    }
525
526
    /**
527
     * Show all current database connections and any queries they
528
     * are running.
529
     *
530
     * @param mixed $msg
531
     */
532
    public function doProcesses($msg = '')
533
    {
534
        $this->printTrail('database');
535
        $this->printTabs('database', 'processes');
536
        $this->printMsg($msg);
537
538
        if (0 === strlen($msg)) {
539
            echo '<br /><a id="control" href=""><img src="'.$this->misc->icon('Refresh')."\" alt=\"{$this->lang['strrefresh']}\" title=\"{$this->lang['strrefresh']}\"/>&nbsp;{$this->lang['strrefresh']}</a>";
540
        }
541
542
        echo '<div id="data_block">';
543
        $this->currentProcesses();
544
        echo '</div>';
545
    }
546
547
    public function currentProcesses($isAjax = false)
548
    {
549
        $data = $this->misc->getDatabaseAccessor();
550
551
        // Display prepared transactions
552
        if ($data->hasPreparedXacts()) {
553
            echo "<h3>{$this->lang['strpreparedxacts']}</h3>".PHP_EOL;
554
            $prep_xacts = $data->getPreparedXacts($_REQUEST['database']);
555
556
            $columns = [
557
                'transaction' => [
558
                    'title' => $this->lang['strxactid'],
559
                    'field' => Decorator::field('transaction'),
560
                ],
561
                'gid'         => [
562
                    'title' => $this->lang['strgid'],
563
                    'field' => Decorator::field('gid'),
564
                ],
565
                'prepared'    => [
566
                    'title' => $this->lang['strstarttime'],
567
                    'field' => Decorator::field('prepared'),
568
                ],
569
                'owner'       => [
570
                    'title' => $this->lang['strowner'],
571
                    'field' => Decorator::field('owner'),
572
                ],
573
            ];
574
575
            $actions = [];
576
577
            echo $this->printTable($prep_xacts, $columns, $actions, 'database-processes-preparedxacts', $this->lang['strnodata']);
578
        }
579
580
        // Fetch the processes from the database
581
        echo "<h3>{$this->lang['strprocesses']}</h3>".PHP_EOL;
582
        $processes = $data->getProcesses($_REQUEST['database']);
583
584
        $columns = [
585
            'user'             => [
586
                'title' => $this->lang['strusername'],
587
                'field' => Decorator::field('usename'),
588
            ],
589
            'process'          => [
590
                'title' => $this->lang['strprocess'],
591
                'field' => Decorator::field('pid'),
592
            ],
593
            'application_name' => [
594
                'title' => 'application',
595
                'field' => Decorator::field('application_name'),
596
            ],
597
            'client_addr'      => [
598
                'title' => 'address',
599
                'field' => Decorator::field('client_addr'),
600
            ],
601
            'blocked'          => [
602
                'title' => $this->lang['strblocked'],
603
                'field' => Decorator::field('waiting'),
604
            ],
605
            'query'            => [
606
                'title' => $this->lang['strsql'],
607
                'field' => Decorator::field('query'),
608
            ],
609
            'start_time'       => [
610
                'title' => $this->lang['strstarttime'],
611
                'field' => Decorator::field('query_start'),
612
            ],
613
        ];
614
615
        // Build possible actions for our process list
616
        $columns['actions'] = ['title' => $this->lang['stractions']];
617
618
        $actions = [];
619
        if ($data->hasUserSignals() || $data->isSuperUser()) {
620
            $actions = [
621
                'cancel' => [
622
                    'content' => $this->lang['strcancel'],
623
                    'attr'    => [
624
                        'href' => [
625
                            'url'     => 'database',
626
                            'urlvars' => [
627
                                'action' => 'signal',
628
                                'signal' => 'CANCEL',
629
                                'pid'    => Decorator::field('pid'),
630
                            ],
631
                        ],
632
                    ],
633
                ],
634
                'kill'   => [
635
                    'content' => $this->lang['strkill'],
636
                    'attr'    => [
637
                        'href' => [
638
                            'url'     => 'database',
639
                            'urlvars' => [
640
                                'action' => 'signal',
641
                                'signal' => 'KILL',
642
                                'pid'    => Decorator::field('pid'),
643
                            ],
644
                        ],
645
                    ],
646
                ],
647
            ];
648
649
            // Remove actions where not supported
650
            if (!$data->hasQueryKill()) {
651
                unset($actions['kill']);
652
            }
653
654
            if (!$data->hasQueryCancel()) {
655
                unset($actions['cancel']);
656
            }
657
        }
658
659
        if (0 == count($actions)) {
660
            unset($columns['actions']);
661
        }
662
663
        echo $this->printTable($processes, $columns, $actions, 'database-processes', $this->lang['strnodata']);
664
    }
665
666
    public function currentLocks($isAjax = false)
667
    {
668
        $data = $this->misc->getDatabaseAccessor();
669
670
        // Get the info from the pg_locks view
671
        $variables = $data->getLocks();
672
673
        $columns = [
674
            'namespace'     => [
675
                'title' => $this->lang['strschema'],
676
                'field' => Decorator::field('nspname'),
677
            ],
678
            'tablename'     => [
679
                'title' => $this->lang['strtablename'],
680
                'field' => Decorator::field('tablename'),
681
            ],
682
            'vxid'          => [
683
                'title' => $this->lang['strvirtualtransaction'],
684
                'field' => Decorator::field('virtualtransaction'),
685
            ],
686
            'transactionid' => [
687
                'title' => $this->lang['strtransaction'],
688
                'field' => Decorator::field('transaction'),
689
            ],
690
            'processid'     => [
691
                'title' => $this->lang['strprocessid'],
692
                'field' => Decorator::field('pid'),
693
            ],
694
            'mode'          => [
695
                'title' => $this->lang['strmode'],
696
                'field' => Decorator::field('mode'),
697
            ],
698
            'granted'       => [
699
                'title' => $this->lang['strislockheld'],
700
                'field' => Decorator::field('granted'),
701
                'type'  => 'yesno',
702
            ],
703
        ];
704
705
        if (!$data->hasVirtualTransactionId()) {
706
            unset($columns['vxid']);
707
        }
708
709
        $actions = [];
710
        echo $this->printTable($variables, $columns, $actions, 'database-locks', $this->lang['strnodata']);
711
    }
712
713
    /**
714
     * Show the existing table locks in the current database.
715
     */
716
    public function doLocks()
717
    {
718
        $this->printTrail('database');
719
        $this->printTabs('database', 'locks');
720
721
        echo '<br /><a id="control" href=""><img src="'.$this->misc->icon('Refresh')."\" alt=\"{$this->lang['strrefresh']}\" title=\"{$this->lang['strrefresh']}\"/>&nbsp;{$this->lang['strrefresh']}</a>";
722
723
        echo '<div id="data_block">';
724
        $this->currentLocks();
725
        echo '</div>';
726
    }
727
728
    /**
729
     * Allow execution of arbitrary SQL statements on a database.
730
     */
731
    public function doSQL()
732
    {
733
        if ((!isset($_SESSION['sqlquery'])) || isset($_REQUEST['new'])) {
734
            $_SESSION['sqlquery'] = '';
735
            $_REQUEST['paginate'] = 'on';
736
        }
737
738
        $this->printTrail('database');
739
        $this->printTabs('database', 'sql');
740
        echo "<p>{$this->lang['strentersql']}</p>".PHP_EOL;
741
        echo '<form action="'.\SUBFOLDER.'/src/views/sql" method="post" enctype="multipart/form-data" id="sqlform">'.PHP_EOL;
742
        echo "<p>{$this->lang['strsql']}<br />".PHP_EOL;
743
        echo '<textarea style="width:95%;" rows="15" cols="50" name="query" id="query">',
744
        htmlspecialchars($_SESSION['sqlquery']), '</textarea></p>'.PHP_EOL;
745
746
        // Check that file uploads are enabled
747
        if (ini_get('file_uploads')) {
748
            // Don't show upload option if max size of uploads is zero
749
            $max_size = $this->misc->inisizeToBytes(ini_get('upload_max_filesize'));
750
            if (is_double($max_size) && $max_size > 0) {
751
                echo "<p><input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"{$max_size}\" />".PHP_EOL;
752
                echo "<label for=\"script\">{$this->lang['struploadscript']}</label> <input id=\"script\" name=\"script\" type=\"file\" /></p>".PHP_EOL;
753
            }
754
        }
755
756
        echo '<p><input type="checkbox" id="paginate" name="paginate"', (isset($_REQUEST['paginate']) ? ' checked="checked"' : ''), " /><label for=\"paginate\">{$this->lang['strpaginate']}</label></p>".PHP_EOL;
757
        echo "<p><input type=\"submit\" name=\"execute\" accesskey=\"r\" value=\"{$this->lang['strexecute']}\" />".PHP_EOL;
758
        echo $this->misc->form;
759
        echo "<input type=\"reset\" accesskey=\"q\" value=\"{$this->lang['strreset']}\" /></p>".PHP_EOL;
760
        echo '</form>'.PHP_EOL;
761
762
        // Default focus
763
        $this->setFocus('forms[0].query');
764
    }
765
766
    /**
767
     * This functions does pretty much nothing. It's meant to implement
768
     * an abstract method of AdminTrait.
769
     *
770
     * @param string $msg The message
771
     *
772
     * @return string The message
773
     */
774
    public function doDefault($msg = '')
775
    {
776
        return $msg;
777
    }
778
}
779