operations.lib.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * set of functions with the operations section in pma
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Get HTML output for database comment
  13. *
  14. * @param string $db database name
  15. *
  16. * @return string $html_output
  17. */
  18. function PMA_getHtmlForDatabaseComment($db)
  19. {
  20. $html_output = '<div class="operations_half_width">'
  21. . '<form method="post" action="db_operations.php" id="formDatabaseComment">'
  22. . PMA_URL_getHiddenInputs($db)
  23. . '<fieldset>'
  24. . '<legend>';
  25. if (PMA_Util::showIcons('ActionLinksMode')) {
  26. $html_output .= '<img class="icon ic_b_comment" '
  27. . 'src="themes/dot.gif" alt="" />';
  28. }
  29. $html_output .= __('Database comment:');
  30. $html_output .= '</legend>';
  31. $html_output .= '<input type="text" name="comment" '
  32. . 'class="textfield" size="30"'
  33. . 'value="' . htmlspecialchars(PMA_getDBComment($db)) . '" />'
  34. . '</fieldset>';
  35. $html_output .= '<fieldset class="tblFooters">'
  36. . '<input type="submit" value="' . __('Go') . '" />'
  37. . '</fieldset>'
  38. . '</form>'
  39. . '</div>';
  40. return $html_output;
  41. }
  42. /**
  43. * Get HTML output for rename database
  44. *
  45. * @param string $db database name
  46. *
  47. * @return string $html_output
  48. */
  49. function PMA_getHtmlForRenameDatabase($db)
  50. {
  51. $html_output = '<div class="operations_half_width">'
  52. . '<form id="rename_db_form" '
  53. . 'class="ajax" '
  54. . 'method="post" action="db_operations.php" '
  55. . 'onsubmit="return emptyFormElements(this, \'newname\')">';
  56. if (isset($_REQUEST['db_collation'])) {
  57. $html_output .= '<input type="hidden" name="db_collation" '
  58. . 'value="' . $_REQUEST['db_collation']
  59. . '" />' . "\n";
  60. }
  61. $html_output .= '<input type="hidden" name="what" value="data" />'
  62. . '<input type="hidden" name="db_rename" value="true" />'
  63. . PMA_URL_getHiddenInputs($db)
  64. . '<fieldset>'
  65. . '<legend>';
  66. if (PMA_Util::showIcons('ActionLinksMode')) {
  67. $html_output .= PMA_Util::getImage('b_edit.png');
  68. }
  69. $html_output .= __('Rename database to:')
  70. . '</legend>';
  71. $html_output .= '<input id="new_db_name" type="text" name="newname" '
  72. . 'size="30" class="textfield" value="" required="required" />'
  73. . '</fieldset>'
  74. . '<fieldset class="tblFooters">'
  75. . '<input id="rename_db_input" type="submit" value="' . __('Go') . '" />'
  76. . '</fieldset>'
  77. . '</form>'
  78. . '</div>';
  79. return $html_output;
  80. }
  81. /**
  82. * Get HTML for database drop link
  83. *
  84. * @param string $db database name
  85. *
  86. * @return string $html_output
  87. */
  88. function PMA_getHtmlForDropDatabaseLink($db)
  89. {
  90. $this_sql_query = 'DROP DATABASE ' . PMA_Util::backquote($db);
  91. $this_url_params = array(
  92. 'sql_query' => $this_sql_query,
  93. 'back' => 'db_operations.php',
  94. 'goto' => 'index.php',
  95. 'reload' => '1',
  96. 'purge' => '1',
  97. 'message_to_show' => sprintf(
  98. __('Database %s has been dropped.'),
  99. htmlspecialchars(PMA_Util::backquote($db))
  100. ),
  101. 'db' => null,
  102. );
  103. $html_output = '<div class="operations_half_width">'
  104. . '<fieldset class="caution">';
  105. $html_output .= '<legend>';
  106. if (PMA_Util::showIcons('ActionLinksMode')) {
  107. $html_output .= PMA_Util::getImage('b_deltbl.png');
  108. }
  109. $html_output .= __('Remove database')
  110. . '</legend>';
  111. $html_output .= '<ul>';
  112. $html_output .= PMA_getDeleteDataOrTablelink(
  113. $this_url_params,
  114. 'DROP_DATABASE',
  115. __('Drop the database (DROP)'),
  116. 'drop_db_anchor'
  117. );
  118. $html_output .= '</ul></fieldset>'
  119. . '</div>';
  120. return $html_output;
  121. }
  122. /**
  123. * Get HTML snippet for copy database
  124. *
  125. * @param string $db database name
  126. *
  127. * @return string $html_output
  128. */
  129. function PMA_getHtmlForCopyDatabase($db)
  130. {
  131. $drop_clause = 'DROP TABLE / DROP VIEW';
  132. $choices = array(
  133. 'structure' => __('Structure only'),
  134. 'data' => __('Structure and data'),
  135. 'dataonly' => __('Data only')
  136. );
  137. if (isset($_COOKIE)
  138. && isset($_COOKIE['pma_switch_to_new'])
  139. && $_COOKIE['pma_switch_to_new'] == 'true'
  140. ) {
  141. $pma_switch_to_new = 'true';
  142. }
  143. $html_output = '<div class="operations_half_width clearfloat">';
  144. $html_output .= '<form id="copy_db_form" '
  145. . 'class="ajax" '
  146. . 'method="post" action="db_operations.php"'
  147. . 'onsubmit="return emptyFormElements(this, \'newname\')">';
  148. if (isset($_REQUEST['db_collation'])) {
  149. $html_output .= '<input type="hidden" name="db_collation" '
  150. . 'value="' . $_REQUEST['db_collation'] . '" />' . "\n";
  151. }
  152. $html_output .= '<input type="hidden" name="db_copy" value="true" />' . "\n"
  153. . PMA_URL_getHiddenInputs($db);
  154. $html_output .= '<fieldset>'
  155. . '<legend>';
  156. if (PMA_Util::showIcons('ActionLinksMode')) {
  157. $html_output .= PMA_Util::getImage('b_edit.png');
  158. }
  159. $html_output .= __('Copy database to:')
  160. . '</legend>'
  161. . '<input type="text" name="newname" size="30" '
  162. . 'class="textfield" value="" required="required" /><br />'
  163. . PMA_Util::getRadioFields(
  164. 'what', $choices, 'data', true
  165. );
  166. $html_output .= '<input type="checkbox" name="create_database_before_copying" '
  167. . 'value="1" id="checkbox_create_database_before_copying"'
  168. . 'checked="checked" />';
  169. $html_output .= '<label for="checkbox_create_database_before_copying">'
  170. . __('CREATE DATABASE before copying') . '</label><br />';
  171. $html_output .= '<input type="checkbox" name="drop_if_exists" value="true"'
  172. . 'id="checkbox_drop" />';
  173. $html_output .= '<label for="checkbox_drop">'
  174. . sprintf(__('Add %s'), $drop_clause)
  175. . '</label><br />';
  176. $html_output .= '<input type="checkbox" name="sql_auto_increment" value="1" '
  177. . 'checked="checked" id="checkbox_auto_increment" />';
  178. $html_output .= '<label for="checkbox_auto_increment">'
  179. . __('Add AUTO_INCREMENT value') . '</label><br />';
  180. $html_output .= '<input type="checkbox" name="add_constraints" value="1"'
  181. . 'id="checkbox_constraints" />';
  182. $html_output .= '<label for="checkbox_constraints">'
  183. . __('Add constraints') . '</label><br />';
  184. $html_output .= '<input type="checkbox" name="switch_to_new" value="true"'
  185. . 'id="checkbox_switch"'
  186. . ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true')
  187. ? ' checked="checked"'
  188. : '')
  189. . '/>';
  190. $html_output .= '<label for="checkbox_switch">'
  191. . __('Switch to copied database') . '</label>'
  192. . '</fieldset>';
  193. $html_output .= '<fieldset class="tblFooters">'
  194. . '<input type="submit" name="submit_copy" value="' . __('Go') . '" />'
  195. . '</fieldset>'
  196. . '</form>'
  197. . '</div>';
  198. return $html_output;
  199. }
  200. /**
  201. * Get HTML snippet for change database charset
  202. *
  203. * @param string $db database name
  204. * @param string $table tabel name
  205. *
  206. * @return string $html_output
  207. */
  208. function PMA_getHtmlForChangeDatabaseCharset($db, $table)
  209. {
  210. $html_output = '<div class="operations_half_width">'
  211. . '<form id="change_db_charset_form" ';
  212. $html_output .= 'class="ajax" ';
  213. $html_output .= 'method="post" action="db_operations.php">';
  214. $html_output .= PMA_URL_getHiddenInputs($db, $table);
  215. $html_output .= '<fieldset>' . "\n"
  216. . ' <legend>';
  217. if (PMA_Util::showIcons('ActionLinksMode')) {
  218. $html_output .= PMA_Util::getImage('s_asci.png');
  219. }
  220. $html_output .= '<label for="select_db_collation">' . __('Collation')
  221. . ':</label>' . "\n"
  222. . '</legend>' . "\n"
  223. . PMA_generateCharsetDropdownBox(
  224. PMA_CSDROPDOWN_COLLATION,
  225. 'db_collation',
  226. 'select_db_collation',
  227. isset($_REQUEST['db_collation']) ? $_REQUEST['db_collation'] : '',
  228. false,
  229. 3
  230. )
  231. . '</fieldset>'
  232. . '<fieldset class="tblFooters">'
  233. . '<input type="submit" name="submitcollation"'
  234. . ' value="' . __('Go') . '" />' . "\n"
  235. . '</fieldset>' . "\n"
  236. . '</form></div>' . "\n";
  237. return $html_output;
  238. }
  239. /**
  240. * Get HTML snippet for export relational schema view
  241. *
  242. * @param string $url_query Query string for link
  243. *
  244. * @return string $html_output
  245. */
  246. function PMA_getHtmlForExportRelationalSchemaView($url_query)
  247. {
  248. $html_output = '<div class="operations_full_width">'
  249. . '<fieldset><a href="schema_edit.php?' . $url_query . '">';
  250. if (PMA_Util::showIcons('ActionLinksMode')) {
  251. $html_output .= PMA_Util::getImage(
  252. 'b_edit.png'
  253. );
  254. }
  255. $html_output .= __('Edit or export relational schema')
  256. . '</a></fieldset>'
  257. . '</div>';
  258. return $html_output;
  259. }
  260. /**
  261. * Run the Procedure definitions and function definitions
  262. *
  263. * to avoid selecting alternatively the current and new db
  264. * we would need to modify the CREATE definitions to qualify
  265. * the db name
  266. *
  267. * @param string $db database name
  268. *
  269. * @return void
  270. */
  271. function PMA_runProcedureAndFunctionDefinitions($db)
  272. {
  273. $procedure_names = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'PROCEDURE');
  274. if ($procedure_names) {
  275. foreach ($procedure_names as $procedure_name) {
  276. $GLOBALS['dbi']->selectDb($db);
  277. $tmp_query = $GLOBALS['dbi']->getDefinition(
  278. $db, 'PROCEDURE', $procedure_name
  279. );
  280. // collect for later display
  281. $GLOBALS['sql_query'] .= "\n" . $tmp_query;
  282. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  283. $GLOBALS['dbi']->query($tmp_query);
  284. }
  285. }
  286. $function_names = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'FUNCTION');
  287. if ($function_names) {
  288. foreach ($function_names as $function_name) {
  289. $GLOBALS['dbi']->selectDb($db);
  290. $tmp_query = $GLOBALS['dbi']->getDefinition(
  291. $db, 'FUNCTION', $function_name
  292. );
  293. // collect for later display
  294. $GLOBALS['sql_query'] .= "\n" . $tmp_query;
  295. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  296. $GLOBALS['dbi']->query($tmp_query);
  297. }
  298. }
  299. }
  300. /**
  301. * Get sql query and create database before copy
  302. *
  303. * @return string $sql_query
  304. */
  305. function PMA_getSqlQueryAndCreateDbBeforeCopy()
  306. {
  307. // lower_case_table_names=1 `DB` becomes `db`
  308. if (! PMA_DRIZZLE) {
  309. $lowerCaseTableNames = $GLOBALS['dbi']->fetchValue(
  310. 'SHOW VARIABLES LIKE "lower_case_table_names"', 0, 1
  311. );
  312. if ($lowerCaseTableNames === '1') {
  313. $_REQUEST['newname'] = $GLOBALS['PMA_String']->strtolower(
  314. $_REQUEST['newname']
  315. );
  316. }
  317. }
  318. $local_query = 'CREATE DATABASE IF NOT EXISTS '
  319. . PMA_Util::backquote($_REQUEST['newname']);
  320. if (isset($_REQUEST['db_collation'])) {
  321. $local_query .= ' DEFAULT'
  322. . PMA_generateCharsetQueryPart($_REQUEST['db_collation']);
  323. }
  324. $local_query .= ';';
  325. $sql_query = $local_query;
  326. // save the original db name because Tracker.class.php which
  327. // may be called under $GLOBALS['dbi']->query() changes $GLOBALS['db']
  328. // for some statements, one of which being CREATE DATABASE
  329. $original_db = $GLOBALS['db'];
  330. $GLOBALS['dbi']->query($local_query);
  331. $GLOBALS['db'] = $original_db;
  332. // Set the SQL mode to NO_AUTO_VALUE_ON_ZERO to prevent MySQL from creating
  333. // export statements it cannot import
  334. $sql_set_mode = "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'";
  335. $GLOBALS['dbi']->query($sql_set_mode);
  336. // rebuild the database list because PMA_Table::moveCopy
  337. // checks in this list if the target db exists
  338. $GLOBALS['pma']->databases->build();
  339. return $sql_query;
  340. }
  341. /**
  342. * remove all foreign key constraints and return
  343. * sql constraints query for full database
  344. *
  345. * @param array $tables_full array of all tables in given db or dbs
  346. * @param ExportSql $export_sql_plugin export plugin instance
  347. * @param boolean $move whether database name is empty or not
  348. * @param string $db database name
  349. *
  350. * @return string sql constraints query for full databases
  351. */
  352. function PMA_getSqlConstraintsQueryForFullDb(
  353. $tables_full, $export_sql_plugin, $move, $db
  354. ) {
  355. global $sql_constraints, $sql_drop_foreign_keys;
  356. $sql_constraints_query_full_db = array();
  357. foreach ($tables_full as $each_table => $tmp) {
  358. /* Following globals are set in getTableDef */
  359. $sql_constraints = '';
  360. $sql_drop_foreign_keys = '';
  361. $export_sql_plugin->getTableDef(
  362. $db, $each_table, "\n", '', false, false, false, false
  363. );
  364. if ($move && ! empty($sql_drop_foreign_keys)) {
  365. $GLOBALS['dbi']->query($sql_drop_foreign_keys);
  366. }
  367. // keep the constraint we just dropped
  368. if (! empty($sql_constraints)) {
  369. $sql_constraints_query_full_db[] = $sql_constraints;
  370. }
  371. }
  372. return $sql_constraints_query_full_db;
  373. }
  374. /**
  375. * Get views as an array and create SQL view stand-in
  376. *
  377. * @param array $tables_full array of all tables in given db or dbs
  378. * @param ExportSql $export_sql_plugin export plugin instance
  379. * @param string $db database name
  380. *
  381. * @return array $views
  382. */
  383. function PMA_getViewsAndCreateSqlViewStandIn(
  384. $tables_full, $export_sql_plugin, $db
  385. ) {
  386. $views = array();
  387. foreach ($tables_full as $each_table => $tmp) {
  388. // to be able to rename a db containing views,
  389. // first all the views are collected and a stand-in is created
  390. // the real views are created after the tables
  391. if (PMA_Table::isView($db, $each_table)) {
  392. $views[] = $each_table;
  393. // Create stand-in definition to resolve view dependencies
  394. $sql_view_standin = $export_sql_plugin->getTableDefStandIn(
  395. $db, $each_table, "\n"
  396. );
  397. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  398. $GLOBALS['dbi']->query($sql_view_standin);
  399. $GLOBALS['sql_query'] .= "\n" . $sql_view_standin;
  400. }
  401. }
  402. return $views;
  403. }
  404. /**
  405. * Get sql query for copy/rename table and boolean for whether copy/rename or not
  406. *
  407. * @param array $tables_full array of all tables in given db or dbs
  408. * @param string $sql_query sql query for all operations
  409. * @param boolean $move whether database name is empty or not
  410. * @param string $db database name
  411. *
  412. * @return array ($sql_query, $error)
  413. */
  414. function PMA_getSqlQueryForCopyTable($tables_full, $sql_query, $move, $db)
  415. {
  416. $error = false;
  417. foreach ($tables_full as $each_table => $tmp) {
  418. // skip the views; we have creted stand-in definitions
  419. if (PMA_Table::isView($db, $each_table)) {
  420. continue;
  421. }
  422. $back = $sql_query;
  423. $sql_query = '';
  424. // value of $what for this table only
  425. $this_what = $_REQUEST['what'];
  426. // do not copy the data from a Merge table
  427. // note: on the calling FORM, 'data' means 'structure and data'
  428. if (PMA_Table::isMerge($db, $each_table)) {
  429. if ($this_what == 'data') {
  430. $this_what = 'structure';
  431. }
  432. if ($this_what == 'dataonly') {
  433. $this_what = 'nocopy';
  434. }
  435. }
  436. if ($this_what != 'nocopy') {
  437. // keep the triggers from the original db+table
  438. // (third param is empty because delimiters are only intended
  439. // for importing via the mysql client or our Import feature)
  440. $triggers = $GLOBALS['dbi']->getTriggers($db, $each_table, '');
  441. if (! PMA_Table::moveCopy(
  442. $db, $each_table, $_REQUEST['newname'], $each_table,
  443. (isset($this_what) ? $this_what : 'data'),
  444. $move, 'db_copy'
  445. )) {
  446. $error = true;
  447. // $sql_query is filled by PMA_Table::moveCopy()
  448. $sql_query = $back . $sql_query;
  449. break;
  450. }
  451. // apply the triggers to the destination db+table
  452. if ($triggers) {
  453. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  454. foreach ($triggers as $trigger) {
  455. $GLOBALS['dbi']->query($trigger['create']);
  456. $GLOBALS['sql_query'] .= "\n" . $trigger['create'] . ';';
  457. }
  458. }
  459. // this does not apply to a rename operation
  460. if (isset($_REQUEST['add_constraints'])
  461. && ! empty($GLOBALS['sql_constraints_query'])
  462. ) {
  463. $GLOBALS['sql_constraints_query_full_db'][]
  464. = $GLOBALS['sql_constraints_query'];
  465. unset($GLOBALS['sql_constraints_query']);
  466. }
  467. }
  468. // $sql_query is filled by PMA_Table::moveCopy()
  469. $sql_query = $back . $sql_query;
  470. }
  471. return array($sql_query, $error);
  472. }
  473. /**
  474. * Run the EVENT definition for selected database
  475. *
  476. * to avoid selecting alternatively the current and new db
  477. * we would need to modify the CREATE definitions to qualify
  478. * the db name
  479. *
  480. * @param string $db database name
  481. *
  482. * @return void
  483. */
  484. function PMA_runEventDefinitionsForDb($db)
  485. {
  486. $event_names = $GLOBALS['dbi']->fetchResult(
  487. 'SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \''
  488. . PMA_Util::sqlAddSlashes($db, true) . '\';'
  489. );
  490. if ($event_names) {
  491. foreach ($event_names as $event_name) {
  492. $GLOBALS['dbi']->selectDb($db);
  493. $tmp_query = $GLOBALS['dbi']->getDefinition($db, 'EVENT', $event_name);
  494. // collect for later display
  495. $GLOBALS['sql_query'] .= "\n" . $tmp_query;
  496. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  497. $GLOBALS['dbi']->query($tmp_query);
  498. }
  499. }
  500. }
  501. /**
  502. * Handle the views, return the boolean value whether table rename/copy or not
  503. *
  504. * @param array $views views as an array
  505. * @param boolean $move whether database name is empty or not
  506. * @param string $db database name
  507. *
  508. * @return boolean $_error whether table rename/copy or not
  509. */
  510. function PMA_handleTheViews($views, $move, $db)
  511. {
  512. $_error = false;
  513. // temporarily force to add DROP IF EXIST to CREATE VIEW query,
  514. // to remove stand-in VIEW that was created earlier
  515. // ( $_REQUEST['drop_if_exists'] is used in moveCopy() )
  516. if (isset($_REQUEST['drop_if_exists'])) {
  517. $temp_drop_if_exists = $_REQUEST['drop_if_exists'];
  518. }
  519. $_REQUEST['drop_if_exists'] = 'true';
  520. foreach ($views as $view) {
  521. $copying_succeeded = PMA_Table::moveCopy(
  522. $db, $view, $_REQUEST['newname'], $view, 'structure', $move, 'db_copy'
  523. );
  524. if (! $copying_succeeded) {
  525. $_error = true;
  526. break;
  527. }
  528. }
  529. unset($_REQUEST['drop_if_exists']);
  530. if (isset($temp_drop_if_exists)) {
  531. // restore previous value
  532. $_REQUEST['drop_if_exists'] = $temp_drop_if_exists;
  533. }
  534. return $_error;
  535. }
  536. /**
  537. * Create all accumulated constraaints
  538. *
  539. * @return void
  540. */
  541. function PMA_createAllAccumulatedConstraints()
  542. {
  543. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  544. foreach ($GLOBALS['sql_constraints_query_full_db'] as $one_query) {
  545. $GLOBALS['dbi']->query($one_query);
  546. // and prepare to display them
  547. $GLOBALS['sql_query'] .= "\n" . $one_query;
  548. }
  549. unset($GLOBALS['sql_constraints_query_full_db']);
  550. }
  551. /**
  552. * Duplicate the bookmarks for the db (done once for each db)
  553. *
  554. * @param boolean $_error whether table rename/copy or not
  555. * @param string $db database name
  556. *
  557. * @return void
  558. */
  559. function PMA_duplicateBookmarks($_error, $db)
  560. {
  561. if (! $_error && $db != $_REQUEST['newname']) {
  562. $get_fields = array('user', 'label', 'query');
  563. $where_fields = array('dbase' => $db);
  564. $new_fields = array('dbase' => $_REQUEST['newname']);
  565. PMA_Table::duplicateInfo(
  566. 'bookmarkwork', 'bookmark', $get_fields,
  567. $where_fields, $new_fields
  568. );
  569. }
  570. }
  571. /**
  572. * Get the HTML snippet for order the table
  573. *
  574. * @param array $columns columns array
  575. *
  576. * @return string $html_out
  577. */
  578. function PMA_getHtmlForOrderTheTable($columns)
  579. {
  580. $html_output = '<div class="operations_half_width">';
  581. $html_output .= '<form method="post" id="alterTableOrderby" '
  582. . 'action="tbl_operations.php">';
  583. $html_output .= PMA_URL_getHiddenInputs(
  584. $GLOBALS['db'], $GLOBALS['table']
  585. );
  586. $html_output .= '<fieldset id="fieldset_table_order">'
  587. . '<legend>' . __('Alter table order by') . '</legend>'
  588. . '<select name="order_field">';
  589. foreach ($columns as $fieldname) {
  590. $html_output .= '<option '
  591. . 'value="' . htmlspecialchars($fieldname['Field']) . '">'
  592. . htmlspecialchars($fieldname['Field']) . '</option>' . "\n";
  593. }
  594. $html_output .= '</select> ' . __('(singly)') . ' '
  595. . '<br />'
  596. . '<input id="order_order_asc" name="order_order"'
  597. . ' type="radio" value="asc" checked="checked" />'
  598. . '<label for="order_order_asc">' . __('Ascending') . '</label>'
  599. . '<input id="order_order_desc" name="order_order"'
  600. . ' type="radio" value="desc" />'
  601. . '<label for="order_order_desc">' . __('Descending') . '</label>'
  602. . '</fieldset>'
  603. . '<fieldset class="tblFooters">'
  604. . '<input type="hidden" name="submitorderby" value="1" />'
  605. . '<input type="submit" value="' . __('Go') . '" />'
  606. . '</fieldset>'
  607. . '</form>'
  608. . '</div>';
  609. return $html_output;
  610. }
  611. /**
  612. * Get the HTML snippet for move table
  613. *
  614. * @return string $html_output
  615. */
  616. function PMA_getHtmlForMoveTable()
  617. {
  618. $html_output = '<div class="operations_half_width">';
  619. $html_output .= '<form method="post" action="tbl_operations.php"'
  620. . ' id="moveTableForm" class="ajax"'
  621. . ' onsubmit="return emptyFormElements(this, \'new_name\')">'
  622. . PMA_URL_getHiddenInputs($GLOBALS['db'], $GLOBALS['table']);
  623. $html_output .= '<input type="hidden" name="reload" value="1" />'
  624. . '<input type="hidden" name="what" value="data" />'
  625. . '<fieldset id="fieldset_table_rename">';
  626. $html_output .= '<legend>' . __('Move table to (database<b>.</b>table):')
  627. . '</legend>';
  628. if (count($GLOBALS['pma']->databases) > $GLOBALS['cfg']['MaxDbList']) {
  629. $html_output .= '<input type="text" maxlength="100" size="30" '
  630. . 'name="target_db" value="' . htmlspecialchars($GLOBALS['db'])
  631. . '"/>';
  632. } else {
  633. $html_output .= '<select class="halfWidth" name="target_db">'
  634. . $GLOBALS['pma']->databases->getHtmlOptions(true, false)
  635. . '</select>';
  636. }
  637. $html_output .= '&nbsp;<strong>.</strong>&nbsp;';
  638. $html_output .= '<input class="halfWidth" type="text" size="20" name="new_name"'
  639. . ' onfocus="this.select()" required="required" '
  640. . 'value="' . htmlspecialchars($GLOBALS['table']) . '" /><br />';
  641. // starting with MySQL 5.0.24, SHOW CREATE TABLE includes the AUTO_INCREMENT
  642. // next value but users can decide if they want it or not for the operation
  643. $html_output .= '<input type="checkbox" name="sql_auto_increment" '
  644. . 'value="1" id="checkbox_auto_increment_mv" checked="checked" />'
  645. . '<label for="checkbox_auto_increment_mv">'
  646. . __('Add AUTO_INCREMENT value')
  647. . '</label><br />'
  648. . '</fieldset>';
  649. $html_output .= '<fieldset class="tblFooters">'
  650. . '<input type="submit" name="submit_move" value="' . __('Go') . '" />'
  651. . '</fieldset>'
  652. . '</form>'
  653. . '</div>';
  654. return $html_output;
  655. }
  656. /**
  657. * Get the HTML div for Table option
  658. *
  659. * @param string $comment Comment
  660. * @param array $tbl_collation table collation
  661. * @param string $tbl_storage_engine table storage engine
  662. * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not
  663. * @param boolean $is_isam whether ISAM or not
  664. * @param array $pack_keys pack keys
  665. * @param string $auto_increment value of auto increment
  666. * @param string $delay_key_write delay key write
  667. * @param string $transactional value of transactional
  668. * @param string $page_checksum value of page checksum
  669. * @param boolean $is_innodb whether INNODB or not
  670. * @param boolean $is_pbxt whether PBXT or not
  671. * @param boolean $is_aria whether ARIA or not
  672. * @param string $checksum the checksum
  673. *
  674. * @return string $html_output
  675. */
  676. function PMA_getTableOptionDiv($comment, $tbl_collation, $tbl_storage_engine,
  677. $is_myisam_or_aria, $is_isam, $pack_keys, $auto_increment, $delay_key_write,
  678. $transactional, $page_checksum, $is_innodb, $is_pbxt, $is_aria, $checksum
  679. ) {
  680. $html_output = '<div class="operations_half_width clearfloat">';
  681. $html_output .= '<form method="post" action="tbl_operations.php"';
  682. $html_output .= ' id="tableOptionsForm" class="ajax">';
  683. $html_output .= PMA_URL_getHiddenInputs(
  684. $GLOBALS['db'], $GLOBALS['table']
  685. );
  686. $html_output .= '<input type="hidden" name="reload" value="1" />';
  687. $html_output .= PMA_getTableOptionFieldset(
  688. $comment, $tbl_collation,
  689. $tbl_storage_engine, $is_myisam_or_aria, $is_isam, $pack_keys,
  690. $delay_key_write, $auto_increment, $transactional, $page_checksum,
  691. $is_innodb, $is_pbxt, $is_aria, $checksum
  692. );
  693. $html_output .= '<fieldset class="tblFooters">'
  694. . '<input type="hidden" name="submitoptions" value="1" />'
  695. . '<input type="submit" value="' . __('Go') . '" />'
  696. . '</fieldset>'
  697. . '</form>'
  698. . '</div>';
  699. return $html_output;
  700. }
  701. /**
  702. * Get HTML fieldset for Table option, it contains HTML table for options
  703. *
  704. * @param string $comment Comment
  705. * @param array $tbl_collation table collation
  706. * @param string $tbl_storage_engine table storage engine
  707. * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not
  708. * @param boolean $is_isam whether ISAM or not
  709. * @param array $pack_keys pack keys
  710. * @param string $delay_key_write delay key write
  711. * @param string $auto_increment value of auto increment
  712. * @param string $transactional value of transactional
  713. * @param string $page_checksum value of page checksum
  714. * @param boolean $is_innodb whether INNODB or not
  715. * @param boolean $is_pbxt whether PBXT or not
  716. * @param boolean $is_aria whether ARIA or not
  717. * @param string $checksum the checksum
  718. *
  719. * @return string $html_output
  720. */
  721. function PMA_getTableOptionFieldset($comment, $tbl_collation,
  722. $tbl_storage_engine, $is_myisam_or_aria, $is_isam, $pack_keys,
  723. $delay_key_write, $auto_increment, $transactional,
  724. $page_checksum, $is_innodb, $is_pbxt, $is_aria, $checksum
  725. ) {
  726. $html_output = '<fieldset>'
  727. . '<legend>' . __('Table options') . '</legend>';
  728. $html_output .= '<table>';
  729. //Change table name
  730. $html_output .= '<tr><td>' . __('Rename table to') . '</td>'
  731. . '<td>'
  732. . '<input type="text" size="20" name="new_name" onfocus="this.select()"'
  733. . 'value="' . htmlspecialchars($GLOBALS['table'])
  734. . '" required="required" />'
  735. . '</td>'
  736. . '</tr>';
  737. //Table comments
  738. $html_output .= '<tr><td>' . __('Table comments') . '</td>'
  739. . '<td><input type="text" name="comment" maxlength="60" size="30"'
  740. . 'value="' . htmlspecialchars($comment) . '" onfocus="this.select()" />'
  741. . '<input type="hidden" name="prev_comment" value="'
  742. . htmlspecialchars($comment) . '" />'
  743. . '</td>'
  744. . '</tr>';
  745. //Storage engine
  746. $html_output .= '<tr><td>' . __('Storage Engine')
  747. . PMA_Util::showMySQLDocu('Storage_engines')
  748. . '</td>'
  749. . '<td>'
  750. . PMA_StorageEngine::getHtmlSelect(
  751. 'new_tbl_storage_engine', null, $tbl_storage_engine
  752. )
  753. . '</td>'
  754. . '</tr>';
  755. //Table character set
  756. $html_output .= '<tr><td>' . __('Collation') . '</td>'
  757. . '<td>'
  758. . PMA_generateCharsetDropdownBox(
  759. PMA_CSDROPDOWN_COLLATION,
  760. 'tbl_collation', null, $tbl_collation, false, 3
  761. )
  762. . '</td>'
  763. . '</tr>';
  764. if ($is_myisam_or_aria || $is_isam) {
  765. $html_output .= '<tr>'
  766. . '<td><label for="new_pack_keys">PACK_KEYS</label></td>'
  767. . '<td><select name="new_pack_keys" id="new_pack_keys">';
  768. $html_output .= '<option value="DEFAULT"';
  769. if ($pack_keys == 'DEFAULT') {
  770. $html_output .= 'selected="selected"';
  771. }
  772. $html_output .= '>DEFAULT</option>
  773. <option value="0"';
  774. if ($pack_keys == '0') {
  775. $html_output .= 'selected="selected"';
  776. }
  777. $html_output .= '>0</option>
  778. <option value="1" ';
  779. if ($pack_keys == '1') {
  780. $html_output .= 'selected="selected"';
  781. }
  782. $html_output .= '>1</option>'
  783. . '</select>'
  784. . '</td>'
  785. . '</tr>';
  786. } // end if (MYISAM|ISAM)
  787. if ($is_myisam_or_aria) {
  788. $html_output .= PMA_getHtmlForTableRow(
  789. 'new_checksum',
  790. 'CHECKSUM',
  791. $checksum
  792. );
  793. $html_output .= PMA_getHtmlForTableRow(
  794. 'new_delay_key_write',
  795. 'DELAY_KEY_WRITE',
  796. $delay_key_write
  797. );
  798. } // end if (MYISAM)
  799. if ($is_aria) {
  800. $html_output .= PMA_getHtmlForTableRow(
  801. 'new_transactional',
  802. 'TRANSACTIONAL',
  803. $transactional
  804. );
  805. $html_output .= PMA_getHtmlForTableRow(
  806. 'new_page_checksum',
  807. 'PAGE_CHECKSUM',
  808. $page_checksum
  809. );
  810. } // end if (ARIA)
  811. if (strlen($auto_increment) > 0
  812. && ($is_myisam_or_aria || $is_innodb || $is_pbxt)
  813. ) {
  814. $html_output .= '<tr><td>'
  815. . '<label for="auto_increment_opt">AUTO_INCREMENT</label></td>'
  816. . '<td><input type="number" name="new_auto_increment" '
  817. . 'id="auto_increment_opt"'
  818. . 'value="' . $auto_increment . '" /></td>'
  819. . '</tr> ';
  820. } // end if (MYISAM|INNODB)
  821. $possible_row_formats = PMA_getPossibleRowFormat();
  822. // for MYISAM there is also COMPRESSED but it can be set only by the
  823. // myisampack utility, so don't offer here the choice because if we
  824. // try it inside an ALTER TABLE, MySQL (at least in 5.1.23-maria)
  825. // does not return a warning
  826. // (if the table was compressed, it can be seen on the Structure page)
  827. if (isset($possible_row_formats[$tbl_storage_engine])) {
  828. $current_row_format = strtoupper($GLOBALS['showtable']['Row_format']);
  829. $html_output .= '<tr><td>'
  830. . '<label for="new_row_format">ROW_FORMAT</label></td>'
  831. . '<td>';
  832. $html_output .= PMA_Util::getDropdown(
  833. 'new_row_format', $possible_row_formats[$tbl_storage_engine],
  834. $current_row_format, 'new_row_format'
  835. );
  836. $html_output .= '</td></tr>';
  837. }
  838. $html_output .= '</table>'
  839. . '</fieldset>';
  840. return $html_output;
  841. }
  842. /**
  843. * Get the common HTML table row (tr) for new_checksum, new_delay_key_write,
  844. * new_transactional and new_page_checksum
  845. *
  846. * @param string $attribute class, name and id attribute
  847. * @param string $label label value
  848. * @param string $val checksum, delay_key_write, transactional, page_checksum
  849. *
  850. * @return string $html_output
  851. */
  852. function PMA_getHtmlForTableRow($attribute, $label, $val)
  853. {
  854. return '<tr>'
  855. . '<td><label for="' . $attribute . '">' . $label . '</label></td>'
  856. . '<td><input type="checkbox" name="' . $attribute . '"'
  857. . ' id="' . $attribute . '"'
  858. . ' value="1"'
  859. . ((!empty($val) && $val == 1) ? ' checked="checked"' : '') . '/></td>'
  860. . '</tr>';
  861. }
  862. /**
  863. * Get array of possible row formats
  864. *
  865. * @return array $possible_row_formats
  866. */
  867. function PMA_getPossibleRowFormat()
  868. {
  869. // the outer array is for engines, the inner array contains the dropdown
  870. // option values as keys then the dropdown option labels
  871. $possible_row_formats = array(
  872. 'ARIA' => array(
  873. 'FIXED' => 'FIXED',
  874. 'DYNAMIC' => 'DYNAMIC',
  875. 'PAGE' => 'PAGE'
  876. ),
  877. 'MARIA' => array(
  878. 'FIXED' => 'FIXED',
  879. 'DYNAMIC' => 'DYNAMIC',
  880. 'PAGE' => 'PAGE'
  881. ),
  882. 'MYISAM' => array(
  883. 'FIXED' => 'FIXED',
  884. 'DYNAMIC' => 'DYNAMIC'
  885. ),
  886. 'PBXT' => array(
  887. 'FIXED' => 'FIXED',
  888. 'DYNAMIC' => 'DYNAMIC'
  889. ),
  890. 'INNODB' => array(
  891. 'COMPACT' => 'COMPACT',
  892. 'REDUNDANT' => 'REDUNDANT'
  893. )
  894. );
  895. $innodbEnginePlugin = PMA_StorageEngine::getEngine('innodb');
  896. $innodbPluginVersion = $innodbEnginePlugin->getInnodbPluginVersion();
  897. if (!empty($innodbPluginVersion)) {
  898. $innodb_file_format = $innodbEnginePlugin->getInnodbFileFormat();
  899. } else {
  900. $innodb_file_format = '';
  901. }
  902. if ('Barracuda' == $innodb_file_format
  903. && $innodbEnginePlugin->supportsFilePerTable()
  904. ) {
  905. $possible_row_formats['INNODB']['DYNAMIC'] = 'DYNAMIC';
  906. $possible_row_formats['INNODB']['COMPRESSED'] = 'COMPRESSED';
  907. }
  908. return $possible_row_formats;
  909. }
  910. /**
  911. * Get HTML div for copy table
  912. *
  913. * @return string $html_output
  914. */
  915. function PMA_getHtmlForCopytable()
  916. {
  917. $html_output = '<div class="operations_half_width">';
  918. $html_output .= '<form method="post" action="tbl_operations.php" '
  919. . 'name="copyTable" '
  920. . 'id="copyTable" '
  921. . ' class="ajax" '
  922. . 'onsubmit="return emptyFormElements(this, \'new_name\')">'
  923. . PMA_URL_getHiddenInputs($GLOBALS['db'], $GLOBALS['table'])
  924. . '<input type="hidden" name="reload" value="1" />';
  925. $html_output .= '<fieldset>';
  926. $html_output .= '<legend>'
  927. . __('Copy table to (database<b>.</b>table):') . '</legend>';
  928. if (count($GLOBALS['pma']->databases) > $GLOBALS['cfg']['MaxDbList']) {
  929. $html_output .= '<input class="halfWidth" type="text" maxlength="100" '
  930. . 'size="30" name="target_db" '
  931. . 'value="' . htmlspecialchars($GLOBALS['db']) . '"/>';
  932. } else {
  933. $html_output .= '<select class="halfWidth" name="target_db">'
  934. . $GLOBALS['pma']->databases->getHtmlOptions(true, false)
  935. . '</select>';
  936. }
  937. $html_output .= '&nbsp;<strong>.</strong>&nbsp;';
  938. $html_output .= '<input class="halfWidth" type="text" required="required" '
  939. . 'size="20" name="new_name" onfocus="this.select()" '
  940. . 'value="' . htmlspecialchars($GLOBALS['table']) . '"/><br />';
  941. $choices = array(
  942. 'structure' => __('Structure only'),
  943. 'data' => __('Structure and data'),
  944. 'dataonly' => __('Data only'));
  945. $html_output .= PMA_Util::getRadioFields(
  946. 'what', $choices, 'data', true
  947. );
  948. $html_output .= '<input type="checkbox" name="drop_if_exists" '
  949. . 'value="true" id="checkbox_drop" />'
  950. . '<label for="checkbox_drop">'
  951. . sprintf(__('Add %s'), 'DROP TABLE') . '</label><br />'
  952. . '<input type="checkbox" name="sql_auto_increment" '
  953. . 'value="1" id="checkbox_auto_increment_cp" />'
  954. . '<label for="checkbox_auto_increment_cp">'
  955. . __('Add AUTO_INCREMENT value') . '</label><br />';
  956. // display "Add constraints" choice only if there are
  957. // foreign keys
  958. if (PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'foreign')) {
  959. $html_output .= '<input type="checkbox" name="add_constraints" '
  960. . 'value="1" id="checkbox_constraints" />';
  961. $html_output .= '<label for="checkbox_constraints">'
  962. . __('Add constraints') . '</label><br />';
  963. } // endif
  964. if (isset($_COOKIE['pma_switch_to_new'])
  965. && $_COOKIE['pma_switch_to_new'] == 'true'
  966. ) {
  967. $pma_switch_to_new = 'true';
  968. }
  969. $html_output .= '<input type="checkbox" name="switch_to_new" value="true"'
  970. . 'id="checkbox_switch"'
  971. . ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true')
  972. ? ' checked="checked"'
  973. : '' . '/>');
  974. $html_output .= '<label for="checkbox_switch">'
  975. . __('Switch to copied table') . '</label>'
  976. . '</fieldset>';
  977. $html_output .= '<fieldset class="tblFooters">'
  978. . '<input type="submit" name="submit_copy" value="' . __('Go') . '" />'
  979. . '</fieldset>'
  980. . '</form>'
  981. . '</div>';
  982. return $html_output;
  983. }
  984. /**
  985. * Get HTML snippet for table maintence
  986. *
  987. * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not
  988. * @param boolean $is_innodb whether innodb or not
  989. * @param boolean $is_berkeleydb whether berkeleydb or not
  990. * @param array $url_params array of URL parameters
  991. *
  992. * @return string $html_output
  993. */
  994. function PMA_getHtmlForTableMaintenance(
  995. $is_myisam_or_aria, $is_innodb, $is_berkeleydb, $url_params
  996. ) {
  997. $html_output = '<div class="operations_half_width">';
  998. $html_output .= '<fieldset>'
  999. . '<legend>' . __('Table maintenance') . '</legend>';
  1000. $html_output .= '<ul id="tbl_maintenance">';
  1001. // Note: BERKELEY (BDB) is no longer supported, starting with MySQL 5.1
  1002. $html_output .= PMA_getListofMaintainActionLink(
  1003. $is_myisam_or_aria, $is_innodb, $url_params, $is_berkeleydb
  1004. );
  1005. $html_output .= '</ul>'
  1006. . '</fieldset>'
  1007. . '</div>';
  1008. return $html_output;
  1009. }
  1010. /**
  1011. * Get HTML 'li' having a link of maintain action
  1012. *
  1013. * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not
  1014. * @param boolean $is_innodb whether innodb or not
  1015. * @param array $url_params array of URL parameters
  1016. * @param boolean $is_berkeleydb whether berkeleydb or not
  1017. *
  1018. * @return string $html_output
  1019. */
  1020. function PMA_getListofMaintainActionLink($is_myisam_or_aria,
  1021. $is_innodb, $url_params, $is_berkeleydb
  1022. ) {
  1023. $html_output = '';
  1024. if ($is_myisam_or_aria || $is_innodb || $is_berkeleydb) {
  1025. if ($is_myisam_or_aria || $is_innodb) {
  1026. $params = array(
  1027. 'sql_query' => 'CHECK TABLE '
  1028. . PMA_Util::backquote($GLOBALS['table']),
  1029. 'table_maintenance' => 'Go',
  1030. );
  1031. $html_output .= PMA_getMaintainActionlink(
  1032. __('Check table'),
  1033. $params,
  1034. $url_params,
  1035. 'CHECK_TABLE'
  1036. );
  1037. }
  1038. if ($is_innodb) {
  1039. $params = array(
  1040. 'sql_query' => 'ALTER TABLE '
  1041. . PMA_Util::backquote($GLOBALS['table'])
  1042. . ' ENGINE = InnoDB;'
  1043. );
  1044. $html_output .= PMA_getMaintainActionlink(
  1045. __('Defragment table'),
  1046. $params,
  1047. $url_params,
  1048. 'InnoDB_File_Defragmenting',
  1049. 'Table_types'
  1050. );
  1051. }
  1052. if ($is_innodb || $is_myisam_or_aria || $is_berkeleydb) {
  1053. $params = array(
  1054. 'sql_query' => 'ANALYZE TABLE '
  1055. . PMA_Util::backquote($GLOBALS['table']),
  1056. 'table_maintenance' => 'Go',
  1057. );
  1058. $html_output .= PMA_getMaintainActionlink(
  1059. __('Analyze table'),
  1060. $params,
  1061. $url_params,
  1062. 'ANALYZE_TABLE'
  1063. );
  1064. }
  1065. if ($is_myisam_or_aria && !PMA_DRIZZLE) {
  1066. $params = array(
  1067. 'sql_query' => 'REPAIR TABLE '
  1068. . PMA_Util::backquote($GLOBALS['table']),
  1069. 'table_maintenance' => 'Go',
  1070. );
  1071. $html_output .= PMA_getMaintainActionlink(
  1072. __('Repair table'),
  1073. $params,
  1074. $url_params,
  1075. 'REPAIR_TABLE'
  1076. );
  1077. }
  1078. if (($is_myisam_or_aria || $is_innodb || $is_berkeleydb)
  1079. && !PMA_DRIZZLE
  1080. ) {
  1081. $params = array(
  1082. 'sql_query' => 'OPTIMIZE TABLE '
  1083. . PMA_Util::backquote($GLOBALS['table']),
  1084. 'table_maintenance' => 'Go',
  1085. );
  1086. $html_output .= PMA_getMaintainActionlink(
  1087. __('Optimize table'),
  1088. $params,
  1089. $url_params,
  1090. 'OPTIMIZE_TABLE'
  1091. );
  1092. }
  1093. } // end MYISAM or BERKELEYDB case
  1094. $params = array(
  1095. 'sql_query' => 'FLUSH TABLE '
  1096. . PMA_Util::backquote($GLOBALS['table']),
  1097. 'message_to_show' => sprintf(
  1098. __('Table %s has been flushed.'),
  1099. htmlspecialchars($GLOBALS['table'])
  1100. ),
  1101. 'reload' => 1,
  1102. );
  1103. $html_output .= PMA_getMaintainActionlink(
  1104. __('Flush the table (FLUSH)'),
  1105. $params,
  1106. $url_params,
  1107. 'FLUSH'
  1108. );
  1109. return $html_output;
  1110. }
  1111. /**
  1112. * Get maintain action HTML link
  1113. *
  1114. * @param string $action action name
  1115. * @param array $params url parameters array
  1116. * @param array $url_params additional url parameters
  1117. * @param string $link contains name of page/anchor that is being linked
  1118. *
  1119. * @return string $html_output
  1120. */
  1121. function PMA_getMaintainActionlink($action, $params, $url_params, $link)
  1122. {
  1123. return '<li>'
  1124. . '<a class="maintain_action ajax" '
  1125. . 'href="sql.php'
  1126. . PMA_URL_getCommon(array_merge($url_params, $params)) . '">'
  1127. . $action
  1128. . '</a>'
  1129. . PMA_Util::showMySQLDocu($link)
  1130. . '</li>';
  1131. }
  1132. /**
  1133. * Get HTML for Delete data or table (truncate table, drop table)
  1134. *
  1135. * @param array $truncate_table_url_params url parameter array for truncate table
  1136. * @param array $dropTableUrlParams url parameter array for drop table
  1137. *
  1138. * @return string $html_output
  1139. */
  1140. function PMA_getHtmlForDeleteDataOrTable(
  1141. $truncate_table_url_params,
  1142. $dropTableUrlParams
  1143. ) {
  1144. $html_output = '<div class="operations_half_width">'
  1145. . '<fieldset class="caution">'
  1146. . '<legend>' . __('Delete data or table') . '</legend>';
  1147. $html_output .= '<ul>';
  1148. if (! empty($truncate_table_url_params)) {
  1149. $html_output .= PMA_getDeleteDataOrTablelink(
  1150. $truncate_table_url_params,
  1151. 'TRUNCATE_TABLE',
  1152. __('Empty the table (TRUNCATE)'),
  1153. 'truncate_tbl_anchor'
  1154. );
  1155. }
  1156. if (!empty ($dropTableUrlParams)) {
  1157. $html_output .= PMA_getDeleteDataOrTablelink(
  1158. $dropTableUrlParams,
  1159. 'DROP_TABLE',
  1160. __('Delete the table (DROP)'),
  1161. 'drop_tbl_anchor'
  1162. );
  1163. }
  1164. $html_output .= '</ul></fieldset></div>';
  1165. return $html_output;
  1166. }
  1167. /**
  1168. * Get the HTML link for Truncate table, Drop table and Drop db
  1169. *
  1170. * @param array $url_params url parameter array for delete data or table
  1171. * @param string $syntax TRUNCATE_TABLE or DROP_TABLE or DROP_DATABASE
  1172. * @param string $link link to be shown
  1173. * @param string $htmlId id of the link
  1174. *
  1175. * @return String html output
  1176. */
  1177. function PMA_getDeleteDataOrTablelink($url_params, $syntax, $link, $htmlId)
  1178. {
  1179. return '<li><a '
  1180. . 'href="sql.php' . PMA_URL_getCommon($url_params) . '"'
  1181. . ' id="' . $htmlId . '" class="ajax">'
  1182. . $link . '</a>'
  1183. . PMA_Util::showMySQLDocu($syntax)
  1184. . '</li>';
  1185. }
  1186. /**
  1187. * Get HTML snippet for partition maintenance
  1188. *
  1189. * @param array $partition_names array of partition names for a specific db/table
  1190. * @param array $url_params url parameters
  1191. *
  1192. * @return string $html_output
  1193. */
  1194. function PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
  1195. {
  1196. $choices = array(
  1197. 'ANALYZE' => __('Analyze'),
  1198. 'CHECK' => __('Check'),
  1199. 'OPTIMIZE' => __('Optimize'),
  1200. 'REBUILD' => __('Rebuild'),
  1201. 'REPAIR' => __('Repair')
  1202. );
  1203. $html_output = '<div class="operations_half_width">'
  1204. . '<form method="post" action="tbl_operations.php">'
  1205. . PMA_URL_getHiddenInputs($GLOBALS['db'], $GLOBALS['table'])
  1206. . '<fieldset>'
  1207. . '<legend>' . __('Partition maintenance') . '</legend>';
  1208. $html_select = '<select name="partition_name">' . "\n";
  1209. foreach ($partition_names as $one_partition) {
  1210. $one_partition = htmlspecialchars($one_partition);
  1211. $html_select .= '<option value="' . $one_partition . '">'
  1212. . $one_partition . '</option>' . "\n";
  1213. }
  1214. $html_select .= '</select>' . "\n";
  1215. $html_output .= sprintf(__('Partition %s'), $html_select);
  1216. $html_output .= PMA_Util::getRadioFields(
  1217. 'partition_operation', $choices, '', false
  1218. );
  1219. $html_output .= PMA_Util::showMySQLDocu('partitioning_maintenance');
  1220. $this_url_params = array_merge(
  1221. $url_params,
  1222. array(
  1223. 'sql_query' => 'ALTER TABLE '
  1224. . PMA_Util::backquote($GLOBALS['table'])
  1225. . ' REMOVE PARTITIONING;'
  1226. )
  1227. );
  1228. $html_output .= '<br /><a href="sql.php'
  1229. . PMA_URL_getCommon($this_url_params) . '">'
  1230. . __('Remove partitioning') . '</a>';
  1231. $html_output .= '</fieldset>'
  1232. . '<fieldset class="tblFooters">'
  1233. . '<input type="hidden" name="submit_partition" value="1">'
  1234. . '<input type="submit" value="' . __('Go') . '" />'
  1235. . '</fieldset>'
  1236. . '</form>'
  1237. . '</div>';
  1238. return $html_output;
  1239. }
  1240. /**
  1241. * Get the HTML for Referential Integrity check
  1242. *
  1243. * @param array $foreign all Relations to foreign tables for a given table
  1244. * or optionally a given column in a table
  1245. * @param array $url_params array of url parameters
  1246. *
  1247. * @return string $html_output
  1248. */
  1249. function PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
  1250. {
  1251. $html_output = '<div class="operations_half_width">'
  1252. . '<fieldset>'
  1253. . '<legend>' . __('Check referential integrity:') . '</legend>';
  1254. $html_output .= '<ul>';
  1255. foreach ($foreign as $master => $arr) {
  1256. $join_query = 'SELECT '
  1257. . PMA_Util::backquote($GLOBALS['table']) . '.*'
  1258. . ' FROM ' . PMA_Util::backquote($GLOBALS['table'])
  1259. . ' LEFT JOIN '
  1260. . PMA_Util::backquote($arr['foreign_db'])
  1261. . '.'
  1262. . PMA_Util::backquote($arr['foreign_table']);
  1263. if ($arr['foreign_table'] == $GLOBALS['table']) {
  1264. $foreign_table = $GLOBALS['table'] . '1';
  1265. $join_query .= ' AS ' . PMA_Util::backquote($foreign_table);
  1266. } else {
  1267. $foreign_table = $arr['foreign_table'];
  1268. }
  1269. $join_query .= ' ON '
  1270. . PMA_Util::backquote($GLOBALS['table']) . '.'
  1271. . PMA_Util::backquote($master)
  1272. . ' = '
  1273. . PMA_Util::backquote($arr['foreign_db'])
  1274. . '.'
  1275. . PMA_Util::backquote($foreign_table) . '.'
  1276. . PMA_Util::backquote($arr['foreign_field'])
  1277. . ' WHERE '
  1278. . PMA_Util::backquote($arr['foreign_db'])
  1279. . '.'
  1280. . PMA_Util::backquote($foreign_table) . '.'
  1281. . PMA_Util::backquote($arr['foreign_field'])
  1282. . ' IS NULL AND '
  1283. . PMA_Util::backquote($GLOBALS['table']) . '.'
  1284. . PMA_Util::backquote($master)
  1285. . ' IS NOT NULL';
  1286. $this_url_params = array_merge(
  1287. $url_params,
  1288. array('sql_query' => $join_query)
  1289. );
  1290. $html_output .= '<li>'
  1291. . '<a href="sql.php'
  1292. . PMA_URL_getCommon($this_url_params)
  1293. . '">'
  1294. . $master . '&nbsp;->&nbsp;' . $arr['foreign_db'] . '.'
  1295. . $arr['foreign_table'] . '.' . $arr['foreign_field']
  1296. . '</a></li>' . "\n";
  1297. } // foreach $foreign
  1298. $html_output .= '</ul></fieldset></div>';
  1299. return $html_output;
  1300. }
  1301. /**
  1302. * Reorder table based on request params
  1303. *
  1304. * @return array SQL query and result
  1305. */
  1306. function PMA_getQueryAndResultForReorderingTable()
  1307. {
  1308. $sql_query = 'ALTER TABLE '
  1309. . PMA_Util::backquote($GLOBALS['table'])
  1310. . ' ORDER BY '
  1311. . PMA_Util::backquote(urldecode($_REQUEST['order_field']));
  1312. if (isset($_REQUEST['order_order'])
  1313. && $_REQUEST['order_order'] === 'desc'
  1314. ) {
  1315. $sql_query .= ' DESC';
  1316. }
  1317. $sql_query .= ';';
  1318. $result = $GLOBALS['dbi']->query($sql_query);
  1319. return array($sql_query, $result);
  1320. }
  1321. /**
  1322. * Get table alters array
  1323. *
  1324. * @param boolean $is_myisam_or_aria whether MYISAM | ARIA or not
  1325. * @param boolean $is_isam whether ISAM or not
  1326. * @param string $pack_keys pack keys
  1327. * @param string $checksum value of checksum
  1328. * @param boolean $is_aria whether ARIA or not
  1329. * @param string $page_checksum value of page checksum
  1330. * @param string $delay_key_write delay key write
  1331. * @param boolean $is_innodb whether INNODB or not
  1332. * @param boolean $is_pbxt whether PBXT or not
  1333. * @param string $row_format row format
  1334. * @param string $newTblStorageEngine table storage engine
  1335. * @param string $transactional value of transactional
  1336. * @param string $tbl_collation collation of the table
  1337. *
  1338. * @return array $table_alters
  1339. */
  1340. function PMA_getTableAltersArray($is_myisam_or_aria, $is_isam, $pack_keys,
  1341. $checksum, $is_aria, $page_checksum, $delay_key_write, $is_innodb,
  1342. $is_pbxt, $row_format, $newTblStorageEngine, $transactional, $tbl_collation
  1343. ) {
  1344. global $auto_increment;
  1345. $table_alters = array();
  1346. if (isset($_REQUEST['comment'])
  1347. && urldecode($_REQUEST['prev_comment']) !== $_REQUEST['comment']
  1348. ) {
  1349. $table_alters[] = 'COMMENT = \''
  1350. . PMA_Util::sqlAddSlashes($_REQUEST['comment']) . '\'';
  1351. }
  1352. if (! empty($newTblStorageEngine)
  1353. && strtolower($newTblStorageEngine) !== strtolower($GLOBALS['tbl_storage_engine'])
  1354. ) {
  1355. $table_alters[] = 'ENGINE = ' . $newTblStorageEngine;
  1356. }
  1357. if (! empty($_REQUEST['tbl_collation'])
  1358. && $_REQUEST['tbl_collation'] !== $tbl_collation
  1359. ) {
  1360. $table_alters[] = 'DEFAULT '
  1361. . PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
  1362. }
  1363. if (($is_myisam_or_aria || $is_isam)
  1364. && isset($_REQUEST['new_pack_keys'])
  1365. && $_REQUEST['new_pack_keys'] != (string)$pack_keys
  1366. ) {
  1367. $table_alters[] = 'pack_keys = ' . $_REQUEST['new_pack_keys'];
  1368. }
  1369. $_REQUEST['new_checksum'] = empty($_REQUEST['new_checksum']) ? '0' : '1';
  1370. if ($is_myisam_or_aria
  1371. && $_REQUEST['new_checksum'] !== $checksum
  1372. ) {
  1373. $table_alters[] = 'checksum = ' . $_REQUEST['new_checksum'];
  1374. }
  1375. $_REQUEST['new_transactional']
  1376. = empty($_REQUEST['new_transactional']) ? '0' : '1';
  1377. if ($is_aria
  1378. && $_REQUEST['new_transactional'] !== $transactional
  1379. ) {
  1380. $table_alters[] = 'TRANSACTIONAL = ' . $_REQUEST['new_transactional'];
  1381. }
  1382. $_REQUEST['new_page_checksum']
  1383. = empty($_REQUEST['new_page_checksum']) ? '0' : '1';
  1384. if ($is_aria
  1385. && $_REQUEST['new_page_checksum'] !== $page_checksum
  1386. ) {
  1387. $table_alters[] = 'PAGE_CHECKSUM = ' . $_REQUEST['new_page_checksum'];
  1388. }
  1389. $_REQUEST['new_delay_key_write']
  1390. = empty($_REQUEST['new_delay_key_write']) ? '0' : '1';
  1391. if ($is_myisam_or_aria
  1392. && $_REQUEST['new_delay_key_write'] !== $delay_key_write
  1393. ) {
  1394. $table_alters[] = 'delay_key_write = ' . $_REQUEST['new_delay_key_write'];
  1395. }
  1396. if (($is_myisam_or_aria || $is_innodb || $is_pbxt)
  1397. && ! empty($_REQUEST['new_auto_increment'])
  1398. && (! isset($auto_increment)
  1399. || $_REQUEST['new_auto_increment'] !== $auto_increment)
  1400. ) {
  1401. $table_alters[] = 'auto_increment = '
  1402. . PMA_Util::sqlAddSlashes($_REQUEST['new_auto_increment']);
  1403. }
  1404. if (($is_myisam_or_aria || $is_innodb || $is_pbxt)
  1405. && ! empty($_REQUEST['new_row_format'])
  1406. && (!strlen($row_format)
  1407. || strtolower($_REQUEST['new_row_format']) !== strtolower($row_format))
  1408. ) {
  1409. $table_alters[] = 'ROW_FORMAT = '
  1410. . PMA_Util::sqlAddSlashes($_REQUEST['new_row_format']);
  1411. }
  1412. return $table_alters;
  1413. }
  1414. /**
  1415. * set initial value of the set of variables, based on the current table engine
  1416. *
  1417. * @param string $tbl_storage_engine table storage engine
  1418. *
  1419. * @return array ($is_myisam_or_aria, $is_innodb, $is_isam,
  1420. * $is_berkeleydb, $is_aria, $is_pbxt)
  1421. */
  1422. function PMA_setGlobalVariablesForEngine($tbl_storage_engine)
  1423. {
  1424. $upperTblStorEngine = strtoupper($tbl_storage_engine);
  1425. //Options that apply to MYISAM usually apply to ARIA
  1426. $is_myisam_or_aria = ($upperTblStorEngine == 'MYISAM'
  1427. || $upperTblStorEngine == 'ARIA'
  1428. || $upperTblStorEngine == 'MARIA'
  1429. );
  1430. $is_aria = ($upperTblStorEngine == 'ARIA');
  1431. $is_isam = ($upperTblStorEngine == 'ISAM');
  1432. $is_innodb = ($upperTblStorEngine == 'INNODB');
  1433. $is_berkeleydb = ($upperTblStorEngine == 'BERKELEYDB');
  1434. $is_pbxt = ($upperTblStorEngine == 'PBXT');
  1435. return array(
  1436. $is_myisam_or_aria, $is_innodb, $is_isam,
  1437. $is_berkeleydb, $is_aria, $is_pbxt
  1438. );
  1439. }
  1440. /**
  1441. * Get warning messages array
  1442. *
  1443. * @return array $warning_messages
  1444. */
  1445. function PMA_getWarningMessagesArray()
  1446. {
  1447. $warning_messages = array();
  1448. foreach ($GLOBALS['dbi']->getWarnings() as $warning) {
  1449. // In MariaDB 5.1.44, when altering a table from Maria to MyISAM
  1450. // and if TRANSACTIONAL was set, the system reports an error;
  1451. // I discussed with a Maria developer and he agrees that this
  1452. // should not be reported with a Level of Error, so here
  1453. // I just ignore it. But there are other 1478 messages
  1454. // that it's better to show.
  1455. if (! ($_REQUEST['new_tbl_storage_engine'] == 'MyISAM'
  1456. && $warning['Code'] == '1478'
  1457. && $warning['Level'] == 'Error')
  1458. ) {
  1459. $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
  1460. . ' ' . $warning['Message'];
  1461. }
  1462. }
  1463. return $warning_messages;
  1464. }
  1465. /**
  1466. * Get SQL query and result after ran this SQL query for a partition operation
  1467. * has been requested by the user
  1468. *
  1469. * @return array $sql_query, $result
  1470. */
  1471. function PMA_getQueryAndResultForPartition()
  1472. {
  1473. $sql_query = 'ALTER TABLE '
  1474. . PMA_Util::backquote($GLOBALS['table']) . ' '
  1475. . $_REQUEST['partition_operation']
  1476. . ' PARTITION '
  1477. . $_REQUEST['partition_name'] . ';';
  1478. $result = $GLOBALS['dbi']->query($sql_query);
  1479. return array($sql_query, $result);
  1480. }
  1481. ?>