ExportHtmlword.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * HTML-Word export code
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage HTML-Word
  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 HTML-Word format
  16. *
  17. * @package PhpMyAdmin-Export
  18. * @subpackage HTML-Word
  19. */
  20. class ExportHtmlword extends ExportPlugin
  21. {
  22. /**
  23. * Constructor
  24. */
  25. public function __construct()
  26. {
  27. $this->setProperties();
  28. }
  29. /**
  30. * Sets the export HTML-Word 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/TextPropertyItem.class.php";
  42. include_once "$props/options/items/BoolPropertyItem.class.php";
  43. $exportPluginProperties = new ExportPluginProperties();
  44. $exportPluginProperties->setText('Microsoft Word 2000');
  45. $exportPluginProperties->setExtension('doc');
  46. $exportPluginProperties->setMimeType('application/vnd.ms-word');
  47. $exportPluginProperties->setForceFile(true);
  48. $exportPluginProperties->setOptionsText(__('Options'));
  49. // create the root group that will be the options field for
  50. // $exportPluginProperties
  51. // this will be shown as "Format specific options"
  52. $exportSpecificOptions = new OptionsPropertyRootGroup();
  53. $exportSpecificOptions->setName("Format Specific Options");
  54. // what to dump (structure/data/both)
  55. $dumpWhat = new OptionsPropertyMainGroup();
  56. $dumpWhat->setName("dump_what");
  57. $dumpWhat->setText(__('Dump table'));
  58. // create primary items and add them to the group
  59. $leaf = new RadioPropertyItem();
  60. $leaf->setName("structure_or_data");
  61. $leaf->setValues(
  62. array(
  63. 'structure' => __('structure'),
  64. 'data' => __('data'),
  65. 'structure_and_data' => __('structure and data')
  66. )
  67. );
  68. $dumpWhat->addProperty($leaf);
  69. // add the main group to the root group
  70. $exportSpecificOptions->addProperty($dumpWhat);
  71. // data options main group
  72. $dataOptions = new OptionsPropertyMainGroup();
  73. $dataOptions->setName("dump_what");
  74. $dataOptions->setText(__('Data dump options'));
  75. $dataOptions->setForce('structure');
  76. // create primary items and add them to the group
  77. $leaf = new TextPropertyItem();
  78. $leaf->setName("null");
  79. $leaf->setText(__('Replace NULL with:'));
  80. $dataOptions->addProperty($leaf);
  81. $leaf = new BoolPropertyItem();
  82. $leaf->setName("columns");
  83. $leaf->setText(__('Put columns names in the first row'));
  84. $dataOptions->addProperty($leaf);
  85. // add the main group to the root group
  86. $exportSpecificOptions->addProperty($dataOptions);
  87. // set the options for the export plugin property item
  88. $exportPluginProperties->setOptions($exportSpecificOptions);
  89. $this->properties = $exportPluginProperties;
  90. }
  91. /**
  92. * This method is called when any PluginManager to which the observer
  93. * is attached calls PluginManager::notify()
  94. *
  95. * @param SplSubject $subject The PluginManager notifying the observer
  96. * of an update.
  97. *
  98. * @return void
  99. */
  100. public function update (SplSubject $subject)
  101. {
  102. }
  103. /**
  104. * Outputs export header
  105. *
  106. * @return bool Whether it succeeded
  107. */
  108. public function exportHeader ()
  109. {
  110. global $charset_of_file;
  111. return PMA_exportOutputHandler(
  112. '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  113. xmlns:x="urn:schemas-microsoft-com:office:word"
  114. xmlns="http://www.w3.org/TR/REC-html40">
  115. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
  116. . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  117. <html>
  118. <head>
  119. <meta http-equiv="Content-type" content="text/html;charset='
  120. . (isset($charset_of_file) ? $charset_of_file : 'utf-8') . '" />
  121. </head>
  122. <body>'
  123. );
  124. }
  125. /**
  126. * Outputs export footer
  127. *
  128. * @return bool Whether it succeeded
  129. */
  130. public function exportFooter ()
  131. {
  132. return PMA_exportOutputHandler('</body></html>');
  133. }
  134. /**
  135. * Outputs database header
  136. *
  137. * @param string $db Database name
  138. *
  139. * @return bool Whether it succeeded
  140. */
  141. public function exportDBHeader ($db)
  142. {
  143. return PMA_exportOutputHandler(
  144. '<h1>' . __('Database') . ' ' . htmlspecialchars($db) . '</h1>'
  145. );
  146. }
  147. /**
  148. * Outputs database footer
  149. *
  150. * @param string $db Database name
  151. *
  152. * @return bool Whether it succeeded
  153. */
  154. public function exportDBFooter ($db)
  155. {
  156. return true;
  157. }
  158. /**
  159. * Outputs CREATE DATABASE statement
  160. *
  161. * @param string $db Database name
  162. *
  163. * @return bool Whether it succeeded
  164. */
  165. public function exportDBCreate($db)
  166. {
  167. return true;
  168. }
  169. /**
  170. * Outputs the content of a table in HTML-Word format
  171. *
  172. * @param string $db database name
  173. * @param string $table table name
  174. * @param string $crlf the end of line sequence
  175. * @param string $error_url the url to go back in case of error
  176. * @param string $sql_query SQL query for obtaining data
  177. *
  178. * @return bool Whether it succeeded
  179. */
  180. public function exportData($db, $table, $crlf, $error_url, $sql_query)
  181. {
  182. global $what;
  183. if (! PMA_exportOutputHandler(
  184. '<h2>'
  185. . __('Dumping data for table') . ' ' . htmlspecialchars($table)
  186. . '</h2>'
  187. )) {
  188. return false;
  189. }
  190. if (! PMA_exportOutputHandler(
  191. '<table class="width100" cellspacing="1">'
  192. )) {
  193. return false;
  194. }
  195. // Gets the data from the database
  196. $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  197. $fields_cnt = PMA_DBI_num_fields($result);
  198. // If required, get fields name at the first line
  199. if (isset($GLOBALS['htmlword_columns'])) {
  200. $schema_insert = '<tr class="print-category">';
  201. for ($i = 0; $i < $fields_cnt; $i++) {
  202. $schema_insert .= '<td class="print"><strong>'
  203. . htmlspecialchars(
  204. stripslashes(PMA_DBI_field_name($result, $i))
  205. )
  206. . '</strong></td>';
  207. } // end for
  208. $schema_insert .= '</tr>';
  209. if (! PMA_exportOutputHandler($schema_insert)) {
  210. return false;
  211. }
  212. } // end if
  213. // Format the data
  214. while ($row = PMA_DBI_fetch_row($result)) {
  215. $schema_insert = '<tr class="print-category">';
  216. for ($j = 0; $j < $fields_cnt; $j++) {
  217. if (! isset($row[$j]) || is_null($row[$j])) {
  218. $value = $GLOBALS[$what . '_null'];
  219. } elseif ($row[$j] == '0' || $row[$j] != '') {
  220. $value = $row[$j];
  221. } else {
  222. $value = '';
  223. }
  224. $schema_insert .= '<td class="print">'
  225. . htmlspecialchars($value)
  226. . '</td>';
  227. } // end for
  228. $schema_insert .= '</tr>';
  229. if (! PMA_exportOutputHandler($schema_insert)) {
  230. return false;
  231. }
  232. } // end while
  233. PMA_DBI_free_result($result);
  234. if (! PMA_exportOutputHandler('</table>')) {
  235. return false;
  236. }
  237. return true;
  238. }
  239. /**
  240. * Returns a stand-in CREATE definition to resolve view dependencies
  241. *
  242. * @param string $db the database name
  243. * @param string $view the view name
  244. * @param string $crlf the end of line sequence
  245. *
  246. * @return string resulting definition
  247. */
  248. public function getTableDefStandIn($db, $view, $crlf)
  249. {
  250. $schema_insert = '<table class="width100" cellspacing="1">'
  251. . '<tr class="print-category">'
  252. . '<th class="print">'
  253. . __('Column')
  254. . '</th>'
  255. . '<td class="print"><strong>'
  256. . __('Type')
  257. . '</strong></td>'
  258. . '<td class="print"><strong>'
  259. . __('Null')
  260. . '</strong></td>'
  261. . '<td class="print"><strong>'
  262. . __('Default')
  263. . '</strong></td>'
  264. . '</tr>';
  265. /**
  266. * Get the unique keys in the view
  267. */
  268. $unique_keys = array();
  269. $keys = PMA_DBI_get_table_indexes($db, $view);
  270. foreach ($keys as $key) {
  271. if ($key['Non_unique'] == 0) {
  272. $unique_keys[] = $key['Column_name'];
  273. }
  274. }
  275. $columns = PMA_DBI_get_columns($db, $view);
  276. foreach ($columns as $column) {
  277. $schema_insert .= $this->formatOneColumnDefinition(
  278. $column,
  279. $unique_keys
  280. );
  281. $schema_insert .= '</tr>';
  282. }
  283. $schema_insert .= '</table>';
  284. return $schema_insert;
  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. * PMA_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. *
  306. * @return string resulting schema
  307. */
  308. public function getTableDef(
  309. $db,
  310. $table,
  311. $crlf,
  312. $error_url,
  313. $do_relation,
  314. $do_comments,
  315. $do_mime,
  316. $show_dates = false,
  317. $add_semicolon = true,
  318. $view = false
  319. ) {
  320. // set $cfgRelation here, because there is a chance that it's modified
  321. // since the class initialization
  322. global $cfgRelation;
  323. $schema_insert = '';
  324. /**
  325. * Gets fields properties
  326. */
  327. PMA_DBI_select_db($db);
  328. // Check if we can use Relations
  329. if ($do_relation && ! empty($cfgRelation['relation'])) {
  330. // Find which tables are related with the current one and write it in
  331. // an array
  332. $res_rel = PMA_getForeigners($db, $table);
  333. if ($res_rel && count($res_rel) > 0) {
  334. $have_rel = true;
  335. } else {
  336. $have_rel = false;
  337. }
  338. } else {
  339. $have_rel = false;
  340. } // end if
  341. /**
  342. * Displays the table structure
  343. */
  344. $schema_insert .= '<table class="width100" cellspacing="1">';
  345. $columns_cnt = 4;
  346. if ($do_relation && $have_rel) {
  347. $columns_cnt++;
  348. }
  349. if ($do_comments && $cfgRelation['commwork']) {
  350. $columns_cnt++;
  351. }
  352. if ($do_mime && $cfgRelation['mimework']) {
  353. $columns_cnt++;
  354. }
  355. $schema_insert .= '<tr class="print-category">';
  356. $schema_insert .= '<th class="print">'
  357. . __('Column')
  358. . '</th>';
  359. $schema_insert .= '<td class="print"><strong>'
  360. . __('Type')
  361. . '</strong></td>';
  362. $schema_insert .= '<td class="print"><strong>'
  363. . __('Null')
  364. . '</strong></td>';
  365. $schema_insert .= '<td class="print"><strong>'
  366. . __('Default')
  367. . '</strong></td>';
  368. if ($do_relation && $have_rel) {
  369. $schema_insert .= '<td class="print"><strong>'
  370. . __('Links to')
  371. . '</strong></td>';
  372. }
  373. if ($do_comments) {
  374. $schema_insert .= '<td class="print"><strong>'
  375. . __('Comments')
  376. . '</strong></td>';
  377. $comments = PMA_getComments($db, $table);
  378. }
  379. if ($do_mime && $cfgRelation['mimework']) {
  380. $schema_insert .= '<td class="print"><strong>'
  381. . htmlspecialchars('MIME')
  382. . '</strong></td>';
  383. $mime_map = PMA_getMIME($db, $table, true);
  384. }
  385. $schema_insert .= '</tr>';
  386. $columns = PMA_DBI_get_columns($db, $table);
  387. /**
  388. * Get the unique keys in the table
  389. */
  390. $unique_keys = array();
  391. $keys = PMA_DBI_get_table_indexes($db, $table);
  392. foreach ($keys as $key) {
  393. if ($key['Non_unique'] == 0) {
  394. $unique_keys[] = $key['Column_name'];
  395. }
  396. }
  397. foreach ($columns as $column) {
  398. $schema_insert .= $this->formatOneColumnDefinition(
  399. $column,
  400. $unique_keys
  401. );
  402. $field_name = $column['Field'];
  403. if ($do_relation && $have_rel) {
  404. $schema_insert .= '<td class="print">'
  405. . (isset($res_rel[$field_name])
  406. ? htmlspecialchars(
  407. $res_rel[$field_name]['foreign_table']
  408. . ' (' . $res_rel[$field_name]['foreign_field']
  409. . ')'
  410. )
  411. : '') . '</td>';
  412. }
  413. if ($do_comments && $cfgRelation['commwork']) {
  414. $schema_insert .= '<td class="print">'
  415. . (isset($comments[$field_name])
  416. ? htmlspecialchars($comments[$field_name])
  417. : '') . '</td>';
  418. }
  419. if ($do_mime && $cfgRelation['mimework']) {
  420. $schema_insert .= '<td class="print">'
  421. . (isset($mime_map[$field_name]) ?
  422. htmlspecialchars(
  423. str_replace('_', '/', $mime_map[$field_name]['mimetype'])
  424. )
  425. : '') . '</td>';
  426. }
  427. $schema_insert .= '</tr>';
  428. } // end foreach
  429. $schema_insert .= '</table>';
  430. return $schema_insert;
  431. }
  432. /**
  433. * Outputs triggers
  434. *
  435. * @param string $db database name
  436. * @param string $table table name
  437. *
  438. * @return string Formatted triggers list
  439. */
  440. protected function getTriggers($db, $table)
  441. {
  442. $dump = '<table class="width100" cellspacing="1">';
  443. $dump .= '<tr class="print-category">';
  444. $dump .= '<th class="print">' . __('Name') . '</th>';
  445. $dump .= '<td class="print"><strong>' . __('Time') . '</strong></td>';
  446. $dump .= '<td class="print"><strong>' . __('Event') . '</strong></td>';
  447. $dump .= '<td class="print"><strong>' . __('Definition') . '</strong></td>';
  448. $dump .= '</tr>';
  449. $triggers = PMA_DBI_get_triggers($db, $table);
  450. foreach ($triggers as $trigger) {
  451. $dump .= '<tr class="print-category">';
  452. $dump .= '<td class="print">'
  453. . htmlspecialchars($trigger['name'])
  454. . '</td>'
  455. . '<td class="print">'
  456. . htmlspecialchars($trigger['action_timing'])
  457. . '</td>'
  458. . '<td class="print">'
  459. . htmlspecialchars($trigger['event_manipulation'])
  460. . '</td>'
  461. . '<td class="print">'
  462. . htmlspecialchars($trigger['definition'])
  463. . '</td>'
  464. . '</tr>';
  465. }
  466. $dump .= '</table>';
  467. return $dump;
  468. }
  469. /**
  470. * Outputs table's structure
  471. *
  472. * @param string $db database name
  473. * @param string $table table name
  474. * @param string $crlf the end of line sequence
  475. * @param string $error_url the url to go back in case of error
  476. * @param string $export_mode 'create_table', 'triggers', 'create_view',
  477. * 'stand_in'
  478. * @param string $export_type 'server', 'database', 'table'
  479. * @param bool $do_relation whether to include relation comments
  480. * @param bool $do_comments whether to include the pmadb-style column
  481. * comments as comments in the structure;
  482. * this is deprecated but the parameter is
  483. * left here because export.php calls
  484. * PMA_exportStructure() also for other
  485. * export types which use this parameter
  486. * @param bool $do_mime whether to include mime comments
  487. * @param bool $dates whether to include creation/update/check dates
  488. *
  489. * @return bool Whether it succeeded
  490. */
  491. public function exportStructure(
  492. $db,
  493. $table,
  494. $crlf,
  495. $error_url,
  496. $export_mode,
  497. $export_type,
  498. $do_relation = false,
  499. $do_comments = false,
  500. $do_mime = false,
  501. $dates = false
  502. ) {
  503. $dump = '';
  504. switch($export_mode) {
  505. case 'create_table':
  506. $dump .= '<h2>'
  507. . __('Table structure for table') . ' ' . htmlspecialchars($table)
  508. . '</h2>';
  509. $dump .= $this->getTableDef(
  510. $db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime,
  511. $dates
  512. );
  513. break;
  514. case 'triggers':
  515. $dump = '';
  516. $triggers = PMA_DBI_get_triggers($db, $table);
  517. if ($triggers) {
  518. $dump .= '<h2>'
  519. . __('Triggers') . ' ' . htmlspecialchars($table)
  520. . '</h2>';
  521. $dump .= $this->getTriggers($db, $table);
  522. }
  523. break;
  524. case 'create_view':
  525. $dump .= '<h2>'
  526. . __('Structure for view') . ' ' . htmlspecialchars($table)
  527. . '</h2>';
  528. $dump .= $this->getTableDef(
  529. $db, $table, $crlf, $error_url, $do_relation, $do_comments, $do_mime,
  530. $dates, true, true
  531. );
  532. break;
  533. case 'stand_in':
  534. $dump .= '<h2>'
  535. . __('Stand-in structure for view') . ' ' . htmlspecialchars($table)
  536. . '</h2>';
  537. // export a stand-in definition to resolve view dependencies
  538. $dump .= $this->getTableDefStandIn($db, $table, $crlf);
  539. } // end switch
  540. return PMA_exportOutputHandler($dump);
  541. }
  542. /**
  543. * Formats the definition for one column
  544. *
  545. * @param array $column info about this column
  546. * @param array $unique_keys unique keys of the table
  547. *
  548. * @return string Formatted column definition
  549. */
  550. protected function formatOneColumnDefinition(
  551. $column, $unique_keys
  552. ) {
  553. $definition = '<tr class="print-category">';
  554. $extracted_columnspec
  555. = PMA_Util::extractColumnSpec($column['Type']);
  556. $type = htmlspecialchars($extracted_columnspec['print_type']);
  557. if (empty($type)) {
  558. $type = '&nbsp;';
  559. }
  560. if (! isset($column['Default'])) {
  561. if ($column['Null'] != 'NO') {
  562. $column['Default'] = 'NULL';
  563. }
  564. }
  565. $fmt_pre = '';
  566. $fmt_post = '';
  567. if (in_array($column['Field'], $unique_keys)) {
  568. $fmt_pre = '<strong>' . $fmt_pre;
  569. $fmt_post = $fmt_post . '</strong>';
  570. }
  571. if ($column['Key'] == 'PRI') {
  572. $fmt_pre = '<em>' . $fmt_pre;
  573. $fmt_post = $fmt_post . '</em>';
  574. }
  575. $definition .= '<td class="print">' . $fmt_pre
  576. . htmlspecialchars($column['Field']) . $fmt_post . '</td>';
  577. $definition .= '<td class="print">' . htmlspecialchars($type)
  578. . '</td>';
  579. $definition .= '<td class="print">'
  580. . (($column['Null'] == '' || $column['Null'] == 'NO')
  581. ? __('No')
  582. : __('Yes'))
  583. . '</td>';
  584. $definition .= '<td class="print">'
  585. . htmlspecialchars(
  586. isset($column['Default'])
  587. ? $column['Default']
  588. : ''
  589. )
  590. . '</td>';
  591. return $definition;
  592. }
  593. }
  594. ?>