ExportJson.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 = $GLOBALS['dbi']->query(
  148. $sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED
  149. );
  150. $columns_cnt = $GLOBALS['dbi']->numFields($result);
  151. $columns = array();
  152. for ($i = 0; $i < $columns_cnt; $i++) {
  153. $columns[$i] = stripslashes($GLOBALS['dbi']->fieldName($result, $i));
  154. }
  155. unset($i);
  156. $buffer = '';
  157. $record_cnt = 0;
  158. while ($record = $GLOBALS['dbi']->fetchRow($result)) {
  159. $record_cnt++;
  160. // Output table name as comment if this is the first record of the table
  161. if ($record_cnt == 1) {
  162. $buffer = '// ' . $db . '.' . $table . $crlf . $crlf;
  163. $buffer .= '[';
  164. } else {
  165. $buffer = ', ';
  166. }
  167. if (! PMA_exportOutputHandler($buffer)) {
  168. return false;
  169. }
  170. $data = array();
  171. for ($i = 0; $i < $columns_cnt; $i++) {
  172. $data[$columns[$i]] = $record[$i];
  173. }
  174. if (! PMA_exportOutputHandler(json_encode($data))) {
  175. return false;
  176. }
  177. }
  178. if ($record_cnt) {
  179. if (! PMA_exportOutputHandler(']')) {
  180. return false;
  181. }
  182. }
  183. $GLOBALS['dbi']->freeResult($result);
  184. return true;
  185. }
  186. }
  187. ?>