DatabaseStructureController.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Controllers\Database\DatabaseStructureController
  5. *
  6. * @package PhpMyAdmin\Controllers
  7. */
  8. namespace PhpMyAdmin\Controllers\Database;
  9. use PhpMyAdmin\Charsets;
  10. use PhpMyAdmin\Config\PageSettings;
  11. use PhpMyAdmin\Controllers\DatabaseController;
  12. use PhpMyAdmin\Core;
  13. use PhpMyAdmin\Display\CreateTable;
  14. use PhpMyAdmin\Message;
  15. use PhpMyAdmin\RecentFavoriteTable;
  16. use PhpMyAdmin\Relation;
  17. use PhpMyAdmin\Replication;
  18. use PhpMyAdmin\Response;
  19. use PhpMyAdmin\Sanitize;
  20. use PhpMyAdmin\Template;
  21. use PhpMyAdmin\Tracker;
  22. use PhpMyAdmin\Util;
  23. use PhpMyAdmin\Url;
  24. /**
  25. * Handles database structure logic
  26. *
  27. * @package PhpMyAdmin\Controllers
  28. */
  29. class DatabaseStructureController extends DatabaseController
  30. {
  31. /**
  32. * @var int Number of tables
  33. */
  34. protected $_num_tables;
  35. /**
  36. * @var int Current position in the list
  37. */
  38. protected $_pos;
  39. /**
  40. * @var bool DB is information_schema
  41. */
  42. protected $_db_is_system_schema;
  43. /**
  44. * @var int Number of tables
  45. */
  46. protected $_total_num_tables;
  47. /**
  48. * @var array Tables in the database
  49. */
  50. protected $_tables;
  51. /**
  52. * @var bool whether stats show or not
  53. */
  54. protected $_is_show_stats;
  55. /**
  56. * @var Relation $relation
  57. */
  58. private $relation;
  59. /**
  60. * Constructor
  61. */
  62. public function __construct($response, $dbi, $db)
  63. {
  64. parent::__construct($response, $dbi, $db);
  65. $this->relation = new Relation();
  66. }
  67. /**
  68. * Retrieves databse information for further use
  69. *
  70. * @param string $sub_part Page part name
  71. *
  72. * @return void
  73. */
  74. private function _getDbInfo($sub_part)
  75. {
  76. list(
  77. $tables,
  78. $num_tables,
  79. $total_num_tables,
  80. ,
  81. $is_show_stats,
  82. $db_is_system_schema,
  83. ,
  84. ,
  85. $pos
  86. ) = Util::getDbInfo($this->db, $sub_part);
  87. $this->_tables = $tables;
  88. $this->_num_tables = $num_tables;
  89. $this->_pos = $pos;
  90. $this->_db_is_system_schema = $db_is_system_schema;
  91. $this->_total_num_tables = $total_num_tables;
  92. $this->_is_show_stats = $is_show_stats;
  93. }
  94. /**
  95. * Index action
  96. *
  97. * @return void
  98. */
  99. public function indexAction()
  100. {
  101. $response = Response::getInstance();
  102. // Add/Remove favorite tables using Ajax request.
  103. if ($response->isAjax() && !empty($_REQUEST['favorite_table'])) {
  104. $this->addRemoveFavoriteTablesAction();
  105. return;
  106. }
  107. // If there is an Ajax request for real row count of a table.
  108. if ($response->isAjax()
  109. && isset($_REQUEST['real_row_count'])
  110. && $_REQUEST['real_row_count'] == true
  111. ) {
  112. $this->handleRealRowCountRequestAction();
  113. return;
  114. }
  115. // Drops/deletes/etc. multiple tables if required
  116. if ((! empty($_POST['submit_mult']) && isset($_POST['selected_tbl']))
  117. || isset($_POST['mult_btn'])
  118. ) {
  119. $this->multiSubmitAction();
  120. }
  121. $this->response->getHeader()->getScripts()->addFiles(
  122. array(
  123. 'db_structure.js',
  124. 'tbl_change.js',
  125. )
  126. );
  127. // Gets the database structure
  128. $this->_getDbInfo('_structure');
  129. // Checks if there are any tables to be shown on current page.
  130. // If there are no tables, the user is redirected to the last page
  131. // having any.
  132. if ($this->_total_num_tables > 0 && $this->_pos > $this->_total_num_tables) {
  133. $uri = './db_structure.php' . Url::getCommonRaw(array(
  134. 'db' => $this->db,
  135. 'pos' => max(0, $this->_total_num_tables - $GLOBALS['cfg']['MaxTableList']),
  136. 'reload' => 1
  137. ));
  138. Core::sendHeaderLocation($uri);
  139. }
  140. include_once 'libraries/replication.inc.php';
  141. PageSettings::showGroup('DbStructure');
  142. // 1. No tables
  143. if ($this->_num_tables == 0) {
  144. $this->response->addHTML(
  145. Message::notice(__('No tables found in database.'))
  146. );
  147. if (empty($this->_db_is_system_schema)) {
  148. $this->response->addHTML(CreateTable::getHtml($this->db));
  149. }
  150. return;
  151. }
  152. // else
  153. // 2. Shows table information
  154. /**
  155. * Displays the tables list
  156. */
  157. $this->response->addHTML('<div id="tableslistcontainer">');
  158. $_url_params = array(
  159. 'pos' => $this->_pos,
  160. 'db' => $this->db);
  161. // Add the sort options if they exists
  162. if (isset($_REQUEST['sort'])) {
  163. $_url_params['sort'] = $_REQUEST['sort'];
  164. }
  165. if (isset($_REQUEST['sort_order'])) {
  166. $_url_params['sort_order'] = $_REQUEST['sort_order'];
  167. }
  168. $this->response->addHTML(
  169. Util::getListNavigator(
  170. $this->_total_num_tables, $this->_pos, $_url_params,
  171. 'db_structure.php', 'frame_content', $GLOBALS['cfg']['MaxTableList']
  172. )
  173. );
  174. $this->displayTableList();
  175. // display again the table list navigator
  176. $this->response->addHTML(
  177. Util::getListNavigator(
  178. $this->_total_num_tables, $this->_pos, $_url_params,
  179. 'db_structure.php', 'frame_content',
  180. $GLOBALS['cfg']['MaxTableList']
  181. )
  182. );
  183. $this->response->addHTML('</div><hr />');
  184. /**
  185. * Work on the database
  186. */
  187. /* DATABASE WORK */
  188. /* Printable view of a table */
  189. $this->response->addHTML(
  190. Template::get('database/structure/print_view_data_dictionary_link')
  191. ->render(array('url_query' => Url::getCommon(
  192. array(
  193. 'db' => $this->db,
  194. 'goto' => 'db_structure.php',
  195. )
  196. )))
  197. );
  198. if (empty($this->_db_is_system_schema)) {
  199. $this->response->addHTML(CreateTable::getHtml($this->db));
  200. }
  201. }
  202. /**
  203. * Add or remove favorite tables
  204. *
  205. * @return void
  206. */
  207. public function addRemoveFavoriteTablesAction()
  208. {
  209. $fav_instance = RecentFavoriteTable::getInstance('favorite');
  210. if (isset($_REQUEST['favorite_tables'])) {
  211. $favorite_tables = json_decode($_REQUEST['favorite_tables'], true);
  212. } else {
  213. $favorite_tables = array();
  214. }
  215. // Required to keep each user's preferences separate.
  216. $user = sha1($GLOBALS['cfg']['Server']['user']);
  217. // Request for Synchronization of favorite tables.
  218. if (isset($_REQUEST['sync_favorite_tables'])) {
  219. $cfgRelation = $this->relation->getRelationsParam();
  220. if ($cfgRelation['favoritework']) {
  221. $this->synchronizeFavoriteTables($fav_instance, $user, $favorite_tables);
  222. }
  223. return;
  224. }
  225. $changes = true;
  226. $titles = Util::buildActionTitles();
  227. $favorite_table = $_REQUEST['favorite_table'];
  228. $already_favorite = $this->checkFavoriteTable($favorite_table);
  229. if (isset($_REQUEST['remove_favorite'])) {
  230. if ($already_favorite) {
  231. // If already in favorite list, remove it.
  232. $fav_instance->remove($this->db, $favorite_table);
  233. $already_favorite = false; // for favorite_anchor template
  234. }
  235. } elseif (isset($_REQUEST['add_favorite'])) {
  236. if (!$already_favorite) {
  237. $nbTables = count($fav_instance->getTables());
  238. if ($nbTables == $GLOBALS['cfg']['NumFavoriteTables']) {
  239. $changes = false;
  240. } else {
  241. // Otherwise add to favorite list.
  242. $fav_instance->add($this->db, $favorite_table);
  243. $already_favorite = true; // for favorite_anchor template
  244. }
  245. }
  246. }
  247. $favorite_tables[$user] = $fav_instance->getTables();
  248. $this->response->addJSON('changes', $changes);
  249. if (!$changes) {
  250. $this->response->addJSON(
  251. 'message',
  252. Template::get('components/error_message')
  253. ->render(
  254. array(
  255. 'msg' => __("Favorite List is full!")
  256. )
  257. )
  258. );
  259. return;
  260. }
  261. // Check if current table is already in favorite list.
  262. $favParams = array('db' => $this->db,
  263. 'ajax_request' => true,
  264. 'favorite_table' => $favorite_table,
  265. (($already_favorite ? 'remove' : 'add') . '_favorite') => true
  266. );
  267. $this->response->addJSON(
  268. array(
  269. 'user' => $user,
  270. 'favorite_tables' => json_encode($favorite_tables),
  271. 'list' => $fav_instance->getHtmlList(),
  272. 'anchor' => Template::get('database/structure/favorite_anchor')
  273. ->render(
  274. array(
  275. 'table_name_hash' => md5($favorite_table),
  276. 'db_table_name_hash' => md5($this->db . "." . $favorite_table),
  277. 'fav_params' => $favParams,
  278. 'already_favorite' => $already_favorite,
  279. 'titles' => $titles,
  280. )
  281. )
  282. )
  283. );
  284. }
  285. /**
  286. * Handles request for real row count on database level view page.
  287. *
  288. * @return boolean true
  289. */
  290. public function handleRealRowCountRequestAction()
  291. {
  292. $ajax_response = $this->response;
  293. // If there is a request to update all table's row count.
  294. if (!isset($_REQUEST['real_row_count_all'])) {
  295. // Get the real row count for the table.
  296. $real_row_count = $this->dbi
  297. ->getTable($this->db, $_REQUEST['table'])
  298. ->getRealRowCountTable();
  299. // Format the number.
  300. $real_row_count = Util::formatNumber($real_row_count, 0);
  301. $ajax_response->addJSON('real_row_count', $real_row_count);
  302. return;
  303. }
  304. // Array to store the results.
  305. $real_row_count_all = array();
  306. // Iterate over each table and fetch real row count.
  307. foreach ($this->_tables as $table) {
  308. $row_count = $this->dbi
  309. ->getTable($this->db, $table['TABLE_NAME'])
  310. ->getRealRowCountTable();
  311. $real_row_count_all[] = array(
  312. 'table' => $table['TABLE_NAME'],
  313. 'row_count' => $row_count
  314. );
  315. }
  316. $ajax_response->addJSON(
  317. 'real_row_count_all',
  318. json_encode($real_row_count_all)
  319. );
  320. }
  321. /**
  322. * Handles actions related to multiple tables
  323. *
  324. * @return void
  325. */
  326. public function multiSubmitAction()
  327. {
  328. $action = 'db_structure.php';
  329. $err_url = 'db_structure.php' . Url::getCommon(
  330. array('db' => $this->db)
  331. );
  332. // see bug #2794840; in this case, code path is:
  333. // db_structure.php -> libraries/mult_submits.inc.php -> sql.php
  334. // -> db_structure.php and if we got an error on the multi submit,
  335. // we must display it here and not call again mult_submits.inc.php
  336. if (! isset($_POST['error']) || false === $_POST['error']) {
  337. include 'libraries/mult_submits.inc.php';
  338. }
  339. if (empty($_POST['message'])) {
  340. $_POST['message'] = Message::success();
  341. }
  342. }
  343. /**
  344. * Displays the list of tables
  345. *
  346. * @return void
  347. */
  348. protected function displayTableList()
  349. {
  350. // filtering
  351. $this->response->addHTML(
  352. Template::get('filter')->render(array('filter_value' => ''))
  353. );
  354. // table form
  355. $this->response->addHTML(
  356. Template::get('database/structure/table_header')->render([
  357. 'db' => $this->db,
  358. 'db_is_system_schema' => $this->_db_is_system_schema,
  359. 'replication' => $GLOBALS['replication_info']['slave']['status'],
  360. 'properties_num_columns' => $GLOBALS['cfg']['PropertiesNumColumns'],
  361. 'is_show_stats' => $GLOBALS['is_show_stats'],
  362. 'show_charset' => $GLOBALS['cfg']['ShowDbStructureCharset'],
  363. 'show_comment' => $GLOBALS['cfg']['ShowDbStructureComment'],
  364. 'show_creation' => $GLOBALS['cfg']['ShowDbStructureCreation'],
  365. 'show_last_update' => $GLOBALS['cfg']['ShowDbStructureLastUpdate'],
  366. 'show_last_check' => $GLOBALS['cfg']['ShowDbStructureLastCheck'],
  367. 'num_favorite_tables' => $GLOBALS['cfg']['NumFavoriteTables'],
  368. ])
  369. );
  370. $i = $sum_entries = 0;
  371. $overhead_check = false;
  372. $create_time_all = '';
  373. $update_time_all = '';
  374. $check_time_all = '';
  375. $num_columns = $GLOBALS['cfg']['PropertiesNumColumns'] > 1
  376. ? ceil($this->_num_tables / $GLOBALS['cfg']['PropertiesNumColumns']) + 1
  377. : 0;
  378. $row_count = 0;
  379. $sum_size = 0;
  380. $overhead_size = 0;
  381. $hidden_fields = array();
  382. $overall_approx_rows = false;
  383. foreach ($this->_tables as $keyname => $current_table) {
  384. // Get valid statistics whatever is the table type
  385. $drop_query = '';
  386. $drop_message = '';
  387. $overhead = '';
  388. $input_class = ['checkall'];
  389. $table_is_view = false;
  390. // Sets parameters for links
  391. $tbl_url_query = Url::getCommon(
  392. array('db' => $this->db, 'table' => $current_table['TABLE_NAME'])
  393. );
  394. // do not list the previous table's size info for a view
  395. list($current_table, $formatted_size, $unit, $formatted_overhead,
  396. $overhead_unit, $overhead_size, $table_is_view, $sum_size)
  397. = $this->getStuffForEngineTypeTable(
  398. $current_table, $sum_size, $overhead_size
  399. );
  400. $curTable = $this->dbi
  401. ->getTable($this->db, $current_table['TABLE_NAME']);
  402. if (!$curTable->isMerge()) {
  403. $sum_entries += $current_table['TABLE_ROWS'];
  404. }
  405. if (isset($current_table['Collation'])) {
  406. $collation = '<dfn title="'
  407. . Charsets::getCollationDescr($current_table['Collation']) . '">'
  408. . $current_table['Collation'] . '</dfn>';
  409. } else {
  410. $collation = '---';
  411. }
  412. if ($this->_is_show_stats) {
  413. if ($formatted_overhead != '') {
  414. $overhead = '<a href="tbl_structure.php'
  415. . $tbl_url_query . '#showusage">'
  416. . '<span>' . $formatted_overhead . '</span>&nbsp;'
  417. . '<span class="unit">' . $overhead_unit . '</span>'
  418. . '</a>' . "\n";
  419. $overhead_check = true;
  420. $input_class[] = 'tbl-overhead';
  421. } else {
  422. $overhead = '-';
  423. }
  424. } // end if
  425. if ($GLOBALS['cfg']['ShowDbStructureCharset']) {
  426. if (isset($current_table['Collation'])) {
  427. $charset = mb_substr($collation, 0, mb_strpos($collation, "_"));
  428. } else {
  429. $charset = '';
  430. }
  431. }
  432. if ($GLOBALS['cfg']['ShowDbStructureCreation']) {
  433. $create_time = isset($current_table['Create_time'])
  434. ? $current_table['Create_time'] : '';
  435. if ($create_time
  436. && (!$create_time_all
  437. || $create_time < $create_time_all)
  438. ) {
  439. $create_time_all = $create_time;
  440. }
  441. }
  442. if ($GLOBALS['cfg']['ShowDbStructureLastUpdate']) {
  443. $update_time = isset($current_table['Update_time'])
  444. ? $current_table['Update_time'] : '';
  445. if ($update_time
  446. && (!$update_time_all
  447. || $update_time < $update_time_all)
  448. ) {
  449. $update_time_all = $update_time;
  450. }
  451. }
  452. if ($GLOBALS['cfg']['ShowDbStructureLastCheck']) {
  453. $check_time = isset($current_table['Check_time'])
  454. ? $current_table['Check_time'] : '';
  455. if ($check_time
  456. && (!$check_time_all
  457. || $check_time < $check_time_all)
  458. ) {
  459. $check_time_all = $check_time;
  460. }
  461. }
  462. $truename = (!empty($tooltip_truename)
  463. && isset($tooltip_truename[$current_table['TABLE_NAME']]))
  464. ? $tooltip_truename[$current_table['TABLE_NAME']]
  465. : $current_table['TABLE_NAME'];
  466. $i++;
  467. $row_count++;
  468. if ($table_is_view) {
  469. $hidden_fields[] = '<input type="hidden" name="views[]" value="'
  470. . htmlspecialchars($current_table['TABLE_NAME']) . '" />';
  471. }
  472. /*
  473. * Always activate links for Browse, Search and Empty, even if
  474. * the icons are greyed, because
  475. * 1. for views, we don't know the number of rows at this point
  476. * 2. for tables, another source could have populated them since the
  477. * page was generated
  478. *
  479. * I could have used the PHP ternary conditional operator but I find
  480. * the code easier to read without this operator.
  481. */
  482. $may_have_rows = $current_table['TABLE_ROWS'] > 0 || $table_is_view;
  483. $titles = Util::buildActionTitles();
  484. $browse_table = Template::get('database/structure/browse_table')
  485. ->render(
  486. array(
  487. 'tbl_url_query' => $tbl_url_query,
  488. 'title' => $may_have_rows ? $titles['Browse']
  489. : $titles['NoBrowse'],
  490. )
  491. );
  492. $search_table = Template::get('database/structure/search_table')
  493. ->render(
  494. array(
  495. 'tbl_url_query' => $tbl_url_query,
  496. 'title' => $may_have_rows ? $titles['Search']
  497. : $titles['NoSearch'],
  498. )
  499. );
  500. $browse_table_label = Template::get(
  501. 'database/structure/browse_table_label'
  502. )
  503. ->render(
  504. array(
  505. 'tbl_url_query' => $tbl_url_query,
  506. 'title' => htmlspecialchars(
  507. $current_table['TABLE_COMMENT']
  508. ),
  509. 'truename' => $truename,
  510. )
  511. );
  512. $empty_table = '';
  513. if (!$this->_db_is_system_schema) {
  514. $empty_table = '&nbsp;';
  515. if (!$table_is_view) {
  516. $empty_table = Template::get('database/structure/empty_table')
  517. ->render(
  518. array(
  519. 'tbl_url_query' => $tbl_url_query,
  520. 'sql_query' => urlencode(
  521. 'TRUNCATE ' . Util::backquote(
  522. $current_table['TABLE_NAME']
  523. )
  524. ),
  525. 'message_to_show' => urlencode(
  526. sprintf(
  527. __('Table %s has been emptied.'),
  528. htmlspecialchars(
  529. $current_table['TABLE_NAME']
  530. )
  531. )
  532. ),
  533. 'title' => $may_have_rows ? $titles['Empty']
  534. : $titles['NoEmpty'],
  535. )
  536. );
  537. }
  538. $drop_query = sprintf(
  539. 'DROP %s %s',
  540. ($table_is_view || $current_table['ENGINE'] == null) ? 'VIEW'
  541. : 'TABLE',
  542. Util::backquote(
  543. $current_table['TABLE_NAME']
  544. )
  545. );
  546. $drop_message = sprintf(
  547. (($table_is_view || $current_table['ENGINE'] == null)
  548. ? __('View %s has been dropped.')
  549. : __('Table %s has been dropped.')),
  550. str_replace(
  551. ' ', '&nbsp;',
  552. htmlspecialchars($current_table['TABLE_NAME'])
  553. )
  554. );
  555. }
  556. if ($num_columns > 0
  557. && $this->_num_tables > $num_columns
  558. && ($row_count % $num_columns) == 0
  559. ) {
  560. $row_count = 1;
  561. $this->response->addHTML(
  562. '</tr></tbody></table></div></form>'
  563. );
  564. $this->response->addHTML(
  565. Template::get('database/structure/table_header')->render([
  566. 'db' => $this->db,
  567. 'db_is_system_schema' => $this->_db_is_system_schema,
  568. 'replication' => $GLOBALS['replication_info']['slave']['status'],
  569. 'properties_num_columns' => $GLOBALS['cfg']['PropertiesNumColumns'],
  570. 'is_show_stats' => $GLOBALS['is_show_stats'],
  571. 'show_charset' => $GLOBALS['cfg']['ShowDbStructureCharset'],
  572. 'show_comment' => $GLOBALS['cfg']['ShowDbStructureComment'],
  573. 'show_creation' => $GLOBALS['cfg']['ShowDbStructureCreation'],
  574. 'show_last_update' => $GLOBALS['cfg']['ShowDbStructureLastUpdate'],
  575. 'show_last_check' => $GLOBALS['cfg']['ShowDbStructureLastCheck'],
  576. 'num_favorite_tables' => $GLOBALS['cfg']['NumFavoriteTables'],
  577. ])
  578. );
  579. }
  580. list($approx_rows, $show_superscript) = $this->isRowCountApproximated(
  581. $current_table, $table_is_view
  582. );
  583. list($do, $ignored) = $this->getReplicationStatus($truename);
  584. $this->response->addHTML(
  585. Template::get('database/structure/structure_table_row')
  586. ->render(
  587. array(
  588. 'db' => $this->db,
  589. 'curr' => $i,
  590. 'input_class' => implode(' ', $input_class),
  591. 'table_is_view' => $table_is_view,
  592. 'current_table' => $current_table,
  593. 'browse_table_label' => $browse_table_label,
  594. 'tracking_icon' => $this->getTrackingIcon($truename),
  595. 'server_slave_status' => $GLOBALS['replication_info']['slave']['status'],
  596. 'browse_table' => $browse_table,
  597. 'tbl_url_query' => $tbl_url_query,
  598. 'search_table' => $search_table,
  599. 'db_is_system_schema' => $this->_db_is_system_schema,
  600. 'titles' => $titles,
  601. 'empty_table' => $empty_table,
  602. 'drop_query' => $drop_query,
  603. 'drop_message' => $drop_message,
  604. 'collation' => $collation,
  605. 'formatted_size' => $formatted_size,
  606. 'unit' => $unit,
  607. 'overhead' => $overhead,
  608. 'create_time' => isset($create_time)
  609. ? $create_time : '',
  610. 'update_time' => isset($update_time)
  611. ? $update_time : '',
  612. 'check_time' => isset($check_time)
  613. ? $check_time : '',
  614. 'charset' => isset($charset)
  615. ? $charset : '',
  616. 'is_show_stats' => $this->_is_show_stats,
  617. 'ignored' => $ignored,
  618. 'do' => $do,
  619. 'approx_rows' => $approx_rows,
  620. 'show_superscript' => $show_superscript,
  621. 'already_favorite' => $this->checkFavoriteTable(
  622. $current_table['TABLE_NAME']
  623. ),
  624. 'num_favorite_tables' => $GLOBALS['cfg']['NumFavoriteTables'],
  625. 'properties_num_columns' => $GLOBALS['cfg']['PropertiesNumColumns'],
  626. 'limit_chars' => $GLOBALS['cfg']['LimitChars'],
  627. 'show_charset' => $GLOBALS['cfg']['ShowDbStructureCharset'],
  628. 'show_comment' => $GLOBALS['cfg']['ShowDbStructureComment'],
  629. 'show_creation' => $GLOBALS['cfg']['ShowDbStructureCreation'],
  630. 'show_last_update' => $GLOBALS['cfg']['ShowDbStructureLastUpdate'],
  631. 'show_last_check' => $GLOBALS['cfg']['ShowDbStructureLastCheck'],
  632. )
  633. )
  634. );
  635. $overall_approx_rows = $overall_approx_rows || $approx_rows;
  636. } // end foreach
  637. $this->response->addHTML('</tbody>');
  638. $db_collation = $this->dbi->getDbCollation($this->db);
  639. $db_charset = mb_substr($db_collation, 0, mb_strpos($db_collation, "_"));
  640. // Show Summary
  641. $this->response->addHTML(
  642. Template::get('database/structure/body_for_table_summary')->render(
  643. array(
  644. 'num_tables' => $this->_num_tables,
  645. 'server_slave_status' => $GLOBALS['replication_info']['slave']['status'],
  646. 'db_is_system_schema' => $this->_db_is_system_schema,
  647. 'sum_entries' => $sum_entries,
  648. 'db_collation' => $db_collation,
  649. 'is_show_stats' => $this->_is_show_stats,
  650. 'db_charset' => $db_charset,
  651. 'sum_size' => $sum_size,
  652. 'overhead_size' => $overhead_size,
  653. 'create_time_all' => $create_time_all,
  654. 'update_time_all' => $update_time_all,
  655. 'check_time_all' => $check_time_all,
  656. 'approx_rows' => $overall_approx_rows,
  657. 'num_favorite_tables' => $GLOBALS['cfg']['NumFavoriteTables'],
  658. 'db' => $GLOBALS['db'],
  659. 'properties_num_columns' => $GLOBALS['cfg']['PropertiesNumColumns'],
  660. 'dbi' => $GLOBALS['dbi'],
  661. 'show_charset' => $GLOBALS['cfg']['ShowDbStructureCharset'],
  662. 'show_comment' => $GLOBALS['cfg']['ShowDbStructureComment'],
  663. 'show_creation' => $GLOBALS['cfg']['ShowDbStructureCreation'],
  664. 'show_last_update' => $GLOBALS['cfg']['ShowDbStructureLastUpdate'],
  665. 'show_last_check' => $GLOBALS['cfg']['ShowDbStructureLastCheck'],
  666. )
  667. )
  668. );
  669. $this->response->addHTML('</table>');
  670. //check all
  671. $this->response->addHTML(
  672. Template::get('database/structure/check_all_tables')->render([
  673. 'pma_theme_image' => $GLOBALS['pmaThemeImage'],
  674. 'text_dir' => $GLOBALS['text_dir'],
  675. 'overhead_check' => $overhead_check,
  676. 'db_is_system_schema' => $this->_db_is_system_schema,
  677. 'hidden_fields' => $hidden_fields,
  678. 'disable_multi_table' => $GLOBALS['cfg']['DisableMultiTableMaintenance'],
  679. 'central_columns_work' => $GLOBALS['cfgRelation']['centralcolumnswork'],
  680. ])
  681. );
  682. $this->response->addHTML('</form>'); //end of form
  683. }
  684. /**
  685. * Returns the tracking icon if the table is tracked
  686. *
  687. * @param string $table table name
  688. *
  689. * @return string HTML for tracking icon
  690. */
  691. protected function getTrackingIcon($table)
  692. {
  693. $tracking_icon = '';
  694. if (Tracker::isActive()) {
  695. $is_tracked = Tracker::isTracked($this->db, $table);
  696. if ($is_tracked
  697. || Tracker::getVersion($this->db, $table) > 0
  698. ) {
  699. $tracking_icon = Template::get(
  700. 'database/structure/tracking_icon'
  701. )
  702. ->render(
  703. array(
  704. 'db' => $this->db,
  705. 'table' => $table,
  706. 'is_tracked' => $is_tracked,
  707. )
  708. );
  709. }
  710. }
  711. return $tracking_icon;
  712. }
  713. /**
  714. * Returns whether the row count is approximated
  715. *
  716. * @param array $current_table array containing details about the table
  717. * @param boolean $table_is_view whether the table is a view
  718. *
  719. * @return array
  720. */
  721. protected function isRowCountApproximated(array $current_table, $table_is_view)
  722. {
  723. $approx_rows = false;
  724. $show_superscript = '';
  725. // there is a null value in the ENGINE
  726. // - when the table needs to be repaired, or
  727. // - when it's a view
  728. // so ensure that we'll display "in use" below for a table
  729. // that needs to be repaired
  730. if (isset($current_table['TABLE_ROWS'])
  731. && ($current_table['ENGINE'] != null || $table_is_view)
  732. ) {
  733. // InnoDB/TokuDB table: we did not get an accurate row count
  734. $approx_rows = !$table_is_view
  735. && in_array($current_table['ENGINE'], array('InnoDB', 'TokuDB'))
  736. && !$current_table['COUNTED'];
  737. if ($table_is_view
  738. && $current_table['TABLE_ROWS'] >= $GLOBALS['cfg']['MaxExactCountViews']
  739. ) {
  740. $approx_rows = true;
  741. $show_superscript = Util::showHint(
  742. Sanitize::sanitize(
  743. sprintf(
  744. __(
  745. 'This view has at least this number of '
  746. . 'rows. Please refer to %sdocumentation%s.'
  747. ),
  748. '[doc@cfg_MaxExactCountViews]', '[/doc]'
  749. )
  750. )
  751. );
  752. }
  753. }
  754. return array($approx_rows, $show_superscript);
  755. }
  756. /**
  757. * Returns the replication status of the table.
  758. *
  759. * @param string $table table name
  760. *
  761. * @return array
  762. */
  763. protected function getReplicationStatus($table)
  764. {
  765. $do = $ignored = false;
  766. if ($GLOBALS['replication_info']['slave']['status']) {
  767. $nbServSlaveDoDb = count(
  768. $GLOBALS['replication_info']['slave']['Do_DB']
  769. );
  770. $nbServSlaveIgnoreDb = count(
  771. $GLOBALS['replication_info']['slave']['Ignore_DB']
  772. );
  773. $searchDoDBInTruename = array_search(
  774. $table, $GLOBALS['replication_info']['slave']['Do_DB']
  775. );
  776. $searchDoDBInDB = array_search(
  777. $this->db, $GLOBALS['replication_info']['slave']['Do_DB']
  778. );
  779. $do = strlen($searchDoDBInTruename) > 0
  780. || strlen($searchDoDBInDB) > 0
  781. || ($nbServSlaveDoDb == 0 && $nbServSlaveIgnoreDb == 0)
  782. || $this->hasTable(
  783. $GLOBALS['replication_info']['slave']['Wild_Do_Table'],
  784. $table
  785. );
  786. $searchDb = array_search(
  787. $this->db,
  788. $GLOBALS['replication_info']['slave']['Ignore_DB']
  789. );
  790. $searchTable = array_search(
  791. $table,
  792. $GLOBALS['replication_info']['slave']['Ignore_Table']
  793. );
  794. $ignored = strlen($searchTable) > 0
  795. || strlen($searchDb) > 0
  796. || $this->hasTable(
  797. $GLOBALS['replication_info']['slave']['Wild_Ignore_Table'],
  798. $table
  799. );
  800. }
  801. return array($do, $ignored);
  802. }
  803. /**
  804. * Synchronize favorite tables
  805. *
  806. *
  807. * @param RecentFavoriteTable $fav_instance Instance of this class
  808. * @param string $user The user hash
  809. * @param array $favorite_tables Existing favorites
  810. *
  811. * @return void
  812. */
  813. protected function synchronizeFavoriteTables(
  814. $fav_instance,
  815. $user,
  816. array $favorite_tables
  817. ) {
  818. $fav_instance_tables = $fav_instance->getTables();
  819. if (empty($fav_instance_tables)
  820. && isset($favorite_tables[$user])
  821. ) {
  822. foreach ($favorite_tables[$user] as $key => $value) {
  823. $fav_instance->add($value['db'], $value['table']);
  824. }
  825. }
  826. $favorite_tables[$user] = $fav_instance->getTables();
  827. $this->response->addJSON(
  828. array(
  829. 'favorite_tables' => json_encode($favorite_tables),
  830. 'list' => $fav_instance->getHtmlList()
  831. )
  832. );
  833. $server_id = $GLOBALS['server'];
  834. // Set flag when localStorage and pmadb(if present) are in sync.
  835. $_SESSION['tmpval']['favorites_synced'][$server_id] = true;
  836. }
  837. /**
  838. * Function to check if a table is already in favorite list.
  839. *
  840. * @param string $current_table current table
  841. *
  842. * @return true|false
  843. */
  844. protected function checkFavoriteTable($current_table)
  845. {
  846. // ensure $_SESSION['tmpval']['favorite_tables'] is initialized
  847. RecentFavoriteTable::getInstance('favorite');
  848. foreach (
  849. $_SESSION['tmpval']['favorite_tables'][$GLOBALS['server']] as $value
  850. ) {
  851. if ($value['db'] == $this->db && $value['table'] == $current_table) {
  852. return true;
  853. }
  854. }
  855. return false;
  856. }
  857. /**
  858. * Find table with truename
  859. *
  860. * @param array $db DB to look into
  861. * @param string $truename Table name
  862. *
  863. * @return bool
  864. */
  865. protected function hasTable(array $db, $truename)
  866. {
  867. foreach ($db as $db_table) {
  868. if ($this->db == Replication::extractDbOrTable($db_table)
  869. && preg_match(
  870. "@^" .
  871. preg_quote(mb_substr(Replication::extractDbOrTable($db_table, 'table'), 0, -1)) . "@",
  872. $truename
  873. )
  874. ) {
  875. return true;
  876. }
  877. }
  878. return false;
  879. }
  880. /**
  881. * Get the value set for ENGINE table,
  882. *
  883. * @param array $current_table current table
  884. * @param integer $sum_size total table size
  885. * @param integer $overhead_size overhead size
  886. *
  887. * @return array
  888. * @internal param bool $table_is_view whether table is view or not
  889. */
  890. protected function getStuffForEngineTypeTable(
  891. array $current_table, $sum_size, $overhead_size
  892. ) {
  893. $formatted_size = '-';
  894. $unit = '';
  895. $formatted_overhead = '';
  896. $overhead_unit = '';
  897. $table_is_view = false;
  898. switch ( $current_table['ENGINE']) {
  899. // MyISAM, ISAM or Heap table: Row count, data size and index size
  900. // are accurate; data size is accurate for ARCHIVE
  901. case 'MyISAM' :
  902. case 'ISAM' :
  903. case 'HEAP' :
  904. case 'MEMORY' :
  905. case 'ARCHIVE' :
  906. case 'Aria' :
  907. case 'Maria' :
  908. list($current_table, $formatted_size, $unit, $formatted_overhead,
  909. $overhead_unit, $overhead_size, $sum_size)
  910. = $this->getValuesForAriaTable(
  911. $current_table, $sum_size, $overhead_size,
  912. $formatted_size, $unit, $formatted_overhead, $overhead_unit
  913. );
  914. break;
  915. case 'InnoDB' :
  916. case 'PBMS' :
  917. case 'TokuDB' :
  918. // InnoDB table: Row count is not accurate but data and index sizes are.
  919. // PBMS table in Drizzle: TABLE_ROWS is taken from table cache,
  920. // so it may be unavailable
  921. list($current_table, $formatted_size, $unit, $sum_size)
  922. = $this->getValuesForInnodbTable(
  923. $current_table, $sum_size
  924. );
  925. break;
  926. // Mysql 5.0.x (and lower) uses MRG_MyISAM
  927. // and MySQL 5.1.x (and higher) uses MRG_MYISAM
  928. // Both are aliases for MERGE
  929. case 'MRG_MyISAM' :
  930. case 'MRG_MYISAM' :
  931. case 'MERGE' :
  932. case 'BerkeleyDB' :
  933. // Merge or BerkleyDB table: Only row count is accurate.
  934. if ($this->_is_show_stats) {
  935. $formatted_size = ' - ';
  936. $unit = '';
  937. }
  938. break;
  939. // for a view, the ENGINE is sometimes reported as null,
  940. // or on some servers it's reported as "SYSTEM VIEW"
  941. case null :
  942. case 'SYSTEM VIEW' :
  943. // possibly a view, do nothing
  944. break;
  945. default :
  946. // Unknown table type.
  947. if ($this->_is_show_stats) {
  948. $formatted_size = __('unknown');
  949. $unit = '';
  950. }
  951. } // end switch
  952. if ($current_table['TABLE_TYPE'] == 'VIEW'
  953. || $current_table['TABLE_TYPE'] == 'SYSTEM VIEW'
  954. ) {
  955. // countRecords() takes care of $cfg['MaxExactCountViews']
  956. $current_table['TABLE_ROWS'] = $this->dbi
  957. ->getTable($this->db, $current_table['TABLE_NAME'])
  958. ->countRecords(true);
  959. $table_is_view = true;
  960. }
  961. return array($current_table, $formatted_size, $unit, $formatted_overhead,
  962. $overhead_unit, $overhead_size, $table_is_view, $sum_size
  963. );
  964. }
  965. /**
  966. * Get values for ARIA/MARIA tables
  967. *
  968. * @param array $current_table current table
  969. * @param integer $sum_size sum size
  970. * @param integer $overhead_size overhead size
  971. * @param integer $formatted_size formatted size
  972. * @param string $unit unit
  973. * @param integer $formatted_overhead overhead formatted
  974. * @param string $overhead_unit overhead unit
  975. *
  976. * @return array
  977. */
  978. protected function getValuesForAriaTable(
  979. array $current_table, $sum_size, $overhead_size, $formatted_size, $unit,
  980. $formatted_overhead, $overhead_unit
  981. ) {
  982. if ($this->_db_is_system_schema) {
  983. $current_table['Rows'] = $this->dbi
  984. ->getTable($this->db, $current_table['Name'])
  985. ->countRecords();
  986. }
  987. if ($this->_is_show_stats) {
  988. $tblsize = $current_table['Data_length']
  989. + $current_table['Index_length'];
  990. $sum_size += $tblsize;
  991. list($formatted_size, $unit) = Util::formatByteDown(
  992. $tblsize, 3, ($tblsize > 0) ? 1 : 0
  993. );
  994. if (isset($current_table['Data_free'])
  995. && $current_table['Data_free'] > 0
  996. ) {
  997. list($formatted_overhead, $overhead_unit)
  998. = Util::formatByteDown(
  999. $current_table['Data_free'], 3,
  1000. (($current_table['Data_free'] > 0) ? 1 : 0)
  1001. );
  1002. $overhead_size += $current_table['Data_free'];
  1003. }
  1004. }
  1005. return array($current_table, $formatted_size, $unit, $formatted_overhead,
  1006. $overhead_unit, $overhead_size, $sum_size
  1007. );
  1008. }
  1009. /**
  1010. * Get values for InnoDB table
  1011. *
  1012. * @param array $current_table current table
  1013. * @param integer $sum_size sum size
  1014. *
  1015. * @return array
  1016. */
  1017. protected function getValuesForInnodbTable(
  1018. array $current_table, $sum_size
  1019. ) {
  1020. $formatted_size = $unit = '';
  1021. if ((in_array($current_table['ENGINE'], array('InnoDB', 'TokuDB'))
  1022. && $current_table['TABLE_ROWS'] < $GLOBALS['cfg']['MaxExactCount'])
  1023. || !isset($current_table['TABLE_ROWS'])
  1024. ) {
  1025. $current_table['COUNTED'] = true;
  1026. $current_table['TABLE_ROWS'] = $this->dbi
  1027. ->getTable($this->db, $current_table['TABLE_NAME'])
  1028. ->countRecords(true);
  1029. } else {
  1030. $current_table['COUNTED'] = false;
  1031. }
  1032. if ($this->_is_show_stats) {
  1033. $tblsize = $current_table['Data_length']
  1034. + $current_table['Index_length'];
  1035. $sum_size += $tblsize;
  1036. list($formatted_size, $unit) = Util::formatByteDown(
  1037. $tblsize, 3, (($tblsize > 0) ? 1 : 0)
  1038. );
  1039. }
  1040. return array($current_table, $formatted_size, $unit, $sum_size);
  1041. }
  1042. }