-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Node.class.php
455 lines (419 loc) · 13.2 KB
/
Node.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Functionality for the navigation tree in the left frame
*
* @package PhpMyAdmin-Navigation
*/
if (! defined('PHPMYADMIN')) {
exit;
}
/**
* The Node is the building block for the collapsible navigation tree
*
* @package PhpMyAdmin-Navigation
*/
class Node
{
/**
* @var int Defines a possible node type
*/
const CONTAINER = 0;
/**
* @var int Defines a possible node type
*/
const OBJECT = 1;
/**
* @var string A non-unique identifier for the node
* This may be trimmed when grouping nodes
*/
public $name = "";
/**
* @var string A non-unique identifier for the node
* This will never change after being assigned
*/
public $real_name = "";
/**
* @var int May be one of CONTAINER or OBJECT
*/
public $type = Node::OBJECT;
/**
* @var bool Whether this object has been created while grouping nodes
* Only relevant if the node is of type CONTAINER
*/
public $is_group;
/**
* @var bool Whether to add a "display: none;" CSS
* rule to the node when rendering it
*/
public $visible = false;
/**
* @var Node A reference to the parent object of
* this node, NULL for the root node.
*/
public $parent;
/**
* @var array An array of Node objects that are
* direct children of this node
*/
public $children = array();
/**
* @var Mixed A string used to group nodes, or an array of strings
* Only relevant if the node is of type CONTAINER
*/
public $separator = '';
/**
* @var int How many time to recursively apply the grouping function
* Only relevant if the node is of type CONTAINER
*/
public $separator_depth = 1;
/**
* @var string An IMG tag, used when rendering the node
*/
public $icon;
/**
* @var Array An array of A tags, used when rendering the node
* The indexes in the array may be 'icon' and 'text'
*/
public $links;
/**
* @var string Extra CSS classes for the node
*/
public $classes = '';
/**
* @var string Whether this node is a link for creating new objects
*/
public $isNew = false;
/**
* @var int The position for the pagination of
* the branch at the second level of the tree
*/
public $pos2 = 0;
/**
* @var int The position for the pagination of
* the branch at the third level of the tree
*/
public $pos3 = 0;
/**
* Initialises the class by setting the mandatory variables
*
* @param string $name An identifier for the new node
* @param int $type Type of node, may be one of CONTAINER or OBJECT
* @param bool $is_group Whether this object has been created
* while grouping nodes
*
* @return Node
*/
public function __construct($name, $type = Node::OBJECT, $is_group = false)
{
if (! empty($name)) {
$this->name = $name;
$this->real_name = $name;
}
if ($type === Node::CONTAINER) {
$this->type = Node::CONTAINER;
}
$this->is_group = (bool)$is_group;
}
/**
* Adds a child node to this node
*
* @param Node $child A child node
*
* @return nothing
*/
public function addChild($child)
{
$this->children[] = $child;
$child->parent = $this;
}
/**
* Returns a child node given it's name
*
* @param string $name The name of requested child
* @param bool $real_name Whether to use the "real_name"
* instead of "name" in comparisons
*
* @return false|Node The requested child node or false,
* if the requested node cannot be found
*/
public function getChild($name, $real_name = false)
{
if ($real_name) {
foreach ($this->children as $child) {
if ($child->real_name == $name) {
return $child;
}
}
} else {
foreach ($this->children as $child) {
if ($child->name == $name) {
return $child;
}
}
}
return false;
}
/**
* Removes a child node from this node
*
* @param string $name The name of child to be removed
*
* @return nothing
*/
public function removeChild($name)
{
foreach ($this->children as $key => $child) {
if ($child->name == $name) {
unset($this->children[$key]);
break;
}
}
}
/**
* Retreives the parents for a node
*
* @param bool $self Whether to include the Node itself in the results
* @param bool $containers Whether to include nodes of type CONTAINER
* @param bool $groups Whether to include nodes which have $group == true
*
* @return array An array of parent Nodes
*/
public function parents($self = false, $containers = false, $groups = false)
{
$parents = array();
if ($self
&& ($this->type != Node::CONTAINER || $containers)
&& ($this->is_group != true || $groups)
) {
$parents[] = $this;
$self = false;
}
$parent = $this->parent;
while (isset($parent)) {
if ( ($parent->type != Node::CONTAINER || $containers)
&& ($parent->is_group != true || $groups)
) {
$parents[] = $parent;
}
$parent = $parent->parent;
}
return $parents;
}
/**
* Returns the actual parent of a node. If used twice on an index or columns
* node, it will return the table and database nodes. The names of the returned
* nodes can be used in SQL queries, etc...
*
* @return Node
*/
public function realParent()
{
$retval = $this->parents();
if (count($retval) > 0) {
return $retval[0];
} else {
return false;
}
}
/**
* This function checks if the node has children nodes associated with it
*
* @param bool $count_empty_containers Whether to count empty child
* containers as valid children
*
* @return bool Whether the node has child nodes
*/
public function hasChildren($count_empty_containers = true)
{
$retval = false;
if ($count_empty_containers) {
if (count($this->children)) {
$retval = true;
}
} else {
foreach ($this->children as $child) {
if ($child->type == Node::OBJECT || $child->hasChildren(false)) {
$retval = true;
break;
}
}
}
return $retval;
}
/**
* Returns true the node has some siblings (other nodes on the same tree level,
* in the same branch), false otherwise. The only exception is for nodes on
* the third level of the tree (columns and indexes), for which the function
* always returns true. This is because we want to render the containers
* for these nodes
*
* @return bool
*/
public function hasSiblings()
{
$retval = false;
$paths = $this->getPaths();
if (count($paths['aPath_clean']) > 3) {
$retval = true;
} else {
foreach ($this->parent->children as $child) {
if ($child !== $this
&& ($child->type == Node::OBJECT || $child->hasChildren(false))
) {
$retval = true;
break;
}
}
}
return $retval;
}
/**
* Returns the number of child nodes that a node has associated with it
*
* @return int The number of children nodes
*/
public function numChildren()
{
$retval = 0;
foreach ($this->children as $child) {
if ($child->type == Node::OBJECT) {
$retval++;
} else {
$retval += $child->numChildren();
}
}
return $retval;
}
/**
* Returns the actual path and the virtual paths for a node
* both as clean arrays and base64 encoded strings
*
* @return array
*/
public function getPaths()
{
$aPath = array();
$aPath_clean = array();
foreach ($this->parents(true, true, false) as $parent) {
$aPath[] = base64_encode($parent->real_name);
$aPath_clean[] = $parent->real_name;
}
$aPath = implode('.', array_reverse($aPath));
$aPath_clean = array_reverse($aPath_clean);
$vPath = array();
$vPath_clean = array();
foreach ($this->parents(true, true, true) as $parent) {
$vPath[] = base64_encode($parent->name);
$vPath_clean[] = $parent->name;
}
$vPath = implode('.', array_reverse($vPath));
$vPath_clean = array_reverse($vPath_clean);
return array(
'aPath' => $aPath,
'aPath_clean' => $aPath_clean,
'vPath' => $vPath,
'vPath_clean' => $vPath_clean
);
}
/**
* Returns the names of children of type $type present inside this container
* This method is overridden by the Node_Database and Node_Table classes
*
* @param string $type The type of item we are looking for
* ('tables', 'views', etc)
* @param int $pos The offset of the list within the results
* @param string $searchClause A string used to filter the results of the query
*
* @return array
*/
public function getData($type, $pos, $searchClause = '')
{
// @todo obey the DisableIS directive
$query = "SELECT `SCHEMA_NAME` ";
$query .= "FROM `INFORMATION_SCHEMA`.`SCHEMATA` ";
$query .= $this->_getWhereClause($searchClause);
$query .= "ORDER BY `SCHEMA_NAME` ASC ";
$query .= "LIMIT $pos, {$GLOBALS['cfg']['MaxNavigationItems']}";
return PMA_DBI_fetchResult($query);
}
/**
* Returns the comment associated with node
* This method should be overridden by specific type of nodes
*
* @return string
*/
public function getComment()
{
return '';
}
/**
* Returns the number of children of type $type present inside this container
* This method is overridden by the Node_Database and Node_Table classes
*
* @param string $type The type of item we are looking for
* ('tables', 'views', etc)
* @param string $searchClause A string used to filter the results of the query
*
* @return int
*/
public function getPresence($type = '', $searchClause = '')
{
if (! $GLOBALS['cfg']['Servers'][$GLOBALS['server']]['DisableIS']) {
$query = "SELECT COUNT(*) ";
$query .= "FROM `INFORMATION_SCHEMA`.`SCHEMATA` ";
$query .= $this->_getWhereClause($searchClause);
$retval = (int)PMA_DBI_fetchValue($query);
} else {
$query = "SHOW DATABASES ";
if (! empty($searchClause)) {
$query .= "LIKE '%";
$query .= PMA_Util::sqlAddSlashes(
$searchClause, true
);
$query .= "%' ";
}
$retval = PMA_DBI_numRows(PMA_DBI_tryQuery($query));
}
return $retval;
}
/**
* Returns the WHERE clause depending on the $searchClause parameter
* and the hide_db directive
*
* @param string $searchClause A string used to filter the results of the query
*
* @return string
*/
private function _getWhereClause($searchClause = '')
{
$whereClause = "WHERE TRUE ";
if (! empty($searchClause)) {
$whereClause .= "AND `SCHEMA_NAME` LIKE '%";
$whereClause .= PMA_Util::sqlAddSlashes(
$searchClause, true
);
$whereClause .= "%' ";
}
if (! empty($GLOBALS['cfg']['Server']['hide_db'])) {
$whereClause .= "AND `SCHEMA_NAME` NOT REGEXP '"
. $GLOBALS['cfg']['Server']['hide_db'] . "' ";
}
if (! empty($GLOBALS['cfg']['Server']['only_db'])) {
if (is_string($GLOBALS['cfg']['Server']['only_db'])) {
$GLOBALS['cfg']['Server']['only_db'] = array(
$GLOBALS['cfg']['Server']['only_db']
);
}
$whereClause .= "AND (";
$subClauses = array();
foreach ($GLOBALS['cfg']['Server']['only_db'] as $each_only_db) {
$subClauses[] = " `SCHEMA_NAME` LIKE '"
. $each_only_db . "' ";
}
$whereClause .= implode("OR", $subClauses) . ")";
}
return $whereClause;
}
}
?>