ExportHtmlword.class.php 21 KB

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