NavigationTree.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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. namespace PhpMyAdmin\Navigation;
  10. use PhpMyAdmin\CheckUserPrivileges;
  11. use PhpMyAdmin\DatabaseInterface;
  12. use PhpMyAdmin\Navigation\Nodes\Node;
  13. use PhpMyAdmin\Navigation\Nodes\NodeDatabase;
  14. use PhpMyAdmin\Navigation\Nodes\NodeTable;
  15. use PhpMyAdmin\Navigation\Nodes\NodeTableContainer;
  16. use PhpMyAdmin\Navigation\Nodes\NodeViewContainer;
  17. use PhpMyAdmin\RecentFavoriteTable;
  18. use PhpMyAdmin\Response;
  19. use PhpMyAdmin\Template;
  20. use PhpMyAdmin\Url;
  21. use PhpMyAdmin\Util;
  22. /**
  23. * Displays a collapsible of database objects in the navigation frame
  24. *
  25. * @package PhpMyAdmin-Navigation
  26. */
  27. class NavigationTree
  28. {
  29. /**
  30. * @var Node Reference to the root node of the tree
  31. */
  32. private $tree;
  33. /**
  34. * @var array The actual paths to all expanded nodes in the tree
  35. * This does not include nodes created after the grouping
  36. * of nodes has been performed
  37. */
  38. private $aPath = [];
  39. /**
  40. * @var array The virtual paths to all expanded nodes in the tree
  41. * This includes nodes created after the grouping of
  42. * nodes has been performed
  43. */
  44. private $vPath = [];
  45. /**
  46. * @var int Position in the list of databases,
  47. * used for pagination
  48. */
  49. private $pos;
  50. /**
  51. * @var array The names of the type of items that are being paginated on
  52. * the second level of the navigation tree. These may be
  53. * tables, views, functions, procedures or events.
  54. */
  55. private $pos2Name = [];
  56. /**
  57. * @var array The positions of nodes in the lists of tables, views,
  58. * routines or events used for pagination
  59. */
  60. private $pos2Value = [];
  61. /**
  62. * @var array The names of the type of items that are being paginated
  63. * on the second level of the navigation tree.
  64. * These may be columns or indexes
  65. */
  66. private $pos3Name = [];
  67. /**
  68. * @var array The positions of nodes in the lists of columns or indexes
  69. * used for pagination
  70. */
  71. private $pos3Value = [];
  72. /**
  73. * @var string The search clause to use in SQL queries for
  74. * fetching databases
  75. * Used by the asynchronous fast filter
  76. */
  77. private $searchClause = '';
  78. /**
  79. * @var string The search clause to use in SQL queries for
  80. * fetching nodes
  81. * Used by the asynchronous fast filter
  82. */
  83. private $searchClause2 = '';
  84. /**
  85. * @var bool Whether a warning was raised for large item groups
  86. * which can affect performance.
  87. */
  88. private $largeGroupWarning = false;
  89. /**
  90. * @var Template
  91. */
  92. private $template;
  93. /**
  94. * @var DatabaseInterface
  95. */
  96. private $dbi;
  97. /**
  98. * NavigationTree constructor.
  99. * @param Template $template Template instance
  100. * @param DatabaseInterface $dbi DatabaseInterface instance
  101. */
  102. public function __construct($template, $dbi)
  103. {
  104. $this->template = $template;
  105. $this->dbi = $dbi;
  106. $checkUserPrivileges = new CheckUserPrivileges($this->dbi);
  107. $checkUserPrivileges->getPrivileges();
  108. // Save the position at which we are in the database list
  109. if (isset($_POST['pos'])) {
  110. $this->pos = (int) $_POST['pos'];
  111. } elseif (isset($_GET['pos'])) {
  112. $this->pos = (int) $_GET['pos'];
  113. }
  114. if (! isset($this->pos)) {
  115. $this->pos = $this->getNavigationDbPos();
  116. }
  117. // Get the active node
  118. if (isset($_REQUEST['aPath'])) {
  119. $this->aPath[0] = $this->parsePath($_REQUEST['aPath']);
  120. $this->pos2Name[0] = $_REQUEST['pos2_name'];
  121. $this->pos2Value[0] = (int) $_REQUEST['pos2_value'];
  122. if (isset($_REQUEST['pos3_name'])) {
  123. $this->pos3Name[0] = $_REQUEST['pos3_name'];
  124. $this->pos3Value[0] = $_REQUEST['pos3_value'];
  125. }
  126. } else {
  127. if (isset($_POST['n0_aPath'])) {
  128. $count = 0;
  129. while (isset($_POST['n' . $count . '_aPath'])) {
  130. $this->aPath[$count] = $this->parsePath(
  131. $_POST['n' . $count . '_aPath']
  132. );
  133. $index = 'n' . $count . '_pos2_';
  134. $this->pos2Name[$count] = $_POST[$index . 'name'];
  135. $this->pos2Value[$count] = $_POST[$index . 'value'];
  136. $index = 'n' . $count . '_pos3_';
  137. if (isset($_POST[$index])) {
  138. $this->pos3Name[$count] = $_POST[$index . 'name'];
  139. $this->pos3Value[$count] = $_POST[$index . 'value'];
  140. }
  141. $count++;
  142. }
  143. }
  144. }
  145. if (isset($_REQUEST['vPath'])) {
  146. $this->vPath[0] = $this->parsePath($_REQUEST['vPath']);
  147. } else {
  148. if (isset($_POST['n0_vPath'])) {
  149. $count = 0;
  150. while (isset($_POST['n' . $count . '_vPath'])) {
  151. $this->vPath[$count] = $this->parsePath(
  152. $_POST['n' . $count . '_vPath']
  153. );
  154. $count++;
  155. }
  156. }
  157. }
  158. if (isset($_REQUEST['searchClause'])) {
  159. $this->searchClause = $_REQUEST['searchClause'];
  160. }
  161. if (isset($_REQUEST['searchClause2'])) {
  162. $this->searchClause2 = $_REQUEST['searchClause2'];
  163. }
  164. // Initialise the tree by creating a root node
  165. $node = NodeFactory::getInstance('NodeDatabaseContainer', 'root');
  166. $this->tree = $node;
  167. if ($GLOBALS['cfg']['NavigationTreeEnableGrouping']
  168. && $GLOBALS['cfg']['ShowDatabasesNavigationAsTree']
  169. ) {
  170. $this->tree->separator = $GLOBALS['cfg']['NavigationTreeDbSeparator'];
  171. $this->tree->separatorDepth = 10000;
  172. }
  173. }
  174. /**
  175. * Returns the database position for the page selector
  176. *
  177. * @return int
  178. */
  179. private function getNavigationDbPos()
  180. {
  181. $retval = 0;
  182. if (strlen($GLOBALS['db']) == 0) {
  183. return $retval;
  184. }
  185. /*
  186. * @todo describe a scenario where this code is executed
  187. */
  188. if (! $GLOBALS['cfg']['Server']['DisableIS']) {
  189. $dbSeparator = $this->dbi->escapeString(
  190. $GLOBALS['cfg']['NavigationTreeDbSeparator']
  191. );
  192. $query = "SELECT (COUNT(DB_first_level) DIV %d) * %d ";
  193. $query .= "from ( ";
  194. $query .= " SELECT distinct SUBSTRING_INDEX(SCHEMA_NAME, ";
  195. $query .= " '%s', 1) ";
  196. $query .= " DB_first_level ";
  197. $query .= " FROM INFORMATION_SCHEMA.SCHEMATA ";
  198. $query .= " WHERE `SCHEMA_NAME` < '%s' ";
  199. $query .= ") t ";
  200. $retval = $this->dbi->fetchValue(
  201. sprintf(
  202. $query,
  203. (int) $GLOBALS['cfg']['FirstLevelNavigationItems'],
  204. (int) $GLOBALS['cfg']['FirstLevelNavigationItems'],
  205. $dbSeparator,
  206. $this->dbi->escapeString($GLOBALS['db'])
  207. )
  208. );
  209. return $retval;
  210. }
  211. $prefixMap = [];
  212. if ($GLOBALS['dbs_to_test'] === false) {
  213. $handle = $this->dbi->tryQuery("SHOW DATABASES");
  214. if ($handle !== false) {
  215. while ($arr = $this->dbi->fetchArray($handle)) {
  216. if (strcasecmp($arr[0], $GLOBALS['db']) >= 0) {
  217. break;
  218. }
  219. $prefix = strstr(
  220. $arr[0],
  221. $GLOBALS['cfg']['NavigationTreeDbSeparator'],
  222. true
  223. );
  224. if ($prefix === false) {
  225. $prefix = $arr[0];
  226. }
  227. $prefixMap[$prefix] = 1;
  228. }
  229. }
  230. } else {
  231. $databases = [];
  232. foreach ($GLOBALS['dbs_to_test'] as $db) {
  233. $query = "SHOW DATABASES LIKE '" . $db . "'";
  234. $handle = $this->dbi->tryQuery($query);
  235. if ($handle === false) {
  236. continue;
  237. }
  238. while ($arr = $this->dbi->fetchArray($handle)) {
  239. $databases[] = $arr[0];
  240. }
  241. }
  242. sort($databases);
  243. foreach ($databases as $database) {
  244. if (strcasecmp($database, $GLOBALS['db']) >= 0) {
  245. break;
  246. }
  247. $prefix = strstr(
  248. $database,
  249. $GLOBALS['cfg']['NavigationTreeDbSeparator'],
  250. true
  251. );
  252. if ($prefix === false) {
  253. $prefix = $database;
  254. }
  255. $prefixMap[$prefix] = 1;
  256. }
  257. }
  258. $navItems = (int) $GLOBALS['cfg']['FirstLevelNavigationItems'];
  259. $retval = (int) floor(count($prefixMap) / $navItems) * $navItems;
  260. return $retval;
  261. }
  262. /**
  263. * Converts an encoded path to a node in string format to an array
  264. *
  265. * @param string $string The path to parse
  266. *
  267. * @return array
  268. */
  269. private function parsePath($string)
  270. {
  271. $path = explode('.', $string);
  272. foreach ($path as $key => $value) {
  273. $path[$key] = base64_decode($value);
  274. }
  275. return $path;
  276. }
  277. /**
  278. * Generates the tree structure so that it can be rendered later
  279. *
  280. * @return Node|false The active node or false in case of failure
  281. */
  282. private function buildPath()
  283. {
  284. $retval = $this->tree;
  285. // Add all databases unconditionally
  286. $data = $this->tree->getData(
  287. 'databases',
  288. $this->pos,
  289. $this->searchClause
  290. );
  291. $hiddenCounts = $this->tree->getNavigationHidingData();
  292. foreach ($data as $db) {
  293. $node = NodeFactory::getInstance('NodeDatabase', $db);
  294. if (isset($hiddenCounts[$db])) {
  295. $node->setHiddenCount($hiddenCounts[$db]);
  296. }
  297. $this->tree->addChild($node);
  298. }
  299. // Whether build other parts of the tree depends
  300. // on whether we have any paths in $this->_aPath
  301. foreach ($this->aPath as $key => $path) {
  302. $retval = $this->buildPathPart(
  303. $path,
  304. $this->pos2Name[$key],
  305. $this->pos2Value[$key],
  306. isset($this->pos3Name[$key]) ? $this->pos3Name[$key] : '',
  307. isset($this->pos3Value[$key]) ? $this->pos3Value[$key] : ''
  308. );
  309. }
  310. return $retval;
  311. }
  312. /**
  313. * Builds a branch of the tree
  314. *
  315. * @param array $path A paths pointing to the branch
  316. * of the tree that needs to be built
  317. * @param string $type2 The type of item being paginated on
  318. * the second level of the tree
  319. * @param int $pos2 The position for the pagination of
  320. * the branch at the second level of the tree
  321. * @param string $type3 The type of item being paginated on
  322. * the third level of the tree
  323. * @param int $pos3 The position for the pagination of
  324. * the branch at the third level of the tree
  325. *
  326. * @return Node|bool The active node or false in case of failure, true if the path contains <= 1 items
  327. */
  328. private function buildPathPart(array $path, $type2, $pos2, $type3, $pos3)
  329. {
  330. if (empty($pos2)) {
  331. $pos2 = 0;
  332. }
  333. if (empty($pos3)) {
  334. $pos3 = 0;
  335. }
  336. $retval = true;
  337. if (count($path) <= 1) {
  338. return $retval;
  339. }
  340. array_shift($path); // remove 'root'
  341. /** @var NodeDatabase $db */
  342. $db = $this->tree->getChild($path[0]);
  343. $retval = $db;
  344. if ($db === false) {
  345. return false;
  346. }
  347. $containers = $this->addDbContainers($db, $type2, $pos2);
  348. array_shift($path); // remove db
  349. if ((count($path) <= 0 || ! array_key_exists($path[0], $containers))
  350. && count($containers) != 1
  351. ) {
  352. return $retval;
  353. }
  354. if (count($containers) === 1) {
  355. $container = array_shift($containers);
  356. } else {
  357. $container = $db->getChild($path[0], true);
  358. if ($container === false) {
  359. return false;
  360. }
  361. }
  362. $retval = $container;
  363. if (count($container->children) <= 1) {
  364. $dbData = $db->getData(
  365. $container->realName,
  366. $pos2,
  367. $this->searchClause2
  368. );
  369. foreach ($dbData as $item) {
  370. switch ($container->realName) {
  371. case 'events':
  372. $node = NodeFactory::getInstance(
  373. 'NodeEvent',
  374. $item
  375. );
  376. break;
  377. case 'functions':
  378. $node = NodeFactory::getInstance(
  379. 'NodeFunction',
  380. $item
  381. );
  382. break;
  383. case 'procedures':
  384. $node = NodeFactory::getInstance(
  385. 'NodeProcedure',
  386. $item
  387. );
  388. break;
  389. case 'tables':
  390. $node = NodeFactory::getInstance(
  391. 'NodeTable',
  392. $item
  393. );
  394. break;
  395. case 'views':
  396. $node = NodeFactory::getInstance(
  397. 'NodeView',
  398. $item
  399. );
  400. break;
  401. default:
  402. break;
  403. }
  404. if (isset($node)) {
  405. if ($type2 == $container->realName) {
  406. $node->pos2 = $pos2;
  407. }
  408. $container->addChild($node);
  409. }
  410. }
  411. }
  412. if (count($path) > 1 && $path[0] != 'tables') {
  413. $retval = false;
  414. return $retval;
  415. }
  416. array_shift($path); // remove container
  417. if (count($path) <= 0) {
  418. return $retval;
  419. }
  420. /** @var NodeTable $table */
  421. $table = $container->getChild($path[0], true);
  422. if ($table === false) {
  423. if (! $db->getPresence('tables', $path[0])) {
  424. return false;
  425. }
  426. $node = NodeFactory::getInstance(
  427. 'NodeTable',
  428. $path[0]
  429. );
  430. if ($type2 == $container->realName) {
  431. $node->pos2 = $pos2;
  432. }
  433. $container->addChild($node);
  434. $table = $container->getChild($path[0], true);
  435. }
  436. $retval = $table;
  437. $containers = $this->addTableContainers(
  438. $table,
  439. $pos2,
  440. $type3,
  441. $pos3
  442. );
  443. array_shift($path); // remove table
  444. if (count($path) <= 0
  445. || ! array_key_exists($path[0], $containers)
  446. ) {
  447. return $retval;
  448. }
  449. $container = $table->getChild($path[0], true);
  450. $retval = $container;
  451. $tableData = $table->getData(
  452. $container->realName,
  453. $pos3
  454. );
  455. foreach ($tableData as $item) {
  456. switch ($container->realName) {
  457. case 'indexes':
  458. $node = NodeFactory::getInstance(
  459. 'NodeIndex',
  460. $item
  461. );
  462. break;
  463. case 'columns':
  464. $node = NodeFactory::getInstance(
  465. 'NodeColumn',
  466. $item
  467. );
  468. break;
  469. case 'triggers':
  470. $node = NodeFactory::getInstance(
  471. 'NodeTrigger',
  472. $item
  473. );
  474. break;
  475. default:
  476. break;
  477. }
  478. if (isset($node)) {
  479. $node->pos2 = $container->parent->pos2;
  480. if ($type3 == $container->realName) {
  481. $node->pos3 = $pos3;
  482. }
  483. $container->addChild($node);
  484. }
  485. }
  486. return $retval;
  487. }
  488. /**
  489. * Adds containers to a node that is a table
  490. *
  491. * References to existing children are returned
  492. * if this function is called twice on the same node
  493. *
  494. * @param NodeTable $table The table node, new containers will be
  495. * attached to this node
  496. * @param int $pos2 The position for the pagination of
  497. * the branch at the second level of the tree
  498. * @param string $type3 The type of item being paginated on
  499. * the third level of the tree
  500. * @param int $pos3 The position for the pagination of
  501. * the branch at the third level of the tree
  502. *
  503. * @return array An array of new nodes
  504. */
  505. private function addTableContainers($table, $pos2, $type3, $pos3)
  506. {
  507. $retval = [];
  508. if ($table->hasChildren(true) == 0) {
  509. if ($table->getPresence('columns')) {
  510. $retval['columns'] = NodeFactory::getInstance(
  511. 'NodeColumnContainer'
  512. );
  513. }
  514. if ($table->getPresence('indexes')) {
  515. $retval['indexes'] = NodeFactory::getInstance(
  516. 'NodeIndexContainer'
  517. );
  518. }
  519. if ($table->getPresence('triggers')) {
  520. $retval['triggers'] = NodeFactory::getInstance(
  521. 'NodeTriggerContainer'
  522. );
  523. }
  524. // Add all new Nodes to the tree
  525. foreach ($retval as $node) {
  526. $node->pos2 = $pos2;
  527. if ($type3 == $node->realName) {
  528. $node->pos3 = $pos3;
  529. }
  530. $table->addChild($node);
  531. }
  532. } else {
  533. foreach ($table->children as $node) {
  534. if ($type3 == $node->realName) {
  535. $node->pos3 = $pos3;
  536. }
  537. $retval[$node->realName] = $node;
  538. }
  539. }
  540. return $retval;
  541. }
  542. /**
  543. * Adds containers to a node that is a database
  544. *
  545. * References to existing children are returned
  546. * if this function is called twice on the same node
  547. *
  548. * @param NodeDatabase $db The database node, new containers will be
  549. * attached to this node
  550. * @param string $type The type of item being paginated on
  551. * the second level of the tree
  552. * @param int $pos2 The position for the pagination of
  553. * the branch at the second level of the tree
  554. *
  555. * @return array An array of new nodes
  556. */
  557. private function addDbContainers($db, $type, $pos2)
  558. {
  559. // Get items to hide
  560. $hidden = $db->getHiddenItems('group');
  561. if (! $GLOBALS['cfg']['NavigationTreeShowTables']
  562. && ! in_array('tables', $hidden)
  563. ) {
  564. $hidden[] = 'tables';
  565. }
  566. if (! $GLOBALS['cfg']['NavigationTreeShowViews']
  567. && ! in_array('views', $hidden)
  568. ) {
  569. $hidden[] = 'views';
  570. }
  571. if (! $GLOBALS['cfg']['NavigationTreeShowFunctions']
  572. && ! in_array('functions', $hidden)
  573. ) {
  574. $hidden[] = 'functions';
  575. }
  576. if (! $GLOBALS['cfg']['NavigationTreeShowProcedures']
  577. && ! in_array('procedures', $hidden)
  578. ) {
  579. $hidden[] = 'procedures';
  580. }
  581. if (! $GLOBALS['cfg']['NavigationTreeShowEvents']
  582. && ! in_array('events', $hidden)
  583. ) {
  584. $hidden[] = 'events';
  585. }
  586. $retval = [];
  587. if ($db->hasChildren(true) == 0) {
  588. if (! in_array('tables', $hidden) && $db->getPresence('tables')) {
  589. $retval['tables'] = NodeFactory::getInstance(
  590. 'NodeTableContainer'
  591. );
  592. }
  593. if (! in_array('views', $hidden) && $db->getPresence('views')) {
  594. $retval['views'] = NodeFactory::getInstance(
  595. 'NodeViewContainer'
  596. );
  597. }
  598. if (! in_array('functions', $hidden) && $db->getPresence('functions')) {
  599. $retval['functions'] = NodeFactory::getInstance(
  600. 'NodeFunctionContainer'
  601. );
  602. }
  603. if (! in_array('procedures', $hidden) && $db->getPresence('procedures')) {
  604. $retval['procedures'] = NodeFactory::getInstance(
  605. 'NodeProcedureContainer'
  606. );
  607. }
  608. if (! in_array('events', $hidden) && $db->getPresence('events')) {
  609. $retval['events'] = NodeFactory::getInstance(
  610. 'NodeEventContainer'
  611. );
  612. }
  613. // Add all new Nodes to the tree
  614. foreach ($retval as $node) {
  615. if ($type == $node->realName) {
  616. $node->pos2 = $pos2;
  617. }
  618. $db->addChild($node);
  619. }
  620. } else {
  621. foreach ($db->children as $node) {
  622. if ($type == $node->realName) {
  623. $node->pos2 = $pos2;
  624. }
  625. $retval[$node->realName] = $node;
  626. }
  627. }
  628. return $retval;
  629. }
  630. /**
  631. * Recursively groups tree nodes given a separator
  632. *
  633. * @param mixed $node The node to group or null
  634. * to group the whole tree. If
  635. * passed as an argument, $node
  636. * must be of type CONTAINER
  637. *
  638. * @return void
  639. */
  640. public function groupTree($node = null)
  641. {
  642. if (! isset($node)) {
  643. $node = $this->tree;
  644. }
  645. $this->groupNode($node);
  646. foreach ($node->children as $child) {
  647. $this->groupTree($child);
  648. }
  649. }
  650. /**
  651. * Recursively groups tree nodes given a separator
  652. *
  653. * @param Node $node The node to group
  654. *
  655. * @return void
  656. */
  657. public function groupNode($node)
  658. {
  659. if ($node->type != Node::CONTAINER
  660. || ! $GLOBALS['cfg']['NavigationTreeEnableExpansion']
  661. ) {
  662. return;
  663. }
  664. $separators = [];
  665. if (is_array($node->separator)) {
  666. $separators = $node->separator;
  667. } else {
  668. if (strlen($node->separator)) {
  669. $separators[] = $node->separator;
  670. }
  671. }
  672. $prefixes = [];
  673. if ($node->separatorDepth > 0) {
  674. foreach ($node->children as $child) {
  675. $prefixPos = false;
  676. foreach ($separators as $separator) {
  677. $sepPos = mb_strpos((string) $child->name, $separator);
  678. if ($sepPos != false
  679. && $sepPos != mb_strlen($child->name)
  680. && $sepPos != 0
  681. && ($prefixPos === false || $sepPos < $prefixPos)
  682. ) {
  683. $prefixPos = $sepPos;
  684. }
  685. }
  686. if ($prefixPos !== false) {
  687. $prefix = mb_substr($child->name, 0, $prefixPos);
  688. if (! isset($prefixes[$prefix])) {
  689. $prefixes[$prefix] = 1;
  690. } else {
  691. $prefixes[$prefix]++;
  692. }
  693. }
  694. //Bug #4375: Check if prefix is the name of a DB, to create a group.
  695. foreach ($node->children as $otherChild) {
  696. if (array_key_exists($otherChild->name, $prefixes)) {
  697. $prefixes[$otherChild->name]++;
  698. }
  699. }
  700. }
  701. //Check if prefix is the name of a DB, to create a group.
  702. foreach ($node->children as $child) {
  703. if (array_key_exists($child->name, $prefixes)) {
  704. $prefixes[$child->name]++;
  705. }
  706. }
  707. }
  708. // It is not a group if it has only one item
  709. foreach ($prefixes as $key => $value) {
  710. if ($value == 1) {
  711. unset($prefixes[$key]);
  712. }
  713. }
  714. // rfe #1634 Don't group if there's only one group and no other items
  715. if (count($prefixes) === 1) {
  716. $keys = array_keys($prefixes);
  717. $key = $keys[0];
  718. if ($prefixes[$key] == count($node->children) - 1) {
  719. unset($prefixes[$key]);
  720. }
  721. }
  722. if (count($prefixes)) {
  723. /** @var Node[] $groups */
  724. $groups = [];
  725. foreach ($prefixes as $key => $value) {
  726. // warn about large groups
  727. if ($value > 500 && ! $this->largeGroupWarning) {
  728. trigger_error(
  729. __(
  730. 'There are large item groups in navigation panel which '
  731. . 'may affect the performance. Consider disabling item '
  732. . 'grouping in the navigation panel.'
  733. ),
  734. E_USER_WARNING
  735. );
  736. $this->largeGroupWarning = true;
  737. }
  738. $groups[$key] = new Node(
  739. htmlspecialchars((string) $key),
  740. Node::CONTAINER,
  741. true
  742. );
  743. $groups[$key]->separator = $node->separator;
  744. $groups[$key]->separatorDepth = $node->separatorDepth - 1;
  745. $groups[$key]->icon = Util::getImage(
  746. 'b_group',
  747. __('Groups')
  748. );
  749. $groups[$key]->pos2 = $node->pos2;
  750. $groups[$key]->pos3 = $node->pos3;
  751. if ($node instanceof NodeTableContainer
  752. || $node instanceof NodeViewContainer
  753. ) {
  754. $tblGroup = '&amp;tbl_group=' . urlencode((string) $key);
  755. $groups[$key]->links = [
  756. 'text' => $node->links['text'] . $tblGroup,
  757. 'icon' => $node->links['icon'] . $tblGroup,
  758. ];
  759. }
  760. $node->addChild($groups[$key]);
  761. foreach ($separators as $separator) {
  762. $separatorLength = strlen($separator);
  763. // FIXME: this could be more efficient
  764. foreach ($node->children as $child) {
  765. $keySeparatorLength = mb_strlen((string) $key) + $separatorLength;
  766. $nameSubstring = mb_substr(
  767. (string) $child->name,
  768. 0,
  769. $keySeparatorLength
  770. );
  771. if (($nameSubstring != $key . $separator
  772. && $child->name != $key)
  773. || $child->type != Node::OBJECT
  774. ) {
  775. continue;
  776. }
  777. $class = get_class($child);
  778. $className = substr($class, strrpos($class, '\\') + 1);
  779. unset($class);
  780. $newChild = NodeFactory::getInstance(
  781. $className,
  782. mb_substr(
  783. $child->name,
  784. $keySeparatorLength
  785. )
  786. );
  787. if ($newChild instanceof NodeDatabase
  788. && $child->getHiddenCount() > 0
  789. ) {
  790. $newChild->setHiddenCount($child->getHiddenCount());
  791. }
  792. $newChild->realName = $child->realName;
  793. $newChild->icon = $child->icon;
  794. $newChild->links = $child->links;
  795. $newChild->pos2 = $child->pos2;
  796. $newChild->pos3 = $child->pos3;
  797. $groups[$key]->addChild($newChild);
  798. foreach ($child->children as $elm) {
  799. $newChild->addChild($elm);
  800. }
  801. $node->removeChild($child->name);
  802. }
  803. }
  804. }
  805. foreach ($prefixes as $key => $value) {
  806. $this->groupNode($groups[$key]);
  807. $groups[$key]->classes = "navGroup";
  808. }
  809. }
  810. }
  811. /**
  812. * Renders a state of the tree, used in light mode when
  813. * either JavaScript and/or Ajax are disabled
  814. *
  815. * @return string HTML code for the navigation tree
  816. */
  817. public function renderState()
  818. {
  819. $this->buildPath();
  820. $quickWarp = $this->quickWarp();
  821. $fastFilter = $this->fastFilterHtml($this->tree);
  822. $controls = '';
  823. if ($GLOBALS['cfg']['NavigationTreeEnableExpansion']) {
  824. $controls = $this->controls();
  825. }
  826. $pageSelector = $this->getPageSelector($this->tree);
  827. $this->groupTree();
  828. $children = $this->tree->children;
  829. usort($children, [
  830. NavigationTree::class,
  831. 'sortNode',
  832. ]);
  833. $this->setVisibility();
  834. $nodes = '';
  835. for ($i = 0, $nbChildren = count($children); $i < $nbChildren; $i++) {
  836. if ($i == 0) {
  837. $nodes .= $this->renderNode($children[0], true, 'first');
  838. } else {
  839. if ($i + 1 != $nbChildren) {
  840. $nodes .= $this->renderNode($children[$i], true);
  841. } else {
  842. $nodes .= $this->renderNode($children[$i], true, 'last');
  843. }
  844. }
  845. }
  846. return $this->template->render('navigation/tree/state', [
  847. 'quick_warp' => $quickWarp,
  848. 'fast_filter' => $fastFilter,
  849. 'controls' => $controls,
  850. 'page_selector' => $pageSelector,
  851. 'nodes' => $nodes,
  852. ]);
  853. }
  854. /**
  855. * Renders a part of the tree, used for Ajax requests in light mode
  856. *
  857. * @return string|false HTML code for the navigation tree
  858. */
  859. public function renderPath()
  860. {
  861. $node = $this->buildPath();
  862. if ($node !== false) {
  863. $this->groupTree();
  864. $listContent = $this->fastFilterHtml($node);
  865. $listContent .= $this->getPageSelector($node);
  866. $children = $node->children;
  867. usort($children, [
  868. NavigationTree::class,
  869. 'sortNode',
  870. ]);
  871. for ($i = 0, $nbChildren = count($children); $i < $nbChildren; $i++) {
  872. if ($i + 1 != $nbChildren) {
  873. $listContent .= $this->renderNode($children[$i], true);
  874. } else {
  875. $listContent .= $this->renderNode($children[$i], true, 'last');
  876. }
  877. }
  878. if (! $GLOBALS['cfg']['ShowDatabasesNavigationAsTree']) {
  879. $parents = $node->parents(true);
  880. $parentName = $parents[0]->realName;
  881. }
  882. }
  883. if (! empty($this->searchClause) || ! empty($this->searchClause2)) {
  884. $results = 0;
  885. if (! empty($this->searchClause2)) {
  886. if (is_object($node->realParent())) {
  887. $results = $node->realParent()
  888. ->getPresence(
  889. $node->realName,
  890. $this->searchClause2
  891. );
  892. }
  893. } else {
  894. $results = $this->tree->getPresence(
  895. 'databases',
  896. $this->searchClause
  897. );
  898. }
  899. $results = sprintf(
  900. _ngettext(
  901. '%s result found',
  902. '%s results found',
  903. $results
  904. ),
  905. $results
  906. );
  907. Response::getInstance()
  908. ->addJSON(
  909. 'results',
  910. $results
  911. );
  912. }
  913. if ($node !== false) {
  914. return $this->template->render('navigation/tree/path', [
  915. 'has_search_results' => ! empty($this->searchClause) || ! empty($this->searchClause2),
  916. 'list_content' => $listContent ?? '',
  917. 'is_tree' => $GLOBALS['cfg']['ShowDatabasesNavigationAsTree'],
  918. 'parent_name' => $parentName ?? '',
  919. ]);
  920. }
  921. return false;
  922. }
  923. /**
  924. * Renders the parameters that are required on the client
  925. * side to know which page(s) we will be requesting data from
  926. *
  927. * @param Node $node The node to create the pagination parameters for
  928. *
  929. * @return string
  930. */
  931. private function getPaginationParamsHtml($node)
  932. {
  933. $retval = '';
  934. $paths = $node->getPaths();
  935. if (isset($paths['aPath_clean'][2])) {
  936. $retval .= "<span class='hide pos2_name'>";
  937. $retval .= $paths['aPath_clean'][2];
  938. $retval .= "</span>";
  939. $retval .= "<span class='hide pos2_value'>";
  940. $retval .= htmlspecialchars((string) $node->pos2);
  941. $retval .= "</span>";
  942. }
  943. if (isset($paths['aPath_clean'][4])) {
  944. $retval .= "<span class='hide pos3_name'>";
  945. $retval .= $paths['aPath_clean'][4];
  946. $retval .= "</span>";
  947. $retval .= "<span class='hide pos3_value'>";
  948. $retval .= htmlspecialchars((string) $node->pos3);
  949. $retval .= "</span>";
  950. }
  951. return $retval;
  952. }
  953. /**
  954. * Finds whether given tree matches this tree.
  955. *
  956. * @param array $tree Tree to check
  957. * @param array $paths Paths to check
  958. *
  959. * @return boolean
  960. */
  961. private function findTreeMatch(array $tree, array $paths)
  962. {
  963. $match = false;
  964. foreach ($tree as $path) {
  965. $match = true;
  966. foreach ($paths as $key => $part) {
  967. if (! isset($path[$key]) || $part != $path[$key]) {
  968. $match = false;
  969. break;
  970. }
  971. }
  972. if ($match) {
  973. break;
  974. }
  975. }
  976. return $match;
  977. }
  978. /**
  979. * Renders a single node or a branch of the tree
  980. *
  981. * @param Node $node The node to render
  982. * @param bool $recursive Bool: Whether to render a single node or a branch
  983. * @param string $class An additional class for the list item
  984. *
  985. * @return string HTML code for the tree node or branch
  986. */
  987. private function renderNode($node, $recursive, $class = '')
  988. {
  989. $retval = '';
  990. $paths = $node->getPaths();
  991. if ($node->hasSiblings()
  992. || $node->realParent() === false
  993. ) {
  994. $response = Response::getInstance();
  995. if ($node->type == Node::CONTAINER
  996. && count($node->children) === 0
  997. && ! $response->isAjax()
  998. ) {
  999. return '';
  1000. }
  1001. $retval .= '<li class="' . trim($class . ' ' . $node->classes) . '">';
  1002. $sterile = [
  1003. 'events',
  1004. 'triggers',
  1005. 'functions',
  1006. 'procedures',
  1007. 'views',
  1008. 'columns',
  1009. 'indexes',
  1010. ];
  1011. $parentName = '';
  1012. $parents = $node->parents(false, true);
  1013. if (count($parents)) {
  1014. $parentName = $parents[0]->realName;
  1015. }
  1016. // if node name itself is in sterile, then allow
  1017. if ($node->isGroup
  1018. || (! in_array($parentName, $sterile) && ! $node->isNew)
  1019. || (in_array($node->realName, $sterile) && ! empty($node->children))
  1020. ) {
  1021. $retval .= "<div class='block'>";
  1022. $iClass = '';
  1023. if ($class == 'first') {
  1024. $iClass = " class='first'";
  1025. }
  1026. $retval .= "<i$iClass></i>";
  1027. if (strpos($class, 'last') === false) {
  1028. $retval .= "<b></b>";
  1029. }
  1030. $match = $this->findTreeMatch(
  1031. $this->vPath,
  1032. $paths['vPath_clean']
  1033. );
  1034. $retval .= '<a class="' . $node->getCssClasses($match) . '"';
  1035. $retval .= " href='#'>";
  1036. $retval .= "<span class='hide aPath'>";
  1037. $retval .= $paths['aPath'];
  1038. $retval .= "</span>";
  1039. $retval .= "<span class='hide vPath'>";
  1040. $retval .= $paths['vPath'];
  1041. $retval .= "</span>";
  1042. $retval .= "<span class='hide pos'>";
  1043. $retval .= $this->pos;
  1044. $retval .= "</span>";
  1045. $retval .= $this->getPaginationParamsHtml($node);
  1046. if ($GLOBALS['cfg']['ShowDatabasesNavigationAsTree']
  1047. || $parentName != 'root'
  1048. ) {
  1049. $retval .= $node->getIcon($match);
  1050. }
  1051. $retval .= "</a>";
  1052. $retval .= "</div>";
  1053. } else {
  1054. $retval .= "<div class='block'>";
  1055. $iClass = '';
  1056. if ($class == 'first') {
  1057. $iClass = " class='first'";
  1058. }
  1059. $retval .= "<i$iClass></i>";
  1060. $retval .= $this->getPaginationParamsHtml($node);
  1061. $retval .= "</div>";
  1062. }
  1063. $linkClass = '';
  1064. $haveAjax = [
  1065. 'functions',
  1066. 'procedures',
  1067. 'events',
  1068. 'triggers',
  1069. 'indexes',
  1070. ];
  1071. $parent = $node->parents(false, true);
  1072. $isNewView = $parent[0]->realName == 'views' && $node->isNew === true;
  1073. if ($parent[0]->type == Node::CONTAINER
  1074. && (in_array($parent[0]->realName, $haveAjax) || $isNewView)
  1075. ) {
  1076. $linkClass = ' ajax';
  1077. }
  1078. if ($node->type == Node::CONTAINER) {
  1079. $retval .= "<i>";
  1080. }
  1081. $divClass = '';
  1082. if (isset($node->links['icon']) && ! empty($node->links['icon'])) {
  1083. $iconLinks = $node->links['icon'];
  1084. $icons = $node->icon;
  1085. if (! is_array($iconLinks)) {
  1086. $iconLinks = [$iconLinks];
  1087. $icons = [$icons];
  1088. }
  1089. if (count($icons) > 1) {
  1090. $divClass = 'double';
  1091. }
  1092. }
  1093. $retval .= "<div class='block " . $divClass . "'>";
  1094. if (isset($node->links['icon']) && ! empty($node->links['icon'])) {
  1095. $args = [];
  1096. foreach ($node->parents(true) as $parent) {
  1097. $args[] = urlencode($parent->realName);
  1098. }
  1099. foreach ($icons as $key => $icon) {
  1100. $link = vsprintf($iconLinks[$key], $args);
  1101. if ($linkClass != '') {
  1102. $retval .= "<a class='$linkClass' href='$link'>";
  1103. $retval .= "{$icon}</a>";
  1104. } else {
  1105. $retval .= "<a href='$link'>{$icon}</a>";
  1106. }
  1107. }
  1108. } else {
  1109. $retval .= "<u>{$node->icon}</u>";
  1110. }
  1111. $retval .= "</div>";
  1112. if (isset($node->links['text'])) {
  1113. $args = [];
  1114. foreach ($node->parents(true) as $parent) {
  1115. $args[] = urlencode($parent->realName);
  1116. }
  1117. $link = vsprintf($node->links['text'], $args);
  1118. $title = isset($node->links['title']) ? $node->links['title'] : $node->title ?? '';
  1119. if ($node->type == Node::CONTAINER) {
  1120. $retval .= "&nbsp;<a class='hover_show_full' href='$link'>";
  1121. $retval .= htmlspecialchars($node->name);
  1122. $retval .= "</a>";
  1123. } else {
  1124. $retval .= "<a class='hover_show_full$linkClass' href='$link'";
  1125. $retval .= " title='$title'>";
  1126. $retval .= htmlspecialchars($node->displayName ?? $node->realName);
  1127. $retval .= "</a>";
  1128. }
  1129. } else {
  1130. $retval .= "&nbsp;{$node->name}";
  1131. }
  1132. $retval .= $node->getHtmlForControlButtons();
  1133. if ($node->type == Node::CONTAINER) {
  1134. $retval .= "</i>";
  1135. }
  1136. $retval .= '<div class="clearfloat"></div>';
  1137. $wrap = true;
  1138. } else {
  1139. $node->visible = true;
  1140. $wrap = false;
  1141. $retval .= $this->getPaginationParamsHtml($node);
  1142. }
  1143. if ($recursive) {
  1144. $hide = '';
  1145. if (! $node->visible) {
  1146. $hide = " style='display: none;'";
  1147. }
  1148. $children = $node->children;
  1149. usort(
  1150. $children,
  1151. [
  1152. NavigationTree::class,
  1153. 'sortNode',
  1154. ]
  1155. );
  1156. $buffer = '';
  1157. $extraClass = '';
  1158. for ($i = 0, $nbChildren = count($children); $i < $nbChildren; $i++) {
  1159. if ($i + 1 == $nbChildren) {
  1160. $extraClass = ' last';
  1161. }
  1162. $buffer .= $this->renderNode(
  1163. $children[$i],
  1164. true,
  1165. $children[$i]->classes . $extraClass
  1166. );
  1167. }
  1168. if (! empty($buffer)) {
  1169. if ($wrap) {
  1170. $retval .= "<div$hide class='list_container'><ul>";
  1171. }
  1172. $retval .= $this->fastFilterHtml($node);
  1173. $retval .= $this->getPageSelector($node);
  1174. $retval .= $buffer;
  1175. if ($wrap) {
  1176. $retval .= "</ul></div>";
  1177. }
  1178. }
  1179. }
  1180. if ($node->hasSiblings()) {
  1181. $retval .= "</li>";
  1182. }
  1183. return $retval;
  1184. }
  1185. /**
  1186. * Renders a database select box like the pre-4.0 navigation panel
  1187. *
  1188. * @return string HTML code
  1189. */
  1190. public function renderDbSelect()
  1191. {
  1192. $this->buildPath();
  1193. $quickWarp = $this->quickWarp();
  1194. $this->tree->isGroup = false;
  1195. // Provide for pagination in database select
  1196. $listNavigator = Util::getListNavigator(
  1197. $this->tree->getPresence('databases', ''),
  1198. $this->pos,
  1199. ['server' => $GLOBALS['server']],
  1200. 'navigation.php',
  1201. 'frame_navigation',
  1202. $GLOBALS['cfg']['FirstLevelNavigationItems'],
  1203. 'pos',
  1204. ['dbselector']
  1205. );
  1206. $children = $this->tree->children;
  1207. $selected = $GLOBALS['db'];
  1208. $options = '';
  1209. foreach ($children as $node) {
  1210. if ($node->isNew) {
  1211. continue;
  1212. }
  1213. $paths = $node->getPaths();
  1214. if (isset($node->links['text'])) {
  1215. $title = isset($node->links['title']) ? '' : $node->links['title'];
  1216. $options .= '<option value="'
  1217. . htmlspecialchars($node->realName) . '"'
  1218. . ' title="' . htmlspecialchars($title) . '"'
  1219. . ' apath="' . $paths['aPath'] . '"'
  1220. . ' vpath="' . $paths['vPath'] . '"'
  1221. . ' pos="' . $this->pos . '"';
  1222. if ($node->realName == $selected) {
  1223. $options .= ' selected';
  1224. }
  1225. $options .= '>' . htmlspecialchars($node->realName);
  1226. $options .= '</option>';
  1227. }
  1228. }
  1229. $children = $this->tree->children;
  1230. usort($children, [
  1231. NavigationTree::class,
  1232. 'sortNode',
  1233. ]);
  1234. $this->setVisibility();
  1235. $nodes = '';
  1236. for ($i = 0, $nbChildren = count($children); $i < $nbChildren; $i++) {
  1237. if ($i == 0) {
  1238. $nodes .= $this->renderNode($children[0], true, 'first');
  1239. } else {
  1240. if ($i + 1 != $nbChildren) {
  1241. $nodes .= $this->renderNode($children[$i], true);
  1242. } else {
  1243. $nodes .= $this->renderNode($children[$i], true, 'last');
  1244. }
  1245. }
  1246. }
  1247. return $this->template->render('navigation/tree/database_select', [
  1248. 'quick_warp' => $quickWarp,
  1249. 'list_navigator' => $listNavigator,
  1250. 'server' => $GLOBALS['server'],
  1251. 'options' => $options,
  1252. 'nodes' => $nodes,
  1253. ]);
  1254. }
  1255. /**
  1256. * Makes some nodes visible based on the which node is active
  1257. *
  1258. * @return void
  1259. */
  1260. private function setVisibility()
  1261. {
  1262. foreach ($this->vPath as $path) {
  1263. $node = $this->tree;
  1264. foreach ($path as $value) {
  1265. $child = $node->getChild($value);
  1266. if ($child !== false) {
  1267. $child->visible = true;
  1268. $node = $child;
  1269. }
  1270. }
  1271. }
  1272. }
  1273. /**
  1274. * Generates the HTML code for displaying the fast filter for tables
  1275. *
  1276. * @param Node $node The node for which to generate the fast filter html
  1277. *
  1278. * @return string LI element used for the fast filter
  1279. */
  1280. private function fastFilterHtml($node)
  1281. {
  1282. $retval = '';
  1283. $filterDbMin
  1284. = (int) $GLOBALS['cfg']['NavigationTreeDisplayDbFilterMinimum'];
  1285. $filterItemMin
  1286. = (int) $GLOBALS['cfg']['NavigationTreeDisplayItemFilterMinimum'];
  1287. if ($node === $this->tree
  1288. && $this->tree->getPresence() >= $filterDbMin
  1289. ) {
  1290. $urlParams = [
  1291. 'pos' => 0,
  1292. ];
  1293. $retval .= '<li class="fast_filter db_fast_filter">';
  1294. $retval .= '<form class="ajax fast_filter">';
  1295. $retval .= Url::getHiddenInputs($urlParams);
  1296. $retval .= '<input class="searchClause" type="text"';
  1297. $retval .= ' name="searchClause" accesskey="q"';
  1298. $retval .= " placeholder='"
  1299. . __("Type to filter these, Enter to search all");
  1300. $retval .= "'>";
  1301. $retval .= '<span title="' . __('Clear fast filter') . '">X</span>';
  1302. $retval .= "</form>";
  1303. $retval .= "</li>";
  1304. return $retval;
  1305. }
  1306. if (($node->type == Node::CONTAINER
  1307. && ($node->realName == 'tables'
  1308. || $node->realName == 'views'
  1309. || $node->realName == 'functions'
  1310. || $node->realName == 'procedures'
  1311. || $node->realName == 'events'))
  1312. && method_exists($node->realParent(), 'getPresence')
  1313. && $node->realParent()->getPresence($node->realName) >= $filterItemMin
  1314. ) {
  1315. $paths = $node->getPaths();
  1316. $urlParams = [
  1317. 'pos' => $this->pos,
  1318. 'aPath' => $paths['aPath'],
  1319. 'vPath' => $paths['vPath'],
  1320. 'pos2_name' => $node->realName,
  1321. 'pos2_value' => 0,
  1322. ];
  1323. $retval .= "<li class='fast_filter'>";
  1324. $retval .= "<form class='ajax fast_filter'>";
  1325. $retval .= Url::getHiddenFields($urlParams);
  1326. $retval .= "<input class='searchClause' type='text'";
  1327. $retval .= " name='searchClause2'";
  1328. $retval .= " placeholder='"
  1329. . __("Type to filter these, Enter to search all") . "'>";
  1330. $retval .= "<span title='" . __('Clear fast filter') . "'>X</span>";
  1331. $retval .= "</form>";
  1332. $retval .= "</li>";
  1333. }
  1334. return $retval;
  1335. }
  1336. /**
  1337. * Creates the code for displaying the controls
  1338. * at the top of the navigation tree
  1339. *
  1340. * @return string HTML code for the controls
  1341. */
  1342. private function controls()
  1343. {
  1344. // always iconic
  1345. $showIcon = true;
  1346. $showText = false;
  1347. $retval = '<!-- CONTROLS START -->';
  1348. $retval .= '<li id="navigation_controls_outer">';
  1349. $retval .= '<div id="navigation_controls">';
  1350. $retval .= Util::getNavigationLink(
  1351. '#',
  1352. $showText,
  1353. __('Collapse all'),
  1354. $showIcon,
  1355. 's_collapseall',
  1356. 'pma_navigation_collapse'
  1357. );
  1358. $syncImage = 's_unlink';
  1359. $title = __('Link with main panel');
  1360. if ($GLOBALS['cfg']['NavigationLinkWithMainPanel']) {
  1361. $syncImage = 's_link';
  1362. $title = __('Unlink from main panel');
  1363. }
  1364. $retval .= Util::getNavigationLink(
  1365. '#',
  1366. $showText,
  1367. $title,
  1368. $showIcon,
  1369. $syncImage,
  1370. 'pma_navigation_sync'
  1371. );
  1372. $retval .= '</div>';
  1373. $retval .= '</li>';
  1374. $retval .= '<!-- CONTROLS ENDS -->';
  1375. return $retval;
  1376. }
  1377. /**
  1378. * Generates the HTML code for displaying the list pagination
  1379. *
  1380. * @param Node $node The node for whose children the page
  1381. * selector will be created
  1382. *
  1383. * @return string
  1384. */
  1385. private function getPageSelector($node)
  1386. {
  1387. $retval = '';
  1388. if ($node === $this->tree) {
  1389. $retval .= Util::getListNavigator(
  1390. $this->tree->getPresence('databases', $this->searchClause),
  1391. $this->pos,
  1392. ['server' => $GLOBALS['server']],
  1393. 'navigation.php',
  1394. 'frame_navigation',
  1395. $GLOBALS['cfg']['FirstLevelNavigationItems'],
  1396. 'pos',
  1397. ['dbselector']
  1398. );
  1399. } else {
  1400. if ($node->type == Node::CONTAINER && ! $node->isGroup) {
  1401. $paths = $node->getPaths();
  1402. $level = isset($paths['aPath_clean'][4]) ? 3 : 2;
  1403. $urlParams = [
  1404. 'aPath' => $paths['aPath'],
  1405. 'vPath' => $paths['vPath'],
  1406. 'pos' => $this->pos,
  1407. 'server' => $GLOBALS['server'],
  1408. 'pos2_name' => $paths['aPath_clean'][2],
  1409. ];
  1410. if ($level == 3) {
  1411. $pos = $node->pos3;
  1412. $urlParams['pos2_value'] = $node->pos2;
  1413. $urlParams['pos3_name'] = $paths['aPath_clean'][4];
  1414. } else {
  1415. $pos = $node->pos2;
  1416. }
  1417. $num = $node->realParent()
  1418. ->getPresence(
  1419. $node->realName,
  1420. $this->searchClause2
  1421. );
  1422. $retval .= Util::getListNavigator(
  1423. $num,
  1424. $pos,
  1425. $urlParams,
  1426. 'navigation.php',
  1427. 'frame_navigation',
  1428. $GLOBALS['cfg']['MaxNavigationItems'],
  1429. 'pos' . $level . '_value'
  1430. );
  1431. }
  1432. }
  1433. return $retval;
  1434. }
  1435. /**
  1436. * Called by usort() for sorting the nodes in a container
  1437. *
  1438. * @param Node $a The first element used in the comparison
  1439. * @param Node $b The second element used in the comparison
  1440. *
  1441. * @return int See strnatcmp() and strcmp()
  1442. */
  1443. public static function sortNode($a, $b)
  1444. {
  1445. if ($a->isNew) {
  1446. return -1;
  1447. }
  1448. if ($b->isNew) {
  1449. return 1;
  1450. }
  1451. if ($GLOBALS['cfg']['NaturalOrder']) {
  1452. return strnatcasecmp($a->name, $b->name);
  1453. }
  1454. return strcasecmp($a->name, $b->name);
  1455. }
  1456. /**
  1457. * Display quick warp links, contain Recents and Favorites
  1458. *
  1459. * @return string HTML code
  1460. */
  1461. private function quickWarp()
  1462. {
  1463. $retval = '<div class="pma_quick_warp">';
  1464. if ($GLOBALS['cfg']['NumRecentTables'] > 0) {
  1465. $retval .= RecentFavoriteTable::getInstance('recent')
  1466. ->getHtml();
  1467. }
  1468. if ($GLOBALS['cfg']['NumFavoriteTables'] > 0) {
  1469. $retval .= RecentFavoriteTable::getInstance('favorite')
  1470. ->getHtml();
  1471. }
  1472. $retval .= '<div class="clearfloat"></div>';
  1473. $retval .= '</div>';
  1474. return $retval;
  1475. }
  1476. }