ExportJson.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. 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\BoolPropertyItem;
  17. use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
  18. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  19. /**
  20. * Handles the export for the JSON format
  21. *
  22. * @package PhpMyAdmin-Export
  23. * @subpackage JSON
  24. */
  25. class ExportJson extends ExportPlugin
  26. {
  27. private $first = true;
  28. /**
  29. * Constructor
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. $this->setProperties();
  35. }
  36. /**
  37. * Encodes the data into JSON
  38. *
  39. * @param mixed $data Data to encode
  40. *
  41. * @return string
  42. */
  43. public function encode($data)
  44. {
  45. $options = 0;
  46. if (isset($GLOBALS['json_pretty_print'])
  47. && $GLOBALS['json_pretty_print']
  48. ) {
  49. $options |= JSON_PRETTY_PRINT;
  50. }
  51. if (isset($GLOBALS['json_unicode'])
  52. && $GLOBALS['json_unicode']
  53. ) {
  54. $options |= JSON_UNESCAPED_UNICODE;
  55. }
  56. return json_encode($data, $options);
  57. }
  58. /**
  59. * Sets the export JSON properties
  60. *
  61. * @return void
  62. */
  63. protected function setProperties()
  64. {
  65. $exportPluginProperties = new ExportPluginProperties();
  66. $exportPluginProperties->setText('JSON');
  67. $exportPluginProperties->setExtension('json');
  68. $exportPluginProperties->setMimeType('text/plain');
  69. $exportPluginProperties->setOptionsText(__('Options'));
  70. // create the root group that will be the options field for
  71. // $exportPluginProperties
  72. // this will be shown as "Format specific options"
  73. $exportSpecificOptions = new OptionsPropertyRootGroup(
  74. "Format Specific Options"
  75. );
  76. // general options main group
  77. $generalOptions = new OptionsPropertyMainGroup("general_opts");
  78. // create primary items and add them to the group
  79. $leaf = new HiddenPropertyItem("structure_or_data");
  80. $generalOptions->addProperty($leaf);
  81. $leaf = new BoolPropertyItem(
  82. 'pretty_print',
  83. __('Output pretty-printed JSON (Use human-readable formatting)')
  84. );
  85. $generalOptions->addProperty($leaf);
  86. $leaf = new BoolPropertyItem(
  87. 'unicode',
  88. __('Output unicode characters unescaped')
  89. );
  90. $generalOptions->addProperty($leaf);
  91. // add the main group to the root group
  92. $exportSpecificOptions->addProperty($generalOptions);
  93. // set the options for the export plugin property item
  94. $exportPluginProperties->setOptions($exportSpecificOptions);
  95. $this->properties = $exportPluginProperties;
  96. }
  97. /**
  98. * Outputs export header
  99. *
  100. * @return bool Whether it succeeded
  101. */
  102. public function exportHeader()
  103. {
  104. global $crlf;
  105. $meta = [
  106. 'type' => 'header',
  107. 'version' => PMA_VERSION,
  108. 'comment' => 'Export to JSON plugin for PHPMyAdmin',
  109. ];
  110. return $this->export->outputHandler(
  111. '[' . $crlf . $this->encode($meta) . ',' . $crlf
  112. );
  113. }
  114. /**
  115. * Outputs export footer
  116. *
  117. * @return bool Whether it succeeded
  118. */
  119. public function exportFooter()
  120. {
  121. global $crlf;
  122. return $this->export->outputHandler(']' . $crlf);
  123. }
  124. /**
  125. * Outputs database header
  126. *
  127. * @param string $db Database name
  128. * @param string $db_alias Aliases of db
  129. *
  130. * @return bool Whether it succeeded
  131. */
  132. public function exportDBHeader($db, $db_alias = '')
  133. {
  134. global $crlf;
  135. if (empty($db_alias)) {
  136. $db_alias = $db;
  137. }
  138. $meta = [
  139. 'type' => 'database',
  140. 'name' => $db_alias,
  141. ];
  142. return $this->export->outputHandler(
  143. $this->encode($meta) . ',' . $crlf
  144. );
  145. }
  146. /**
  147. * Outputs database footer
  148. *
  149. * @param string $db Database name
  150. *
  151. * @return bool Whether it succeeded
  152. */
  153. public function exportDBFooter($db)
  154. {
  155. return true;
  156. }
  157. /**
  158. * Outputs CREATE DATABASE statement
  159. *
  160. * @param string $db Database name
  161. * @param string $export_type 'server', 'database', 'table'
  162. * @param string $db_alias Aliases of db
  163. *
  164. * @return bool Whether it succeeded
  165. */
  166. public function exportDBCreate($db, $export_type, $db_alias = '')
  167. {
  168. return true;
  169. }
  170. /**
  171. * Outputs the content of a table in JSON format
  172. *
  173. * @param string $db database name
  174. * @param string $table table name
  175. * @param string $crlf the end of line sequence
  176. * @param string $error_url the url to go back in case of error
  177. * @param string $sql_query SQL query for obtaining data
  178. * @param array $aliases Aliases of db/table/columns
  179. *
  180. * @return bool Whether it succeeded
  181. */
  182. public function exportData(
  183. $db,
  184. $table,
  185. $crlf,
  186. $error_url,
  187. $sql_query,
  188. array $aliases = []
  189. ) {
  190. $db_alias = $db;
  191. $table_alias = $table;
  192. $this->initAlias($aliases, $db_alias, $table_alias);
  193. if (! $this->first) {
  194. if (! $this->export->outputHandler(',')) {
  195. return false;
  196. }
  197. } else {
  198. $this->first = false;
  199. }
  200. $buffer = $this->encode(
  201. [
  202. 'type' => 'table',
  203. 'name' => $table_alias,
  204. 'database' => $db_alias,
  205. 'data' => "@@DATA@@",
  206. ]
  207. );
  208. list($header, $footer) = explode('"@@DATA@@"', $buffer);
  209. if (! $this->export->outputHandler($header . $crlf . '[' . $crlf)) {
  210. return false;
  211. }
  212. $result = $GLOBALS['dbi']->query(
  213. $sql_query,
  214. DatabaseInterface::CONNECT_USER,
  215. DatabaseInterface::QUERY_UNBUFFERED
  216. );
  217. $columns_cnt = $GLOBALS['dbi']->numFields($result);
  218. $fieldsMeta = $GLOBALS['dbi']->getFieldsMeta($result);
  219. $columns = [];
  220. for ($i = 0; $i < $columns_cnt; $i++) {
  221. $col_as = $GLOBALS['dbi']->fieldName($result, $i);
  222. if (! empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  223. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  224. }
  225. $columns[$i] = stripslashes($col_as);
  226. }
  227. $record_cnt = 0;
  228. while ($record = $GLOBALS['dbi']->fetchRow($result)) {
  229. $record_cnt++;
  230. // Output table name as comment if this is the first record of the table
  231. if ($record_cnt > 1) {
  232. if (! $this->export->outputHandler(',' . $crlf)) {
  233. return false;
  234. }
  235. }
  236. $data = [];
  237. for ($i = 0; $i < $columns_cnt; $i++) {
  238. if ($fieldsMeta[$i]->type === 'geometry') {
  239. // export GIS types as hex
  240. $record[$i] = '0x' . bin2hex($record[$i]);
  241. }
  242. $data[$columns[$i]] = $record[$i];
  243. }
  244. $encodedData = $this->encode($data);
  245. if (! $encodedData) {
  246. return false;
  247. }
  248. if (! $this->export->outputHandler($encodedData)) {
  249. return false;
  250. }
  251. }
  252. if (! $this->export->outputHandler($crlf . ']' . $crlf . $footer . $crlf)) {
  253. return false;
  254. }
  255. $GLOBALS['dbi']->freeResult($result);
  256. return true;
  257. }
  258. }