Node_Database.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Represents a database node in the navigation tree
  13. *
  14. * @package PhpMyAdmin-Navigation
  15. */
  16. class Node_Database extends Node
  17. {
  18. /**
  19. * Initialises the class
  20. *
  21. * @param string $name An identifier for the new node
  22. * @param int $type Type of node, may be one of CONTAINER or OBJECT
  23. * @param bool $is_group Whether this object has been created
  24. * while grouping nodes
  25. *
  26. * @return Node_Database
  27. */
  28. public function __construct($name, $type = Node::OBJECT, $is_group = false)
  29. {
  30. parent::__construct($name, $type, $is_group);
  31. $this->icon = PMA_Util::getImage(
  32. 's_db.png',
  33. __('Database operations')
  34. );
  35. $this->links = array(
  36. 'text' => $GLOBALS['cfg']['DefaultTabDatabase']
  37. . '?server=' . $GLOBALS['server']
  38. . '&amp;db=%1$s&amp;token=' . $GLOBALS['token'],
  39. 'icon' => 'db_operations.php?server=' . $GLOBALS['server']
  40. . '&amp;db=%1$s&amp;token=' . $GLOBALS['token']
  41. );
  42. $this->classes = 'database';
  43. }
  44. /**
  45. * Returns the number of children of type $type present inside this container
  46. * This method is overridden by the Node_Database and Node_Table classes
  47. *
  48. * @param string $type The type of item we are looking for
  49. * ('tables', 'views', etc)
  50. * @param string $searchClause A string used to filter the results of the query
  51. *
  52. * @return int
  53. */
  54. public function getPresence($type = '', $searchClause = '')
  55. {
  56. $retval = 0;
  57. $db = $this->real_name;
  58. switch ($type) {
  59. case 'tables':
  60. $db = PMA_Util::sqlAddSlashes($db);
  61. $query = "SELECT COUNT(*) ";
  62. $query .= "FROM `INFORMATION_SCHEMA`.`TABLES` ";
  63. $query .= "WHERE `TABLE_SCHEMA`='$db' ";
  64. if (PMA_DRIZZLE) {
  65. $query .= "AND `TABLE_TYPE`='BASE' ";
  66. } else {
  67. $query .= "AND `TABLE_TYPE`='BASE TABLE' ";
  68. }
  69. if (! empty($searchClause)) {
  70. $query .= "AND `TABLE_NAME` LIKE '%";
  71. $query .= PMA_Util::sqlAddSlashes(
  72. $searchClause, true
  73. );
  74. $query .= "%'";
  75. }
  76. $retval = (int)$GLOBALS['dbi']->fetchValue($query);
  77. break;
  78. case 'views':
  79. $db = PMA_Util::sqlAddSlashes($db);
  80. $query = "SELECT COUNT(*) ";
  81. $query .= "FROM `INFORMATION_SCHEMA`.`TABLES` ";
  82. $query .= "WHERE `TABLE_SCHEMA`='$db' ";
  83. if (PMA_DRIZZLE) {
  84. $query .= "AND `TABLE_TYPE`!='BASE' ";
  85. } else {
  86. $query .= "AND `TABLE_TYPE`!='BASE TABLE' ";
  87. }
  88. if (! empty($searchClause)) {
  89. $query .= "AND `TABLE_NAME` LIKE '%";
  90. $query .= PMA_Util::sqlAddSlashes(
  91. $searchClause, true
  92. );
  93. $query .= "%'";
  94. }
  95. $retval = (int)$GLOBALS['dbi']->fetchValue($query);
  96. break;
  97. case 'procedures':
  98. $db = PMA_Util::sqlAddSlashes($db);
  99. $query = "SELECT COUNT(*) ";
  100. $query .= "FROM `INFORMATION_SCHEMA`.`ROUTINES` ";
  101. $query .= "WHERE `ROUTINE_SCHEMA`='$db'";
  102. $query .= "AND `ROUTINE_TYPE`='PROCEDURE' ";
  103. if (! empty($searchClause)) {
  104. $query .= "AND `ROUTINE_NAME` LIKE '%";
  105. $query .= PMA_Util::sqlAddSlashes(
  106. $searchClause, true
  107. );
  108. $query .= "%'";
  109. }
  110. $retval = (int)$GLOBALS['dbi']->fetchValue($query);
  111. break;
  112. case 'functions':
  113. $db = PMA_Util::sqlAddSlashes($db);
  114. $query = "SELECT COUNT(*) ";
  115. $query .= "FROM `INFORMATION_SCHEMA`.`ROUTINES` ";
  116. $query .= "WHERE `ROUTINE_SCHEMA`='$db' ";
  117. $query .= "AND `ROUTINE_TYPE`='FUNCTION' ";
  118. if (! empty($searchClause)) {
  119. $query .= "AND `ROUTINE_NAME` LIKE '%";
  120. $query .= PMA_Util::sqlAddSlashes(
  121. $searchClause, true
  122. );
  123. $query .= "%'";
  124. }
  125. $retval = (int)$GLOBALS['dbi']->fetchValue($query);
  126. break;
  127. case 'events':
  128. $db = PMA_Util::sqlAddSlashes($db);
  129. $query = "SELECT COUNT(*) ";
  130. $query .= "FROM `INFORMATION_SCHEMA`.`EVENTS` ";
  131. $query .= "WHERE `EVENT_SCHEMA`='$db' ";
  132. if (! empty($searchClause)) {
  133. $query .= "AND `EVENT_NAME` LIKE '%";
  134. $query .= PMA_Util::sqlAddSlashes(
  135. $searchClause, true
  136. );
  137. $query .= "%'";
  138. }
  139. $retval = (int)$GLOBALS['dbi']->fetchValue($query);
  140. break;
  141. default:
  142. break;
  143. }
  144. return $retval;
  145. }
  146. /**
  147. * Returns the names of children of type $type present inside this container
  148. * This method is overridden by the Node_Database and Node_Table classes
  149. *
  150. * @param string $type The type of item we are looking for
  151. * ('tables', 'views', etc)
  152. * @param int $pos The offset of the list within the results
  153. * @param string $searchClause A string used to filter the results of the query
  154. *
  155. * @return array
  156. */
  157. public function getData($type, $pos, $searchClause = '')
  158. {
  159. $maxItems = $GLOBALS['cfg']['MaxNavigationItems'];
  160. $retval = array();
  161. $db = $this->real_name;
  162. switch ($type) {
  163. case 'tables':
  164. $escdDb = PMA_Util::sqlAddSlashes($db);
  165. $query = "SELECT `TABLE_NAME` AS `name` ";
  166. $query .= "FROM `INFORMATION_SCHEMA`.`TABLES` ";
  167. $query .= "WHERE `TABLE_SCHEMA`='$escdDb' ";
  168. if (PMA_DRIZZLE) {
  169. $query .= "AND `TABLE_TYPE`='BASE' ";
  170. } else {
  171. $query .= "AND `TABLE_TYPE`='BASE TABLE' ";
  172. }
  173. if (! empty($searchClause)) {
  174. $query .= "AND `TABLE_NAME` LIKE '%";
  175. $query .= PMA_Util::sqlAddSlashes(
  176. $searchClause, true
  177. );
  178. $query .= "%'";
  179. }
  180. $query .= "ORDER BY `TABLE_NAME` ASC ";
  181. $query .= "LIMIT " . intval($pos) . ", $maxItems";
  182. $retval = $GLOBALS['dbi']->fetchResult($query);
  183. break;
  184. case 'views':
  185. $escdDb = PMA_Util::sqlAddSlashes($db);
  186. $query = "SELECT `TABLE_NAME` AS `name` ";
  187. $query .= "FROM `INFORMATION_SCHEMA`.`TABLES` ";
  188. $query .= "WHERE `TABLE_SCHEMA`='$escdDb' ";
  189. if (PMA_DRIZZLE) {
  190. $query .= "AND `TABLE_TYPE`!='BASE' ";
  191. } else {
  192. $query .= "AND `TABLE_TYPE`!='BASE TABLE' ";
  193. }
  194. if (! empty($searchClause)) {
  195. $query .= "AND `TABLE_NAME` LIKE '%";
  196. $query .= PMA_Util::sqlAddSlashes(
  197. $searchClause, true
  198. );
  199. $query .= "%'";
  200. }
  201. $query .= "ORDER BY `TABLE_NAME` ASC ";
  202. $query .= "LIMIT " . intval($pos) . ", $maxItems";
  203. $retval = $GLOBALS['dbi']->fetchResult($query);
  204. break;
  205. case 'procedures':
  206. $escdDb = PMA_Util::sqlAddSlashes($db);
  207. $query = "SELECT `ROUTINE_NAME` AS `name` ";
  208. $query .= "FROM `INFORMATION_SCHEMA`.`ROUTINES` ";
  209. $query .= "WHERE `ROUTINE_SCHEMA`='$escdDb'";
  210. $query .= "AND `ROUTINE_TYPE`='PROCEDURE' ";
  211. if (! empty($searchClause)) {
  212. $query .= "AND `ROUTINE_NAME` LIKE '%";
  213. $query .= PMA_Util::sqlAddSlashes(
  214. $searchClause, true
  215. );
  216. $query .= "%'";
  217. }
  218. $query .= "ORDER BY `ROUTINE_NAME` ASC ";
  219. $query .= "LIMIT " . intval($pos) . ", $maxItems";
  220. $retval = $GLOBALS['dbi']->fetchResult($query);
  221. break;
  222. case 'functions':
  223. $escdDb = PMA_Util::sqlAddSlashes($db);
  224. $query = "SELECT `ROUTINE_NAME` AS `name` ";
  225. $query .= "FROM `INFORMATION_SCHEMA`.`ROUTINES` ";
  226. $query .= "WHERE `ROUTINE_SCHEMA`='$escdDb' ";
  227. $query .= "AND `ROUTINE_TYPE`='FUNCTION' ";
  228. if (! empty($searchClause)) {
  229. $query .= "AND `ROUTINE_NAME` LIKE '%";
  230. $query .= PMA_Util::sqlAddSlashes(
  231. $searchClause, true
  232. );
  233. $query .= "%'";
  234. }
  235. $query .= "ORDER BY `ROUTINE_NAME` ASC ";
  236. $query .= "LIMIT " . intval($pos) . ", $maxItems";
  237. $retval = $GLOBALS['dbi']->fetchResult($query);
  238. break;
  239. case 'events':
  240. $escdDb = PMA_Util::sqlAddSlashes($db);
  241. $query = "SELECT `EVENT_NAME` AS `name` ";
  242. $query .= "FROM `INFORMATION_SCHEMA`.`EVENTS` ";
  243. $query .= "WHERE `EVENT_SCHEMA`='$escdDb' ";
  244. if (! empty($searchClause)) {
  245. $query .= "AND `EVENT_NAME` LIKE '%";
  246. $query .= PMA_Util::sqlAddSlashes(
  247. $searchClause, true
  248. );
  249. $query .= "%'";
  250. }
  251. $query .= "ORDER BY `EVENT_NAME` ASC ";
  252. $query .= "LIMIT " . intval($pos) . ", $maxItems";
  253. $retval = $GLOBALS['dbi']->fetchResult($query);
  254. break;
  255. default:
  256. break;
  257. }
  258. // Remove hidden items so that they are not displayed in navigation tree
  259. $cfgRelation = PMA_getRelationsParam();
  260. if ($cfgRelation['navwork']) {
  261. $navTable = PMA_Util::backquote($cfgRelation['db'])
  262. . "." . PMA_Util::backquote($cfgRelation['navigationhiding']);
  263. $sqlQuery = "SELECT `item_name` FROM " . $navTable
  264. . " WHERE `username`='" . $cfgRelation['user'] . "'"
  265. . " AND `item_type`='" . substr($type, 0, -1) . "'"
  266. . " AND `db_name`='" . PMA_Util::sqlAddSlashes($db) . "'";
  267. $result = PMA_queryAsControlUser($sqlQuery, false);
  268. if ($result) {
  269. $hiddenItems = array();
  270. while ($row = $GLOBALS['dbi']->fetchArray($result)) {
  271. $hiddenItems[] = $row[0];
  272. }
  273. foreach ($retval as $key => $item) {
  274. if (in_array($item, $hiddenItems)) {
  275. unset($retval[$key]);
  276. }
  277. }
  278. }
  279. $GLOBALS['dbi']->freeResult($result);
  280. }
  281. return $retval;
  282. }
  283. /**
  284. * Returns HTML for show hidden button displayed infront of database node
  285. *
  286. * @return String HTML for show hidden button
  287. */
  288. public function getHtmlForControlButtons()
  289. {
  290. $ret = '';
  291. $db = $this->real_name;
  292. $cfgRelation = PMA_getRelationsParam();
  293. if ($cfgRelation['navwork']) {
  294. $navTable = PMA_Util::backquote($cfgRelation['db'])
  295. . "." . PMA_Util::backquote($cfgRelation['navigationhiding']);
  296. $sqlQuery = "SELECT COUNT(*) FROM " . $navTable
  297. . " WHERE `username`='"
  298. . PMA_Util::sqlAddSlashes($GLOBALS['cfg']['Server']['user']) . "'"
  299. . " AND `db_name`='" . PMA_Util::sqlAddSlashes($db) . "'";
  300. $count = $GLOBALS['dbi']->fetchValue(
  301. $sqlQuery, 0, 0, $GLOBALS['controllink']
  302. );
  303. if ($count > 0) {
  304. $ret = '<span class="dbItemControls">'
  305. . '<a href="navigation.php?'
  306. . PMA_URL_getCommon()
  307. . '&showUnhideDialog=true'
  308. . '&dbName=' . urldecode($db) . '"'
  309. . ' class="showUnhide ajax">'
  310. . PMA_Util::getImage(
  311. 'lightbulb.png', __('Show hidden items')
  312. )
  313. . '</a></span>';
  314. }
  315. }
  316. return $ret;
  317. }
  318. }
  319. ?>