DatabaseStructureController.class.php 39 KB

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