ExportTexytext.php 20 KB

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