UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / navigation / Nodes / Node_Table.class.php
blob10a521a52cd8f54e520227e6c02ab5594df7481c
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Functionality for the navigation tree
6 * @package PhpMyAdmin-Navigation
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 require_once 'libraries/navigation/Nodes/Node_DatabaseChild.class.php';
14 /**
15 * Represents a columns node in the navigation tree
17 * @package PhpMyAdmin-Navigation
19 class Node_Table extends Node_DatabaseChild
21 /**
22 * Initialises the class
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 $is_group Whether this object has been created
27 * while grouping nodes
29 * @return Node_Table
31 public function __construct($name, $type = Node::OBJECT, $is_group = false)
33 parent::__construct($name, $type, $is_group);
34 $this->icon = array();
35 $this->_addIcon($GLOBALS['cfg']['NavigationTreeDefaultTabTable']);
36 $this->_addIcon($GLOBALS['cfg']['NavigationTreeDefaultTabTable2']);
37 switch($GLOBALS['cfg']['DefaultTabTable']) {
38 case 'tbl_structure.php':
39 $this->title = __('Structure');
40 break;
41 case 'tbl_select.php':
42 $this->title = __('Search');
43 break;
44 case 'tbl_change.php':
45 $this->title = __('Insert');
46 break;
47 case 'tbl_sql.php':
48 $this->title = __('SQL');
49 break;
50 case 'sql.php':
51 $this->title = __('Browse');
52 break;
54 $this->links = array(
55 'text' => $GLOBALS['cfg']['DefaultTabTable']
56 . '?server=' . $GLOBALS['server']
57 . '&amp;db=%2$s&amp;table=%1$s'
58 . '&amp;pos=0&amp;token=' . $_SESSION[' PMA_token '],
59 'icon' => array(
60 $GLOBALS['cfg']['NavigationTreeDefaultTabTable']
61 . '?server=' . $GLOBALS['server']
62 . '&amp;db=%2$s&amp;table=%1$s&amp;token='
63 . $_SESSION[' PMA_token '],
64 $GLOBALS['cfg']['NavigationTreeDefaultTabTable2']
65 . '?server=' . $GLOBALS['server']
66 . '&amp;db=%2$s&amp;table=%1$s&amp;token='
67 . $_SESSION[' PMA_token ']
69 'title' => $this->title
71 $this->classes = 'table';
74 /**
75 * Returns the number of children of type $type present inside this container
76 * This method is overridden by the Node_Database and Node_Table classes
78 * @param string $type The type of item we are looking for
79 * ('columns' or 'indexes')
80 * @param string $searchClause A string used to filter the results of the query
82 * @return int
84 public function getPresence($type = '', $searchClause = '')
86 $retval = 0;
87 $db = $this->realParent()->real_name;
88 $table = $this->real_name;
89 switch ($type) {
90 case 'columns':
91 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
92 $db = PMA_Util::sqlAddSlashes($db);
93 $table = PMA_Util::sqlAddSlashes($table);
94 $query = "SELECT COUNT(*) ";
95 $query .= "FROM `INFORMATION_SCHEMA`.`COLUMNS` ";
96 $query .= "WHERE `TABLE_NAME`='$table' ";
97 $query .= "AND `TABLE_SCHEMA`='$db'";
98 $retval = (int)$GLOBALS['dbi']->fetchValue($query);
99 } else {
100 $db = PMA_Util::backquote($db);
101 $table = PMA_Util::backquote($table);
102 $query = "SHOW COLUMNS FROM $table FROM $db";
103 $retval = (int)$GLOBALS['dbi']->numRows(
104 $GLOBALS['dbi']->tryQuery($query)
107 break;
108 case 'indexes':
109 $db = PMA_Util::backquote($db);
110 $table = PMA_Util::backquote($table);
111 $query = "SHOW INDEXES FROM $table FROM $db";
112 $retval = (int)$GLOBALS['dbi']->numRows(
113 $GLOBALS['dbi']->tryQuery($query)
115 break;
116 case 'triggers':
117 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
118 $db = PMA_Util::sqlAddSlashes($db);
119 $table = PMA_Util::sqlAddSlashes($table);
120 $query = "SELECT COUNT(*) ";
121 $query .= "FROM `INFORMATION_SCHEMA`.`TRIGGERS` ";
122 $query .= "WHERE `EVENT_OBJECT_SCHEMA` "
123 . PMA_Util::getCollateForIS() . "='$db' ";
124 $query .= "AND `EVENT_OBJECT_TABLE` "
125 . PMA_Util::getCollateForIS() . "='$table'";
126 $retval = (int)$GLOBALS['dbi']->fetchValue($query);
127 } else {
128 $db = PMA_Util::backquote($db);
129 $table = PMA_Util::sqlAddSlashes($table);
130 $query = "SHOW TRIGGERS FROM $db WHERE `Table` = '$table'";
131 $retval = (int)$GLOBALS['dbi']->numRows(
132 $GLOBALS['dbi']->tryQuery($query)
135 break;
136 default:
137 break;
139 return $retval;
143 * Returns the names of children of type $type present inside this container
144 * This method is overridden by the Node_Database and Node_Table classes
146 * @param string $type The type of item we are looking for
147 * ('tables', 'views', etc)
148 * @param int $pos The offset of the list within the results
149 * @param string $searchClause A string used to filter the results of the query
151 * @return array
153 public function getData($type, $pos, $searchClause = '')
155 $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
156 $retval = array();
157 $db = $this->realParent()->real_name;
158 $table = $this->real_name;
159 switch ($type) {
160 case 'columns':
161 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
162 $db = PMA_Util::sqlAddSlashes($db);
163 $table = PMA_Util::sqlAddSlashes($table);
164 $query = "SELECT `COLUMN_NAME` AS `name` ";
165 $query .= "FROM `INFORMATION_SCHEMA`.`COLUMNS` ";
166 $query .= "WHERE `TABLE_NAME`='$table' ";
167 $query .= "AND `TABLE_SCHEMA`='$db' ";
168 $query .= "ORDER BY `COLUMN_NAME` ASC ";
169 $query .= "LIMIT " . intval($pos) . ", $maxItems";
170 $retval = $GLOBALS['dbi']->fetchResult($query);
171 break;
174 $db = PMA_Util::backquote($db);
175 $table = PMA_Util::backquote($table);
176 $query = "SHOW COLUMNS FROM $table FROM $db";
177 $handle = $GLOBALS['dbi']->tryQuery($query);
178 if ($handle === false) {
179 break;
182 $count = 0;
183 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
184 if ($pos <= 0 && $count < $maxItems) {
185 $retval[] = $arr['Field'];
186 $count++;
188 $pos--;
190 break;
191 case 'indexes':
192 $db = PMA_Util::backquote($db);
193 $table = PMA_Util::backquote($table);
194 $query = "SHOW INDEXES FROM $table FROM $db";
195 $handle = $GLOBALS['dbi']->tryQuery($query);
196 if ($handle === false) {
197 break;
200 $count = 0;
201 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
202 if (in_array($arr['Key_name'], $retval)) {
203 continue;
205 if ($pos <= 0 && $count < $maxItems) {
206 $retval[] = $arr['Key_name'];
207 $count++;
209 $pos--;
211 break;
212 case 'triggers':
213 if (! $GLOBALS['cfg']['Server']['DisableIS']) {
214 $db = PMA_Util::sqlAddSlashes($db);
215 $table = PMA_Util::sqlAddSlashes($table);
216 $query = "SELECT `TRIGGER_NAME` AS `name` ";
217 $query .= "FROM `INFORMATION_SCHEMA`.`TRIGGERS` ";
218 $query .= "WHERE `EVENT_OBJECT_SCHEMA` "
219 . PMA_Util::getCollateForIS() . "='$db' ";
220 $query .= "AND `EVENT_OBJECT_TABLE` "
221 . PMA_Util::getCollateForIS() . "='$table' ";
222 $query .= "ORDER BY `TRIGGER_NAME` ASC ";
223 $query .= "LIMIT " . intval($pos) . ", $maxItems";
224 $retval = $GLOBALS['dbi']->fetchResult($query);
225 break;
228 $db = PMA_Util::backquote($db);
229 $table = PMA_Util::sqlAddSlashes($table);
230 $query = "SHOW TRIGGERS FROM $db WHERE `Table` = '$table'";
231 $handle = $GLOBALS['dbi']->tryQuery($query);
232 if ($handle === false) {
233 break;
236 $count = 0;
237 while ($arr = $GLOBALS['dbi']->fetchArray($handle)) {
238 if ($pos <= 0 && $count < $maxItems) {
239 $retval[] = $arr['Trigger'];
240 $count++;
242 $pos--;
244 break;
245 default:
246 break;
248 return $retval;
252 * Returns the type of the item represented by the node.
254 * @return string type of the item
256 protected function getItemType()
258 return 'table';
262 * Add an icon to navigation tree
264 * @param string $page Page name to redirect
266 * @return void
268 private function _addIcon($page)
270 if (empty($page)) {
271 return;
274 switch ($page) {
275 case 'tbl_structure.php':
276 $this->icon[] = PMA_Util::getImage('b_props.png', __('Structure'));
277 break;
278 case 'tbl_select.php':
279 $this->icon[] = PMA_Util::getImage('b_search.png', __('Search'));
280 break;
281 case 'tbl_change.php':
282 $this->icon[] = PMA_Util::getImage('b_insrow.png', __('Insert'));
283 break;
284 case 'tbl_sql.php':
285 $this->icon[] = PMA_Util::getImage('b_sql.png', __('SQL'));
286 break;
287 case 'sql.php':
288 $this->icon[] = PMA_Util::getImage('b_browse.png', __('Browse'));
289 break;