Operations.php 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Operations class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use PhpMyAdmin\Charsets;
  10. use PhpMyAdmin\Core;
  11. use PhpMyAdmin\Engines\Innodb;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Partition;
  14. use PhpMyAdmin\Plugins\Export\ExportSql;
  15. use PhpMyAdmin\Relation;
  16. use PhpMyAdmin\Response;
  17. use PhpMyAdmin\StorageEngine;
  18. use PhpMyAdmin\Table;
  19. use PhpMyAdmin\Url;
  20. use PhpMyAdmin\Util;
  21. /**
  22. * Set of functions with the operations section in phpMyAdmin
  23. *
  24. * @package PhpMyAdmin
  25. */
  26. class Operations
  27. {
  28. /**
  29. * @var Relation $relation
  30. */
  31. private $relation;
  32. /**
  33. * Constructor
  34. */
  35. public function __construct()
  36. {
  37. $this->relation = new Relation();
  38. }
  39. /**
  40. * Get HTML output for database comment
  41. *
  42. * @param string $db database name
  43. *
  44. * @return string $html_output
  45. */
  46. public function getHtmlForDatabaseComment($db)
  47. {
  48. $html_output = '<div>'
  49. . '<form method="post" action="db_operations.php" id="formDatabaseComment">'
  50. . Url::getHiddenInputs($db)
  51. . '<fieldset>'
  52. . '<legend>';
  53. if (Util::showIcons('ActionLinksMode')) {
  54. $html_output .= Util::getImage('b_comment') . '&nbsp;';
  55. }
  56. $html_output .= __('Database comment');
  57. $html_output .= '</legend>';
  58. $html_output .= '<input type="text" name="comment" '
  59. . 'class="textfield"'
  60. . 'value="' . htmlspecialchars($this->relation->getDbComment($db)) . '" />'
  61. . '</fieldset>';
  62. $html_output .= '<fieldset class="tblFooters">'
  63. . '<input type="submit" value="' . __('Go') . '" />'
  64. . '</fieldset>'
  65. . '</form>'
  66. . '</div>';
  67. return $html_output;
  68. }
  69. /**
  70. * Get HTML output for rename database
  71. *
  72. * @param string $db database name
  73. *
  74. * @return string $html_output
  75. */
  76. public function getHtmlForRenameDatabase($db)
  77. {
  78. $html_output = '<div>'
  79. . '<form id="rename_db_form" '
  80. . 'class="ajax" '
  81. . 'method="post" action="db_operations.php" '
  82. . 'onsubmit="return emptyCheckTheField(this, \'newname\')">';
  83. if (isset($_REQUEST['db_collation'])) {
  84. $html_output .= '<input type="hidden" name="db_collation" '
  85. . 'value="' . $_REQUEST['db_collation']
  86. . '" />' . "\n";
  87. }
  88. $html_output .= '<input type="hidden" name="what" value="data" />'
  89. . '<input type="hidden" name="db_rename" value="true" />'
  90. . Url::getHiddenInputs($db)
  91. . '<fieldset>'
  92. . '<legend>';
  93. if (Util::showIcons('ActionLinksMode')) {
  94. $html_output .= Util::getImage('b_edit') . '&nbsp;';
  95. }
  96. $html_output .= __('Rename database to')
  97. . '</legend>';
  98. $html_output .= '<input id="new_db_name" type="text" name="newname" '
  99. . 'maxlength="64" class="textfield" required="required"/>';
  100. $html_output .= '<br />';
  101. if ($GLOBALS['db_priv'] && $GLOBALS['table_priv']
  102. && $GLOBALS['col_priv'] && $GLOBALS['proc_priv']
  103. && $GLOBALS['is_reload_priv']
  104. ) {
  105. $html_output .= '<input type="checkbox" name="adjust_privileges" '
  106. . 'value="1" id="checkbox_adjust_privileges" checked="checked" />';
  107. } else {
  108. $html_output .= '<input type="checkbox" name="adjust_privileges" '
  109. . 'value="1" id="checkbox_adjust_privileges" title="' . __(
  110. 'You don\'t have sufficient privileges to perform this '
  111. . 'operation; Please refer to the documentation for more details'
  112. )
  113. . '" disabled/>';
  114. }
  115. $html_output .= '<label for="checkbox_adjust_privileges">'
  116. . __('Adjust privileges') . Util::showDocu('faq', 'faq6-39')
  117. . '</label><br />';
  118. $html_output .= ''
  119. . '</fieldset>'
  120. . '<fieldset class="tblFooters">'
  121. . '<input id="rename_db_input" type="submit" value="' . __('Go') . '" />'
  122. . '</fieldset>'
  123. . '</form>'
  124. . '</div>';
  125. return $html_output;
  126. }
  127. /**
  128. * Get HTML for database drop link
  129. *
  130. * @param string $db database name
  131. *
  132. * @return string $html_output
  133. */
  134. public function getHtmlForDropDatabaseLink($db)
  135. {
  136. $this_sql_query = 'DROP DATABASE ' . Util::backquote($db);
  137. $this_url_params = array(
  138. 'sql_query' => $this_sql_query,
  139. 'back' => 'db_operations.php',
  140. 'goto' => 'index.php',
  141. 'reload' => '1',
  142. 'purge' => '1',
  143. 'message_to_show' => sprintf(
  144. __('Database %s has been dropped.'),
  145. htmlspecialchars(Util::backquote($db))
  146. ),
  147. 'db' => null,
  148. );
  149. $html_output = '<div>'
  150. . '<fieldset class="caution">';
  151. $html_output .= '<legend>';
  152. if (Util::showIcons('ActionLinksMode')) {
  153. $html_output .= Util::getImage('b_deltbl') . '&nbsp';
  154. }
  155. $html_output .= __('Remove database')
  156. . '</legend>';
  157. $html_output .= '<ul>';
  158. $html_output .= $this->getDeleteDataOrTablelink(
  159. $this_url_params,
  160. 'DROP_DATABASE',
  161. __('Drop the database (DROP)'),
  162. 'drop_db_anchor'
  163. );
  164. $html_output .= '</ul></fieldset>'
  165. . '</div>';
  166. return $html_output;
  167. }
  168. /**
  169. * Get HTML snippet for copy database
  170. *
  171. * @param string $db database name
  172. *
  173. * @return string $html_output
  174. */
  175. public function getHtmlForCopyDatabase($db)
  176. {
  177. $drop_clause = 'DROP TABLE / DROP VIEW';
  178. $choices = array(
  179. 'structure' => __('Structure only'),
  180. 'data' => __('Structure and data'),
  181. 'dataonly' => __('Data only')
  182. );
  183. $pma_switch_to_new = isset($_SESSION['pma_switch_to_new']) && $_SESSION['pma_switch_to_new'];
  184. $html_output = '<div>';
  185. $html_output .= '<form id="copy_db_form" '
  186. . 'class="ajax" '
  187. . 'method="post" action="db_operations.php" '
  188. . 'onsubmit="return emptyCheckTheField(this, \'newname\')">';
  189. if (isset($_REQUEST['db_collation'])) {
  190. $html_output .= '<input type="hidden" name="db_collation" '
  191. . 'value="' . $_REQUEST['db_collation'] . '" />' . "\n";
  192. }
  193. $html_output .= '<input type="hidden" name="db_copy" value="true" />' . "\n"
  194. . Url::getHiddenInputs($db);
  195. $html_output .= '<fieldset>'
  196. . '<legend>';
  197. if (Util::showIcons('ActionLinksMode')) {
  198. $html_output .= Util::getImage('b_edit') . '&nbsp';
  199. }
  200. $html_output .= __('Copy database to')
  201. . '</legend>'
  202. . '<input type="text" maxlength="64" name="newname" '
  203. . 'class="textfield" required="required" /><br />'
  204. . Util::getRadioFields(
  205. 'what', $choices, 'data', true
  206. );
  207. $html_output .= '<br />';
  208. $html_output .= '<input type="checkbox" name="create_database_before_copying" '
  209. . 'value="1" id="checkbox_create_database_before_copying"'
  210. . 'checked="checked" />';
  211. $html_output .= '<label for="checkbox_create_database_before_copying">'
  212. . __('CREATE DATABASE before copying') . '</label><br />';
  213. $html_output .= '<input type="checkbox" name="drop_if_exists" value="true"'
  214. . 'id="checkbox_drop" />';
  215. $html_output .= '<label for="checkbox_drop">'
  216. . sprintf(__('Add %s'), $drop_clause)
  217. . '</label><br />';
  218. $html_output .= '<input type="checkbox" name="sql_auto_increment" value="1" '
  219. . 'checked="checked" id="checkbox_auto_increment" />';
  220. $html_output .= '<label for="checkbox_auto_increment">'
  221. . __('Add AUTO_INCREMENT value') . '</label><br />';
  222. $html_output .= '<input type="checkbox" name="add_constraints" value="1"'
  223. . 'id="checkbox_constraints" checked="checked"/>';
  224. $html_output .= '<label for="checkbox_constraints">'
  225. . __('Add constraints') . '</label><br />';
  226. $html_output .= '<br />';
  227. if ($GLOBALS['db_priv'] && $GLOBALS['table_priv']
  228. && $GLOBALS['col_priv'] && $GLOBALS['proc_priv']
  229. && $GLOBALS['is_reload_priv']
  230. ) {
  231. $html_output .= '<input type="checkbox" name="adjust_privileges" '
  232. . 'value="1" id="checkbox_privileges" checked="checked" />';
  233. } else {
  234. $html_output .= '<input type="checkbox" name="adjust_privileges" '
  235. . 'value="1" id="checkbox_privileges" title="' . __(
  236. 'You don\'t have sufficient privileges to perform this '
  237. . 'operation; Please refer to the documentation for more details'
  238. )
  239. . '" disabled/>';
  240. }
  241. $html_output .= '<label for="checkbox_privileges">'
  242. . __('Adjust privileges') . Util::showDocu('faq', 'faq6-39')
  243. . '</label><br />';
  244. $html_output .= '<input type="checkbox" name="switch_to_new" value="true"'
  245. . 'id="checkbox_switch"'
  246. . ($pma_switch_to_new ? ' checked="checked"' : '') . '/>';
  247. $html_output .= '<label for="checkbox_switch">'
  248. . __('Switch to copied database') . '</label>'
  249. . '</fieldset>';
  250. $html_output .= '<fieldset class="tblFooters">'
  251. . '<input type="submit" name="submit_copy" value="' . __('Go') . '" />'
  252. . '</fieldset>'
  253. . '</form>'
  254. . '</div>';
  255. return $html_output;
  256. }
  257. /**
  258. * Get HTML snippet for change database charset
  259. *
  260. * @param string $db database name
  261. * @param string $table table name
  262. *
  263. * @return string $html_output
  264. */
  265. public function getHtmlForChangeDatabaseCharset($db, $table)
  266. {
  267. $html_output = '<div>'
  268. . '<form id="change_db_charset_form" ';
  269. $html_output .= 'class="ajax" ';
  270. $html_output .= 'method="post" action="db_operations.php">';
  271. $html_output .= Url::getHiddenInputs($db, $table);
  272. $html_output .= '<fieldset>' . "\n"
  273. . ' <legend>';
  274. if (Util::showIcons('ActionLinksMode')) {
  275. $html_output .= Util::getImage('s_asci') . '&nbsp';
  276. }
  277. $html_output .= '<label for="select_db_collation">' . __('Collation')
  278. . '</label>' . "\n"
  279. . '</legend>' . "\n"
  280. . Charsets::getCollationDropdownBox(
  281. $GLOBALS['dbi'],
  282. $GLOBALS['cfg']['Server']['DisableIS'],
  283. 'db_collation',
  284. 'select_db_collation',
  285. isset($_REQUEST['db_collation']) ? $_REQUEST['db_collation'] : '',
  286. false
  287. )
  288. . '<br />'
  289. . '<input type="checkbox" name="change_all_tables_collations"'
  290. . 'id="checkbox_change_all_tables_collations" />'
  291. . '<label for="checkbox_change_all_tables_collations">'
  292. . __('Change all tables collations')
  293. . '</label>'
  294. . '<br />'
  295. . '<span id="span_change_all_tables_columns_collations"><input type="checkbox" name="change_all_tables_columns_collations"'
  296. . 'id="checkbox_change_all_tables_columns_collations" />'
  297. . '<label for="checkbox_change_all_tables_columns_collations">'
  298. . __('Change all tables columns collations')
  299. . '</label></span>'
  300. . '</fieldset>'
  301. . '<fieldset class="tblFooters">'
  302. . '<input type="submit" name="submitcollation"'
  303. . ' value="' . __('Go') . '" />' . "\n"
  304. . '</fieldset>' . "\n"
  305. . '</form></div>' . "\n";
  306. return $html_output;
  307. }
  308. /**
  309. * Run the Procedure definitions and function definitions
  310. *
  311. * to avoid selecting alternatively the current and new db
  312. * we would need to modify the CREATE definitions to qualify
  313. * the db name
  314. *
  315. * @param string $db database name
  316. *
  317. * @return void
  318. */
  319. public function runProcedureAndFunctionDefinitions($db)
  320. {
  321. $procedure_names = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'PROCEDURE');
  322. if ($procedure_names) {
  323. foreach ($procedure_names as $procedure_name) {
  324. $GLOBALS['dbi']->selectDb($db);
  325. $tmp_query = $GLOBALS['dbi']->getDefinition(
  326. $db, 'PROCEDURE', $procedure_name
  327. );
  328. // collect for later display
  329. $GLOBALS['sql_query'] .= "\n" . $tmp_query;
  330. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  331. $GLOBALS['dbi']->query($tmp_query);
  332. }
  333. }
  334. $function_names = $GLOBALS['dbi']->getProceduresOrFunctions($db, 'FUNCTION');
  335. if ($function_names) {
  336. foreach ($function_names as $function_name) {
  337. $GLOBALS['dbi']->selectDb($db);
  338. $tmp_query = $GLOBALS['dbi']->getDefinition(
  339. $db, 'FUNCTION', $function_name
  340. );
  341. // collect for later display
  342. $GLOBALS['sql_query'] .= "\n" . $tmp_query;
  343. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  344. $GLOBALS['dbi']->query($tmp_query);
  345. }
  346. }
  347. }
  348. /**
  349. * Create database before copy
  350. *
  351. * @return void
  352. */
  353. public function createDbBeforeCopy()
  354. {
  355. $local_query = 'CREATE DATABASE IF NOT EXISTS '
  356. . Util::backquote($_REQUEST['newname']);
  357. if (isset($_REQUEST['db_collation'])) {
  358. $local_query .= ' DEFAULT'
  359. . Util::getCharsetQueryPart($_REQUEST['db_collation']);
  360. }
  361. $local_query .= ';';
  362. $GLOBALS['sql_query'] .= $local_query;
  363. // save the original db name because Tracker.php which
  364. // may be called under $GLOBALS['dbi']->query() changes $GLOBALS['db']
  365. // for some statements, one of which being CREATE DATABASE
  366. $original_db = $GLOBALS['db'];
  367. $GLOBALS['dbi']->query($local_query);
  368. $GLOBALS['db'] = $original_db;
  369. // Set the SQL mode to NO_AUTO_VALUE_ON_ZERO to prevent MySQL from creating
  370. // export statements it cannot import
  371. $sql_set_mode = "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'";
  372. $GLOBALS['dbi']->query($sql_set_mode);
  373. // rebuild the database list because Table::moveCopy
  374. // checks in this list if the target db exists
  375. $GLOBALS['dblist']->databases->build();
  376. }
  377. /**
  378. * Get views as an array and create SQL view stand-in
  379. *
  380. * @param array $tables_full array of all tables in given db or dbs
  381. * @param ExportSql $export_sql_plugin export plugin instance
  382. * @param string $db database name
  383. *
  384. * @return array $views
  385. */
  386. public function getViewsAndCreateSqlViewStandIn(
  387. array $tables_full, $export_sql_plugin, $db
  388. ) {
  389. $views = array();
  390. foreach ($tables_full as $each_table => $tmp) {
  391. // to be able to rename a db containing views,
  392. // first all the views are collected and a stand-in is created
  393. // the real views are created after the tables
  394. if ($GLOBALS['dbi']->getTable($db, $each_table)->isView()) {
  395. // If view exists, and 'add drop view' is selected: Drop it!
  396. if ($_REQUEST['what'] != 'nocopy'
  397. && isset($_REQUEST['drop_if_exists'])
  398. && $_REQUEST['drop_if_exists'] == 'true'
  399. ) {
  400. $drop_query = 'DROP VIEW IF EXISTS '
  401. . Util::backquote($_REQUEST['newname']) . '.'
  402. . Util::backquote($each_table);
  403. $GLOBALS['dbi']->query($drop_query);
  404. $GLOBALS['sql_query'] .= "\n" . $drop_query . ';';
  405. }
  406. $views[] = $each_table;
  407. // Create stand-in definition to resolve view dependencies
  408. $sql_view_standin = $export_sql_plugin->getTableDefStandIn(
  409. $db, $each_table, "\n"
  410. );
  411. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  412. $GLOBALS['dbi']->query($sql_view_standin);
  413. $GLOBALS['sql_query'] .= "\n" . $sql_view_standin;
  414. }
  415. }
  416. return $views;
  417. }
  418. /**
  419. * Get sql query for copy/rename table and boolean for whether copy/rename or not
  420. *
  421. * @param array $tables_full array of all tables in given db or dbs
  422. * @param boolean $move whether database name is empty or not
  423. * @param string $db database name
  424. *
  425. * @return array SQL queries for the constraints
  426. */
  427. public function copyTables(array $tables_full, $move, $db)
  428. {
  429. $sqlContraints = array();
  430. foreach ($tables_full as $each_table => $tmp) {
  431. // skip the views; we have created stand-in definitions
  432. if ($GLOBALS['dbi']->getTable($db, $each_table)->isView()) {
  433. continue;
  434. }
  435. // value of $what for this table only
  436. $this_what = $_REQUEST['what'];
  437. // do not copy the data from a Merge table
  438. // note: on the calling FORM, 'data' means 'structure and data'
  439. if ($GLOBALS['dbi']->getTable($db, $each_table)->isMerge()) {
  440. if ($this_what == 'data') {
  441. $this_what = 'structure';
  442. }
  443. if ($this_what == 'dataonly') {
  444. $this_what = 'nocopy';
  445. }
  446. }
  447. if ($this_what != 'nocopy') {
  448. // keep the triggers from the original db+table
  449. // (third param is empty because delimiters are only intended
  450. // for importing via the mysql client or our Import feature)
  451. $triggers = $GLOBALS['dbi']->getTriggers($db, $each_table, '');
  452. if (! Table::moveCopy(
  453. $db, $each_table, $_REQUEST['newname'], $each_table,
  454. (isset($this_what) ? $this_what : 'data'),
  455. $move, 'db_copy'
  456. )) {
  457. $GLOBALS['_error'] = true;
  458. break;
  459. }
  460. // apply the triggers to the destination db+table
  461. if ($triggers) {
  462. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  463. foreach ($triggers as $trigger) {
  464. $GLOBALS['dbi']->query($trigger['create']);
  465. $GLOBALS['sql_query'] .= "\n" . $trigger['create'] . ';';
  466. }
  467. }
  468. // this does not apply to a rename operation
  469. if (isset($_REQUEST['add_constraints'])
  470. && ! empty($GLOBALS['sql_constraints_query'])
  471. ) {
  472. $sqlContraints[] = $GLOBALS['sql_constraints_query'];
  473. unset($GLOBALS['sql_constraints_query']);
  474. }
  475. }
  476. }
  477. return $sqlContraints;
  478. }
  479. /**
  480. * Run the EVENT definition for selected database
  481. *
  482. * to avoid selecting alternatively the current and new db
  483. * we would need to modify the CREATE definitions to qualify
  484. * the db name
  485. *
  486. * @param string $db database name
  487. *
  488. * @return void
  489. */
  490. public function runEventDefinitionsForDb($db)
  491. {
  492. $event_names = $GLOBALS['dbi']->fetchResult(
  493. 'SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \''
  494. . $GLOBALS['dbi']->escapeString($db) . '\';'
  495. );
  496. if ($event_names) {
  497. foreach ($event_names as $event_name) {
  498. $GLOBALS['dbi']->selectDb($db);
  499. $tmp_query = $GLOBALS['dbi']->getDefinition($db, 'EVENT', $event_name);
  500. // collect for later display
  501. $GLOBALS['sql_query'] .= "\n" . $tmp_query;
  502. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  503. $GLOBALS['dbi']->query($tmp_query);
  504. }
  505. }
  506. }
  507. /**
  508. * Handle the views, return the boolean value whether table rename/copy or not
  509. *
  510. * @param array $views views as an array
  511. * @param boolean $move whether database name is empty or not
  512. * @param string $db database name
  513. *
  514. * @return void
  515. */
  516. public function handleTheViews(array $views, $move, $db)
  517. {
  518. // temporarily force to add DROP IF EXIST to CREATE VIEW query,
  519. // to remove stand-in VIEW that was created earlier
  520. // ( $_REQUEST['drop_if_exists'] is used in moveCopy() )
  521. if (isset($_REQUEST['drop_if_exists'])) {
  522. $temp_drop_if_exists = $_REQUEST['drop_if_exists'];
  523. }
  524. $_REQUEST['drop_if_exists'] = 'true';
  525. foreach ($views as $view) {
  526. $copying_succeeded = Table::moveCopy(
  527. $db, $view, $_REQUEST['newname'], $view, 'structure', $move, 'db_copy'
  528. );
  529. if (! $copying_succeeded) {
  530. $GLOBALS['_error'] = true;
  531. break;
  532. }
  533. }
  534. unset($_REQUEST['drop_if_exists']);
  535. if (isset($temp_drop_if_exists)) {
  536. // restore previous value
  537. $_REQUEST['drop_if_exists'] = $temp_drop_if_exists;
  538. }
  539. }
  540. /**
  541. * Adjust the privileges after Renaming the db
  542. *
  543. * @param string $oldDb Database name before renaming
  544. * @param string $newname New Database name requested
  545. *
  546. * @return void
  547. */
  548. public function adjustPrivilegesMoveDb($oldDb, $newname)
  549. {
  550. if ($GLOBALS['db_priv'] && $GLOBALS['table_priv']
  551. && $GLOBALS['col_priv'] && $GLOBALS['proc_priv']
  552. && $GLOBALS['is_reload_priv']
  553. ) {
  554. $GLOBALS['dbi']->selectDb('mysql');
  555. $newname = str_replace("_", "\_", $newname);
  556. $oldDb = str_replace("_", "\_", $oldDb);
  557. // For Db specific privileges
  558. $query_db_specific = 'UPDATE ' . Util::backquote('db')
  559. . 'SET Db = \'' . $GLOBALS['dbi']->escapeString($newname)
  560. . '\' where Db = \'' . $GLOBALS['dbi']->escapeString($oldDb) . '\';';
  561. $GLOBALS['dbi']->query($query_db_specific);
  562. // For table specific privileges
  563. $query_table_specific = 'UPDATE ' . Util::backquote('tables_priv')
  564. . 'SET Db = \'' . $GLOBALS['dbi']->escapeString($newname)
  565. . '\' where Db = \'' . $GLOBALS['dbi']->escapeString($oldDb) . '\';';
  566. $GLOBALS['dbi']->query($query_table_specific);
  567. // For column specific privileges
  568. $query_col_specific = 'UPDATE ' . Util::backquote('columns_priv')
  569. . 'SET Db = \'' . $GLOBALS['dbi']->escapeString($newname)
  570. . '\' where Db = \'' . $GLOBALS['dbi']->escapeString($oldDb) . '\';';
  571. $GLOBALS['dbi']->query($query_col_specific);
  572. // For procedures specific privileges
  573. $query_proc_specific = 'UPDATE ' . Util::backquote('procs_priv')
  574. . 'SET Db = \'' . $GLOBALS['dbi']->escapeString($newname)
  575. . '\' where Db = \'' . $GLOBALS['dbi']->escapeString($oldDb) . '\';';
  576. $GLOBALS['dbi']->query($query_proc_specific);
  577. // Finally FLUSH the new privileges
  578. $flush_query = "FLUSH PRIVILEGES;";
  579. $GLOBALS['dbi']->query($flush_query);
  580. }
  581. }
  582. /**
  583. * Adjust the privileges after Copying the db
  584. *
  585. * @param string $oldDb Database name before copying
  586. * @param string $newname New Database name requested
  587. *
  588. * @return void
  589. */
  590. public function adjustPrivilegesCopyDb($oldDb, $newname)
  591. {
  592. if ($GLOBALS['db_priv'] && $GLOBALS['table_priv']
  593. && $GLOBALS['col_priv'] && $GLOBALS['proc_priv']
  594. && $GLOBALS['is_reload_priv']
  595. ) {
  596. $GLOBALS['dbi']->selectDb('mysql');
  597. $newname = str_replace("_", "\_", $newname);
  598. $oldDb = str_replace("_", "\_", $oldDb);
  599. $query_db_specific_old = 'SELECT * FROM '
  600. . Util::backquote('db') . ' WHERE '
  601. . 'Db = "' . $oldDb . '";';
  602. $old_privs_db = $GLOBALS['dbi']->fetchResult($query_db_specific_old, 0);
  603. foreach ($old_privs_db as $old_priv) {
  604. $newDb_db_privs_query = 'INSERT INTO ' . Util::backquote('db')
  605. . ' VALUES("' . $old_priv[0] . '", "' . $newname . '", "'
  606. . $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
  607. . '", "' . $old_priv[5] . '", "' . $old_priv[6] . '", "'
  608. . $old_priv[7] . '", "' . $old_priv[8] . '", "' . $old_priv[9]
  609. . '", "' . $old_priv[10] . '", "' . $old_priv[11] . '", "'
  610. . $old_priv[12] . '", "' . $old_priv[13] . '", "' . $old_priv[14]
  611. . '", "' . $old_priv[15] . '", "' . $old_priv[16] . '", "'
  612. . $old_priv[17] . '", "' . $old_priv[18] . '", "' . $old_priv[19]
  613. . '", "' . $old_priv[20] . '", "' . $old_priv[21] . '");';
  614. $GLOBALS['dbi']->query($newDb_db_privs_query);
  615. }
  616. // For Table Specific privileges
  617. $query_table_specific_old = 'SELECT * FROM '
  618. . Util::backquote('tables_priv') . ' WHERE '
  619. . 'Db = "' . $oldDb . '";';
  620. $old_privs_table = $GLOBALS['dbi']->fetchResult(
  621. $query_table_specific_old,
  622. 0
  623. );
  624. foreach ($old_privs_table as $old_priv) {
  625. $newDb_table_privs_query = 'INSERT INTO ' . Util::backquote(
  626. 'tables_priv'
  627. ) . ' VALUES("' . $old_priv[0] . '", "' . $newname . '", "'
  628. . $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
  629. . '", "' . $old_priv[5] . '", "' . $old_priv[6] . '", "'
  630. . $old_priv[7] . '");';
  631. $GLOBALS['dbi']->query($newDb_table_privs_query);
  632. }
  633. // For Column Specific privileges
  634. $query_col_specific_old = 'SELECT * FROM '
  635. . Util::backquote('columns_priv') . ' WHERE '
  636. . 'Db = "' . $oldDb . '";';
  637. $old_privs_col = $GLOBALS['dbi']->fetchResult(
  638. $query_col_specific_old,
  639. 0
  640. );
  641. foreach ($old_privs_col as $old_priv) {
  642. $newDb_col_privs_query = 'INSERT INTO ' . Util::backquote(
  643. 'columns_priv'
  644. ) . ' VALUES("' . $old_priv[0] . '", "' . $newname . '", "'
  645. . $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
  646. . '", "' . $old_priv[5] . '", "' . $old_priv[6] . '");';
  647. $GLOBALS['dbi']->query($newDb_col_privs_query);
  648. }
  649. // For Procedure Specific privileges
  650. $query_proc_specific_old = 'SELECT * FROM '
  651. . Util::backquote('procs_priv') . ' WHERE '
  652. . 'Db = "' . $oldDb . '";';
  653. $old_privs_proc = $GLOBALS['dbi']->fetchResult(
  654. $query_proc_specific_old,
  655. 0
  656. );
  657. foreach ($old_privs_proc as $old_priv) {
  658. $newDb_proc_privs_query = 'INSERT INTO ' . Util::backquote(
  659. 'procs_priv'
  660. ) . ' VALUES("' . $old_priv[0] . '", "' . $newname . '", "'
  661. . $old_priv[2] . '", "' . $old_priv[3] . '", "' . $old_priv[4]
  662. . '", "' . $old_priv[5] . '", "' . $old_priv[6] . '", "'
  663. . $old_priv[7] . '");';
  664. $GLOBALS['dbi']->query($newDb_proc_privs_query);
  665. }
  666. // Finally FLUSH the new privileges
  667. $flush_query = "FLUSH PRIVILEGES;";
  668. $GLOBALS['dbi']->query($flush_query);
  669. }
  670. }
  671. /**
  672. * Create all accumulated constraints
  673. *
  674. * @param array $sqlConstratints array of sql constraints for the database
  675. *
  676. * @return void
  677. */
  678. public function createAllAccumulatedConstraints(array $sqlConstratints)
  679. {
  680. $GLOBALS['dbi']->selectDb($_REQUEST['newname']);
  681. foreach ($sqlConstratints as $one_query) {
  682. $GLOBALS['dbi']->query($one_query);
  683. // and prepare to display them
  684. $GLOBALS['sql_query'] .= "\n" . $one_query;
  685. }
  686. }
  687. /**
  688. * Duplicate the bookmarks for the db (done once for each db)
  689. *
  690. * @param boolean $_error whether table rename/copy or not
  691. * @param string $db database name
  692. *
  693. * @return void
  694. */
  695. public function duplicateBookmarks($_error, $db)
  696. {
  697. if (! $_error && $db != $_REQUEST['newname']) {
  698. $get_fields = array('user', 'label', 'query');
  699. $where_fields = array('dbase' => $db);
  700. $new_fields = array('dbase' => $_REQUEST['newname']);
  701. Table::duplicateInfo(
  702. 'bookmarkwork', 'bookmark', $get_fields,
  703. $where_fields, $new_fields
  704. );
  705. }
  706. }
  707. /**
  708. * Get the HTML snippet for order the table
  709. *
  710. * @param array $columns columns array
  711. *
  712. * @return string $html_out
  713. */
  714. public function getHtmlForOrderTheTable(array $columns)
  715. {
  716. $html_output = '<div>';
  717. $html_output .= '<form method="post" id="alterTableOrderby" '
  718. . 'action="tbl_operations.php">';
  719. $html_output .= Url::getHiddenInputs(
  720. $GLOBALS['db'], $GLOBALS['table']
  721. );
  722. $html_output .= '<fieldset id="fieldset_table_order">'
  723. . '<legend>' . __('Alter table order by') . '</legend>'
  724. . '<select name="order_field">';
  725. foreach ($columns as $fieldname) {
  726. $html_output .= '<option '
  727. . 'value="' . htmlspecialchars($fieldname['Field']) . '">'
  728. . htmlspecialchars($fieldname['Field']) . '</option>' . "\n";
  729. }
  730. $html_output .= '</select> ' . __('(singly)') . ' '
  731. . '<br />'
  732. . '<input id="order_order_asc" name="order_order"'
  733. . ' type="radio" value="asc" checked="checked" />'
  734. . '<label for="order_order_asc">' . __('Ascending') . '</label>'
  735. . '<input id="order_order_desc" name="order_order"'
  736. . ' type="radio" value="desc" />'
  737. . '<label for="order_order_desc">' . __('Descending') . '</label>'
  738. . '</fieldset>'
  739. . '<fieldset class="tblFooters">'
  740. . '<input type="hidden" name="submitorderby" value="1" />'
  741. . '<input type="submit" value="' . __('Go') . '" />'
  742. . '</fieldset>'
  743. . '</form>'
  744. . '</div>';
  745. return $html_output;
  746. }
  747. /**
  748. * Get the HTML snippet for move table
  749. *
  750. * @return string $html_output
  751. */
  752. public function getHtmlForMoveTable()
  753. {
  754. $html_output = '<div>';
  755. $html_output .= '<form method="post" action="tbl_operations.php"'
  756. . ' id="moveTableForm" class="ajax"'
  757. . ' onsubmit="return emptyCheckTheField(this, \'new_name\')">'
  758. . Url::getHiddenInputs($GLOBALS['db'], $GLOBALS['table']);
  759. $html_output .= '<input type="hidden" name="reload" value="1" />'
  760. . '<input type="hidden" name="what" value="data" />'
  761. . '<fieldset id="fieldset_table_rename">';
  762. $html_output .= '<legend>' . __('Move table to (database<b>.</b>table)')
  763. . '</legend>';
  764. if (count($GLOBALS['dblist']->databases) > $GLOBALS['cfg']['MaxDbList']) {
  765. $html_output .= '<input type="text" maxlength="100" '
  766. . 'name="target_db" value="' . htmlspecialchars($GLOBALS['db'])
  767. . '"/>';
  768. } else {
  769. $html_output .= '<select class="halfWidth" name="target_db">'
  770. . $GLOBALS['dblist']->databases->getHtmlOptions(true, false)
  771. . '</select>';
  772. }
  773. $html_output .= '&nbsp;<strong>.</strong>&nbsp;';
  774. $html_output .= '<input class="halfWidth" type="text" name="new_name"'
  775. . ' maxlength="64" required="required" '
  776. . 'value="' . htmlspecialchars($GLOBALS['table']) . '" /><br />';
  777. // starting with MySQL 5.0.24, SHOW CREATE TABLE includes the AUTO_INCREMENT
  778. // next value but users can decide if they want it or not for the operation
  779. $html_output .= '<input type="checkbox" name="sql_auto_increment" '
  780. . 'value="1" id="checkbox_auto_increment_mv" checked="checked" />'
  781. . '<label for="checkbox_auto_increment_mv">'
  782. . __('Add AUTO_INCREMENT value')
  783. . '</label><br />';
  784. if ($GLOBALS['table_priv'] && $GLOBALS['col_priv']
  785. && $GLOBALS['is_reload_priv']
  786. ) {
  787. $html_output .= '<input type="checkbox" name="adjust_privileges" '
  788. . 'value="1" id="checkbox_privileges_tables_move" '
  789. . 'checked="checked" />';
  790. } else {
  791. $html_output .= '<input type="checkbox" name="adjust_privileges" '
  792. . 'value="1" id="checkbox_privileges_tables_move" title="' . __(
  793. 'You don\'t have sufficient privileges to perform this '
  794. . 'operation; Please refer to the documentation for more details'
  795. )
  796. . '" disabled/>';
  797. }
  798. $html_output .= '<label for="checkbox_privileges_tables_move">'
  799. . __('Adjust privileges') . Util::showDocu('faq', 'faq6-39')
  800. . '</label><br />';
  801. $html_output .= '</fieldset><fieldset class="tblFooters">'
  802. . '<input type="submit" name="submit_move" value="' . __('Go') . '" />'
  803. . '</fieldset>'
  804. . '</form>'
  805. . '</div>';
  806. return $html_output;
  807. }
  808. /**
  809. * Get the HTML div for Table option
  810. *
  811. * @param Table $pma_table Table object
  812. * @param string $comment Comment
  813. * @param array $tbl_collation table collation
  814. * @param string $tbl_storage_engine table storage engine
  815. * @param string $pack_keys pack keys
  816. * @param string $auto_increment value of auto increment
  817. * @param string $delay_key_write delay key write
  818. * @param string $transactional value of transactional
  819. * @param string $page_checksum value of page checksum
  820. * @param string $checksum the checksum
  821. *
  822. * @return string $html_output
  823. */
  824. public function getTableOptionDiv($pma_table, $comment, $tbl_collation, $tbl_storage_engine,
  825. $pack_keys, $auto_increment, $delay_key_write,
  826. $transactional, $page_checksum, $checksum
  827. ) {
  828. $html_output = '<div>';
  829. $html_output .= '<form method="post" action="tbl_operations.php"';
  830. $html_output .= ' id="tableOptionsForm" class="ajax">';
  831. $html_output .= Url::getHiddenInputs(
  832. $GLOBALS['db'], $GLOBALS['table']
  833. );
  834. $html_output .= '<input type="hidden" name="reload" value="1" />';
  835. $html_output .= $this->getTableOptionFieldset(
  836. $pma_table, $comment, $tbl_collation,
  837. $tbl_storage_engine, $pack_keys,
  838. $delay_key_write, $auto_increment, $transactional, $page_checksum,
  839. $checksum
  840. );
  841. $html_output .= '<fieldset class="tblFooters">'
  842. . '<input type="hidden" name="submitoptions" value="1" />'
  843. . '<input type="submit" value="' . __('Go') . '" />'
  844. . '</fieldset>'
  845. . '</form>'
  846. . '</div>';
  847. return $html_output;
  848. }
  849. /**
  850. * Get HTML for the rename table part of table options
  851. *
  852. * @return string $html_output
  853. */
  854. private function getHtmlForRenameTable()
  855. {
  856. $html_output = '<tr><td class="vmiddle">' . __('Rename table to') . '</td>'
  857. . '<td>'
  858. . '<input type="text" name="new_name" maxlength="64" '
  859. . 'value="' . htmlspecialchars($GLOBALS['table'])
  860. . '" required="required" />'
  861. . '</td></tr>'
  862. . '<tr><td></td><td>';
  863. if ($GLOBALS['table_priv'] && $GLOBALS['col_priv']
  864. && $GLOBALS['is_reload_priv']
  865. ) {
  866. $html_output .= '<input type="checkbox" name="adjust_privileges" '
  867. . 'value="1" id="checkbox_privileges_table_options" '
  868. . 'checked="checked" />';
  869. } else {
  870. $html_output .= '<input type="checkbox" name="adjust_privileges" '
  871. . 'value="1" id="checkbox_privileges_table_options" title="' . __(
  872. 'You don\'t have sufficient privileges to perform this '
  873. . 'operation; Please refer to the documentation for more details'
  874. )
  875. . '" disabled/>';
  876. }
  877. $html_output .= '<label for="checkbox_privileges_table_options">'
  878. . __('Adjust privileges') . '&nbsp;'
  879. . Util::showDocu('faq', 'faq6-39') . '</label>';
  880. $html_output .= '</td></tr>';
  881. return $html_output;
  882. }
  883. /**
  884. * Get HTML for the table comments part of table options
  885. *
  886. * @param string $current_value of the table comments
  887. *
  888. * @return string $html_output
  889. */
  890. private function getHtmlForTableComments($current_value)
  891. {
  892. $commentLength = $GLOBALS['dbi']->getVersion() >= 50503 ? 2048 : 60;
  893. $html_output = '<tr><td class="vmiddle">' . __('Table comments') . '</td>'
  894. . '<td><input type="text" name="comment" '
  895. . 'maxlength="' . $commentLength . '"'
  896. . 'value="' . htmlspecialchars($current_value) . '" />'
  897. . '<input type="hidden" name="prev_comment" value="'
  898. . htmlspecialchars($current_value) . '" />'
  899. . '</td>'
  900. . '</tr>';
  901. return $html_output;
  902. }
  903. /**
  904. * Get HTML for the PACK KEYS part of table options
  905. *
  906. * @param string $current_value of the pack keys option
  907. *
  908. * @return string $html_output
  909. */
  910. private function getHtmlForPackKeys($current_value)
  911. {
  912. $html_output = '<tr>'
  913. . '<td class="vmiddle"><label for="new_pack_keys">PACK_KEYS</label></td>'
  914. . '<td><select name="new_pack_keys" id="new_pack_keys">';
  915. $html_output .= '<option value="DEFAULT"';
  916. if ($current_value == 'DEFAULT') {
  917. $html_output .= 'selected="selected"';
  918. }
  919. $html_output .= '>DEFAULT</option>
  920. <option value="0"';
  921. if ($current_value == '0') {
  922. $html_output .= 'selected="selected"';
  923. }
  924. $html_output .= '>0</option>
  925. <option value="1" ';
  926. if ($current_value == '1') {
  927. $html_output .= 'selected="selected"';
  928. }
  929. $html_output .= '>1</option>'
  930. . '</select>'
  931. . '</td>'
  932. . '</tr>';
  933. return $html_output;
  934. }
  935. /**
  936. * Get HTML fieldset for Table option, it contains HTML table for options
  937. *
  938. * @param Table $pma_table Table object
  939. * @param string $comment Comment
  940. * @param array $tbl_collation table collation
  941. * @param string $tbl_storage_engine table storage engine
  942. * @param string $pack_keys pack keys
  943. * @param string $delay_key_write delay key write
  944. * @param string $auto_increment value of auto increment
  945. * @param string $transactional value of transactional
  946. * @param string $page_checksum value of page checksum
  947. * @param string $checksum the checksum
  948. *
  949. * @return string $html_output
  950. */
  951. private function getTableOptionFieldset($pma_table, $comment, $tbl_collation,
  952. $tbl_storage_engine, $pack_keys,
  953. $delay_key_write, $auto_increment, $transactional,
  954. $page_checksum, $checksum
  955. ) {
  956. $html_output = '<fieldset>'
  957. . '<legend>' . __('Table options') . '</legend>';
  958. $html_output .= '<table>';
  959. $html_output .= $this->getHtmlForRenameTable();
  960. $html_output .= $this->getHtmlForTableComments($comment);
  961. //Storage engine
  962. $html_output .= '<tr><td class="vmiddle">' . __('Storage Engine')
  963. . '&nbsp;' . Util::showMySQLDocu('Storage_engines')
  964. . '</td>'
  965. . '<td>'
  966. . StorageEngine::getHtmlSelect(
  967. 'new_tbl_storage_engine', null, $tbl_storage_engine
  968. )
  969. . '</td>'
  970. . '</tr>';
  971. //Table character set
  972. $html_output .= '<tr><td class="vmiddle">' . __('Collation') . '</td>'
  973. . '<td>'
  974. . Charsets::getCollationDropdownBox(
  975. $GLOBALS['dbi'],
  976. $GLOBALS['cfg']['Server']['DisableIS'],
  977. 'tbl_collation',
  978. null,
  979. $tbl_collation,
  980. false
  981. )
  982. . '</td>'
  983. . '</tr>';
  984. // Change all Column collations
  985. $html_output .= '<tr><td></td><td>'
  986. . '<input type="checkbox" name="change_all_collations" value="1" '
  987. . 'id="checkbox_change_all_collations" />'
  988. . '<label for="checkbox_change_all_collations">'
  989. . __('Change all column collations')
  990. . '</label>'
  991. . '</td></tr>';
  992. if ($pma_table->isEngine(array('MYISAM', 'ARIA', 'ISAM'))) {
  993. $html_output .= $this->getHtmlForPackKeys($pack_keys);
  994. } // end if (MYISAM|ISAM)
  995. if ($pma_table->isEngine(array('MYISAM', 'ARIA'))) {
  996. $html_output .= $this->getHtmlForTableRow(
  997. 'new_checksum',
  998. 'CHECKSUM',
  999. $checksum
  1000. );
  1001. $html_output .= $this->getHtmlForTableRow(
  1002. 'new_delay_key_write',
  1003. 'DELAY_KEY_WRITE',
  1004. $delay_key_write
  1005. );
  1006. } // end if (MYISAM)
  1007. if ($pma_table->isEngine('ARIA')) {
  1008. $html_output .= $this->getHtmlForTableRow(
  1009. 'new_transactional',
  1010. 'TRANSACTIONAL',
  1011. $transactional
  1012. );
  1013. $html_output .= $this->getHtmlForTableRow(
  1014. 'new_page_checksum',
  1015. 'PAGE_CHECKSUM',
  1016. $page_checksum
  1017. );
  1018. } // end if (ARIA)
  1019. if (strlen($auto_increment) > 0
  1020. && $pma_table->isEngine(array('MYISAM', 'ARIA', 'INNODB', 'PBXT'))
  1021. ) {
  1022. $html_output .= '<tr><td class="vmiddle">'
  1023. . '<label for="auto_increment_opt">AUTO_INCREMENT</label></td>'
  1024. . '<td><input type="number" name="new_auto_increment" '
  1025. . 'id="auto_increment_opt"'
  1026. . 'value="' . $auto_increment . '" /></td>'
  1027. . '</tr> ';
  1028. } // end if (MYISAM|INNODB)
  1029. $possible_row_formats = $this->getPossibleRowFormat();
  1030. // for MYISAM there is also COMPRESSED but it can be set only by the
  1031. // myisampack utility, so don't offer here the choice because if we
  1032. // try it inside an ALTER TABLE, MySQL (at least in 5.1.23-maria)
  1033. // does not return a warning
  1034. // (if the table was compressed, it can be seen on the Structure page)
  1035. if (isset($possible_row_formats[$tbl_storage_engine])) {
  1036. $current_row_format
  1037. = mb_strtoupper($GLOBALS['showtable']['Row_format']);
  1038. $html_output .= '<tr><td class="vmiddle">'
  1039. . '<label for="new_row_format">ROW_FORMAT</label></td>'
  1040. . '<td>';
  1041. $html_output .= Util::getDropdown(
  1042. 'new_row_format', $possible_row_formats[$tbl_storage_engine],
  1043. $current_row_format, 'new_row_format'
  1044. );
  1045. $html_output .= '</td></tr>';
  1046. }
  1047. $html_output .= '</table>'
  1048. . '</fieldset>';
  1049. return $html_output;
  1050. }
  1051. /**
  1052. * Get the common HTML table row (tr) for new_checksum, new_delay_key_write,
  1053. * new_transactional and new_page_checksum
  1054. *
  1055. * @param string $attribute class, name and id attribute
  1056. * @param string $label label value
  1057. * @param string $val checksum, delay_key_write, transactional, page_checksum
  1058. *
  1059. * @return string $html_output
  1060. */
  1061. private function getHtmlForTableRow($attribute, $label, $val)
  1062. {
  1063. return '<tr>'
  1064. . '<td class="vmiddle">'
  1065. . '<label for="' . $attribute . '">' . $label . '</label>'
  1066. . '</td>'
  1067. . '<td>'
  1068. . '<input type="checkbox" name="' . $attribute . '" id="' . $attribute . '"'
  1069. . ' value="1"' . ((!empty($val) && $val == 1) ? ' checked="checked"' : '')
  1070. . '/>'
  1071. . '</td>'
  1072. . '</tr>';
  1073. }
  1074. /**
  1075. * Get array of possible row formats
  1076. *
  1077. * @return array $possible_row_formats
  1078. */
  1079. private function getPossibleRowFormat()
  1080. {
  1081. // the outer array is for engines, the inner array contains the dropdown
  1082. // option values as keys then the dropdown option labels
  1083. $possible_row_formats = array(
  1084. 'ARCHIVE' => array(
  1085. 'COMPRESSED' => 'COMPRESSED',
  1086. ),
  1087. 'ARIA' => array(
  1088. 'FIXED' => 'FIXED',
  1089. 'DYNAMIC' => 'DYNAMIC',
  1090. 'PAGE' => 'PAGE'
  1091. ),
  1092. 'MARIA' => array(
  1093. 'FIXED' => 'FIXED',
  1094. 'DYNAMIC' => 'DYNAMIC',
  1095. 'PAGE' => 'PAGE'
  1096. ),
  1097. 'MYISAM' => array(
  1098. 'FIXED' => 'FIXED',
  1099. 'DYNAMIC' => 'DYNAMIC'
  1100. ),
  1101. 'PBXT' => array(
  1102. 'FIXED' => 'FIXED',
  1103. 'DYNAMIC' => 'DYNAMIC'
  1104. ),
  1105. 'INNODB' => array(
  1106. 'COMPACT' => 'COMPACT',
  1107. 'REDUNDANT' => 'REDUNDANT'
  1108. )
  1109. );
  1110. /** @var Innodb $innodbEnginePlugin */
  1111. $innodbEnginePlugin = StorageEngine::getEngine('Innodb');
  1112. $innodbPluginVersion = $innodbEnginePlugin->getInnodbPluginVersion();
  1113. if (!empty($innodbPluginVersion)) {
  1114. $innodb_file_format = $innodbEnginePlugin->getInnodbFileFormat();
  1115. } else {
  1116. $innodb_file_format = '';
  1117. }
  1118. if ('Barracuda' == $innodb_file_format
  1119. && $innodbEnginePlugin->supportsFilePerTable()
  1120. ) {
  1121. $possible_row_formats['INNODB']['DYNAMIC'] = 'DYNAMIC';
  1122. $possible_row_formats['INNODB']['COMPRESSED'] = 'COMPRESSED';
  1123. }
  1124. return $possible_row_formats;
  1125. }
  1126. /**
  1127. * Get HTML div for copy table
  1128. *
  1129. * @return string $html_output
  1130. */
  1131. public function getHtmlForCopytable()
  1132. {
  1133. $html_output = '<div>';
  1134. $html_output .= '<form method="post" action="tbl_operations.php" '
  1135. . 'name="copyTable" '
  1136. . 'id="copyTable" '
  1137. . ' class="ajax" '
  1138. . 'onsubmit="return emptyCheckTheField(this, \'new_name\')">'
  1139. . Url::getHiddenInputs($GLOBALS['db'], $GLOBALS['table'])
  1140. . '<input type="hidden" name="reload" value="1" />';
  1141. $html_output .= '<fieldset>';
  1142. $html_output .= '<legend>'
  1143. . __('Copy table to (database<b>.</b>table)') . '</legend>';
  1144. if (count($GLOBALS['dblist']->databases) > $GLOBALS['cfg']['MaxDbList']) {
  1145. $html_output .= '<input class="halfWidth" type="text" maxlength="100" '
  1146. . 'name="target_db" '
  1147. . 'value="' . htmlspecialchars($GLOBALS['db']) . '"/>';
  1148. } else {
  1149. $html_output .= '<select class="halfWidth" name="target_db">'
  1150. . $GLOBALS['dblist']->databases->getHtmlOptions(true, false)
  1151. . '</select>';
  1152. }
  1153. $html_output .= '&nbsp;<strong>.</strong>&nbsp;';
  1154. $html_output .= '<input class="halfWidth" type="text" required="required" '
  1155. . 'name="new_name" maxlength="64" '
  1156. . 'value="' . htmlspecialchars($GLOBALS['table']) . '"/><br />';
  1157. $choices = array(
  1158. 'structure' => __('Structure only'),
  1159. 'data' => __('Structure and data'),
  1160. 'dataonly' => __('Data only')
  1161. );
  1162. $html_output .= Util::getRadioFields(
  1163. 'what', $choices, 'data', true
  1164. );
  1165. $html_output .= '<br />';
  1166. $html_output .= '<input type="checkbox" name="drop_if_exists" '
  1167. . 'value="true" id="checkbox_drop" />'
  1168. . '<label for="checkbox_drop">'
  1169. . sprintf(__('Add %s'), 'DROP TABLE') . '</label><br />'
  1170. . '<input type="checkbox" name="sql_auto_increment" '
  1171. . 'value="1" id="checkbox_auto_increment_cp" />'
  1172. . '<label for="checkbox_auto_increment_cp">'
  1173. . __('Add AUTO_INCREMENT value') . '</label><br />';
  1174. // display "Add constraints" choice only if there are
  1175. // foreign keys
  1176. if ($this->relation->getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'foreign')) {
  1177. $html_output .= '<input type="checkbox" name="add_constraints" '
  1178. . 'value="1" id="checkbox_constraints" checked="checked"/>';
  1179. $html_output .= '<label for="checkbox_constraints">'
  1180. . __('Add constraints') . '</label><br />';
  1181. } // endif
  1182. $html_output .= '<br />';
  1183. if ($GLOBALS['table_priv'] && $GLOBALS['col_priv']
  1184. && $GLOBALS['is_reload_priv']
  1185. ) {
  1186. $html_output .= '<input type="checkbox" name="adjust_privileges" '
  1187. . 'value="1" id="checkbox_adjust_privileges" checked="checked" />';
  1188. } else {
  1189. $html_output .= '<input type="checkbox" name="adjust_privileges" '
  1190. . 'value="1" id="checkbox_adjust_privileges" title="' . __(
  1191. 'You don\'t have sufficient privileges to perform this '
  1192. . 'operation; Please refer to the documentation for more details'
  1193. )
  1194. . '" disabled/>';
  1195. }
  1196. $html_output .= '<label for="checkbox_adjust_privileges">'
  1197. . __('Adjust privileges') . Util::showDocu('faq', 'faq6-39')
  1198. . '</label><br />';
  1199. $pma_switch_to_new = isset($_SESSION['pma_switch_to_new']) && $_SESSION['pma_switch_to_new'];
  1200. $html_output .= '<input type="checkbox" name="switch_to_new" value="true"'
  1201. . 'id="checkbox_switch"'
  1202. . ($pma_switch_to_new ? ' checked="checked"' : '') . '/>';
  1203. $html_output .= '<label for="checkbox_switch">'
  1204. . __('Switch to copied table') . '</label>'
  1205. . '</fieldset>';
  1206. $html_output .= '<fieldset class="tblFooters">'
  1207. . '<input type="submit" name="submit_copy" value="' . __('Go') . '" />'
  1208. . '</fieldset>'
  1209. . '</form>'
  1210. . '</div>';
  1211. return $html_output;
  1212. }
  1213. /**
  1214. * Get HTML snippet for table maintenance
  1215. *
  1216. * @param Table $pma_table Table object
  1217. * @param array $url_params array of URL parameters
  1218. *
  1219. * @return string $html_output
  1220. */
  1221. public function getHtmlForTableMaintenance($pma_table, array $url_params)
  1222. {
  1223. $html_output = '<div>';
  1224. $html_output .= '<fieldset>'
  1225. . '<legend>' . __('Table maintenance') . '</legend>';
  1226. $html_output .= '<ul id="tbl_maintenance">';
  1227. // Note: BERKELEY (BDB) is no longer supported, starting with MySQL 5.1
  1228. $html_output .= $this->getListofMaintainActionLink($pma_table, $url_params);
  1229. $html_output .= '</ul>'
  1230. . '</fieldset>'
  1231. . '</div>';
  1232. return $html_output;
  1233. }
  1234. /**
  1235. * Get HTML 'li' having a link of maintain action
  1236. *
  1237. * @param Table $pma_table Table object
  1238. * @param array $url_params Array of URL parameters
  1239. *
  1240. * @return string $html_output
  1241. */
  1242. private function getListofMaintainActionLink($pma_table, array $url_params)
  1243. {
  1244. $html_output = '';
  1245. // analyze table
  1246. if ($pma_table->isEngine(array('MYISAM', 'ARIA', 'INNODB', 'BERKELEYDB', 'TOKUDB'))) {
  1247. $params = array(
  1248. 'sql_query' => 'ANALYZE TABLE '
  1249. . Util::backquote($GLOBALS['table']),
  1250. 'table_maintenance' => 'Go',
  1251. );
  1252. $html_output .= $this->getMaintainActionlink(
  1253. __('Analyze table'),
  1254. $params,
  1255. $url_params,
  1256. 'ANALYZE_TABLE'
  1257. );
  1258. }
  1259. // check table
  1260. if ($pma_table->isEngine(array('MYISAM', 'ARIA', 'INNODB', 'TOKUDB'))) {
  1261. $params = array(
  1262. 'sql_query' => 'CHECK TABLE '
  1263. . Util::backquote($GLOBALS['table']),
  1264. 'table_maintenance' => 'Go',
  1265. );
  1266. $html_output .= $this->getMaintainActionlink(
  1267. __('Check table'),
  1268. $params,
  1269. $url_params,
  1270. 'CHECK_TABLE'
  1271. );
  1272. }
  1273. // checksum table
  1274. $params = array(
  1275. 'sql_query' => 'CHECKSUM TABLE '
  1276. . Util::backquote($GLOBALS['table']),
  1277. 'table_maintenance' => 'Go',
  1278. );
  1279. $html_output .= $this->getMaintainActionlink(
  1280. __('Checksum table'),
  1281. $params,
  1282. $url_params,
  1283. 'CHECKSUM_TABLE'
  1284. );
  1285. // defragment table
  1286. if ($pma_table->isEngine(array('INNODB'))) {
  1287. $params = array(
  1288. 'sql_query' => 'ALTER TABLE '
  1289. . Util::backquote($GLOBALS['table'])
  1290. . ' ENGINE = InnoDB;'
  1291. );
  1292. $html_output .= $this->getMaintainActionlink(
  1293. __('Defragment table'),
  1294. $params,
  1295. $url_params,
  1296. 'InnoDB_File_Defragmenting'
  1297. );
  1298. }
  1299. // flush table
  1300. $params = array(
  1301. 'sql_query' => 'FLUSH TABLE '
  1302. . Util::backquote($GLOBALS['table']),
  1303. 'message_to_show' => sprintf(
  1304. __('Table %s has been flushed.'),
  1305. htmlspecialchars($GLOBALS['table'])
  1306. ),
  1307. 'reload' => 1,
  1308. );
  1309. $html_output .= $this->getMaintainActionlink(
  1310. __('Flush the table (FLUSH)'),
  1311. $params,
  1312. $url_params,
  1313. 'FLUSH'
  1314. );
  1315. // optimize table
  1316. if ($pma_table->isEngine(array('MYISAM', 'ARIA', 'INNODB', 'BERKELEYDB', 'TOKUDB'))) {
  1317. $params = array(
  1318. 'sql_query' => 'OPTIMIZE TABLE '
  1319. . Util::backquote($GLOBALS['table']),
  1320. 'table_maintenance' => 'Go',
  1321. );
  1322. $html_output .= $this->getMaintainActionlink(
  1323. __('Optimize table'),
  1324. $params,
  1325. $url_params,
  1326. 'OPTIMIZE_TABLE'
  1327. );
  1328. }
  1329. // repair table
  1330. if ($pma_table->isEngine(array('MYISAM', 'ARIA'))) {
  1331. $params = array(
  1332. 'sql_query' => 'REPAIR TABLE '
  1333. . Util::backquote($GLOBALS['table']),
  1334. 'table_maintenance' => 'Go',
  1335. );
  1336. $html_output .= $this->getMaintainActionlink(
  1337. __('Repair table'),
  1338. $params,
  1339. $url_params,
  1340. 'REPAIR_TABLE'
  1341. );
  1342. }
  1343. return $html_output;
  1344. }
  1345. /**
  1346. * Get maintain action HTML link
  1347. *
  1348. * @param string $action_message action message
  1349. * @param array $params url parameters array
  1350. * @param array $url_params additional url parameters
  1351. * @param string $link contains name of page/anchor that is being linked
  1352. *
  1353. * @return string $html_output
  1354. */
  1355. private function getMaintainActionlink($action_message, array $params, array $url_params, $link)
  1356. {
  1357. return '<li>'
  1358. . Util::linkOrButton(
  1359. 'sql.php' . Url::getCommon(array_merge($url_params, $params)),
  1360. $action_message,
  1361. ['class' => 'maintain_action ajax']
  1362. )
  1363. . Util::showMySQLDocu($link)
  1364. . '</li>';
  1365. }
  1366. /**
  1367. * Get HTML for Delete data or table (truncate table, drop table)
  1368. *
  1369. * @param array $truncate_table_url_params url parameter array for truncate table
  1370. * @param array $dropTableUrlParams url parameter array for drop table
  1371. *
  1372. * @return string $html_output
  1373. */
  1374. public function getHtmlForDeleteDataOrTable(
  1375. array $truncate_table_url_params,
  1376. array $dropTableUrlParams
  1377. ) {
  1378. $html_output = '<div>'
  1379. . '<fieldset class="caution">'
  1380. . '<legend>' . __('Delete data or table') . '</legend>';
  1381. $html_output .= '<ul>';
  1382. if (! empty($truncate_table_url_params)) {
  1383. $html_output .= $this->getDeleteDataOrTablelink(
  1384. $truncate_table_url_params,
  1385. 'TRUNCATE_TABLE',
  1386. __('Empty the table (TRUNCATE)'),
  1387. 'truncate_tbl_anchor'
  1388. );
  1389. }
  1390. if (!empty($dropTableUrlParams)) {
  1391. $html_output .= $this->getDeleteDataOrTablelink(
  1392. $dropTableUrlParams,
  1393. 'DROP_TABLE',
  1394. __('Delete the table (DROP)'),
  1395. 'drop_tbl_anchor'
  1396. );
  1397. }
  1398. $html_output .= '</ul></fieldset></div>';
  1399. return $html_output;
  1400. }
  1401. /**
  1402. * Get the HTML link for Truncate table, Drop table and Drop db
  1403. *
  1404. * @param array $url_params url parameter array for delete data or table
  1405. * @param string $syntax TRUNCATE_TABLE or DROP_TABLE or DROP_DATABASE
  1406. * @param string $link link to be shown
  1407. * @param string $htmlId id of the link
  1408. *
  1409. * @return string html output
  1410. */
  1411. public function getDeleteDataOrTablelink(array $url_params, $syntax, $link, $htmlId)
  1412. {
  1413. return '<li>' . Util::linkOrButton(
  1414. 'sql.php' . Url::getCommon($url_params),
  1415. $link,
  1416. array('id' => $htmlId, 'class' => 'ajax')
  1417. )
  1418. . Util::showMySQLDocu($syntax)
  1419. . '</li>';
  1420. }
  1421. /**
  1422. * Get HTML snippet for partition maintenance
  1423. *
  1424. * @param array $partition_names array of partition names for a specific db/table
  1425. * @param array $url_params url parameters
  1426. *
  1427. * @return string $html_output
  1428. */
  1429. public function getHtmlForPartitionMaintenance(array $partition_names, array $url_params)
  1430. {
  1431. $choices = array(
  1432. 'ANALYZE' => __('Analyze'),
  1433. 'CHECK' => __('Check'),
  1434. 'OPTIMIZE' => __('Optimize'),
  1435. 'REBUILD' => __('Rebuild'),
  1436. 'REPAIR' => __('Repair'),
  1437. 'TRUNCATE' => __('Truncate')
  1438. );
  1439. $partition_method = Partition::getPartitionMethod(
  1440. $GLOBALS['db'], $GLOBALS['table']
  1441. );
  1442. // add COALESCE or DROP option to choices array depeding on Partition method
  1443. if ($partition_method == 'RANGE'
  1444. || $partition_method == 'RANGE COLUMNS'
  1445. || $partition_method == 'LIST'
  1446. || $partition_method == 'LIST COLUMNS'
  1447. ) {
  1448. $choices['DROP'] = __('Drop');
  1449. } else {
  1450. $choices['COALESCE'] = __('Coalesce');
  1451. }
  1452. $html_output = '<div>'
  1453. . '<form id="partitionsForm" class="ajax" '
  1454. . 'method="post" action="tbl_operations.php" >'
  1455. . Url::getHiddenInputs($GLOBALS['db'], $GLOBALS['table'])
  1456. . '<fieldset>'
  1457. . '<legend>'
  1458. . __('Partition maintenance')
  1459. . Util::showMySQLDocu('partitioning_maintenance')
  1460. . '</legend>';
  1461. $html_select = '<select id="partition_name" name="partition_name[]"'
  1462. . ' multiple="multiple" required="required">' . "\n";
  1463. $first = true;
  1464. foreach ($partition_names as $one_partition) {
  1465. $one_partition = htmlspecialchars($one_partition);
  1466. $html_select .= '<option value="' . $one_partition . '"';
  1467. if ($first) {
  1468. $html_select .= ' selected="selected"';
  1469. $first = false;
  1470. }
  1471. $html_select .= '>' . $one_partition . '</option>' . "\n";
  1472. }
  1473. $html_select .= '</select>' . "\n";
  1474. $html_output .= sprintf(__('Partition %s'), $html_select);
  1475. $html_output .= '<div class="clearfloat" />';
  1476. $html_output .= Util::getRadioFields(
  1477. 'partition_operation', $choices, 'ANALYZE', false, true, 'floatleft'
  1478. );
  1479. $this_url_params = array_merge(
  1480. $url_params,
  1481. array(
  1482. 'sql_query' => 'ALTER TABLE '
  1483. . Util::backquote($GLOBALS['table'])
  1484. . ' REMOVE PARTITIONING;'
  1485. )
  1486. );
  1487. $html_output .= '<div class="clearfloat" /><br />';
  1488. $html_output .= '<a href="sql.php'
  1489. . Url::getCommon($this_url_params) . '">'
  1490. . __('Remove partitioning') . '</a>';
  1491. $html_output .= '</fieldset>'
  1492. . '<fieldset class="tblFooters">'
  1493. . '<input type="hidden" name="submit_partition" value="1">'
  1494. . '<input type="submit" value="' . __('Go') . '" />'
  1495. . '</fieldset>'
  1496. . '</form>'
  1497. . '</div>';
  1498. return $html_output;
  1499. }
  1500. /**
  1501. * Get the HTML for Referential Integrity check
  1502. *
  1503. * @param array $foreign all Relations to foreign tables for a given table
  1504. * or optionally a given column in a table
  1505. * @param array $url_params array of url parameters
  1506. *
  1507. * @return string $html_output
  1508. */
  1509. public function getHtmlForReferentialIntegrityCheck(array $foreign, array $url_params)
  1510. {
  1511. $html_output = '<div>'
  1512. . '<fieldset>'
  1513. . '<legend>' . __('Check referential integrity:') . '</legend>';
  1514. $html_output .= '<ul>';
  1515. foreach ($foreign as $master => $arr) {
  1516. $join_query = 'SELECT '
  1517. . Util::backquote($GLOBALS['table']) . '.*'
  1518. . ' FROM ' . Util::backquote($GLOBALS['table'])
  1519. . ' LEFT JOIN '
  1520. . Util::backquote($arr['foreign_db'])
  1521. . '.'
  1522. . Util::backquote($arr['foreign_table']);
  1523. if ($arr['foreign_table'] == $GLOBALS['table']) {
  1524. $foreign_table = $GLOBALS['table'] . '1';
  1525. $join_query .= ' AS ' . Util::backquote($foreign_table);
  1526. } else {
  1527. $foreign_table = $arr['foreign_table'];
  1528. }
  1529. $join_query .= ' ON '
  1530. . Util::backquote($GLOBALS['table']) . '.'
  1531. . Util::backquote($master)
  1532. . ' = '
  1533. . Util::backquote($arr['foreign_db'])
  1534. . '.'
  1535. . Util::backquote($foreign_table) . '.'
  1536. . Util::backquote($arr['foreign_field'])
  1537. . ' WHERE '
  1538. . Util::backquote($arr['foreign_db'])
  1539. . '.'
  1540. . Util::backquote($foreign_table) . '.'
  1541. . Util::backquote($arr['foreign_field'])
  1542. . ' IS NULL AND '
  1543. . Util::backquote($GLOBALS['table']) . '.'
  1544. . Util::backquote($master)
  1545. . ' IS NOT NULL';
  1546. $this_url_params = array_merge(
  1547. $url_params,
  1548. array('sql_query' => $join_query)
  1549. );
  1550. $html_output .= '<li>'
  1551. . '<a href="sql.php'
  1552. . Url::getCommon($this_url_params)
  1553. . '">'
  1554. . $master . '&nbsp;->&nbsp;' . $arr['foreign_db'] . '.'
  1555. . $arr['foreign_table'] . '.' . $arr['foreign_field']
  1556. . '</a></li>' . "\n";
  1557. } // foreach $foreign
  1558. $html_output .= '</ul></fieldset></div>';
  1559. return $html_output;
  1560. }
  1561. /**
  1562. * Reorder table based on request params
  1563. *
  1564. * @return array SQL query and result
  1565. */
  1566. public function getQueryAndResultForReorderingTable()
  1567. {
  1568. $sql_query = 'ALTER TABLE '
  1569. . Util::backquote($GLOBALS['table'])
  1570. . ' ORDER BY '
  1571. . Util::backquote(urldecode($_REQUEST['order_field']));
  1572. if (isset($_REQUEST['order_order'])
  1573. && $_REQUEST['order_order'] === 'desc'
  1574. ) {
  1575. $sql_query .= ' DESC';
  1576. } else {
  1577. $sql_query .= ' ASC';
  1578. }
  1579. $sql_query .= ';';
  1580. $result = $GLOBALS['dbi']->query($sql_query);
  1581. return array($sql_query, $result);
  1582. }
  1583. /**
  1584. * Get table alters array
  1585. *
  1586. * @param Table $pma_table The Table object
  1587. * @param string $pack_keys pack keys
  1588. * @param string $checksum value of checksum
  1589. * @param string $page_checksum value of page checksum
  1590. * @param string $delay_key_write delay key write
  1591. * @param string $row_format row format
  1592. * @param string $newTblStorageEngine table storage engine
  1593. * @param string $transactional value of transactional
  1594. * @param string $tbl_collation collation of the table
  1595. *
  1596. * @return array $table_alters
  1597. */
  1598. public function getTableAltersArray($pma_table, $pack_keys,
  1599. $checksum, $page_checksum, $delay_key_write,
  1600. $row_format, $newTblStorageEngine, $transactional, $tbl_collation
  1601. ) {
  1602. global $auto_increment;
  1603. $table_alters = array();
  1604. if (isset($_REQUEST['comment'])
  1605. && urldecode($_REQUEST['prev_comment']) !== $_REQUEST['comment']
  1606. ) {
  1607. $table_alters[] = 'COMMENT = \''
  1608. . $GLOBALS['dbi']->escapeString($_REQUEST['comment']) . '\'';
  1609. }
  1610. if (! empty($newTblStorageEngine)
  1611. && mb_strtolower($newTblStorageEngine) !== mb_strtolower($GLOBALS['tbl_storage_engine'])
  1612. ) {
  1613. $table_alters[] = 'ENGINE = ' . $newTblStorageEngine;
  1614. }
  1615. if (! empty($_REQUEST['tbl_collation'])
  1616. && $_REQUEST['tbl_collation'] !== $tbl_collation
  1617. ) {
  1618. $table_alters[] = 'DEFAULT '
  1619. . Util::getCharsetQueryPart($_REQUEST['tbl_collation']);
  1620. }
  1621. if ($pma_table->isEngine(array('MYISAM', 'ARIA', 'ISAM'))
  1622. && isset($_REQUEST['new_pack_keys'])
  1623. && $_REQUEST['new_pack_keys'] != (string)$pack_keys
  1624. ) {
  1625. $table_alters[] = 'pack_keys = ' . $_REQUEST['new_pack_keys'];
  1626. }
  1627. $_REQUEST['new_checksum'] = empty($_REQUEST['new_checksum']) ? '0' : '1';
  1628. if ($pma_table->isEngine(array('MYISAM', 'ARIA'))
  1629. && $_REQUEST['new_checksum'] !== $checksum
  1630. ) {
  1631. $table_alters[] = 'checksum = ' . $_REQUEST['new_checksum'];
  1632. }
  1633. $_REQUEST['new_transactional']
  1634. = empty($_REQUEST['new_transactional']) ? '0' : '1';
  1635. if ($pma_table->isEngine('ARIA')
  1636. && $_REQUEST['new_transactional'] !== $transactional
  1637. ) {
  1638. $table_alters[] = 'TRANSACTIONAL = ' . $_REQUEST['new_transactional'];
  1639. }
  1640. $_REQUEST['new_page_checksum']
  1641. = empty($_REQUEST['new_page_checksum']) ? '0' : '1';
  1642. if ($pma_table->isEngine('ARIA')
  1643. && $_REQUEST['new_page_checksum'] !== $page_checksum
  1644. ) {
  1645. $table_alters[] = 'PAGE_CHECKSUM = ' . $_REQUEST['new_page_checksum'];
  1646. }
  1647. $_REQUEST['new_delay_key_write']
  1648. = empty($_REQUEST['new_delay_key_write']) ? '0' : '1';
  1649. if ($pma_table->isEngine(array('MYISAM', 'ARIA'))
  1650. && $_REQUEST['new_delay_key_write'] !== $delay_key_write
  1651. ) {
  1652. $table_alters[] = 'delay_key_write = ' . $_REQUEST['new_delay_key_write'];
  1653. }
  1654. if ($pma_table->isEngine(array('MYISAM', 'ARIA', 'INNODB', 'PBXT'))
  1655. && ! empty($_REQUEST['new_auto_increment'])
  1656. && (! isset($auto_increment)
  1657. || $_REQUEST['new_auto_increment'] !== $auto_increment)
  1658. ) {
  1659. $table_alters[] = 'auto_increment = '
  1660. . $GLOBALS['dbi']->escapeString($_REQUEST['new_auto_increment']);
  1661. }
  1662. if (! empty($_REQUEST['new_row_format'])) {
  1663. $newRowFormat = $_REQUEST['new_row_format'];
  1664. $newRowFormatLower = mb_strtolower($newRowFormat);
  1665. if ($pma_table->isEngine(array('MYISAM', 'ARIA', 'INNODB', 'PBXT'))
  1666. && (strlen($row_format) === 0
  1667. || $newRowFormatLower !== mb_strtolower($row_format))
  1668. ) {
  1669. $table_alters[] = 'ROW_FORMAT = '
  1670. . $GLOBALS['dbi']->escapeString($newRowFormat);
  1671. }
  1672. }
  1673. return $table_alters;
  1674. }
  1675. /**
  1676. * Get warning messages array
  1677. *
  1678. * @return array $warning_messages
  1679. */
  1680. public function getWarningMessagesArray()
  1681. {
  1682. $warning_messages = array();
  1683. foreach ($GLOBALS['dbi']->getWarnings() as $warning) {
  1684. // In MariaDB 5.1.44, when altering a table from Maria to MyISAM
  1685. // and if TRANSACTIONAL was set, the system reports an error;
  1686. // I discussed with a Maria developer and he agrees that this
  1687. // should not be reported with a Level of Error, so here
  1688. // I just ignore it. But there are other 1478 messages
  1689. // that it's better to show.
  1690. if (! (isset($_REQUEST['new_tbl_storage_engine'])
  1691. && $_REQUEST['new_tbl_storage_engine'] == 'MyISAM'
  1692. && $warning['Code'] == '1478'
  1693. && $warning['Level'] == 'Error')
  1694. ) {
  1695. $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
  1696. . ' ' . $warning['Message'];
  1697. }
  1698. }
  1699. return $warning_messages;
  1700. }
  1701. /**
  1702. * Get SQL query and result after ran this SQL query for a partition operation
  1703. * has been requested by the user
  1704. *
  1705. * @return array $sql_query, $result
  1706. */
  1707. public function getQueryAndResultForPartition()
  1708. {
  1709. $sql_query = 'ALTER TABLE '
  1710. . Util::backquote($GLOBALS['table']) . ' '
  1711. . $_REQUEST['partition_operation']
  1712. . ' PARTITION ';
  1713. if ($_REQUEST['partition_operation'] == 'COALESCE') {
  1714. $sql_query .= count($_REQUEST['partition_name']);
  1715. } else {
  1716. $sql_query .= implode(', ', $_REQUEST['partition_name']) . ';';
  1717. }
  1718. $result = $GLOBALS['dbi']->query($sql_query);
  1719. return array($sql_query, $result);
  1720. }
  1721. /**
  1722. * Adjust the privileges after renaming/moving a table
  1723. *
  1724. * @param string $oldDb Database name before table renaming/moving table
  1725. * @param string $oldTable Table name before table renaming/moving table
  1726. * @param string $newDb Database name after table renaming/ moving table
  1727. * @param string $newTable Table name after table renaming/moving table
  1728. *
  1729. * @return void
  1730. */
  1731. public function adjustPrivilegesRenameOrMoveTable($oldDb, $oldTable, $newDb, $newTable)
  1732. {
  1733. if ($GLOBALS['table_priv'] && $GLOBALS['col_priv']
  1734. && $GLOBALS['is_reload_priv']
  1735. ) {
  1736. $GLOBALS['dbi']->selectDb('mysql');
  1737. // For table specific privileges
  1738. $query_table_specific = 'UPDATE ' . Util::backquote('tables_priv')
  1739. . 'SET Db = \'' . $GLOBALS['dbi']->escapeString($newDb) . '\', Table_name = \'' . $GLOBALS['dbi']->escapeString($newTable)
  1740. . '\' where Db = \'' . $GLOBALS['dbi']->escapeString($oldDb) . '\' AND Table_name = \'' . $GLOBALS['dbi']->escapeString($oldTable)
  1741. . '\';';
  1742. $GLOBALS['dbi']->query($query_table_specific);
  1743. // For column specific privileges
  1744. $query_col_specific = 'UPDATE ' . Util::backquote('columns_priv')
  1745. . 'SET Db = \'' . $GLOBALS['dbi']->escapeString($newDb) . '\', Table_name = \'' . $GLOBALS['dbi']->escapeString($newTable)
  1746. . '\' where Db = \'' . $GLOBALS['dbi']->escapeString($oldDb) . '\' AND Table_name = \'' . $GLOBALS['dbi']->escapeString($oldTable)
  1747. . '\';';
  1748. $GLOBALS['dbi']->query($query_col_specific);
  1749. // Finally FLUSH the new privileges
  1750. $flush_query = "FLUSH PRIVILEGES;";
  1751. $GLOBALS['dbi']->query($flush_query);
  1752. }
  1753. }
  1754. /**
  1755. * Adjust the privileges after copying a table
  1756. *
  1757. * @param string $oldDb Database name before table copying
  1758. * @param string $oldTable Table name before table copying
  1759. * @param string $newDb Database name after table copying
  1760. * @param string $newTable Table name after table copying
  1761. *
  1762. * @return void
  1763. */
  1764. public function adjustPrivilegesCopyTable($oldDb, $oldTable, $newDb, $newTable)
  1765. {
  1766. if ($GLOBALS['table_priv'] && $GLOBALS['col_priv']
  1767. && $GLOBALS['is_reload_priv']
  1768. ) {
  1769. $GLOBALS['dbi']->selectDb('mysql');
  1770. // For Table Specific privileges
  1771. $query_table_specific_old = 'SELECT * FROM '
  1772. . Util::backquote('tables_priv') . ' where '
  1773. . 'Db = "' . $oldDb . '" AND Table_name = "' . $oldTable . '";';
  1774. $old_privs_table = $GLOBALS['dbi']->fetchResult(
  1775. $query_table_specific_old,
  1776. 0
  1777. );
  1778. foreach ($old_privs_table as $old_priv) {
  1779. $newDb_table_privs_query = 'INSERT INTO '
  1780. . Util::backquote('tables_priv') . ' VALUES("'
  1781. . $old_priv[0] . '", "' . $newDb . '", "' . $old_priv[2] . '", "'
  1782. . $newTable . '", "' . $old_priv[4] . '", "' . $old_priv[5]
  1783. . '", "' . $old_priv[6] . '", "' . $old_priv[7] . '");';
  1784. $GLOBALS['dbi']->query($newDb_table_privs_query);
  1785. }
  1786. // For Column Specific privileges
  1787. $query_col_specific_old = 'SELECT * FROM '
  1788. . Util::backquote('columns_priv') . ' WHERE '
  1789. . 'Db = "' . $oldDb . '" AND Table_name = "' . $oldTable . '";';
  1790. $old_privs_col = $GLOBALS['dbi']->fetchResult(
  1791. $query_col_specific_old,
  1792. 0
  1793. );
  1794. foreach ($old_privs_col as $old_priv) {
  1795. $newDb_col_privs_query = 'INSERT INTO '
  1796. . Util::backquote('columns_priv') . ' VALUES("'
  1797. . $old_priv[0] . '", "' . $newDb . '", "' . $old_priv[2] . '", "'
  1798. . $newTable . '", "' . $old_priv[4] . '", "' . $old_priv[5]
  1799. . '", "' . $old_priv[6] . '");';
  1800. $GLOBALS['dbi']->query($newDb_col_privs_query);
  1801. }
  1802. // Finally FLUSH the new privileges
  1803. $flush_query = "FLUSH PRIVILEGES;";
  1804. $GLOBALS['dbi']->query($flush_query);
  1805. }
  1806. }
  1807. /**
  1808. * Change all collations and character sets of all columns in table
  1809. *
  1810. * @param string $db Database name
  1811. * @param string $table Table name
  1812. * @param string $tbl_collation Collation Name
  1813. *
  1814. * @return void
  1815. */
  1816. public function changeAllColumnsCollation($db, $table, $tbl_collation)
  1817. {
  1818. $GLOBALS['dbi']->selectDb($db);
  1819. $change_all_collations_query = 'ALTER TABLE '
  1820. . Util::backquote($table)
  1821. . ' CONVERT TO';
  1822. list($charset) = explode('_', $tbl_collation);
  1823. $change_all_collations_query .= ' CHARACTER SET ' . $charset
  1824. . ($charset == $tbl_collation ? '' : ' COLLATE ' . $tbl_collation);
  1825. $GLOBALS['dbi']->query($change_all_collations_query);
  1826. }
  1827. /**
  1828. * Move or copy a table
  1829. *
  1830. * @param string $db current database name
  1831. * @param string $table current table name
  1832. *
  1833. * @return void
  1834. */
  1835. public function moveOrCopyTable($db, $table)
  1836. {
  1837. /**
  1838. * Selects the database to work with
  1839. */
  1840. $GLOBALS['dbi']->selectDb($db);
  1841. /**
  1842. * $_REQUEST['target_db'] could be empty in case we came from an input field
  1843. * (when there are many databases, no drop-down)
  1844. */
  1845. if (empty($_REQUEST['target_db'])) {
  1846. $_REQUEST['target_db'] = $db;
  1847. }
  1848. /**
  1849. * A target table name has been sent to this script -> do the work
  1850. */
  1851. if (Core::isValid($_REQUEST['new_name'])) {
  1852. if ($db == $_REQUEST['target_db'] && $table == $_REQUEST['new_name']) {
  1853. if (isset($_REQUEST['submit_move'])) {
  1854. $message = Message::error(__('Can\'t move table to same one!'));
  1855. } else {
  1856. $message = Message::error(__('Can\'t copy table to same one!'));
  1857. }
  1858. } else {
  1859. Table::moveCopy(
  1860. $db, $table, $_REQUEST['target_db'], $_REQUEST['new_name'],
  1861. $_REQUEST['what'], isset($_REQUEST['submit_move']), 'one_table'
  1862. );
  1863. if (isset($_REQUEST['adjust_privileges'])
  1864. && ! empty($_REQUEST['adjust_privileges'])
  1865. ) {
  1866. if (isset($_REQUEST['submit_move'])) {
  1867. $this->adjustPrivilegesRenameOrMoveTable(
  1868. $db, $table, $_REQUEST['target_db'], $_REQUEST['new_name']
  1869. );
  1870. } else {
  1871. $this->adjustPrivilegesCopyTable(
  1872. $db, $table, $_REQUEST['target_db'], $_REQUEST['new_name']
  1873. );
  1874. }
  1875. if (isset($_REQUEST['submit_move'])) {
  1876. $message = Message::success(
  1877. __(
  1878. 'Table %s has been moved to %s. Privileges have been '
  1879. . 'adjusted.'
  1880. )
  1881. );
  1882. } else {
  1883. $message = Message::success(
  1884. __(
  1885. 'Table %s has been copied to %s. Privileges have been '
  1886. . 'adjusted.'
  1887. )
  1888. );
  1889. }
  1890. } else {
  1891. if (isset($_REQUEST['submit_move'])) {
  1892. $message = Message::success(
  1893. __('Table %s has been moved to %s.')
  1894. );
  1895. } else {
  1896. $message = Message::success(
  1897. __('Table %s has been copied to %s.')
  1898. );
  1899. }
  1900. }
  1901. $old = Util::backquote($db) . '.'
  1902. . Util::backquote($table);
  1903. $message->addParam($old);
  1904. $new_name = $_REQUEST['new_name'];
  1905. if ($GLOBALS['dbi']->getLowerCaseNames() === '1') {
  1906. $new_name = strtolower($new_name);
  1907. }
  1908. $new = Util::backquote($_REQUEST['target_db']) . '.'
  1909. . Util::backquote($new_name);
  1910. $message->addParam($new);
  1911. /* Check: Work on new table or on old table? */
  1912. if (isset($_REQUEST['submit_move'])
  1913. || Core::isValid($_REQUEST['switch_to_new'])
  1914. ) {
  1915. }
  1916. }
  1917. } else {
  1918. /**
  1919. * No new name for the table!
  1920. */
  1921. $message = Message::error(__('The table name is empty!'));
  1922. }
  1923. $response = Response::getInstance();
  1924. if ($response->isAjax()) {
  1925. $response->addJSON('message', $message);
  1926. if ($message->isSuccess()) {
  1927. $response->addJSON('db', $GLOBALS['db']);
  1928. } else {
  1929. $response->setRequestStatus(false);
  1930. }
  1931. exit;
  1932. }
  1933. }
  1934. }