structure.lib.php 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * set of functions for structure section in pma
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (!defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Get the HTML links for action links
  13. * Actions are, Browse, Search, Browse table label, empty table
  14. *
  15. * @param array $current_table current table
  16. * @param boolean $table_is_view Is table view or not
  17. * @param string $tbl_url_query table url query
  18. * @param array $titles titles and icons for action links
  19. * @param string $truename table name
  20. * @param boolean $db_is_system_schema is database information schema or not
  21. * @param string $url_query url query
  22. *
  23. * @return array ($browse_table, $search_table, $browse_table_label, $empty_table,
  24. * $tracking_icon)
  25. */
  26. function PMA_getHtmlForActionLinks($current_table, $table_is_view, $tbl_url_query,
  27. $titles, $truename, $db_is_system_schema, $url_query
  28. ) {
  29. $empty_table = '';
  30. if ($current_table['TABLE_ROWS'] > 0 || $table_is_view) {
  31. $may_have_rows = true;
  32. } else {
  33. $may_have_rows = false;
  34. }
  35. $browse_table = '<a href="sql.php?' . $tbl_url_query . '&amp;pos=0">';
  36. if ($may_have_rows) {
  37. $browse_table .= $titles['Browse'];
  38. } else {
  39. $browse_table .= $titles['NoBrowse'];
  40. }
  41. $browse_table .= '</a>';
  42. $search_table = '<a href="tbl_select.php?' . $tbl_url_query . '">';
  43. if ($may_have_rows) {
  44. $search_table .= $titles['Search'];
  45. } else {
  46. $search_table .= $titles['NoSearch'];
  47. }
  48. $search_table .= '</a>';
  49. $browse_table_label = '<a href="sql.php?' . $tbl_url_query
  50. . '&amp;pos=0" title="'
  51. . htmlspecialchars($current_table['TABLE_COMMENT']) . '">'
  52. . $truename . '</a>';
  53. if (!$db_is_system_schema) {
  54. $empty_table = '<a class="truncate_table_anchor ajax"';
  55. $empty_table .= ' href="sql.php?' . $tbl_url_query
  56. . '&amp;sql_query=';
  57. $empty_table .= urlencode(
  58. 'TRUNCATE ' . PMA_Util::backquote($current_table['TABLE_NAME'])
  59. );
  60. $empty_table .= '&amp;message_to_show='
  61. . urlencode(
  62. sprintf(
  63. __('Table %s has been emptied.'),
  64. htmlspecialchars($current_table['TABLE_NAME'])
  65. )
  66. )
  67. . '">';
  68. if ($may_have_rows) {
  69. $empty_table .= $titles['Empty'];
  70. } else {
  71. $empty_table .= $titles['NoEmpty'];
  72. }
  73. $empty_table .= '</a>';
  74. // truncating views doesn't work
  75. if ($table_is_view) {
  76. $empty_table = '&nbsp;';
  77. }
  78. }
  79. $tracking_icon = '';
  80. if (PMA_Tracker::isActive()) {
  81. if (PMA_Tracker::isTracked($GLOBALS["db"], $truename)) {
  82. $tracking_icon = '<a href="tbl_tracking.php?' . $url_query
  83. . '&amp;table=' . $truename . '">'
  84. . PMA_Util::getImage(
  85. 'eye.png', __('Tracking is active.')
  86. )
  87. . '</a>';
  88. } elseif (PMA_Tracker::getVersion($GLOBALS["db"], $truename) > 0) {
  89. $tracking_icon = '<a href="tbl_tracking.php?' . $url_query
  90. . '&amp;table=' . $truename . '">'
  91. . PMA_Util::getImage(
  92. 'eye_grey.png', __('Tracking is not active.')
  93. )
  94. . '</a>';
  95. }
  96. }
  97. return array($browse_table,
  98. $search_table,
  99. $browse_table_label,
  100. $empty_table,
  101. $tracking_icon
  102. );
  103. }
  104. /**
  105. * Get table drop query and drop message
  106. *
  107. * @param boolean $table_is_view Is table view or not
  108. * @param string $current_table current table
  109. *
  110. * @return array ($drop_query, $drop_message)
  111. */
  112. function PMA_getTableDropQueryAndMessage($table_is_view, $current_table)
  113. {
  114. $drop_query = 'DROP '
  115. . (($table_is_view || $current_table['ENGINE'] == null) ? 'VIEW' : 'TABLE')
  116. . ' ' . PMA_Util::backquote(
  117. $current_table['TABLE_NAME']
  118. );
  119. $drop_message = sprintf(
  120. (($table_is_view || $current_table['ENGINE'] == null)
  121. ? __('View %s has been dropped.')
  122. : __('Table %s has been dropped.')),
  123. str_replace(
  124. ' ',
  125. '&nbsp;',
  126. htmlspecialchars($current_table['TABLE_NAME'])
  127. )
  128. );
  129. return array($drop_query, $drop_message);
  130. }
  131. /**
  132. * Get HTML body for table summery
  133. *
  134. * @param integer $num_tables number of tables
  135. * @param boolean $server_slave_status server slave state
  136. * @param boolean $db_is_system_schema whether database is information schema or not
  137. * @param integer $sum_entries sum entries
  138. * @param string $db_collation collation of given db
  139. * @param boolean $is_show_stats whether stats is show or not
  140. * @param double $sum_size sum size
  141. * @param double $overhead_size overhead size
  142. * @param string $create_time_all create time
  143. * @param string $update_time_all update time
  144. * @param string $check_time_all check time
  145. * @param integer $sum_row_count_pre sum row count pre
  146. *
  147. * @return string $html_output
  148. */
  149. function PMA_getHtmlBodyForTableSummary($num_tables, $server_slave_status,
  150. $db_is_system_schema, $sum_entries, $db_collation, $is_show_stats,
  151. $sum_size, $overhead_size, $create_time_all, $update_time_all,
  152. $check_time_all, $sum_row_count_pre
  153. ) {
  154. if ($is_show_stats) {
  155. list($sum_formatted, $unit) = PMA_Util::formatByteDown(
  156. $sum_size, 3, 1
  157. );
  158. list($overhead_formatted, $overhead_unit)
  159. = PMA_Util::formatByteDown($overhead_size, 3, 1);
  160. }
  161. $html_output = '<tbody id="tbl_summary_row">'
  162. . '<tr><th></th>';
  163. $html_output .= '<th class="tbl_num nowrap">';
  164. $html_output .= sprintf(
  165. _ngettext('%s table', '%s tables', $num_tables),
  166. PMA_Util::formatNumber($num_tables, 0)
  167. );
  168. $html_output .= '</th>';
  169. if ($server_slave_status) {
  170. $html_output .= '<th>' . __('Replication') . '</th>' . "\n";
  171. }
  172. $html_output .= '<th colspan="' . ($db_is_system_schema ? 4 : 7) . '">'
  173. . __('Sum')
  174. . '</th>';
  175. $html_output .= '<th class="value tbl_rows">'
  176. . $sum_row_count_pre . PMA_Util::formatNumber($sum_entries, 0)
  177. . '</th>';
  178. if (!($GLOBALS['cfg']['PropertiesNumColumns'] > 1)) {
  179. $default_engine = $GLOBALS['dbi']->fetchValue(
  180. 'SHOW VARIABLES LIKE \'storage_engine\';',
  181. 0,
  182. 1
  183. );
  184. $html_output .= '<th class="center">' . "\n"
  185. . '<dfn title="'
  186. . sprintf(
  187. __('%s is the default storage engine on this MySQL server.'),
  188. $default_engine
  189. )
  190. . '">' . $default_engine . '</dfn></th>' . "\n";
  191. // we got a case where $db_collation was empty
  192. $html_output .= '<th>' . "\n";
  193. if (! empty($db_collation)) {
  194. $html_output .= '<dfn title="'
  195. . PMA_getCollationDescr($db_collation)
  196. . ' (' . __('Default') . ')">'
  197. . $db_collation
  198. . '</dfn>';
  199. }
  200. $html_output .= '</th>';
  201. }
  202. if ($is_show_stats) {
  203. $html_output .= '<th class="value tbl_size">'
  204. . $sum_formatted . ' ' . $unit
  205. . '</th>';
  206. $html_output .= '<th class="value tbl_overhead">'
  207. . $overhead_formatted . ' ' . $overhead_unit
  208. . '</th>';
  209. }
  210. if ($GLOBALS['cfg']['ShowDbStructureCreation']) {
  211. $html_output .= '<th class="value tbl_creation">' . "\n"
  212. . ' '
  213. . ($create_time_all
  214. ? PMA_Util::localisedDate(strtotime($create_time_all))
  215. : '-'
  216. )
  217. . '</th>';
  218. }
  219. if ($GLOBALS['cfg']['ShowDbStructureLastUpdate']) {
  220. $html_output .= '<th class="value tbl_last_update">' . "\n"
  221. . ' '
  222. . ($update_time_all
  223. ? PMA_Util::localisedDate(strtotime($update_time_all))
  224. : '-'
  225. )
  226. . '</th>';
  227. }
  228. if ($GLOBALS['cfg']['ShowDbStructureLastCheck']) {
  229. $html_output .= '<th class="value tbl_last_check">' . "\n"
  230. . ' '
  231. . ($check_time_all
  232. ? PMA_Util::localisedDate(strtotime($check_time_all))
  233. : '-'
  234. )
  235. . '</th>';
  236. }
  237. $html_output .= '</tr>'
  238. . '</tbody>';
  239. return $html_output;
  240. }
  241. /**
  242. * Get HTML for "check all" check box with "with selected" dropdown
  243. *
  244. * @param string $pmaThemeImage pma theme image url
  245. * @param string $text_dir url for text directory
  246. * @param string $overhead_check overhead check
  247. * @param boolean $db_is_system_schema whether database is information schema or not
  248. * @param array $hidden_fields hidden fields
  249. *
  250. * @return string $html_output
  251. */
  252. function PMA_getHtmlForCheckAllTables($pmaThemeImage, $text_dir,
  253. $overhead_check, $db_is_system_schema, $hidden_fields
  254. ) {
  255. $html_output = '<div class="clearfloat">';
  256. $html_output .= '<img class="selectallarrow" '
  257. . 'src="' . $pmaThemeImage . 'arrow_' . $text_dir . '.png" '
  258. . 'width="38" height="22" alt="' . __('With selected:') . '" />';
  259. $html_output .= '<input type="checkbox" id="tablesForm_checkall" '
  260. . 'class="checkall_box" title="' . __('Check All') . '" />';
  261. $html_output .= '<label for="tablesForm_checkall">' . __('Check All')
  262. . '</label>';
  263. if ($overhead_check != '') {
  264. $html_output .= PMA_getHtmlForCheckTablesHavingOverheadlink(
  265. $overhead_check
  266. );
  267. }
  268. $html_output .= '<select name="submit_mult" class="autosubmit" '
  269. . 'style="margin: 0 3em 0 3em;">';
  270. $html_output .= '<option value="' . __('With selected:')
  271. . '" selected="selected">'
  272. . __('With selected:') . '</option>' . "\n";
  273. $html_output .= '<option value="export" >'
  274. . __('Export') . '</option>' . "\n";
  275. $html_output .= '<option value="print" >'
  276. . __('Print view') . '</option>' . "\n";
  277. if (!$db_is_system_schema
  278. && !$GLOBALS['cfg']['DisableMultiTableMaintenance']
  279. ) {
  280. $html_output .= '<option value="empty_tbl" >'
  281. . __('Empty') . '</option>' . "\n";
  282. $html_output .= '<option value="drop_tbl" >'
  283. . __('Drop') . '</option>' . "\n";
  284. $html_output .= '<option value="check_tbl" >'
  285. . __('Check table') . '</option>' . "\n";
  286. if (!PMA_DRIZZLE) {
  287. $html_output .= '<option value="optimize_tbl" >'
  288. . __('Optimize table') . '</option>' . "\n";
  289. $html_output .= '<option value="repair_tbl" >'
  290. . __('Repair table') . '</option>' . "\n";
  291. }
  292. $html_output .= '<option value="analyze_tbl" >'
  293. . __('Analyze table') . '</option>' . "\n";
  294. $html_output .= '<option value="add_prefix_tbl" >'
  295. . __('Add prefix to table') . '</option>' . "\n";
  296. $html_output .= '<option value="replace_prefix_tbl" >'
  297. . __('Replace table prefix') . '</option>' . "\n";
  298. $html_output .= '<option value="copy_tbl_change_prefix" >'
  299. . __('Copy table with prefix') . '</option>' . "\n";
  300. }
  301. $html_output .= '</select>'
  302. . implode("\n", $hidden_fields) . "\n";
  303. $html_output .= '</div>';
  304. return $html_output;
  305. }
  306. /**
  307. * Get HTML code for "Check tables having overhead" link
  308. *
  309. * @param string $overhead_check overhead check
  310. *
  311. * @return string $html_output
  312. */
  313. function PMA_getHtmlForCheckTablesHavingOverheadlink($overhead_check)
  314. {
  315. return ' / '
  316. . '<a href="#" onclick="unMarkAllRows(\'tablesForm\');'
  317. . $overhead_check . 'return false;">'
  318. . __('Check tables having overhead')
  319. . '</a>';
  320. }
  321. /**
  322. * Get HTML links for "Print view" options
  323. *
  324. * @param string $url_query url query
  325. *
  326. * @return string $html_output
  327. */
  328. function PMA_getHtmlForTablePrintViewLink($url_query)
  329. {
  330. return '<p>'
  331. . '<a href="db_printview.php?' . $url_query . '" target="print_view">'
  332. . PMA_Util::getIcon(
  333. 'b_print.png',
  334. __('Print view'),
  335. true
  336. ) . '</a>';
  337. }
  338. /**
  339. * Get HTML links "Data Dictionary" options
  340. *
  341. * @param string $url_query url query
  342. *
  343. * @return string $html_output
  344. */
  345. function PMA_getHtmlForDataDictionaryLink($url_query)
  346. {
  347. return '<a href="db_datadict.php?' . $url_query . '" target="print_view">'
  348. . PMA_Util::getIcon(
  349. 'b_tblanalyse.png',
  350. __('Data Dictionary'),
  351. true
  352. ) . '</a>'
  353. . '</p>';
  354. }
  355. /**
  356. * Get Time for Create time, update time and check time
  357. *
  358. * @param array $current_table current table
  359. * @param string $time_label Create_time, Update_time, Check_time
  360. * @param integer $time_all time
  361. *
  362. * @return array ($time, $time_all)
  363. */
  364. function PMA_getTimeForCreateUpdateCheck($current_table, $time_label, $time_all)
  365. {
  366. $showtable = PMA_Table::sGetStatusInfo(
  367. $GLOBALS['db'],
  368. $current_table['TABLE_NAME'],
  369. null,
  370. true
  371. );
  372. $time = isset($showtable[$time_label])
  373. ? $showtable[$time_label]
  374. : false;
  375. // show oldest creation date in summary row
  376. if ($time && (!$time_all || $time < $time_all)) {
  377. $time_all = $time;
  378. }
  379. return array($time, $time_all);
  380. }
  381. /**
  382. * Get HTML for each table row of the database structure table,
  383. * And this function returns $odd_row param also
  384. *
  385. * @param integer $curr current entry
  386. * @param boolean $odd_row whether row is odd or not
  387. * @param boolean $table_is_view whether table is view or not
  388. * @param array $current_table current table
  389. * @param string $browse_table_label browse table label action link
  390. * @param string $tracking_icon tracking icon
  391. * @param boolean $server_slave_status server slave state
  392. * @param string $browse_table browse table action link
  393. * @param string $tbl_url_query table url query
  394. * @param string $search_table search table action link
  395. * @param boolean $db_is_system_schema whether db is information schema or not
  396. * @param array $titles titles array
  397. * @param string $empty_table empty table action link
  398. * @param string $drop_query table dropt query
  399. * @param string $drop_message table drop message
  400. * @param string $collation collation
  401. * @param string $formatted_size formatted size
  402. * @param string $unit unit
  403. * @param string $overhead overhead
  404. * @param string $create_time create time
  405. * @param string $update_time last update time
  406. * @param string $check_time last check time
  407. * @param boolean $is_show_stats whether stats is show or not
  408. * @param boolean $ignored ignored
  409. * @param boolean $do do
  410. * @param integer $colspan_for_structure colspan for structure
  411. *
  412. * @return array $html_output, $odd_row
  413. */
  414. function PMA_getHtmlForStructureTableRow(
  415. $curr, $odd_row, $table_is_view, $current_table,
  416. $browse_table_label, $tracking_icon,$server_slave_status,
  417. $browse_table, $tbl_url_query, $search_table,
  418. $db_is_system_schema,$titles, $empty_table, $drop_query, $drop_message,
  419. $collation, $formatted_size, $unit, $overhead, $create_time, $update_time,
  420. $check_time,$is_show_stats, $ignored, $do, $colspan_for_structure
  421. ) {
  422. global $db;
  423. $html_output = '<tr class="' . ($odd_row ? 'odd' : 'even');
  424. $odd_row = ! $odd_row;
  425. $html_output .= ($table_is_view ? ' is_view' : '')
  426. . '" id="row_tbl_' . $curr . '">';
  427. $html_output .= '<td class="center">'
  428. . '<input type="checkbox" name="selected_tbl[]" class="checkall" '
  429. . 'value="' . htmlspecialchars($current_table['TABLE_NAME']) . '" '
  430. . 'id="checkbox_tbl_' . $curr . '" /></td>';
  431. $html_output .= '<th>'
  432. . $browse_table_label
  433. . (! empty($tracking_icon) ? $tracking_icon : '')
  434. . '</th>';
  435. if ($server_slave_status) {
  436. $html_output .= '<td class="center">'
  437. . ($ignored
  438. ? PMA_Util::getImage('s_cancel.png', 'NOT REPLICATED')
  439. : '')
  440. . ($do
  441. ? PMA_Util::getImage('s_success.png', 'REPLICATED')
  442. : '')
  443. . '</td>';
  444. }
  445. //Favorite table anchor.
  446. $html_output .= '<td class="center">'
  447. . PMA_getHtmlForFavoriteAnchor($db, $current_table['TABLE_NAME'], $titles)
  448. . '</td>';
  449. $html_output .= '<td class="center">' . $browse_table . '</td>';
  450. $html_output .= '<td class="center">'
  451. . '<a href="tbl_structure.php?' . $tbl_url_query . '">'
  452. . $titles['Structure'] . '</a></td>';
  453. $html_output .= '<td class="center">' . $search_table . '</td>';
  454. if (! $db_is_system_schema) {
  455. $html_output .= PMA_getHtmlForInsertEmptyDropActionLinks(
  456. $tbl_url_query, $table_is_view,
  457. $titles, $empty_table, $current_table, $drop_query, $drop_message
  458. );
  459. } // end if (! $db_is_system_schema)
  460. // there is a null value in the ENGINE
  461. // - when the table needs to be repaired, or
  462. // - when it's a view
  463. // so ensure that we'll display "in use" below for a table
  464. // that needs to be repaired
  465. if (isset($current_table['TABLE_ROWS'])
  466. && ($current_table['ENGINE'] != null
  467. || $table_is_view)
  468. ) {
  469. $html_output .= PMA_getHtmlForNotNullEngineViewTable(
  470. $table_is_view, $current_table, $collation, $is_show_stats,
  471. $tbl_url_query, $formatted_size, $unit, $overhead, $create_time,
  472. $update_time, $check_time
  473. );
  474. } elseif ($table_is_view) {
  475. $html_output .= PMA_getHtmlForViewTable($is_show_stats);
  476. } else {
  477. $html_output .= PMA_getHtmlForRepairtable(
  478. $colspan_for_structure,
  479. $db_is_system_schema
  480. );
  481. } // end if (isset($current_table['TABLE_ROWS'])) else
  482. $html_output .= '</tr>';
  483. return array($html_output, $odd_row);
  484. }
  485. /**
  486. * Get HTML for Insert/Empty/Drop action links
  487. *
  488. * @param string $tbl_url_query table url query
  489. * @param boolean $table_is_view whether table is view or not
  490. * @param array $titles titles array
  491. * @param string $empty_table HTML link for empty table
  492. * @param array $current_table current table
  493. * @param string $drop_query query for drop table
  494. * @param string $drop_message table drop message
  495. *
  496. * @return string $html_output
  497. */
  498. function PMA_getHtmlForInsertEmptyDropActionLinks($tbl_url_query, $table_is_view,
  499. $titles, $empty_table, $current_table, $drop_query, $drop_message
  500. ) {
  501. $html_output = '<td class="insert_table center">'
  502. . '<a href="tbl_change.php?' . $tbl_url_query . '">'
  503. . $titles['Insert']
  504. . '</a></td>';
  505. $html_output .= '<td class="center">' . $empty_table . '</td>';
  506. $html_output .= '<td class="center">';
  507. $html_output .= '<a ';
  508. $html_output .= 'class="ajax drop_table_anchor';
  509. if ($table_is_view || $current_table['ENGINE'] == null) {
  510. // this class is used in db_structure.js to display the
  511. // correct confirmation message
  512. $html_output .= ' view';
  513. }
  514. $html_output .= '" ';
  515. $html_output .= 'href="sql.php?' . $tbl_url_query
  516. . '&amp;reload=1&amp;purge=1&amp;sql_query='
  517. . urlencode($drop_query) . '&amp;message_to_show='
  518. . urlencode($drop_message) . '" >'
  519. . $titles['Drop'] . '</a></td>';
  520. return $html_output;
  521. }
  522. /**
  523. * Get HTML for show stats
  524. *
  525. * @param string $tbl_url_query tabel url query
  526. * @param string $formatted_size formatted size
  527. * @param string $unit unit
  528. * @param string $overhead overhead
  529. *
  530. * @return string $html_output
  531. */
  532. function PMA_getHtmlForShowStats($tbl_url_query, $formatted_size,
  533. $unit, $overhead
  534. ) {
  535. $html_output = '<td class="value tbl_size"><a '
  536. . 'href="tbl_structure.php?' . $tbl_url_query . '#showusage" >'
  537. . '<span>' . $formatted_size . '</span> '
  538. . '<span class="unit">' . $unit . '</span>'
  539. . '</a></td>';
  540. $html_output .= '<td class="value tbl_overhead">' . $overhead . '</td>';
  541. return $html_output;
  542. }
  543. /**
  544. * Get HTML to show database structure creation, last update and last checkx time
  545. *
  546. * @param string $create_time create time
  547. * @param string $update_time last update time
  548. * @param string $check_time last check time
  549. *
  550. * @return string $html_output
  551. */
  552. function PMA_getHtmlForStructureTimes($create_time, $update_time, $check_time)
  553. {
  554. $html_output = '';
  555. if ($GLOBALS['cfg']['ShowDbStructureCreation']) {
  556. $html_output .= '<td class="value tbl_creation">'
  557. . ($create_time
  558. ? PMA_Util::localisedDate(strtotime($create_time))
  559. : '-' )
  560. . '</td>';
  561. } // end if
  562. if ($GLOBALS['cfg']['ShowDbStructureLastUpdate']) {
  563. $html_output .= '<td class="value tbl_last_update">'
  564. . ($update_time
  565. ? PMA_Util::localisedDate(strtotime($update_time))
  566. : '-' )
  567. . '</td>';
  568. } // end if
  569. if ($GLOBALS['cfg']['ShowDbStructureLastCheck']) {
  570. $html_output .= '<td class="value tbl_last_check">'
  571. . ($check_time
  572. ? PMA_Util::localisedDate(strtotime($check_time))
  573. : '-' )
  574. . '</td>';
  575. }
  576. return $html_output;
  577. }
  578. /**
  579. * Get HTML for ENGINE value not null or view tables that are not empty tables
  580. *
  581. * @param boolean $table_is_view whether table is view
  582. * @param array $current_table current table
  583. * @param string $collation collation
  584. * @param boolean $is_show_stats whether atats show or not
  585. * @param string $tbl_url_query table url query
  586. * @param string $formatted_size formatted size
  587. * @param string $unit unit
  588. * @param string $overhead overhead
  589. * @param string $create_time create time
  590. * @param string $update_time update time
  591. * @param string $check_time check time
  592. *
  593. * @return string $html_output
  594. */
  595. function PMA_getHtmlForNotNullEngineViewTable($table_is_view, $current_table,
  596. $collation, $is_show_stats, $tbl_url_query, $formatted_size, $unit,
  597. $overhead, $create_time, $update_time, $check_time
  598. ) {
  599. $html_output = '';
  600. $row_count_pre = '';
  601. $show_superscript = '';
  602. if ($table_is_view) {
  603. // Drizzle views use FunctionEngine, and the only place where they are
  604. // available are I_S and D_D schemas, where we do exact counting
  605. if ($current_table['TABLE_ROWS'] >= $GLOBALS['cfg']['MaxExactCountViews']
  606. && $current_table['ENGINE'] != 'FunctionEngine'
  607. ) {
  608. $row_count_pre = '~';
  609. $sum_row_count_pre = '~';
  610. $show_superscript = PMA_Util::showHint(
  611. PMA_sanitize(
  612. sprintf(
  613. __('This view has at least this number of rows. Please refer to %sdocumentation%s.'),
  614. '[doc@cfg_MaxExactCountViews]',
  615. '[/doc]'
  616. )
  617. )
  618. );
  619. }
  620. } elseif ($current_table['ENGINE'] == 'InnoDB'
  621. && (! $current_table['COUNTED'])
  622. ) {
  623. // InnoDB table: we did not get an accurate row count
  624. $row_count_pre = '~';
  625. $sum_row_count_pre = '~';
  626. $show_superscript = '';
  627. }
  628. $html_output .= '<td class="value tbl_rows">'
  629. . $row_count_pre . PMA_Util::formatNumber(
  630. $current_table['TABLE_ROWS'], 0
  631. )
  632. . $show_superscript . '</td>';
  633. if (!($GLOBALS['cfg']['PropertiesNumColumns'] > 1)) {
  634. $html_output .= '<td class="nowrap">'
  635. . ($table_is_view ? __('View') : $current_table['ENGINE'])
  636. . '</td>';
  637. if (strlen($collation)) {
  638. $html_output .= '<td class="nowrap">' . $collation . '</td>';
  639. }
  640. }
  641. if ($is_show_stats) {
  642. $html_output .= PMA_getHtmlForShowStats(
  643. $tbl_url_query, $formatted_size, $unit, $overhead
  644. );
  645. }
  646. $html_output .= PMA_getHtmlForStructureTimes(
  647. $create_time, $update_time, $check_time
  648. );
  649. return $html_output;
  650. }
  651. /**
  652. * Get HTML snippet view table
  653. *
  654. * @param boolean $is_show_stats whether stats show or not
  655. *
  656. * @return string $html_output
  657. */
  658. function PMA_getHtmlForViewTable($is_show_stats)
  659. {
  660. $html_output = '<td class="value">-</td>'
  661. . '<td>' . __('View') . '</td>'
  662. . '<td>---</td>';
  663. if ($is_show_stats) {
  664. $html_output .= '<td class="value">-</td>'
  665. . '<td class="value">-</td>';
  666. }
  667. return $html_output;
  668. }
  669. /**
  670. * display "in use" below for a table that needs to be repaired
  671. *
  672. * @param integer $colspan_for_structure colspan for structure
  673. * @param boolean $db_is_system_schema whether db is information schema or not
  674. *
  675. * @return string HTML snippet
  676. */
  677. function PMA_getHtmlForRepairtable(
  678. $colspan_for_structure,
  679. $db_is_system_schema
  680. ) {
  681. return '<td colspan="'
  682. . ($colspan_for_structure - ($db_is_system_schema ? 5 : 8)) . '"'
  683. . 'class="center">'
  684. . __('in use')
  685. . '</td>';
  686. }
  687. /**
  688. * display table header (<table><thead>...</thead><tbody>)
  689. *
  690. * @param boolean $db_is_system_schema whether db is information schema or not
  691. * @param boolean $replication whether to sho replication status
  692. *
  693. * @return string html data
  694. */
  695. function PMA_tableHeader($db_is_system_schema = false, $replication = false)
  696. {
  697. $cnt = 0; // Let's count the columns...
  698. if ($db_is_system_schema) {
  699. $action_colspan = 4;
  700. } else {
  701. $action_colspan = 7;
  702. }
  703. $html_output = '<table class="data">' . "\n"
  704. . '<thead>' . "\n"
  705. . '<tr><th></th>' . "\n"
  706. . '<th>'
  707. . PMA_sortableTableHeader(__('Table'), 'table')
  708. . '</th>' . "\n";
  709. if ($replication) {
  710. $html_output .= '<th>' . "\n"
  711. . ' ' . __('Replication') . "\n"
  712. . '</th>';
  713. }
  714. $html_output .= '<th colspan="' . $action_colspan . '">' . "\n"
  715. . ' ' . __('Action') . "\n"
  716. . '</th>'
  717. // larger values are more interesting so default sort order is DESC
  718. . '<th>' . PMA_sortableTableHeader(__('Rows'), 'records', 'DESC')
  719. . PMA_Util::showHint(
  720. PMA_sanitize(
  721. __('May be approximate. See [doc@faq3-11]FAQ 3.11[/doc].')
  722. )
  723. ) . "\n"
  724. . '</th>' . "\n";
  725. if (!($GLOBALS['cfg']['PropertiesNumColumns'] > 1)) {
  726. $html_output .= '<th>' . PMA_sortableTableHeader(__('Type'), 'type')
  727. . '</th>' . "\n";
  728. $cnt++;
  729. $html_output .= '<th>'
  730. . PMA_sortableTableHeader(__('Collation'), 'collation')
  731. . '</th>' . "\n";
  732. $cnt++;
  733. }
  734. if ($GLOBALS['is_show_stats']) {
  735. // larger values are more interesting so default sort order is DESC
  736. $html_output .= '<th>'
  737. . PMA_sortableTableHeader(__('Size'), 'size', 'DESC')
  738. . '</th>' . "\n"
  739. // larger values are more interesting so default sort order is DESC
  740. . '<th>'
  741. . PMA_sortableTableHeader(__('Overhead'), 'overhead', 'DESC')
  742. . '</th>' . "\n";
  743. $cnt += 2;
  744. }
  745. if ($GLOBALS['cfg']['ShowDbStructureCreation']) {
  746. // larger values are more interesting so default sort order is DESC
  747. $html_output .= '<th>'
  748. . PMA_sortableTableHeader(__('Creation'), 'creation', 'DESC')
  749. . '</th>' . "\n";
  750. $cnt += 2;
  751. }
  752. if ($GLOBALS['cfg']['ShowDbStructureLastUpdate']) {
  753. // larger values are more interesting so default sort order is DESC
  754. $html_output .= '<th>'
  755. . PMA_sortableTableHeader(__('Last update'), 'last_update', 'DESC')
  756. . '</th>' . "\n";
  757. $cnt += 2;
  758. }
  759. if ($GLOBALS['cfg']['ShowDbStructureLastCheck']) {
  760. // larger values are more interesting so default sort order is DESC
  761. $html_output .= '<th>'
  762. . PMA_sortableTableHeader(__('Last check'), 'last_check', 'DESC')
  763. . '</th>' . "\n";
  764. $cnt += 2;
  765. }
  766. $html_output .= '</tr>' . "\n";
  767. $html_output .= '</thead>' . "\n";
  768. $html_output .= '<tbody>' . "\n";
  769. $GLOBALS['colspan_for_structure'] = $cnt + $action_colspan + 3;
  770. return $html_output;
  771. }
  772. /**
  773. * Creates a clickable column header for table information
  774. *
  775. * @param string $title title to use for the link
  776. * @param string $sort corresponds to sortable data name mapped in
  777. * libraries/db_info.inc.php
  778. * @param string $initial_sort_order initial sort order
  779. *
  780. * @return string link to be displayed in the table header
  781. */
  782. function PMA_sortableTableHeader($title, $sort, $initial_sort_order = 'ASC')
  783. {
  784. // Set some defaults
  785. $requested_sort = 'table';
  786. $requested_sort_order = $future_sort_order = $initial_sort_order;
  787. // If the user requested a sort
  788. if (isset($_REQUEST['sort'])) {
  789. $requested_sort = $_REQUEST['sort'];
  790. if (isset($_REQUEST['sort_order'])) {
  791. $requested_sort_order = $_REQUEST['sort_order'];
  792. }
  793. }
  794. $order_img = '';
  795. $order_link_params = array();
  796. $order_link_params['title'] = __('Sort');
  797. // If this column was requested to be sorted.
  798. if ($requested_sort == $sort) {
  799. if ($requested_sort_order == 'ASC') {
  800. $future_sort_order = 'DESC';
  801. // current sort order is ASC
  802. $order_img = ' ' . PMA_Util::getImage(
  803. 's_asc.png',
  804. __('Ascending'),
  805. array('class' => 'sort_arrow', 'title' => '')
  806. );
  807. $order_img .= ' ' . PMA_Util::getImage(
  808. 's_desc.png',
  809. __('Descending'),
  810. array('class' => 'sort_arrow hide', 'title' => '')
  811. );
  812. // but on mouse over, show the reverse order (DESC)
  813. $order_link_params['onmouseover'] = "$('.sort_arrow').toggle();";
  814. // on mouse out, show current sort order (ASC)
  815. $order_link_params['onmouseout'] = "$('.sort_arrow').toggle();";
  816. } else {
  817. $future_sort_order = 'ASC';
  818. // current sort order is DESC
  819. $order_img = ' ' . PMA_Util::getImage(
  820. 's_asc.png',
  821. __('Ascending'),
  822. array('class' => 'sort_arrow hide', 'title' => '')
  823. );
  824. $order_img .= ' ' . PMA_Util::getImage(
  825. 's_desc.png',
  826. __('Descending'),
  827. array('class' => 'sort_arrow', 'title' => '')
  828. );
  829. // but on mouse over, show the reverse order (ASC)
  830. $order_link_params['onmouseover'] = "$('.sort_arrow').toggle();";
  831. // on mouse out, show current sort order (DESC)
  832. $order_link_params['onmouseout'] = "$('.sort_arrow').toggle();";
  833. }
  834. }
  835. $_url_params = array(
  836. 'db' => $_REQUEST['db'],
  837. );
  838. $url = 'db_structure.php' . PMA_URL_getCommon($_url_params);
  839. // We set the position back to 0 every time they sort.
  840. $url .= "&amp;pos=0&amp;sort=$sort&amp;sort_order=$future_sort_order";
  841. if (! empty($_REQUEST['tbl_type'])) {
  842. $url .= "&amp;tbl_type=" . $_REQUEST['tbl_type'];
  843. }
  844. if (! empty($_REQUEST['tbl_group'])) {
  845. $url .= "&amp;tbl_group=" . $_REQUEST['tbl_group'];
  846. }
  847. return PMA_Util::linkOrButton(
  848. $url, $title . $order_img, $order_link_params
  849. );
  850. }
  851. /**
  852. * Get the alias ant truname
  853. *
  854. * @param string $tooltip_aliasname tooltip alias name
  855. * @param array $current_table current table
  856. * @param string $tooltip_truename tooltip true name
  857. *
  858. * @return array ($alias, $truename)
  859. */
  860. function PMA_getAliasAndTrueName($tooltip_aliasname, $current_table,
  861. $tooltip_truename
  862. ) {
  863. $alias = (! empty($tooltip_aliasname)
  864. && isset($tooltip_aliasname[$current_table['TABLE_NAME']])
  865. )
  866. ? str_replace(
  867. ' ', '&nbsp;',
  868. htmlspecialchars($tooltip_truename[$current_table['TABLE_NAME']])
  869. )
  870. : str_replace(
  871. ' ', '&nbsp;',
  872. htmlspecialchars($current_table['TABLE_NAME'])
  873. );
  874. $truename = (! empty($tooltip_truename)
  875. && isset($tooltip_truename[$current_table['TABLE_NAME']])
  876. )
  877. ? str_replace(
  878. ' ', '&nbsp;',
  879. htmlspecialchars($tooltip_truename[$current_table['TABLE_NAME']])
  880. )
  881. : str_replace(
  882. ' ', '&nbsp;',
  883. htmlspecialchars($current_table['TABLE_NAME'])
  884. );
  885. return array($alias, $truename);
  886. }
  887. /**
  888. * Get the server slave state
  889. *
  890. * @param boolean $server_slave_status server slave state
  891. * @param string $truename true name
  892. *
  893. * @return array ($do, $ignored)
  894. */
  895. function PMA_getServerSlaveStatus($server_slave_status, $truename)
  896. {
  897. $ignored = false;
  898. $do = false;
  899. include_once 'libraries/replication.inc.php';
  900. if ($server_slave_status) {
  901. $nbServerSlaveDoDb = count($server_slave_Do_DB);
  902. $nbServerSlaveIgnoreDb = count($server_slave_Ignore_DB);
  903. if ((strlen(array_search($truename, $server_slave_Do_Table)) > 0)
  904. || (strlen(array_search($GLOBALS['db'], $server_slave_Do_DB)) > 0)
  905. || ($nbServerSlaveDoDb == 1 && $nbServerSlaveIgnoreDb == 1)
  906. ) {
  907. $do = true;
  908. }
  909. foreach ($server_slave_Wild_Do_Table as $db_table) {
  910. $table_part = PMA_extractDbOrTable($db_table, 'table');
  911. $pattern = "@^" . substr($table_part, 0, strlen($table_part) - 1) . "@";
  912. if (($GLOBALS['db'] == PMA_extractDbOrTable($db_table, 'db'))
  913. && (preg_match($pattern, $truename))
  914. ) {
  915. $do = true;
  916. }
  917. }
  918. if ((strlen(array_search($truename, $server_slave_Ignore_Table)) > 0)
  919. || (strlen(array_search($GLOBALS['db'], $server_slave_Ignore_DB)) > 0)
  920. ) {
  921. $ignored = true;
  922. }
  923. foreach ($server_slave_Wild_Ignore_Table as $db_table) {
  924. $table_part = PMA_extractDbOrTable($db_table, 'table');
  925. $pattern = "@^" . substr($table_part, 0, strlen($table_part) - 1) . "@";
  926. if (($db == PMA_extractDbOrTable($db_table))
  927. && (preg_match($pattern, $truename))
  928. ) {
  929. $ignored = true;
  930. }
  931. }
  932. }
  933. return array($do, $ignored);
  934. }
  935. /**
  936. * Get the value set for ENGINE table,
  937. * $current_table, $formatted_size, $unit, $formatted_overhead,
  938. * $overhead_unit, $overhead_size, $table_is_view
  939. *
  940. * @param array $current_table current table
  941. * @param boolean $db_is_system_schema whether db is information schema or not
  942. * @param boolean $is_show_stats whether stats show or not
  943. * @param boolean $table_is_view whether table is view or not
  944. * @param double $sum_size totle table size
  945. * @param double $overhead_size overhead size
  946. *
  947. * @return array
  948. */
  949. function PMA_getStuffForEngineTypeTable($current_table, $db_is_system_schema,
  950. $is_show_stats, $table_is_view, $sum_size, $overhead_size
  951. ) {
  952. $formatted_size = '-';
  953. $unit = '';
  954. $formatted_overhead = '';
  955. $overhead_unit = '';
  956. switch ( $current_table['ENGINE']) {
  957. // MyISAM, ISAM or Heap table: Row count, data size and index size
  958. // are accurate; data size is accurate for ARCHIVE
  959. case 'MyISAM' :
  960. case 'ISAM' :
  961. case 'HEAP' :
  962. case 'MEMORY' :
  963. case 'ARCHIVE' :
  964. case 'Aria' :
  965. case 'Maria' :
  966. list($current_table, $formatted_size, $unit, $formatted_overhead,
  967. $overhead_unit, $overhead_size, $sum_size) = PMA_getValuesForAriaTable(
  968. $db_is_system_schema, $current_table, $is_show_stats,
  969. $sum_size, $overhead_size, $formatted_size, $unit,
  970. $formatted_overhead, $overhead_unit
  971. );
  972. break;
  973. case 'InnoDB' :
  974. case 'PBMS' :
  975. // InnoDB table: Row count is not accurate but data and index sizes are.
  976. // PBMS table in Drizzle: TABLE_ROWS is taken from table cache,
  977. // so it may be unavailable
  978. list($current_table, $formatted_size, $unit, $sum_size)
  979. = PMA_getValuesForInnodbTable($current_table, $is_show_stats, $sum_size);
  980. //$display_rows = ' - ';
  981. break;
  982. // Mysql 5.0.x (and lower) uses MRG_MyISAM
  983. // and MySQL 5.1.x (and higher) uses MRG_MYISAM
  984. // Both are aliases for MERGE
  985. case 'MRG_MyISAM' :
  986. case 'MRG_MYISAM' :
  987. case 'MERGE' :
  988. case 'BerkeleyDB' :
  989. // Merge or BerkleyDB table: Only row count is accurate.
  990. if ($is_show_stats) {
  991. $formatted_size = ' - ';
  992. $unit = '';
  993. }
  994. break;
  995. // for a view, the ENGINE is sometimes reported as null,
  996. // or on some servers it's reported as "SYSTEM VIEW"
  997. case null :
  998. case 'SYSTEM VIEW' :
  999. case 'FunctionEngine' :
  1000. // if table is broken, Engine is reported as null, so one more test
  1001. if ($current_table['TABLE_TYPE'] == 'VIEW') {
  1002. // countRecords() takes care of $cfg['MaxExactCountViews']
  1003. $current_table['TABLE_ROWS'] = PMA_Table::countRecords(
  1004. $GLOBALS['db'], $current_table['TABLE_NAME'],
  1005. true, true
  1006. );
  1007. $table_is_view = true;
  1008. }
  1009. break;
  1010. default :
  1011. // Unknown table type.
  1012. if ($is_show_stats) {
  1013. $formatted_size = __('unknown');
  1014. $unit = '';
  1015. }
  1016. } // end switch
  1017. return array($current_table, $formatted_size, $unit, $formatted_overhead,
  1018. $overhead_unit, $overhead_size, $table_is_view, $sum_size
  1019. );
  1020. }
  1021. /**
  1022. * Get values for ARIA/MARIA tables
  1023. * $current_table, $formatted_size, $unit, $formatted_overhead,
  1024. * $overhead_unit, $overhead_size
  1025. *
  1026. * @param boolean $db_is_system_schema whether db is information schema or not
  1027. * @param array $current_table current table
  1028. * @param boolean $is_show_stats whether stats show or not
  1029. * @param double $sum_size sum size
  1030. * @param double $overhead_size overhead size
  1031. * @param number $formatted_size formatted size
  1032. * @param string $unit unit
  1033. * @param number $formatted_overhead overhead formatted
  1034. * @param string $overhead_unit overhead unit
  1035. *
  1036. * @return array
  1037. */
  1038. function PMA_getValuesForAriaTable($db_is_system_schema, $current_table,
  1039. $is_show_stats, $sum_size, $overhead_size, $formatted_size, $unit,
  1040. $formatted_overhead, $overhead_unit
  1041. ) {
  1042. if ($db_is_system_schema) {
  1043. $current_table['Rows'] = PMA_Table::countRecords(
  1044. $GLOBALS['db'], $current_table['Name']
  1045. );
  1046. }
  1047. if ($is_show_stats) {
  1048. $tblsize = doubleval($current_table['Data_length'])
  1049. + doubleval($current_table['Index_length']);
  1050. $sum_size += $tblsize;
  1051. list($formatted_size, $unit) = PMA_Util::formatByteDown(
  1052. $tblsize, 3, ($tblsize > 0) ? 1 : 0
  1053. );
  1054. if (isset($current_table['Data_free']) && $current_table['Data_free'] > 0) {
  1055. list($formatted_overhead, $overhead_unit)
  1056. = PMA_Util::formatByteDown(
  1057. $current_table['Data_free'], 3,
  1058. (($current_table['Data_free'] > 0) ? 1 : 0)
  1059. );
  1060. $overhead_size += $current_table['Data_free'];
  1061. }
  1062. }
  1063. return array($current_table, $formatted_size, $unit, $formatted_overhead,
  1064. $overhead_unit, $overhead_size, $sum_size
  1065. );
  1066. }
  1067. /**
  1068. * Get values for InnoDB table
  1069. * $current_table, $formatted_size, $unit, $sum_size
  1070. *
  1071. * @param array $current_table current table
  1072. * @param boolean $is_show_stats whether stats show or not
  1073. * @param double $sum_size sum size
  1074. *
  1075. * @return array
  1076. */
  1077. function PMA_getValuesForInnodbTable($current_table, $is_show_stats, $sum_size)
  1078. {
  1079. $formatted_size = $unit = '';
  1080. if (($current_table['ENGINE'] == 'InnoDB'
  1081. && $current_table['TABLE_ROWS'] < $GLOBALS['cfg']['MaxExactCount'])
  1082. || !isset($current_table['TABLE_ROWS'])
  1083. ) {
  1084. $current_table['COUNTED'] = true;
  1085. $current_table['TABLE_ROWS'] = PMA_Table::countRecords(
  1086. $GLOBALS['db'], $current_table['TABLE_NAME'],
  1087. true, false
  1088. );
  1089. } else {
  1090. $current_table['COUNTED'] = false;
  1091. }
  1092. // Drizzle doesn't provide data and index length, check for null
  1093. if ($is_show_stats && $current_table['Data_length'] !== null) {
  1094. $tblsize = $current_table['Data_length'] + $current_table['Index_length'];
  1095. $sum_size += $tblsize;
  1096. list($formatted_size, $unit) = PMA_Util::formatByteDown(
  1097. $tblsize, 3, (($tblsize > 0) ? 1 : 0)
  1098. );
  1099. }
  1100. return array($current_table, $formatted_size, $unit, $sum_size);
  1101. }
  1102. /**
  1103. * table structure
  1104. */
  1105. /**
  1106. * Get the HTML snippet for structure table table header
  1107. *
  1108. * @param boolean $db_is_system_schema whether db is information schema or not
  1109. * @param boolean $tbl_is_view whether table is view or not
  1110. *
  1111. * @return string $html_output
  1112. */
  1113. function PMA_getHtmlForTableStructureHeader(
  1114. $db_is_system_schema,
  1115. $tbl_is_view
  1116. ) {
  1117. $html_output = '<thead>';
  1118. $html_output .= '<tr>';
  1119. $html_output .= '<th></th>'
  1120. . '<th>#</th>'
  1121. . '<th>' . __('Name') . '</th>'
  1122. . '<th>' . __('Type') . '</th>'
  1123. . '<th>' . __('Collation') . '</th>'
  1124. . '<th>' . __('Attributes') . '</th>'
  1125. . '<th>' . __('Null') . '</th>'
  1126. . '<th>' . __('Default') . '</th>'
  1127. . '<th>' . __('Extra') . '</th>';
  1128. if ($db_is_system_schema || $tbl_is_view) {
  1129. $html_output .= '<th>' . __('View') . '</th>';
  1130. } else { /* see tbl_structure.js, function moreOptsMenuResize() */
  1131. $colspan = 9;
  1132. if (PMA_DRIZZLE) {
  1133. $colspan -= 2;
  1134. }
  1135. if (PMA_Util::showIcons('ActionLinksMode')) {
  1136. $colspan--;
  1137. }
  1138. $html_output .= '<th colspan="' . $colspan . '" '
  1139. . 'class="action">' . __('Action') . '</th>';
  1140. }
  1141. $html_output .= '</tr>'
  1142. . '</thead>';
  1143. return $html_output;
  1144. }
  1145. /**
  1146. * Get HTML for structure table's rows and return $odd_row parameter also
  1147. * For "Action" Column, this function contains only HTML code for "Change"
  1148. * and "Drop"
  1149. *
  1150. * @param array $row current row
  1151. * @param string $rownum row number
  1152. * @param string $displayed_field_name displayed field name
  1153. * @param string $type_nowrap type nowrap
  1154. * @param array $extracted_columnspec associative array containing type,
  1155. * spec_in_brackets and possibly
  1156. * enum_set_values (another array)
  1157. * @param string $type_mime mime type
  1158. * @param string $field_charset field charset
  1159. * @param string $attribute attribute (BINARY, UNSIGNED,
  1160. * UNSIGNED ZEROFILL,
  1161. * on update CURRENT_TIMESTAMP)
  1162. * @param boolean $tbl_is_view whether tables is view or not
  1163. * @param boolean $db_is_system_schema whether db is information schema or not
  1164. * @param string $url_query url query
  1165. * @param string $field_encoded field encoded
  1166. * @param array $titles titles array
  1167. * @param string $table table
  1168. *
  1169. * @return array ($html_output, $odd_row)
  1170. */
  1171. function PMA_getHtmlTableStructureRow($row, $rownum,
  1172. $displayed_field_name, $type_nowrap, $extracted_columnspec, $type_mime,
  1173. $field_charset, $attribute, $tbl_is_view, $db_is_system_schema,
  1174. $url_query, $field_encoded, $titles, $table
  1175. ) {
  1176. $html_output = '<td class="center">'
  1177. . '<input type="checkbox" class="checkall" name="selected_fld[]" '
  1178. . 'value="' . htmlspecialchars($row['Field']) . '" '
  1179. . 'id="checkbox_row_' . $rownum . '"/>'
  1180. . '</td>';
  1181. $html_output .= '<td class="right">'
  1182. . $rownum
  1183. . '</td>';
  1184. $html_output .= '<th class="nowrap">'
  1185. . '<label for="checkbox_row_' . $rownum . '">'
  1186. . $displayed_field_name . '</label>'
  1187. . '</th>';
  1188. $html_output .= '<td' . $type_nowrap . '>'
  1189. . '<bdo dir="ltr" lang="en">'
  1190. . $extracted_columnspec['displayed_type'] . $type_mime
  1191. . '</bdo></td>';
  1192. $html_output .= '<td>' .
  1193. (empty($field_charset)
  1194. ? ''
  1195. : '<dfn title="' . PMA_getCollationDescr($field_charset) . '">'
  1196. . $field_charset . '</dfn>'
  1197. )
  1198. . '</td>';
  1199. $html_output .= '<td class="column_attribute nowrap">'
  1200. . $attribute . '</td>';
  1201. $html_output .= '<td>'
  1202. . (($row['Null'] == 'YES') ? __('Yes') : __('No')) . ' </td>';
  1203. $html_output .= '<td class="nowrap">';
  1204. if (isset($row['Default'])) {
  1205. if ($extracted_columnspec['type'] == 'bit') {
  1206. // here, $row['Default'] contains something like b'010'
  1207. $html_output .= PMA_Util::convertBitDefaultValue($row['Default']);
  1208. } else {
  1209. $html_output .= $row['Default'];
  1210. }
  1211. } else {
  1212. $html_output .= '<i>' . _pgettext('None for default', 'None') . '</i>';
  1213. }
  1214. $html_output .= '</td>';
  1215. $html_output .= '<td class="nowrap">' . strtoupper($row['Extra']) . '</td>';
  1216. $html_output .= PMA_getHtmlForDropColumn(
  1217. $tbl_is_view, $db_is_system_schema,
  1218. $url_query, $field_encoded,
  1219. $titles, $table, $row
  1220. );
  1221. return $html_output;
  1222. }
  1223. /**
  1224. * Get HTML code for "Drop" Action link
  1225. *
  1226. * @param boolean $tbl_is_view whether tables is view or not
  1227. * @param boolean $db_is_system_schema whether db is information schema or not
  1228. * @param string $url_query url query
  1229. * @param string $field_encoded field encoded
  1230. * @param array $titles tittles array
  1231. * @param string $table table
  1232. * @param array $row current row
  1233. *
  1234. * @return string $html_output
  1235. */
  1236. function PMA_getHtmlForDropColumn($tbl_is_view, $db_is_system_schema,
  1237. $url_query, $field_encoded, $titles, $table, $row
  1238. ) {
  1239. $html_output = '';
  1240. if (! $tbl_is_view && ! $db_is_system_schema) {
  1241. $html_output .= '<td class="edit center">'
  1242. . '<a class="change_column_anchor ajax"'
  1243. . ' href="tbl_structure.php?'
  1244. . $url_query . '&amp;field=' . $field_encoded
  1245. . '&amp;change_column=1">'
  1246. . $titles['Change'] . '</a>' . '</td>';
  1247. $html_output .= '<td class="drop center">'
  1248. . '<a class="drop_column_anchor ajax"'
  1249. . ' href="sql.php?' . $url_query . '&amp;sql_query='
  1250. . urlencode(
  1251. 'ALTER TABLE ' . PMA_Util::backquote($table)
  1252. . ' DROP ' . PMA_Util::backquote($row['Field']) . ';'
  1253. )
  1254. . '&amp;dropped_column=' . urlencode($row['Field'])
  1255. . '&amp;message_to_show=' . urlencode(
  1256. sprintf(
  1257. __('Column %s has been dropped.'),
  1258. htmlspecialchars($row['Field'])
  1259. )
  1260. ) . '" >'
  1261. . $titles['Drop'] . '</a>'
  1262. . '</td>';
  1263. }
  1264. return $html_output;
  1265. }
  1266. /**
  1267. * Get HTML for "check all" check box with "with selected" actions in table
  1268. * structure
  1269. *
  1270. * @param string $pmaThemeImage pma theme image url
  1271. * @param string $text_dir test directory
  1272. * @param boolean $tbl_is_view whether table is view or not
  1273. * @param boolean $db_is_system_schema whether db is information schema or not
  1274. * @param string $tbl_storage_engine table storage engine
  1275. *
  1276. * @return string $html_output
  1277. */
  1278. function PMA_getHtmlForCheckAllTableColumn($pmaThemeImage, $text_dir,
  1279. $tbl_is_view, $db_is_system_schema, $tbl_storage_engine
  1280. ) {
  1281. $html_output = '<img class="selectallarrow" '
  1282. . 'src="' . $pmaThemeImage . 'arrow_' . $text_dir . '.png" '
  1283. . 'width="38" height="22" alt="' . __('With selected:') . '" />';
  1284. $html_output .= '<input type="checkbox" id="fieldsForm_checkall" '
  1285. . 'class="checkall_box" title="' . __('Check All') . '" />'
  1286. . '<label for="fieldsForm_checkall">' . __('Check All') . '</label>';
  1287. $html_output .= '<i style="margin-left: 2em">'
  1288. . __('With selected:') . '</i>';
  1289. $html_output .= PMA_Util::getButtonOrImage(
  1290. 'submit_mult', 'mult_submit', 'submit_mult_browse',
  1291. __('Browse'), 'b_browse.png', 'browse'
  1292. );
  1293. if (! $tbl_is_view && ! $db_is_system_schema) {
  1294. $html_output .= PMA_Util::getButtonOrImage(
  1295. 'submit_mult', 'mult_submit change_columns_anchor ajax',
  1296. 'submit_mult_change', __('Change'), 'b_edit.png', 'change'
  1297. );
  1298. $html_output .= PMA_Util::getButtonOrImage(
  1299. 'submit_mult', 'mult_submit', 'submit_mult_drop',
  1300. __('Drop'), 'b_drop.png', 'drop'
  1301. );
  1302. if ('ARCHIVE' != $tbl_storage_engine) {
  1303. $html_output .= PMA_Util::getButtonOrImage(
  1304. 'submit_mult', 'mult_submit', 'submit_mult_primary',
  1305. __('Primary'), 'b_primary.png', 'primary'
  1306. );
  1307. $html_output .= PMA_Util::getButtonOrImage(
  1308. 'submit_mult', 'mult_submit', 'submit_mult_unique',
  1309. __('Unique'), 'b_unique.png', 'unique'
  1310. );
  1311. $html_output .= PMA_Util::getButtonOrImage(
  1312. 'submit_mult', 'mult_submit', 'submit_mult_index',
  1313. __('Index'), 'b_index.png', 'index'
  1314. );
  1315. }
  1316. if (! empty($tbl_storage_engine) && $tbl_storage_engine == 'MYISAM') {
  1317. $html_output .= PMA_Util::getButtonOrImage(
  1318. 'submit_mult', 'mult_submit', 'submit_mult_spatial',
  1319. __('Spatial'), 'b_spatial.png', 'spatial'
  1320. );
  1321. }
  1322. if (! empty($tbl_storage_engine)
  1323. && ($tbl_storage_engine == 'MYISAM'
  1324. || $tbl_storage_engine == 'ARIA'
  1325. || $tbl_storage_engine == 'MARIA')
  1326. ) {
  1327. $html_output .= PMA_Util::getButtonOrImage(
  1328. 'submit_mult', 'mult_submit', 'submit_mult_fulltext',
  1329. __('Fulltext'), 'b_ftext.png', 'ftext'
  1330. );
  1331. }
  1332. }
  1333. return $html_output;
  1334. }
  1335. /**
  1336. * Get HTML for move columns dialog
  1337. *
  1338. * @return string $html_output
  1339. */
  1340. function PMA_getHtmlDivForMoveColumnsDialog()
  1341. {
  1342. $html_output = '<div id="move_columns_dialog" '
  1343. . 'title="' . __('Move columns') . '" style="display: none">';
  1344. $html_output .= '<p>'
  1345. . __('Move the columns by dragging them up and down.') . '</p>';
  1346. $html_output .= '<form action="tbl_structure.php">'
  1347. . '<div>'
  1348. . PMA_URL_getHiddenInputs($GLOBALS['db'], $GLOBALS['table'])
  1349. . '<ul></ul>'
  1350. . '</div>'
  1351. . '</form>'
  1352. . '</div>';
  1353. return $html_output;
  1354. }
  1355. /**
  1356. * Get HTML for edit views'
  1357. *
  1358. * @param string $url_params URL parameters
  1359. *
  1360. * @return string $html_output
  1361. */
  1362. function PMA_getHtmlForEditView($url_params)
  1363. {
  1364. $query = "SELECT `VIEW_DEFINITION`, `CHECK_OPTION`, `DEFINER`, `SECURITY_TYPE`"
  1365. . " FROM `INFORMATION_SCHEMA`.`VIEWS`"
  1366. . " WHERE TABLE_SCHEMA='" . PMA_Util::sqlAddSlashes($GLOBALS['db']) . "'"
  1367. . " AND TABLE_NAME='" . PMA_Util::sqlAddSlashes($GLOBALS['table']) . "';";
  1368. $item = $GLOBALS['dbi']->fetchSingleRow($query);
  1369. $view = array(
  1370. 'operation' => 'alter',
  1371. 'definer' => $item['DEFINER'],
  1372. 'sql_security' => $item['SECURITY_TYPE'],
  1373. 'name' => $GLOBALS['table'],
  1374. 'as' => $item['VIEW_DEFINITION'],
  1375. 'with' => $item['CHECK_OPTION'],
  1376. );
  1377. $url = 'view_create.php' . PMA_URL_getCommon($url_params) . '&amp;';
  1378. $url .= implode(
  1379. '&amp;',
  1380. array_map(
  1381. function ($key, $val) {
  1382. return 'view[' . urlencode($key) . ']=' . urlencode($val);
  1383. },
  1384. array_keys($view),
  1385. $view
  1386. )
  1387. );
  1388. $html_output = PMA_Util::linkOrButton(
  1389. $url,
  1390. PMA_Util::getIcon('b_edit.png', __('Edit view'), true)
  1391. );
  1392. return $html_output;
  1393. }
  1394. /**
  1395. * Get HTML links for 'Print view', 'Relation view', 'Propose table structure',
  1396. * 'Track table' and 'Move columns'
  1397. *
  1398. * @param string $url_query url query
  1399. * @param boolean $tbl_is_view whether table is view or not
  1400. * @param boolean $db_is_system_schema whether db is information schema or not
  1401. * @param string $tbl_storage_engine table storage engine
  1402. * @param array $cfgRelation current relation parameters
  1403. *
  1404. * @return string $html_output
  1405. */
  1406. function PMA_getHtmlForOptionalActionLinks($url_query, $tbl_is_view,
  1407. $db_is_system_schema, $tbl_storage_engine, $cfgRelation
  1408. ) {
  1409. $html_output = '<a href="tbl_printview.php?' . $url_query
  1410. . '" target="print_view">'
  1411. . PMA_Util::getIcon('b_print.png', __('Print view'), true)
  1412. . '</a>';
  1413. if (! $tbl_is_view && ! $db_is_system_schema) {
  1414. // if internal relations are available, or foreign keys are supported
  1415. // ($tbl_storage_engine comes from libraries/tbl_info.inc.php
  1416. if ($cfgRelation['relwork']
  1417. || PMA_Util::isForeignKeySupported($tbl_storage_engine)
  1418. ) {
  1419. $html_output .= '<a href="tbl_relation.php?' . $url_query . '">'
  1420. . PMA_Util::getIcon(
  1421. 'b_relations.png', __('Relation view'), true
  1422. )
  1423. . '</a>';
  1424. }
  1425. if (!PMA_DRIZZLE) {
  1426. $html_output .= '<a href="sql.php?' . $url_query
  1427. . '&amp;session_max_rows=all&amp;sql_query=' . urlencode(
  1428. 'SELECT * FROM ' . PMA_Util::backquote($GLOBALS['table'])
  1429. . ' PROCEDURE ANALYSE()'
  1430. ) . '">'
  1431. . PMA_Util::getIcon(
  1432. 'b_tblanalyse.png',
  1433. __('Propose table structure'),
  1434. true
  1435. )
  1436. . '</a>';
  1437. $html_output .= PMA_Util::showMySQLDocu('procedure_analyse') . "\n";
  1438. }
  1439. if (PMA_Tracker::isActive()) {
  1440. $html_output .= '<a href="tbl_tracking.php?' . $url_query . '">'
  1441. . PMA_Util::getIcon('eye.png', __('Track table'), true)
  1442. . '</a>';
  1443. }
  1444. $html_output .= '<a href="#" id="move_columns_anchor">'
  1445. . PMA_Util::getIcon('b_move.png', __('Move columns'), true)
  1446. . '</a>';
  1447. }
  1448. return $html_output;
  1449. }
  1450. /**
  1451. * Get HTML snippet for "Add column" feature in structure table
  1452. *
  1453. * @param array $columns_list column list array
  1454. *
  1455. * @return string $html_output
  1456. */
  1457. function PMA_getHtmlForAddColumn($columns_list)
  1458. {
  1459. $html_output = '<form method="post" action="tbl_addfield.php" '
  1460. . 'id="addColumns" name="addColumns" '
  1461. . 'onsubmit="return checkFormElementInRange('
  1462. . 'this, \'num_fields\', \'' . str_replace(
  1463. '\'',
  1464. '\\\'',
  1465. __('You have to add at least one column.')
  1466. ) . '\', 1)'
  1467. . '">';
  1468. $html_output .= PMA_URL_getHiddenInputs(
  1469. $GLOBALS['db'],
  1470. $GLOBALS['table']
  1471. );
  1472. if (PMA_Util::showIcons('ActionLinksMode')) {
  1473. $html_output .=PMA_Util::getImage(
  1474. 'b_insrow.png',
  1475. __('Add column')
  1476. );
  1477. }
  1478. $num_fields = '<input type="number" name="num_fields" '
  1479. . 'value="1" onfocus="this.select()" '
  1480. . 'min="1" required />';
  1481. $html_output .= sprintf(__('Add %s column(s)'), $num_fields);
  1482. // I tried displaying the drop-down inside the label but with Firefox
  1483. // the drop-down was blinking
  1484. $column_selector = '<select name="after_field" '
  1485. . 'onclick="this.form.field_where[2].checked=true" '
  1486. . 'onchange="this.form.field_where[2].checked=true">';
  1487. foreach ($columns_list as $one_column_name) {
  1488. $column_selector .= '<option '
  1489. . 'value="' . htmlspecialchars($one_column_name) . '">'
  1490. . htmlspecialchars($one_column_name)
  1491. . '</option>';
  1492. }
  1493. $column_selector .= '</select>';
  1494. $choices = array(
  1495. 'last' => __('At End of Table'),
  1496. 'first' => __('At Beginning of Table'),
  1497. 'after' => sprintf(__('After %s'), '')
  1498. );
  1499. $html_output .= PMA_Util::getRadioFields(
  1500. 'field_where', $choices, 'last', false
  1501. );
  1502. $html_output .= $column_selector;
  1503. $html_output .= '<input type="submit" value="' . __('Go') . '" />'
  1504. . '</form>';
  1505. return $html_output;
  1506. }
  1507. /**
  1508. * Get HTML snippet for table rows in the Information ->Space usage table
  1509. *
  1510. * @param boolean $odd_row whether current row is odd or even
  1511. * @param string $name type of usage
  1512. * @param string $value value of usage
  1513. * @param string $unit unit
  1514. *
  1515. * @return string $html_output
  1516. */
  1517. function PMA_getHtmlForSpaceUsageTableRow($odd_row, $name, $value, $unit)
  1518. {
  1519. $html_output = '<tr class="' . (($odd_row = !$odd_row) ? 'odd' : 'even') . '">';
  1520. $html_output .= '<th class="name">' . $name . '</th>';
  1521. $html_output .= '<td class="value">' . $value . '</td>';
  1522. $html_output .= '<td class="unit">' . $unit . '</td>';
  1523. $html_output .= '</tr>';
  1524. return $html_output;
  1525. }
  1526. /**
  1527. * Get HTML for Optimize link if overhead in Information fieldset
  1528. *
  1529. * @param string $url_query URL query
  1530. *
  1531. * @return string $html_output
  1532. */
  1533. function PMA_getHtmlForOptimizeLink($url_query)
  1534. {
  1535. $html_output = '<tr class="tblFooters">';
  1536. $html_output .= '<td colspan="3" class="center">';
  1537. $html_output .= '<a href="sql.php?' . $url_query
  1538. . '&pos=0&amp;sql_query=' . urlencode(
  1539. 'OPTIMIZE TABLE ' . PMA_Util::backquote($GLOBALS['table'])
  1540. )
  1541. . '">'
  1542. . PMA_Util::getIcon('b_tbloptimize.png', __('Optimize table'))
  1543. . '</a>';
  1544. $html_output .= '</td>';
  1545. $html_output .= '</tr>';
  1546. return $html_output;
  1547. }
  1548. /**
  1549. * Get HTML for 'Row statistics' table row
  1550. *
  1551. * @param boolean $odd_row whether current row is odd or even
  1552. * @param string $name statement name
  1553. * @param mixed $value value
  1554. *
  1555. * @return string $html_output
  1556. */
  1557. function PMA_getHtmlForRowStatsTableRow($odd_row, $name, $value)
  1558. {
  1559. $html_output = '<tr class="' . (($odd_row = !$odd_row) ? 'odd' : 'even') . '">';
  1560. $html_output .= '<th class="name">' . $name . '</th>';
  1561. $html_output .= '<td class="value">' . $value . '</td>';
  1562. $html_output .= '</tr>';
  1563. return $html_output;
  1564. }
  1565. /**
  1566. * Get HTML snippet for display Row statistics table
  1567. *
  1568. * @param array $showtable show table array
  1569. * @param string $tbl_collation table collation
  1570. * @param boolean $is_innodb whether table is innob or not
  1571. * @param boolean $mergetable Checks if current table is a merge table
  1572. * @param integer $avg_size average size
  1573. * @param string $avg_unit average unit
  1574. *
  1575. * @return string $html_output
  1576. */
  1577. function getHtmlForRowStatsTable($showtable, $tbl_collation,
  1578. $is_innodb, $mergetable, $avg_size, $avg_unit
  1579. ) {
  1580. $odd_row = false;
  1581. $html_output = '<table id="tablerowstats" class="data">';
  1582. $html_output .= '<caption class="tblHeaders">'
  1583. . __('Row statistics') . '</caption>';
  1584. $html_output .= '<tbody>';
  1585. if (isset($showtable['Row_format'])) {
  1586. if ($showtable['Row_format'] == 'Fixed') {
  1587. $value = __('static');
  1588. } elseif ($showtable['Row_format'] == 'Dynamic') {
  1589. $value = __('dynamic');
  1590. } else {
  1591. $value = $showtable['Row_format'];
  1592. }
  1593. $html_output .= PMA_getHtmlForRowStatsTableRow(
  1594. $odd_row, __('Format'), $value
  1595. );
  1596. $odd_row = !$odd_row;
  1597. }
  1598. if (! empty($showtable['Create_options'])) {
  1599. if ($showtable['Create_options'] == 'partitioned') {
  1600. $value = __('partitioned');
  1601. } else {
  1602. $value = $showtable['Create_options'];
  1603. }
  1604. $html_output .= PMA_getHtmlForRowStatsTableRow(
  1605. $odd_row, __('Options'), $value
  1606. );
  1607. $odd_row = !$odd_row;
  1608. }
  1609. if (!empty($tbl_collation)) {
  1610. $value = '<dfn title="' . PMA_getCollationDescr($tbl_collation) . '">'
  1611. . $tbl_collation . '</dfn>';
  1612. $html_output .= PMA_getHtmlForRowStatsTableRow(
  1613. $odd_row, __('Collation'), $value
  1614. );
  1615. $odd_row = !$odd_row;
  1616. }
  1617. if (!$is_innodb && isset($showtable['Rows'])) {
  1618. $html_output .= PMA_getHtmlForRowStatsTableRow(
  1619. $odd_row,
  1620. __('Rows'),
  1621. PMA_Util::formatNumber($showtable['Rows'], 0)
  1622. );
  1623. $odd_row = !$odd_row;
  1624. }
  1625. if (!$is_innodb
  1626. && isset($showtable['Avg_row_length'])
  1627. && $showtable['Avg_row_length'] > 0
  1628. ) {
  1629. list($avg_row_length_value, $avg_row_length_unit)
  1630. = PMA_Util::formatByteDown(
  1631. $showtable['Avg_row_length'],
  1632. 6,
  1633. 1
  1634. );
  1635. $html_output .= PMA_getHtmlForRowStatsTableRow(
  1636. $odd_row,
  1637. __('Row length'),
  1638. ($avg_row_length_value . ' ' . $avg_row_length_unit)
  1639. );
  1640. unset($avg_row_length_value, $avg_row_length_unit);
  1641. $odd_row = !$odd_row;
  1642. }
  1643. if (!$is_innodb
  1644. && isset($showtable['Data_length'])
  1645. && $showtable['Rows'] > 0
  1646. && $mergetable == false
  1647. ) {
  1648. $html_output .= PMA_getHtmlForRowStatsTableRow(
  1649. $odd_row,
  1650. __('Row size'),
  1651. ($avg_size . ' ' . $avg_unit)
  1652. );
  1653. $odd_row = !$odd_row;
  1654. }
  1655. if (isset($showtable['Auto_increment'])) {
  1656. $html_output .= PMA_getHtmlForRowStatsTableRow(
  1657. $odd_row,
  1658. __('Next autoindex'),
  1659. PMA_Util::formatNumber($showtable['Auto_increment'], 0)
  1660. );
  1661. $odd_row = !$odd_row;
  1662. }
  1663. if (isset($showtable['Create_time'])) {
  1664. $html_output .= PMA_getHtmlForRowStatsTableRow(
  1665. $odd_row,
  1666. __('Creation'),
  1667. PMA_Util::localisedDate(strtotime($showtable['Create_time']))
  1668. );
  1669. $odd_row = !$odd_row;
  1670. }
  1671. if (isset($showtable['Update_time'])) {
  1672. $html_output .= PMA_getHtmlForRowStatsTableRow(
  1673. $odd_row,
  1674. __('Last update'),
  1675. PMA_Util::localisedDate(strtotime($showtable['Update_time']))
  1676. );
  1677. $odd_row = !$odd_row;
  1678. }
  1679. if (isset($showtable['Check_time'])) {
  1680. $html_output .= PMA_getHtmlForRowStatsTableRow(
  1681. $odd_row,
  1682. __('Last check'),
  1683. PMA_Util::localisedDate(strtotime($showtable['Check_time']))
  1684. );
  1685. }
  1686. $html_output .= '</tbody>'
  1687. . '</table>';
  1688. return $html_output;
  1689. }
  1690. /**
  1691. * Get HTML snippet for action row in structure table,
  1692. * This function returns common HTML <td> for Primary, Unique, Index,
  1693. * Spatial actions
  1694. *
  1695. * @param array $type column type
  1696. * @param array $tbl_storage_engine table storage engine
  1697. * @param string $class class attribute for <td>
  1698. * @param boolean $hasField has field
  1699. * @param boolean $hasLinkClass has <a> the class attribute
  1700. * @param string $url_query url query
  1701. * @param boolean $primary primary if set, false otherwise
  1702. * @param string $syntax Sql syntax
  1703. * @param string $message message to show
  1704. * @param string $action action
  1705. * @param array $titles titles array
  1706. * @param array $row current row
  1707. * @param boolean $isPrimary is primary action
  1708. *
  1709. * @return string $html_output
  1710. */
  1711. function PMA_getHtmlForActionRowInStructureTable($type, $tbl_storage_engine,
  1712. $class, $hasField, $hasLinkClass, $url_query, $primary, $syntax,
  1713. $message, $action, $titles, $row, $isPrimary
  1714. ) {
  1715. $html_output = '<li class="' . $class . '">';
  1716. if ($type == 'text'
  1717. || $type == 'blob'
  1718. || 'ARCHIVE' == $tbl_storage_engine
  1719. || $hasField
  1720. ) {
  1721. $html_output .= $titles['No' . $action];
  1722. } else {
  1723. $html_output .= '<a rel="samepage" '
  1724. . ($hasLinkClass ? 'class="ajax add_primary_key_anchor" ' :
  1725. ($action=='Index' ? 'class="ajax add_index_anchor"' :
  1726. ($action=='Unique' ? 'class="ajax add_unique_anchor"' : ' ')
  1727. )
  1728. )
  1729. . ' href="sql.php?' . $url_query . '&amp;sql_query='
  1730. . urlencode(
  1731. 'ALTER TABLE ' . PMA_Util::backquote($GLOBALS['table'])
  1732. . ($isPrimary ? ($primary ? ' DROP PRIMARY KEY,' : '') : '')
  1733. . ' ' . $syntax . '('
  1734. . PMA_Util::backquote($row['Field']) . ');'
  1735. )
  1736. . '&amp;message_to_show=' . urlencode(
  1737. sprintf(
  1738. $message,
  1739. htmlspecialchars($row['Field'])
  1740. )
  1741. ) . '" >'
  1742. . $titles[$action] . '</a>';
  1743. }
  1744. $html_output .= '</li>';
  1745. return $html_output;
  1746. }
  1747. /**
  1748. * Get HTML for fulltext action
  1749. *
  1750. * @param string $tbl_storage_engine table storage engine
  1751. * @param string $type column type
  1752. * @param string $url_query url query
  1753. * @param array $row current row
  1754. * @param array $titles titles array
  1755. *
  1756. * @return string $html_output
  1757. */
  1758. function PMA_getHtmlForFullTextAction($tbl_storage_engine, $type, $url_query,
  1759. $row, $titles
  1760. ) {
  1761. $html_output = '<li class="fulltext nowrap">';
  1762. if (! empty($tbl_storage_engine)
  1763. && ($tbl_storage_engine == 'MYISAM'
  1764. || $tbl_storage_engine == 'ARIA'
  1765. || $tbl_storage_engine == 'MARIA'
  1766. || ($tbl_storage_engine == 'INNODB' && PMA_MYSQL_INT_VERSION >= 50604))
  1767. && (strpos($type, 'text') !== false || strpos($type, 'char') !== false)
  1768. ) {
  1769. $html_output .= '<a rel="samepage" href="sql.php?' . $url_query
  1770. . '&amp;sql_query='
  1771. . urlencode(
  1772. 'ALTER TABLE ' . PMA_Util::backquote($GLOBALS['table'])
  1773. . ' ADD FULLTEXT(' . PMA_Util::backquote($row['Field'])
  1774. . ');'
  1775. )
  1776. . '&amp;message_to_show='
  1777. . urlencode(
  1778. sprintf(
  1779. __('An index has been added on %s.'),
  1780. htmlspecialchars($row['Field'])
  1781. )
  1782. )
  1783. . '">';
  1784. $html_output .= $titles['IdxFulltext'] . '</a>';
  1785. } else {
  1786. $html_output .= $titles['NoIdxFulltext'];
  1787. }
  1788. $html_output .= '</li>';
  1789. return $html_output;
  1790. }
  1791. /**
  1792. * Get HTML snippet for "Distinc Value" action
  1793. *
  1794. * @param string $url_query url query
  1795. * @param array $row current row
  1796. * @param array $titles titles array
  1797. *
  1798. * @return string $html_output
  1799. */
  1800. function PMA_getHtmlForDistinctValueAction($url_query, $row, $titles)
  1801. {
  1802. $html_output = '<li class="browse nowrap">';
  1803. $html_output .= '<a href="sql.php?' . $url_query . '&amp;sql_query='
  1804. . urlencode(
  1805. 'SELECT COUNT(*) AS ' . PMA_Util::backquote(__('Rows'))
  1806. . ', ' . PMA_Util::backquote($row['Field'])
  1807. . ' FROM ' . PMA_Util::backquote($GLOBALS['table'])
  1808. . ' GROUP BY ' . PMA_Util::backquote($row['Field'])
  1809. . ' ORDER BY ' . PMA_Util::backquote($row['Field'])
  1810. )
  1811. . '">'
  1812. . $titles['DistinctValues']
  1813. . '</a>';
  1814. $html_output .= '</li>';
  1815. return $html_output;
  1816. }
  1817. /**
  1818. * Get HTML snippet for Actions in table structure
  1819. *
  1820. * @param string $type column type
  1821. * @param string $tbl_storage_engine table storage engine
  1822. * @param boolean $primary primary if set, false otherwise
  1823. * @param string $field_name column name
  1824. * @param string $url_query url query
  1825. * @param array $titles titles array
  1826. * @param array $row current row
  1827. * @param string $rownum row number
  1828. * @param array $hidden_titles hidden titles
  1829. * @param array $columns_with_unique_index columns with unique index
  1830. *
  1831. * @return string $html_output;
  1832. */
  1833. function PMA_getHtmlForActionsInTableStructure($type, $tbl_storage_engine,
  1834. $primary, $field_name, $url_query, $titles, $row, $rownum, $hidden_titles,
  1835. $columns_with_unique_index
  1836. ) {
  1837. $html_output = '<td><ul class="table-structure-actions resizable-menu">';
  1838. $html_output .= PMA_getHtmlForActionRowInStructureTable(
  1839. $type, $tbl_storage_engine,
  1840. 'primary nowrap',
  1841. ($primary && $primary->hasColumn($field_name)),
  1842. true, $url_query, $primary,
  1843. 'ADD PRIMARY KEY',
  1844. __('A primary key has been added on %s.'),
  1845. 'Primary', $titles, $row, true
  1846. );
  1847. $html_output .= PMA_getHtmlForActionRowInStructureTable(
  1848. $type, $tbl_storage_engine,
  1849. 'add_unique unique nowrap',
  1850. isset($columns_with_unique_index[$field_name]),
  1851. false, $url_query, $primary, 'ADD UNIQUE',
  1852. __('An index has been added on %s.'),
  1853. 'Unique', $titles, $row, false
  1854. );
  1855. $html_output .= PMA_getHtmlForActionRowInStructureTable(
  1856. $type, $tbl_storage_engine,
  1857. 'add_index nowrap', false, false, $url_query,
  1858. $primary, 'ADD INDEX', __('An index has been added on %s.'),
  1859. 'Index', $titles, $row, false
  1860. );
  1861. if (!PMA_DRIZZLE) {
  1862. $spatial_types = array(
  1863. 'geometry', 'point', 'linestring', 'polygon', 'multipoint',
  1864. 'multilinestring', 'multipolygon', 'geomtrycollection'
  1865. );
  1866. $html_output .= PMA_getHtmlForActionRowInStructureTable(
  1867. $type, $tbl_storage_engine,
  1868. 'spatial nowrap',
  1869. (! in_array($type, $spatial_types)
  1870. || 'MYISAM' != $tbl_storage_engine
  1871. ),
  1872. false, $url_query, $primary, 'ADD SPATIAL',
  1873. __('An index has been added on %s.'), 'Spatial',
  1874. $titles, $row, false
  1875. );
  1876. // FULLTEXT is possible on TEXT, CHAR and VARCHAR
  1877. $html_output .= PMA_getHtmlForFullTextAction(
  1878. $tbl_storage_engine, $type, $url_query, $row, $titles
  1879. );
  1880. }
  1881. $html_output .= PMA_getHtmlForDistinctValueAction($url_query, $row, $titles);
  1882. $html_output .= '<div class="clearfloat"></div></ul></td>';
  1883. return $html_output;
  1884. }
  1885. /**
  1886. * Get hidden action titles (image and string)
  1887. *
  1888. * @return array $hidden_titles
  1889. */
  1890. function PMA_getHiddenTitlesArray()
  1891. {
  1892. $hidden_titles = array();
  1893. $hidden_titles['DistinctValues'] = PMA_Util::getIcon(
  1894. 'b_browse.png', __('Distinct values'), true
  1895. );
  1896. $hidden_titles['Primary'] = PMA_Util::getIcon(
  1897. 'b_primary.png', __('Add primary key'), true
  1898. );
  1899. $hidden_titles['NoPrimary'] = PMA_Util::getIcon(
  1900. 'bd_primary.png', __('Add primary key'), true
  1901. );
  1902. $hidden_titles['Index'] = PMA_Util::getIcon(
  1903. 'b_index.png', __('Add index'), true
  1904. );
  1905. $hidden_titles['NoIndex'] = PMA_Util::getIcon(
  1906. 'bd_index.png', __('Add index'), true
  1907. );
  1908. $hidden_titles['Unique'] = PMA_Util::getIcon(
  1909. 'b_unique.png', __('Add unique index'), true
  1910. );
  1911. $hidden_titles['NoUnique'] = PMA_Util::getIcon(
  1912. 'bd_unique.png', __('Add unique index'), true
  1913. );
  1914. $hidden_titles['Spatial'] = PMA_Util::getIcon(
  1915. 'b_spatial.png', __('Add SPATIAL index'), true
  1916. );
  1917. $hidden_titles['NoSpatial'] = PMA_Util::getIcon(
  1918. 'bd_spatial.png', __('Add SPATIAL index'), true
  1919. );
  1920. $hidden_titles['IdxFulltext'] = PMA_Util::getIcon(
  1921. 'b_ftext.png', __('Add FULLTEXT index'), true
  1922. );
  1923. $hidden_titles['NoIdxFulltext'] = PMA_Util::getIcon(
  1924. 'bd_ftext.png', __('Add FULLTEXT index'), true
  1925. );
  1926. return $hidden_titles;
  1927. }
  1928. /**
  1929. * Get action titles (image or string array
  1930. *
  1931. * @return array $titles
  1932. */
  1933. function PMA_getActionTitlesArray()
  1934. {
  1935. $titles = array();
  1936. $titles['Change']
  1937. = PMA_Util::getIcon('b_edit.png', __('Change'));
  1938. $titles['Drop']
  1939. = PMA_Util::getIcon('b_drop.png', __('Drop'));
  1940. $titles['NoDrop']
  1941. = PMA_Util::getIcon('b_drop.png', __('Drop'));
  1942. $titles['Primary']
  1943. = PMA_Util::getIcon('b_primary.png', __('Primary'));
  1944. $titles['Index']
  1945. = PMA_Util::getIcon('b_index.png', __('Index'));
  1946. $titles['Unique']
  1947. = PMA_Util::getIcon('b_unique.png', __('Unique'));
  1948. $titles['Spatial']
  1949. = PMA_Util::getIcon('b_spatial.png', __('Spatial'));
  1950. $titles['IdxFulltext']
  1951. = PMA_Util::getIcon('b_ftext.png', __('Fulltext'));
  1952. $titles['NoPrimary']
  1953. = PMA_Util::getIcon('bd_primary.png', __('Primary'));
  1954. $titles['NoIndex']
  1955. = PMA_Util::getIcon('bd_index.png', __('Index'));
  1956. $titles['NoUnique']
  1957. = PMA_Util::getIcon('bd_unique.png', __('Unique'));
  1958. $titles['NoSpatial']
  1959. = PMA_Util::getIcon('bd_spatial.png', __('Spatial'));
  1960. $titles['NoIdxFulltext']
  1961. = PMA_Util::getIcon('bd_ftext.png', __('Fulltext'));
  1962. $titles['DistinctValues']
  1963. = PMA_Util::getIcon('b_browse.png', __('Distinct values'));
  1964. return $titles;
  1965. }
  1966. /**
  1967. * Get HTML snippet for display table statistics
  1968. *
  1969. * @param array $showtable full table status info
  1970. * @param integer $table_info_num_rows table info number of rows
  1971. * @param boolean $tbl_is_view whether table is view or not
  1972. * @param boolean $db_is_system_schema whether db is information schema or not
  1973. * @param string $tbl_storage_engine table storage engine
  1974. * @param string $url_query url query
  1975. * @param string $tbl_collation table collation
  1976. *
  1977. * @return string $html_output
  1978. */
  1979. function PMA_getHtmlForDisplayTableStats($showtable, $table_info_num_rows,
  1980. $tbl_is_view, $db_is_system_schema, $tbl_storage_engine, $url_query,
  1981. $tbl_collation
  1982. ) {
  1983. $html_output = '<div id="tablestatistics">';
  1984. if (empty($showtable)) {
  1985. $showtable = PMA_Table::sGetStatusInfo(
  1986. $GLOBALS['db'], $GLOBALS['table'], null, true
  1987. );
  1988. }
  1989. $nonisam = false;
  1990. $is_innodb = (isset($showtable['Type']) && $showtable['Type'] == 'InnoDB');
  1991. if (isset($showtable['Type'])
  1992. && ! preg_match('@ISAM|HEAP@i', $showtable['Type'])
  1993. ) {
  1994. $nonisam = true;
  1995. }
  1996. // Gets some sizes
  1997. $mergetable = PMA_Table::isMerge($GLOBALS['db'], $GLOBALS['table']);
  1998. // this is to display for example 261.2 MiB instead of 268k KiB
  1999. $max_digits = 3;
  2000. $decimals = 1;
  2001. list($data_size, $data_unit) = PMA_Util::formatByteDown(
  2002. $showtable['Data_length'], $max_digits, $decimals
  2003. );
  2004. if ($mergetable == false) {
  2005. list($index_size, $index_unit) = PMA_Util::formatByteDown(
  2006. $showtable['Index_length'], $max_digits, $decimals
  2007. );
  2008. }
  2009. // InnoDB returns a huge value in Data_free, do not use it
  2010. if (! $is_innodb
  2011. && isset($showtable['Data_free'])
  2012. && $showtable['Data_free'] > 0
  2013. ) {
  2014. list($free_size, $free_unit) = PMA_Util::formatByteDown(
  2015. $showtable['Data_free'], $max_digits, $decimals
  2016. );
  2017. list($effect_size, $effect_unit) = PMA_Util::formatByteDown(
  2018. $showtable['Data_length'] + $showtable['Index_length']
  2019. - $showtable['Data_free'],
  2020. $max_digits, $decimals
  2021. );
  2022. } else {
  2023. list($effect_size, $effect_unit) = PMA_Util::formatByteDown(
  2024. $showtable['Data_length'] + $showtable['Index_length'],
  2025. $max_digits, $decimals
  2026. );
  2027. }
  2028. list($tot_size, $tot_unit) = PMA_Util::formatByteDown(
  2029. $showtable['Data_length'] + $showtable['Index_length'],
  2030. $max_digits, $decimals
  2031. );
  2032. if ($table_info_num_rows > 0) {
  2033. list($avg_size, $avg_unit) = PMA_Util::formatByteDown(
  2034. ($showtable['Data_length'] + $showtable['Index_length'])
  2035. / $showtable['Rows'],
  2036. 6, 1
  2037. );
  2038. }
  2039. // Displays them
  2040. $odd_row = false;
  2041. $html_output .= '<fieldset>'
  2042. . '<legend>' . __('Information') . '</legend>'
  2043. . '<a id="showusage"></a>';
  2044. if (! $tbl_is_view && ! $db_is_system_schema) {
  2045. $html_output .= '<table id="tablespaceusage" class="data">'
  2046. . '<caption class="tblHeaders">' . __('Space usage') . '</caption>'
  2047. . '<tbody>';
  2048. $html_output .= PMA_getHtmlForSpaceUsageTableRow(
  2049. $odd_row, __('Data'), $data_size, $data_unit
  2050. );
  2051. $odd_row = !$odd_row;
  2052. if (isset($index_size)) {
  2053. $html_output .= PMA_getHtmlForSpaceUsageTableRow(
  2054. $odd_row, __('Index'), $index_size, $index_unit
  2055. );
  2056. $odd_row = !$odd_row;
  2057. }
  2058. if (isset($free_size)) {
  2059. $html_output .= PMA_getHtmlForSpaceUsageTableRow(
  2060. $odd_row, __('Overhead'), $free_size, $free_unit
  2061. );
  2062. $html_output .= PMA_getHtmlForSpaceUsageTableRow(
  2063. $odd_row, __('Effective'), $effect_size, $effect_unit
  2064. );
  2065. $odd_row = !$odd_row;
  2066. }
  2067. if (isset($tot_size) && $mergetable == false) {
  2068. $html_output .= PMA_getHtmlForSpaceUsageTableRow(
  2069. $odd_row, __('Total'), $tot_size, $tot_unit
  2070. );
  2071. $odd_row = !$odd_row;
  2072. }
  2073. // Optimize link if overhead
  2074. if (isset($free_size) && !PMA_DRIZZLE
  2075. && ($tbl_storage_engine == 'MYISAM'
  2076. || $tbl_storage_engine == 'ARIA'
  2077. || $tbl_storage_engine == 'MARIA'
  2078. || $tbl_storage_engine == 'BDB')
  2079. ) {
  2080. $html_output .= PMA_getHtmlForOptimizeLink($url_query);
  2081. }
  2082. $html_output .= '</tbody>'
  2083. . '</table>';
  2084. }
  2085. $html_output .= getHtmlForRowStatsTable(
  2086. $showtable, $tbl_collation,
  2087. $is_innodb, $mergetable,
  2088. (isset ($avg_size) ? $avg_size : ''),
  2089. (isset ($avg_unit) ? $avg_unit : '')
  2090. );
  2091. $html_output .= '</fieldset>'
  2092. . '</div>';
  2093. return $html_output;
  2094. }
  2095. /**
  2096. * Displays HTML for changing one or more columns
  2097. *
  2098. * @param string $db database name
  2099. * @param string $table table name
  2100. * @param array $selected the selected columns
  2101. * @param string $action target script to call
  2102. *
  2103. * @return boolean $regenerate true if error occurred
  2104. *
  2105. */
  2106. function PMA_displayHtmlForColumnChange($db, $table, $selected, $action)
  2107. {
  2108. // $selected comes from multi_submits.inc.php
  2109. if (empty($selected)) {
  2110. $selected[] = $_REQUEST['field'];
  2111. $selected_cnt = 1;
  2112. } else { // from a multiple submit
  2113. $selected_cnt = count($selected);
  2114. }
  2115. /**
  2116. * @todo optimize in case of multiple fields to modify
  2117. */
  2118. $fields_meta = array();
  2119. for ($i = 0; $i < $selected_cnt; $i++) {
  2120. $fields_meta[] = $GLOBALS['dbi']->getColumns(
  2121. $db, $table, $selected[$i], true
  2122. );
  2123. }
  2124. $num_fields = count($fields_meta);
  2125. // set these globals because tbl_columns_definition_form.inc.php
  2126. // verifies them
  2127. // @todo: refactor tbl_columns_definition_form.inc.php so that it uses
  2128. // function params
  2129. $GLOBALS['action'] = 'tbl_structure.php';
  2130. $GLOBALS['num_fields'] = $num_fields;
  2131. // Get more complete field information.
  2132. // For now, this is done to obtain MySQL 4.1.2+ new TIMESTAMP options
  2133. // and to know when there is an empty DEFAULT value.
  2134. // Later, if the analyser returns more information, it
  2135. // could be executed to replace the info given by SHOW FULL COLUMNS FROM.
  2136. /**
  2137. * @todo put this code into a require()
  2138. * or maybe make it part of $GLOBALS['dbi']->getColumns();
  2139. */
  2140. // We also need this to correctly learn if a TIMESTAMP is NOT NULL, since
  2141. // SHOW FULL COLUMNS says NULL and SHOW CREATE TABLE says NOT NULL (tested
  2142. // in MySQL 4.0.25).
  2143. $show_create_table = $GLOBALS['dbi']->fetchValue(
  2144. 'SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.'
  2145. . PMA_Util::backquote($table),
  2146. 0, 1
  2147. );
  2148. $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
  2149. unset($show_create_table);
  2150. /**
  2151. * Form for changing properties.
  2152. */
  2153. include 'libraries/tbl_columns_definition_form.inc.php';
  2154. }
  2155. /**
  2156. * Verifies if some elements of a column have changed
  2157. *
  2158. * @param integer $i column index in the request
  2159. *
  2160. * @return boolean $alterTableNeeded true if we need to generate ALTER TABLE
  2161. *
  2162. */
  2163. function PMA_columnNeedsAlterTable($i)
  2164. {
  2165. // these two fields are checkboxes so might not be part of the
  2166. // request; therefore we define them to avoid notices below
  2167. if (! isset($_REQUEST['field_null'][$i])) {
  2168. $_REQUEST['field_null'][$i] = 'NO';
  2169. }
  2170. if (! isset($_REQUEST['field_extra'][$i])) {
  2171. $_REQUEST['field_extra'][$i] = '';
  2172. }
  2173. // field_name does not follow the convention (corresponds to field_orig)
  2174. if ($_REQUEST['field_attribute'][$i] != $_REQUEST['field_attribute_orig'][$i]
  2175. || $_REQUEST['field_collation'][$i] != $_REQUEST['field_collation_orig'][$i]
  2176. || $_REQUEST['field_comments'][$i] != $_REQUEST['field_comments_orig'][$i]
  2177. || $_REQUEST['field_default_value'][$i] != $_REQUEST['field_default_value_orig'][$i]
  2178. || $_REQUEST['field_default_type'][$i] != $_REQUEST['field_default_type_orig'][$i]
  2179. || $_REQUEST['field_extra'][$i] != $_REQUEST['field_extra_orig'][$i]
  2180. || $_REQUEST['field_length'][$i] != $_REQUEST['field_length_orig'][$i]
  2181. || $_REQUEST['field_name'][$i] != $_REQUEST['field_orig'][$i]
  2182. || $_REQUEST['field_null'][$i] != $_REQUEST['field_null_orig'][$i]
  2183. || $_REQUEST['field_type'][$i] != $_REQUEST['field_type_orig'][$i]
  2184. || ! empty($_REQUEST['field_move_to'][$i])
  2185. ) {
  2186. return true;
  2187. } else {
  2188. return false;
  2189. }
  2190. }
  2191. /**
  2192. * Update the table's structure based on $_REQUEST
  2193. *
  2194. * @param string $db database name
  2195. * @param string $table table name
  2196. *
  2197. * @return boolean $regenerate true if error occurred
  2198. *
  2199. */
  2200. function PMA_updateColumns($db, $table)
  2201. {
  2202. $err_url = 'tbl_structure.php?' . PMA_URL_getCommon($db, $table);
  2203. $regenerate = false;
  2204. $field_cnt = count($_REQUEST['field_name']);
  2205. $key_fields = array();
  2206. $changes = array();
  2207. for ($i = 0; $i < $field_cnt; $i++) {
  2208. if (PMA_columnNeedsAlterTable($i)) {
  2209. $changes[] = 'CHANGE ' . PMA_Table::generateAlter(
  2210. isset($_REQUEST['field_orig'][$i])
  2211. ? $_REQUEST['field_orig'][$i]
  2212. : '',
  2213. $_REQUEST['field_name'][$i],
  2214. $_REQUEST['field_type'][$i],
  2215. $_REQUEST['field_length'][$i],
  2216. $_REQUEST['field_attribute'][$i],
  2217. isset($_REQUEST['field_collation'][$i])
  2218. ? $_REQUEST['field_collation'][$i]
  2219. : '',
  2220. isset($_REQUEST['field_null'][$i])
  2221. ? $_REQUEST['field_null'][$i]
  2222. : 'NOT NULL',
  2223. $_REQUEST['field_default_type'][$i],
  2224. $_REQUEST['field_default_value'][$i],
  2225. isset($_REQUEST['field_extra'][$i])
  2226. ? $_REQUEST['field_extra'][$i]
  2227. : false,
  2228. isset($_REQUEST['field_comments'][$i])
  2229. ? $_REQUEST['field_comments'][$i]
  2230. : '',
  2231. $key_fields,
  2232. $i,
  2233. isset($_REQUEST['field_move_to'][$i])
  2234. ? $_REQUEST['field_move_to'][$i]
  2235. : ''
  2236. );
  2237. }
  2238. } // end for
  2239. $response = PMA_Response::getInstance();
  2240. if (count($changes) > 0) {
  2241. // Builds the primary keys statements and updates the table
  2242. $key_query = '';
  2243. /**
  2244. * this is a little bit more complex
  2245. *
  2246. * @todo if someone selects A_I when altering a column we need to check:
  2247. * - no other column with A_I
  2248. * - the column has an index, if not create one
  2249. *
  2250. */
  2251. // To allow replication, we first select the db to use
  2252. // and then run queries on this db.
  2253. if (! $GLOBALS['dbi']->selectDb($db)) {
  2254. PMA_Util::mysqlDie(
  2255. $GLOBALS['dbi']->getError(),
  2256. 'USE ' . PMA_Util::backquote($db) . ';',
  2257. '',
  2258. $err_url
  2259. );
  2260. }
  2261. $sql_query = 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ';
  2262. $sql_query .= implode(', ', $changes) . $key_query;
  2263. $sql_query .= ';';
  2264. $result = $GLOBALS['dbi']->tryQuery($sql_query);
  2265. if ($result !== false) {
  2266. $message = PMA_Message::success(
  2267. __('Table %1$s has been altered successfully.')
  2268. );
  2269. $message->addParam($table);
  2270. $response->addHTML(
  2271. PMA_Util::getMessage($message, $sql_query, 'success')
  2272. );
  2273. } else {
  2274. // An error happened while inserting/updating a table definition
  2275. $response->isSuccess(false);
  2276. $response->addJSON(
  2277. 'message',
  2278. PMA_Message::rawError(__('Query error') . ':<br />'.$GLOBALS['dbi']->getError())
  2279. );
  2280. $regenerate = true;
  2281. }
  2282. }
  2283. include_once 'libraries/transformations.lib.php';
  2284. // update field names in relation
  2285. if (isset($_REQUEST['field_orig']) && is_array($_REQUEST['field_orig'])) {
  2286. foreach ($_REQUEST['field_orig'] as $fieldindex => $fieldcontent) {
  2287. if ($_REQUEST['field_name'][$fieldindex] != $fieldcontent) {
  2288. PMA_REL_renameField(
  2289. $db, $table, $fieldcontent,
  2290. $_REQUEST['field_name'][$fieldindex]
  2291. );
  2292. }
  2293. }
  2294. }
  2295. // update mime types
  2296. if (isset($_REQUEST['field_mimetype'])
  2297. && is_array($_REQUEST['field_mimetype'])
  2298. && $GLOBALS['cfg']['BrowseMIME']
  2299. ) {
  2300. foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
  2301. if (isset($_REQUEST['field_name'][$fieldindex])
  2302. && strlen($_REQUEST['field_name'][$fieldindex])
  2303. ) {
  2304. PMA_setMIME(
  2305. $db, $table, $_REQUEST['field_name'][$fieldindex],
  2306. $mimetype,
  2307. $_REQUEST['field_transformation'][$fieldindex],
  2308. $_REQUEST['field_transformation_options'][$fieldindex]
  2309. );
  2310. }
  2311. }
  2312. }
  2313. return $regenerate;
  2314. }
  2315. /**
  2316. * Moves columns in the table's structure based on $_REQUEST
  2317. *
  2318. * @param string $db database name
  2319. * @param string $table table name
  2320. *
  2321. * @return void
  2322. */
  2323. function PMA_moveColumns($db, $table)
  2324. {
  2325. $GLOBALS['dbi']->selectDb($db);
  2326. /*
  2327. * load the definitions for all columns
  2328. */
  2329. $columns = $GLOBALS['dbi']->getColumnsFull($db, $table);
  2330. $column_names = array_keys($columns);
  2331. $changes = array();
  2332. $we_dont_change_keys = array();
  2333. // move columns from first to last
  2334. for ($i = 0, $l = count($_REQUEST['move_columns']); $i < $l; $i++) {
  2335. $column = $_REQUEST['move_columns'][$i];
  2336. // is this column already correctly placed?
  2337. if ($column_names[$i] == $column) {
  2338. continue;
  2339. }
  2340. // it is not, let's move it to index $i
  2341. $data = $columns[$column];
  2342. $extracted_columnspec = PMA_Util::extractColumnSpec($data['Type']);
  2343. if (isset($data['Extra'])
  2344. && $data['Extra'] == 'on update CURRENT_TIMESTAMP'
  2345. ) {
  2346. $extracted_columnspec['attribute'] = $data['Extra'];
  2347. unset($data['Extra']);
  2348. }
  2349. $current_timestamp = false;
  2350. if (($data['Type'] == 'timestamp' || $data['Type'] == 'datetime')
  2351. && $data['Default'] == 'CURRENT_TIMESTAMP'
  2352. ) {
  2353. $current_timestamp = true;
  2354. }
  2355. $default_type
  2356. = $data['Null'] === 'YES' && $data['Default'] === null
  2357. ? 'NULL'
  2358. : ($current_timestamp
  2359. ? 'CURRENT_TIMESTAMP'
  2360. : ($data['Default'] === null
  2361. ? 'NONE'
  2362. : 'USER_DEFINED'));
  2363. $changes[] = 'CHANGE ' . PMA_Table::generateAlter(
  2364. $column,
  2365. $column,
  2366. strtoupper($extracted_columnspec['type']),
  2367. $extracted_columnspec['spec_in_brackets'],
  2368. $extracted_columnspec['attribute'],
  2369. isset($data['Collation']) ? $data['Collation'] : '',
  2370. $data['Null'] === 'YES' ? 'NULL' : 'NOT NULL',
  2371. $default_type,
  2372. $current_timestamp ? '' : $data['Default'],
  2373. isset($data['Extra']) && $data['Extra'] !== '' ? $data['Extra'] : false,
  2374. isset($data['COLUMN_COMMENT']) && $data['COLUMN_COMMENT'] !== ''
  2375. ? $data['COLUMN_COMMENT'] : false,
  2376. $we_dont_change_keys,
  2377. $i,
  2378. $i === 0 ? '-first' : $column_names[$i - 1]
  2379. );
  2380. // update current column_names array, first delete old position
  2381. for ($j = 0, $ll = count($column_names); $j < $ll; $j++) {
  2382. if ($column_names[$j] == $column) {
  2383. unset($column_names[$j]);
  2384. }
  2385. }
  2386. // insert moved column
  2387. array_splice($column_names, $i, 0, $column);
  2388. }
  2389. $response = PMA_Response::getInstance();
  2390. if (empty($changes)) { // should never happen
  2391. $response->isSuccess(false);
  2392. exit;
  2393. }
  2394. $move_query = 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ';
  2395. $move_query .= implode(', ', $changes);
  2396. // move columns
  2397. $GLOBALS['dbi']->tryQuery($move_query);
  2398. $tmp_error = $GLOBALS['dbi']->getError();
  2399. if ($tmp_error) {
  2400. $response->isSuccess(false);
  2401. $response->addJSON('message', PMA_Message::error($tmp_error));
  2402. } else {
  2403. $message = PMA_Message::success(
  2404. __('The columns have been moved successfully.')
  2405. );
  2406. $response->addJSON('message', $message);
  2407. $response->addJSON('columns', $column_names);
  2408. }
  2409. exit;
  2410. }
  2411. /**
  2412. * Get columns with unique index
  2413. *
  2414. * @param string $db database name
  2415. * @param string $table tablename
  2416. *
  2417. * @return array $columns_with_unique_index An array of columns with unique index,
  2418. * with $column name as the array key
  2419. */
  2420. function PMA_getColumnsWithUniqueIndex($db ,$table)
  2421. {
  2422. $columns_with_unique_index = array();
  2423. foreach (PMA_Index::getFromTable($table, $db) as $index) {
  2424. if ($index->isUnique() && $index->getChoice() == 'UNIQUE') {
  2425. $columns = $index->getColumns();
  2426. foreach ($columns as $column_name => $dummy) {
  2427. $columns_with_unique_index[$column_name] = 1;
  2428. }
  2429. }
  2430. }
  2431. return $columns_with_unique_index;
  2432. }
  2433. /**
  2434. * Check column names for MySQL reserved words
  2435. *
  2436. * @param string $db database name
  2437. * @param string $table tablename
  2438. *
  2439. * @return array $messages array of PMA_Messages
  2440. */
  2441. function PMA_getReservedWordColumnNameMessages($db ,$table)
  2442. {
  2443. $messages = array();
  2444. if ($GLOBALS['cfg']['ReservedWordDisableWarning'] === false) {
  2445. $pma_table = new PMA_Table($table, $db);
  2446. $columns = $pma_table->getReservedColumnNames();
  2447. if (!empty($columns)) {
  2448. foreach ($columns as $column) {
  2449. $msg = PMA_message::notice(
  2450. __('The column name \'%s\' is a MySQL reserved keyword.')
  2451. );
  2452. $msg->addParam($column);
  2453. $messages[] = $msg;
  2454. }
  2455. }
  2456. }
  2457. return $messages;
  2458. }
  2459. /**
  2460. * Function to get the type of command for multiple field handling
  2461. *
  2462. * @return string
  2463. */
  2464. function PMA_getMultipleFieldCommandType()
  2465. {
  2466. $submit_mult = null;
  2467. if (isset($_REQUEST['submit_mult_change_x'])) {
  2468. $submit_mult = 'change';
  2469. } elseif (isset($_REQUEST['submit_mult_drop_x'])) {
  2470. $submit_mult = 'drop';
  2471. } elseif (isset($_REQUEST['submit_mult_primary_x'])) {
  2472. $submit_mult = 'primary';
  2473. } elseif (isset($_REQUEST['submit_mult_index_x'])) {
  2474. $submit_mult = 'index';
  2475. } elseif (isset($_REQUEST['submit_mult_unique_x'])) {
  2476. $submit_mult = 'unique';
  2477. } elseif (isset($_REQUEST['submit_mult_spatial_x'])) {
  2478. $submit_mult = 'spatial';
  2479. } elseif (isset($_REQUEST['submit_mult_fulltext_x'])) {
  2480. $submit_mult = 'ftext';
  2481. } elseif (isset($_REQUEST['submit_mult_browse_x'])) {
  2482. $submit_mult = 'browse';
  2483. } elseif (isset($_REQUEST['submit_mult'])) {
  2484. $submit_mult = $_REQUEST['submit_mult'];
  2485. } elseif (isset($_REQUEST['mult_btn']) && $_REQUEST['mult_btn'] == __('Yes')) {
  2486. $submit_mult = 'row_delete';
  2487. if (isset($_REQUEST['selected'])) {
  2488. $_REQUEST['selected_fld'] = $_REQUEST['selected'];
  2489. }
  2490. }
  2491. return $submit_mult;
  2492. }
  2493. /**
  2494. * Function to display table browse for selected columns
  2495. *
  2496. * @param string $db current database
  2497. * @param string $table current table
  2498. * @param string $goto goto page url
  2499. * @param string $pmaThemeImage URI of the pma theme image
  2500. *
  2501. * @return void
  2502. */
  2503. function PMA_displayTableBrowseForSelectedColumns($db, $table, $goto,
  2504. $pmaThemeImage
  2505. ) {
  2506. $GLOBALS['active_page'] = 'sql.php';
  2507. $sql_query = '';
  2508. foreach ($_REQUEST['selected_fld'] as $sval) {
  2509. if ($sql_query == '') {
  2510. $sql_query .= 'SELECT ' . PMA_Util::backquote($sval);
  2511. } else {
  2512. $sql_query .= ', ' . PMA_Util::backquote($sval);
  2513. }
  2514. }
  2515. $sql_query .= ' FROM ' . PMA_Util::backquote($db)
  2516. . '.' . PMA_Util::backquote($table);
  2517. // Parse and analyze the query
  2518. include_once 'libraries/parse_analyze.inc.php';
  2519. include_once 'libraries/sql.lib.php';
  2520. PMA_executeQueryAndSendQueryResponse(
  2521. $analyzed_sql_results, false, $db, $table, null, null, null, false,
  2522. null, null, null, null, $goto, $pmaThemeImage, null, null,
  2523. null, $sql_query, null, null
  2524. );
  2525. }
  2526. /**
  2527. * Function to check if a table is already in favorite list.
  2528. *
  2529. * @param string $db current database
  2530. * @param string $current_table current table
  2531. *
  2532. * @return true|false
  2533. */
  2534. function PMA_checkFavoriteTable($db, $current_table)
  2535. {
  2536. foreach ($_SESSION['tmpval']['favorite_tables'][$GLOBALS['server']] as $key => $value) {
  2537. if ($value['db'] == $db && $value['table'] == $current_table) {
  2538. return true;
  2539. }
  2540. }
  2541. return false;
  2542. }
  2543. /**
  2544. * Get HTML for favorite anchor.
  2545. *
  2546. * @param string $db current database
  2547. * @param string $current_table current table
  2548. * @param array $titles titles
  2549. *
  2550. * @return $html_output
  2551. */
  2552. function PMA_getHtmlForFavoriteAnchor($db, $current_table, $titles)
  2553. {
  2554. $html_output = '<a ';
  2555. $html_output .= 'id="' . md5($current_table)
  2556. . '_favorite_anchor" ';
  2557. $html_output .= 'class="ajax favorite_table_anchor';
  2558. // Check if current table is already in favorite list.
  2559. $already_favorite = PMA_checkFavoriteTable($db, $current_table);
  2560. $fav_params = array('db' => $db,
  2561. 'ajax_request' => true,
  2562. 'favorite_table' => $current_table,
  2563. (($already_favorite ? 'remove' : 'add') . '_favorite') => true
  2564. );
  2565. $fav_url = 'db_structure.php' . PMA_URL_getCommon($fav_params);
  2566. $html_output .= '" ';
  2567. $html_output .= 'href="' . $fav_url
  2568. . '" title="' . ($already_favorite ? __("Remove from Favorites")
  2569. : __("Add to Favorites"))
  2570. . '" data-favtargets="'
  2571. . md5($db . "." . $current_table)
  2572. . '" >'
  2573. . (!$already_favorite ? $titles['NoFavorite']
  2574. : $titles['Favorite']) . '</a>';
  2575. return $html_output;
  2576. }
  2577. ?>