ExportJson.php 7.8 KB

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