ExportYaml.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. 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 YAML format
  16. *
  17. * @package PhpMyAdmin-Export
  18. * @subpackage YAML
  19. */
  20. class ExportYaml extends ExportPlugin
  21. {
  22. /**
  23. * Constructor
  24. */
  25. public function __construct()
  26. {
  27. $this->setProperties();
  28. }
  29. /**
  30. * Sets the export YAML properties
  31. *
  32. * @return void
  33. */
  34. protected function setProperties()
  35. {
  36. $props = 'libraries/properties/';
  37. include_once "$props/plugins/ExportPluginProperties.class.php";
  38. include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
  39. include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
  40. include_once "$props/options/items/HiddenPropertyItem.class.php";
  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. $exportSpecificOptions->setName("Format Specific Options");
  52. // general options main group
  53. $generalOptions = new OptionsPropertyMainGroup();
  54. $generalOptions->setName("general_opts");
  55. // create primary items and add them to the group
  56. $leaf = new HiddenPropertyItem();
  57. $leaf->setName("structure_or_data");
  58. $generalOptions->addProperty($leaf);
  59. // add the main group to the root group
  60. $exportSpecificOptions->addProperty($generalOptions);
  61. // set the options for the export plugin property item
  62. $exportPluginProperties->setOptions($exportSpecificOptions);
  63. $this->properties = $exportPluginProperties;
  64. }
  65. /**
  66. * This method is called when any PluginManager to which the observer
  67. * is attached calls PluginManager::notify()
  68. *
  69. * @param SplSubject $subject The PluginManager notifying the observer
  70. * of an update.
  71. *
  72. * @return void
  73. */
  74. public function update (SplSubject $subject)
  75. {
  76. }
  77. /**
  78. * Outputs export header
  79. *
  80. * @return bool Whether it succeeded
  81. */
  82. public function exportHeader ()
  83. {
  84. PMA_exportOutputHandler(
  85. '%YAML 1.1' . $GLOBALS['crlf'] . '---' . $GLOBALS['crlf']
  86. );
  87. return true;
  88. }
  89. /**
  90. * Outputs export footer
  91. *
  92. * @return bool Whether it succeeded
  93. */
  94. public function exportFooter ()
  95. {
  96. PMA_exportOutputHandler('...' . $GLOBALS['crlf']);
  97. return true;
  98. }
  99. /**
  100. * Outputs database header
  101. *
  102. * @param string $db Database name
  103. *
  104. * @return bool Whether it succeeded
  105. */
  106. public function exportDBHeader ($db)
  107. {
  108. return true;
  109. }
  110. /**
  111. * Outputs database footer
  112. *
  113. * @param string $db Database name
  114. *
  115. * @return bool Whether it succeeded
  116. */
  117. public function exportDBFooter ($db)
  118. {
  119. return true;
  120. }
  121. /**
  122. * Outputs CREATE DATABASE statement
  123. *
  124. * @param string $db Database name
  125. *
  126. * @return bool Whether it succeeded
  127. */
  128. public function exportDBCreate($db)
  129. {
  130. return true;
  131. }
  132. /**
  133. * Outputs the content of a table in JSON format
  134. *
  135. * @param string $db database name
  136. * @param string $table table name
  137. * @param string $crlf the end of line sequence
  138. * @param string $error_url the url to go back in case of error
  139. * @param string $sql_query SQL query for obtaining data
  140. *
  141. * @return bool Whether it succeeded
  142. */
  143. public function exportData($db, $table, $crlf, $error_url, $sql_query)
  144. {
  145. $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  146. $columns_cnt = PMA_DBI_num_fields($result);
  147. for ($i = 0; $i < $columns_cnt; $i++) {
  148. $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
  149. }
  150. unset($i);
  151. $buffer = '';
  152. $record_cnt = 0;
  153. while ($record = PMA_DBI_fetch_row($result)) {
  154. $record_cnt++;
  155. // Output table name as comment if this is the first record of the table
  156. if ($record_cnt == 1) {
  157. $buffer = '# ' . $db . '.' . $table . $crlf;
  158. $buffer .= '-' . $crlf;
  159. } else {
  160. $buffer = '-' . $crlf;
  161. }
  162. for ($i = 0; $i < $columns_cnt; $i++) {
  163. if (! isset($record[$i])) {
  164. continue;
  165. }
  166. $column = $columns[$i];
  167. if (is_null($record[$i])) {
  168. $buffer .= ' ' . $column . ': null' . $crlf;
  169. continue;
  170. }
  171. if (is_numeric($record[$i])) {
  172. $buffer .= ' ' . $column . ': ' . $record[$i] . $crlf;
  173. continue;
  174. }
  175. $record[$i] = str_replace(
  176. array('\\', '"', "\n", "\r"),
  177. array('\\\\', '\"', '\n', '\r'),
  178. $record[$i]
  179. );
  180. $buffer .= ' ' . $column . ': "' . $record[$i] . '"' . $crlf;
  181. }
  182. if (! PMA_exportOutputHandler($buffer)) {
  183. return false;
  184. }
  185. }
  186. PMA_DBI_free_result($result);
  187. return true;
  188. } // end getTableYAML
  189. }
  190. ?>