ExportYaml.php 6.1 KB

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