ExportTexytext.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <?php
  2. /**
  3. * Export to Texy! text.
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Export;
  7. use PhpMyAdmin\DatabaseInterface;
  8. use PhpMyAdmin\Plugins\ExportPlugin;
  9. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  10. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  11. use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
  12. use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem;
  13. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  14. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  15. use PhpMyAdmin\Util;
  16. use function htmlspecialchars;
  17. use function in_array;
  18. use function str_replace;
  19. use function stripslashes;
  20. /**
  21. * Handles the export for the Texy! text class
  22. */
  23. class ExportTexytext extends ExportPlugin
  24. {
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->setProperties();
  29. }
  30. /**
  31. * Sets the export Texy! text properties
  32. *
  33. * @return void
  34. */
  35. protected function setProperties()
  36. {
  37. $exportPluginProperties = new ExportPluginProperties();
  38. $exportPluginProperties->setText('Texy! text');
  39. $exportPluginProperties->setExtension('txt');
  40. $exportPluginProperties->setMimeType('text/plain');
  41. $exportPluginProperties->setOptionsText(__('Options'));
  42. // create the root group that will be the options field for
  43. // $exportPluginProperties
  44. // this will be shown as "Format specific options"
  45. $exportSpecificOptions = new OptionsPropertyRootGroup(
  46. 'Format Specific Options'
  47. );
  48. // what to dump (structure/data/both) main group
  49. $dumpWhat = new OptionsPropertyMainGroup(
  50. 'general_opts',
  51. __('Dump table')
  52. );
  53. // create primary items and add them to the group
  54. $leaf = new RadioPropertyItem('structure_or_data');
  55. $leaf->setValues(
  56. [
  57. 'structure' => __('structure'),
  58. 'data' => __('data'),
  59. 'structure_and_data' => __('structure and data'),
  60. ]
  61. );
  62. $dumpWhat->addProperty($leaf);
  63. // add the main group to the root group
  64. $exportSpecificOptions->addProperty($dumpWhat);
  65. // data options main group
  66. $dataOptions = new OptionsPropertyMainGroup(
  67. 'data',
  68. __('Data dump options')
  69. );
  70. $dataOptions->setForce('structure');
  71. // create primary items and add them to the group
  72. $leaf = new BoolPropertyItem(
  73. 'columns',
  74. __('Put columns names in the first row')
  75. );
  76. $dataOptions->addProperty($leaf);
  77. $leaf = new TextPropertyItem(
  78. 'null',
  79. __('Replace NULL with:')
  80. );
  81. $dataOptions->addProperty($leaf);
  82. // add the main group to the root group
  83. $exportSpecificOptions->addProperty($dataOptions);
  84. // set the options for the export plugin property item
  85. $exportPluginProperties->setOptions($exportSpecificOptions);
  86. $this->properties = $exportPluginProperties;
  87. }
  88. /**
  89. * Outputs export header
  90. *
  91. * @return bool Whether it succeeded
  92. */
  93. public function exportHeader()
  94. {
  95. return true;
  96. }
  97. /**
  98. * Outputs export footer
  99. *
  100. * @return bool Whether it succeeded
  101. */
  102. public function exportFooter()
  103. {
  104. return true;
  105. }
  106. /**
  107. * Outputs database header
  108. *
  109. * @param string $db Database name
  110. * @param string $db_alias Alias of db
  111. *
  112. * @return bool Whether it succeeded
  113. */
  114. public function exportDBHeader($db, $db_alias = '')
  115. {
  116. if (empty($db_alias)) {
  117. $db_alias = $db;
  118. }
  119. return $this->export->outputHandler(
  120. '===' . __('Database') . ' ' . $db_alias . "\n\n"
  121. );
  122. }
  123. /**
  124. * Outputs database footer
  125. *
  126. * @param string $db Database name
  127. *
  128. * @return bool Whether it succeeded
  129. */
  130. public function exportDBFooter($db)
  131. {
  132. return true;
  133. }
  134. /**
  135. * Outputs CREATE DATABASE statement
  136. *
  137. * @param string $db Database name
  138. * @param string $export_type 'server', 'database', 'table'
  139. * @param string $db_alias Aliases of db
  140. *
  141. * @return bool Whether it succeeded
  142. */
  143. public function exportDBCreate($db, $export_type, $db_alias = '')
  144. {
  145. return true;
  146. }
  147. /**
  148. * Outputs the content of a table in NHibernate format
  149. *
  150. * @param string $db database name
  151. * @param string $table table name
  152. * @param string $crlf the end of line sequence
  153. * @param string $error_url the url to go back in case of error
  154. * @param string $sql_query SQL query for obtaining data
  155. * @param array $aliases Aliases of db/table/columns
  156. *
  157. * @return bool Whether it succeeded
  158. */
  159. public function exportData(
  160. $db,
  161. $table,
  162. $crlf,
  163. $error_url,
  164. $sql_query,
  165. array $aliases = []
  166. ) {
  167. global $what, $dbi;
  168. $db_alias = $db;
  169. $table_alias = $table;
  170. $this->initAlias($aliases, $db_alias, $table_alias);
  171. if (! $this->export->outputHandler(
  172. $table_alias != ''
  173. ? '== ' . __('Dumping data for table') . ' ' . $table_alias . "\n\n"
  174. : '==' . __('Dumping data for query result') . "\n\n"
  175. )
  176. ) {
  177. return false;
  178. }
  179. // Gets the data from the database
  180. $result = $dbi->query(
  181. $sql_query,
  182. DatabaseInterface::CONNECT_USER,
  183. DatabaseInterface::QUERY_UNBUFFERED
  184. );
  185. $fields_cnt = $dbi->numFields($result);
  186. // If required, get fields name at the first line
  187. if (isset($GLOBALS[$what . '_columns'])) {
  188. $text_output = "|------\n";
  189. for ($i = 0; $i < $fields_cnt; $i++) {
  190. $col_as = $dbi->fieldName($result, $i);
  191. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  192. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  193. }
  194. $text_output .= '|'
  195. . htmlspecialchars(stripslashes($col_as));
  196. }
  197. $text_output .= "\n|------\n";
  198. if (! $this->export->outputHandler($text_output)) {
  199. return false;
  200. }
  201. }
  202. // Format the data
  203. while ($row = $dbi->fetchRow($result)) {
  204. $text_output = '';
  205. for ($j = 0; $j < $fields_cnt; $j++) {
  206. if (! isset($row[$j]) || $row[$j] === null) {
  207. $value = $GLOBALS[$what . '_null'];
  208. } elseif ($row[$j] == '0' || $row[$j] != '') {
  209. $value = $row[$j];
  210. } else {
  211. $value = ' ';
  212. }
  213. $text_output .= '|'
  214. . str_replace(
  215. '|',
  216. '&#124;',
  217. htmlspecialchars($value)
  218. );
  219. }
  220. $text_output .= "\n";
  221. if (! $this->export->outputHandler($text_output)) {
  222. return false;
  223. }
  224. }
  225. $dbi->freeResult($result);
  226. return true;
  227. }
  228. /**
  229. * Outputs result raw query in TexyText format
  230. *
  231. * @param string $err_url the url to go back in case of error
  232. * @param string $sql_query the rawquery to output
  233. * @param string $crlf the end of line sequence
  234. *
  235. * @return bool if succeeded
  236. */
  237. public function exportRawQuery(string $err_url, string $sql_query, string $crlf): bool
  238. {
  239. return $this->exportData('', '', $crlf, $err_url, $sql_query);
  240. }
  241. /**
  242. * Returns a stand-in CREATE definition to resolve view dependencies
  243. *
  244. * @param string $db the database name
  245. * @param string $view the view name
  246. * @param string $crlf the end of line sequence
  247. * @param array $aliases Aliases of db/table/columns
  248. *
  249. * @return string resulting definition
  250. */
  251. public function getTableDefStandIn($db, $view, $crlf, $aliases = [])
  252. {
  253. global $dbi;
  254. $text_output = '';
  255. /**
  256. * Get the unique keys in the table
  257. */
  258. $unique_keys = [];
  259. $keys = $dbi->getTableIndexes($db, $view);
  260. foreach ($keys as $key) {
  261. if ($key['Non_unique'] != 0) {
  262. continue;
  263. }
  264. $unique_keys[] = $key['Column_name'];
  265. }
  266. /**
  267. * Gets fields properties
  268. */
  269. $dbi->selectDb($db);
  270. /**
  271. * Displays the table structure
  272. */
  273. $text_output .= "|------\n"
  274. . '|' . __('Column')
  275. . '|' . __('Type')
  276. . '|' . __('Null')
  277. . '|' . __('Default')
  278. . "\n|------\n";
  279. $columns = $dbi->getColumns($db, $view);
  280. foreach ($columns as $column) {
  281. $col_as = $column['Field'] ?? null;
  282. if (! empty($aliases[$db]['tables'][$view]['columns'][$col_as])) {
  283. $col_as = $aliases[$db]['tables'][$view]['columns'][$col_as];
  284. }
  285. $text_output .= $this->formatOneColumnDefinition(
  286. $column,
  287. $unique_keys,
  288. $col_as
  289. );
  290. $text_output .= "\n";
  291. }
  292. return $text_output;
  293. }
  294. /**
  295. * Returns $table's CREATE definition
  296. *
  297. * @param string $db the database name
  298. * @param string $table the table name
  299. * @param string $crlf the end of line sequence
  300. * @param string $error_url the url to go back in case of error
  301. * @param bool $do_relation whether to include relation comments
  302. * @param bool $do_comments whether to include the pmadb-style column
  303. * comments as comments in the structure;
  304. * this is deprecated but the parameter is
  305. * left here because /export calls
  306. * $this->exportStructure() also for other
  307. * export types which use this parameter
  308. * @param bool $do_mime whether to include mime comments
  309. * @param bool $show_dates whether to include creation/update/check dates
  310. * @param bool $add_semicolon whether to add semicolon and end-of-line
  311. * at the end
  312. * @param bool $view whether we're handling a view
  313. * @param array $aliases Aliases of db/table/columns
  314. *
  315. * @return string resulting schema
  316. */
  317. public function getTableDef(
  318. $db,
  319. $table,
  320. $crlf,
  321. $error_url,
  322. $do_relation,
  323. $do_comments,
  324. $do_mime,
  325. $show_dates = false,
  326. $add_semicolon = true,
  327. $view = false,
  328. array $aliases = []
  329. ) {
  330. global $cfgRelation, $dbi;
  331. $text_output = '';
  332. /**
  333. * Get the unique keys in the table
  334. */
  335. $unique_keys = [];
  336. $keys = $dbi->getTableIndexes($db, $table);
  337. foreach ($keys as $key) {
  338. if ($key['Non_unique'] != 0) {
  339. continue;
  340. }
  341. $unique_keys[] = $key['Column_name'];
  342. }
  343. /**
  344. * Gets fields properties
  345. */
  346. $dbi->selectDb($db);
  347. // Check if we can use Relations
  348. [$res_rel, $have_rel] = $this->relation->getRelationsAndStatus(
  349. $do_relation && ! empty($cfgRelation['relation']),
  350. $db,
  351. $table
  352. );
  353. /**
  354. * Displays the table structure
  355. */
  356. $text_output .= "|------\n";
  357. $text_output .= '|' . __('Column');
  358. $text_output .= '|' . __('Type');
  359. $text_output .= '|' . __('Null');
  360. $text_output .= '|' . __('Default');
  361. if ($do_relation && $have_rel) {
  362. $text_output .= '|' . __('Links to');
  363. }
  364. if ($do_comments) {
  365. $text_output .= '|' . __('Comments');
  366. $comments = $this->relation->getComments($db, $table);
  367. }
  368. if ($do_mime && $cfgRelation['mimework']) {
  369. $text_output .= '|' . __('Media type');
  370. $mime_map = $this->transformations->getMime($db, $table, true);
  371. }
  372. $text_output .= "\n|------\n";
  373. $columns = $dbi->getColumns($db, $table);
  374. foreach ($columns as $column) {
  375. $col_as = $column['Field'];
  376. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  377. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  378. }
  379. $text_output .= $this->formatOneColumnDefinition(
  380. $column,
  381. $unique_keys,
  382. $col_as
  383. );
  384. $field_name = $column['Field'];
  385. if ($do_relation && $have_rel) {
  386. $text_output .= '|' . htmlspecialchars(
  387. $this->getRelationString(
  388. $res_rel,
  389. $field_name,
  390. $db,
  391. $aliases
  392. )
  393. );
  394. }
  395. if ($do_comments && $cfgRelation['commwork']) {
  396. $text_output .= '|'
  397. . (isset($comments[$field_name])
  398. ? htmlspecialchars($comments[$field_name])
  399. : '');
  400. }
  401. if ($do_mime && $cfgRelation['mimework']) {
  402. $text_output .= '|'
  403. . (isset($mime_map[$field_name])
  404. ? htmlspecialchars(
  405. str_replace('_', '/', $mime_map[$field_name]['mimetype'])
  406. )
  407. : '');
  408. }
  409. $text_output .= "\n";
  410. }
  411. return $text_output;
  412. }
  413. /**
  414. * Outputs triggers
  415. *
  416. * @param string $db database name
  417. * @param string $table table name
  418. *
  419. * @return string Formatted triggers list
  420. */
  421. public function getTriggers($db, $table)
  422. {
  423. global $dbi;
  424. $dump = "|------\n";
  425. $dump .= '|' . __('Name');
  426. $dump .= '|' . __('Time');
  427. $dump .= '|' . __('Event');
  428. $dump .= '|' . __('Definition');
  429. $dump .= "\n|------\n";
  430. $triggers = $dbi->getTriggers($db, $table);
  431. foreach ($triggers as $trigger) {
  432. $dump .= '|' . $trigger['name'];
  433. $dump .= '|' . $trigger['action_timing'];
  434. $dump .= '|' . $trigger['event_manipulation'];
  435. $dump .= '|' .
  436. str_replace(
  437. '|',
  438. '&#124;',
  439. htmlspecialchars($trigger['definition'])
  440. );
  441. $dump .= "\n";
  442. }
  443. return $dump;
  444. }
  445. /**
  446. * Outputs table's structure
  447. *
  448. * @param string $db database name
  449. * @param string $table table name
  450. * @param string $crlf the end of line sequence
  451. * @param string $error_url the url to go back in case of error
  452. * @param string $export_mode 'create_table', 'triggers', 'create_view',
  453. * 'stand_in'
  454. * @param string $export_type 'server', 'database', 'table'
  455. * @param bool $do_relation whether to include relation comments
  456. * @param bool $do_comments whether to include the pmadb-style column
  457. * comments as comments in the structure;
  458. * this is deprecated but the parameter is
  459. * left here because /export calls
  460. * $this->exportStructure() also for other
  461. * export types which use this parameter
  462. * @param bool $do_mime whether to include mime comments
  463. * @param bool $dates whether to include creation/update/check dates
  464. * @param array $aliases Aliases of db/table/columns
  465. *
  466. * @return bool Whether it succeeded
  467. */
  468. public function exportStructure(
  469. $db,
  470. $table,
  471. $crlf,
  472. $error_url,
  473. $export_mode,
  474. $export_type,
  475. $do_relation = false,
  476. $do_comments = false,
  477. $do_mime = false,
  478. $dates = false,
  479. array $aliases = []
  480. ) {
  481. global $dbi;
  482. $db_alias = $db;
  483. $table_alias = $table;
  484. $this->initAlias($aliases, $db_alias, $table_alias);
  485. $dump = '';
  486. switch ($export_mode) {
  487. case 'create_table':
  488. $dump .= '== ' . __('Table structure for table') . ' '
  489. . $table_alias . "\n\n";
  490. $dump .= $this->getTableDef(
  491. $db,
  492. $table,
  493. $crlf,
  494. $error_url,
  495. $do_relation,
  496. $do_comments,
  497. $do_mime,
  498. $dates,
  499. true,
  500. false,
  501. $aliases
  502. );
  503. break;
  504. case 'triggers':
  505. $dump = '';
  506. $triggers = $dbi->getTriggers($db, $table);
  507. if ($triggers) {
  508. $dump .= '== ' . __('Triggers') . ' ' . $table_alias . "\n\n";
  509. $dump .= $this->getTriggers($db, $table);
  510. }
  511. break;
  512. case 'create_view':
  513. $dump .= '== ' . __('Structure for view') . ' ' . $table_alias . "\n\n";
  514. $dump .= $this->getTableDef(
  515. $db,
  516. $table,
  517. $crlf,
  518. $error_url,
  519. $do_relation,
  520. $do_comments,
  521. $do_mime,
  522. $dates,
  523. true,
  524. true,
  525. $aliases
  526. );
  527. break;
  528. case 'stand_in':
  529. $dump .= '== ' . __('Stand-in structure for view')
  530. . ' ' . $table . "\n\n";
  531. // export a stand-in definition to resolve view dependencies
  532. $dump .= $this->getTableDefStandIn($db, $table, $crlf, $aliases);
  533. }
  534. return $this->export->outputHandler($dump);
  535. }
  536. /**
  537. * Formats the definition for one column
  538. *
  539. * @param array $column info about this column
  540. * @param array $unique_keys unique keys for this table
  541. * @param string $col_alias Column Alias
  542. *
  543. * @return string Formatted column definition
  544. */
  545. public function formatOneColumnDefinition(
  546. $column,
  547. $unique_keys,
  548. $col_alias = ''
  549. ) {
  550. if (empty($col_alias)) {
  551. $col_alias = $column['Field'];
  552. }
  553. $extracted_columnspec
  554. = Util::extractColumnSpec($column['Type']);
  555. $type = $extracted_columnspec['print_type'];
  556. if (empty($type)) {
  557. $type = '&nbsp;';
  558. }
  559. if (! isset($column['Default'])) {
  560. if ($column['Null'] !== 'NO') {
  561. $column['Default'] = 'NULL';
  562. }
  563. }
  564. $fmt_pre = '';
  565. $fmt_post = '';
  566. if (in_array($column['Field'], $unique_keys)) {
  567. $fmt_pre = '**' . $fmt_pre;
  568. $fmt_post .= '**';
  569. }
  570. if ($column['Key'] === 'PRI') {
  571. $fmt_pre = '//' . $fmt_pre;
  572. $fmt_post .= '//';
  573. }
  574. $definition = '|'
  575. . $fmt_pre . htmlspecialchars($col_alias) . $fmt_post;
  576. $definition .= '|' . htmlspecialchars($type);
  577. $definition .= '|'
  578. . ($column['Null'] == '' || $column['Null'] === 'NO'
  579. ? __('No') : __('Yes'));
  580. $definition .= '|'
  581. . htmlspecialchars(
  582. $column['Default'] ?? ''
  583. );
  584. return $definition;
  585. }
  586. }