PageRenderTime 56ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/classes/Plugins/Export/ExportSql.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 2859 lines | 1995 code | 362 blank | 502 comment | 308 complexity | a75ad2d2fbb396e6354cbeb6aab6cde7 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Set of functions used to build SQL dumps of tables
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Export;
  7. use PhpMyAdmin\Charsets;
  8. use PhpMyAdmin\DatabaseInterface;
  9. use PhpMyAdmin\FieldMetadata;
  10. use PhpMyAdmin\Plugins\ExportPlugin;
  11. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  12. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  13. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertySubgroup;
  14. use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
  15. use PhpMyAdmin\Properties\Options\Items\MessageOnlyPropertyItem;
  16. use PhpMyAdmin\Properties\Options\Items\NumberPropertyItem;
  17. use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem;
  18. use PhpMyAdmin\Properties\Options\Items\SelectPropertyItem;
  19. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  20. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  21. use PhpMyAdmin\SqlParser\Components\CreateDefinition;
  22. use PhpMyAdmin\SqlParser\Context;
  23. use PhpMyAdmin\SqlParser\Parser;
  24. use PhpMyAdmin\SqlParser\Statements\CreateStatement;
  25. use PhpMyAdmin\SqlParser\Token;
  26. use PhpMyAdmin\Util;
  27. use PhpMyAdmin\Version;
  28. use function __;
  29. use function bin2hex;
  30. use function count;
  31. use function defined;
  32. use function explode;
  33. use function implode;
  34. use function in_array;
  35. use function intval;
  36. use function is_array;
  37. use function mb_strlen;
  38. use function mb_strpos;
  39. use function mb_substr;
  40. use function preg_quote;
  41. use function preg_replace;
  42. use function preg_split;
  43. use function sprintf;
  44. use function str_repeat;
  45. use function str_replace;
  46. use function strtotime;
  47. use function strtoupper;
  48. use function trigger_error;
  49. use const E_USER_ERROR;
  50. use const PHP_VERSION;
  51. /**
  52. * Handles the export for the SQL class
  53. */
  54. class ExportSql extends ExportPlugin
  55. {
  56. /**
  57. * Whether charset header was sent.
  58. *
  59. * @var bool
  60. */
  61. private $sentCharset = false;
  62. protected function init(): void
  63. {
  64. // Avoids undefined variables, use NULL so isset() returns false
  65. if (isset($GLOBALS['sql_backquotes'])) {
  66. return;
  67. }
  68. $GLOBALS['sql_backquotes'] = null;
  69. }
  70. /**
  71. * @psalm-return non-empty-lowercase-string
  72. */
  73. public function getName(): string
  74. {
  75. return 'sql';
  76. }
  77. protected function setProperties(): ExportPluginProperties
  78. {
  79. global $plugin_param, $dbi;
  80. $hideSql = false;
  81. $hideStructure = false;
  82. if ($plugin_param['export_type'] === 'table' && ! $plugin_param['single_table']) {
  83. $hideStructure = true;
  84. $hideSql = true;
  85. }
  86. // In case we have `raw_query` parameter set,
  87. // we initialize SQL option
  88. if (isset($_REQUEST['raw_query'])) {
  89. $hideStructure = false;
  90. $hideSql = false;
  91. }
  92. $exportPluginProperties = new ExportPluginProperties();
  93. $exportPluginProperties->setText('SQL');
  94. $exportPluginProperties->setExtension('sql');
  95. $exportPluginProperties->setMimeType('text/x-sql');
  96. if ($hideSql) {
  97. return $exportPluginProperties;
  98. }
  99. $exportPluginProperties->setOptionsText(__('Options'));
  100. // create the root group that will be the options field for
  101. // $exportPluginProperties
  102. // this will be shown as "Format specific options"
  103. $exportSpecificOptions = new OptionsPropertyRootGroup('Format Specific Options');
  104. // general options main group
  105. $generalOptions = new OptionsPropertyMainGroup('general_opts');
  106. // comments
  107. $subgroup = new OptionsPropertySubgroup('include_comments');
  108. $leaf = new BoolPropertyItem(
  109. 'include_comments',
  110. __(
  111. 'Display comments <i>(includes info such as export timestamp, PHP version, and server version)</i>'
  112. )
  113. );
  114. $subgroup->setSubgroupHeader($leaf);
  115. $leaf = new TextPropertyItem(
  116. 'header_comment',
  117. __('Additional custom header comment (\n splits lines):')
  118. );
  119. $subgroup->addProperty($leaf);
  120. $leaf = new BoolPropertyItem(
  121. 'dates',
  122. __(
  123. 'Include a timestamp of when databases were created, last updated, and last checked'
  124. )
  125. );
  126. $subgroup->addProperty($leaf);
  127. if (! empty($GLOBALS['cfgRelation']['relation'])) {
  128. $leaf = new BoolPropertyItem(
  129. 'relation',
  130. __('Display foreign key relationships')
  131. );
  132. $subgroup->addProperty($leaf);
  133. }
  134. if (! empty($GLOBALS['cfgRelation']['mimework'])) {
  135. $leaf = new BoolPropertyItem(
  136. 'mime',
  137. __('Display media types')
  138. );
  139. $subgroup->addProperty($leaf);
  140. }
  141. $generalOptions->addProperty($subgroup);
  142. // enclose in a transaction
  143. $leaf = new BoolPropertyItem(
  144. 'use_transaction',
  145. __('Enclose export in a transaction')
  146. );
  147. $leaf->setDoc(
  148. [
  149. 'programs',
  150. 'mysqldump',
  151. 'option_mysqldump_single-transaction',
  152. ]
  153. );
  154. $generalOptions->addProperty($leaf);
  155. // disable foreign key checks
  156. $leaf = new BoolPropertyItem(
  157. 'disable_fk',
  158. __('Disable foreign key checks')
  159. );
  160. $leaf->setDoc(
  161. [
  162. 'manual_MySQL_Database_Administration',
  163. 'server-system-variables',
  164. 'sysvar_foreign_key_checks',
  165. ]
  166. );
  167. $generalOptions->addProperty($leaf);
  168. // export views as tables
  169. $leaf = new BoolPropertyItem(
  170. 'views_as_tables',
  171. __('Export views as tables')
  172. );
  173. $generalOptions->addProperty($leaf);
  174. // export metadata
  175. $leaf = new BoolPropertyItem(
  176. 'metadata',
  177. __('Export metadata')
  178. );
  179. $generalOptions->addProperty($leaf);
  180. // compatibility maximization
  181. $compats = $dbi->getCompatibilities();
  182. if (count($compats) > 0) {
  183. $values = [];
  184. foreach ($compats as $val) {
  185. $values[$val] = $val;
  186. }
  187. $leaf = new SelectPropertyItem(
  188. 'compatibility',
  189. __(
  190. 'Database system or older MySQL server to maximize output compatibility with:'
  191. )
  192. );
  193. $leaf->setValues($values);
  194. $leaf->setDoc(
  195. [
  196. 'manual_MySQL_Database_Administration',
  197. 'Server_SQL_mode',
  198. ]
  199. );
  200. $generalOptions->addProperty($leaf);
  201. unset($values);
  202. }
  203. // what to dump (structure/data/both)
  204. $subgroup = new OptionsPropertySubgroup(
  205. 'dump_table',
  206. __('Dump table')
  207. );
  208. $leaf = new RadioPropertyItem('structure_or_data');
  209. $leaf->setValues(
  210. [
  211. 'structure' => __('structure'),
  212. 'data' => __('data'),
  213. 'structure_and_data' => __('structure and data'),
  214. ]
  215. );
  216. $subgroup->setSubgroupHeader($leaf);
  217. $generalOptions->addProperty($subgroup);
  218. // add the main group to the root group
  219. $exportSpecificOptions->addProperty($generalOptions);
  220. // structure options main group
  221. if (! $hideStructure) {
  222. $structureOptions = new OptionsPropertyMainGroup(
  223. 'structure',
  224. __('Object creation options')
  225. );
  226. $structureOptions->setForce('data');
  227. // begin SQL Statements
  228. $subgroup = new OptionsPropertySubgroup();
  229. $leaf = new MessageOnlyPropertyItem(
  230. 'add_statements',
  231. __('Add statements:')
  232. );
  233. $subgroup->setSubgroupHeader($leaf);
  234. // server export options
  235. if ($plugin_param['export_type'] === 'server') {
  236. $leaf = new BoolPropertyItem(
  237. 'drop_database',
  238. sprintf(__('Add %s statement'), '<code>DROP DATABASE IF EXISTS</code>')
  239. );
  240. $subgroup->addProperty($leaf);
  241. }
  242. if ($plugin_param['export_type'] === 'database') {
  243. $createClause = '<code>CREATE DATABASE / USE</code>';
  244. $leaf = new BoolPropertyItem(
  245. 'create_database',
  246. sprintf(__('Add %s statement'), $createClause)
  247. );
  248. $subgroup->addProperty($leaf);
  249. }
  250. if ($plugin_param['export_type'] === 'table') {
  251. $dropClause = $dbi->getTable($GLOBALS['db'], $GLOBALS['table'])->isView()
  252. ? '<code>DROP VIEW</code>'
  253. : '<code>DROP TABLE</code>';
  254. } else {
  255. $dropClause = '<code>DROP TABLE / VIEW / PROCEDURE / FUNCTION / EVENT</code>';
  256. }
  257. $dropClause .= '<code> / TRIGGER</code>';
  258. $leaf = new BoolPropertyItem(
  259. 'drop_table',
  260. sprintf(__('Add %s statement'), $dropClause)
  261. );
  262. $subgroup->addProperty($leaf);
  263. $subgroupCreateTable = new OptionsPropertySubgroup();
  264. // Add table structure option
  265. $leaf = new BoolPropertyItem(
  266. 'create_table',
  267. sprintf(__('Add %s statement'), '<code>CREATE TABLE</code>')
  268. );
  269. $subgroupCreateTable->setSubgroupHeader($leaf);
  270. $leaf = new BoolPropertyItem(
  271. 'if_not_exists',
  272. '<code>IF NOT EXISTS</code> ' . __(
  273. '(less efficient as indexes will be generated during table creation)'
  274. )
  275. );
  276. $subgroupCreateTable->addProperty($leaf);
  277. $leaf = new BoolPropertyItem(
  278. 'auto_increment',
  279. sprintf(__('%s value'), '<code>AUTO_INCREMENT</code>')
  280. );
  281. $subgroupCreateTable->addProperty($leaf);
  282. $subgroup->addProperty($subgroupCreateTable);
  283. // Add view option
  284. $subgroupCreateView = new OptionsPropertySubgroup();
  285. $leaf = new BoolPropertyItem(
  286. 'create_view',
  287. sprintf(__('Add %s statement'), '<code>CREATE VIEW</code>')
  288. );
  289. $subgroupCreateView->setSubgroupHeader($leaf);
  290. $leaf = new BoolPropertyItem(
  291. 'simple_view_export',
  292. /* l10n: Allow simplifying exported view syntax to only "CREATE VIEW" */
  293. __('Use simple view export')
  294. );
  295. $subgroupCreateView->addProperty($leaf);
  296. $leaf = new BoolPropertyItem(
  297. 'view_current_user',
  298. __('Exclude definition of current user')
  299. );
  300. $subgroupCreateView->addProperty($leaf);
  301. $leaf = new BoolPropertyItem(
  302. 'or_replace_view',
  303. sprintf(__('%s view'), '<code>OR REPLACE</code>')
  304. );
  305. $subgroupCreateView->addProperty($leaf);
  306. $subgroup->addProperty($subgroupCreateView);
  307. $leaf = new BoolPropertyItem(
  308. 'procedure_function',
  309. sprintf(
  310. __('Add %s statement'),
  311. '<code>CREATE PROCEDURE / FUNCTION / EVENT</code>'
  312. )
  313. );
  314. $subgroup->addProperty($leaf);
  315. // Add triggers option
  316. $leaf = new BoolPropertyItem(
  317. 'create_trigger',
  318. sprintf(__('Add %s statement'), '<code>CREATE TRIGGER</code>')
  319. );
  320. $subgroup->addProperty($leaf);
  321. $structureOptions->addProperty($subgroup);
  322. $leaf = new BoolPropertyItem(
  323. 'backquotes',
  324. __(
  325. 'Enclose table and column names with backquotes '
  326. . '<i>(Protects column and table names formed with'
  327. . ' special characters or keywords)</i>'
  328. )
  329. );
  330. $structureOptions->addProperty($leaf);
  331. // add the main group to the root group
  332. $exportSpecificOptions->addProperty($structureOptions);
  333. }
  334. // begin Data options
  335. $dataOptions = new OptionsPropertyMainGroup(
  336. 'data',
  337. __('Data creation options')
  338. );
  339. $dataOptions->setForce('structure');
  340. $leaf = new BoolPropertyItem(
  341. 'truncate',
  342. __('Truncate table before insert')
  343. );
  344. $dataOptions->addProperty($leaf);
  345. // begin SQL Statements
  346. $subgroup = new OptionsPropertySubgroup();
  347. $leaf = new MessageOnlyPropertyItem(
  348. __('Instead of <code>INSERT</code> statements, use:')
  349. );
  350. $subgroup->setSubgroupHeader($leaf);
  351. $leaf = new BoolPropertyItem(
  352. 'delayed',
  353. __('<code>INSERT DELAYED</code> statements')
  354. );
  355. $leaf->setDoc(
  356. [
  357. 'manual_MySQL_Database_Administration',
  358. 'insert_delayed',
  359. ]
  360. );
  361. $subgroup->addProperty($leaf);
  362. $leaf = new BoolPropertyItem(
  363. 'ignore',
  364. __('<code>INSERT IGNORE</code> statements')
  365. );
  366. $leaf->setDoc(
  367. [
  368. 'manual_MySQL_Database_Administration',
  369. 'insert',
  370. ]
  371. );
  372. $subgroup->addProperty($leaf);
  373. $dataOptions->addProperty($subgroup);
  374. // Function to use when dumping dat
  375. $leaf = new SelectPropertyItem(
  376. 'type',
  377. __('Function to use when dumping data:')
  378. );
  379. $leaf->setValues(
  380. [
  381. 'INSERT' => 'INSERT',
  382. 'UPDATE' => 'UPDATE',
  383. 'REPLACE' => 'REPLACE',
  384. ]
  385. );
  386. $dataOptions->addProperty($leaf);
  387. /* Syntax to use when inserting data */
  388. $subgroup = new OptionsPropertySubgroup();
  389. $leaf = new MessageOnlyPropertyItem(
  390. null,
  391. __('Syntax to use when inserting data:')
  392. );
  393. $subgroup->setSubgroupHeader($leaf);
  394. $leaf = new RadioPropertyItem(
  395. 'insert_syntax',
  396. __('<code>INSERT IGNORE</code> statements')
  397. );
  398. $leaf->setValues(
  399. [
  400. 'complete' => __(
  401. 'include column names in every <code>INSERT</code> statement'
  402. . ' <br> &nbsp; &nbsp; &nbsp; Example: <code>INSERT INTO'
  403. . ' tbl_name (col_A,col_B,col_C) VALUES (1,2,3)</code>'
  404. ),
  405. 'extended' => __(
  406. 'insert multiple rows in every <code>INSERT</code> statement'
  407. . '<br> &nbsp; &nbsp; &nbsp; Example: <code>INSERT INTO'
  408. . ' tbl_name VALUES (1,2,3), (4,5,6), (7,8,9)</code>'
  409. ),
  410. 'both' => __(
  411. 'both of the above<br> &nbsp; &nbsp; &nbsp; Example:'
  412. . ' <code>INSERT INTO tbl_name (col_A,col_B,col_C) VALUES'
  413. . ' (1,2,3), (4,5,6), (7,8,9)</code>'
  414. ),
  415. 'none' => __(
  416. 'neither of the above<br> &nbsp; &nbsp; &nbsp; Example:'
  417. . ' <code>INSERT INTO tbl_name VALUES (1,2,3)</code>'
  418. ),
  419. ]
  420. );
  421. $subgroup->addProperty($leaf);
  422. $dataOptions->addProperty($subgroup);
  423. // Max length of query
  424. $leaf = new NumberPropertyItem(
  425. 'max_query_size',
  426. __('Maximal length of created query')
  427. );
  428. $dataOptions->addProperty($leaf);
  429. // Dump binary columns in hexadecimal
  430. $leaf = new BoolPropertyItem(
  431. 'hex_for_binary',
  432. __(
  433. 'Dump binary columns in hexadecimal notation <i>(for example, "abc" becomes 0x616263)</i>'
  434. )
  435. );
  436. $dataOptions->addProperty($leaf);
  437. // Dump time in UTC
  438. $leaf = new BoolPropertyItem(
  439. 'utc_time',
  440. __(
  441. 'Dump TIMESTAMP columns in UTC <i>(enables TIMESTAMP columns'
  442. . ' to be dumped and reloaded between servers in different'
  443. . ' time zones)</i>'
  444. )
  445. );
  446. $dataOptions->addProperty($leaf);
  447. // add the main group to the root group
  448. $exportSpecificOptions->addProperty($dataOptions);
  449. // set the options for the export plugin property item
  450. $exportPluginProperties->setOptions($exportSpecificOptions);
  451. return $exportPluginProperties;
  452. }
  453. /**
  454. * Generates SQL for routines export
  455. *
  456. * @param string $db Database
  457. * @param array $aliases Aliases of db/table/columns
  458. * @param string $type Type of exported routine
  459. * @param string $name Verbose name of exported routine
  460. * @param array $routines List of routines to export
  461. * @param string $delimiter Delimiter to use in SQL
  462. *
  463. * @return string SQL query
  464. */
  465. protected function exportRoutineSQL(
  466. $db,
  467. array $aliases,
  468. $type,
  469. $name,
  470. array $routines,
  471. $delimiter
  472. ) {
  473. global $crlf, $dbi;
  474. $text = $this->exportComment()
  475. . $this->exportComment($name)
  476. . $this->exportComment();
  477. $usedAlias = false;
  478. $procQuery = '';
  479. foreach ($routines as $routine) {
  480. if (! empty($GLOBALS['sql_drop_table'])) {
  481. $procQuery .= 'DROP ' . $type . ' IF EXISTS '
  482. . Util::backquote($routine)
  483. . $delimiter . $crlf;
  484. }
  485. $createQuery = $this->replaceWithAliases(
  486. $dbi->getDefinition($db, $type, $routine),
  487. $aliases,
  488. $db,
  489. '',
  490. $flag
  491. );
  492. // One warning per database
  493. if ($flag) {
  494. $usedAlias = true;
  495. }
  496. $procQuery .= $createQuery . $delimiter . $crlf . $crlf;
  497. }
  498. if ($usedAlias) {
  499. $text .= $this->exportComment(
  500. __('It appears your database uses routines;')
  501. )
  502. . $this->exportComment(
  503. __('alias export may not work reliably in all cases.')
  504. )
  505. . $this->exportComment();
  506. }
  507. $text .= $procQuery;
  508. return $text;
  509. }
  510. /**
  511. * Exports routines (procedures and functions)
  512. *
  513. * @param string $db Database
  514. * @param array $aliases Aliases of db/table/columns
  515. */
  516. public function exportRoutines($db, array $aliases = []): bool
  517. {
  518. global $crlf, $dbi;
  519. $dbAlias = $db;
  520. $this->initAlias($aliases, $dbAlias);
  521. $text = '';
  522. $delimiter = '$$';
  523. $procedureNames = $dbi->getProceduresOrFunctions($db, 'PROCEDURE');
  524. $functionNames = $dbi->getProceduresOrFunctions($db, 'FUNCTION');
  525. if ($procedureNames || $functionNames) {
  526. $text .= $crlf
  527. . 'DELIMITER ' . $delimiter . $crlf;
  528. if ($procedureNames) {
  529. $text .= $this->exportRoutineSQL(
  530. $db,
  531. $aliases,
  532. 'PROCEDURE',
  533. __('Procedures'),
  534. $procedureNames,
  535. $delimiter
  536. );
  537. }
  538. if ($functionNames) {
  539. $text .= $this->exportRoutineSQL(
  540. $db,
  541. $aliases,
  542. 'FUNCTION',
  543. __('Functions'),
  544. $functionNames,
  545. $delimiter
  546. );
  547. }
  548. $text .= 'DELIMITER ;' . $crlf;
  549. }
  550. if (! empty($text)) {
  551. return $this->export->outputHandler($text);
  552. }
  553. return false;
  554. }
  555. /**
  556. * Possibly outputs comment
  557. *
  558. * @param string $text Text of comment
  559. *
  560. * @return string The formatted comment
  561. */
  562. private function exportComment($text = '')
  563. {
  564. if (isset($GLOBALS['sql_include_comments']) && $GLOBALS['sql_include_comments']) {
  565. // see https://dev.mysql.com/doc/refman/5.0/en/ansi-diff-comments.html
  566. if (empty($text)) {
  567. return '--' . $GLOBALS['crlf'];
  568. }
  569. $lines = preg_split("/\\r\\n|\\r|\\n/", $text);
  570. if ($lines === false) {
  571. return '--' . $GLOBALS['crlf'];
  572. }
  573. $result = [];
  574. foreach ($lines as $line) {
  575. $result[] = '-- ' . $line . $GLOBALS['crlf'];
  576. }
  577. return implode('', $result);
  578. }
  579. return '';
  580. }
  581. /**
  582. * Possibly outputs CRLF
  583. *
  584. * @return string crlf or nothing
  585. */
  586. private function possibleCRLF()
  587. {
  588. if (isset($GLOBALS['sql_include_comments']) && $GLOBALS['sql_include_comments']) {
  589. return $GLOBALS['crlf'];
  590. }
  591. return '';
  592. }
  593. /**
  594. * Outputs export footer
  595. */
  596. public function exportFooter(): bool
  597. {
  598. global $crlf, $dbi;
  599. $foot = '';
  600. if (isset($GLOBALS['sql_disable_fk'])) {
  601. $foot .= 'SET FOREIGN_KEY_CHECKS=1;' . $crlf;
  602. }
  603. if (isset($GLOBALS['sql_use_transaction'])) {
  604. $foot .= 'COMMIT;' . $crlf;
  605. }
  606. // restore connection settings
  607. if ($this->sentCharset) {
  608. $foot .= $crlf
  609. . '/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;'
  610. . $crlf
  611. . '/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;'
  612. . $crlf
  613. . '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;'
  614. . $crlf;
  615. $this->sentCharset = false;
  616. }
  617. /* Restore timezone */
  618. if (isset($GLOBALS['sql_utc_time']) && $GLOBALS['sql_utc_time']) {
  619. $dbi->query('SET time_zone = "' . $GLOBALS['old_tz'] . '"');
  620. }
  621. return $this->export->outputHandler($foot);
  622. }
  623. /**
  624. * Outputs export header. It is the first method to be called, so all
  625. * the required variables are initialized here.
  626. */
  627. public function exportHeader(): bool
  628. {
  629. global $crlf, $cfg, $dbi;
  630. if (isset($GLOBALS['sql_compatibility'])) {
  631. $tmpCompat = $GLOBALS['sql_compatibility'];
  632. if ($tmpCompat === 'NONE') {
  633. $tmpCompat = '';
  634. }
  635. $dbi->tryQuery('SET SQL_MODE="' . $tmpCompat . '"');
  636. unset($tmpCompat);
  637. }
  638. $head = $this->exportComment('phpMyAdmin SQL Dump')
  639. . $this->exportComment('version ' . Version::VERSION)
  640. . $this->exportComment('https://www.phpmyadmin.net/')
  641. . $this->exportComment();
  642. $hostString = __('Host:') . ' ' . $cfg['Server']['host'];
  643. if (! empty($cfg['Server']['port'])) {
  644. $hostString .= ':' . $cfg['Server']['port'];
  645. }
  646. $head .= $this->exportComment($hostString);
  647. $head .= $this->exportComment(
  648. __('Generation Time:') . ' '
  649. . Util::localisedDate()
  650. )
  651. . $this->exportComment(
  652. __('Server version:') . ' ' . $dbi->getVersionString()
  653. )
  654. . $this->exportComment(__('PHP Version:') . ' ' . PHP_VERSION)
  655. . $this->possibleCRLF();
  656. if (isset($GLOBALS['sql_header_comment']) && ! empty($GLOBALS['sql_header_comment'])) {
  657. // '\n' is not a newline (like "\n" would be), it's the characters
  658. // backslash and n, as explained on the export interface
  659. $lines = explode('\n', $GLOBALS['sql_header_comment']);
  660. $head .= $this->exportComment();
  661. foreach ($lines as $oneLine) {
  662. $head .= $this->exportComment($oneLine);
  663. }
  664. $head .= $this->exportComment();
  665. }
  666. if (isset($GLOBALS['sql_disable_fk'])) {
  667. $head .= 'SET FOREIGN_KEY_CHECKS=0;' . $crlf;
  668. }
  669. // We want exported AUTO_INCREMENT columns to have still same value,
  670. // do this only for recent MySQL exports
  671. if (! isset($GLOBALS['sql_compatibility']) || $GLOBALS['sql_compatibility'] === 'NONE') {
  672. $head .= 'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";' . $crlf;
  673. }
  674. if (isset($GLOBALS['sql_use_transaction'])) {
  675. $head .= 'START TRANSACTION;' . $crlf;
  676. }
  677. /* Change timezone if we should export timestamps in UTC */
  678. if (isset($GLOBALS['sql_utc_time']) && $GLOBALS['sql_utc_time']) {
  679. $head .= 'SET time_zone = "+00:00";' . $crlf;
  680. $GLOBALS['old_tz'] = $dbi
  681. ->fetchValue('SELECT @@session.time_zone');
  682. $dbi->query('SET time_zone = "+00:00"');
  683. }
  684. $head .= $this->possibleCRLF();
  685. if (! empty($GLOBALS['asfile'])) {
  686. // we are saving as file, therefore we provide charset information
  687. // so that a utility like the mysql client can interpret
  688. // the file correctly
  689. if (isset($GLOBALS['charset'], Charsets::$mysqlCharsetMap[$GLOBALS['charset']])) {
  690. // we got a charset from the export dialog
  691. $setNames = Charsets::$mysqlCharsetMap[$GLOBALS['charset']];
  692. } else {
  693. // by default we use the connection charset
  694. $setNames = Charsets::$mysqlCharsetMap['utf-8'];
  695. }
  696. if ($setNames === 'utf8' && $dbi->getVersion() > 50503) {
  697. $setNames = 'utf8mb4';
  698. }
  699. $head .= $crlf
  700. . '/*!40101 SET @OLD_CHARACTER_SET_CLIENT='
  701. . '@@CHARACTER_SET_CLIENT */;' . $crlf
  702. . '/*!40101 SET @OLD_CHARACTER_SET_RESULTS='
  703. . '@@CHARACTER_SET_RESULTS */;' . $crlf
  704. . '/*!40101 SET @OLD_COLLATION_CONNECTION='
  705. . '@@COLLATION_CONNECTION */;' . $crlf
  706. . '/*!40101 SET NAMES ' . $setNames . ' */;' . $crlf . $crlf;
  707. $this->sentCharset = true;
  708. }
  709. return $this->export->outputHandler($head);
  710. }
  711. /**
  712. * Outputs CREATE DATABASE statement
  713. *
  714. * @param string $db Database name
  715. * @param string $exportType 'server', 'database', 'table'
  716. * @param string $dbAlias Aliases of db
  717. */
  718. public function exportDBCreate($db, $exportType, $dbAlias = ''): bool
  719. {
  720. global $crlf, $dbi;
  721. if (empty($dbAlias)) {
  722. $dbAlias = $db;
  723. }
  724. if (isset($GLOBALS['sql_compatibility'])) {
  725. $compat = $GLOBALS['sql_compatibility'];
  726. } else {
  727. $compat = 'NONE';
  728. }
  729. if (isset($GLOBALS['sql_drop_database'])) {
  730. if (
  731. ! $this->export->outputHandler(
  732. 'DROP DATABASE IF EXISTS '
  733. . Util::backquoteCompat(
  734. $dbAlias,
  735. $compat,
  736. isset($GLOBALS['sql_backquotes'])
  737. )
  738. . ';' . $crlf
  739. )
  740. ) {
  741. return false;
  742. }
  743. }
  744. if ($exportType === 'database' && ! isset($GLOBALS['sql_create_database'])) {
  745. return true;
  746. }
  747. $createQuery = 'CREATE DATABASE IF NOT EXISTS '
  748. . Util::backquoteCompat($dbAlias, $compat, isset($GLOBALS['sql_backquotes']));
  749. $collation = $dbi->getDbCollation($db);
  750. if (mb_strpos($collation, '_')) {
  751. $createQuery .= ' DEFAULT CHARACTER SET '
  752. . mb_substr(
  753. $collation,
  754. 0,
  755. (int) mb_strpos($collation, '_')
  756. )
  757. . ' COLLATE ' . $collation;
  758. } else {
  759. $createQuery .= ' DEFAULT CHARACTER SET ' . $collation;
  760. }
  761. $createQuery .= ';' . $crlf;
  762. if (! $this->export->outputHandler($createQuery)) {
  763. return false;
  764. }
  765. return $this->exportUseStatement($dbAlias, $compat);
  766. }
  767. /**
  768. * Outputs USE statement
  769. *
  770. * @param string $db db to use
  771. * @param string $compat sql compatibility
  772. */
  773. private function exportUseStatement($db, $compat): bool
  774. {
  775. global $crlf;
  776. if (isset($GLOBALS['sql_compatibility']) && $GLOBALS['sql_compatibility'] === 'NONE') {
  777. $result = $this->export->outputHandler(
  778. 'USE '
  779. . Util::backquoteCompat(
  780. $db,
  781. $compat,
  782. isset($GLOBALS['sql_backquotes'])
  783. )
  784. . ';' . $crlf
  785. );
  786. } else {
  787. $result = $this->export->outputHandler('USE ' . $db . ';' . $crlf);
  788. }
  789. return $result;
  790. }
  791. /**
  792. * Outputs database header
  793. *
  794. * @param string $db Database name
  795. * @param string $dbAlias Alias of db
  796. */
  797. public function exportDBHeader($db, $dbAlias = ''): bool
  798. {
  799. if (empty($dbAlias)) {
  800. $dbAlias = $db;
  801. }
  802. if (isset($GLOBALS['sql_compatibility'])) {
  803. $compat = $GLOBALS['sql_compatibility'];
  804. } else {
  805. $compat = 'NONE';
  806. }
  807. $head = $this->exportComment()
  808. . $this->exportComment(
  809. __('Database:') . ' '
  810. . Util::backquoteCompat(
  811. $dbAlias,
  812. $compat,
  813. isset($GLOBALS['sql_backquotes'])
  814. )
  815. )
  816. . $this->exportComment();
  817. return $this->export->outputHandler($head);
  818. }
  819. /**
  820. * Outputs database footer
  821. *
  822. * @param string $db Database name
  823. */
  824. public function exportDBFooter($db): bool
  825. {
  826. global $crlf;
  827. $result = true;
  828. //add indexes to the sql dump file
  829. if (isset($GLOBALS['sql_indexes'])) {
  830. $result = $this->export->outputHandler($GLOBALS['sql_indexes']);
  831. unset($GLOBALS['sql_indexes']);
  832. }
  833. //add auto increments to the sql dump file
  834. if (isset($GLOBALS['sql_auto_increments'])) {
  835. $result = $this->export->outputHandler($GLOBALS['sql_auto_increments']);
  836. unset($GLOBALS['sql_auto_increments']);
  837. }
  838. //add constraints to the sql dump file
  839. if (isset($GLOBALS['sql_constraints'])) {
  840. $result = $this->export->outputHandler($GLOBALS['sql_constraints']);
  841. unset($GLOBALS['sql_constraints']);
  842. }
  843. return $result;
  844. }
  845. /**
  846. * Exports events
  847. *
  848. * @param string $db Database
  849. */
  850. public function exportEvents($db): bool
  851. {
  852. global $crlf, $dbi;
  853. $text = '';
  854. $delimiter = '$$';
  855. $eventNames = $dbi->fetchResult(
  856. 'SELECT EVENT_NAME FROM information_schema.EVENTS WHERE'
  857. . " EVENT_SCHEMA= '" . $dbi->escapeString($db)
  858. . "';"
  859. );
  860. if ($eventNames) {
  861. $text .= $crlf
  862. . 'DELIMITER ' . $delimiter . $crlf;
  863. $text .= $this->exportComment()
  864. . $this->exportComment(__('Events'))
  865. . $this->exportComment();
  866. foreach ($eventNames as $eventName) {
  867. if (! empty($GLOBALS['sql_drop_table'])) {
  868. $text .= 'DROP EVENT IF EXISTS '
  869. . Util::backquote($eventName)
  870. . $delimiter . $crlf;
  871. }
  872. $text .= $dbi->getDefinition($db, 'EVENT', $eventName)
  873. . $delimiter . $crlf . $crlf;
  874. }
  875. $text .= 'DELIMITER ;' . $crlf;
  876. }
  877. if (! empty($text)) {
  878. return $this->export->outputHandler($text);
  879. }
  880. return false;
  881. }
  882. /**
  883. * Exports metadata from Configuration Storage
  884. *
  885. * @param string $db database being exported
  886. * @param string|array $tables table(s) being exported
  887. * @param array $metadataTypes types of metadata to export
  888. */
  889. public function exportMetadata(
  890. $db,
  891. $tables,
  892. array $metadataTypes
  893. ): bool {
  894. $cfgRelation = $this->relation->getRelationsParam();
  895. if (! isset($cfgRelation['db'])) {
  896. return true;
  897. }
  898. $comment = $this->possibleCRLF()
  899. . $this->possibleCRLF()
  900. . $this->exportComment()
  901. . $this->exportComment(__('Metadata'))
  902. . $this->exportComment();
  903. if (! $this->export->outputHandler($comment)) {
  904. return false;
  905. }
  906. if (! $this->exportUseStatement($cfgRelation['db'], $GLOBALS['sql_compatibility'])) {
  907. return false;
  908. }
  909. $r = 1;
  910. if (is_array($tables)) {
  911. // export metadata for each table
  912. foreach ($tables as $table) {
  913. $r &= (int) $this->exportConfigurationMetadata($db, $table, $metadataTypes);
  914. }
  915. // export metadata for the database
  916. $r &= (int) $this->exportConfigurationMetadata($db, null, $metadataTypes);
  917. } else {
  918. // export metadata for single table
  919. $r &= (int) $this->exportConfigurationMetadata($db, $tables, $metadataTypes);
  920. }
  921. return (bool) $r;
  922. }
  923. /**
  924. * Exports metadata from Configuration Storage
  925. *
  926. * @param string $db database being exported
  927. * @param string|null $table table being exported
  928. * @param array $metadataTypes types of metadata to export
  929. */
  930. private function exportConfigurationMetadata(
  931. $db,
  932. $table,
  933. array $metadataTypes
  934. ): bool {
  935. global $dbi;
  936. $cfgRelation = $this->relation->getRelationsParam();
  937. if (isset($table)) {
  938. $types = [
  939. 'column_info' => 'db_name',
  940. 'table_uiprefs' => 'db_name',
  941. 'tracking' => 'db_name',
  942. ];
  943. } else {
  944. $types = [
  945. 'bookmark' => 'dbase',
  946. 'relation' => 'master_db',
  947. 'pdf_pages' => 'db_name',
  948. 'savedsearches' => 'db_name',
  949. 'central_columns' => 'db_name',
  950. ];
  951. }
  952. $aliases = [];
  953. $comment = $this->possibleCRLF()
  954. . $this->exportComment();
  955. if (isset($table)) {
  956. $comment .= $this->exportComment(
  957. sprintf(
  958. __('Metadata for table %s'),
  959. $table
  960. )
  961. );
  962. } else {
  963. $comment .= $this->exportComment(
  964. sprintf(
  965. __('Metadata for database %s'),
  966. $db
  967. )
  968. );
  969. }
  970. $comment .= $this->exportComment();
  971. if (! $this->export->outputHandler($comment)) {
  972. return false;
  973. }
  974. foreach ($types as $type => $dbNameColumn) {
  975. if (! in_array($type, $metadataTypes) || ! isset($cfgRelation[$type])) {
  976. continue;
  977. }
  978. // special case, designer pages and their coordinates
  979. if ($type === 'pdf_pages') {
  980. $sqlQuery = 'SELECT `page_nr`, `page_descr` FROM '
  981. . Util::backquote($cfgRelation['db'])
  982. . '.' . Util::backquote($cfgRelation[$type])
  983. . ' WHERE ' . Util::backquote($dbNameColumn)
  984. . " = '" . $dbi->escapeString($db) . "'";
  985. $result = $dbi->fetchResult($sqlQuery, 'page_nr', 'page_descr');
  986. foreach ($result as $page => $name) {
  987. // insert row for pdf_page
  988. $sqlQueryRow = 'SELECT `db_name`, `page_descr` FROM '
  989. . Util::backquote($cfgRelation['db'])
  990. . '.' . Util::backquote($cfgRelation[$type])
  991. . ' WHERE ' . Util::backquote($dbNameColumn)
  992. . " = '" . $dbi->escapeString($db) . "'"
  993. . " AND `page_nr` = '" . intval($page) . "'";
  994. if (
  995. ! $this->exportData(
  996. $cfgRelation['db'],
  997. $cfgRelation[$type],
  998. $GLOBALS['crlf'],
  999. '',
  1000. $sqlQueryRow,
  1001. $aliases
  1002. )
  1003. ) {
  1004. return false;
  1005. }
  1006. $lastPage = $GLOBALS['crlf']
  1007. . 'SET @LAST_PAGE = LAST_INSERT_ID();'
  1008. . $GLOBALS['crlf'];
  1009. if (! $this->export->outputHandler($lastPage)) {
  1010. return false;
  1011. }
  1012. $sqlQueryCoords = 'SELECT `db_name`, `table_name`, '
  1013. . "'@LAST_PAGE' AS `pdf_page_number`, `x`, `y` FROM "
  1014. . Util::backquote($cfgRelation['db'])
  1015. . '.' . Util::backquote($cfgRelation['table_coords'])
  1016. . " WHERE `pdf_page_number` = '" . $page . "'";
  1017. $GLOBALS['exporting_metadata'] = true;
  1018. if (
  1019. ! $this->exportData(
  1020. $cfgRelation['db'],
  1021. $cfgRelation['table_coords'],
  1022. $GLOBALS['crlf'],
  1023. '',
  1024. $sqlQueryCoords,
  1025. $aliases
  1026. )
  1027. ) {
  1028. $GLOBALS['exporting_metadata'] = false;
  1029. return false;
  1030. }
  1031. $GLOBALS['exporting_metadata'] = false;
  1032. }
  1033. continue;
  1034. }
  1035. // remove auto_incrementing id field for some tables
  1036. if ($type === 'bookmark') {
  1037. $sqlQuery = 'SELECT `dbase`, `user`, `label`, `query` FROM ';
  1038. } elseif ($type === 'column_info') {
  1039. $sqlQuery = 'SELECT `db_name`, `table_name`, `column_name`,'
  1040. . ' `comment`, `mimetype`, `transformation`,'
  1041. . ' `transformation_options`, `input_transformation`,'
  1042. . ' `input_transformation_options` FROM';
  1043. } elseif ($type === 'savedsearches') {
  1044. $sqlQuery = 'SELECT `username`, `db_name`, `search_name`, `search_data` FROM';
  1045. } else {
  1046. $sqlQuery = 'SELECT * FROM ';
  1047. }
  1048. $sqlQuery .= Util::backquote($cfgRelation['db'])
  1049. . '.' . Util::backquote($cfgRelation[$type])
  1050. . ' WHERE ' . Util::backquote($dbNameColumn)
  1051. . " = '" . $dbi->escapeString($db) . "'";
  1052. if (isset($table)) {
  1053. $sqlQuery .= " AND `table_name` = '"
  1054. . $dbi->escapeString($table) . "'";
  1055. }
  1056. if (
  1057. ! $this->exportData(
  1058. $cfgRelation['db'],
  1059. $cfgRelation[$type],
  1060. $GLOBALS['crlf'],
  1061. '',
  1062. $sqlQuery,
  1063. $aliases
  1064. )
  1065. ) {
  1066. return false;
  1067. }
  1068. }
  1069. return true;
  1070. }
  1071. /**
  1072. * Returns a stand-in CREATE definition to resolve view dependencies
  1073. *
  1074. * @param string $db the database name
  1075. * @param string $view the view name
  1076. * @param string $crlf the end of line sequence
  1077. * @param array $aliases Aliases of db/table/columns
  1078. *
  1079. * @return string resulting definition
  1080. */
  1081. public function getTableDefStandIn($db, $view, $crlf, $aliases = [])
  1082. {
  1083. global $dbi;
  1084. $dbAlias = $db;
  1085. $viewAlias = $view;
  1086. $this->initAlias($aliases, $dbAlias, $viewAlias);
  1087. $createQuery = '';
  1088. if (! empty($GLOBALS['sql_drop_table'])) {
  1089. $createQuery .= 'DROP VIEW IF EXISTS '
  1090. . Util::backquote($viewAlias)
  1091. . ';' . $crlf;
  1092. }
  1093. $createQuery .= 'CREATE TABLE ';
  1094. if (isset($GLOBALS['sql_if_not_exists']) && $GLOBALS['sql_if_not_exists']) {
  1095. $createQuery .= 'IF NOT EXISTS ';
  1096. }
  1097. $createQuery .= Util::backquote($viewAlias) . ' (' . $crlf;
  1098. $tmp = [];
  1099. $columns = $dbi->getColumnsFull($db, $view);
  1100. foreach ($columns as $columnName => $definition) {
  1101. $colAlias = $columnName;
  1102. if (! empty($aliases[$db]['tables'][$view]['columns'][$colAlias])) {
  1103. $colAlias = $aliases[$db]['tables'][$view]['columns'][$colAlias];
  1104. }
  1105. $tmp[] = Util::backquote($colAlias) . ' ' .
  1106. $definition['Type'] . $crlf;
  1107. }
  1108. return $createQuery . implode(',', $tmp) . ');' . $crlf;
  1109. }
  1110. /**
  1111. * Returns CREATE definition that matches $view's structure
  1112. *
  1113. * @param string $db the database name
  1114. * @param string $view the view name
  1115. * @param string $crlf the end of line sequence
  1116. * @param bool $addSemicolon whether to add semicolon and end-of-line at
  1117. * the end
  1118. * @param array $aliases Aliases of db/table/columns
  1119. *
  1120. * @return string resulting schema
  1121. */
  1122. private function getTableDefForView(
  1123. $db,
  1124. $view,
  1125. $crlf,
  1126. $addSemicolon = true,
  1127. array $aliases = []
  1128. ) {
  1129. global $dbi;
  1130. $dbAlias = $db;
  1131. $viewAlias = $view;
  1132. $this->initAlias($aliases, $dbAlias, $viewAlias);
  1133. $createQuery = 'CREATE TABLE';
  1134. if (isset($GLOBALS['sql_if_not_exists'])) {
  1135. $createQuery .= ' IF NOT EXISTS ';
  1136. }
  1137. $createQuery .= Util::backquote($viewAlias) . '(' . $crlf;
  1138. $columns = $dbi->getColumns($db, $view, null, true);
  1139. $firstCol = true;
  1140. foreach ($columns as $column) {
  1141. $colAlias = $column['Field'];
  1142. if (! empty($aliases[$db]['tables'][$view]['columns'][$colAlias])) {
  1143. $colAlias = $aliases[$db]['tables'][$view]['columns'][$colAlias];
  1144. }
  1145. $extractedColumnspec = Util::extractColumnSpec($column['Type']);
  1146. if (! $firstCol) {
  1147. $createQuery .= ',' . $crlf;
  1148. }
  1149. $createQuery .= ' ' . Util::backquote($colAlias);
  1150. $createQuery .= ' ' . $column['Type'];
  1151. if ($extractedColumnspec['can_contain_collation'] && ! empty($column['Collation'])) {
  1152. $createQuery .= ' COLLATE ' . $column['Collation'];
  1153. }
  1154. if ($column['Null'] === 'NO') {
  1155. $createQuery .= ' NOT NULL';
  1156. }
  1157. if (isset($column['Default'])) {
  1158. $createQuery .= " DEFAULT '"
  1159. . $dbi->escapeString($column['Default']) . "'";
  1160. } else {
  1161. if ($column['Null'] === 'YES') {
  1162. $createQuery .= ' DEFAULT NULL';
  1163. }
  1164. }
  1165. if (! empty($column['Comment'])) {
  1166. $createQuery .= " COMMENT '"
  1167. . $dbi->escapeString($column['Comment']) . "'";
  1168. }
  1169. $firstCol = false;
  1170. }
  1171. $createQuery .= $crlf . ')' . ($addSemicolon ? ';' : '') . $crlf;
  1172. if (isset($GLOBALS['sql_compatibility'])) {
  1173. $compat = $GLOBALS['sql_compatibility'];
  1174. } else {
  1175. $compat = 'NONE';
  1176. }
  1177. if ($compat === 'MSSQL') {
  1178. $createQuery = $this->makeCreateTableMSSQLCompatible($createQuery);
  1179. }
  1180. return $createQuery;
  1181. }
  1182. /**
  1183. * Returns $table's CREATE definition
  1184. *
  1185. * @param string $db the database name
  1186. * @param string $table the table name
  1187. * @param string $crlf the end of line sequence
  1188. * @param string $errorUrl the url to go back in case
  1189. * of error
  1190. * @param bool $showDates whether to include creation/
  1191. * update/check dates
  1192. * @param bool $addSemicolon whether to add semicolon and
  1193. * end-of-line at the end
  1194. * @param bool $view whether we're handling a view
  1195. * @param bool $updateIndexesIncrements whether we need to update
  1196. * two global variables
  1197. * @param array $aliases Aliases of db/table/columns
  1198. *
  1199. * @return string resulting schema
  1200. */
  1201. public function getTableDef(
  1202. $db,
  1203. $table,
  1204. $crlf,
  1205. $errorUrl,
  1206. $showDates = false,
  1207. $addSemicolon = true,
  1208. $view = false,
  1209. $updateIndexesIncrements = true,
  1210. array $aliases = []
  1211. ) {
  1212. global $sql_drop_table, $sql_backquotes, $sql_constraints,
  1213. $sql_constraints_query, $sql_indexes, $sql_indexes_query,
  1214. $sql_auto_increments, $sql_drop_foreign_keys, $dbi;
  1215. $dbAlias = $db;
  1216. $tableAlias = $table;
  1217. $this->initAlias($aliases, $dbAlias, $tableAlias);
  1218. $schemaCreate = '';
  1219. $autoIncrement = '';
  1220. $newCrlf = $crlf;
  1221. if (isset($GLOBALS['sql_compatibility'])) {
  1222. $compat = $GLOBALS['sql_compatibility'];
  1223. } else {
  1224. $compat = 'NONE';
  1225. }
  1226. // need to use PhpMyAdmin\DatabaseInterface::QUERY_STORE
  1227. // with $dbi->numRows() in mysqli
  1228. $result = $dbi->tryQuery(
  1229. 'SHOW TABLE STATUS FROM ' . Util::backquote($db)
  1230. . ' WHERE Name = \'' . $dbi->escapeString((string) $table) . '\'',
  1231. DatabaseInterface::CONNECT_USER,
  1232. DatabaseInterface::QUERY_STORE
  1233. );
  1234. if ($result != false) {
  1235. if ($dbi->numRows($result) > 0) {
  1236. $tmpres = $dbi->fetchAssoc($result);
  1237. // Here we optionally add the AUTO_INCREMENT next value,
  1238. // but starting with MySQL 5.0.24, the clause is already included
  1239. // in SHOW CREATE TABLE so we'll remove it below
  1240. if (isset($GLOBALS['sql_auto_increment']) && ! empty($tmpres['Auto_increment'])) {
  1241. $autoIncrement .= ' AUTO_INCREMENT='
  1242. . $tmpres['Auto_increment'] . ' ';
  1243. }
  1244. if ($showDates && isset($tmpres['Create_time']) && ! empty($tmpres['Create_time'])) {
  1245. $schemaCreate .= $this->exportComment(
  1246. __('Creation:') . ' '
  1247. . Util::localisedDate(
  1248. strtotime($tmpres['Create_time'])
  1249. )
  1250. );
  1251. $newCrlf = $this->exportComment() . $crlf;
  1252. }
  1253. if ($showDates && isset($tmpres['Update_time']) && ! empty($tmpres['Update_time'])) {
  1254. $schemaCreate .= $this->exportComment(
  1255. __('Last update:') . ' '
  1256. . Util::localisedDate(
  1257. strtotime($tmpres['Update_time'])
  1258. )
  1259. );
  1260. $newCrlf = $this->exportComment() . $crlf;
  1261. }
  1262. if ($showDates && isset($tmpres['Check_time']) && ! empty($tmpres['Check_time'])) {
  1263. $schemaCreate .= $this->exportComment(
  1264. __('Last check:') . ' '
  1265. . Util::localisedDate(
  1266. strtotime($tmpres['Check_time'])
  1267. )
  1268. );
  1269. $newCrlf = $this->exportComment() . $crlf;
  1270. }
  1271. }
  1272. $dbi->freeResult($result);
  1273. }
  1274. $schemaCreate .= $newCrlf;
  1275. if (! empty($sql_drop_table) && $dbi->getTable($db, $table)->isView()) {
  1276. $schemaCreate .= 'DROP VIEW IF EXISTS '
  1277. . Util::backquoteCompat($tableAlias, 'NONE', $sql_backquotes) . ';'
  1278. . $crlf;
  1279. }
  1280. // no need to generate a DROP VIEW here, it was done earlier
  1281. if (! empty($sql_drop_table) && ! $dbi->getTable($db, $table)->isView()) {
  1282. $schemaCreate .= 'DROP TABLE IF EXISTS '
  1283. . Util::backquoteCompat($tableAlias, 'NONE', $sql_backquotes) . ';'
  1284. . $crlf;
  1285. }
  1286. // Complete table dump,
  1287. // Whether to quote table and column names or not
  1288. if ($sql_backquotes) {
  1289. $dbi->query('SET SQL_QUOTE_SHOW_CREATE = 1');
  1290. } else {
  1291. $dbi->query('SET SQL_QUOTE_SHOW_CREATE = 0');
  1292. }
  1293. // I don't see the reason why this unbuffered query could cause problems,
  1294. // because SHOW CREATE TABLE returns only one row, and we free the
  1295. // results below. Nonetheless, we got 2 user reports about this
  1296. // (see bug 1562533) so I removed the unbuffered mode.
  1297. // $result = $dbi->query('SHOW CREATE TABLE ' . backquote($db)
  1298. // . '.' . backquote($table), null, DatabaseInterface::QUERY_UNBUFFERED);
  1299. //
  1300. // Note: SHOW CREATE TABLE, at least in MySQL 5.1.23, does not
  1301. // produce a displayable result for the default value…

Large files files are truncated, but you can click here to view the full file