ExportOdt.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. <?php
  2. /**
  3. * Set of functions used to build OpenDocument Text dumps of tables
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Export;
  7. use PhpMyAdmin\DatabaseInterface;
  8. use PhpMyAdmin\OpenDocument;
  9. use PhpMyAdmin\Plugins\ExportPlugin;
  10. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  11. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  12. use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
  13. use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem;
  14. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  15. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  16. use PhpMyAdmin\Util;
  17. use function bin2hex;
  18. use function htmlspecialchars;
  19. use function str_replace;
  20. use function stripos;
  21. use function stripslashes;
  22. /**
  23. * Handles the export for the ODT class
  24. */
  25. class ExportOdt extends ExportPlugin
  26. {
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. $GLOBALS['odt_buffer'] = '';
  31. $this->setProperties();
  32. }
  33. /**
  34. * Sets the export ODT properties
  35. *
  36. * @return void
  37. */
  38. protected function setProperties()
  39. {
  40. global $plugin_param;
  41. $hide_structure = false;
  42. if ($plugin_param['export_type'] === 'table'
  43. && ! $plugin_param['single_table']
  44. ) {
  45. $hide_structure = true;
  46. }
  47. $exportPluginProperties = new ExportPluginProperties();
  48. $exportPluginProperties->setText('OpenDocument Text');
  49. $exportPluginProperties->setExtension('odt');
  50. $exportPluginProperties->setMimeType(
  51. 'application/vnd.oasis.opendocument.text'
  52. );
  53. $exportPluginProperties->setForceFile(true);
  54. $exportPluginProperties->setOptionsText(__('Options'));
  55. // create the root group that will be the options field for
  56. // $exportPluginProperties
  57. // this will be shown as "Format specific options"
  58. $exportSpecificOptions = new OptionsPropertyRootGroup(
  59. 'Format Specific Options'
  60. );
  61. // what to dump (structure/data/both) main group
  62. $dumpWhat = new OptionsPropertyMainGroup(
  63. 'general_opts',
  64. __('Dump table')
  65. );
  66. // create primary items and add them to the group
  67. $leaf = new RadioPropertyItem('structure_or_data');
  68. $leaf->setValues(
  69. [
  70. 'structure' => __('structure'),
  71. 'data' => __('data'),
  72. 'structure_and_data' => __('structure and data'),
  73. ]
  74. );
  75. $dumpWhat->addProperty($leaf);
  76. // add the main group to the root group
  77. $exportSpecificOptions->addProperty($dumpWhat);
  78. // structure options main group
  79. if (! $hide_structure) {
  80. $structureOptions = new OptionsPropertyMainGroup(
  81. 'structure',
  82. __('Object creation options')
  83. );
  84. $structureOptions->setForce('data');
  85. // create primary items and add them to the group
  86. if (! empty($GLOBALS['cfgRelation']['relation'])) {
  87. $leaf = new BoolPropertyItem(
  88. 'relation',
  89. __('Display foreign key relationships')
  90. );
  91. $structureOptions->addProperty($leaf);
  92. }
  93. $leaf = new BoolPropertyItem(
  94. 'comments',
  95. __('Display comments')
  96. );
  97. $structureOptions->addProperty($leaf);
  98. if (! empty($GLOBALS['cfgRelation']['mimework'])) {
  99. $leaf = new BoolPropertyItem(
  100. 'mime',
  101. __('Display media types')
  102. );
  103. $structureOptions->addProperty($leaf);
  104. }
  105. // add the main group to the root group
  106. $exportSpecificOptions->addProperty($structureOptions);
  107. }
  108. // data options main group
  109. $dataOptions = new OptionsPropertyMainGroup(
  110. 'data',
  111. __('Data dump options')
  112. );
  113. $dataOptions->setForce('structure');
  114. // create primary items and add them to the group
  115. $leaf = new BoolPropertyItem(
  116. 'columns',
  117. __('Put columns names in the first row')
  118. );
  119. $dataOptions->addProperty($leaf);
  120. $leaf = new TextPropertyItem(
  121. 'null',
  122. __('Replace NULL with:')
  123. );
  124. $dataOptions->addProperty($leaf);
  125. // add the main group to the root group
  126. $exportSpecificOptions->addProperty($dataOptions);
  127. // set the options for the export plugin property item
  128. $exportPluginProperties->setOptions($exportSpecificOptions);
  129. $this->properties = $exportPluginProperties;
  130. }
  131. /**
  132. * Outputs export header
  133. *
  134. * @return bool Whether it succeeded
  135. */
  136. public function exportHeader()
  137. {
  138. $GLOBALS['odt_buffer'] .= '<?xml version="1.0" encoding="utf-8"?' . '>'
  139. . '<office:document-content '
  140. . OpenDocument::NS . ' office:version="1.0">'
  141. . '<office:body>'
  142. . '<office:text>';
  143. return true;
  144. }
  145. /**
  146. * Outputs export footer
  147. *
  148. * @return bool Whether it succeeded
  149. */
  150. public function exportFooter()
  151. {
  152. $GLOBALS['odt_buffer'] .= '</office:text>'
  153. . '</office:body>'
  154. . '</office:document-content>';
  155. return $this->export->outputHandler(OpenDocument::create(
  156. 'application/vnd.oasis.opendocument.text',
  157. $GLOBALS['odt_buffer']
  158. ));
  159. }
  160. /**
  161. * Outputs database header
  162. *
  163. * @param string $db Database name
  164. * @param string $db_alias Aliases of db
  165. *
  166. * @return bool Whether it succeeded
  167. */
  168. public function exportDBHeader($db, $db_alias = '')
  169. {
  170. if (empty($db_alias)) {
  171. $db_alias = $db;
  172. }
  173. $GLOBALS['odt_buffer']
  174. .= '<text:h text:outline-level="1" text:style-name="Heading_1"'
  175. . ' text:is-list-header="true">'
  176. . __('Database') . ' ' . htmlspecialchars($db_alias)
  177. . '</text:h>';
  178. return true;
  179. }
  180. /**
  181. * Outputs database footer
  182. *
  183. * @param string $db Database name
  184. *
  185. * @return bool Whether it succeeded
  186. */
  187. public function exportDBFooter($db)
  188. {
  189. return true;
  190. }
  191. /**
  192. * Outputs CREATE DATABASE statement
  193. *
  194. * @param string $db Database name
  195. * @param string $export_type 'server', 'database', 'table'
  196. * @param string $db_alias Aliases of db
  197. *
  198. * @return bool Whether it succeeded
  199. */
  200. public function exportDBCreate($db, $export_type, $db_alias = '')
  201. {
  202. return true;
  203. }
  204. /**
  205. * Outputs the content of a table in NHibernate format
  206. *
  207. * @param string $db database name
  208. * @param string $table table name
  209. * @param string $crlf the end of line sequence
  210. * @param string $error_url the url to go back in case of error
  211. * @param string $sql_query SQL query for obtaining data
  212. * @param array $aliases Aliases of db/table/columns
  213. *
  214. * @return bool Whether it succeeded
  215. */
  216. public function exportData(
  217. $db,
  218. $table,
  219. $crlf,
  220. $error_url,
  221. $sql_query,
  222. array $aliases = []
  223. ) {
  224. global $what, $dbi;
  225. $db_alias = $db;
  226. $table_alias = $table;
  227. $this->initAlias($aliases, $db_alias, $table_alias);
  228. // Gets the data from the database
  229. $result = $dbi->query(
  230. $sql_query,
  231. DatabaseInterface::CONNECT_USER,
  232. DatabaseInterface::QUERY_UNBUFFERED
  233. );
  234. $fields_cnt = $dbi->numFields($result);
  235. $fields_meta = $dbi->getFieldsMeta($result);
  236. $field_flags = [];
  237. for ($j = 0; $j < $fields_cnt; $j++) {
  238. $field_flags[$j] = $dbi->fieldFlags($result, $j);
  239. }
  240. $GLOBALS['odt_buffer']
  241. .= '<text:h text:outline-level="2" text:style-name="Heading_2"'
  242. . ' text:is-list-header="true">';
  243. $table_alias != ''
  244. ? $GLOBALS['odt_buffer'] .= __('Dumping data for table') . ' ' . htmlspecialchars($table_alias)
  245. : $GLOBALS['odt_buffer'] .= __('Dumping data for query result');
  246. $GLOBALS['odt_buffer']
  247. .= '</text:h>'
  248. . '<table:table'
  249. . ' table:name="' . htmlspecialchars($table_alias) . '_structure">'
  250. . '<table:table-column'
  251. . ' table:number-columns-repeated="' . $fields_cnt . '"/>';
  252. // If required, get fields name at the first line
  253. if (isset($GLOBALS[$what . '_columns'])) {
  254. $GLOBALS['odt_buffer'] .= '<table:table-row>';
  255. for ($i = 0; $i < $fields_cnt; $i++) {
  256. $col_as = $dbi->fieldName($result, $i);
  257. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  258. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  259. }
  260. $GLOBALS['odt_buffer']
  261. .= '<table:table-cell office:value-type="string">'
  262. . '<text:p>'
  263. . htmlspecialchars(
  264. stripslashes($col_as)
  265. )
  266. . '</text:p>'
  267. . '</table:table-cell>';
  268. }
  269. $GLOBALS['odt_buffer'] .= '</table:table-row>';
  270. }
  271. // Format the data
  272. while ($row = $dbi->fetchRow($result)) {
  273. $GLOBALS['odt_buffer'] .= '<table:table-row>';
  274. for ($j = 0; $j < $fields_cnt; $j++) {
  275. if ($fields_meta[$j]->type === 'geometry') {
  276. // export GIS types as hex
  277. $row[$j] = '0x' . bin2hex($row[$j]);
  278. }
  279. if (! isset($row[$j]) || $row[$j] === null) {
  280. $GLOBALS['odt_buffer']
  281. .= '<table:table-cell office:value-type="string">'
  282. . '<text:p>'
  283. . htmlspecialchars($GLOBALS[$what . '_null'])
  284. . '</text:p>'
  285. . '</table:table-cell>';
  286. } elseif (stripos($field_flags[$j], 'BINARY') !== false
  287. && $fields_meta[$j]->blob
  288. ) {
  289. // ignore BLOB
  290. $GLOBALS['odt_buffer']
  291. .= '<table:table-cell office:value-type="string">'
  292. . '<text:p></text:p>'
  293. . '</table:table-cell>';
  294. } elseif ($fields_meta[$j]->numeric
  295. && $fields_meta[$j]->type !== 'timestamp'
  296. && ! $fields_meta[$j]->blob
  297. ) {
  298. $GLOBALS['odt_buffer']
  299. .= '<table:table-cell office:value-type="float"'
  300. . ' office:value="' . $row[$j] . '" >'
  301. . '<text:p>'
  302. . htmlspecialchars($row[$j])
  303. . '</text:p>'
  304. . '</table:table-cell>';
  305. } else {
  306. $GLOBALS['odt_buffer']
  307. .= '<table:table-cell office:value-type="string">'
  308. . '<text:p>'
  309. . htmlspecialchars($row[$j])
  310. . '</text:p>'
  311. . '</table:table-cell>';
  312. }
  313. }
  314. $GLOBALS['odt_buffer'] .= '</table:table-row>';
  315. }
  316. $dbi->freeResult($result);
  317. $GLOBALS['odt_buffer'] .= '</table:table>';
  318. return true;
  319. }
  320. /**
  321. * Outputs result raw query in ODT format
  322. *
  323. * @param string $err_url the url to go back in case of error
  324. * @param string $sql_query the rawquery to output
  325. * @param string $crlf the end of line sequence
  326. *
  327. * @return bool if succeeded
  328. */
  329. public function exportRawQuery(string $err_url, string $sql_query, string $crlf): bool
  330. {
  331. return $this->exportData('', '', $crlf, $err_url, $sql_query);
  332. }
  333. /**
  334. * Returns a stand-in CREATE definition to resolve view dependencies
  335. *
  336. * @param string $db the database name
  337. * @param string $view the view name
  338. * @param string $crlf the end of line sequence
  339. * @param array $aliases Aliases of db/table/columns
  340. *
  341. * @return string resulting definition
  342. */
  343. public function getTableDefStandIn($db, $view, $crlf, $aliases = [])
  344. {
  345. global $dbi;
  346. $db_alias = $db;
  347. $view_alias = $view;
  348. $this->initAlias($aliases, $db_alias, $view_alias);
  349. /**
  350. * Gets fields properties
  351. */
  352. $dbi->selectDb($db);
  353. /**
  354. * Displays the table structure
  355. */
  356. $GLOBALS['odt_buffer']
  357. .= '<table:table table:name="'
  358. . htmlspecialchars($view_alias) . '_data">';
  359. $columns_cnt = 4;
  360. $GLOBALS['odt_buffer']
  361. .= '<table:table-column'
  362. . ' table:number-columns-repeated="' . $columns_cnt . '"/>';
  363. /* Header */
  364. $GLOBALS['odt_buffer'] .= '<table:table-row>'
  365. . '<table:table-cell office:value-type="string">'
  366. . '<text:p>' . __('Column') . '</text:p>'
  367. . '</table:table-cell>'
  368. . '<table:table-cell office:value-type="string">'
  369. . '<text:p>' . __('Type') . '</text:p>'
  370. . '</table:table-cell>'
  371. . '<table:table-cell office:value-type="string">'
  372. . '<text:p>' . __('Null') . '</text:p>'
  373. . '</table:table-cell>'
  374. . '<table:table-cell office:value-type="string">'
  375. . '<text:p>' . __('Default') . '</text:p>'
  376. . '</table:table-cell>'
  377. . '</table:table-row>';
  378. $columns = $dbi->getColumns($db, $view);
  379. foreach ($columns as $column) {
  380. $col_as = $column['Field'] ?? null;
  381. if (! empty($aliases[$db]['tables'][$view]['columns'][$col_as])) {
  382. $col_as = $aliases[$db]['tables'][$view]['columns'][$col_as];
  383. }
  384. $GLOBALS['odt_buffer'] .= $this->formatOneColumnDefinition(
  385. $column,
  386. $col_as
  387. );
  388. $GLOBALS['odt_buffer'] .= '</table:table-row>';
  389. }
  390. $GLOBALS['odt_buffer'] .= '</table:table>';
  391. return '';
  392. }
  393. /**
  394. * Returns $table's CREATE definition
  395. *
  396. * @param string $db the database name
  397. * @param string $table the table name
  398. * @param string $crlf the end of line sequence
  399. * @param string $error_url the url to go back in case of error
  400. * @param bool $do_relation whether to include relation comments
  401. * @param bool $do_comments whether to include the pmadb-style column
  402. * comments as comments in the structure;
  403. * this is deprecated but the parameter is
  404. * left here because /export calls
  405. * PMA_exportStructure() also for other
  406. * @param bool $do_mime whether to include mime comments
  407. * @param bool $show_dates whether to include creation/update/check dates
  408. * @param bool $add_semicolon whether to add semicolon and end-of-line at
  409. * the end
  410. * @param bool $view whether we're handling a view
  411. * @param array $aliases Aliases of db/table/columns
  412. *
  413. * @return bool true
  414. */
  415. public function getTableDef(
  416. $db,
  417. $table,
  418. $crlf,
  419. $error_url,
  420. $do_relation,
  421. $do_comments,
  422. $do_mime,
  423. $show_dates = false,
  424. $add_semicolon = true,
  425. $view = false,
  426. array $aliases = []
  427. ) {
  428. global $cfgRelation, $dbi;
  429. $db_alias = $db;
  430. $table_alias = $table;
  431. $this->initAlias($aliases, $db_alias, $table_alias);
  432. /**
  433. * Gets fields properties
  434. */
  435. $dbi->selectDb($db);
  436. // Check if we can use Relations
  437. [$res_rel, $have_rel] = $this->relation->getRelationsAndStatus(
  438. $do_relation && ! empty($cfgRelation['relation']),
  439. $db,
  440. $table
  441. );
  442. /**
  443. * Displays the table structure
  444. */
  445. $GLOBALS['odt_buffer'] .= '<table:table table:name="'
  446. . htmlspecialchars($table_alias) . '_structure">';
  447. $columns_cnt = 4;
  448. if ($do_relation && $have_rel) {
  449. $columns_cnt++;
  450. }
  451. if ($do_comments) {
  452. $columns_cnt++;
  453. }
  454. if ($do_mime && $cfgRelation['mimework']) {
  455. $columns_cnt++;
  456. }
  457. $GLOBALS['odt_buffer'] .= '<table:table-column'
  458. . ' table:number-columns-repeated="' . $columns_cnt . '"/>';
  459. /* Header */
  460. $GLOBALS['odt_buffer'] .= '<table:table-row>'
  461. . '<table:table-cell office:value-type="string">'
  462. . '<text:p>' . __('Column') . '</text:p>'
  463. . '</table:table-cell>'
  464. . '<table:table-cell office:value-type="string">'
  465. . '<text:p>' . __('Type') . '</text:p>'
  466. . '</table:table-cell>'
  467. . '<table:table-cell office:value-type="string">'
  468. . '<text:p>' . __('Null') . '</text:p>'
  469. . '</table:table-cell>'
  470. . '<table:table-cell office:value-type="string">'
  471. . '<text:p>' . __('Default') . '</text:p>'
  472. . '</table:table-cell>';
  473. if ($do_relation && $have_rel) {
  474. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  475. . '<text:p>' . __('Links to') . '</text:p>'
  476. . '</table:table-cell>';
  477. }
  478. if ($do_comments) {
  479. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  480. . '<text:p>' . __('Comments') . '</text:p>'
  481. . '</table:table-cell>';
  482. $comments = $this->relation->getComments($db, $table);
  483. }
  484. if ($do_mime && $cfgRelation['mimework']) {
  485. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  486. . '<text:p>' . __('Media type') . '</text:p>'
  487. . '</table:table-cell>';
  488. $mime_map = $this->transformations->getMime($db, $table, true);
  489. }
  490. $GLOBALS['odt_buffer'] .= '</table:table-row>';
  491. $columns = $dbi->getColumns($db, $table);
  492. foreach ($columns as $column) {
  493. $col_as = $field_name = $column['Field'];
  494. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  495. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  496. }
  497. $GLOBALS['odt_buffer'] .= $this->formatOneColumnDefinition(
  498. $column,
  499. $col_as
  500. );
  501. if ($do_relation && $have_rel) {
  502. $foreigner = $this->relation->searchColumnInForeigners($res_rel, $field_name);
  503. if ($foreigner) {
  504. $rtable = $foreigner['foreign_table'];
  505. $rfield = $foreigner['foreign_field'];
  506. if (! empty($aliases[$db]['tables'][$rtable]['columns'][$rfield])
  507. ) {
  508. $rfield
  509. = $aliases[$db]['tables'][$rtable]['columns'][$rfield];
  510. }
  511. if (! empty($aliases[$db]['tables'][$rtable]['alias'])) {
  512. $rtable = $aliases[$db]['tables'][$rtable]['alias'];
  513. }
  514. $relation = htmlspecialchars($rtable . ' (' . $rfield . ')');
  515. $GLOBALS['odt_buffer']
  516. .= '<table:table-cell office:value-type="string">'
  517. . '<text:p>'
  518. . htmlspecialchars($relation)
  519. . '</text:p>'
  520. . '</table:table-cell>';
  521. }
  522. }
  523. if ($do_comments) {
  524. if (isset($comments[$field_name])) {
  525. $GLOBALS['odt_buffer']
  526. .= '<table:table-cell office:value-type="string">'
  527. . '<text:p>'
  528. . htmlspecialchars($comments[$field_name])
  529. . '</text:p>'
  530. . '</table:table-cell>';
  531. } else {
  532. $GLOBALS['odt_buffer']
  533. .= '<table:table-cell office:value-type="string">'
  534. . '<text:p></text:p>'
  535. . '</table:table-cell>';
  536. }
  537. }
  538. if ($do_mime && $cfgRelation['mimework']) {
  539. if (isset($mime_map[$field_name])) {
  540. $GLOBALS['odt_buffer']
  541. .= '<table:table-cell office:value-type="string">'
  542. . '<text:p>'
  543. . htmlspecialchars(
  544. str_replace('_', '/', $mime_map[$field_name]['mimetype'])
  545. )
  546. . '</text:p>'
  547. . '</table:table-cell>';
  548. } else {
  549. $GLOBALS['odt_buffer']
  550. .= '<table:table-cell office:value-type="string">'
  551. . '<text:p></text:p>'
  552. . '</table:table-cell>';
  553. }
  554. }
  555. $GLOBALS['odt_buffer'] .= '</table:table-row>';
  556. }
  557. $GLOBALS['odt_buffer'] .= '</table:table>';
  558. return true;
  559. }
  560. /**
  561. * Outputs triggers
  562. *
  563. * @param string $db database name
  564. * @param string $table table name
  565. * @param array $aliases Aliases of db/table/columns
  566. *
  567. * @return string
  568. */
  569. protected function getTriggers($db, $table, array $aliases = [])
  570. {
  571. global $dbi;
  572. $db_alias = $db;
  573. $table_alias = $table;
  574. $this->initAlias($aliases, $db_alias, $table_alias);
  575. $GLOBALS['odt_buffer'] .= '<table:table'
  576. . ' table:name="' . htmlspecialchars($table_alias) . '_triggers">'
  577. . '<table:table-column'
  578. . ' table:number-columns-repeated="4"/>'
  579. . '<table:table-row>'
  580. . '<table:table-cell office:value-type="string">'
  581. . '<text:p>' . __('Name') . '</text:p>'
  582. . '</table:table-cell>'
  583. . '<table:table-cell office:value-type="string">'
  584. . '<text:p>' . __('Time') . '</text:p>'
  585. . '</table:table-cell>'
  586. . '<table:table-cell office:value-type="string">'
  587. . '<text:p>' . __('Event') . '</text:p>'
  588. . '</table:table-cell>'
  589. . '<table:table-cell office:value-type="string">'
  590. . '<text:p>' . __('Definition') . '</text:p>'
  591. . '</table:table-cell>'
  592. . '</table:table-row>';
  593. $triggers = $dbi->getTriggers($db, $table);
  594. foreach ($triggers as $trigger) {
  595. $GLOBALS['odt_buffer'] .= '<table:table-row>';
  596. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  597. . '<text:p>'
  598. . htmlspecialchars($trigger['name'])
  599. . '</text:p>'
  600. . '</table:table-cell>';
  601. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  602. . '<text:p>'
  603. . htmlspecialchars($trigger['action_timing'])
  604. . '</text:p>'
  605. . '</table:table-cell>';
  606. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  607. . '<text:p>'
  608. . htmlspecialchars($trigger['event_manipulation'])
  609. . '</text:p>'
  610. . '</table:table-cell>';
  611. $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
  612. . '<text:p>'
  613. . htmlspecialchars($trigger['definition'])
  614. . '</text:p>'
  615. . '</table:table-cell>';
  616. $GLOBALS['odt_buffer'] .= '</table:table-row>';
  617. }
  618. $GLOBALS['odt_buffer'] .= '</table:table>';
  619. return $GLOBALS['odt_buffer'];
  620. }
  621. /**
  622. * Outputs table's structure
  623. *
  624. * @param string $db database name
  625. * @param string $table table name
  626. * @param string $crlf the end of line sequence
  627. * @param string $error_url the url to go back in case of error
  628. * @param string $export_mode 'create_table', 'triggers', 'create_view',
  629. * 'stand_in'
  630. * @param string $export_type 'server', 'database', 'table'
  631. * @param bool $do_relation whether to include relation comments
  632. * @param bool $do_comments whether to include the pmadb-style column
  633. * comments as comments in the structure;
  634. * this is deprecated but the parameter is
  635. * left here because /export calls
  636. * PMA_exportStructure() also for other
  637. * @param bool $do_mime whether to include mime comments
  638. * @param bool $dates whether to include creation/update/check dates
  639. * @param array $aliases Aliases of db/table/columns
  640. *
  641. * @return bool Whether it succeeded
  642. */
  643. public function exportStructure(
  644. $db,
  645. $table,
  646. $crlf,
  647. $error_url,
  648. $export_mode,
  649. $export_type,
  650. $do_relation = false,
  651. $do_comments = false,
  652. $do_mime = false,
  653. $dates = false,
  654. array $aliases = []
  655. ) {
  656. global $dbi;
  657. $db_alias = $db;
  658. $table_alias = $table;
  659. $this->initAlias($aliases, $db_alias, $table_alias);
  660. switch ($export_mode) {
  661. case 'create_table':
  662. $GLOBALS['odt_buffer']
  663. .= '<text:h text:outline-level="2" text:style-name="Heading_2"'
  664. . ' text:is-list-header="true">'
  665. . __('Table structure for table') . ' ' .
  666. htmlspecialchars($table_alias)
  667. . '</text:h>';
  668. $this->getTableDef(
  669. $db,
  670. $table,
  671. $crlf,
  672. $error_url,
  673. $do_relation,
  674. $do_comments,
  675. $do_mime,
  676. $dates,
  677. true,
  678. false,
  679. $aliases
  680. );
  681. break;
  682. case 'triggers':
  683. $triggers = $dbi->getTriggers($db, $table);
  684. if ($triggers) {
  685. $GLOBALS['odt_buffer']
  686. .= '<text:h text:outline-level="2" text:style-name="Heading_2"'
  687. . ' text:is-list-header="true">'
  688. . __('Triggers') . ' '
  689. . htmlspecialchars($table_alias)
  690. . '</text:h>';
  691. $this->getTriggers($db, $table);
  692. }
  693. break;
  694. case 'create_view':
  695. $GLOBALS['odt_buffer']
  696. .= '<text:h text:outline-level="2" text:style-name="Heading_2"'
  697. . ' text:is-list-header="true">'
  698. . __('Structure for view') . ' '
  699. . htmlspecialchars($table_alias)
  700. . '</text:h>';
  701. $this->getTableDef(
  702. $db,
  703. $table,
  704. $crlf,
  705. $error_url,
  706. $do_relation,
  707. $do_comments,
  708. $do_mime,
  709. $dates,
  710. true,
  711. true,
  712. $aliases
  713. );
  714. break;
  715. case 'stand_in':
  716. $GLOBALS['odt_buffer']
  717. .= '<text:h text:outline-level="2" text:style-name="Heading_2"'
  718. . ' text:is-list-header="true">'
  719. . __('Stand-in structure for view') . ' '
  720. . htmlspecialchars($table_alias)
  721. . '</text:h>';
  722. // export a stand-in definition to resolve view dependencies
  723. $this->getTableDefStandIn($db, $table, $crlf, $aliases);
  724. }
  725. return true;
  726. }
  727. /**
  728. * Formats the definition for one column
  729. *
  730. * @param array $column info about this column
  731. * @param string $col_as column alias
  732. *
  733. * @return string Formatted column definition
  734. */
  735. protected function formatOneColumnDefinition($column, $col_as = '')
  736. {
  737. if (empty($col_as)) {
  738. $col_as = $column['Field'];
  739. }
  740. $definition = '<table:table-row>';
  741. $definition .= '<table:table-cell office:value-type="string">'
  742. . '<text:p>' . htmlspecialchars($col_as) . '</text:p>'
  743. . '</table:table-cell>';
  744. $extracted_columnspec
  745. = Util::extractColumnSpec($column['Type']);
  746. $type = htmlspecialchars($extracted_columnspec['print_type']);
  747. if (empty($type)) {
  748. $type = '&nbsp;';
  749. }
  750. $definition .= '<table:table-cell office:value-type="string">'
  751. . '<text:p>' . htmlspecialchars($type) . '</text:p>'
  752. . '</table:table-cell>';
  753. if (! isset($column['Default'])) {
  754. if ($column['Null'] !== 'NO') {
  755. $column['Default'] = 'NULL';
  756. } else {
  757. $column['Default'] = '';
  758. }
  759. }
  760. $definition .= '<table:table-cell office:value-type="string">'
  761. . '<text:p>'
  762. . ($column['Null'] == '' || $column['Null'] === 'NO'
  763. ? __('No')
  764. : __('Yes'))
  765. . '</text:p>'
  766. . '</table:table-cell>';
  767. $definition .= '<table:table-cell office:value-type="string">'
  768. . '<text:p>' . htmlspecialchars($column['Default']) . '</text:p>'
  769. . '</table:table-cell>';
  770. return $definition;
  771. }
  772. }