tbl_printview.lib.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions related to show table print view
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * return html for tables' info
  13. *
  14. * @param array $the_tables selected tables
  15. *
  16. * @return string
  17. */
  18. function PMA_getHtmlForTablesInfo($the_tables)
  19. {
  20. $html = '';
  21. $multi_tables = (count($the_tables) > 1);
  22. if ($multi_tables) {
  23. $tbl_list = '';
  24. foreach ($the_tables as $table) {
  25. $tbl_list .= (empty($tbl_list) ? '' : ', ')
  26. . PMA_Util::backquote($table);
  27. }
  28. $html .= '<strong>' . __('Showing tables:') . ' '
  29. . htmlspecialchars($tbl_list) . '</strong>' . "\n";
  30. $html .= '<hr />' . "\n";
  31. } // end if
  32. return $html;
  33. }
  34. /**
  35. * return html for print view footer
  36. *
  37. * @return string
  38. */
  39. function PMA_getHtmlForPrintViewFooter()
  40. {
  41. $html = PMA_Util::getButton();
  42. $html .= "<div id='PMA_disable_floating_menubar'></div>\n";
  43. return $html;
  44. }
  45. /**
  46. * return html for Print View Columns
  47. *
  48. * @param array $columns columns list
  49. * @param array $analyzed_sql analyzed sql
  50. * @param array $pk_array primary key array
  51. * @param bool $have_rel have relation?
  52. * @param array $res_rel relations array
  53. * @param string $db database name
  54. * @param string $table table name
  55. * @param array $cfgRelation config from PMA_getRelationsParam
  56. *
  57. * @return string
  58. */
  59. function PMA_getHtmlForPrintViewColumns(
  60. $columns, $analyzed_sql, $pk_array, $have_rel,
  61. $res_rel, $db, $table, $cfgRelation
  62. ) {
  63. $html = '';
  64. foreach ($columns as $row) {
  65. $extracted_columnspec = PMA_Util::extractColumnSpec($row['Type']);
  66. $type = $extracted_columnspec['print_type'];
  67. if (! isset($row['Default'])) {
  68. if ($row['Null'] != '' && $row['Null'] != 'NO') {
  69. $row['Default'] = '<i>NULL</i>';
  70. }
  71. } else {
  72. $row['Default'] = htmlspecialchars($row['Default']);
  73. }
  74. $field_name = htmlspecialchars($row['Field']);
  75. // here, we have a TIMESTAMP that SHOW FULL COLUMNS reports as having the
  76. // NULL attribute, but SHOW CREATE TABLE says the contrary. Believe
  77. // the latter.
  78. /**
  79. * @todo merge this logic with the one in tbl_structure.php
  80. * or move it in a function similar to $GLOBALS['dbi']->getColumnsFull()
  81. * but based on SHOW CREATE TABLE because information_schema
  82. * cannot be trusted in this case (MySQL bug)
  83. */
  84. $analyzed_for_field = $analyzed_sql[0]['create_table_fields'][$field_name];
  85. if (! empty($analyzed_for_field['type'])
  86. && $analyzed_for_field['type'] == 'TIMESTAMP'
  87. && $analyzed_for_field['timestamp_not_null']
  88. ) {
  89. $row['Null'] = '';
  90. }
  91. $html .= "\n";
  92. $html .= '<tr><td>';
  93. if (isset($pk_array[$row['Field']])) {
  94. $html .= ' <u>' . $field_name . '</u>' . "\n";
  95. } else {
  96. $html .= ' ' . $field_name . "\n";
  97. }
  98. $html .= '</td>';
  99. $html .= '<td>' . $type . '<bdo dir="ltr"></bdo></td>';
  100. $html .= '<td>';
  101. $html .= (($row['Null'] == '' || $row['Null'] == 'NO')
  102. ? __('No')
  103. : __('Yes'));
  104. $html .= '&nbsp;</td>';
  105. $html .= '<td>';
  106. if (isset($row['Default'])) {
  107. $html .= $row['Default'];
  108. }
  109. $html .= '&nbsp;</td>';
  110. if ($have_rel) {
  111. $html .= ' <td>';
  112. if (isset($res_rel[$field_name])) {
  113. $html .= htmlspecialchars(
  114. $res_rel[$field_name]['foreign_table']
  115. . ' -> ' . $res_rel[$field_name]['foreign_field']
  116. );
  117. }
  118. $html .= '&nbsp;</td>' . "\n";
  119. }
  120. $html .= ' <td>';
  121. $comments = PMA_getComments($db, $table);
  122. if (isset($comments[$field_name])) {
  123. $html .= htmlspecialchars($comments[$field_name]);
  124. }
  125. $html .= '&nbsp;</td>' . "\n";
  126. if ($cfgRelation['mimework']) {
  127. $mime_map = PMA_getMIME($db, $table, true);
  128. $html .= ' <td>';
  129. if (isset($mime_map[$field_name])) {
  130. $html .= htmlspecialchars(
  131. str_replace('_', '/', $mime_map[$field_name]['mimetype'])
  132. );
  133. }
  134. $html .= '&nbsp;</td>' . "\n";
  135. }
  136. $html .= '</tr>';
  137. } // end foreach
  138. return $html;
  139. }
  140. /**
  141. * return html for Row Statistic
  142. *
  143. * @param array $showtable showing table information
  144. * @param int $cell_align_left cell align left
  145. * @param int $avg_size avg size
  146. * @param int $avg_unit avg unit
  147. * @param bool $mergetable is merge table?
  148. *
  149. * @return string
  150. */
  151. function PMA_getHtmlForRowStatistics(
  152. $showtable, $cell_align_left, $avg_size, $avg_unit, $mergetable
  153. ) {
  154. $html = '<td width="20">&nbsp;</td>';
  155. // Rows Statistic
  156. $html .= "\n";
  157. $html .= '<td class="vtop">';
  158. $html .= '<big>' . __('Row Statistics:') . '</big>';
  159. $html .= '<table width="100%">';
  160. if (isset($showtable['Row_format'])) {
  161. $html .= "\n";
  162. $html .= '<tr>';
  163. $html .= '<td>' . __('Format') . '</td>';
  164. $html .= '<td class="' . $cell_align_left . '">';
  165. if ($showtable['Row_format'] == 'Fixed') {
  166. $html .= __('static');
  167. } elseif ($showtable['Row_format'] == 'Dynamic') {
  168. $html .= __('dynamic');
  169. } else {
  170. $html .= $showtable['Row_format'];
  171. }
  172. $html .= '</td>';
  173. $html .= '</tr>';
  174. }
  175. if (isset($showtable['Rows'])) {
  176. $html .= "\n";
  177. $html .= '<tr>';
  178. $html .= '<td>' . __('Rows') . '</td>';
  179. $html .= '<td class="right">';
  180. $html .= PMA_Util::formatNumber($showtable['Rows'], 0);
  181. $html .= '</td>';
  182. $html .= '</tr>';
  183. }
  184. if (isset($showtable['Avg_row_length'])
  185. && $showtable['Avg_row_length'] > 0
  186. ) {
  187. $html .= "\n";
  188. $html .= '<tr>';
  189. $html .= '<td>' . __('Row length') . '&nbsp;&oslash;</td>';
  190. $html .= '<td>';
  191. $html .= PMA_Util::formatNumber(
  192. $showtable['Avg_row_length'], 0
  193. );
  194. $html .= '</td>';
  195. $html .= '</tr>';
  196. }
  197. if (isset($showtable['Data_length'])
  198. && $showtable['Rows'] > 0
  199. && $mergetable == false
  200. ) {
  201. $html .= "\n";
  202. $html .= '<tr>';
  203. $html .= '<td>' . __('Row size') . '&nbsp;&oslash;</td>';
  204. $html .= '<td class="right">';
  205. $html .= $avg_size . ' ' . $avg_unit;
  206. $html .= '</td>';
  207. $html .= '</tr>';
  208. }
  209. if (isset($showtable['Auto_increment'])) {
  210. $html .= "\n";
  211. $html .= '<tr>';
  212. $html .= '<td>' . __('Next autoindex') . ' </td>';
  213. $html .= '<td class="right">';
  214. $html .= PMA_Util::formatNumber(
  215. $showtable['Auto_increment'], 0
  216. );
  217. $html .= '</td>';
  218. $html .= '</tr>';
  219. }
  220. if (isset($showtable['Create_time'])) {
  221. $html .= "\n";
  222. $html .= '<tr>';
  223. $html .= '<td>' . __('Creation') . '</td>';
  224. $html .= '<td class="right">';
  225. $html .= PMA_Util::localisedDate(
  226. strtotime($showtable['Create_time'])
  227. );
  228. $html .= '</td>';
  229. $html .= '</tr>';
  230. }
  231. if (isset($showtable['Update_time'])) {
  232. $html .= "\n";
  233. $html .= '<tr>';
  234. $html .= '<td>' . __('Last update') . '</td>';
  235. $html .= '<td class="right">';
  236. $html .= PMA_Util::localisedDate(
  237. strtotime($showtable['Update_time'])
  238. );
  239. $html .= '</td>';
  240. $html .= '</tr>';
  241. }
  242. if (isset($showtable['Check_time'])) {
  243. $html .= "\n";
  244. $html .= '<tr>';
  245. $html .= '<td>' . __('Last check') . '</td>';
  246. $html .= '<td class="right">';
  247. $html .= PMA_Util::localisedDate(
  248. strtotime($showtable['Check_time'])
  249. );
  250. $html .= '</td>';
  251. $html .= '</tr>';
  252. }
  253. return $html;
  254. }
  255. /**
  256. * return html for Space Usage
  257. *
  258. * @param int $data_size data size
  259. * @param int $data_unit data unit
  260. * @param int $index_size index size
  261. * @param int $index_unit index unit
  262. * @param int $free_size free size
  263. * @param int $free_unit free unit
  264. * @param int $effect_size effect size
  265. * @param int $effect_unit effect unit
  266. * @param int $tot_size total size
  267. * @param int $tot_unit total unit
  268. * @param bool $mergetable is merge table?
  269. *
  270. * @return string
  271. */
  272. function PMA_getHtmlForSpaceUsage(
  273. $data_size, $data_unit, $index_size, $index_unit,
  274. $free_size, $free_unit, $effect_size, $effect_unit,
  275. $tot_size, $tot_unit, $mergetable
  276. ) {
  277. $html = '<table cellspacing="0" cellpadding="0">';
  278. $html .= "\n";
  279. $html .= '<tr>';
  280. // Space usage
  281. $html .= '<td class="vtop">';
  282. $html .= '<big>' . __('Space usage:') . '</big>';
  283. $html .= '<table width="100%">';
  284. $html .= '<tr>';
  285. $html .= '<td style="padding-right: 10px">' . __('Data') . '</td>';
  286. $html .= '<td class="right">' . $data_size . '</td>';
  287. $html .= '<td>' . $data_unit . '</td>';
  288. $html .= '</tr>';
  289. if (isset($index_size)) {
  290. $html .= "\n";
  291. $html .= '<tr>';
  292. $html .= '<td style="padding-right: 10px">' . __('Index') . '</td>';
  293. $html .= '<td class="right">' . $index_size . '</td>';
  294. $html .= '<td>' . $index_unit . '</td>';
  295. $html .= '</tr>';
  296. }
  297. if (isset($free_size)) {
  298. $html .= "\n";
  299. $html .= '<tr style="color: #bb0000">';
  300. $html .= '<td style="padding-right: 10px">';
  301. $html .= __('Overhead');
  302. $html .= '</td>';
  303. $html .= '<td class="right">' . $free_size . '</td>';
  304. $html .= '<td>' . $free_unit . '</td>';
  305. $html .= '</tr>';
  306. $html .= '<tr>';
  307. $html .= '<td style="padding-right: 10px">';
  308. $html .= __('Effective');
  309. $html .= '</td>';
  310. $html .= '<td class="right">' . $effect_size . '</td>';
  311. $html .= '<td>' . $effect_unit . '</td>';
  312. $html .= '</tr>';
  313. }
  314. if (isset($tot_size) && $mergetable == false) {
  315. $html .= "\n";
  316. $html .= '<tr>';
  317. $html .= '<td style="padding-right: 10px">' . __('Total') . '</td>';
  318. $html .= '<td class="right">' . $tot_size . '</td>';
  319. $html .= '<td>' . $tot_unit . '</td>';
  320. $html .= '</tr>';
  321. }
  322. $html .= "\n";
  323. $html .= '</table>';
  324. return $html;
  325. }
  326. /**
  327. * return html for Space Usage And Row Statistic
  328. *
  329. * @param array $showtable showing table information
  330. * @param string $db database
  331. * @param string $table table
  332. * @param int $cell_align_left cell align left
  333. *
  334. * @return string
  335. */
  336. function PMA_getHtmlForSpaceUsageAndRowStatistics(
  337. $showtable, $db, $table, $cell_align_left
  338. ) {
  339. $html = '';
  340. $nonisam = false;
  341. if (isset($showtable['Type'])
  342. && ! preg_match('@ISAM|HEAP@i', $showtable['Type'])
  343. ) {
  344. $nonisam = true;
  345. }
  346. if ($nonisam == false) {
  347. // Gets some sizes
  348. $mergetable = PMA_Table::isMerge($db, $table);
  349. list($data_size, $data_unit) = PMA_Util::formatByteDown(
  350. $showtable['Data_length']
  351. );
  352. if ($mergetable == false) {
  353. list($index_size, $index_unit)
  354. = PMA_Util::formatByteDown(
  355. $showtable['Index_length']
  356. );
  357. }
  358. if (isset($showtable['Data_free']) && $showtable['Data_free'] > 0) {
  359. list($free_size, $free_unit)
  360. = PMA_Util::formatByteDown(
  361. $showtable['Data_free']
  362. );
  363. list($effect_size, $effect_unit)
  364. = PMA_Util::formatByteDown(
  365. $showtable['Data_length'] + $showtable['Index_length']
  366. - $showtable['Data_free']
  367. );
  368. } else {
  369. unset($free_size);
  370. unset($free_unit);
  371. list($effect_size, $effect_unit)
  372. = PMA_Util::formatByteDown(
  373. $showtable['Data_length'] + $showtable['Index_length']
  374. );
  375. }
  376. list($tot_size, $tot_unit) = PMA_Util::formatByteDown(
  377. $showtable['Data_length'] + $showtable['Index_length']
  378. );
  379. $num_rows = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
  380. if ($num_rows > 0) {
  381. list($avg_size, $avg_unit)
  382. = PMA_Util::formatByteDown(
  383. ($showtable['Data_length'] + $showtable['Index_length'])
  384. / $showtable['Rows'],
  385. 6,
  386. 1
  387. );
  388. }
  389. // Displays them
  390. $html .= '<br /><br />';
  391. $html .= PMA_getHtmlForSpaceUsage(
  392. $data_size, $data_unit,
  393. isset($index_size)? $index_size : null,
  394. isset($index_unit)? $index_unit : null,
  395. isset($free_size)? $free_size : null,
  396. isset($free_unit)? $free_unit : null,
  397. isset($effect_size)? $effect_size : null,
  398. isset($effect_unit)? $effect_unit : null,
  399. isset($tot_size)? $tot_size : null,
  400. isset($tot_unit)? $tot_unit : null,
  401. $mergetable
  402. );
  403. $html .= '</td>';
  404. $html .= PMA_getHtmlForRowStatistics(
  405. $showtable, $cell_align_left,
  406. isset($avg_size)? $avg_size: 0,
  407. isset($avg_unit)? $avg_unit: 0,
  408. $mergetable
  409. );
  410. $html .= "\n";
  411. $html .= '</table>';
  412. $html .= '</td>';
  413. $html .= '</tr>';
  414. $html .= '</table>';
  415. } // end if ($nonisam == false)
  416. return $html;
  417. }
  418. /**
  419. * return html for Table Structure
  420. *
  421. * @param bool $have_rel whether have relation
  422. * @param array $tbl_is_view Is a table view?
  423. * @param array $columns columns list
  424. * @param array $analyzed_sql analyzed sql
  425. * @param array $pk_array primary key array
  426. * @param array $res_rel relations array
  427. * @param string $db database
  428. * @param string $table table
  429. * @param array $cfgRelation config from PMA_getRelationsParam
  430. * @param array $cfg global config
  431. * @param array $showtable showing table information
  432. * @param int $cell_align_left cell align left
  433. *
  434. * @return string
  435. */
  436. function PMA_getHtmlForTableStructure(
  437. $have_rel, $tbl_is_view, $columns, $analyzed_sql,
  438. $pk_array, $res_rel, $db, $table, $cfgRelation,
  439. $cfg, $showtable, $cell_align_left
  440. ) {
  441. /**
  442. * Displays the table structure
  443. */
  444. $html = '<table style="width: 100%;">';
  445. $html .= '<thead>';
  446. $html .= '<tr>';
  447. $html .= '<th>' . __('Column') . '</th>';
  448. $html .= '<th>' . __('Type') . '</th>';
  449. $html .= '<th>' . __('Null') . '</th>';
  450. $html .= '<th>' . __('Default') . '</th>';
  451. if ($have_rel) {
  452. $html .= '<th>' . __('Links to') . '</th>' . "\n";
  453. }
  454. $html .= ' <th>' . __('Comments') . '</th>' . "\n";
  455. if ($cfgRelation['mimework']) {
  456. $html .= ' <th>MIME</th>' . "\n";
  457. }
  458. $html .= '</tr>';
  459. $html .= '</thead>';
  460. $html .= '<tbody>';
  461. $html .= PMA_getHtmlForPrintViewColumns(
  462. $columns, $analyzed_sql, $pk_array, $have_rel,
  463. $res_rel, $db, $table, $cfgRelation
  464. );
  465. $html .= '</tbody>';
  466. $html .= '</table>';
  467. if (! $tbl_is_view && !$GLOBALS['dbi']->isSystemSchema($db)) {
  468. /**
  469. * Displays indexes
  470. */
  471. $html .= PMA_Index::getView($table, $db, true);
  472. /**
  473. * Displays Space usage and row statistics
  474. *
  475. */
  476. if ($cfg['ShowStats']) {
  477. $html .= PMA_getHtmlForSpaceUsageAndRowStatistics(
  478. $showtable, $db, $table, $cell_align_left
  479. );
  480. } // end if ($cfg['ShowStats'])
  481. }
  482. return $html;
  483. }
  484. /**
  485. * return html for tables' detail
  486. *
  487. * @param array $the_tables tables list
  488. * @param string $db database name
  489. * @param array $cfg global config
  490. * @param array $cfgRelation config from PMA_getRelationsParam
  491. * @param array $pk_array primary key array
  492. * @param int $cell_align_left cell align left
  493. *
  494. * @return string
  495. */
  496. function PMA_getHtmlForTablesDetail(
  497. $the_tables, $db, $cfg, $cfgRelation, $pk_array, $cell_align_left
  498. ) {
  499. $html = '';
  500. $tables_cnt = count($the_tables);
  501. $multi_tables = (count($the_tables) > 1);
  502. $counter = 0;
  503. foreach ($the_tables as $table) {
  504. if ($counter + 1 >= $tables_cnt) {
  505. $breakstyle = '';
  506. } else {
  507. $breakstyle = ' style="page-break-after: always;"';
  508. }
  509. $counter++;
  510. $html .= '<div' . $breakstyle . '>' . "\n";
  511. $html .= '<h1>' . htmlspecialchars($table) . '</h1>' . "\n";
  512. /**
  513. * Gets table informations
  514. */
  515. $showtable = PMA_Table::sGetStatusInfo($db, $table);
  516. $num_rows = (isset($showtable['Rows']) ? $showtable['Rows'] : 0);
  517. $show_comment = (isset($showtable['Comment']) ? $showtable['Comment'] : '');
  518. $tbl_is_view = PMA_Table::isView($db, $table);
  519. /**
  520. * Gets fields properties
  521. */
  522. $columns = $GLOBALS['dbi']->getColumns($db, $table);
  523. // We need this to correctly learn if a TIMESTAMP is NOT NULL, since
  524. // SHOW FULL FIELDS or INFORMATION_SCHEMA incorrectly says NULL
  525. // and SHOW CREATE TABLE says NOT NULL (tested
  526. // in MySQL 4.0.25 and 5.0.21, http://bugs.mysql.com/20910).
  527. $show_create_table = $GLOBALS['dbi']->fetchValue(
  528. 'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.'
  529. . PMA_Util::backquote($table),
  530. 0, 1
  531. );
  532. $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
  533. // Check if we can use Relations
  534. // Find which tables are related with the current one and write it in
  535. // an array
  536. $res_rel = PMA_getForeigners($db, $table);
  537. $have_rel = (bool) count($res_rel);
  538. /**
  539. * Displays the comments of the table if MySQL >= 3.23
  540. */
  541. if (!empty($show_comment)) {
  542. $html .= __('Table comments:') . ' '
  543. . htmlspecialchars($show_comment) . '<br /><br />';
  544. }
  545. $html .= PMA_getHtmlForTableStructure(
  546. $have_rel, $tbl_is_view, $columns, $analyzed_sql,
  547. $pk_array, $res_rel, $db, $table, $cfgRelation,
  548. $cfg, $showtable, $cell_align_left
  549. );
  550. if ($multi_tables) {
  551. unset($num_rows, $show_comment);
  552. $html .= '<hr />' . "\n";
  553. } // end if
  554. $html .= '</div>' . "\n";
  555. } // end while
  556. return $html;
  557. }
  558. ?>