ExportTexytext.php 19 KB

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