ExportTexytext.class.php 18 KB

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