ExportYaml.php 6.4 KB

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