Operations.php 79 KB

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