Passed
Push — master ( 9102b9...990f80 )
by Maurício
07:29
created

NodeTable::addIcon()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 7
nop 1
dl 0
loc 22
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/* vim: set expandtab sw=4 ts=4 sts=4: */
3
/**
4
 * Functionality for the navigation tree
5
 *
6
 * @package PhpMyAdmin-Navigation
7
 */
8
declare(strict_types=1);
9
10
namespace PhpMyAdmin\Navigation\Nodes;
11
12
use PhpMyAdmin\Util;
13
14
/**
15
 * Represents a columns node in the navigation tree
16
 *
17
 * @package PhpMyAdmin-Navigation
18
 */
19
class NodeTable extends NodeDatabaseChild
20
{
21
    /**
22
     * Initialises the class
23
     *
24
     * @param string $name    An identifier for the new node
25
     * @param int    $type    Type of node, may be one of CONTAINER or OBJECT
26
     * @param bool   $isGroup Whether this object has been created
27
     *                        while grouping nodes
28
     */
29
    public function __construct($name, $type = Node::OBJECT, $isGroup = false)
30
    {
31
        parent::__construct($name, $type, $isGroup);
32
        $this->icon = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type string of property $icon.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
        $this->addIcon(
34
            Util::getScriptNameForOption(
35
                $GLOBALS['cfg']['NavigationTreeDefaultTabTable'],
36
                'table'
37
            )
38
        );
39
        $this->addIcon(
40
            Util::getScriptNameForOption(
41
                $GLOBALS['cfg']['NavigationTreeDefaultTabTable2'],
42
                'table'
43
            )
44
        );
45
        $title = Util::getTitleForTarget(
46
            $GLOBALS['cfg']['DefaultTabTable']
47
        );
48
        $this->title = $title;
49
50
        $scriptName = Util::getScriptNameForOption(
51
            $GLOBALS['cfg']['DefaultTabTable'],
52
            'table'
53
        );
54
        $this->links = [
55
            'text'  => $scriptName
56
                . '?server=' . $GLOBALS['server']
57
                . '&amp;db=%2$s&amp;table=%1$s'
58
                . '&amp;pos=0',
59
            'icon'  => [
60
                Util::getScriptNameForOption(
61
                    $GLOBALS['cfg']['NavigationTreeDefaultTabTable'],
62
                    'table'
63
                )
64
                . '?server=' . $GLOBALS['server']
65
                . '&amp;db=%2$s&amp;table=%1$s',
66
                Util::getScriptNameForOption(
67
                    $GLOBALS['cfg']['NavigationTreeDefaultTabTable2'],
68
                    'table'
69
                )
70
                . '?server=' . $GLOBALS['server']
71
                . '&amp;db=%2$s&amp;table=%1$s',
72
            ],
73
            'title' => $this->title,
74
        ];
75
        $this->classes = 'table';
76
    }
77
78
    /**
79
     * Returns the number of children of type $type present inside this container
80
     * This method is overridden by the PhpMyAdmin\Navigation\Nodes\NodeDatabase
81
     * and PhpMyAdmin\Navigation\Nodes\NodeTable classes
82
     *
83
     * @param string $type         The type of item we are looking for
84
     *                             ('columns' or 'indexes')
85
     * @param string $searchClause A string used to filter the results of the query
86
     *
87
     * @return int
88
     */
89
    public function getPresence($type = '', $searchClause = '')
90
    {
91
        $retval = 0;
92
        $db = $this->realParent()->realName;
93
        $table = $this->realName;
94
        switch ($type) {
95
            case 'columns':
96
                if (! $GLOBALS['cfg']['Server']['DisableIS']) {
97
                    $db = $GLOBALS['dbi']->escapeString($db);
98
                    $table = $GLOBALS['dbi']->escapeString($table);
99
                    $query = "SELECT COUNT(*) ";
100
                    $query .= "FROM `INFORMATION_SCHEMA`.`COLUMNS` ";
101
                    $query .= "WHERE `TABLE_NAME`='$table' ";
102
                    $query .= "AND `TABLE_SCHEMA`='$db'";
103
                    $retval = (int) $GLOBALS['dbi']->fetchValue($query);
104
                } else {
105
                    $db = Util::backquote($db);
106
                    $table = Util::backquote($table);
107
                    $query = "SHOW COLUMNS FROM $table FROM $db";
108
                    $retval = (int) $GLOBALS['dbi']->numRows(
109
                        $GLOBALS['dbi']->tryQuery($query)
110
                    );
111
                }
112
                break;
113
            case 'indexes':
114
                $db = Util::backquote($db);
115
                $table = Util::backquote($table);
116
                $query = "SHOW INDEXES FROM $table FROM $db";
117
                $retval = (int) $GLOBALS['dbi']->numRows(
118
                    $GLOBALS['dbi']->tryQuery($query)
119
                );
120
                break;
121
            case 'triggers':
122
                if (! $GLOBALS['cfg']['Server']['DisableIS']) {
123
                    $db = $GLOBALS['dbi']->escapeString($db);
124
                    $table = $GLOBALS['dbi']->escapeString($table);
125
                    $query = "SELECT COUNT(*) ";
126
                    $query .= "FROM `INFORMATION_SCHEMA`.`TRIGGERS` ";
127
                    $query .= "WHERE `EVENT_OBJECT_SCHEMA` "
128
                    . Util::getCollateForIS() . "='$db' ";
129
                    $query .= "AND `EVENT_OBJECT_TABLE` "
130
                    . Util::getCollateForIS() . "='$table'";
131
                    $retval = (int) $GLOBALS['dbi']->fetchValue($query);
132
                } else {
133
                    $db = Util::backquote($db);
134
                    $table = $GLOBALS['dbi']->escapeString($table);
135
                    $query = "SHOW TRIGGERS FROM $db WHERE `Table` = '$table'";
136
                    $retval = (int) $GLOBALS['dbi']->numRows(
137
                        $GLOBALS['dbi']->tryQuery($query)
138
                    );
139
                }
140
                break;
141
            default:
142
                break;
143
        }
144
145
        return $retval;
146
    }
147
148
    /**
149
     * Returns the names of children of type $type present inside this container
150
     * This method is overridden by the PhpMyAdmin\Navigation\Nodes\NodeDatabase
151
     * and PhpMyAdmin\Navigation\Nodes\NodeTable classes
152
     *
153
     * @param string $type         The type of item we are looking for
154
     *                             ('tables', 'views', etc)
155
     * @param int    $pos          The offset of the list within the results
156
     * @param string $searchClause A string used to filter the results of the query
157
     *
158
     * @return array
159
     */
160
    public function getData($type, $pos, $searchClause = '')
161
    {
162
        $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
163
        $retval = [];
164
        $db = $this->realParent()->realName;
165
        $table = $this->realName;
166
        switch ($type) {
167
            case 'columns':
168
                if (! $GLOBALS['cfg']['Server']['DisableIS']) {
169
                    $db = $GLOBALS['dbi']->escapeString($db);
170
                    $table = $GLOBALS['dbi']->escapeString($table);
171
                    $query = "SELECT `COLUMN_NAME` AS `name` ";
172
                    $query .= ",`COLUMN_KEY` AS `key` ";
173
                    $query .= ",`DATA_TYPE` AS `type` ";
174
                    $query .= ",`COLUMN_DEFAULT` AS `default` ";
175
                    $query .= ",IF (`IS_NULLABLE` = 'NO', '', 'nullable') AS `nullable` ";
176
                    $query .= "FROM `INFORMATION_SCHEMA`.`COLUMNS` ";
177
                    $query .= "WHERE `TABLE_NAME`='$table' ";
178
                    $query .= "AND `TABLE_SCHEMA`='$db' ";
179
                    $query .= "ORDER BY `COLUMN_NAME` ASC ";
180
                    $query .= "LIMIT " . intval($pos) . ", $maxItems";
181
                    $retval = $GLOBALS['dbi']->fetchResult($query);
182
                    break;
183
                }
184
185
                $db = Util::backquote($db);
186
                $table = Util::backquote($table);
187
                $query = "SHOW COLUMNS FROM $table FROM $db";
188
                $handle = $GLOBALS['dbi']->tryQuery($query);
189
                if ($handle === false) {
190
                    break;
191
                }
192
193
                $count = 0;
194
                if ($GLOBALS['dbi']->dataSeek($handle, $pos)) {
195
                    while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
196
                        if ($count < $maxItems) {
197
                            $retval[] = $arr['Field'];
198
                            $count++;
199
                        } else {
200
                            break;
201
                        }
202
                    }
203
                }
204
                break;
205
            case 'indexes':
206
                $db = Util::backquote($db);
207
                $table = Util::backquote($table);
208
                $query = "SHOW INDEXES FROM $table FROM $db";
209
                $handle = $GLOBALS['dbi']->tryQuery($query);
210
                if ($handle === false) {
211
                    break;
212
                }
213
214
                $count = 0;
215
                while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
216
                    if (in_array($arr['Key_name'], $retval)) {
217
                        continue;
218
                    }
219
                    if ($pos <= 0 && $count < $maxItems) {
220
                        $retval[] = $arr['Key_name'];
221
                        $count++;
222
                    }
223
                    $pos--;
224
                }
225
                break;
226
            case 'triggers':
227
                if (! $GLOBALS['cfg']['Server']['DisableIS']) {
228
                    $db = $GLOBALS['dbi']->escapeString($db);
229
                    $table = $GLOBALS['dbi']->escapeString($table);
230
                    $query = "SELECT `TRIGGER_NAME` AS `name` ";
231
                    $query .= "FROM `INFORMATION_SCHEMA`.`TRIGGERS` ";
232
                    $query .= "WHERE `EVENT_OBJECT_SCHEMA` "
233
                    . Util::getCollateForIS() . "='$db' ";
234
                    $query .= "AND `EVENT_OBJECT_TABLE` "
235
                    . Util::getCollateForIS() . "='$table' ";
236
                    $query .= "ORDER BY `TRIGGER_NAME` ASC ";
237
                    $query .= "LIMIT " . intval($pos) . ", $maxItems";
238
                    $retval = $GLOBALS['dbi']->fetchResult($query);
239
                    break;
240
                }
241
242
                $db = Util::backquote($db);
243
                $table = $GLOBALS['dbi']->escapeString($table);
244
                $query = "SHOW TRIGGERS FROM $db WHERE `Table` = '$table'";
245
                $handle = $GLOBALS['dbi']->tryQuery($query);
246
                if ($handle === false) {
247
                    break;
248
                }
249
250
                $count = 0;
251
                if ($GLOBALS['dbi']->dataSeek($handle, $pos)) {
252
                    while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
253
                        if ($count < $maxItems) {
254
                            $retval[] = $arr['Trigger'];
255
                            $count++;
256
                        } else {
257
                            break;
258
                        }
259
                    }
260
                }
261
                break;
262
            default:
263
                break;
264
        }
265
266
        return $retval;
267
    }
268
269
    /**
270
     * Returns the type of the item represented by the node.
271
     *
272
     * @return string type of the item
273
     */
274
    protected function getItemType()
275
    {
276
        return 'table';
277
    }
278
279
    /**
280
     * Add an icon to navigation tree
281
     *
282
     * @param string $page Page name to redirect
283
     *
284
     * @return void
285
     */
286
    private function addIcon($page)
287
    {
288
        if (empty($page)) {
289
            return;
290
        }
291
292
        switch ($page) {
293
            case 'tbl_structure.php':
294
                $this->icon[] = Util::getImage('b_props', __('Structure'));
295
                break;
296
            case 'tbl_select.php':
297
                $this->icon[] = Util::getImage('b_search', __('Search'));
298
                break;
299
            case 'tbl_change.php':
300
                $this->icon[] = Util::getImage('b_insrow', __('Insert'));
301
                break;
302
            case 'tbl_sql.php':
303
                $this->icon[] = Util::getImage('b_sql', __('SQL'));
304
                break;
305
            case 'sql.php':
306
                $this->icon[] = Util::getImage('b_browse', __('Browse'));
307
                break;
308
        }
309
    }
310
}
311