ExportLatex.php 23 KB

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