ExportJson.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of methods used to build dumps of tables as JSON
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage JSON
  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 JSON format
  16. *
  17. * @package PhpMyAdmin-Export
  18. * @subpackage JSON
  19. */
  20. class ExportJson extends ExportPlugin
  21. {
  22. /**
  23. * Constructor
  24. */
  25. public function __construct()
  26. {
  27. $this->setProperties();
  28. }
  29. /**
  30. * Sets the export JSON 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('JSON');
  43. $exportPluginProperties->setExtension('json');
  44. $exportPluginProperties->setMimeType('text/plain');
  45. $exportPluginProperties->setOptionsText(__('Options'));
  46. // create the root group that will be the options field for
  47. // $exportPluginProperties
  48. // this will be shown as "Format specific options"
  49. $exportSpecificOptions = new OptionsPropertyRootGroup();
  50. $exportSpecificOptions->setName("Format Specific Options");
  51. // general options main group
  52. $generalOptions = new OptionsPropertyMainGroup();
  53. $generalOptions->setName("general_opts");
  54. // create primary items and add them to the group
  55. $leaf = new HiddenPropertyItem();
  56. $leaf->setName("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. * This method is called when any PluginManager to which the observer
  66. * is attached calls PluginManager::notify()
  67. *
  68. * @param SplSubject $subject The PluginManager notifying the observer
  69. * of an update.
  70. *
  71. * @return void
  72. */
  73. public function update (SplSubject $subject)
  74. {
  75. }
  76. /**
  77. * Outputs export header
  78. *
  79. * @return bool Whether it succeeded
  80. */
  81. public function exportHeader ()
  82. {
  83. PMA_exportOutputHandler(
  84. '/**' . $GLOBALS['crlf']
  85. . ' Export to JSON plugin for PHPMyAdmin' . $GLOBALS['crlf']
  86. . ' @version 0.1' . $GLOBALS['crlf']
  87. . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
  88. );
  89. return true;
  90. }
  91. /**
  92. * Outputs export footer
  93. *
  94. * @return bool Whether it succeeded
  95. */
  96. public function exportFooter ()
  97. {
  98. return true;
  99. }
  100. /**
  101. * Outputs database header
  102. *
  103. * @param string $db Database name
  104. *
  105. * @return bool Whether it succeeded
  106. */
  107. public function exportDBHeader ($db)
  108. {
  109. PMA_exportOutputHandler('// Database \'' . $db . '\'' . $GLOBALS['crlf']);
  110. return true;
  111. }
  112. /**
  113. * Outputs database footer
  114. *
  115. * @param string $db Database name
  116. *
  117. * @return bool Whether it succeeded
  118. */
  119. public function exportDBFooter ($db)
  120. {
  121. return true;
  122. }
  123. /**
  124. * Outputs CREATE DATABASE statement
  125. *
  126. * @param string $db Database name
  127. *
  128. * @return bool Whether it succeeded
  129. */
  130. public function exportDBCreate($db)
  131. {
  132. return true;
  133. }
  134. /**
  135. * Outputs the content of a table in JSON format
  136. *
  137. * @param string $db database name
  138. * @param string $table table name
  139. * @param string $crlf the end of line sequence
  140. * @param string $error_url the url to go back in case of error
  141. * @param string $sql_query SQL query for obtaining data
  142. *
  143. * @return bool Whether it succeeded
  144. */
  145. public function exportData($db, $table, $crlf, $error_url, $sql_query)
  146. {
  147. $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
  148. $columns_cnt = PMA_DBI_num_fields($result);
  149. // Get field information
  150. $fields_meta = PMA_DBI_get_fields_meta($result);
  151. for ($i = 0; $i < $columns_cnt; $i++) {
  152. $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
  153. }
  154. unset($i);
  155. $buffer = '';
  156. $record_cnt = 0;
  157. while ($record = PMA_DBI_fetch_row($result)) {
  158. $record_cnt++;
  159. // Output table name as comment if this is the first record of the table
  160. if ($record_cnt == 1) {
  161. $buffer .= '// ' . $db . '.' . $table . $crlf . $crlf;
  162. $buffer .= '[{';
  163. } else {
  164. $buffer .= ', {';
  165. }
  166. for ($i = 0; $i < $columns_cnt; $i++) {
  167. $isLastLine = ($i + 1 >= $columns_cnt);
  168. $column = $columns[$i];
  169. if (is_null($record[$i])) {
  170. $buffer .= '"' . addslashes($column)
  171. . '": null'
  172. . (! $isLastLine ? ',' : '');
  173. } elseif ($fields_meta[$i]->numeric) {
  174. $buffer .= '"' . addslashes($column)
  175. . '": '
  176. . $record[$i]
  177. . (! $isLastLine ? ',' : '');
  178. } else {
  179. $buffer .= '"' . addslashes($column)
  180. . '": "'
  181. . addslashes($record[$i])
  182. . '"'
  183. . (! $isLastLine ? ',' : '');
  184. }
  185. }
  186. $buffer .= '}';
  187. }
  188. if ($record_cnt) {
  189. $buffer .= ']';
  190. }
  191. if (! PMA_exportOutputHandler($buffer)) {
  192. return false;
  193. }
  194. PMA_DBI_free_result($result);
  195. return true;
  196. }
  197. }
  198. ?>