structure.lib.php 87 KB

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