ExportYaml.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Set of functions used to build YAML dumps of tables
  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\HiddenPropertyItem;
  12. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  13. use function is_numeric;
  14. use function str_replace;
  15. use function stripslashes;
  16. use function strpos;
  17. /**
  18. * Handles the export for the YAML format
  19. */
  20. class ExportYaml extends ExportPlugin
  21. {
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->setProperties();
  26. }
  27. /**
  28. * Sets the export YAML properties
  29. *
  30. * @return void
  31. */
  32. protected function setProperties()
  33. {
  34. $exportPluginProperties = new ExportPluginProperties();
  35. $exportPluginProperties->setText('YAML');
  36. $exportPluginProperties->setExtension('yml');
  37. $exportPluginProperties->setMimeType('text/yaml');
  38. $exportPluginProperties->setForceFile(true);
  39. $exportPluginProperties->setOptionsText(__('Options'));
  40. // create the root group that will be the options field for
  41. // $exportPluginProperties
  42. // this will be shown as "Format specific options"
  43. $exportSpecificOptions = new OptionsPropertyRootGroup(
  44. 'Format Specific Options'
  45. );
  46. // general options main group
  47. $generalOptions = new OptionsPropertyMainGroup('general_opts');
  48. // create primary items and add them to the group
  49. $leaf = new HiddenPropertyItem('structure_or_data');
  50. $generalOptions->addProperty($leaf);
  51. // add the main group to the root group
  52. $exportSpecificOptions->addProperty($generalOptions);
  53. // set the options for the export plugin property item
  54. $exportPluginProperties->setOptions($exportSpecificOptions);
  55. $this->properties = $exportPluginProperties;
  56. }
  57. /**
  58. * Outputs export header
  59. *
  60. * @return bool Whether it succeeded
  61. */
  62. public function exportHeader()
  63. {
  64. $this->export->outputHandler(
  65. '%YAML 1.1' . $GLOBALS['crlf'] . '---' . $GLOBALS['crlf']
  66. );
  67. return true;
  68. }
  69. /**
  70. * Outputs export footer
  71. *
  72. * @return bool Whether it succeeded
  73. */
  74. public function exportFooter()
  75. {
  76. $this->export->outputHandler('...' . $GLOBALS['crlf']);
  77. return true;
  78. }
  79. /**
  80. * Outputs database header
  81. *
  82. * @param string $db Database name
  83. * @param string $db_alias Aliases of db
  84. *
  85. * @return bool Whether it succeeded
  86. */
  87. public function exportDBHeader($db, $db_alias = '')
  88. {
  89. return true;
  90. }
  91. /**
  92. * Outputs database footer
  93. *
  94. * @param string $db Database name
  95. *
  96. * @return bool Whether it succeeded
  97. */
  98. public function exportDBFooter($db)
  99. {
  100. return true;
  101. }
  102. /**
  103. * Outputs CREATE DATABASE statement
  104. *
  105. * @param string $db Database name
  106. * @param string $export_type 'server', 'database', 'table'
  107. * @param string $db_alias Aliases of db
  108. *
  109. * @return bool Whether it succeeded
  110. */
  111. public function exportDBCreate($db, $export_type, $db_alias = '')
  112. {
  113. return true;
  114. }
  115. /**
  116. * Outputs the content of a table in JSON format
  117. *
  118. * @param string $db database name
  119. * @param string $table table name
  120. * @param string $crlf the end of line sequence
  121. * @param string $error_url the url to go back in case of error
  122. * @param string $sql_query SQL query for obtaining data
  123. * @param array $aliases Aliases of db/table/columns
  124. *
  125. * @return bool Whether it succeeded
  126. */
  127. public function exportData(
  128. $db,
  129. $table,
  130. $crlf,
  131. $error_url,
  132. $sql_query,
  133. array $aliases = []
  134. ) {
  135. global $dbi;
  136. $db_alias = $db;
  137. $table_alias = $table;
  138. $this->initAlias($aliases, $db_alias, $table_alias);
  139. $result = $dbi->query(
  140. $sql_query,
  141. DatabaseInterface::CONNECT_USER,
  142. DatabaseInterface::QUERY_UNBUFFERED
  143. );
  144. $columns_cnt = $dbi->numFields($result);
  145. $fieldsMeta = $dbi->getFieldsMeta($result);
  146. $columns = [];
  147. for ($i = 0; $i < $columns_cnt; $i++) {
  148. $col_as = $dbi->fieldName($result, $i);
  149. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  150. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  151. }
  152. $columns[$i] = stripslashes($col_as);
  153. }
  154. $buffer = '';
  155. $record_cnt = 0;
  156. while ($record = $dbi->fetchRow($result)) {
  157. $record_cnt++;
  158. // Output table name as comment if this is the first record of the table
  159. if ($record_cnt == 1) {
  160. $buffer = '# ' . $db_alias . '.' . $table_alias . $crlf;
  161. $buffer .= '-' . $crlf;
  162. } else {
  163. $buffer = '-' . $crlf;
  164. }
  165. for ($i = 0; $i < $columns_cnt; $i++) {
  166. if (! isset($record[$i])) {
  167. continue;
  168. }
  169. if ($record[$i] === null) {
  170. $buffer .= ' ' . $columns[$i] . ': null' . $crlf;
  171. continue;
  172. }
  173. if (is_numeric($record[$i]) && strpos($fieldsMeta[$i]->type, 'string') === false) {
  174. $buffer .= ' ' . $columns[$i] . ': ' . $record[$i] . $crlf;
  175. continue;
  176. }
  177. $record[$i] = str_replace(
  178. [
  179. '\\',
  180. '"',
  181. "\n",
  182. "\r",
  183. ],
  184. [
  185. '\\\\',
  186. '\"',
  187. '\n',
  188. '\r',
  189. ],
  190. $record[$i]
  191. );
  192. $buffer .= ' ' . $columns[$i] . ': "' . $record[$i] . '"' . $crlf;
  193. }
  194. if (! $this->export->outputHandler($buffer)) {
  195. return false;
  196. }
  197. }
  198. $dbi->freeResult($result);
  199. return true;
  200. }
  201. /**
  202. * Outputs result raw query in YAML format
  203. *
  204. * @param string $err_url the url to go back in case of error
  205. * @param string $sql_query the rawquery to output
  206. * @param string $crlf the end of line sequence
  207. *
  208. * @return bool if succeeded
  209. */
  210. public function exportRawQuery(string $err_url, string $sql_query, string $crlf): bool
  211. {
  212. return $this->exportData('', '', $crlf, $err_url, $sql_query);
  213. }
  214. }