ExportCsv.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * CSV export code
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage CSV
  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. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  19. /**
  20. * Handles the export for the CSV format
  21. *
  22. * @package PhpMyAdmin-Export
  23. * @subpackage CSV
  24. */
  25. class ExportCsv extends ExportPlugin
  26. {
  27. /**
  28. * Constructor
  29. */
  30. public function __construct()
  31. {
  32. $this->setProperties();
  33. }
  34. /**
  35. * Sets the export CSV properties
  36. *
  37. * @return void
  38. */
  39. protected function setProperties()
  40. {
  41. $exportPluginProperties = new ExportPluginProperties();
  42. $exportPluginProperties->setText('CSV');
  43. $exportPluginProperties->setExtension('csv');
  44. $exportPluginProperties->setMimeType('text/comma-separated-values');
  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. "Format Specific Options"
  51. );
  52. // general options main group
  53. $generalOptions = new OptionsPropertyMainGroup("general_opts");
  54. // create leaf items and add them to the group
  55. $leaf = new TextPropertyItem(
  56. "separator",
  57. __('Columns separated with:')
  58. );
  59. $generalOptions->addProperty($leaf);
  60. $leaf = new TextPropertyItem(
  61. "enclosed",
  62. __('Columns enclosed with:')
  63. );
  64. $generalOptions->addProperty($leaf);
  65. $leaf = new TextPropertyItem(
  66. "escaped",
  67. __('Columns escaped with:')
  68. );
  69. $generalOptions->addProperty($leaf);
  70. $leaf = new TextPropertyItem(
  71. "terminated",
  72. __('Lines terminated with:')
  73. );
  74. $generalOptions->addProperty($leaf);
  75. $leaf = new TextPropertyItem(
  76. 'null',
  77. __('Replace NULL with:')
  78. );
  79. $generalOptions->addProperty($leaf);
  80. $leaf = new BoolPropertyItem(
  81. 'removeCRLF',
  82. __('Remove carriage return/line feed characters within columns')
  83. );
  84. $generalOptions->addProperty($leaf);
  85. $leaf = new BoolPropertyItem(
  86. 'columns',
  87. __('Put columns names in the first row')
  88. );
  89. $generalOptions->addProperty($leaf);
  90. $leaf = new HiddenPropertyItem(
  91. 'structure_or_data'
  92. );
  93. $generalOptions->addProperty($leaf);
  94. // add the main group to the root group
  95. $exportSpecificOptions->addProperty($generalOptions);
  96. // set the options for the export plugin property item
  97. $exportPluginProperties->setOptions($exportSpecificOptions);
  98. $this->properties = $exportPluginProperties;
  99. }
  100. /**
  101. * Outputs export header
  102. *
  103. * @return bool Whether it succeeded
  104. */
  105. public function exportHeader()
  106. {
  107. global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped;
  108. // Here we just prepare some values for export
  109. if ($what == 'excel') {
  110. $csv_terminated = "\015\012";
  111. switch ($GLOBALS['excel_edition']) {
  112. case 'win':
  113. // as tested on Windows with Excel 2002 and Excel 2007
  114. $csv_separator = ';';
  115. break;
  116. case 'mac_excel2003':
  117. $csv_separator = ';';
  118. break;
  119. case 'mac_excel2008':
  120. $csv_separator = ',';
  121. break;
  122. }
  123. $csv_enclosed = '"';
  124. $csv_escaped = '"';
  125. if (isset($GLOBALS['excel_columns'])) {
  126. $GLOBALS['csv_columns'] = 'yes';
  127. }
  128. } else {
  129. if (empty($csv_terminated)
  130. || mb_strtolower($csv_terminated) == 'auto'
  131. ) {
  132. $csv_terminated = $GLOBALS['crlf'];
  133. } else {
  134. $csv_terminated = str_replace('\\r', "\015", $csv_terminated);
  135. $csv_terminated = str_replace('\\n', "\012", $csv_terminated);
  136. $csv_terminated = str_replace('\\t', "\011", $csv_terminated);
  137. } // end if
  138. $csv_separator = str_replace('\\t', "\011", $csv_separator);
  139. }
  140. return true;
  141. }
  142. /**
  143. * Outputs export footer
  144. *
  145. * @return bool Whether it succeeded
  146. */
  147. public function exportFooter()
  148. {
  149. return true;
  150. }
  151. /**
  152. * Outputs database header
  153. *
  154. * @param string $db Database name
  155. * @param string $db_alias Alias of db
  156. *
  157. * @return bool Whether it succeeded
  158. */
  159. public function exportDBHeader($db, $db_alias = '')
  160. {
  161. return true;
  162. }
  163. /**
  164. * Outputs database footer
  165. *
  166. * @param string $db Database name
  167. *
  168. * @return bool Whether it succeeded
  169. */
  170. public function exportDBFooter($db)
  171. {
  172. return true;
  173. }
  174. /**
  175. * Outputs CREATE DATABASE statement
  176. *
  177. * @param string $db Database name
  178. * @param string $export_type 'server', 'database', 'table'
  179. * @param string $db_alias Aliases of db
  180. *
  181. * @return bool Whether it succeeded
  182. */
  183. public function exportDBCreate($db, $export_type, $db_alias = '')
  184. {
  185. return true;
  186. }
  187. /**
  188. * Outputs the content of a table in CSV format
  189. *
  190. * @param string $db database name
  191. * @param string $table table name
  192. * @param string $crlf the end of line sequence
  193. * @param string $error_url the url to go back in case of error
  194. * @param string $sql_query SQL query for obtaining data
  195. * @param array $aliases Aliases of db/table/columns
  196. *
  197. * @return bool Whether it succeeded
  198. */
  199. public function exportData(
  200. $db,
  201. $table,
  202. $crlf,
  203. $error_url,
  204. $sql_query,
  205. array $aliases = array()
  206. ) {
  207. global $what, $csv_terminated, $csv_separator, $csv_enclosed, $csv_escaped;
  208. $db_alias = $db;
  209. $table_alias = $table;
  210. $this->initAlias($aliases, $db_alias, $table_alias);
  211. // Gets the data from the database
  212. $result = $GLOBALS['dbi']->query(
  213. $sql_query,
  214. DatabaseInterface::CONNECT_USER,
  215. DatabaseInterface::QUERY_UNBUFFERED
  216. );
  217. $fields_cnt = $GLOBALS['dbi']->numFields($result);
  218. // If required, get fields name at the first line
  219. if (isset($GLOBALS['csv_columns'])) {
  220. $schema_insert = '';
  221. for ($i = 0; $i < $fields_cnt; $i++) {
  222. $col_as = $GLOBALS['dbi']->fieldName($result, $i);
  223. if (!empty($aliases[$db]['tables'][$table]['columns'][$col_as])) {
  224. $col_as = $aliases[$db]['tables'][$table]['columns'][$col_as];
  225. }
  226. $col_as = stripslashes($col_as);
  227. if ($csv_enclosed == '') {
  228. $schema_insert .= $col_as;
  229. } else {
  230. $schema_insert .= $csv_enclosed
  231. . str_replace(
  232. $csv_enclosed,
  233. $csv_escaped . $csv_enclosed,
  234. $col_as
  235. )
  236. . $csv_enclosed;
  237. }
  238. $schema_insert .= $csv_separator;
  239. } // end for
  240. $schema_insert = trim(mb_substr($schema_insert, 0, -1));
  241. if (!Export::outputHandler($schema_insert . $csv_terminated)) {
  242. return false;
  243. }
  244. } // end if
  245. // Format the data
  246. while ($row = $GLOBALS['dbi']->fetchRow($result)) {
  247. $schema_insert = '';
  248. for ($j = 0; $j < $fields_cnt; $j++) {
  249. if (!isset($row[$j]) || is_null($row[$j])) {
  250. $schema_insert .= $GLOBALS[$what . '_null'];
  251. } elseif ($row[$j] == '0' || $row[$j] != '') {
  252. // always enclose fields
  253. if ($what == 'excel') {
  254. $row[$j] = preg_replace("/\015(\012)?/", "\012", $row[$j]);
  255. }
  256. // remove CRLF characters within field
  257. if (isset($GLOBALS[$what . '_removeCRLF'])
  258. && $GLOBALS[$what . '_removeCRLF']
  259. ) {
  260. $row[$j] = str_replace(
  261. "\n",
  262. "",
  263. str_replace(
  264. "\r",
  265. "",
  266. $row[$j]
  267. )
  268. );
  269. }
  270. if ($csv_enclosed == '') {
  271. $schema_insert .= $row[$j];
  272. } else {
  273. // also double the escape string if found in the data
  274. if ($csv_escaped != $csv_enclosed) {
  275. $schema_insert .= $csv_enclosed
  276. . str_replace(
  277. $csv_enclosed,
  278. $csv_escaped . $csv_enclosed,
  279. str_replace(
  280. $csv_escaped,
  281. $csv_escaped . $csv_escaped,
  282. $row[$j]
  283. )
  284. )
  285. . $csv_enclosed;
  286. } else {
  287. // avoid a problem when escape string equals enclose
  288. $schema_insert .= $csv_enclosed
  289. . str_replace(
  290. $csv_enclosed,
  291. $csv_escaped . $csv_enclosed,
  292. $row[$j]
  293. )
  294. . $csv_enclosed;
  295. }
  296. }
  297. } else {
  298. $schema_insert .= '';
  299. }
  300. if ($j < $fields_cnt - 1) {
  301. $schema_insert .= $csv_separator;
  302. }
  303. } // end for
  304. if (!Export::outputHandler($schema_insert . $csv_terminated)) {
  305. return false;
  306. }
  307. } // end while
  308. $GLOBALS['dbi']->freeResult($result);
  309. return true;
  310. }
  311. }