ExportTexytext.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 = $GLOBALS['dbi']->query(
  176. $sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED
  177. );
  178. $fields_cnt = $GLOBALS['dbi']->numFields($result);
  179. // If required, get fields name at the first line
  180. if (isset($GLOBALS[$what . '_columns'])) {
  181. $text_output = "|------\n";
  182. for ($i = 0; $i < $fields_cnt; $i++) {
  183. $text_output .= '|'
  184. . htmlspecialchars(
  185. stripslashes($GLOBALS['dbi']->fieldName($result, $i))
  186. );
  187. } // end for
  188. $text_output .= "\n|------\n";
  189. if (! PMA_exportOutputHandler($text_output)) {
  190. return false;
  191. }
  192. } // end if
  193. // Format the data
  194. while ($row = $GLOBALS['dbi']->fetchRow($result)) {
  195. $text_output = '';
  196. for ($j = 0; $j < $fields_cnt; $j++) {
  197. if (! isset($row[$j]) || is_null($row[$j])) {
  198. $value = $GLOBALS[$what . '_null'];
  199. } elseif ($row[$j] == '0' || $row[$j] != '') {
  200. $value = $row[$j];
  201. } else {
  202. $value = ' ';
  203. }
  204. $text_output .= '|'
  205. . str_replace(
  206. '|', '&#124;', htmlspecialchars($value)
  207. );
  208. } // end for
  209. $text_output .= "\n";
  210. if (! PMA_exportOutputHandler($text_output)) {
  211. return false;
  212. }
  213. } // end while
  214. $GLOBALS['dbi']->freeResult($result);
  215. return true;
  216. }
  217. /**
  218. * Returns a stand-in CREATE definition to resolve view dependencies
  219. *
  220. * @param string $db the database name
  221. * @param string $view the view name
  222. * @param string $crlf the end of line sequence
  223. *
  224. * @return string resulting definition
  225. */
  226. function getTableDefStandIn($db, $view, $crlf)
  227. {
  228. $text_output = '';
  229. /**
  230. * Get the unique keys in the table
  231. */
  232. $unique_keys = array();
  233. $keys = $GLOBALS['dbi']->getTableIndexes($db, $view);
  234. foreach ($keys as $key) {
  235. if ($key['Non_unique'] == 0) {
  236. $unique_keys[] = $key['Column_name'];
  237. }
  238. }
  239. /**
  240. * Gets fields properties
  241. */
  242. $GLOBALS['dbi']->selectDb($db);
  243. /**
  244. * Displays the table structure
  245. */
  246. $text_output .= "|------\n"
  247. . '|' . __('Column')
  248. . '|' . __('Type')
  249. . '|' . __('Null')
  250. . '|' . __('Default')
  251. . "\n|------\n";
  252. $columns = $GLOBALS['dbi']->getColumns($db, $view);
  253. foreach ($columns as $column) {
  254. $text_output .= $this->formatOneColumnDefinition($column, $unique_keys);
  255. $text_output .= "\n";
  256. } // end foreach
  257. return $text_output;
  258. }
  259. /**
  260. * Returns $table's CREATE definition
  261. *
  262. * @param string $db the database name
  263. * @param string $table the table name
  264. * @param string $crlf the end of line sequence
  265. * @param string $error_url the url to go back in case of error
  266. * @param bool $do_relation whether to include relation comments
  267. * @param bool $do_comments whether to include the pmadb-style column
  268. * comments as comments in the structure;
  269. * this is deprecated but the parameter is
  270. * left here because export.php calls
  271. * $this->exportStructure() also for other
  272. * export types which use this parameter
  273. * @param bool $do_mime whether to include mime comments
  274. * @param bool $show_dates whether to include creation/update/check dates
  275. * @param bool $add_semicolon whether to add semicolon and end-of-line
  276. * at the end
  277. * @param bool $view whether we're handling a view
  278. *
  279. * @return string resulting schema
  280. */
  281. function getTableDef(
  282. $db,
  283. $table,
  284. $crlf,
  285. $error_url,
  286. $do_relation,
  287. $do_comments,
  288. $do_mime,
  289. $show_dates = false,
  290. $add_semicolon = true,
  291. $view = false
  292. ) {
  293. global $cfgRelation;
  294. $text_output = '';
  295. /**
  296. * Get the unique keys in the table
  297. */
  298. $unique_keys = array();
  299. $keys = $GLOBALS['dbi']->getTableIndexes($db, $table);
  300. foreach ($keys as $key) {
  301. if ($key['Non_unique'] == 0) {
  302. $unique_keys[] = $key['Column_name'];
  303. }
  304. }
  305. /**
  306. * Gets fields properties
  307. */
  308. $GLOBALS['dbi']->selectDb($db);
  309. // Check if we can use Relations
  310. if ($do_relation && ! empty($cfgRelation['relation'])) {
  311. // Find which tables are related with the current one and write it in
  312. // an array
  313. $res_rel = PMA_getForeigners($db, $table);
  314. if ($res_rel && count($res_rel) > 0) {
  315. $have_rel = true;
  316. } else {
  317. $have_rel = false;
  318. }
  319. } else {
  320. $have_rel = false;
  321. } // end if
  322. /**
  323. * Displays the table structure
  324. */
  325. $columns_cnt = 4;
  326. if ($do_relation && $have_rel) {
  327. $columns_cnt++;
  328. }
  329. if ($do_comments && $cfgRelation['commwork']) {
  330. $columns_cnt++;
  331. }
  332. if ($do_mime && $cfgRelation['mimework']) {
  333. $columns_cnt++;
  334. }
  335. $text_output .= "|------\n";
  336. $text_output .= '|' . __('Column');
  337. $text_output .= '|' . __('Type');
  338. $text_output .= '|' . __('Null');
  339. $text_output .= '|' . __('Default');
  340. if ($do_relation && $have_rel) {
  341. $text_output .= '|' . __('Links to');
  342. }
  343. if ($do_comments) {
  344. $text_output .= '|' . __('Comments');
  345. $comments = PMA_getComments($db, $table);
  346. }
  347. if ($do_mime && $cfgRelation['mimework']) {
  348. $text_output .= '|' . htmlspecialchars('MIME');
  349. $mime_map = PMA_getMIME($db, $table, true);
  350. }
  351. $text_output .= "\n|------\n";
  352. $columns = $GLOBALS['dbi']->getColumns($db, $table);
  353. foreach ($columns as $column) {
  354. $text_output .= $this->formatOneColumnDefinition($column, $unique_keys);
  355. $field_name = $column['Field'];
  356. if ($do_relation && $have_rel) {
  357. $text_output .= '|'
  358. . (isset($res_rel[$field_name])
  359. ? htmlspecialchars(
  360. $res_rel[$field_name]['foreign_table']
  361. . ' (' . $res_rel[$field_name]['foreign_field'] . ')'
  362. )
  363. : '');
  364. }
  365. if ($do_comments && $cfgRelation['commwork']) {
  366. $text_output .= '|'
  367. . (isset($comments[$field_name])
  368. ? htmlspecialchars($comments[$field_name])
  369. : '');
  370. }
  371. if ($do_mime && $cfgRelation['mimework']) {
  372. $text_output .= '|'
  373. . (isset($mime_map[$field_name])
  374. ? htmlspecialchars(
  375. str_replace('_', '/', $mime_map[$field_name]['mimetype'])
  376. )
  377. : '');
  378. }
  379. $text_output .= "\n";
  380. } // end foreach
  381. return $text_output;
  382. } // end of the '$this->getTableDef()' function
  383. /**
  384. * Outputs triggers
  385. *
  386. * @param string $db database name
  387. * @param string $table table name
  388. *
  389. * @return string Formatted triggers list
  390. */
  391. function getTriggers($db, $table)
  392. {
  393. $text_output = "|------\n";
  394. $text_output .= '|' . __('Column');
  395. $dump = "|------\n";
  396. $dump .= '|' . __('Name');
  397. $dump .= '|' . __('Time');
  398. $dump .= '|' . __('Event');
  399. $dump .= '|' . __('Definition');
  400. $dump .= "\n|------\n";
  401. $triggers = $GLOBALS['dbi']->getTriggers($db, $table);
  402. foreach ($triggers as $trigger) {
  403. $dump .= '|' . $trigger['name'];
  404. $dump .= '|' . $trigger['action_timing'];
  405. $dump .= '|' . $trigger['event_manipulation'];
  406. $dump .= '|' .
  407. str_replace(
  408. '|',
  409. '&#124;',
  410. htmlspecialchars($trigger['definition'])
  411. );
  412. $dump .= "\n";
  413. }
  414. return $dump;
  415. }
  416. /**
  417. * Outputs table's structure
  418. *
  419. * @param string $db database name
  420. * @param string $table table name
  421. * @param string $crlf the end of line sequence
  422. * @param string $error_url the url to go back in case of error
  423. * @param string $export_mode 'create_table', 'triggers', 'create_view',
  424. * 'stand_in'
  425. * @param string $export_type 'server', 'database', 'table'
  426. * @param bool $do_relation whether to include relation comments
  427. * @param bool $do_comments whether to include the pmadb-style column
  428. * comments as comments in the structure;
  429. * this is deprecated but the parameter is
  430. * left here because export.php calls
  431. * $this->exportStructure() also for other
  432. * export types which use this parameter
  433. * @param bool $do_mime whether to include mime comments
  434. * @param bool $dates whether to include creation/update/check dates
  435. *
  436. * @return bool Whether it succeeded
  437. */
  438. function exportStructure(
  439. $db,
  440. $table,
  441. $crlf,
  442. $error_url,
  443. $export_mode,
  444. $export_type,
  445. $do_relation = false,
  446. $do_comments = false,
  447. $do_mime = false,
  448. $dates = false
  449. ) {
  450. $dump = '';
  451. switch($export_mode) {
  452. case 'create_table':
  453. $dump .= '== ' . __('Table structure for table') . ' ' . $table . "\n\n";
  454. $dump .= $this->getTableDef(
  455. $db, $table, $crlf, $error_url, $do_relation, $do_comments,
  456. $do_mime, $dates
  457. );
  458. break;
  459. case 'triggers':
  460. $dump = '';
  461. $triggers = $GLOBALS['dbi']->getTriggers($db, $table);
  462. if ($triggers) {
  463. $dump .= '== ' . __('Triggers') . ' ' . $table . "\n\n";
  464. $dump .= $this->getTriggers($db, $table);
  465. }
  466. break;
  467. case 'create_view':
  468. $dump .= '== ' . __('Structure for view') . ' ' . $table . "\n\n";
  469. $dump .= $this->getTableDef(
  470. $db, $table, $crlf, $error_url, $do_relation, $do_comments,
  471. $do_mime, $dates, true, true
  472. );
  473. break;
  474. case 'stand_in':
  475. $dump .= '== ' . __('Stand-in structure for view')
  476. . ' ' . $table . "\n\n";
  477. // export a stand-in definition to resolve view dependencies
  478. $dump .= $this->getTableDefStandIn($db, $table, $crlf);
  479. } // end switch
  480. return PMA_exportOutputHandler($dump);
  481. }
  482. /**
  483. * Formats the definition for one column
  484. *
  485. * @param array $column info about this column
  486. * @param array $unique_keys unique keys for this table
  487. *
  488. * @return string Formatted column definition
  489. */
  490. function formatOneColumnDefinition(
  491. $column, $unique_keys
  492. ) {
  493. $extracted_columnspec
  494. = PMA_Util::extractColumnSpec($column['Type']);
  495. $type = $extracted_columnspec['print_type'];
  496. if (empty($type)) {
  497. $type = '&nbsp;';
  498. }
  499. if (! isset($column['Default'])) {
  500. if ($column['Null'] != 'NO') {
  501. $column['Default'] = 'NULL';
  502. }
  503. }
  504. $fmt_pre = '';
  505. $fmt_post = '';
  506. if (in_array($column['Field'], $unique_keys)) {
  507. $fmt_pre = '**' . $fmt_pre;
  508. $fmt_post = $fmt_post . '**';
  509. }
  510. if ($column['Key']=='PRI') {
  511. $fmt_pre = '//' . $fmt_pre;
  512. $fmt_post = $fmt_post . '//';
  513. }
  514. $definition = '|'
  515. . $fmt_pre . htmlspecialchars($column['Field']) . $fmt_post;
  516. $definition .= '|' . htmlspecialchars($type);
  517. $definition .= '|'
  518. . (($column['Null'] == '' || $column['Null'] == 'NO')
  519. ? __('No') : __('Yes'));
  520. $definition .= '|'
  521. . htmlspecialchars(
  522. isset($column['Default']) ? $column['Default'] : ''
  523. );
  524. return $definition;
  525. }
  526. }
  527. ?>