ExportLatex.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of methods used to build dumps of tables as Latex
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage Latex
  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 Latex format
  16. *
  17. * @package PhpMyAdmin-Export
  18. * @subpackage Latex
  19. */
  20. class ExportLatex extends ExportPlugin
  21. {
  22. /**
  23. * Constructor
  24. */
  25. public function __construct()
  26. {
  27. // initialize the specific export sql variables
  28. $this->initSpecificVariables();
  29. $this->setProperties();
  30. }
  31. /**
  32. * Initialize the local variables that are used for export Latex
  33. *
  34. * @return void
  35. */
  36. protected function initSpecificVariables()
  37. {
  38. /* Messages used in default captions */
  39. $GLOBALS['strLatexContent'] = __('Content of table @TABLE@');
  40. $GLOBALS['strLatexContinued'] = __('(continued)');
  41. $GLOBALS['strLatexStructure'] = __('Structure of table @TABLE@');
  42. }
  43. /**
  44. * Sets the export Latex properties
  45. *
  46. * @return void
  47. */
  48. protected function setProperties()
  49. {
  50. global $plugin_param;
  51. $hide_structure = false;
  52. if ($plugin_param['export_type'] == 'table'
  53. && ! $plugin_param['single_table']
  54. ) {
  55. $hide_structure = true;
  56. }
  57. $props = 'libraries/properties/';
  58. include_once "$props/plugins/ExportPluginProperties.class.php";
  59. include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
  60. include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
  61. include_once "$props/options/items/BoolPropertyItem.class.php";
  62. include_once "$props/options/items/RadioPropertyItem.class.php";
  63. include_once "$props/options/items/TextPropertyItem.class.php";
  64. $exportPluginProperties = new ExportPluginProperties();
  65. $exportPluginProperties->setText('LaTeX');
  66. $exportPluginProperties->setExtension('tex');
  67. $exportPluginProperties->setMimeType('application/x-tex');
  68. $exportPluginProperties->setOptionsText(__('Options'));
  69. // create the root group that will be the options field for
  70. // $exportPluginProperties
  71. // this will be shown as "Format specific options"
  72. $exportSpecificOptions = new OptionsPropertyRootGroup();
  73. $exportSpecificOptions->setName("Format Specific Options");
  74. // general options main group
  75. $generalOptions = new OptionsPropertyMainGroup();
  76. $generalOptions->setName("general_opts");
  77. // create primary items and add them to the group
  78. $leaf = new BoolPropertyItem();
  79. $leaf->setName("caption");
  80. $leaf->setText(__('Include table caption'));
  81. $generalOptions->addProperty($leaf);
  82. // add the main group to the root group
  83. $exportSpecificOptions->addProperty($generalOptions);
  84. // what to dump (structure/data/both) main group
  85. $dumpWhat = new OptionsPropertyMainGroup();
  86. $dumpWhat->setName("dump_what");
  87. $dumpWhat->setText(__('Dump table'));
  88. // create primary items and add them to the group
  89. $leaf = new RadioPropertyItem();
  90. $leaf->setName("structure_or_data");
  91. $leaf->setValues(
  92. array(
  93. 'structure' => __('structure'),
  94. 'data' => __('data'),
  95. 'structure_and_data' => __('structure and data')
  96. )
  97. );
  98. $dumpWhat->addProperty($leaf);
  99. // add the main group to the root group
  100. $exportSpecificOptions->addProperty($dumpWhat);
  101. // structure options main group
  102. if (! $hide_structure) {
  103. $structureOptions = new OptionsPropertyMainGroup();
  104. $structureOptions->setName("structure");
  105. $structureOptions->setText(__('Object creation options'));
  106. $structureOptions->setForce('data');
  107. // create primary items and add them to the group
  108. $leaf = new TextPropertyItem();
  109. $leaf->setName("structure_caption");
  110. $leaf->setText(__('Table caption'));
  111. $leaf->setDoc('faq6-27');
  112. $structureOptions->addProperty($leaf);
  113. $leaf = new TextPropertyItem();
  114. $leaf->setName("structure_continued_caption");
  115. $leaf->setText(__('Table caption (continued)'));
  116. $leaf->setDoc('faq6-27');
  117. $structureOptions->addProperty($leaf);
  118. $leaf = new TextPropertyItem();
  119. $leaf->setName("structure_label");
  120. $leaf->setText(__('Label key'));
  121. $leaf->setDoc('faq6-27');
  122. $structureOptions->addProperty($leaf);
  123. if (! empty($GLOBALS['cfgRelation']['relation'])) {
  124. $leaf = new BoolPropertyItem();
  125. $leaf->setName("relation");
  126. $leaf->setText(__('Display foreign key relationships'));
  127. $structureOptions->addProperty($leaf);
  128. }
  129. $leaf = new BoolPropertyItem();
  130. $leaf->setName("comments");
  131. $leaf->setText(__('Display comments'));
  132. $structureOptions->addProperty($leaf);
  133. if (! empty($GLOBALS['cfgRelation']['mimework'])) {
  134. $leaf = new BoolPropertyItem();
  135. $leaf->setName("mime");
  136. $leaf->setText(__('Display MIME types'));
  137. $structureOptions->addProperty($leaf);
  138. }
  139. // add the main group to the root group
  140. $exportSpecificOptions->addProperty($structureOptions);
  141. }
  142. // data options main group
  143. $dataOptions = new OptionsPropertyMainGroup();
  144. $dataOptions->setName("data");
  145. $dataOptions->setText(__('Data dump options'));
  146. $dataOptions->setForce('structure');
  147. // create primary items and add them to the group
  148. $leaf = new BoolPropertyItem();
  149. $leaf->setName("columns");
  150. $leaf->setText(__('Put columns names in the first row'));
  151. $dataOptions->addProperty($leaf);
  152. $leaf = new TextPropertyItem();
  153. $leaf->setName("data_caption");
  154. $leaf->setText(__('Table caption'));
  155. $leaf->setDoc('faq6-27');
  156. $dataOptions->addProperty($leaf);
  157. $leaf = new TextPropertyItem();
  158. $leaf->setName("data_continued_caption");
  159. $leaf->setText(__('Table caption (continued)'));
  160. $leaf->setDoc('faq6-27');
  161. $dataOptions->addProperty($leaf);
  162. $leaf = new TextPropertyItem();
  163. $leaf->setName("data_label");
  164. $leaf->setText(__('Label key'));
  165. $leaf->setDoc('faq6-27');
  166. $dataOptions->addProperty($leaf);
  167. $leaf = new TextPropertyItem();
  168. $leaf->setName('null');
  169. $leaf->setText(__('Replace NULL with:'));
  170. $dataOptions->addProperty($leaf);
  171. // add the main group to the root group
  172. $exportSpecificOptions->addProperty($dataOptions);
  173. // set the options for the export plugin property item
  174. $exportPluginProperties->setOptions($exportSpecificOptions);
  175. $this->properties = $exportPluginProperties;
  176. }
  177. /**
  178. * This method is called when any PluginManager to which the observer
  179. * is attached calls PluginManager::notify()
  180. *
  181. * @param SplSubject $subject The PluginManager notifying the observer
  182. * of an update.
  183. *
  184. * @return void
  185. */
  186. public function update (SplSubject $subject)
  187. {
  188. }
  189. /**
  190. * Outputs export header
  191. *
  192. * @return bool Whether it succeeded
  193. */
  194. public function exportHeader ()
  195. {
  196. global $crlf;
  197. global $cfg;
  198. $head = '% phpMyAdmin LaTeX Dump' . $crlf
  199. . '% version ' . PMA_VERSION . $crlf
  200. . '% https://www.phpmyadmin.net' . $crlf
  201. . '%' . $crlf
  202. . '% ' . __('Host') . ': ' . $cfg['Server']['host'];
  203. if (! empty($cfg['Server']['port'])) {
  204. $head .= ':' . $cfg['Server']['port'];
  205. }
  206. $head .= $crlf
  207. . '% ' . __('Generation Time') . ': '
  208. . PMA_Util::localisedDate() . $crlf
  209. . '% ' . __('Server version') . ': ' . PMA_MYSQL_STR_VERSION . $crlf
  210. . '% ' . __('PHP Version') . ': ' . phpversion() . $crlf;
  211. return PMA_exportOutputHandler($head);
  212. }
  213. /**
  214. * Outputs export footer
  215. *
  216. * @return bool Whether it succeeded
  217. */
  218. public function exportFooter ()
  219. {
  220. return true;
  221. }
  222. /**
  223. * Outputs database header
  224. *
  225. * @param string $db Database name
  226. *
  227. * @return bool Whether it succeeded
  228. */
  229. public function exportDBHeader ($db)
  230. {
  231. global $crlf;
  232. $head = '% ' . $crlf
  233. . '% ' . __('Database') . ': ' . '\'' . $db . '\'' . $crlf
  234. . '% ' . $crlf;
  235. return PMA_exportOutputHandler($head);
  236. }
  237. /**
  238. * Outputs database footer
  239. *
  240. * @param string $db Database name
  241. *
  242. * @return bool Whether it succeeded
  243. */
  244. public function exportDBFooter ($db)
  245. {
  246. return true;
  247. }
  248. /**
  249. * Outputs CREATE DATABASE statement
  250. *
  251. * @param string $db Database name
  252. *
  253. * @return bool Whether it succeeded
  254. */
  255. public function exportDBCreate($db)
  256. {
  257. return true;
  258. }
  259. /**
  260. * Outputs the content of a table in JSON format
  261. *
  262. * @param string $db database name
  263. * @param string $table 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 string $sql_query SQL query for obtaining data
  267. *
  268. * @return bool Whether it succeeded
  269. */
  270. public function exportData($db, $table, $crlf, $error_url, $sql_query)
  271. {
  272. $result = PMA_DBI_try_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  273. $columns_cnt = PMA_DBI_num_fields($result);
  274. for ($i = 0; $i < $columns_cnt; $i++) {
  275. $columns[$i] = PMA_DBI_field_name($result, $i);
  276. }
  277. unset($i);
  278. $buffer = $crlf . '%' . $crlf . '% ' . __('Data') . ': ' . $table
  279. . $crlf . '%' . $crlf . ' \\begin{longtable}{|';
  280. for ($index = 0; $index < $columns_cnt; $index++) {
  281. $buffer .= 'l|';
  282. }
  283. $buffer .= '} ' . $crlf ;
  284. $buffer .= ' \\hline \\endhead \\hline \\endfoot \\hline ' . $crlf;
  285. if (isset($GLOBALS['latex_caption'])) {
  286. $buffer .= ' \\caption{'
  287. . PMA_Util::expandUserString(
  288. $GLOBALS['latex_data_caption'],
  289. array(
  290. 'texEscape',
  291. get_class($this),
  292. 'libraries/plugins/export/' . get_class($this) . ".class.php"
  293. ),
  294. array('table' => $table, 'database' => $db)
  295. )
  296. . '} \\label{'
  297. . PMA_Util::expandUserString(
  298. $GLOBALS['latex_data_label'],
  299. null,
  300. array('table' => $table, 'database' => $db)
  301. )
  302. . '} \\\\';
  303. }
  304. if (! PMA_exportOutputHandler($buffer)) {
  305. return false;
  306. }
  307. // show column names
  308. if (isset($GLOBALS['latex_columns'])) {
  309. $buffer = '\\hline ';
  310. for ($i = 0; $i < $columns_cnt; $i++) {
  311. $buffer .= '\\multicolumn{1}{|c|}{\\textbf{'
  312. . self::texEscape(stripslashes($columns[$i])) . '}} & ';
  313. }
  314. $buffer = substr($buffer, 0, -2) . '\\\\ \\hline \hline ';
  315. if (! PMA_exportOutputHandler($buffer . ' \\endfirsthead ' . $crlf)) {
  316. return false;
  317. }
  318. if (isset($GLOBALS['latex_caption'])) {
  319. if (! PMA_exportOutputHandler(
  320. '\\caption{'
  321. . PMA_Util::expandUserString(
  322. $GLOBALS['latex_data_continued_caption'],
  323. array(
  324. 'texEscape',
  325. get_class($this),
  326. 'libraries/plugins/export/'
  327. . get_class($this) . ".class.php"
  328. ),
  329. array('table' => $table, 'database' => $db)
  330. )
  331. . '} \\\\ '
  332. )) {
  333. return false;
  334. }
  335. }
  336. if (! PMA_exportOutputHandler($buffer . '\\endhead \\endfoot' . $crlf)) {
  337. return false;
  338. }
  339. } else {
  340. if (! PMA_exportOutputHandler('\\\\ \hline')) {
  341. return false;
  342. }
  343. }
  344. // print the whole table
  345. while ($record = PMA_DBI_fetch_assoc($result)) {
  346. $buffer = '';
  347. // print each row
  348. for ($i = 0; $i < $columns_cnt; $i++) {
  349. if ((! function_exists('is_null')
  350. || ! is_null($record[$columns[$i]]))
  351. && isset($record[$columns[$i]])
  352. ) {
  353. $column_value = self::texEscape(
  354. stripslashes($record[$columns[$i]])
  355. );
  356. } else {
  357. $column_value = $GLOBALS['latex_null'];
  358. }
  359. // last column ... no need for & character
  360. if ($i == ($columns_cnt - 1)) {
  361. $buffer .= $column_value;
  362. } else {
  363. $buffer .= $column_value . " & ";
  364. }
  365. }
  366. $buffer .= ' \\\\ \\hline ' . $crlf;
  367. if (! PMA_exportOutputHandler($buffer)) {
  368. return false;
  369. }
  370. }
  371. $buffer = ' \\end{longtable}' . $crlf;
  372. if (! PMA_exportOutputHandler($buffer)) {
  373. return false;
  374. }
  375. PMA_DBI_free_result($result);
  376. return true;
  377. } // end getTableLaTeX
  378. /**
  379. * Outputs table's structure
  380. *
  381. * @param string $db database name
  382. * @param string $table table name
  383. * @param string $crlf the end of line sequence
  384. * @param string $error_url the url to go back in case of error
  385. * @param string $export_mode 'create_table', 'triggers', 'create_view',
  386. * 'stand_in'
  387. * @param string $export_type 'server', 'database', 'table'
  388. * @param bool $do_relation whether to include relation comments
  389. * @param bool $do_comments whether to include the pmadb-style column
  390. * comments as comments in the structure;
  391. * this is deprecated but the parameter is
  392. * left here because export.php calls
  393. * exportStructure() also for other
  394. * export types which use this parameter
  395. * @param bool $do_mime whether to include mime comments
  396. * @param bool $dates whether to include creation/update/check dates
  397. *
  398. * @return bool Whether it succeeded
  399. */
  400. public function exportStructure(
  401. $db,
  402. $table,
  403. $crlf,
  404. $error_url,
  405. $export_mode,
  406. $export_type,
  407. $do_relation = false,
  408. $do_comments = false,
  409. $do_mime = false,
  410. $dates = false
  411. ) {
  412. global $cfgRelation;
  413. /* We do not export triggers */
  414. if ($export_mode == 'triggers') {
  415. return true;
  416. }
  417. /**
  418. * Get the unique keys in the table
  419. */
  420. $unique_keys = array();
  421. $keys = PMA_DBI_get_table_indexes($db, $table);
  422. foreach ($keys as $key) {
  423. if ($key['Non_unique'] == 0) {
  424. $unique_keys[] = $key['Column_name'];
  425. }
  426. }
  427. /**
  428. * Gets fields properties
  429. */
  430. PMA_DBI_select_db($db);
  431. // Check if we can use Relations
  432. if ($do_relation && ! empty($cfgRelation['relation'])) {
  433. // Find which tables are related with the current one and write it in
  434. // an array
  435. $res_rel = PMA_getForeigners($db, $table);
  436. if ($res_rel && count($res_rel) > 0) {
  437. $have_rel = true;
  438. } else {
  439. $have_rel = false;
  440. }
  441. } else {
  442. $have_rel = false;
  443. } // end if
  444. /**
  445. * Displays the table structure
  446. */
  447. $buffer = $crlf . '%' . $crlf . '% ' . __('Structure') . ': ' . $table
  448. . $crlf . '%' . $crlf . ' \\begin{longtable}{';
  449. if (! PMA_exportOutputHandler($buffer)) {
  450. return false;
  451. }
  452. $columns_cnt = 4;
  453. $alignment = '|l|c|c|c|';
  454. if ($do_relation && $have_rel) {
  455. $columns_cnt++;
  456. $alignment .= 'l|';
  457. }
  458. if ($do_comments) {
  459. $columns_cnt++;
  460. $alignment .= 'l|';
  461. }
  462. if ($do_mime && $cfgRelation['mimework']) {
  463. $columns_cnt++;
  464. $alignment .='l|';
  465. }
  466. $buffer = $alignment . '} ' . $crlf ;
  467. $header = ' \\hline ';
  468. $header .= '\\multicolumn{1}{|c|}{\\textbf{' . __('Column')
  469. . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Type')
  470. . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Null')
  471. . '}} & \\multicolumn{1}{|c|}{\\textbf{' . __('Default') . '}}';
  472. if ($do_relation && $have_rel) {
  473. $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Links to') . '}}';
  474. }
  475. if ($do_comments) {
  476. $header .= ' & \\multicolumn{1}{|c|}{\\textbf{' . __('Comments') . '}}';
  477. $comments = PMA_getComments($db, $table);
  478. }
  479. if ($do_mime && $cfgRelation['mimework']) {
  480. $header .= ' & \\multicolumn{1}{|c|}{\\textbf{MIME}}';
  481. $mime_map = PMA_getMIME($db, $table, true);
  482. }
  483. // Table caption for first page and label
  484. if (isset($GLOBALS['latex_caption'])) {
  485. $buffer .= ' \\caption{'
  486. . PMA_Util::expandUserString(
  487. $GLOBALS['latex_structure_caption'],
  488. array(
  489. 'texEscape',
  490. get_class($this),
  491. 'libraries/plugins/export/' . get_class($this) . ".class.php"
  492. ),
  493. array('table' => $table, 'database' => $db)
  494. )
  495. . '} \\label{'
  496. . PMA_Util::expandUserString(
  497. $GLOBALS['latex_structure_label'],
  498. null,
  499. array('table' => $table, 'database' => $db)
  500. )
  501. . '} \\\\' . $crlf;
  502. }
  503. $buffer .= $header . ' \\\\ \\hline \\hline' . $crlf
  504. . '\\endfirsthead' . $crlf;
  505. // Table caption on next pages
  506. if (isset($GLOBALS['latex_caption'])) {
  507. $buffer .= ' \\caption{'
  508. . PMA_Util::expandUserString(
  509. $GLOBALS['latex_structure_continued_caption'],
  510. array(
  511. 'texEscape',
  512. get_class($this),
  513. 'libraries/plugins/export/' . get_class($this) . ".class.php"
  514. ),
  515. array('table' => $table, 'database' => $db)
  516. )
  517. . '} \\\\ ' . $crlf;
  518. }
  519. $buffer .= $header . ' \\\\ \\hline \\hline \\endhead \\endfoot ' . $crlf;
  520. if (! PMA_exportOutputHandler($buffer)) {
  521. return false;
  522. }
  523. $fields = PMA_DBI_get_columns($db, $table);
  524. foreach ($fields as $row) {
  525. $extracted_columnspec
  526. = PMA_Util::extractColumnSpec(
  527. $row['Type']
  528. );
  529. $type = $extracted_columnspec['print_type'];
  530. if (empty($type)) {
  531. $type = ' ';
  532. }
  533. if (! isset($row['Default'])) {
  534. if ($row['Null'] != 'NO') {
  535. $row['Default'] = 'NULL';
  536. }
  537. }
  538. $field_name = $row['Field'];
  539. $local_buffer = $field_name . "\000" . $type . "\000"
  540. . (($row['Null'] == '' || $row['Null'] == 'NO')
  541. ? __('No') : __('Yes'))
  542. . "\000" . (isset($row['Default']) ? $row['Default'] : '');
  543. if ($do_relation && $have_rel) {
  544. $local_buffer .= "\000";
  545. if (isset($res_rel[$field_name])) {
  546. $local_buffer .= $res_rel[$field_name]['foreign_table'] . ' ('
  547. . $res_rel[$field_name]['foreign_field'] . ')';
  548. }
  549. }
  550. if ($do_comments && $cfgRelation['commwork']) {
  551. $local_buffer .= "\000";
  552. if (isset($comments[$field_name])) {
  553. $local_buffer .= $comments[$field_name];
  554. }
  555. }
  556. if ($do_mime && $cfgRelation['mimework']) {
  557. $local_buffer .= "\000";
  558. if (isset($mime_map[$field_name])) {
  559. $local_buffer .= str_replace(
  560. '_',
  561. '/',
  562. $mime_map[$field_name]['mimetype']
  563. );
  564. }
  565. }
  566. $local_buffer = self::texEscape($local_buffer);
  567. if ($row['Key']=='PRI') {
  568. $pos=strpos($local_buffer, "\000");
  569. $local_buffer = '\\textit{'
  570. . substr($local_buffer, 0, $pos)
  571. . '}' . substr($local_buffer, $pos);
  572. }
  573. if (in_array($field_name, $unique_keys)) {
  574. $pos=strpos($local_buffer, "\000");
  575. $local_buffer = '\\textbf{'
  576. . substr($local_buffer, 0, $pos)
  577. . '}' . substr($local_buffer, $pos);
  578. }
  579. $buffer = str_replace("\000", ' & ', $local_buffer);
  580. $buffer .= ' \\\\ \\hline ' . $crlf;
  581. if (! PMA_exportOutputHandler($buffer)) {
  582. return false;
  583. }
  584. } // end while
  585. $buffer = ' \\end{longtable}' . $crlf;
  586. return PMA_exportOutputHandler($buffer);
  587. } // end of the 'exportStructure' method
  588. /**
  589. * Escapes some special characters for use in TeX/LaTeX
  590. *
  591. * @param string $string the string to convert
  592. *
  593. * @return string the converted string with escape codes
  594. */
  595. public static function texEscape($string)
  596. {
  597. $escape = array('$', '%', '{', '}', '&', '#', '_', '^');
  598. $cnt_escape = count($escape);
  599. for ($k = 0; $k < $cnt_escape; $k++) {
  600. $string = str_replace($escape[$k], '\\' . $escape[$k], $string);
  601. }
  602. return $string;
  603. }
  604. }
  605. ?>